13 static int CmdHelp(const char *Cmd
);
15 int CmdAmp(const char *Cmd
)
17 int i
, rising
, falling
;
18 int max
= INT_MIN
, min
= INT_MAX
;
20 for (i
= 10; i
< GraphTraceLen
; ++i
) {
21 if (GraphBuffer
[i
] > max
)
23 if (GraphBuffer
[i
] < min
)
29 for (i
= 0; i
< GraphTraceLen
; ++i
) {
30 if (GraphBuffer
[i
+ 1] < GraphBuffer
[i
]) {
37 if (GraphBuffer
[i
+ 1] > GraphBuffer
[i
]) {
51 * Generic command to demodulate ASK.
53 * Argument is convention: positive or negative (High mod means zero
54 * or high mod means one)
56 * Updates the Graph trace with 0/1 values
61 int Cmdaskdemod(const char *Cmd
)
64 int c
, high
= 0, low
= 0;
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
);
70 /* Detect high and lows and clock */
72 for (i
= 0; i
< GraphTraceLen
; ++i
)
74 if (GraphBuffer
[i
] > high
)
75 high
= GraphBuffer
[i
];
76 else if (GraphBuffer
[i
] < low
)
79 if (c
!= 0 && c
!= 1) {
80 PrintAndLog("Invalid argument: %s", Cmd
);
84 if (GraphBuffer
[0] > 0) {
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
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
))){
104 GraphBuffer
[i
] = GraphBuffer
[i
- 1];
107 RepaintGraphWindow();
111 int CmdAutoCorr(const char *Cmd
)
113 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
115 int window
= atoi(Cmd
);
118 PrintAndLog("needs a window");
121 if (window
>= GraphTraceLen
) {
122 PrintAndLog("window must be smaller than trace (%d samples)",
127 PrintAndLog("performing %d correlations", GraphTraceLen
- window
);
129 for (int i
= 0; i
< GraphTraceLen
- window
; ++i
) {
131 for (int j
= 0; j
< window
; ++j
) {
132 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+ j
]) / 256;
134 CorrelBuffer
[i
] = sum
;
136 GraphTraceLen
= GraphTraceLen
- window
;
137 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
* sizeof (int));
139 RepaintGraphWindow();
143 int CmdBitsamples(const char *Cmd
)
148 for (int i
= 0; i
< n
; i
+= 12) {
149 UsbCommand c
= {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
, {i
, 0, 0}};
151 WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
);
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;
158 GraphBuffer
[cnt
++] = 0;
164 RepaintGraphWindow();
169 * Convert to a bitstream
171 int CmdBitstream(const char *Cmd
)
179 int hithigh
, hitlow
, first
;
181 /* Detect high and lows and clock */
182 for (i
= 0; i
< GraphTraceLen
; ++i
)
184 if (GraphBuffer
[i
] > high
)
185 high
= GraphBuffer
[i
];
186 else if (GraphBuffer
[i
] < low
)
187 low
= GraphBuffer
[i
];
191 clock
= GetClock(Cmd
, high
, 1);
195 for (i
= 0; i
< (int)(gtl
/ clock
); ++i
)
200 /* Find out if we hit both high and low peaks */
201 for (j
= 0; j
< clock
; ++j
)
203 if (GraphBuffer
[(i
* clock
) + j
] == high
)
205 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
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;
214 if (hithigh
&& hitlow
)
218 /* If we didn't hit both high and low peaks, we had a bit transition */
219 if (!hithigh
|| !hitlow
)
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;
229 RepaintGraphWindow();
233 int CmdBuffClear(const char *Cmd
)
235 UsbCommand c
= {CMD_BUFF_CLEAR
};
241 int CmdDec(const char *Cmd
)
243 for (int i
= 0; i
< (GraphTraceLen
/ 2); ++i
)
244 GraphBuffer
[i
] = GraphBuffer
[i
* 2];
246 PrintAndLog("decimated by 2");
247 RepaintGraphWindow();
251 /* Print our clock rate */
252 int CmdDetectClockRate(const char *Cmd
)
254 int clock
= DetectClock(0);
255 PrintAndLog("Auto-detected clock rate: %d", clock
);
259 int CmdFSKdemod(const char *Cmd
)
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
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,
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;
283 int minMark
= 0, maxMark
= 0;
285 for (i
= 0; i
< GraphTraceLen
- convLen
; ++i
) {
286 int lowSum
= 0, highSum
= 0;
288 for (j
= 0; j
< lowLen
; ++j
) {
289 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
291 for (j
= 0; j
< highLen
; ++j
) {
292 highSum
+= HighTone
[j
] * GraphBuffer
[i
+ j
];
294 lowSum
= abs(100 * lowSum
/ lowLen
);
295 highSum
= abs(100 * highSum
/ highLen
);
296 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
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);
305 for (j
= 0; j
< 8; j
++) {
306 highTot
+= (GraphBuffer
[i
+ j
] >> 16);
308 GraphBuffer
[i
] = lowTot
- highTot
;
309 if (GraphBuffer
[i
] > maxMark
) maxMark
= GraphBuffer
[i
];
310 if (GraphBuffer
[i
] < minMark
) minMark
= GraphBuffer
[i
];
313 GraphTraceLen
-= (convLen
+ 16);
314 RepaintGraphWindow();
316 // Find bit-sync (3 lo followed by 3 high)
317 int max
= 0, maxPos
= 0;
318 for (i
= 0; i
< 6000; ++i
) {
320 for (j
= 0; j
< 3 * lowLen
; ++j
) {
321 dec
-= GraphBuffer
[i
+ j
];
323 for (; j
< 3 * (lowLen
+ highLen
); ++j
) {
324 dec
+= GraphBuffer
[i
+ j
];
332 // place start of bit sync marker in graph
333 GraphBuffer
[maxPos
] = maxMark
;
334 GraphBuffer
[maxPos
+ 1] = minMark
;
338 // place end of bit sync marker in graph
339 GraphBuffer
[maxPos
] = maxMark
;
340 GraphBuffer
[maxPos
+1] = minMark
;
342 PrintAndLog("actual data bits start at sample %d", maxPos
);
343 PrintAndLog("length %d/%d", highLen
, lowLen
);
346 bits
[sizeof(bits
)-1] = '\0';
348 // find bit pairs and manchester decode them
349 for (i
= 0; i
< arraylen(bits
) - 1; ++i
) {
351 for (j
= 0; j
< lowLen
; ++j
) {
352 dec
-= GraphBuffer
[maxPos
+ j
];
354 for (; j
< lowLen
+ highLen
; ++j
) {
355 dec
+= GraphBuffer
[maxPos
+ j
];
358 // place inter bit marker in graph
359 GraphBuffer
[maxPos
] = maxMark
;
360 GraphBuffer
[maxPos
+ 1] = minMark
;
362 // hi and lo form a 64 bit pair
363 hi
= (hi
<< 1) | (lo
>> 31);
365 // store decoded bit as binary (in hi/lo) and text (in bits[])
373 PrintAndLog("bits: '%s'", bits
);
374 PrintAndLog("hex: %08x %08x", hi
, lo
);
378 int CmdGrid(const char *Cmd
)
380 sscanf(Cmd
, "%i %i", &PlotGridX
, &PlotGridY
);
381 RepaintGraphWindow();
385 int CmdHexsamples(const char *Cmd
)
390 sscanf(Cmd
, "%i %i", &requested
, &offset
);
391 if (offset
% 4 != 0) {
392 PrintAndLog("Offset must be a multiple of 4");
399 if (requested
== 0) {
406 for (int i
= offset
; i
< n
+offset
; i
+= 12) {
407 UsbCommand c
= {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
, {i
, 0, 0}};
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",
423 if (delivered
>= requested
)
426 if (delivered
>= requested
)
432 int CmdHide(const char *Cmd
)
438 int CmdHpf(const char *Cmd
)
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
;
449 RepaintGraphWindow();
453 int CmdSamples(const char *Cmd
)
458 n
= strtol(Cmd
, NULL
, 0);
460 if (n
> 16000) n
= 16000;
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}};
466 WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
);
467 for (int j
= 0; j
< 48; j
++) {
468 GraphBuffer
[cnt
++] = ((int)sample_buf
[j
]) - 128;
471 PrintAndLog("Done!\n");
473 RepaintGraphWindow();
477 int CmdLoad(const char *Cmd
)
479 FILE *f
= fopen(Cmd
+ 1, "r");
481 PrintAndLog("couldn't open '%s'", Cmd
+ 1);
487 while (fgets(line
, sizeof (line
), f
)) {
488 GraphBuffer
[GraphTraceLen
] = atoi(line
);
492 PrintAndLog("loaded %d samples", GraphTraceLen
);
493 RepaintGraphWindow();
497 int CmdLtrim(const char *Cmd
)
501 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
502 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
505 RepaintGraphWindow();
510 * Manchester demodulate a bitstream. The bitstream needs to be already in
511 * the GraphBuffer as 0 and 1 values
513 * Give the clock rate as argument in order to help the sync - the algorithm
514 * resyncs at each pulse anyway.
516 * Not optimized by any means, this is the 1st time I'm writing this type of
517 * routine, feel free to improve...
519 * 1st argument: clock rate (as number of samples per clock rate)
520 * Typical values can be 64, 32, 128...
522 int CmdManchesterDemod(const char *Cmd
)
530 int hithigh
, hitlow
, first
;
536 /* check if we're inverting output */
537 if (*(Cmd
+ 1) == 'i')
539 PrintAndLog("Inverting output");
544 while(*Cmd
== ' '); // in case a 2nd argument was given
547 /* Holds the decoded bitstream: each clock period contains 2 bits */
548 /* later simplified to 1 bit after manchester decoding. */
549 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
550 /* int BitStream[GraphTraceLen*2/clock+10]; */
552 /* But it does not work if compiling on WIndows: therefore we just allocate a */
554 int BitStream
[MAX_GRAPH_TRACE_LEN
];
556 /* Detect high and lows */
557 for (i
= 0; i
< GraphTraceLen
; i
++)
559 if (GraphBuffer
[i
] > high
)
560 high
= GraphBuffer
[i
];
561 else if (GraphBuffer
[i
] < low
)
562 low
= GraphBuffer
[i
];
566 clock
= GetClock(Cmd
, high
, 1);
568 int tolerance
= clock
/4;
570 /* Detect first transition */
571 /* Lo-Hi (arbitrary) */
572 /* skip to the first high */
573 for (i
= 0; i
< GraphTraceLen
; i
++)
574 if (GraphBuffer
[i
] == high
)
576 /* now look for the first low */
577 for (; i
< GraphTraceLen
; i
++)
579 if (GraphBuffer
[i
] == low
)
586 /* If we're not working with 1/0s, demod based off clock */
589 bit
= 0; /* We assume the 1st bit is zero, it may not be
590 * the case: this routine (I think) has an init problem.
593 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
599 /* Find out if we hit both high and low peaks */
600 for (j
= 0; j
< clock
; j
++)
602 if (GraphBuffer
[(i
* clock
) + j
] == high
)
604 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
607 /* it doesn't count if it's the first part of our read
608 because it's really just trailing from the last sequence */
609 if (first
&& (hithigh
|| hitlow
))
610 hithigh
= hitlow
= 0;
614 if (hithigh
&& hitlow
)
618 /* If we didn't hit both high and low peaks, we had a bit transition */
619 if (!hithigh
|| !hitlow
)
622 BitStream
[bit2idx
++] = bit
^ invert
;
626 /* standard 1/0 bitstream */
630 /* Then detect duration between 2 successive transitions */
631 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
633 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
638 // Error check: if bitidx becomes too large, we do not
639 // have a Manchester encoded bitstream or the clock is really
641 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
642 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
645 // Then switch depending on lc length:
646 // Tolerance is 1/4 of clock rate (arbitrary)
647 if (abs(lc
-clock
/2) < tolerance
) {
648 // Short pulse : either "1" or "0"
649 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
650 } else if (abs(lc
-clock
) < tolerance
) {
651 // Long pulse: either "11" or "00"
652 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
653 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
657 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
658 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
662 PrintAndLog("Error: too many detection errors, aborting.");
669 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
670 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
671 // to stop output at the final bitidx2 value, not bitidx
672 for (i
= 0; i
< bitidx
; i
+= 2) {
673 if ((BitStream
[i
] == 0) && (BitStream
[i
+1] == 1)) {
674 BitStream
[bit2idx
++] = 1 ^ invert
;
675 } else if ((BitStream
[i
] == 1) && (BitStream
[i
+1] == 0)) {
676 BitStream
[bit2idx
++] = 0 ^ invert
;
678 // We cannot end up in this state, this means we are unsynchronized,
682 PrintAndLog("Unsynchronized, resync...");
683 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
687 PrintAndLog("Error: too many decode errors, aborting.");
694 PrintAndLog("Manchester decoded bitstream");
695 // Now output the bitstream to the scrollback by line of 16 bits
696 for (i
= 0; i
< (bit2idx
-16); i
+=16) {
697 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
718 /* Modulate our data into manchester */
719 int CmdManchesterMod(const char *Cmd
)
723 int bit
, lastbit
, wave
;
726 clock
= GetClock(Cmd
, 0, 1);
730 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
732 bit
= GraphBuffer
[i
* clock
] ^ 1;
734 for (j
= 0; j
< (int)(clock
/2); j
++)
735 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
;
736 for (j
= (int)(clock
/2); j
< clock
; j
++)
737 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
^ 1;
739 /* Keep track of how we start our wave and if we changed or not this time */
740 wave
^= bit
^ lastbit
;
744 RepaintGraphWindow();
748 int CmdNorm(const char *Cmd
)
751 int max
= INT_MIN
, min
= INT_MAX
;
753 for (i
= 10; i
< GraphTraceLen
; ++i
) {
754 if (GraphBuffer
[i
] > max
)
755 max
= GraphBuffer
[i
];
756 if (GraphBuffer
[i
] < min
)
757 min
= GraphBuffer
[i
];
761 for (i
= 0; i
< GraphTraceLen
; ++i
) {
762 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 1000 /
766 RepaintGraphWindow();
770 int CmdPlot(const char *Cmd
)
776 int CmdSave(const char *Cmd
)
778 FILE *f
= fopen(Cmd
, "w");
780 PrintAndLog("couldn't open '%s'", Cmd
);
784 for (i
= 0; i
< GraphTraceLen
; i
++) {
785 fprintf(f
, "%d\n", GraphBuffer
[i
]);
788 PrintAndLog("saved to '%s'", Cmd
);
792 int CmdScale(const char *Cmd
)
794 CursorScaleFactor
= atoi(Cmd
);
795 if (CursorScaleFactor
== 0) {
796 PrintAndLog("bad, can't have zero scale");
797 CursorScaleFactor
= 1;
799 RepaintGraphWindow();
803 int CmdThreshold(const char *Cmd
)
805 int threshold
= atoi(Cmd
);
807 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
808 if (GraphBuffer
[i
] >= threshold
)
813 RepaintGraphWindow();
817 int CmdZerocrossings(const char *Cmd
)
819 // Zero-crossings aren't meaningful unless the signal is zero-mean.
826 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
827 if (GraphBuffer
[i
] * sign
>= 0) {
828 // No change in sign, reproduce the previous sample count.
830 GraphBuffer
[i
] = lastZc
;
832 // Change in sign, reset the sample count.
834 GraphBuffer
[i
] = lastZc
;
842 RepaintGraphWindow();
846 static command_t CommandTable
[] =
848 {"help", CmdHelp
, 1, "This help"},
849 {"amp", CmdAmp
, 1, "Amplify peaks"},
850 {"askdemod", Cmdaskdemod
, 1, "<0|1> -- Attempt to demodulate simple ASK tags"},
851 {"autocorr", CmdAutoCorr
, 1, "<window length> -- Autocorrelation over window"},
852 {"bitsamples", CmdBitsamples
, 0, "Get raw samples as bitstring"},
853 {"bitstream", CmdBitstream
, 1, "[clock rate] -- Convert waveform into a bitstream"},
854 {"buffclear", CmdBuffClear
, 1, "Clear sample buffer and graph window"},
855 {"dec", CmdDec
, 1, "Decimate samples"},
856 {"detectclock", CmdDetectClockRate
, 1, "Detect clock rate"},
857 {"fskdemod", CmdFSKdemod
, 1, "Demodulate graph window as a HID FSK"},
858 {"grid", CmdGrid
, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
859 {"hexsamples", CmdHexsamples
, 0, "<blocks> [<offset>] -- Dump big buffer as hex bytes"},
860 {"hide", CmdHide
, 1, "Hide graph window"},
861 {"hpf", CmdHpf
, 1, "Remove DC offset from trace"},
862 {"load", CmdLoad
, 1, "<filename> -- Load trace (to graph window"},
863 {"ltrim", CmdLtrim
, 1, "<samples> -- Trim samples from left of trace"},
864 {"mandemod", CmdManchesterDemod
, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
865 {"manmod", CmdManchesterMod
, 1, "[clock rate] -- Manchester modulate a binary stream"},
866 {"norm", CmdNorm
, 1, "Normalize max/min to +/-500"},
867 {"plot", CmdPlot
, 1, "Show graph window"},
868 {"samples", CmdSamples
, 0, "[128 - 16000] -- Get raw samples for graph window"},
869 {"save", CmdSave
, 1, "<filename> -- Save trace (from graph window)"},
870 {"scale", CmdScale
, 1, "<int> -- Set cursor display scale"},
871 {"threshold", CmdThreshold
, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
872 {"zerocrossings", CmdZerocrossings
, 1, "Count time between zero-crossings"},
873 {NULL
, NULL
, 0, NULL
}
876 int CmdData(const char *Cmd
)
878 CmdsParse(CommandTable
, Cmd
);
882 int CmdHelp(const char *Cmd
)
884 CmdsHelp(CommandTable
);