]>
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 | ||
432 | int CmdHFSamples(const char *Cmd) | |
433 | { | |
434 | int cnt = 0; | |
435 | int n = strtol(Cmd, NULL, 0); | |
436 | ||
437 | if(n == 0) { | |
438 | n = 1000; | |
439 | } else { | |
440 | n/= 4; | |
441 | } | |
442 | ||
443 | for (int i = 0; i < n; i += 12) { | |
444 | UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}}; | |
445 | SendCommand(&c); | |
446 | WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K); | |
447 | for (int j = 0; j < 48; ++j) { | |
448 | GraphBuffer[cnt++] = (int)(sample_buf[j]); | |
449 | } | |
450 | } | |
451 | GraphTraceLen = cnt; | |
452 | RepaintGraphWindow(); | |
453 | return 0; | |
454 | } | |
455 | ||
456 | int CmdHide(const char *Cmd) | |
457 | { | |
458 | HideGraphWindow(); | |
459 | return 0; | |
460 | } | |
461 | ||
462 | int CmdHpf(const char *Cmd) | |
463 | { | |
464 | int i; | |
465 | int accum = 0; | |
466 | ||
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; | |
472 | ||
473 | RepaintGraphWindow(); | |
474 | return 0; | |
475 | } | |
476 | ||
477 | int CmdLFSamples(const char *Cmd) | |
478 | { | |
479 | int cnt = 0; | |
480 | int n; | |
481 | ||
482 | n = strtol(Cmd, NULL, 0); | |
483 | if (n == 0) n = 128; | |
484 | if (n > 16000) n = 16000; | |
485 | ||
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}}; | |
489 | SendCommand(&c); | |
490 | WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K); | |
491 | for (int j = 0; j < 48; j++) { | |
492 | GraphBuffer[cnt++] = ((int)sample_buf[j]) - 128; | |
493 | } | |
494 | } | |
495 | PrintAndLog("Done!\n"); | |
496 | GraphTraceLen = n*4; | |
497 | RepaintGraphWindow(); | |
498 | return 0; | |
499 | } | |
500 | ||
501 | int CmdLoad(const char *Cmd) | |
502 | { | |
503 | FILE *f = fopen(Cmd, "r"); | |
504 | if (!f) { | |
505 | PrintAndLog("couldn't open '%s'", Cmd); | |
506 | return 0; | |
507 | } | |
508 | ||
509 | GraphTraceLen = 0; | |
510 | char line[80]; | |
511 | while (fgets(line, sizeof (line), f)) { | |
512 | GraphBuffer[GraphTraceLen] = atoi(line); | |
513 | GraphTraceLen++; | |
514 | } | |
515 | fclose(f); | |
516 | PrintAndLog("loaded %d samples", GraphTraceLen); | |
517 | RepaintGraphWindow(); | |
518 | return 0; | |
519 | } | |
520 | ||
521 | int CmdLtrim(const char *Cmd) | |
522 | { | |
523 | int ds = atoi(Cmd); | |
524 | ||
525 | for (int i = ds; i < GraphTraceLen; ++i) | |
526 | GraphBuffer[i-ds] = GraphBuffer[i]; | |
527 | GraphTraceLen -= ds; | |
528 | ||
529 | RepaintGraphWindow(); | |
530 | return 0; | |
531 | } | |
532 | ||
533 | /* | |
534 | * Manchester demodulate a bitstream. The bitstream needs to be already in | |
535 | * the GraphBuffer as 0 and 1 values | |
536 | * | |
537 | * Give the clock rate as argument in order to help the sync - the algorithm | |
538 | * resyncs at each pulse anyway. | |
539 | * | |
540 | * Not optimized by any means, this is the 1st time I'm writing this type of | |
541 | * routine, feel free to improve... | |
542 | * | |
543 | * 1st argument: clock rate (as number of samples per clock rate) | |
544 | * Typical values can be 64, 32, 128... | |
545 | */ | |
546 | int CmdManchesterDemod(const char *Cmd) | |
547 | { | |
548 | int i, j, invert= 0; | |
549 | int bit; | |
550 | int clock; | |
551 | int lastval; | |
552 | int low = 0; | |
553 | int high = 0; | |
554 | int hithigh, hitlow, first; | |
555 | int lc = 0; | |
556 | int bitidx = 0; | |
557 | int bit2idx = 0; | |
558 | int warnings = 0; | |
559 | ||
560 | /* check if we're inverting output */ | |
561 | if (*Cmd == 'i') | |
562 | { | |
563 | PrintAndLog("Inverting output"); | |
564 | invert = 1; | |
565 | do | |
566 | ++Cmd; | |
567 | while(*Cmd == ' '); // in case a 2nd argument was given | |
568 | } | |
569 | ||
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]; */ | |
574 | ||
575 | /* But it does not work if compiling on WIndows: therefore we just allocate a */ | |
576 | /* large array */ | |
577 | int BitStream[MAX_GRAPH_TRACE_LEN]; | |
578 | ||
579 | /* Detect high and lows */ | |
580 | for (i = 0; i < GraphTraceLen; i++) | |
581 | { | |
582 | if (GraphBuffer[i] > high) | |
583 | high = GraphBuffer[i]; | |
584 | else if (GraphBuffer[i] < low) | |
585 | low = GraphBuffer[i]; | |
586 | } | |
587 | ||
588 | /* Get our clock */ | |
589 | clock = GetClock(Cmd, high, 1); | |
590 | ||
591 | int tolerance = clock/4; | |
592 | ||
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) | |
598 | break; | |
599 | /* now look for the first low */ | |
600 | for (; i < GraphTraceLen; i++) | |
601 | { | |
602 | if (GraphBuffer[i] == low) | |
603 | { | |
604 | lastval = i; | |
605 | break; | |
606 | } | |
607 | } | |
608 | ||
609 | /* If we're not working with 1/0s, demod based off clock */ | |
610 | if (high != 1) | |
611 | { | |
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. | |
614 | * Ed. | |
615 | */ | |
616 | for (; i < (int)(GraphTraceLen / clock); i++) | |
617 | { | |
618 | hithigh = 0; | |
619 | hitlow = 0; | |
620 | first = 1; | |
621 | ||
622 | /* Find out if we hit both high and low peaks */ | |
623 | for (j = 0; j < clock; j++) | |
624 | { | |
625 | if (GraphBuffer[(i * clock) + j] == high) | |
626 | hithigh = 1; | |
627 | else if (GraphBuffer[(i * clock) + j] == low) | |
628 | hitlow = 1; | |
629 | ||
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; | |
634 | else | |
635 | first = 0; | |
636 | ||
637 | if (hithigh && hitlow) | |
638 | break; | |
639 | } | |
640 | ||
641 | /* If we didn't hit both high and low peaks, we had a bit transition */ | |
642 | if (!hithigh || !hitlow) | |
643 | bit ^= 1; | |
644 | ||
645 | BitStream[bit2idx++] = bit ^ invert; | |
646 | } | |
647 | } | |
648 | ||
649 | /* standard 1/0 bitstream */ | |
650 | else | |
651 | { | |
652 | ||
653 | /* Then detect duration between 2 successive transitions */ | |
654 | for (bitidx = 1; i < GraphTraceLen; i++) | |
655 | { | |
656 | if (GraphBuffer[i-1] != GraphBuffer[i]) | |
657 | { | |
658 | lc = i-lastval; | |
659 | lastval = i; | |
660 | ||
661 | // Error check: if bitidx becomes too large, we do not | |
662 | // have a Manchester encoded bitstream or the clock is really | |
663 | // wrong! | |
664 | if (bitidx > (GraphTraceLen*2/clock+8) ) { | |
665 | PrintAndLog("Error: the clock you gave is probably wrong, aborting."); | |
666 | return 0; | |
667 | } | |
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]; | |
677 | } else { | |
678 | // Error | |
679 | warnings++; | |
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)"); | |
682 | ||
683 | if (warnings > 10) | |
684 | { | |
685 | PrintAndLog("Error: too many detection errors, aborting."); | |
686 | return 0; | |
687 | } | |
688 | } | |
689 | } | |
690 | } | |
691 | ||
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; | |
700 | } else { | |
701 | // We cannot end up in this state, this means we are unsynchronized, | |
702 | // move up 1 bit: | |
703 | i++; | |
704 | warnings++; | |
705 | PrintAndLog("Unsynchronized, resync..."); | |
706 | PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)"); | |
707 | ||
708 | if (warnings > 10) | |
709 | { | |
710 | PrintAndLog("Error: too many decode errors, aborting."); | |
711 | return 0; | |
712 | } | |
713 | } | |
714 | } | |
715 | } | |
716 | ||
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", | |
721 | BitStream[i], | |
722 | BitStream[i+1], | |
723 | BitStream[i+2], | |
724 | BitStream[i+3], | |
725 | BitStream[i+4], | |
726 | BitStream[i+5], | |
727 | BitStream[i+6], | |
728 | BitStream[i+7], | |
729 | BitStream[i+8], | |
730 | BitStream[i+9], | |
731 | BitStream[i+10], | |
732 | BitStream[i+11], | |
733 | BitStream[i+12], | |
734 | BitStream[i+13], | |
735 | BitStream[i+14], | |
736 | BitStream[i+15]); | |
737 | } | |
738 | return 0; | |
739 | } | |
740 | ||
741 | /* Modulate our data into manchester */ | |
742 | int CmdManchesterMod(const char *Cmd) | |
743 | { | |
744 | int i, j; | |
745 | int clock; | |
746 | int bit, lastbit, wave; | |
747 | ||
748 | /* Get our clock */ | |
749 | clock = GetClock(Cmd, 0, 1); | |
750 | ||
751 | wave = 0; | |
752 | lastbit = 1; | |
753 | for (i = 0; i < (int)(GraphTraceLen / clock); i++) | |
754 | { | |
755 | bit = GraphBuffer[i * clock] ^ 1; | |
756 | ||
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; | |
761 | ||
762 | /* Keep track of how we start our wave and if we changed or not this time */ | |
763 | wave ^= bit ^ lastbit; | |
764 | lastbit = bit; | |
765 | } | |
766 | ||
767 | RepaintGraphWindow(); | |
768 | return 0; | |
769 | } | |
770 | ||
771 | int CmdNorm(const char *Cmd) | |
772 | { | |
773 | int i; | |
774 | int max = INT_MIN, min = INT_MAX; | |
775 | ||
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]; | |
781 | } | |
782 | ||
783 | if (max != min) { | |
784 | for (i = 0; i < GraphTraceLen; ++i) { | |
785 | GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 1000 / | |
786 | (max - min); | |
787 | } | |
788 | } | |
789 | RepaintGraphWindow(); | |
790 | return 0; | |
791 | } | |
792 | ||
793 | int CmdPlot(const char *Cmd) | |
794 | { | |
795 | ShowGraphWindow(); | |
796 | return 0; | |
797 | } | |
798 | ||
799 | int CmdSave(const char *Cmd) | |
800 | { | |
801 | FILE *f = fopen(Cmd, "w"); | |
802 | if(!f) { | |
803 | PrintAndLog("couldn't open '%s'", Cmd); | |
804 | return 0; | |
805 | } | |
806 | int i; | |
807 | for (i = 0; i < GraphTraceLen; i++) { | |
808 | fprintf(f, "%d\n", GraphBuffer[i]); | |
809 | } | |
810 | fclose(f); | |
811 | PrintAndLog("saved to '%s'", Cmd); | |
812 | return 0; | |
813 | } | |
814 | ||
815 | int CmdScale(const char *Cmd) | |
816 | { | |
817 | CursorScaleFactor = atoi(Cmd); | |
818 | if (CursorScaleFactor == 0) { | |
819 | PrintAndLog("bad, can't have zero scale"); | |
820 | CursorScaleFactor = 1; | |
821 | } | |
822 | RepaintGraphWindow(); | |
823 | return 0; | |
824 | } | |
825 | ||
826 | int CmdThreshold(const char *Cmd) | |
827 | { | |
828 | int threshold = atoi(Cmd); | |
829 | ||
830 | for (int i = 0; i < GraphTraceLen; ++i) { | |
831 | if (GraphBuffer[i] >= threshold) | |
832 | GraphBuffer[i] = 1; | |
833 | else | |
834 | GraphBuffer[i] =- 1; | |
835 | } | |
836 | RepaintGraphWindow(); | |
837 | return 0; | |
838 | } | |
839 | ||
840 | int CmdZerocrossings(const char *Cmd) | |
841 | { | |
842 | // Zero-crossings aren't meaningful unless the signal is zero-mean. | |
843 | CmdHpf(""); | |
844 | ||
845 | int sign = 1; | |
846 | int zc = 0; | |
847 | int lastZc = 0; | |
848 | ||
849 | for (int i = 0; i < GraphTraceLen; ++i) { | |
850 | if (GraphBuffer[i] * sign >= 0) { | |
851 | // No change in sign, reproduce the previous sample count. | |
852 | zc++; | |
853 | GraphBuffer[i] = lastZc; | |
854 | } else { | |
855 | // Change in sign, reset the sample count. | |
856 | sign = -sign; | |
857 | GraphBuffer[i] = lastZc; | |
858 | if (sign > 0) { | |
859 | lastZc = zc; | |
860 | zc = 0; | |
861 | } | |
862 | } | |
863 | } | |
864 | ||
865 | RepaintGraphWindow(); | |
866 | return 0; | |
867 | } | |
868 | ||
869 | static command_t CommandTable[] = | |
870 | { | |
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} | |
898 | }; | |
899 | ||
900 | int CmdData(const char *Cmd) | |
901 | { | |
902 | CmdsParse(CommandTable, Cmd); | |
903 | return 0; | |
904 | } | |
905 | ||
906 | int CmdHelp(const char *Cmd) | |
907 | { | |
908 | CmdsHelp(CommandTable); | |
909 | return 0; | |
910 | } |