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 CmdHFSamples(const char *Cmd
)
435 int n
= strtol(Cmd
, NULL
, 0);
443 for (int i
= 0; i
< n
; i
+= 12) {
444 UsbCommand c
= {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
, {i
, 0, 0}};
446 WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
);
447 for (int j
= 0; j
< 48; ++j
) {
448 GraphBuffer
[cnt
++] = (int)(sample_buf
[j
]);
452 RepaintGraphWindow();
456 int CmdHide(const char *Cmd
)
462 int CmdHpf(const char *Cmd
)
467 for (i
= 10; i
< GraphTraceLen
; ++i
)
468 accum
+= GraphBuffer
[i
];
469 accum
/= (GraphTraceLen
- 10);
470 for (i
= 0; i
< GraphTraceLen
; ++i
)
471 GraphBuffer
[i
] -= accum
;
473 RepaintGraphWindow();
477 int CmdLFSamples(const char *Cmd
)
482 n
= strtol(Cmd
, NULL
, 0);
484 if (n
> 16000) n
= 16000;
486 PrintAndLog("Reading %d samples\n", n
);
487 for (int i
= 0; i
< n
; i
+= 12) {
488 UsbCommand c
= {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
, {i
, 0, 0}};
490 WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
);
491 for (int j
= 0; j
< 48; j
++) {
492 GraphBuffer
[cnt
++] = ((int)sample_buf
[j
]) - 128;
495 PrintAndLog("Done!\n");
497 RepaintGraphWindow();
501 int CmdLoad(const char *Cmd
)
503 FILE *f
= fopen(Cmd
, "r");
505 PrintAndLog("couldn't open '%s'", Cmd
);
511 while (fgets(line
, sizeof (line
), f
)) {
512 GraphBuffer
[GraphTraceLen
] = atoi(line
);
516 PrintAndLog("loaded %d samples", GraphTraceLen
);
517 RepaintGraphWindow();
521 int CmdLtrim(const char *Cmd
)
525 for (int i
= ds
; i
< GraphTraceLen
; ++i
)
526 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
529 RepaintGraphWindow();
534 * Manchester demodulate a bitstream. The bitstream needs to be already in
535 * the GraphBuffer as 0 and 1 values
537 * Give the clock rate as argument in order to help the sync - the algorithm
538 * resyncs at each pulse anyway.
540 * Not optimized by any means, this is the 1st time I'm writing this type of
541 * routine, feel free to improve...
543 * 1st argument: clock rate (as number of samples per clock rate)
544 * Typical values can be 64, 32, 128...
546 int CmdManchesterDemod(const char *Cmd
)
554 int hithigh
, hitlow
, first
;
560 /* check if we're inverting output */
563 PrintAndLog("Inverting output");
567 while(*Cmd
== ' '); // in case a 2nd argument was given
570 /* Holds the decoded bitstream: each clock period contains 2 bits */
571 /* later simplified to 1 bit after manchester decoding. */
572 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
573 /* int BitStream[GraphTraceLen*2/clock+10]; */
575 /* But it does not work if compiling on WIndows: therefore we just allocate a */
577 int BitStream
[MAX_GRAPH_TRACE_LEN
];
579 /* Detect high and lows */
580 for (i
= 0; i
< GraphTraceLen
; i
++)
582 if (GraphBuffer
[i
] > high
)
583 high
= GraphBuffer
[i
];
584 else if (GraphBuffer
[i
] < low
)
585 low
= GraphBuffer
[i
];
589 clock
= GetClock(Cmd
, high
, 1);
591 int tolerance
= clock
/4;
593 /* Detect first transition */
594 /* Lo-Hi (arbitrary) */
595 /* skip to the first high */
596 for (i
= 0; i
< GraphTraceLen
; i
++)
597 if (GraphBuffer
[i
] == high
)
599 /* now look for the first low */
600 for (; i
< GraphTraceLen
; i
++)
602 if (GraphBuffer
[i
] == low
)
609 /* If we're not working with 1/0s, demod based off clock */
612 bit
= 0; /* We assume the 1st bit is zero, it may not be
613 * the case: this routine (I think) has an init problem.
616 for (; i
< (int)(GraphTraceLen
/ clock
); i
++)
622 /* Find out if we hit both high and low peaks */
623 for (j
= 0; j
< clock
; j
++)
625 if (GraphBuffer
[(i
* clock
) + j
] == high
)
627 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
630 /* it doesn't count if it's the first part of our read
631 because it's really just trailing from the last sequence */
632 if (first
&& (hithigh
|| hitlow
))
633 hithigh
= hitlow
= 0;
637 if (hithigh
&& hitlow
)
641 /* If we didn't hit both high and low peaks, we had a bit transition */
642 if (!hithigh
|| !hitlow
)
645 BitStream
[bit2idx
++] = bit
^ invert
;
649 /* standard 1/0 bitstream */
653 /* Then detect duration between 2 successive transitions */
654 for (bitidx
= 1; i
< GraphTraceLen
; i
++)
656 if (GraphBuffer
[i
-1] != GraphBuffer
[i
])
661 // Error check: if bitidx becomes too large, we do not
662 // have a Manchester encoded bitstream or the clock is really
664 if (bitidx
> (GraphTraceLen
*2/clock
+8) ) {
665 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
668 // Then switch depending on lc length:
669 // Tolerance is 1/4 of clock rate (arbitrary)
670 if (abs(lc
-clock
/2) < tolerance
) {
671 // Short pulse : either "1" or "0"
672 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
673 } else if (abs(lc
-clock
) < tolerance
) {
674 // Long pulse: either "11" or "00"
675 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
676 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
680 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
681 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
685 PrintAndLog("Error: too many detection errors, aborting.");
692 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
693 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
694 // to stop output at the final bitidx2 value, not bitidx
695 for (i
= 0; i
< bitidx
; i
+= 2) {
696 if ((BitStream
[i
] == 0) && (BitStream
[i
+1] == 1)) {
697 BitStream
[bit2idx
++] = 1 ^ invert
;
698 } else if ((BitStream
[i
] == 1) && (BitStream
[i
+1] == 0)) {
699 BitStream
[bit2idx
++] = 0 ^ invert
;
701 // We cannot end up in this state, this means we are unsynchronized,
705 PrintAndLog("Unsynchronized, resync...");
706 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
710 PrintAndLog("Error: too many decode errors, aborting.");
717 PrintAndLog("Manchester decoded bitstream");
718 // Now output the bitstream to the scrollback by line of 16 bits
719 for (i
= 0; i
< (bit2idx
-16); i
+=16) {
720 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
741 /* Modulate our data into manchester */
742 int CmdManchesterMod(const char *Cmd
)
746 int bit
, lastbit
, wave
;
749 clock
= GetClock(Cmd
, 0, 1);
753 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
755 bit
= GraphBuffer
[i
* clock
] ^ 1;
757 for (j
= 0; j
< (int)(clock
/2); j
++)
758 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
;
759 for (j
= (int)(clock
/2); j
< clock
; j
++)
760 GraphBuffer
[(i
* clock
) + j
] = bit
^ lastbit
^ wave
^ 1;
762 /* Keep track of how we start our wave and if we changed or not this time */
763 wave
^= bit
^ lastbit
;
767 RepaintGraphWindow();
771 int CmdNorm(const char *Cmd
)
774 int max
= INT_MIN
, min
= INT_MAX
;
776 for (i
= 10; i
< GraphTraceLen
; ++i
) {
777 if (GraphBuffer
[i
] > max
)
778 max
= GraphBuffer
[i
];
779 if (GraphBuffer
[i
] < min
)
780 min
= GraphBuffer
[i
];
784 for (i
= 0; i
< GraphTraceLen
; ++i
) {
785 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
) / 2)) * 1000 /
789 RepaintGraphWindow();
793 int CmdPlot(const char *Cmd
)
799 int CmdSave(const char *Cmd
)
801 FILE *f
= fopen(Cmd
, "w");
803 PrintAndLog("couldn't open '%s'", Cmd
);
807 for (i
= 0; i
< GraphTraceLen
; i
++) {
808 fprintf(f
, "%d\n", GraphBuffer
[i
]);
811 PrintAndLog("saved to '%s'", Cmd
);
815 int CmdScale(const char *Cmd
)
817 CursorScaleFactor
= atoi(Cmd
);
818 if (CursorScaleFactor
== 0) {
819 PrintAndLog("bad, can't have zero scale");
820 CursorScaleFactor
= 1;
822 RepaintGraphWindow();
826 int CmdThreshold(const char *Cmd
)
828 int threshold
= atoi(Cmd
);
830 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
831 if (GraphBuffer
[i
] >= threshold
)
836 RepaintGraphWindow();
840 int CmdZerocrossings(const char *Cmd
)
842 // Zero-crossings aren't meaningful unless the signal is zero-mean.
849 for (int i
= 0; i
< GraphTraceLen
; ++i
) {
850 if (GraphBuffer
[i
] * sign
>= 0) {
851 // No change in sign, reproduce the previous sample count.
853 GraphBuffer
[i
] = lastZc
;
855 // Change in sign, reset the sample count.
857 GraphBuffer
[i
] = lastZc
;
865 RepaintGraphWindow();
869 static command_t CommandTable
[] =
871 {"help", CmdHelp
, 1, "This help"},
872 {"amp", CmdAmp
, 1, "Amplify peaks"},
873 {"askdemod", Cmdaskdemod
, 1, "<0|1> -- Attempt to demodulate simple ASK tags"},
874 {"autocorr", CmdAutoCorr
, 1, "<window length> -- Autocorrelation over window"},
875 {"bitsamples", CmdBitsamples
, 0, "Get raw samples as bitstring"},
876 {"bitstream", CmdBitstream
, 1, "[clock rate] -- Convert waveform into a bitstream"},
877 {"buffclear", CmdBuffClear
, 1, "Clear sample buffer and graph window"},
878 {"dec", CmdDec
, 1, "Decimate samples"},
879 {"detectclock", CmdDetectClockRate
, 1, "Detect clock rate"},
880 {"fskdemod", CmdFSKdemod
, 1, "Demodulate graph window as a HID FSK"},
881 {"grid", CmdGrid
, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
882 {"hexsamples", CmdHexsamples
, 0, "<blocks> [<offset>] -- Dump big buffer as hex bytes"},
883 {"hfsamples", CmdHFSamples
, 0, "[nb of samples] Get raw samples for HF tag"},
884 {"hide", CmdHide
, 1, "Hide graph window"},
885 {"hpf", CmdHpf
, 1, "Remove DC offset from trace"},
886 {"lfsamples", CmdLFSamples
, 0, "[128 - 16000] -- Get raw samples for LF tag"},
887 {"load", CmdLoad
, 1, "<filename> -- Load trace (to graph window"},
888 {"ltrim", CmdLtrim
, 1, "<samples> -- Trim samples from left of trace"},
889 {"mandemod", CmdManchesterDemod
, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
890 {"manmod", CmdManchesterMod
, 1, "[clock rate] -- Manchester modulate a binary stream"},
891 {"norm", CmdNorm
, 1, "Normalize max/min to +/-500"},
892 {"plot", CmdPlot
, 1, "Show graph window"},
893 {"save", CmdSave
, 1, "<filename> -- Save trace (from graph window)"},
894 {"scale", CmdScale
, 1, "<int> -- Set cursor display scale"},
895 {"threshold", CmdThreshold
, 1, "Maximize/minimize every value in the graph window depending on threshold"},
896 {"zerocrossings", CmdZerocrossings
, 1, "Count time between zero-crossings"},
897 {NULL
, NULL
, 0, NULL
}
900 int CmdData(const char *Cmd
)
902 CmdsParse(CommandTable
, Cmd
);
906 int CmdHelp(const char *Cmd
)
908 CmdsHelp(CommandTable
);