]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmddata.c
Applied patch from http://www.proxmark.org/forum/viewtopic.php?id=1637, where a compi...
[proxmark3-svn] / client / cmddata.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// Data and Graph commands
9//-----------------------------------------------------------------------------
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <limits.h>
15#include "proxmark3.h"
16#include "data.h"
17#include "ui.h"
18#include "graph.h"
19#include "cmdparser.h"
20#include "cmdmain.h"
21#include "cmddata.h"
22
23static int CmdHelp(const char *Cmd);
24
25int CmdAmp(const char *Cmd)
26{
27 int i, rising, falling;
28 int max = INT_MIN, min = INT_MAX;
29
30 for (i = 10; i < GraphTraceLen; ++i) {
31 if (GraphBuffer[i] > max)
32 max = GraphBuffer[i];
33 if (GraphBuffer[i] < min)
34 min = GraphBuffer[i];
35 }
36
37 if (max != min) {
38 rising = falling= 0;
39 for (i = 0; i < GraphTraceLen; ++i) {
40 if (GraphBuffer[i + 1] < GraphBuffer[i]) {
41 if (rising) {
42 GraphBuffer[i] = max;
43 rising = 0;
44 }
45 falling = 1;
46 }
47 if (GraphBuffer[i + 1] > GraphBuffer[i]) {
48 if (falling) {
49 GraphBuffer[i] = min;
50 falling = 0;
51 }
52 rising= 1;
53 }
54 }
55 }
56 RepaintGraphWindow();
57 return 0;
58}
59
60/*
61 * Generic command to demodulate ASK.
62 *
63 * Argument is convention: positive or negative (High mod means zero
64 * or high mod means one)
65 *
66 * Updates the Graph trace with 0/1 values
67 *
68 * Arguments:
69 * c : 0 or 1
70 */
71int Cmdaskdemod(const char *Cmd)
72{
73 int i;
74 int c, high = 0, low = 0;
75
76 // TODO: complain if we do not give 2 arguments here !
77 // (AL - this doesn't make sense! we're only using one argument!!!)
78 sscanf(Cmd, "%i", &c);
79
80 /* Detect high and lows and clock */
81 // (AL - clock???)
82 for (i = 0; i < GraphTraceLen; ++i)
83 {
84 if (GraphBuffer[i] > high)
85 high = GraphBuffer[i];
86 else if (GraphBuffer[i] < low)
87 low = GraphBuffer[i];
88 }
89 if (c != 0 && c != 1) {
90 PrintAndLog("Invalid argument: %s", Cmd);
91 return 0;
92 }
93
94 if (GraphBuffer[0] > 0) {
95 GraphBuffer[0] = 1-c;
96 } else {
97 GraphBuffer[0] = c;
98 }
99 for (i = 1; i < GraphTraceLen; ++i) {
100 /* Transitions are detected at each peak
101 * Transitions are either:
102 * - we're low: transition if we hit a high
103 * - we're high: transition if we hit a low
104 * (we need to do it this way because some tags keep high or
105 * low for long periods, others just reach the peak and go
106 * down)
107 */
108 if ((GraphBuffer[i] == high) && (GraphBuffer[i - 1] == c)) {
109 GraphBuffer[i] = 1 - c;
110 } else if ((GraphBuffer[i] == low) && (GraphBuffer[i - 1] == (1 - c))){
111 GraphBuffer[i] = c;
112 } else {
113 /* No transition */
114 GraphBuffer[i] = GraphBuffer[i - 1];
115 }
116 }
117 RepaintGraphWindow();
118 return 0;
119}
120
121int CmdAutoCorr(const char *Cmd)
122{
123 static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];
124
125 int window = atoi(Cmd);
126
127 if (window == 0) {
128 PrintAndLog("needs a window");
129 return 0;
130 }
131 if (window >= GraphTraceLen) {
132 PrintAndLog("window must be smaller than trace (%d samples)",
133 GraphTraceLen);
134 return 0;
135 }
136
137 PrintAndLog("performing %d correlations", GraphTraceLen - window);
138
139 for (int i = 0; i < GraphTraceLen - window; ++i) {
140 int sum = 0;
141 for (int j = 0; j < window; ++j) {
142 sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256;
143 }
144 CorrelBuffer[i] = sum;
145 }
146 GraphTraceLen = GraphTraceLen - window;
147 memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int));
148
149 RepaintGraphWindow();
150 return 0;
151}
152
153int CmdBitsamples(const char *Cmd)
154{
155 int cnt = 0;
156 uint8_t got[12288];
157
158 GetFromBigBuf(got,sizeof(got),0);
159 WaitForResponse(CMD_ACK,NULL);
160
161 for (int j = 0; j < sizeof(got); j++) {
162 for (int k = 0; k < 8; k++) {
163 if(got[j] & (1 << (7 - k))) {
164 GraphBuffer[cnt++] = 1;
165 } else {
166 GraphBuffer[cnt++] = 0;
167 }
168 }
169 }
170 GraphTraceLen = cnt;
171 RepaintGraphWindow();
172 return 0;
173}
174
175/*
176 * Convert to a bitstream
177 */
178int CmdBitstream(const char *Cmd)
179{
180 int i, j;
181 int bit;
182 int gtl;
183 int clock;
184 int low = 0;
185 int high = 0;
186 int hithigh, hitlow, first;
187
188 /* Detect high and lows and clock */
189 for (i = 0; i < GraphTraceLen; ++i)
190 {
191 if (GraphBuffer[i] > high)
192 high = GraphBuffer[i];
193 else if (GraphBuffer[i] < low)
194 low = GraphBuffer[i];
195 }
196
197 /* Get our clock */
198 clock = GetClock(Cmd, high, 1);
199 gtl = ClearGraph(0);
200
201 bit = 0;
202 for (i = 0; i < (int)(gtl / clock); ++i)
203 {
204 hithigh = 0;
205 hitlow = 0;
206 first = 1;
207 /* Find out if we hit both high and low peaks */
208 for (j = 0; j < clock; ++j)
209 {
210 if (GraphBuffer[(i * clock) + j] == high)
211 hithigh = 1;
212 else if (GraphBuffer[(i * clock) + j] == low)
213 hitlow = 1;
214 /* it doesn't count if it's the first part of our read
215 because it's really just trailing from the last sequence */
216 if (first && (hithigh || hitlow))
217 hithigh = hitlow = 0;
218 else
219 first = 0;
220
221 if (hithigh && hitlow)
222 break;
223 }
224
225 /* If we didn't hit both high and low peaks, we had a bit transition */
226 if (!hithigh || !hitlow)
227 bit ^= 1;
228
229 AppendGraph(0, clock, bit);
230// for (j = 0; j < (int)(clock/2); j++)
231// GraphBuffer[(i * clock) + j] = bit ^ 1;
232// for (j = (int)(clock/2); j < clock; j++)
233// GraphBuffer[(i * clock) + j] = bit;
234 }
235
236 RepaintGraphWindow();
237 return 0;
238}
239
240int CmdBuffClear(const char *Cmd)
241{
242 UsbCommand c = {CMD_BUFF_CLEAR};
243 SendCommand(&c);
244 ClearGraph(true);
245 return 0;
246}
247
248int CmdDec(const char *Cmd)
249{
250 for (int i = 0; i < (GraphTraceLen / 2); ++i)
251 GraphBuffer[i] = GraphBuffer[i * 2];
252 GraphTraceLen /= 2;
253 PrintAndLog("decimated by 2");
254 RepaintGraphWindow();
255 return 0;
256}
257
258/* Print our clock rate */
259int CmdDetectClockRate(const char *Cmd)
260{
261 int clock = DetectClock(0);
262 PrintAndLog("Auto-detected clock rate: %d", clock);
263 return 0;
264}
265
266int CmdFSKdemod(const char *Cmd)
267{
268 static const int LowTone[] = {
269 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
270 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
271 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
272 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
273 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
274 };
275 static const int HighTone[] = {
276 1, 1, 1, 1, 1, -1, -1, -1, -1,
277 1, 1, 1, 1, -1, -1, -1, -1,
278 1, 1, 1, 1, -1, -1, -1, -1,
279 1, 1, 1, 1, -1, -1, -1, -1,
280 1, 1, 1, 1, -1, -1, -1, -1,
281 1, 1, 1, 1, -1, -1, -1, -1, -1,
282 };
283
284 int lowLen = sizeof (LowTone) / sizeof (int);
285 int highLen = sizeof (HighTone) / sizeof (int);
286 int convLen = (highLen > lowLen) ? highLen : lowLen;
287 uint32_t hi = 0, lo = 0;
288
289 int i, j;
290 int minMark = 0, maxMark = 0;
291
292 for (i = 0; i < GraphTraceLen - convLen; ++i) {
293 int lowSum = 0, highSum = 0;
294
295 for (j = 0; j < lowLen; ++j) {
296 lowSum += LowTone[j]*GraphBuffer[i+j];
297 }
298 for (j = 0; j < highLen; ++j) {
299 highSum += HighTone[j] * GraphBuffer[i + j];
300 }
301 lowSum = abs(100 * lowSum / lowLen);
302 highSum = abs(100 * highSum / highLen);
303 GraphBuffer[i] = (highSum << 16) | lowSum;
304 }
305
306 for(i = 0; i < GraphTraceLen - convLen - 16; ++i) {
307 int lowTot = 0, highTot = 0;
308 // 10 and 8 are f_s divided by f_l and f_h, rounded
309 for (j = 0; j < 10; ++j) {
310 lowTot += (GraphBuffer[i+j] & 0xffff);
311 }
312 for (j = 0; j < 8; j++) {
313 highTot += (GraphBuffer[i + j] >> 16);
314 }
315 GraphBuffer[i] = lowTot - highTot;
316 if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i];
317 if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i];
318 }
319
320 GraphTraceLen -= (convLen + 16);
321 RepaintGraphWindow();
322
323 // Find bit-sync (3 lo followed by 3 high)
324 int max = 0, maxPos = 0;
325 for (i = 0; i < 6000; ++i) {
326 int dec = 0;
327 for (j = 0; j < 3 * lowLen; ++j) {
328 dec -= GraphBuffer[i + j];
329 }
330 for (; j < 3 * (lowLen + highLen ); ++j) {
331 dec += GraphBuffer[i + j];
332 }
333 if (dec > max) {
334 max = dec;
335 maxPos = i;
336 }
337 }
338
339 // place start of bit sync marker in graph
340 GraphBuffer[maxPos] = maxMark;
341 GraphBuffer[maxPos + 1] = minMark;
342
343 maxPos += j;
344
345 // place end of bit sync marker in graph
346 GraphBuffer[maxPos] = maxMark;
347 GraphBuffer[maxPos+1] = minMark;
348
349 PrintAndLog("actual data bits start at sample %d", maxPos);
350 PrintAndLog("length %d/%d", highLen, lowLen);
351
352 uint8_t bits[46];
353 bits[sizeof(bits)-1] = '\0';
354
355 // find bit pairs and manchester decode them
356 for (i = 0; i < arraylen(bits) - 1; ++i) {
357 int dec = 0;
358 for (j = 0; j < lowLen; ++j) {
359 dec -= GraphBuffer[maxPos + j];
360 }
361 for (; j < lowLen + highLen; ++j) {
362 dec += GraphBuffer[maxPos + j];
363 }
364 maxPos += j;
365 // place inter bit marker in graph
366 GraphBuffer[maxPos] = maxMark;
367 GraphBuffer[maxPos + 1] = minMark;
368
369 // hi and lo form a 64 bit pair
370 hi = (hi << 1) | (lo >> 31);
371 lo = (lo << 1);
372 // store decoded bit as binary (in hi/lo) and text (in bits[])
373 if(dec < 0) {
374 bits[i] = '1';
375 lo |= 1;
376 } else {
377 bits[i] = '0';
378 }
379 }
380 PrintAndLog("bits: '%s'", bits);
381 PrintAndLog("hex: %08x %08x", hi, lo);
382 return 0;
383}
384
385int CmdGrid(const char *Cmd)
386{
387 sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
388 PlotGridXdefault= PlotGridX;
389 PlotGridYdefault= PlotGridY;
390 RepaintGraphWindow();
391 return 0;
392}
393
394int CmdHexsamples(const char *Cmd)
395{
396 int n;
397 int requested = 0;
398 int offset = 0;
399 sscanf(Cmd, "%i %i", &requested, &offset);
400
401 int delivered = 0;
402 uint8_t got[40000];
403
404 /* round up to nearest 8 bytes so the printed data is all valid */
405 if (requested < 8) {
406 requested = 8;
407 }
408 if (requested % 8 != 0) {
409 int remainder = requested % 8;
410 requested = requested + 8 - remainder;
411 }
412 if (offset + requested > sizeof(got)) {
413 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 40000");
414 return 0;
415 } else {
416 n = requested;
417 }
418
419 GetFromBigBuf(got,n,offset);
420 WaitForResponse(CMD_ACK,NULL);
421
422 for (int j = 0; j < n; j += 8) {
423 PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x",
424 sample_buf[j+0],
425 sample_buf[j+1],
426 sample_buf[j+2],
427 sample_buf[j+3],
428 sample_buf[j+4],
429 sample_buf[j+5],
430 sample_buf[j+6],
431 sample_buf[j+7]
432 );
433 delivered += 8;
434 if (delivered >= requested)
435 break;
436 }
437 return 0;
438}
439
440int CmdHide(const char *Cmd)
441{
442 HideGraphWindow();
443 return 0;
444}
445
446int CmdHpf(const char *Cmd)
447{
448 int i;
449 int accum = 0;
450
451 for (i = 10; i < GraphTraceLen; ++i)
452 accum += GraphBuffer[i];
453 accum /= (GraphTraceLen - 10);
454 for (i = 0; i < GraphTraceLen; ++i)
455 GraphBuffer[i] -= accum;
456
457 RepaintGraphWindow();
458 return 0;
459}
460
461int CmdSamples(const char *Cmd)
462{
463 int cnt = 0;
464 int n;
465 uint8_t got[40000];
466
467 n = strtol(Cmd, NULL, 0);
468 if (n == 0) n = 512;
469 if (n > sizeof(got)) n = sizeof(got);
470
471 PrintAndLog("Reading %d samples\n", n);
472 GetFromBigBuf(got,n,0);
473 WaitForResponse(CMD_ACK,NULL);
474 for (int j = 0; j < n; j++) {
475 GraphBuffer[cnt++] = ((int)got[j]) - 128;
476 }
477
478 PrintAndLog("Done!\n");
479 GraphTraceLen = n;
480 RepaintGraphWindow();
481 return 0;
482}
483
484int CmdLoad(const char *Cmd)
485{
486 FILE *f = fopen(Cmd, "r");
487 if (!f) {
488 PrintAndLog("couldn't open '%s'", Cmd);
489 return 0;
490 }
491
492 GraphTraceLen = 0;
493 char line[80];
494 while (fgets(line, sizeof (line), f)) {
495 GraphBuffer[GraphTraceLen] = atoi(line);
496 GraphTraceLen++;
497 }
498 fclose(f);
499 PrintAndLog("loaded %d samples", GraphTraceLen);
500 RepaintGraphWindow();
501 return 0;
502}
503
504int CmdLtrim(const char *Cmd)
505{
506 int ds = atoi(Cmd);
507
508 for (int i = ds; i < GraphTraceLen; ++i)
509 GraphBuffer[i-ds] = GraphBuffer[i];
510 GraphTraceLen -= ds;
511
512 RepaintGraphWindow();
513 return 0;
514}
515
516/*
517 * Manchester demodulate a bitstream. The bitstream needs to be already in
518 * the GraphBuffer as 0 and 1 values
519 *
520 * Give the clock rate as argument in order to help the sync - the algorithm
521 * resyncs at each pulse anyway.
522 *
523 * Not optimized by any means, this is the 1st time I'm writing this type of
524 * routine, feel free to improve...
525 *
526 * 1st argument: clock rate (as number of samples per clock rate)
527 * Typical values can be 64, 32, 128...
528 */
529int CmdManchesterDemod(const char *Cmd)
530{
531 int i, j, invert= 0;
532 int bit;
533 int clock;
534 int lastval = 0;
535 int low = 0;
536 int high = 0;
537 int hithigh, hitlow, first;
538 int lc = 0;
539 int bitidx = 0;
540 int bit2idx = 0;
541 int warnings = 0;
542
543 /* check if we're inverting output */
544 if (*Cmd == 'i')
545 {
546 PrintAndLog("Inverting output");
547 invert = 1;
548 ++Cmd;
549 do
550 ++Cmd;
551 while(*Cmd == ' '); // in case a 2nd argument was given
552 }
553
554 /* Holds the decoded bitstream: each clock period contains 2 bits */
555 /* later simplified to 1 bit after manchester decoding. */
556 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
557 /* int BitStream[GraphTraceLen*2/clock+10]; */
558
559 /* But it does not work if compiling on WIndows: therefore we just allocate a */
560 /* large array */
561 uint8_t BitStream[MAX_GRAPH_TRACE_LEN];
562
563 /* Detect high and lows */
564 for (i = 0; i < GraphTraceLen; i++)
565 {
566 if (GraphBuffer[i] > high)
567 high = GraphBuffer[i];
568 else if (GraphBuffer[i] < low)
569 low = GraphBuffer[i];
570 }
571
572 /* Get our clock */
573 clock = GetClock(Cmd, high, 1);
574
575 int tolerance = clock/4;
576
577 /* Detect first transition */
578 /* Lo-Hi (arbitrary) */
579 /* skip to the first high */
580 for (i= 0; i < GraphTraceLen; i++)
581 if (GraphBuffer[i] == high)
582 break;
583 /* now look for the first low */
584 for (; i < GraphTraceLen; i++)
585 {
586 if (GraphBuffer[i] == low)
587 {
588 lastval = i;
589 break;
590 }
591 }
592
593 /* If we're not working with 1/0s, demod based off clock */
594 if (high != 1)
595 {
596 bit = 0; /* We assume the 1st bit is zero, it may not be
597 * the case: this routine (I think) has an init problem.
598 * Ed.
599 */
600 for (; i < (int)(GraphTraceLen / clock); i++)
601 {
602 hithigh = 0;
603 hitlow = 0;
604 first = 1;
605
606 /* Find out if we hit both high and low peaks */
607 for (j = 0; j < clock; j++)
608 {
609 if (GraphBuffer[(i * clock) + j] == high)
610 hithigh = 1;
611 else if (GraphBuffer[(i * clock) + j] == low)
612 hitlow = 1;
613
614 /* it doesn't count if it's the first part of our read
615 because it's really just trailing from the last sequence */
616 if (first && (hithigh || hitlow))
617 hithigh = hitlow = 0;
618 else
619 first = 0;
620
621 if (hithigh && hitlow)
622 break;
623 }
624
625 /* If we didn't hit both high and low peaks, we had a bit transition */
626 if (!hithigh || !hitlow)
627 bit ^= 1;
628
629 BitStream[bit2idx++] = bit ^ invert;
630 }
631 }
632
633 /* standard 1/0 bitstream */
634 else
635 {
636
637 /* Then detect duration between 2 successive transitions */
638 for (bitidx = 1; i < GraphTraceLen; i++)
639 {
640 if (GraphBuffer[i-1] != GraphBuffer[i])
641 {
642 lc = i-lastval;
643 lastval = i;
644
645 // Error check: if bitidx becomes too large, we do not
646 // have a Manchester encoded bitstream or the clock is really
647 // wrong!
648 if (bitidx > (GraphTraceLen*2/clock+8) ) {
649 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
650 return 0;
651 }
652 // Then switch depending on lc length:
653 // Tolerance is 1/4 of clock rate (arbitrary)
654 if (abs(lc-clock/2) < tolerance) {
655 // Short pulse : either "1" or "0"
656 BitStream[bitidx++]=GraphBuffer[i-1];
657 } else if (abs(lc-clock) < tolerance) {
658 // Long pulse: either "11" or "00"
659 BitStream[bitidx++]=GraphBuffer[i-1];
660 BitStream[bitidx++]=GraphBuffer[i-1];
661 } else {
662 // Error
663 warnings++;
664 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
665 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
666
667 if (warnings > 10)
668 {
669 PrintAndLog("Error: too many detection errors, aborting.");
670 return 0;
671 }
672 }
673 }
674 }
675
676 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
677 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
678 // to stop output at the final bitidx2 value, not bitidx
679 for (i = 0; i < bitidx; i += 2) {
680 if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) {
681 BitStream[bit2idx++] = 1 ^ invert;
682 } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) {
683 BitStream[bit2idx++] = 0 ^ invert;
684 } else {
685 // We cannot end up in this state, this means we are unsynchronized,
686 // move up 1 bit:
687 i++;
688 warnings++;
689 PrintAndLog("Unsynchronized, resync...");
690 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
691
692 if (warnings > 10)
693 {
694 PrintAndLog("Error: too many decode errors, aborting.");
695 return 0;
696 }
697 }
698 }
699 }
700
701 PrintAndLog("Manchester decoded bitstream");
702 // Now output the bitstream to the scrollback by line of 16 bits
703 for (i = 0; i < (bit2idx-16); i+=16) {
704 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
705 BitStream[i],
706 BitStream[i+1],
707 BitStream[i+2],
708 BitStream[i+3],
709 BitStream[i+4],
710 BitStream[i+5],
711 BitStream[i+6],
712 BitStream[i+7],
713 BitStream[i+8],
714 BitStream[i+9],
715 BitStream[i+10],
716 BitStream[i+11],
717 BitStream[i+12],
718 BitStream[i+13],
719 BitStream[i+14],
720 BitStream[i+15]);
721 }
722 return 0;
723}
724
725/* Modulate our data into manchester */
726int CmdManchesterMod(const char *Cmd)
727{
728 int i, j;
729 int clock;
730 int bit, lastbit, wave;
731
732 /* Get our clock */
733 clock = GetClock(Cmd, 0, 1);
734
735 wave = 0;
736 lastbit = 1;
737 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
738 {
739 bit = GraphBuffer[i * clock] ^ 1;
740
741 for (j = 0; j < (int)(clock/2); j++)
742 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave;
743 for (j = (int)(clock/2); j < clock; j++)
744 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1;
745
746 /* Keep track of how we start our wave and if we changed or not this time */
747 wave ^= bit ^ lastbit;
748 lastbit = bit;
749 }
750
751 RepaintGraphWindow();
752 return 0;
753}
754
755int CmdNorm(const char *Cmd)
756{
757 int i;
758 int max = INT_MIN, min = INT_MAX;
759
760 for (i = 10; i < GraphTraceLen; ++i) {
761 if (GraphBuffer[i] > max)
762 max = GraphBuffer[i];
763 if (GraphBuffer[i] < min)
764 min = GraphBuffer[i];
765 }
766
767 if (max != min) {
768 for (i = 0; i < GraphTraceLen; ++i) {
769 GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 1000 /
770 (max - min);
771 }
772 }
773 RepaintGraphWindow();
774 return 0;
775}
776
777int CmdPlot(const char *Cmd)
778{
779 ShowGraphWindow();
780 return 0;
781}
782
783int CmdSave(const char *Cmd)
784{
785 FILE *f = fopen(Cmd, "w");
786 if(!f) {
787 PrintAndLog("couldn't open '%s'", Cmd);
788 return 0;
789 }
790 int i;
791 for (i = 0; i < GraphTraceLen; i++) {
792 fprintf(f, "%d\n", GraphBuffer[i]);
793 }
794 fclose(f);
795 PrintAndLog("saved to '%s'", Cmd);
796 return 0;
797}
798
799int CmdScale(const char *Cmd)
800{
801 CursorScaleFactor = atoi(Cmd);
802 if (CursorScaleFactor == 0) {
803 PrintAndLog("bad, can't have zero scale");
804 CursorScaleFactor = 1;
805 }
806 RepaintGraphWindow();
807 return 0;
808}
809
810int CmdThreshold(const char *Cmd)
811{
812 int threshold = atoi(Cmd);
813
814 for (int i = 0; i < GraphTraceLen; ++i) {
815 if (GraphBuffer[i] >= threshold)
816 GraphBuffer[i] = 1;
817 else
818 GraphBuffer[i] =- 1;
819 }
820 RepaintGraphWindow();
821 return 0;
822}
823
824int CmdZerocrossings(const char *Cmd)
825{
826 // Zero-crossings aren't meaningful unless the signal is zero-mean.
827 CmdHpf("");
828
829 int sign = 1;
830 int zc = 0;
831 int lastZc = 0;
832
833 for (int i = 0; i < GraphTraceLen; ++i) {
834 if (GraphBuffer[i] * sign >= 0) {
835 // No change in sign, reproduce the previous sample count.
836 zc++;
837 GraphBuffer[i] = lastZc;
838 } else {
839 // Change in sign, reset the sample count.
840 sign = -sign;
841 GraphBuffer[i] = lastZc;
842 if (sign > 0) {
843 lastZc = zc;
844 zc = 0;
845 }
846 }
847 }
848
849 RepaintGraphWindow();
850 return 0;
851}
852
853static command_t CommandTable[] =
854{
855 {"help", CmdHelp, 1, "This help"},
856 {"amp", CmdAmp, 1, "Amplify peaks"},
857 {"askdemod", Cmdaskdemod, 1, "<0|1> -- Attempt to demodulate simple ASK tags"},
858 {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"},
859 {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"},
860 {"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},
861 {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"},
862 {"dec", CmdDec, 1, "Decimate samples"},
863 {"detectclock", CmdDetectClockRate, 1, "Detect clock rate"},
864 {"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
865 {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
866 {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
867 {"hide", CmdHide, 1, "Hide graph window"},
868 {"hpf", CmdHpf, 1, "Remove DC offset from trace"},
869 {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"},
870 {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"},
871 {"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
872 {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"},
873 {"norm", CmdNorm, 1, "Normalize max/min to +/-500"},
874 {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"},
875 {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window"},
876 {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"},
877 {"scale", CmdScale, 1, "<int> -- Set cursor display scale"},
878 {"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
879 {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"},
880 {NULL, NULL, 0, NULL}
881};
882
883int CmdData(const char *Cmd)
884{
885 CmdsParse(CommandTable, Cmd);
886 return 0;
887}
888
889int CmdHelp(const char *Cmd)
890{
891 CmdsHelp(CommandTable);
892 return 0;
893}
Impressum, Datenschutz