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