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