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