]>
Commit | Line | Data |
---|---|---|
7fe9b0b7 | 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 | ||
13 | static int CmdHelp(const char *Cmd); | |
14 | ||
15 | int 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 | */ | |
61 | int 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 | ||
111 | int 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 | ||
143 | int 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 | */ | |
171 | int 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 | ||
233 | int CmdBuffClear(const char *Cmd) | |
234 | { | |
235 | UsbCommand c = {CMD_BUFF_CLEAR}; | |
236 | SendCommand(&c); | |
237 | ClearGraph(true); | |
238 | return 0; | |
239 | } | |
240 | ||
241 | int 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 */ | |
252 | int CmdDetectClockRate(const char *Cmd) | |
253 | { | |
254 | int clock = DetectClock(0); | |
255 | PrintAndLog("Auto-detected clock rate: %d", clock); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | int 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 | ||
378 | int CmdGrid(const char *Cmd) | |
379 | { | |
380 | sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY); | |
381 | RepaintGraphWindow(); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | int 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 | ||
7fe9b0b7 | 432 | int CmdHide(const char *Cmd) |
433 | { | |
434 | HideGraphWindow(); | |
435 | return 0; | |
436 | } | |
437 | ||
438 | int 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 | ||
8d183c53 | 453 | int CmdSamples(const char *Cmd) |
7fe9b0b7 | 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 | ||
477 | int CmdLoad(const char *Cmd) | |
478 | { | |
33984c6a | 479 | FILE *f = fopen(Cmd + 1, "r"); |
7fe9b0b7 | 480 | if (!f) { |
33984c6a | 481 | PrintAndLog("couldn't open '%s'", Cmd + 1); |
7fe9b0b7 | 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 | ||
497 | int 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 | */ | |
522 | int 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 */ | |
fffad860 | 537 | if (*(Cmd + 1) == 'i') |
7fe9b0b7 | 538 | { |
539 | PrintAndLog("Inverting output"); | |
540 | invert = 1; | |
fffad860 | 541 | ++Cmd; |
7fe9b0b7 | 542 | do |
543 | ++Cmd; | |
544 | while(*Cmd == ' '); // in case a 2nd argument was given | |
545 | } | |
546 | ||
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]; */ | |
551 | ||
552 | /* But it does not work if compiling on WIndows: therefore we just allocate a */ | |
553 | /* large array */ | |
554 | int BitStream[MAX_GRAPH_TRACE_LEN]; | |
555 | ||
556 | /* Detect high and lows */ | |
557 | for (i = 0; i < GraphTraceLen; i++) | |
558 | { | |
559 | if (GraphBuffer[i] > high) | |
560 | high = GraphBuffer[i]; | |
561 | else if (GraphBuffer[i] < low) | |
562 | low = GraphBuffer[i]; | |
563 | } | |
564 | ||
565 | /* Get our clock */ | |
566 | clock = GetClock(Cmd, high, 1); | |
567 | ||
568 | int tolerance = clock/4; | |
569 | ||
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) | |
575 | break; | |
576 | /* now look for the first low */ | |
577 | for (; i < GraphTraceLen; i++) | |
578 | { | |
579 | if (GraphBuffer[i] == low) | |
580 | { | |
581 | lastval = i; | |
582 | break; | |
583 | } | |
584 | } | |
585 | ||
586 | /* If we're not working with 1/0s, demod based off clock */ | |
587 | if (high != 1) | |
588 | { | |
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. | |
591 | * Ed. | |
592 | */ | |
593 | for (; i < (int)(GraphTraceLen / clock); i++) | |
594 | { | |
595 | hithigh = 0; | |
596 | hitlow = 0; | |
597 | first = 1; | |
598 | ||
599 | /* Find out if we hit both high and low peaks */ | |
600 | for (j = 0; j < clock; j++) | |
601 | { | |
602 | if (GraphBuffer[(i * clock) + j] == high) | |
603 | hithigh = 1; | |
604 | else if (GraphBuffer[(i * clock) + j] == low) | |
605 | hitlow = 1; | |
606 | ||
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; | |
611 | else | |
612 | first = 0; | |
613 | ||
614 | if (hithigh && hitlow) | |
615 | break; | |
616 | } | |
617 | ||
618 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
619 | if (!hithigh || !hitlow) | |
620 | bit ^= 1; | |
621 | ||
622 | BitStream[bit2idx++] = bit ^ invert; | |
623 | } | |
624 | } | |
625 | ||
626 | /* standard 1/0 bitstream */ | |
627 | else | |
628 | { | |
629 | ||
630 | /* Then detect duration between 2 successive transitions */ | |
631 | for (bitidx = 1; i < GraphTraceLen; i++) | |
632 | { | |
633 | if (GraphBuffer[i-1] != GraphBuffer[i]) | |
634 | { | |
635 | lc = i-lastval; | |
636 | lastval = i; | |
637 | ||
638 | // Error check: if bitidx becomes too large, we do not | |
639 | // have a Manchester encoded bitstream or the clock is really | |
640 | // wrong! | |
641 | if (bitidx > (GraphTraceLen*2/clock+8) ) { | |
642 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
643 | return 0; | |
644 | } | |
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]; | |
654 | } else { | |
655 | // Error | |
656 | warnings++; | |
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)"); | |
659 | ||
660 | if (warnings > 10) | |
661 | { | |
662 | PrintAndLog("Error: too many detection errors, aborting."); | |
663 | return 0; | |
664 | } | |
665 | } | |
666 | } | |
667 | } | |
668 | ||
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; | |
677 | } else { | |
678 | // We cannot end up in this state, this means we are unsynchronized, | |
679 | // move up 1 bit: | |
680 | i++; | |
681 | warnings++; | |
682 | PrintAndLog("Unsynchronized, resync..."); | |
683 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
684 | ||
685 | if (warnings > 10) | |
686 | { | |
687 | PrintAndLog("Error: too many decode errors, aborting."); | |
688 | return 0; | |
689 | } | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
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", | |
698 | BitStream[i], | |
699 | BitStream[i+1], | |
700 | BitStream[i+2], | |
701 | BitStream[i+3], | |
702 | BitStream[i+4], | |
703 | BitStream[i+5], | |
704 | BitStream[i+6], | |
705 | BitStream[i+7], | |
706 | BitStream[i+8], | |
707 | BitStream[i+9], | |
708 | BitStream[i+10], | |
709 | BitStream[i+11], | |
710 | BitStream[i+12], | |
711 | BitStream[i+13], | |
712 | BitStream[i+14], | |
713 | BitStream[i+15]); | |
714 | } | |
715 | return 0; | |
716 | } | |
717 | ||
718 | /* Modulate our data into manchester */ | |
719 | int CmdManchesterMod(const char *Cmd) | |
720 | { | |
721 | int i, j; | |
722 | int clock; | |
723 | int bit, lastbit, wave; | |
724 | ||
725 | /* Get our clock */ | |
726 | clock = GetClock(Cmd, 0, 1); | |
727 | ||
728 | wave = 0; | |
729 | lastbit = 1; | |
730 | for (i = 0; i < (int)(GraphTraceLen / clock); i++) | |
731 | { | |
732 | bit = GraphBuffer[i * clock] ^ 1; | |
733 | ||
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; | |
738 | ||
739 | /* Keep track of how we start our wave and if we changed or not this time */ | |
740 | wave ^= bit ^ lastbit; | |
741 | lastbit = bit; | |
742 | } | |
743 | ||
744 | RepaintGraphWindow(); | |
745 | return 0; | |
746 | } | |
747 | ||
748 | int CmdNorm(const char *Cmd) | |
749 | { | |
750 | int i; | |
751 | int max = INT_MIN, min = INT_MAX; | |
752 | ||
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]; | |
758 | } | |
759 | ||
760 | if (max != min) { | |
761 | for (i = 0; i < GraphTraceLen; ++i) { | |
762 | GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 1000 / | |
763 | (max - min); | |
764 | } | |
765 | } | |
766 | RepaintGraphWindow(); | |
767 | return 0; | |
768 | } | |
769 | ||
770 | int CmdPlot(const char *Cmd) | |
771 | { | |
772 | ShowGraphWindow(); | |
773 | return 0; | |
774 | } | |
775 | ||
776 | int CmdSave(const char *Cmd) | |
777 | { | |
778 | FILE *f = fopen(Cmd, "w"); | |
779 | if(!f) { | |
780 | PrintAndLog("couldn't open '%s'", Cmd); | |
781 | return 0; | |
782 | } | |
783 | int i; | |
784 | for (i = 0; i < GraphTraceLen; i++) { | |
785 | fprintf(f, "%d\n", GraphBuffer[i]); | |
786 | } | |
787 | fclose(f); | |
788 | PrintAndLog("saved to '%s'", Cmd); | |
789 | return 0; | |
790 | } | |
791 | ||
792 | int CmdScale(const char *Cmd) | |
793 | { | |
794 | CursorScaleFactor = atoi(Cmd); | |
795 | if (CursorScaleFactor == 0) { | |
796 | PrintAndLog("bad, can't have zero scale"); | |
797 | CursorScaleFactor = 1; | |
798 | } | |
799 | RepaintGraphWindow(); | |
800 | return 0; | |
801 | } | |
802 | ||
803 | int CmdThreshold(const char *Cmd) | |
804 | { | |
805 | int threshold = atoi(Cmd); | |
806 | ||
807 | for (int i = 0; i < GraphTraceLen; ++i) { | |
808 | if (GraphBuffer[i] >= threshold) | |
809 | GraphBuffer[i] = 1; | |
810 | else | |
811 | GraphBuffer[i] =- 1; | |
812 | } | |
813 | RepaintGraphWindow(); | |
814 | return 0; | |
815 | } | |
816 | ||
817 | int CmdZerocrossings(const char *Cmd) | |
818 | { | |
819 | // Zero-crossings aren't meaningful unless the signal is zero-mean. | |
820 | CmdHpf(""); | |
821 | ||
822 | int sign = 1; | |
823 | int zc = 0; | |
824 | int lastZc = 0; | |
825 | ||
826 | for (int i = 0; i < GraphTraceLen; ++i) { | |
827 | if (GraphBuffer[i] * sign >= 0) { | |
828 | // No change in sign, reproduce the previous sample count. | |
829 | zc++; | |
830 | GraphBuffer[i] = lastZc; | |
831 | } else { | |
832 | // Change in sign, reset the sample count. | |
833 | sign = -sign; | |
834 | GraphBuffer[i] = lastZc; | |
835 | if (sign > 0) { | |
836 | lastZc = zc; | |
837 | zc = 0; | |
838 | } | |
839 | } | |
840 | } | |
841 | ||
842 | RepaintGraphWindow(); | |
843 | return 0; | |
844 | } | |
845 | ||
846 | static command_t CommandTable[] = | |
847 | { | |
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"}, | |
7fe9b0b7 | 860 | {"hide", CmdHide, 1, "Hide graph window"}, |
861 | {"hpf", CmdHpf, 1, "Remove DC offset from trace"}, | |
7fe9b0b7 | 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"}, | |
26aa7d73 | 868 | {"samples", CmdSamples, 0, "[128 - 16000] -- Get raw samples for graph window"}, |
7fe9b0b7 | 869 | {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"}, |
870 | {"scale", CmdScale, 1, "<int> -- Set cursor display scale"}, | |
dbf444a1 | 871 | {"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"}, |
7fe9b0b7 | 872 | {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"}, |
873 | {NULL, NULL, 0, NULL} | |
874 | }; | |
875 | ||
876 | int CmdData(const char *Cmd) | |
877 | { | |
878 | CmdsParse(CommandTable, Cmd); | |
879 | return 0; | |
880 | } | |
881 | ||
882 | int CmdHelp(const char *Cmd) | |
883 | { | |
884 | CmdsHelp(CommandTable); | |
885 | return 0; | |
886 | } |