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