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