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