]> git.zerfleddert.de Git - proxmark3-svn/blob - client/command.c
Added offset feature to hexsamples command
[proxmark3-svn] / client / command.c
1 //-----------------------------------------------------------------------------
2 // The actual command interpeter for what the user types at the command line.
3 // Jonathan Westhues, Sept 2005
4 // Edits by Gerhard de Koning Gans, Sep 2007 (##)
5 //-----------------------------------------------------------------------------
6 #ifdef WIN32
7 #include <windows.h>
8 #endif
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <limits.h>
14 #include <math.h>
15
16 #include "prox.h"
17 #include "../common/iso14443_crc.c"
18 #include "../common/crc16.c"
19 #include "../include/usb_cmd.h"
20
21 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
22 #define BIT(x) GraphBuffer[x * clock]
23 #define BITS (GraphTraceLen / clock)
24 #define SAMPLE_BUFFER_SIZE 64 // XXX check this
25
26 int go = 0;
27 static int CmdHisamplest(char *str, int nrlow);
28 unsigned int current_command = CMD_UNKNOWN;
29 unsigned int received_command = CMD_UNKNOWN;
30 static uint8_t sample_buf[SAMPLE_BUFFER_SIZE];
31
32 void wait_for_response(uint32_t response_type)
33 {
34 while (received_command != response_type) {
35 #ifdef WIN32
36 UsbCommand c;
37 if (ReceiveCommandPoll(&c))
38 UsbCommandReceived(&c);
39 Sleep(0);
40 #else
41 usleep(10000); // XXX ugh
42 #endif
43 }
44 received_command = CMD_UNKNOWN;
45 }
46
47 static void GetFromBigBuf(uint8_t *dest, int bytes)
48 {
49 int n = bytes/4;
50
51 if(n % 48 != 0) {
52 PrintToScrollback("bad len in GetFromBigBuf");
53 return;
54 }
55
56 int i;
57 for(i = 0; i < n; i += 12) {
58 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
59 SendCommand(&c);
60 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
61
62 memcpy(dest+(i*4), sample_buf, 48);
63 }
64 }
65
66 static void CmdReset(char *str)
67 {
68 UsbCommand c = {CMD_HARDWARE_RESET};
69 SendCommand(&c);
70 }
71
72 static void CmdBuffClear(char *str)
73 {
74 UsbCommand c = {CMD_BUFF_CLEAR};
75 SendCommand(&c);
76 CmdClearGraph(true);
77 }
78
79 static void CmdQuit(char *str)
80 {
81 exit(0);
82 }
83
84 static void CmdHIDdemodFSK(char *str)
85 {
86 UsbCommand c={CMD_HID_DEMOD_FSK};
87 SendCommand(&c);
88 }
89
90 static void CmdTune(char *str)
91 {
92 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING};
93 SendCommand(&c);
94 }
95
96 static void CmdHiTune(char *str)
97 {
98 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
99 SendCommand(&c);
100 }
101
102 static void CmdHi15read(char *str)
103 {
104 UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
105 SendCommand(&c);
106 }
107
108 static void CmdHi14read(char *str)
109 {
110 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(str, NULL, 0), 0, 0}};
111 SendCommand(&c);
112 }
113
114
115 /* New command to read the contents of a SRI512 tag
116 * SRI512 tags are ISO14443-B modulated memory tags,
117 * this command just dumps the contents of the memory
118 */
119 static void CmdSri512read(char *str)
120 {
121 UsbCommand c={CMD_READ_SRI512_TAG, {strtol(str, NULL, 0), 0, 0}};
122 SendCommand(&c);
123 }
124
125 /* New command to read the contents of a SRIX4K tag
126 * SRIX4K tags are ISO14443-B modulated memory tags,
127 * this command just dumps the contents of the memory/
128 */
129 static void CmdSrix4kread(char *str)
130 {
131 UsbCommand c={CMD_READ_SRIX4K_TAG, {strtol(str, NULL, 0), 0, 0}};
132 SendCommand(&c);
133 }
134
135 static void CmdHi14areader(char *str)
136 {
137 UsbCommand c={CMD_READER_ISO_14443a, {strtol(str, NULL, 0), 0, 0}};
138 SendCommand(&c);
139 }
140
141 static void CmdHi14amifare(char *str)
142 {
143 UsbCommand c={CMD_READER_MIFARE, {strtol(str, NULL, 0), 0, 0}};
144 SendCommand(&c);
145 }
146
147 static void CmdHi15reader(char *str)
148 {
149 UsbCommand c={CMD_READER_ISO_15693, {strtol(str, NULL, 0), 0, 0}};
150 SendCommand(&c);
151 }
152
153 static void CmdHi15tag(char *str)
154 {
155 UsbCommand c={CMD_SIMTAG_ISO_15693, {strtol(str, NULL, 0), 0, 0}};
156 SendCommand(&c);
157 }
158
159 static void CmdHi14read_sim(char *str)
160 {
161 UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM, {strtol(str, NULL, 0), 0, 0}};
162 SendCommand(&c);
163 }
164
165 static void CmdHi14readt(char *str)
166 {
167 UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(str, NULL, 0), 0, 0}};
168 SendCommand(&c);
169
170 //CmdHisamplest(str);
171 while(CmdHisamplest(str,strtol(str, NULL, 0))==0) {
172 SendCommand(&c);
173 }
174 RepaintGraphWindow();
175 }
176
177 static void CmdHisimlisten(char *str)
178 {
179 UsbCommand c={CMD_SIMULATE_TAG_HF_LISTEN};
180 SendCommand(&c);
181 }
182
183 static void CmdHi14sim(char *str)
184 {
185 UsbCommand c={CMD_SIMULATE_TAG_ISO_14443};
186 SendCommand(&c);
187 }
188
189 static void CmdHi14asim(char *str) // ## simulate iso14443a tag
190 { // ## greg - added ability to specify tag UID
191
192 unsigned int hi=0, lo=0;
193 int n=0, i=0;
194 while (sscanf(&str[i++], "%1x", &n ) == 1) {
195 hi=(hi<<4)|(lo>>28);
196 lo=(lo<<4)|(n&0xf);
197 }
198
199 // c.arg should be set to *str or convert *str to the correct format for a uid
200 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a, {hi, lo, 0}};
201 PrintToScrollback("Emulating 14443A TAG with UID %x%16x", hi, lo);
202 SendCommand(&c);
203 }
204
205 static void CmdHi14snoop(char *str)
206 {
207 UsbCommand c={CMD_SNOOP_ISO_14443};
208 SendCommand(&c);
209 }
210
211 static void CmdHi14asnoop(char *str)
212 {
213 UsbCommand c={CMD_SNOOP_ISO_14443a};
214 SendCommand(&c);
215 }
216
217 static void CmdLegicRfRead(char *str)
218 {
219 UsbCommand c={CMD_READER_LEGIC_RF};
220 SendCommand(&c);
221 }
222
223 static void CmdFPGAOff(char *str) // ## FPGA Control
224 {
225 UsbCommand c={CMD_FPGA_MAJOR_MODE_OFF};
226 SendCommand(&c);
227 }
228
229 /* clear out our graph window */
230 int CmdClearGraph(int redraw)
231 {
232 int gtl = GraphTraceLen;
233 GraphTraceLen = 0;
234
235 if (redraw)
236 RepaintGraphWindow();
237
238 return gtl;
239 }
240
241 /* write a bit to the graph */
242 static void CmdAppendGraph(int redraw, int clock, int bit)
243 {
244 int i;
245
246 for (i = 0; i < (int)(clock/2); i++)
247 GraphBuffer[GraphTraceLen++] = bit ^ 1;
248
249 for (i = (int)(clock/2); i < clock; i++)
250 GraphBuffer[GraphTraceLen++] = bit;
251
252 if (redraw)
253 RepaintGraphWindow();
254 }
255
256 /* Function is equivalent of loread + losamples + em410xread
257 * looped until an EM410x tag is detected */
258 static void CmdEM410xwatch(char *str)
259 {
260 char *zero = "";
261 char *twok = "2000";
262 go = 1;
263
264 do
265 {
266 CmdLoread(zero);
267 CmdLosamples(twok);
268 CmdEM410xread(zero);
269 } while (go);
270 }
271
272 /* Read the transmitted data of an EM4x50 tag
273 * Format:
274 *
275 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
276 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
277 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
278 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
279 * CCCCCCCC <- column parity bits
280 * 0 <- stop bit
281 * LW <- Listen Window
282 *
283 * This pattern repeats for every block of data being transmitted.
284 * Transmission starts with two Listen Windows (LW - a modulated
285 * pattern of 320 cycles each (32/32/128/64/64)).
286 *
287 * Note that this data may or may not be the UID. It is whatever data
288 * is stored in the blocks defined in the control word First and Last
289 * Word Read values. UID is stored in block 32.
290 */
291 static void CmdEM4x50read(char *str)
292 {
293 int i, j, startblock, clock, skip, block, start, end, low, high;
294 bool complete= false;
295 int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
296 char tmp[6];
297
298 high= low= 0;
299 clock= 64;
300
301 /* first get high and low values */
302 for (i = 0; i < GraphTraceLen; i++)
303 {
304 if (GraphBuffer[i] > high)
305 high = GraphBuffer[i];
306 else if (GraphBuffer[i] < low)
307 low = GraphBuffer[i];
308 }
309
310 /* populate a buffer with pulse lengths */
311 i= 0;
312 j= 0;
313 while(i < GraphTraceLen)
314 {
315 // measure from low to low
316 while((GraphBuffer[i] > low) && (i<GraphTraceLen))
317 ++i;
318 start= i;
319 while((GraphBuffer[i] < high) && (i<GraphTraceLen))
320 ++i;
321 while((GraphBuffer[i] > low) && (i<GraphTraceLen))
322 ++i;
323 if (j>(MAX_GRAPH_TRACE_LEN/64)) {
324 break;
325 }
326 tmpbuff[j++]= i - start;
327 }
328
329 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
330 start= -1;
331 skip= 0;
332 for (i= 0; i < j - 4 ; ++i)
333 {
334 skip += tmpbuff[i];
335 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
336 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
337 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
338 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
339 {
340 start= i + 3;
341 break;
342 }
343 }
344 startblock= i + 3;
345
346 /* skip over the remainder of the LW */
347 skip += tmpbuff[i+1]+tmpbuff[i+2];
348 while(skip < MAX_GRAPH_TRACE_LEN && GraphBuffer[skip] > low)
349 ++skip;
350 skip += 8;
351
352 /* now do it again to find the end */
353 end= start;
354 for (i += 3; i < j - 4 ; ++i)
355 {
356 end += tmpbuff[i];
357 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
358 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
359 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
360 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
361 {
362 complete= true;
363 break;
364 }
365 }
366
367 if (start >= 0)
368 PrintToScrollback("Found data at sample: %i",skip);
369 else
370 {
371 PrintToScrollback("No data found!");
372 PrintToScrollback("Try again with more samples.");
373 return;
374 }
375
376 if (!complete)
377 {
378 PrintToScrollback("*** Warning!");
379 PrintToScrollback("Partial data - no end found!");
380 PrintToScrollback("Try again with more samples.");
381 }
382
383 /* get rid of leading crap */
384 sprintf(tmp,"%i",skip);
385 CmdLtrim(tmp);
386
387 /* now work through remaining buffer printing out data blocks */
388 block= 0;
389 i= startblock;
390 while(block < 6)
391 {
392 PrintToScrollback("Block %i:", block);
393 // mandemod routine needs to be split so we can call it for data
394 // just print for now for debugging
395 Cmdmanchesterdemod("i 64");
396 skip= 0;
397 /* look for LW before start of next block */
398 for ( ; i < j - 4 ; ++i)
399 {
400 skip += tmpbuff[i];
401 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
402 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
403 break;
404 }
405 while(GraphBuffer[skip] > low)
406 ++skip;
407 skip += 8;
408 sprintf(tmp,"%i",skip);
409 CmdLtrim(tmp);
410 start += skip;
411 block++;
412 }
413 }
414
415
416 /* Read the ID of an EM410x tag.
417 * Format:
418 * 1111 1111 1 <-- standard non-repeatable header
419 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
420 * ....
421 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
422 * 0 <-- stop bit, end of tag
423 */
424 static void CmdEM410xread(char *str)
425 {
426 int i, j, clock, header, rows, bit, hithigh, hitlow, first, bit2idx, high, low;
427 int parity[4];
428 char id[11];
429 int retested = 0;
430 int BitStream[MAX_GRAPH_TRACE_LEN];
431 high = low = 0;
432
433 /* Detect high and lows and clock */
434 for (i = 0; i < GraphTraceLen; i++)
435 {
436 if (GraphBuffer[i] > high)
437 high = GraphBuffer[i];
438 else if (GraphBuffer[i] < low)
439 low = GraphBuffer[i];
440 }
441
442 /* get clock */
443 clock = GetClock(str, high);
444
445 /* parity for our 4 columns */
446 parity[0] = parity[1] = parity[2] = parity[3] = 0;
447 header = rows = 0;
448
449 /* manchester demodulate */
450 bit = bit2idx = 0;
451 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
452 {
453 hithigh = 0;
454 hitlow = 0;
455 first = 1;
456
457 /* Find out if we hit both high and low peaks */
458 for (j = 0; j < clock; j++)
459 {
460 if (GraphBuffer[(i * clock) + j] == high)
461 hithigh = 1;
462 else if (GraphBuffer[(i * clock) + j] == low)
463 hitlow = 1;
464
465 /* it doesn't count if it's the first part of our read
466 because it's really just trailing from the last sequence */
467 if (first && (hithigh || hitlow))
468 hithigh = hitlow = 0;
469 else
470 first = 0;
471
472 if (hithigh && hitlow)
473 break;
474 }
475
476 /* If we didn't hit both high and low peaks, we had a bit transition */
477 if (!hithigh || !hitlow)
478 bit ^= 1;
479
480 BitStream[bit2idx++] = bit;
481 }
482
483 retest:
484 /* We go till 5 before the graph ends because we'll get that far below */
485 for (i = 1; i < bit2idx - 5; i++)
486 {
487 /* Step 2: We have our header but need our tag ID */
488 if (header == 9 && rows < 10)
489 {
490 /* Confirm parity is correct */
491 if ((BitStream[i] ^ BitStream[i+1] ^ BitStream[i+2] ^ BitStream[i+3]) == BitStream[i+4])
492 {
493 /* Read another byte! */
494 sprintf(id+rows, "%x", (8 * BitStream[i]) + (4 * BitStream[i+1]) + (2 * BitStream[i+2]) + (1 * BitStream[i+3]));
495 rows++;
496
497 /* Keep parity info */
498 parity[0] ^= BitStream[i];
499 parity[1] ^= BitStream[i+1];
500 parity[2] ^= BitStream[i+2];
501 parity[3] ^= BitStream[i+3];
502
503 /* Move 4 bits ahead */
504 i += 4;
505 }
506
507 /* Damn, something wrong! reset */
508 else
509 {
510 PrintToScrollback("Thought we had a valid tag but failed at word %d (i=%d)", rows + 1, i);
511
512 /* Start back rows * 5 + 9 header bits, -1 to not start at same place */
513 i -= 9 + (5 * rows) - 5;
514
515 rows = header = 0;
516 }
517 }
518
519 /* Step 3: Got our 40 bits! confirm column parity */
520 else if (rows == 10)
521 {
522 /* We need to make sure our 4 bits of parity are correct and we have a stop bit */
523 if (BitStream[i] == parity[0] && BitStream[i+1] == parity[1] &&
524 BitStream[i+2] == parity[2] && BitStream[i+3] == parity[3] &&
525 BitStream[i+4] == 0)
526 {
527 /* Sweet! */
528 PrintToScrollback("EM410x Tag ID: %s", id);
529
530 /* Stop any loops */
531 go = 0;
532 return;
533 }
534
535 /* Crap! Incorrect parity or no stop bit, start all over */
536 else
537 {
538 rows = header = 0;
539
540 /* Go back 59 bits (9 header bits + 10 rows at 4+1 parity) */
541 i -= 59;
542 }
543 }
544
545 /* Step 1: get our header */
546 else if (header < 9)
547 {
548 /* Need 9 consecutive 1's */
549 if (BitStream[i] == 1)
550 header++;
551
552 /* We don't have a header, not enough consecutive 1 bits */
553 else
554 header = 0;
555 }
556 }
557
558 /* if we've already retested after flipping bits, return */
559 if (retested++)
560 return;
561
562 /* if this didn't work, try flipping bits */
563 for (i = 0; i < bit2idx; i++)
564 BitStream[i] ^= 1;
565
566 goto retest;
567 }
568
569 /* emulate an EM410X tag
570 * Format:
571 * 1111 1111 1 <-- standard non-repeatable header
572 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
573 * ....
574 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
575 * 0 <-- stop bit, end of tag
576 */
577 static void CmdEM410xsim(char *str)
578 {
579 int i, n, j, h, binary[4], parity[4];
580 char *s = "0";
581
582 /* clock is 64 in EM410x tags */
583 int clock = 64;
584
585 /* clear our graph */
586 CmdClearGraph(0);
587
588 /* write it out a few times */
589 for (h = 0; h < 4; h++)
590 {
591 /* write 9 start bits */
592 for (i = 0; i < 9; i++)
593 CmdAppendGraph(0, clock, 1);
594
595 /* for each hex char */
596 parity[0] = parity[1] = parity[2] = parity[3] = 0;
597 for (i = 0; i < 10; i++)
598 {
599 /* read each hex char */
600 sscanf(&str[i], "%1x", &n);
601 for (j = 3; j >= 0; j--, n/= 2)
602 binary[j] = n % 2;
603
604 /* append each bit */
605 CmdAppendGraph(0, clock, binary[0]);
606 CmdAppendGraph(0, clock, binary[1]);
607 CmdAppendGraph(0, clock, binary[2]);
608 CmdAppendGraph(0, clock, binary[3]);
609
610 /* append parity bit */
611 CmdAppendGraph(0, clock, binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
612
613 /* keep track of column parity */
614 parity[0] ^= binary[0];
615 parity[1] ^= binary[1];
616 parity[2] ^= binary[2];
617 parity[3] ^= binary[3];
618 }
619
620 /* parity columns */
621 CmdAppendGraph(0, clock, parity[0]);
622 CmdAppendGraph(0, clock, parity[1]);
623 CmdAppendGraph(0, clock, parity[2]);
624 CmdAppendGraph(0, clock, parity[3]);
625
626 /* stop bit */
627 CmdAppendGraph(0, clock, 0);
628 }
629
630 /* modulate that biatch */
631 Cmdmanchestermod(s);
632
633 /* booyah! */
634 RepaintGraphWindow();
635
636 CmdLosim(s);
637 }
638
639 static void ChkBitstream(char *str)
640 {
641 int i;
642
643 /* convert to bitstream if necessary */
644 for (i = 0; i < (int)(GraphTraceLen / 2); i++)
645 {
646 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0)
647 {
648 Cmdbitstream(str);
649 break;
650 }
651 }
652 }
653
654 static void CmdLosim(char *str)
655 {
656 int i;
657
658 /* convert to bitstream if necessary */
659 ChkBitstream(str);
660
661 for (i = 0; i < GraphTraceLen; i += 48) {
662 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
663 int j;
664 for(j = 0; j < 48; j++) {
665 c.d.asBytes[j] = GraphBuffer[i+j];
666 }
667 SendCommand(&c);
668 }
669
670 UsbCommand c={CMD_SIMULATE_TAG_125K, {GraphTraceLen, 0, 0}};
671 SendCommand(&c);
672 }
673
674 static void CmdLosimBidir(char *str)
675 {
676 /* Set ADC to twice the carrier for a slight supersampling */
677 UsbCommand c={CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
678 SendCommand(&c);
679 }
680
681 static void CmdLoread(char *str)
682 {
683 UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};
684 // 'h' means higher-low-frequency, 134 kHz
685 if(*str == 'h') {
686 c.arg[0] = 1;
687 } else if (*str == '\0') {
688 c.arg[0] = 0;
689 } else {
690 PrintToScrollback("use 'loread' or 'loread h'");
691 return;
692 }
693 SendCommand(&c);
694 }
695
696 static void CmdDetectReader(char *str)
697 {
698 UsbCommand c={CMD_LISTEN_READER_FIELD};
699 // 'l' means LF - 125/134 kHz
700 if(*str == 'l') {
701 c.arg[0] = 1;
702 } else if (*str == 'h') {
703 c.arg[0] = 2;
704 } else if (*str != '\0') {
705 PrintToScrollback("use 'detectreader' or 'detectreader l' or 'detectreader h'");
706 return;
707 }
708 SendCommand(&c);
709 }
710
711 /* send a command before reading */
712 static void CmdLoCommandRead(char *str)
713 {
714 static char dummy[3];
715
716 dummy[0]= ' ';
717
718 UsbCommand c={CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
719 sscanf(str, "%i %i %i %s %s", &c.arg[0], &c.arg[1], &c.arg[2], (char *) &c.d.asBytes,(char *) &dummy+1);
720 // in case they specified 'h'
721 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
722 SendCommand(&c);
723 }
724
725 static void CmdLosamples(char *str)
726 {
727 int cnt = 0;
728 int i, j, n;
729
730 n=strtol(str, NULL, 0);
731 if (n==0) n=128;
732 if (n>16000) n=16000;
733
734 PrintToScrollback("Reading %d samples\n", n);
735 for(i = 0; i < n; i += 12) {
736 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
737 SendCommand(&c);
738 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
739 for(j = 0; j < 48; j++) {
740 GraphBuffer[cnt++] = ((int)sample_buf[j]) - 128;
741 }
742 }
743 PrintToScrollback("Done!\n");
744 GraphTraceLen = n*4;
745 RepaintGraphWindow();
746 }
747
748 static void CmdBitsamples(char *str)
749 {
750 int cnt = 0;
751 int i, j, k, n;
752
753 n = 3072;
754 for(i = 0; i < n; i += 12) {
755 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
756 SendCommand(&c);
757 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
758
759 for(j = 0; j < 48; j++) {
760 for(k = 0; k < 8; k++) {
761 if(sample_buf[j] & (1 << (7 - k))) {
762 GraphBuffer[cnt++] = 1;
763 } else {
764 GraphBuffer[cnt++] = 0;
765 }
766 }
767 }
768 }
769 GraphTraceLen = cnt;
770 RepaintGraphWindow();
771 }
772
773 static void CmdHisamples(char *str)
774 {
775 int cnt = 0;
776 int i, j, n;
777
778 n = 1000;
779 for(i = 0; i < n; i += 12) {
780 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
781 SendCommand(&c);
782 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
783 for(j = 0; j < 48; j++) {
784 GraphBuffer[cnt++] = (int)(sample_buf[j]);
785 }
786 }
787
788 GraphTraceLen = n*4;
789 RepaintGraphWindow();
790 }
791
792 static int CmdHisamplest(char *str, int nrlow)
793 {
794 int cnt = 0;
795 int t1, t2;
796 int i, j, n;
797 int hasbeennull;
798 int show;
799
800 n = 1000;
801 hasbeennull = 0;
802 for(i = 0; i < n; i += 12) {
803 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
804 SendCommand(&c);
805 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
806 for(j = 0; j < 48; j++) {
807 t2 = (int)(sample_buf[j]);
808 if((t2 ^ 0xC0) & 0xC0) { hasbeennull++; }
809
810 show = 0;
811 switch(show) {
812 case 0:
813 // combined
814 t1 = (t2 & 0x80) ^ (t2 & 0x20);
815 t2 = ((t2 << 1) & 0x80) ^ ((t2 << 1) & 0x20);
816 break;
817
818 case 1:
819 // only reader
820 t1 = (t2 & 0x80);
821 t2 = ((t2 << 1) & 0x80);
822 break;
823
824 case 2:
825 // only tag
826 t1 = (t2 & 0x20);
827 t2 = ((t2 << 1) & 0x20);
828 break;
829
830 case 3:
831 // both, but tag with other algorithm
832 t1 = (t2 & 0x80) ^ (t2 & 0x08);
833 t2 = ((t2 << 1) & 0x80) ^ ((t2 << 1) & 0x08);
834 break;
835 }
836
837 GraphBuffer[cnt++] = t1;
838 GraphBuffer[cnt++] = t2;
839 }
840 }
841 GraphTraceLen = n*4;
842 // 1130
843 if(hasbeennull>nrlow || nrlow==0) {
844 PrintToScrollback("hasbeennull=%d", hasbeennull);
845 return 1;
846 }
847 else {
848 return 0;
849 }
850 }
851
852
853 static void CmdHexsamples(char *str)
854 {
855 int i, j, n;
856 int requested = 0;
857 int offset = 0;
858 sscanf(str, "%i %i", &requested, &offset);
859 if (offset % 4!=0) {
860 PrintToScrollback("Offset must be a multiple of 4");
861 return;
862 }
863 offset = offset/4;
864
865 int delivered = 0;
866
867 if (requested == 0) {
868 n = 12;
869 requested = 12;
870 } else {
871 n = requested/4;
872 }
873
874 for(i = offset; i < n+offset; i += 12) {
875 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
876 SendCommand(&c);
877 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
878 for (j = 0; j < 48; j += 8) {
879 PrintToScrollback("%02x %02x %02x %02x %02x %02x %02x %02x",
880 sample_buf[j+0],
881 sample_buf[j+1],
882 sample_buf[j+2],
883 sample_buf[j+3],
884 sample_buf[j+4],
885 sample_buf[j+5],
886 sample_buf[j+6],
887 sample_buf[j+7],
888 sample_buf[j+8]
889 );
890 delivered += 8;
891 if (delivered >= requested)
892 break;
893 }
894 if (delivered >= requested)
895 break;
896 }
897 }
898
899 static void CmdHisampless(char *str)
900 {
901 int cnt = 0;
902 int i, j;
903 int n = strtol(str, NULL, 0);
904
905 if(n == 0) {
906 n = 1000;
907 } else {
908 n/= 4;
909 }
910
911 for(i = 0; i < n; i += 12) {
912 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
913 SendCommand(&c);
914 wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
915 for(j = 0; j < 48; j++) {
916 GraphBuffer[cnt++] = (int)(sample_buf[j]);
917 }
918 }
919 GraphTraceLen = cnt;
920
921 RepaintGraphWindow();
922 }
923
924 static uint16_t Iso15693Crc(uint8_t *v, int n)
925 {
926 uint32_t reg;
927 int i, j;
928
929 reg = 0xffff;
930 for(i = 0; i < n; i++) {
931 reg = reg ^ ((uint32_t)v[i]);
932 for (j = 0; j < 8; j++) {
933 if (reg & 0x0001) {
934 reg = (reg >> 1) ^ 0x8408;
935 } else {
936 reg = (reg >> 1);
937 }
938 }
939 }
940
941 return (uint16_t)~reg;
942 }
943
944 static void CmdHi14bdemod(char *str)
945 {
946 int i, j, iold;
947 int isum, qsum;
948 int outOfWeakAt;
949 bool negateI, negateQ;
950
951 uint8_t data[256];
952 int dataLen=0;
953
954 // As received, the samples are pairs, correlations against I and Q
955 // square waves. So estimate angle of initial carrier (or just
956 // quadrant, actually), and then do the demod.
957
958 // First, estimate where the tag starts modulating.
959 for(i = 0; i < GraphTraceLen; i += 2) {
960 if(abs(GraphBuffer[i]) + abs(GraphBuffer[i+1]) > 40) {
961 break;
962 }
963 }
964 if(i >= GraphTraceLen) {
965 PrintToScrollback("too weak to sync");
966 return;
967 }
968 PrintToScrollback("out of weak at %d", i);
969 outOfWeakAt = i;
970
971 // Now, estimate the phase in the initial modulation of the tag
972 isum = 0;
973 qsum = 0;
974 for(; i < (outOfWeakAt + 16); i += 2) {
975 isum += GraphBuffer[i+0];
976 qsum += GraphBuffer[i+1];
977 }
978 negateI = (isum < 0);
979 negateQ = (qsum < 0);
980
981 // Turn the correlation pairs into soft decisions on the bit.
982 j = 0;
983 for(i = 0; i < GraphTraceLen/2; i++) {
984 int si = GraphBuffer[j];
985 int sq = GraphBuffer[j+1];
986 if(negateI) si = -si;
987 if(negateQ) sq = -sq;
988 GraphBuffer[i] = si + sq;
989 j += 2;
990 }
991 GraphTraceLen = i;
992
993 i = outOfWeakAt/2;
994 while(GraphBuffer[i] > 0 && i < GraphTraceLen)
995 i++;
996 if(i >= GraphTraceLen) goto demodError;
997
998 iold = i;
999 while(GraphBuffer[i] < 0 && i < GraphTraceLen)
1000 i++;
1001 if(i >= GraphTraceLen) goto demodError;
1002 if((i - iold) > 23) goto demodError;
1003
1004 PrintToScrollback("make it to demod loop");
1005
1006 for(;;) {
1007 iold = i;
1008 while(GraphBuffer[i] >= 0 && i < GraphTraceLen)
1009 i++;
1010 if(i >= GraphTraceLen) goto demodError;
1011 if((i - iold) > 6) goto demodError;
1012
1013 uint16_t shiftReg = 0;
1014 if(i + 20 >= GraphTraceLen) goto demodError;
1015
1016 for(j = 0; j < 10; j++) {
1017 int soft = GraphBuffer[i] + GraphBuffer[i+1];
1018
1019 if(abs(soft) < ((abs(isum) + abs(qsum))/20)) {
1020 PrintToScrollback("weak bit");
1021 }
1022
1023 shiftReg >>= 1;
1024 if(GraphBuffer[i] + GraphBuffer[i+1] >= 0) {
1025 shiftReg |= 0x200;
1026 }
1027
1028 i+= 2;
1029 }
1030
1031 if( (shiftReg & 0x200) &&
1032 !(shiftReg & 0x001))
1033 {
1034 // valid data byte, start and stop bits okay
1035 PrintToScrollback(" %02x", (shiftReg >> 1) & 0xff);
1036 data[dataLen++] = (shiftReg >> 1) & 0xff;
1037 if(dataLen >= sizeof(data)) {
1038 return;
1039 }
1040 } else if(shiftReg == 0x000) {
1041 // this is EOF
1042 break;
1043 } else {
1044 goto demodError;
1045 }
1046 }
1047
1048 uint8_t first, second;
1049 ComputeCrc14443(CRC_14443_B, data, dataLen-2, &first, &second);
1050 PrintToScrollback("CRC: %02x %02x (%s)\n", first, second,
1051 (first == data[dataLen-2] && second == data[dataLen-1]) ?
1052 "ok" : "****FAIL****");
1053
1054 RepaintGraphWindow();
1055 return;
1056
1057 demodError:
1058 PrintToScrollback("demod error");
1059 RepaintGraphWindow();
1060 }
1061
1062 static void CmdHi14list(char *str)
1063 {
1064 uint8_t got[960];
1065 GetFromBigBuf(got, sizeof(got));
1066
1067 PrintToScrollback("recorded activity:");
1068 PrintToScrollback(" time :rssi: who bytes");
1069 PrintToScrollback("---------+----+----+-----------");
1070
1071 int i = 0;
1072 int prev = -1;
1073
1074 for(;;) {
1075 if(i >= 900) {
1076 break;
1077 }
1078
1079 bool isResponse;
1080 int timestamp = *((uint32_t *)(got+i));
1081 if(timestamp & 0x80000000) {
1082 timestamp &= 0x7fffffff;
1083 isResponse = 1;
1084 } else {
1085 isResponse = 0;
1086 }
1087 int metric = *((uint32_t *)(got+i+4));
1088
1089 int len = got[i+8];
1090
1091 if(len > 100) {
1092 break;
1093 }
1094 if(i + len >= 900) {
1095 break;
1096 }
1097
1098 uint8_t *frame = (got+i+9);
1099
1100 char line[1000] = "";
1101 int j;
1102 for(j = 0; j < len; j++) {
1103 sprintf(line+(j*3), "%02x ", frame[j]);
1104 }
1105
1106 char *crc;
1107 if(len > 2) {
1108 uint8_t b1, b2;
1109 ComputeCrc14443(CRC_14443_B, frame, len-2, &b1, &b2);
1110 if(b1 != frame[len-2] || b2 != frame[len-1]) {
1111 crc = "**FAIL CRC**";
1112 } else {
1113 crc = "";
1114 }
1115 } else {
1116 crc = "(SHORT)";
1117 }
1118
1119 char metricString[100];
1120 if(isResponse) {
1121 sprintf(metricString, "%3d", metric);
1122 } else {
1123 strcpy(metricString, " ");
1124 }
1125
1126 PrintToScrollback(" +%7d: %s: %s %s %s",
1127 (prev < 0 ? 0 : timestamp - prev),
1128 metricString,
1129 (isResponse ? "TAG" : " "), line, crc);
1130
1131 prev = timestamp;
1132 i += (len + 9);
1133 }
1134 }
1135
1136 static void CmdHi14alist(char *str)
1137 {
1138 uint8_t got[1920];
1139 GetFromBigBuf(got, sizeof(got));
1140
1141 PrintToScrollback("recorded activity:");
1142 PrintToScrollback(" ETU :rssi: who bytes");
1143 PrintToScrollback("---------+----+----+-----------");
1144
1145 int i = 0;
1146 int prev = -1;
1147
1148 for(;;) {
1149 if(i >= 1900) {
1150 break;
1151 }
1152
1153 bool isResponse;
1154 int timestamp = *((uint32_t *)(got+i));
1155 if(timestamp & 0x80000000) {
1156 timestamp &= 0x7fffffff;
1157 isResponse = 1;
1158 } else {
1159 isResponse = 0;
1160 }
1161
1162 int metric = 0;
1163 int parityBits = *((uint32_t *)(got+i+4));
1164 // 4 bytes of additional information...
1165 // maximum of 32 additional parity bit information
1166 //
1167 // TODO:
1168 // at each quarter bit period we can send power level (16 levels)
1169 // or each half bit period in 256 levels.
1170
1171
1172 int len = got[i+8];
1173
1174 if(len > 100) {
1175 break;
1176 }
1177 if(i + len >= 1900) {
1178 break;
1179 }
1180
1181 uint8_t *frame = (got+i+9);
1182
1183 // Break and stick with current result if buffer was not completely full
1184 if(frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; }
1185
1186 char line[1000] = "";
1187 int j;
1188 for(j = 0; j < len; j++) {
1189 int oddparity = 0x01;
1190 int k;
1191
1192 for(k=0;k<8;k++) {
1193 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
1194 }
1195
1196 //if((parityBits >> (len - j - 1)) & 0x01) {
1197 if(isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) {
1198 sprintf(line+(j*4), "%02x! ", frame[j]);
1199 }
1200 else {
1201 sprintf(line+(j*4), "%02x ", frame[j]);
1202 }
1203 }
1204
1205 char *crc;
1206 crc = "";
1207 if(len > 2) {
1208 uint8_t b1, b2;
1209 for(j = 0; j < (len - 1); j++) {
1210 // gives problems... search for the reason..
1211 /*if(frame[j] == 0xAA) {
1212 switch(frame[j+1]) {
1213 case 0x01:
1214 crc = "[1] Two drops close after each other";
1215 break;
1216 case 0x02:
1217 crc = "[2] Potential SOC with a drop in second half of bitperiod";
1218 break;
1219 case 0x03:
1220 crc = "[3] Segment Z after segment X is not possible";
1221 break;
1222 case 0x04:
1223 crc = "[4] Parity bit of a fully received byte was wrong";
1224 break;
1225 default:
1226 crc = "[?] Unknown error";
1227 break;
1228 }
1229 break;
1230 }*/
1231 }
1232
1233 if(strlen(crc)==0) {
1234 ComputeCrc14443(CRC_14443_A, frame, len-2, &b1, &b2);
1235 if(b1 != frame[len-2] || b2 != frame[len-1]) {
1236 crc = (isResponse & (len < 6)) ? "" : " !crc";
1237 } else {
1238 crc = "";
1239 }
1240 }
1241 } else {
1242 crc = ""; // SHORT
1243 }
1244
1245 char metricString[100];
1246 if(isResponse) {
1247 sprintf(metricString, "%3d", metric);
1248 } else {
1249 strcpy(metricString, " ");
1250 }
1251
1252 PrintToScrollback(" +%7d: %s: %s %s %s",
1253 (prev < 0 ? 0 : (timestamp - prev)),
1254 metricString,
1255 (isResponse ? "TAG" : " "), line, crc);
1256
1257 prev = timestamp;
1258 i += (len + 9);
1259 }
1260 CommandFinished = 1;
1261 }
1262
1263 static void CmdHi15demod(char *str)
1264 {
1265 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
1266
1267 // SOF defined as
1268 // 1) Unmodulated time of 56.64us
1269 // 2) 24 pulses of 423.75khz
1270 // 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
1271
1272 static const int FrameSOF[] = {
1273 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1274 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1275 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1276 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1277 -1, -1, -1, -1,
1278 -1, -1, -1, -1,
1279 1, 1, 1, 1,
1280 1, 1, 1, 1
1281 };
1282 static const int Logic0[] = {
1283 1, 1, 1, 1,
1284 1, 1, 1, 1,
1285 -1, -1, -1, -1,
1286 -1, -1, -1, -1
1287 };
1288 static const int Logic1[] = {
1289 -1, -1, -1, -1,
1290 -1, -1, -1, -1,
1291 1, 1, 1, 1,
1292 1, 1, 1, 1
1293 };
1294
1295 // EOF defined as
1296 // 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
1297 // 2) 24 pulses of 423.75khz
1298 // 3) Unmodulated time of 56.64us
1299
1300 static const int FrameEOF[] = {
1301 1, 1, 1, 1,
1302 1, 1, 1, 1,
1303 -1, -1, -1, -1,
1304 -1, -1, -1, -1,
1305 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1306 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1307 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1308 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
1309 };
1310
1311 int i, j;
1312 int max = 0, maxPos;
1313
1314 int skip = 4;
1315
1316 if(GraphTraceLen < 1000) return;
1317
1318 // First, correlate for SOF
1319 for(i = 0; i < 100; i++) {
1320 int corr = 0;
1321 for(j = 0; j < arraylen(FrameSOF); j += skip) {
1322 corr += FrameSOF[j]*GraphBuffer[i+(j/skip)];
1323 }
1324 if(corr > max) {
1325 max = corr;
1326 maxPos = i;
1327 }
1328 }
1329 PrintToScrollback("SOF at %d, correlation %d", maxPos,
1330 max/(arraylen(FrameSOF)/skip));
1331
1332 i = maxPos + arraylen(FrameSOF)/skip;
1333 int k = 0;
1334 uint8_t outBuf[20];
1335 memset(outBuf, 0, sizeof(outBuf));
1336 uint8_t mask = 0x01;
1337 for(;;) {
1338 int corr0 = 0, corr1 = 0, corrEOF = 0;
1339 for(j = 0; j < arraylen(Logic0); j += skip) {
1340 corr0 += Logic0[j]*GraphBuffer[i+(j/skip)];
1341 }
1342 for(j = 0; j < arraylen(Logic1); j += skip) {
1343 corr1 += Logic1[j]*GraphBuffer[i+(j/skip)];
1344 }
1345 for(j = 0; j < arraylen(FrameEOF); j += skip) {
1346 corrEOF += FrameEOF[j]*GraphBuffer[i+(j/skip)];
1347 }
1348 // Even things out by the length of the target waveform.
1349 corr0 *= 4;
1350 corr1 *= 4;
1351
1352 if(corrEOF > corr1 && corrEOF > corr0) {
1353 PrintToScrollback("EOF at %d", i);
1354 break;
1355 } else if(corr1 > corr0) {
1356 i += arraylen(Logic1)/skip;
1357 outBuf[k] |= mask;
1358 } else {
1359 i += arraylen(Logic0)/skip;
1360 }
1361 mask <<= 1;
1362 if(mask == 0) {
1363 k++;
1364 mask = 0x01;
1365 }
1366 if((i+(int)arraylen(FrameEOF)) >= GraphTraceLen) {
1367 PrintToScrollback("ran off end!");
1368 break;
1369 }
1370 }
1371 if(mask != 0x01) {
1372 PrintToScrollback("error, uneven octet! (discard extra bits!)");
1373 PrintToScrollback(" mask=%02x", mask);
1374 }
1375 PrintToScrollback("%d octets", k);
1376
1377 for(i = 0; i < k; i++) {
1378 PrintToScrollback("# %2d: %02x ", i, outBuf[i]);
1379 }
1380 PrintToScrollback("CRC=%04x", Iso15693Crc(outBuf, k-2));
1381 }
1382
1383 static void CmdFSKdemod(char *cmdline)
1384 {
1385 static const int LowTone[] = {
1386 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1387 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1388 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1389 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1390 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
1391 };
1392 static const int HighTone[] = {
1393 1, 1, 1, 1, 1, -1, -1, -1, -1,
1394 1, 1, 1, 1, -1, -1, -1, -1,
1395 1, 1, 1, 1, -1, -1, -1, -1,
1396 1, 1, 1, 1, -1, -1, -1, -1,
1397 1, 1, 1, 1, -1, -1, -1, -1,
1398 1, 1, 1, 1, -1, -1, -1, -1, -1,
1399 };
1400
1401 int lowLen = sizeof(LowTone)/sizeof(int);
1402 int highLen = sizeof(HighTone)/sizeof(int);
1403 int convLen = (highLen>lowLen)?highLen:lowLen;
1404 uint32_t hi = 0, lo = 0;
1405
1406 int i, j;
1407 int minMark=0, maxMark=0;
1408
1409 for(i = 0; i < GraphTraceLen - convLen; i++) {
1410 int lowSum = 0, highSum = 0;
1411
1412 for(j = 0; j < lowLen; j++) {
1413 lowSum += LowTone[j]*GraphBuffer[i+j];
1414 }
1415 for(j = 0; j < highLen; j++) {
1416 highSum += HighTone[j]*GraphBuffer[i+j];
1417 }
1418 lowSum = abs((100*lowSum) / lowLen);
1419 highSum = abs((100*highSum) / highLen);
1420 GraphBuffer[i] = (highSum << 16) | lowSum;
1421 }
1422
1423 for(i = 0; i < GraphTraceLen - convLen - 16; i++) {
1424 int j;
1425 int lowTot = 0, highTot = 0;
1426 // 10 and 8 are f_s divided by f_l and f_h, rounded
1427 for(j = 0; j < 10; j++) {
1428 lowTot += (GraphBuffer[i+j] & 0xffff);
1429 }
1430 for(j = 0; j < 8; j++) {
1431 highTot += (GraphBuffer[i+j] >> 16);
1432 }
1433 GraphBuffer[i] = lowTot - highTot;
1434 if (GraphBuffer[i]>maxMark) maxMark=GraphBuffer[i];
1435 if (GraphBuffer[i]<minMark) minMark=GraphBuffer[i];
1436 }
1437
1438 GraphTraceLen -= (convLen + 16);
1439
1440 RepaintGraphWindow();
1441
1442 // Find bit-sync (3 lo followed by 3 high)
1443 int max = 0, maxPos = 0;
1444 for(i = 0; i < 6000; i++) {
1445 int dec = 0;
1446 for(j = 0; j < 3*lowLen; j++) {
1447 dec -= GraphBuffer[i+j];
1448 }
1449 for(; j < 3*(lowLen + highLen ); j++) {
1450 dec += GraphBuffer[i+j];
1451 }
1452 if(dec > max) {
1453 max = dec;
1454 maxPos = i;
1455 }
1456 }
1457
1458 // place start of bit sync marker in graph
1459 GraphBuffer[maxPos] = maxMark;
1460 GraphBuffer[maxPos+1] = minMark;
1461
1462 maxPos += j;
1463
1464 // place end of bit sync marker in graph
1465 GraphBuffer[maxPos] = maxMark;
1466 GraphBuffer[maxPos+1] = minMark;
1467
1468 PrintToScrollback("actual data bits start at sample %d", maxPos);
1469 PrintToScrollback("length %d/%d", highLen, lowLen);
1470
1471 uint8_t bits[46];
1472 bits[sizeof(bits)-1] = '\0';
1473
1474 // find bit pairs and manchester decode them
1475 for(i = 0; i < arraylen(bits)-1; i++) {
1476 int dec = 0;
1477 for(j = 0; j < lowLen; j++) {
1478 dec -= GraphBuffer[maxPos+j];
1479 }
1480 for(; j < lowLen + highLen; j++) {
1481 dec += GraphBuffer[maxPos+j];
1482 }
1483 maxPos += j;
1484 // place inter bit marker in graph
1485 GraphBuffer[maxPos] = maxMark;
1486 GraphBuffer[maxPos+1] = minMark;
1487
1488 // hi and lo form a 64 bit pair
1489 hi = (hi<<1)|(lo>>31);
1490 lo = (lo<<1);
1491 // store decoded bit as binary (in hi/lo) and text (in bits[])
1492 if(dec<0) {
1493 bits[i] = '1';
1494 lo|=1;
1495 } else {
1496 bits[i] = '0';
1497 }
1498 }
1499 PrintToScrollback("bits: '%s'", bits);
1500 PrintToScrollback("hex: %08x %08x", hi, lo);
1501 }
1502
1503 // read a TI tag and return its ID
1504 static void CmdTIRead(char *str)
1505 {
1506 UsbCommand c={CMD_READ_TI_TYPE};
1507 SendCommand(&c);
1508 }
1509
1510 // write new data to a r/w TI tag
1511 static void CmdTIWrite(char *str)
1512 {
1513 UsbCommand c={CMD_WRITE_TI_TYPE};
1514 int res=0;
1515
1516 res = sscanf(str, "0x%x 0x%x 0x%x ", &c.arg[0], &c.arg[1], &c.arg[2]);
1517 if (res == 2) c.arg[2]=0;
1518 if (res<2)
1519 PrintToScrollback("Please specify the data as two hex strings, optionally the CRC as a third");
1520 else
1521 SendCommand(&c);
1522 }
1523
1524 static void CmdTIDemod(char *cmdline)
1525 {
1526 /* MATLAB as follows:
1527 f_s = 2000000; % sampling frequency
1528 f_l = 123200; % low FSK tone
1529 f_h = 134200; % high FSK tone
1530
1531 T_l = 119e-6; % low bit duration
1532 T_h = 130e-6; % high bit duration
1533
1534 l = 2*pi*ones(1, floor(f_s*T_l))*(f_l/f_s);
1535 h = 2*pi*ones(1, floor(f_s*T_h))*(f_h/f_s);
1536
1537 l = sign(sin(cumsum(l)));
1538 h = sign(sin(cumsum(h)));
1539 */
1540
1541 // 2M*16/134.2k = 238
1542 static const int LowTone[] = {
1543 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1544 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1545 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1546 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1547 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1548 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1549 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1550 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1551 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1552 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1553 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1554 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1555 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1556 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1557 1, 1, 1, 1, 1, 1, 1, 1, -1, -1
1558 };
1559 // 2M*16/123.2k = 260
1560 static const int HighTone[] = {
1561 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1562 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1563 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1564 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1565 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1566 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1567 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1568 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1569 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1570 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1571 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1572 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1573 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1574 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1575 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1576 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
1577 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
1578 1, 1, 1, 1, 1, 1, 1, 1
1579 };
1580 int lowLen = sizeof(LowTone)/sizeof(int);
1581 int highLen = sizeof(HighTone)/sizeof(int);
1582 int convLen = (highLen>lowLen)?highLen:lowLen;
1583 uint16_t crc;
1584 int i, j, TagType;
1585 int lowSum = 0, highSum = 0;;
1586 int lowTot = 0, highTot = 0;
1587
1588 for(i = 0; i < GraphTraceLen - convLen; i++) {
1589 lowSum = 0;
1590 highSum = 0;;
1591
1592 for(j = 0; j < lowLen; j++) {
1593 lowSum += LowTone[j]*GraphBuffer[i+j];
1594 }
1595 for(j = 0; j < highLen; j++) {
1596 highSum += HighTone[j]*GraphBuffer[i+j];
1597 }
1598 lowSum = abs((100*lowSum) / lowLen);
1599 highSum = abs((100*highSum) / highLen);
1600 lowSum = (lowSum<0)?-lowSum:lowSum;
1601 highSum = (highSum<0)?-highSum:highSum;
1602
1603 GraphBuffer[i] = (highSum << 16) | lowSum;
1604 }
1605
1606 for(i = 0; i < GraphTraceLen - convLen - 16; i++) {
1607 lowTot = 0;
1608 highTot = 0;
1609 // 16 and 15 are f_s divided by f_l and f_h, rounded
1610 for(j = 0; j < 16; j++) {
1611 lowTot += (GraphBuffer[i+j] & 0xffff);
1612 }
1613 for(j = 0; j < 15; j++) {
1614 highTot += (GraphBuffer[i+j] >> 16);
1615 }
1616 GraphBuffer[i] = lowTot - highTot;
1617 }
1618
1619 GraphTraceLen -= (convLen + 16);
1620
1621 RepaintGraphWindow();
1622
1623 // TI tag data format is 16 prebits, 8 start bits, 64 data bits,
1624 // 16 crc CCITT bits, 8 stop bits, 15 end bits
1625
1626 // the 16 prebits are always low
1627 // the 8 start and stop bits of a tag must match
1628 // the start/stop prebits of a ro tag are 01111110
1629 // the start/stop prebits of a rw tag are 11111110
1630 // the 15 end bits of a ro tag are all low
1631 // the 15 end bits of a rw tag match bits 15-1 of the data bits
1632
1633 // Okay, so now we have unsliced soft decisions;
1634 // find bit-sync, and then get some bits.
1635 // look for 17 low bits followed by 6 highs (common pattern for ro and rw tags)
1636 int max = 0, maxPos = 0;
1637 for(i = 0; i < 6000; i++) {
1638 int j;
1639 int dec = 0;
1640 // searching 17 consecutive lows
1641 for(j = 0; j < 17*lowLen; j++) {
1642 dec -= GraphBuffer[i+j];
1643 }
1644 // searching 7 consecutive highs
1645 for(; j < 17*lowLen + 6*highLen; j++) {
1646 dec += GraphBuffer[i+j];
1647 }
1648 if(dec > max) {
1649 max = dec;
1650 maxPos = i;
1651 }
1652 }
1653
1654 // place a marker in the buffer to visually aid location
1655 // of the start of sync
1656 GraphBuffer[maxPos] = 800;
1657 GraphBuffer[maxPos+1] = -800;
1658
1659 // advance pointer to start of actual data stream (after 16 pre and 8 start bits)
1660 maxPos += 17*lowLen;
1661 maxPos += 6*highLen;
1662
1663 // place a marker in the buffer to visually aid location
1664 // of the end of sync
1665 GraphBuffer[maxPos] = 800;
1666 GraphBuffer[maxPos+1] = -800;
1667
1668 PrintToScrollback("actual data bits start at sample %d", maxPos);
1669
1670 PrintToScrollback("length %d/%d", highLen, lowLen);
1671
1672 uint8_t bits[1+64+16+8+16];
1673 bits[sizeof(bits)-1] = '\0';
1674
1675 uint32_t shift3 = 0x7e000000, shift2 = 0, shift1 = 0, shift0 = 0;
1676
1677 for(i = 0; i < arraylen(bits)-1; i++) {
1678 int high = 0;
1679 int low = 0;
1680 int j;
1681 for(j = 0; j < lowLen; j++) {
1682 low -= GraphBuffer[maxPos+j];
1683 }
1684 for(j = 0; j < highLen; j++) {
1685 high += GraphBuffer[maxPos+j];
1686 }
1687
1688 if(high > low) {
1689 bits[i] = '1';
1690 maxPos += highLen;
1691 // bitstream arrives lsb first so shift right
1692 shift3 |= (1<<31);
1693 } else {
1694 bits[i] = '.';
1695 maxPos += lowLen;
1696 }
1697
1698 // 128 bit right shift register
1699 shift0 = (shift0>>1) | (shift1 << 31);
1700 shift1 = (shift1>>1) | (shift2 << 31);
1701 shift2 = (shift2>>1) | (shift3 << 31);
1702 shift3 >>= 1;
1703
1704 // place a marker in the buffer between bits to visually aid location
1705 GraphBuffer[maxPos] = 800;
1706 GraphBuffer[maxPos+1] = -800;
1707 }
1708 PrintToScrollback("Info: raw tag bits = %s", bits);
1709
1710 TagType = (shift3>>8)&0xff;
1711 if ( TagType != ((shift0>>16)&0xff) ) {
1712 PrintToScrollback("Error: start and stop bits do not match!");
1713 return;
1714 }
1715 else if (TagType == 0x7e) {
1716 PrintToScrollback("Info: Readonly TI tag detected.");
1717 return;
1718 }
1719 else if (TagType == 0xfe) {
1720 PrintToScrollback("Info: Rewriteable TI tag detected.");
1721
1722 // put 64 bit data into shift1 and shift0
1723 shift0 = (shift0>>24) | (shift1 << 8);
1724 shift1 = (shift1>>24) | (shift2 << 8);
1725
1726 // align 16 bit crc into lower half of shift2
1727 shift2 = ((shift2>>24) | (shift3 << 8)) & 0x0ffff;
1728
1729 // align 16 bit "end bits" or "ident" into lower half of shift3
1730 shift3 >>= 16;
1731
1732 // only 15 bits compare, last bit of ident is not valid
1733 if ( (shift3^shift0)&0x7fff ) {
1734 PrintToScrollback("Error: Ident mismatch!");
1735 }
1736 // WARNING the order of the bytes in which we calc crc below needs checking
1737 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
1738 // bytes in reverse or something
1739 // calculate CRC
1740 crc=0;
1741 crc = update_crc16(crc, (shift0)&0xff);
1742 crc = update_crc16(crc, (shift0>>8)&0xff);
1743 crc = update_crc16(crc, (shift0>>16)&0xff);
1744 crc = update_crc16(crc, (shift0>>24)&0xff);
1745 crc = update_crc16(crc, (shift1)&0xff);
1746 crc = update_crc16(crc, (shift1>>8)&0xff);
1747 crc = update_crc16(crc, (shift1>>16)&0xff);
1748 crc = update_crc16(crc, (shift1>>24)&0xff);
1749 PrintToScrollback("Info: Tag data = %08X%08X", shift1, shift0);
1750 if (crc != (shift2&0xffff)) {
1751 PrintToScrollback("Error: CRC mismatch, calculated %04X, got ^04X", crc, shift2&0xffff);
1752 } else {
1753 PrintToScrollback("Info: CRC %04X is good", crc);
1754 }
1755 }
1756 else {
1757 PrintToScrollback("Unknown tag type.");
1758 return;
1759 }
1760 }
1761
1762 static void CmdNorm(char *str)
1763 {
1764 int i;
1765 int max = INT_MIN, min = INT_MAX;
1766 for(i = 10; i < GraphTraceLen; i++) {
1767 if(GraphBuffer[i] > max) {
1768 max = GraphBuffer[i];
1769 }
1770 if(GraphBuffer[i] < min) {
1771 min = GraphBuffer[i];
1772 }
1773 }
1774 if(max != min) {
1775 for(i = 0; i < GraphTraceLen; i++) {
1776 GraphBuffer[i] = (GraphBuffer[i] - ((max + min)/2))*1000/
1777 (max - min);
1778 }
1779 }
1780 RepaintGraphWindow();
1781 }
1782
1783 static void CmdAmp(char *str)
1784 {
1785 int i, rising, falling;
1786 int max = INT_MIN, min = INT_MAX;
1787 for(i = 10; i < GraphTraceLen; i++) {
1788 if(GraphBuffer[i] > max) {
1789 max = GraphBuffer[i];
1790 }
1791 if(GraphBuffer[i] < min) {
1792 min = GraphBuffer[i];
1793 }
1794 }
1795 if(max != min) {
1796 rising= falling= 0;
1797 for(i = 0; i < GraphTraceLen; i++) {
1798 if(GraphBuffer[i+1] < GraphBuffer[i]) {
1799 if(rising) {
1800 GraphBuffer[i]= max;
1801 rising= 0;
1802 }
1803 falling= 1;
1804 }
1805 if(GraphBuffer[i+1] > GraphBuffer[i]) {
1806 if(falling) {
1807 GraphBuffer[i]= min;
1808 falling= 0;
1809 }
1810 rising= 1;
1811 }
1812 }
1813 }
1814 RepaintGraphWindow();
1815 }
1816
1817 static void CmdDec(char *str)
1818 {
1819 int i;
1820 for(i = 0; i < (GraphTraceLen/2); i++) {
1821 GraphBuffer[i] = GraphBuffer[i*2];
1822 }
1823 GraphTraceLen /= 2;
1824 PrintToScrollback("decimated by 2");
1825 RepaintGraphWindow();
1826 }
1827
1828 static void CmdHpf(char *str)
1829 {
1830 int i;
1831 int accum = 0;
1832 for(i = 10; i < GraphTraceLen; i++) {
1833 accum += GraphBuffer[i];
1834 }
1835 accum /= (GraphTraceLen - 10);
1836 for(i = 0; i < GraphTraceLen; i++) {
1837 GraphBuffer[i] -= accum;
1838 }
1839
1840 RepaintGraphWindow();
1841 }
1842
1843 static void CmdZerocrossings(char *str)
1844 {
1845 int i;
1846 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1847 CmdHpf("");
1848
1849 int sign = 1;
1850 int zc = 0;
1851 int lastZc = 0;
1852 for(i = 0; i < GraphTraceLen; i++) {
1853 if(GraphBuffer[i]*sign >= 0) {
1854 // No change in sign, reproduce the previous sample count.
1855 zc++;
1856 GraphBuffer[i] = lastZc;
1857 } else {
1858 // Change in sign, reset the sample count.
1859 sign = -sign;
1860 GraphBuffer[i] = lastZc;
1861 if(sign > 0) {
1862 lastZc = zc;
1863 zc = 0;
1864 }
1865 }
1866 }
1867
1868 RepaintGraphWindow();
1869 }
1870
1871 static void CmdThreshold(char *str)
1872 {
1873 int i;
1874 int threshold = atoi(str);
1875
1876 for(i = 0; i < GraphTraceLen; i++) {
1877 if(GraphBuffer[i]>= threshold)
1878 GraphBuffer[i]=1;
1879 else
1880 GraphBuffer[i]=-1;
1881 }
1882 RepaintGraphWindow();
1883 }
1884
1885 static void CmdLtrim(char *str)
1886 {
1887 int i;
1888 int ds = atoi(str);
1889
1890 for(i = ds; i < GraphTraceLen; i++) {
1891 GraphBuffer[i-ds] = GraphBuffer[i];
1892 }
1893 GraphTraceLen -= ds;
1894
1895 RepaintGraphWindow();
1896 }
1897
1898 static void CmdAutoCorr(char *str)
1899 {
1900 static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];
1901
1902 int window = atoi(str);
1903
1904 if(window == 0) {
1905 PrintToScrollback("needs a window");
1906 return;
1907 }
1908
1909 if(window >= GraphTraceLen) {
1910 PrintToScrollback("window must be smaller than trace (%d samples)",
1911 GraphTraceLen);
1912 return;
1913 }
1914
1915 PrintToScrollback("performing %d correlations", GraphTraceLen - window);
1916
1917 int i;
1918 for(i = 0; i < GraphTraceLen - window; i++) {
1919 int sum = 0;
1920 int j;
1921 for(j = 0; j < window; j++) {
1922 sum += (GraphBuffer[j]*GraphBuffer[i+j]) / 256;
1923 }
1924 CorrelBuffer[i] = sum;
1925 }
1926 GraphTraceLen = GraphTraceLen - window;
1927 memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen*sizeof(int));
1928
1929 RepaintGraphWindow();
1930 }
1931
1932 static void CmdVchdemod(char *str)
1933 {
1934 // Is this the entire sync pattern, or does this also include some
1935 // data bits that happen to be the same everywhere? That would be
1936 // lovely to know.
1937 static const int SyncPattern[] = {
1938 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1939 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1940 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1941 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1942 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1943 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1944 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1945 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1946 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1947 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1948 };
1949
1950 // So first, we correlate for the sync pattern, and mark that.
1951 int bestCorrel = 0, bestPos = 0;
1952 int i;
1953 // It does us no good to find the sync pattern, with fewer than
1954 // 2048 samples after it...
1955 for(i = 0; i < (GraphTraceLen-2048); i++) {
1956 int sum = 0;
1957 int j;
1958 for(j = 0; j < arraylen(SyncPattern); j++) {
1959 sum += GraphBuffer[i+j]*SyncPattern[j];
1960 }
1961 if(sum > bestCorrel) {
1962 bestCorrel = sum;
1963 bestPos = i;
1964 }
1965 }
1966 PrintToScrollback("best sync at %d [metric %d]", bestPos, bestCorrel);
1967
1968 char bits[257];
1969 bits[256] = '\0';
1970
1971 int worst = INT_MAX;
1972 int worstPos;
1973
1974 for(i = 0; i < 2048; i += 8) {
1975 int sum = 0;
1976 int j;
1977 for(j = 0; j < 8; j++) {
1978 sum += GraphBuffer[bestPos+i+j];
1979 }
1980 if(sum < 0) {
1981 bits[i/8] = '.';
1982 } else {
1983 bits[i/8] = '1';
1984 }
1985 if(abs(sum) < worst) {
1986 worst = abs(sum);
1987 worstPos = i;
1988 }
1989 }
1990 PrintToScrollback("bits:");
1991 PrintToScrollback("%s", bits);
1992 PrintToScrollback("worst metric: %d at pos %d", worst, worstPos);
1993
1994 if(strcmp(str, "clone")==0) {
1995 GraphTraceLen = 0;
1996 char *s;
1997 for(s = bits; *s; s++) {
1998 int j;
1999 for(j = 0; j < 16; j++) {
2000 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
2001 }
2002 }
2003 RepaintGraphWindow();
2004 }
2005 }
2006
2007 static void CmdIndalademod(char *str)
2008 {
2009 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
2010
2011 int state = -1;
2012 int count = 0;
2013 int i, j;
2014 // worst case with GraphTraceLen=64000 is < 4096
2015 // under normal conditions it's < 2048
2016 uint8_t rawbits[4096];
2017 int rawbit = 0;
2018 int worst = 0, worstPos = 0;
2019 PrintToScrollback("Expecting a bit less than %d raw bits", GraphTraceLen/32);
2020 for(i = 0; i < GraphTraceLen-1; i += 2) {
2021 count+=1;
2022 if((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
2023 if (state == 0) {
2024 for(j = 0; j < count - 8; j += 16) {
2025 rawbits[rawbit++] = 0;
2026 }
2027 if ((abs(count - j)) > worst) {
2028 worst = abs(count - j);
2029 worstPos = i;
2030 }
2031 }
2032 state = 1;
2033 count=0;
2034 } else if((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
2035 if (state == 1) {
2036 for(j = 0; j < count - 8; j += 16) {
2037 rawbits[rawbit++] = 1;
2038 }
2039 if ((abs(count - j)) > worst) {
2040 worst = abs(count - j);
2041 worstPos = i;
2042 }
2043 }
2044 state = 0;
2045 count=0;
2046 }
2047 }
2048 PrintToScrollback("Recovered %d raw bits", rawbit);
2049 PrintToScrollback("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
2050
2051 // Finding the start of a UID
2052 int uidlen, long_wait;
2053 if(strcmp(str, "224") == 0) {
2054 uidlen=224;
2055 long_wait=30;
2056 } else {
2057 uidlen=64;
2058 long_wait=29;
2059 }
2060 int start;
2061 int first = 0;
2062 for(start = 0; start <= rawbit - uidlen; start++) {
2063 first = rawbits[start];
2064 for(i = start; i < start + long_wait; i++) {
2065 if(rawbits[i] != first) {
2066 break;
2067 }
2068 }
2069 if(i == (start + long_wait)) {
2070 break;
2071 }
2072 }
2073 if(start == rawbit - uidlen + 1) {
2074 PrintToScrollback("nothing to wait for");
2075 return;
2076 }
2077
2078 // Inverting signal if needed
2079 if(first == 1) {
2080 for(i = start; i < rawbit; i++) {
2081 rawbits[i] = !rawbits[i];
2082 }
2083 }
2084
2085 // Dumping UID
2086 uint8_t bits[224];
2087 char showbits[225];
2088 showbits[uidlen]='\0';
2089 int bit;
2090 i = start;
2091 int times = 0;
2092 if(uidlen > rawbit) {
2093 PrintToScrollback("Warning: not enough raw bits to get a full UID");
2094 for(bit = 0; bit < rawbit; bit++) {
2095 bits[bit] = rawbits[i++];
2096 // As we cannot know the parity, let's use "." and "/"
2097 showbits[bit] = '.' + bits[bit];
2098 }
2099 showbits[bit+1]='\0';
2100 PrintToScrollback("Partial UID=%s", showbits);
2101 return;
2102 } else {
2103 for(bit = 0; bit < uidlen; bit++) {
2104 bits[bit] = rawbits[i++];
2105 showbits[bit] = '0' + bits[bit];
2106 }
2107 times = 1;
2108 }
2109 PrintToScrollback("UID=%s", showbits);
2110
2111 // Checking UID against next occurences
2112 for(; i + uidlen <= rawbit;) {
2113 int failed = 0;
2114 for(bit = 0; bit < uidlen; bit++) {
2115 if(bits[bit] != rawbits[i++]) {
2116 failed = 1;
2117 break;
2118 }
2119 }
2120 if (failed == 1) {
2121 break;
2122 }
2123 times += 1;
2124 }
2125 PrintToScrollback("Occurences: %d (expected %d)", times, (rawbit - start) / uidlen);
2126
2127 // Remodulating for tag cloning
2128 GraphTraceLen = 32*uidlen;
2129 i = 0;
2130 int phase = 0;
2131 for(bit = 0; bit < uidlen; bit++) {
2132 if(bits[bit] == 0) {
2133 phase = 0;
2134 } else {
2135 phase = 1;
2136 }
2137 int j;
2138 for(j = 0; j < 32; j++) {
2139 GraphBuffer[i++] = phase;
2140 phase = !phase;
2141 }
2142 }
2143
2144 RepaintGraphWindow();
2145 }
2146
2147 static void CmdFlexdemod(char *str)
2148 {
2149 int i;
2150 for(i = 0; i < GraphTraceLen; i++) {
2151 if(GraphBuffer[i] < 0) {
2152 GraphBuffer[i] = -1;
2153 } else {
2154 GraphBuffer[i] = 1;
2155 }
2156 }
2157
2158 #define LONG_WAIT 100
2159 int start;
2160 for(start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
2161 int first = GraphBuffer[start];
2162 for(i = start; i < start + LONG_WAIT; i++) {
2163 if(GraphBuffer[i] != first) {
2164 break;
2165 }
2166 }
2167 if(i == (start + LONG_WAIT)) {
2168 break;
2169 }
2170 }
2171 if(start == GraphTraceLen - LONG_WAIT) {
2172 PrintToScrollback("nothing to wait for");
2173 return;
2174 }
2175
2176 GraphBuffer[start] = 2;
2177 GraphBuffer[start+1] = -2;
2178
2179 uint8_t bits[64];
2180
2181 int bit;
2182 i = start;
2183 for(bit = 0; bit < 64; bit++) {
2184 int j;
2185 int sum = 0;
2186 for(j = 0; j < 16; j++) {
2187 sum += GraphBuffer[i++];
2188 }
2189 if(sum > 0) {
2190 bits[bit] = 1;
2191 } else {
2192 bits[bit] = 0;
2193 }
2194 PrintToScrollback("bit %d sum %d", bit, sum);
2195 }
2196
2197 for(bit = 0; bit < 64; bit++) {
2198 int j;
2199 int sum = 0;
2200 for(j = 0; j < 16; j++) {
2201 sum += GraphBuffer[i++];
2202 }
2203 if(sum > 0 && bits[bit] != 1) {
2204 PrintToScrollback("oops1 at %d", bit);
2205 }
2206 if(sum < 0 && bits[bit] != 0) {
2207 PrintToScrollback("oops2 at %d", bit);
2208 }
2209 }
2210
2211 GraphTraceLen = 32*64;
2212 i = 0;
2213 int phase = 0;
2214 for(bit = 0; bit < 64; bit++) {
2215 if(bits[bit] == 0) {
2216 phase = 0;
2217 } else {
2218 phase = 1;
2219 }
2220 int j;
2221 for(j = 0; j < 32; j++) {
2222 GraphBuffer[i++] = phase;
2223 phase = !phase;
2224 }
2225 }
2226
2227 RepaintGraphWindow();
2228 }
2229
2230 /*
2231 * Generic command to demodulate ASK.
2232 *
2233 * Argument is convention: positive or negative (High mod means zero
2234 * or high mod means one)
2235 *
2236 * Updates the Graph trace with 0/1 values
2237 *
2238 * Arguments:
2239 * c : 0 or 1
2240 */
2241
2242 static void Cmdaskdemod(char *str) {
2243 int i;
2244 int c, high = 0, low = 0;
2245
2246 // TODO: complain if we do not give 2 arguments here !
2247 // (AL - this doesn't make sense! we're only using one argument!!!)
2248 sscanf(str, "%i", &c);
2249
2250 /* Detect high and lows and clock */
2251 // (AL - clock???)
2252 for (i = 0; i < GraphTraceLen; i++)
2253 {
2254 if (GraphBuffer[i] > high)
2255 high = GraphBuffer[i];
2256 else if (GraphBuffer[i] < low)
2257 low = GraphBuffer[i];
2258 }
2259 if(c != 0 && c != 1) {
2260 PrintToScrollback("Invalid argument: %s",str);
2261 return;
2262 }
2263
2264 if (GraphBuffer[0] > 0) {
2265 GraphBuffer[0] = 1-c;
2266 } else {
2267 GraphBuffer[0] = c;
2268 }
2269 for(i=1;i<GraphTraceLen;i++) {
2270 /* Transitions are detected at each peak
2271 * Transitions are either:
2272 * - we're low: transition if we hit a high
2273 * - we're high: transition if we hit a low
2274 * (we need to do it this way because some tags keep high or
2275 * low for long periods, others just reach the peak and go
2276 * down)
2277 */
2278 if ((GraphBuffer[i]==high) && (GraphBuffer[i-1] == c)) {
2279 GraphBuffer[i]=1-c;
2280 } else if ((GraphBuffer[i]==low) && (GraphBuffer[i-1] == (1-c))){
2281 GraphBuffer[i] = c;
2282 } else {
2283 /* No transition */
2284 GraphBuffer[i] = GraphBuffer[i-1];
2285 }
2286 }
2287 RepaintGraphWindow();
2288 }
2289
2290 /* Print our clock rate */
2291 static void Cmddetectclockrate(char *str)
2292 {
2293 int clock = detectclock(0);
2294 PrintToScrollback("Auto-detected clock rate: %d", clock);
2295 }
2296
2297 /*
2298 * Detect clock rate
2299 */
2300 int detectclock(int peak)
2301 {
2302 int i;
2303 int clock = 0xFFFF;
2304 int lastpeak = 0;
2305
2306 /* Detect peak if we don't have one */
2307 if (!peak)
2308 for (i = 0; i < GraphTraceLen; i++)
2309 if (GraphBuffer[i] > peak)
2310 peak = GraphBuffer[i];
2311
2312 for (i = 1; i < GraphTraceLen; i++)
2313 {
2314 /* If this is the beginning of a peak */
2315 if (GraphBuffer[i-1] != GraphBuffer[i] && GraphBuffer[i] == peak)
2316 {
2317 /* Find lowest difference between peaks */
2318 if (lastpeak && i - lastpeak < clock)
2319 {
2320 clock = i - lastpeak;
2321 }
2322 lastpeak = i;
2323 }
2324 }
2325
2326 return clock;
2327 }
2328
2329 /* Get or auto-detect clock rate */
2330 int GetClock(char *str, int peak)
2331 {
2332 int clock;
2333
2334 sscanf(str, "%i", &clock);
2335 if (!strcmp(str, ""))
2336 clock = 0;
2337
2338 /* Auto-detect clock */
2339 if (!clock)
2340 {
2341 clock = detectclock(peak);
2342
2343 /* Only print this message if we're not looping something */
2344 if (!go)
2345 PrintToScrollback("Auto-detected clock rate: %d", clock);
2346 }
2347
2348 return clock;
2349 }
2350
2351 /*
2352 * Convert to a bitstream
2353 */
2354 static void Cmdbitstream(char *str) {
2355 int i, j;
2356 int bit;
2357 int gtl;
2358 int clock;
2359 int low = 0;
2360 int high = 0;
2361 int hithigh, hitlow, first;
2362
2363 /* Detect high and lows and clock */
2364 for (i = 0; i < GraphTraceLen; i++)
2365 {
2366 if (GraphBuffer[i] > high)
2367 high = GraphBuffer[i];
2368 else if (GraphBuffer[i] < low)
2369 low = GraphBuffer[i];
2370 }
2371
2372 /* Get our clock */
2373 clock = GetClock(str, high);
2374
2375 gtl = CmdClearGraph(0);
2376
2377 bit = 0;
2378 for (i = 0; i < (int)(gtl / clock); i++)
2379 {
2380 hithigh = 0;
2381 hitlow = 0;
2382 first = 1;
2383
2384 /* Find out if we hit both high and low peaks */
2385 for (j = 0; j < clock; j++)
2386 {
2387 if (GraphBuffer[(i * clock) + j] == high)
2388 hithigh = 1;
2389 else if (GraphBuffer[(i * clock) + j] == low)
2390 hitlow = 1;
2391
2392 /* it doesn't count if it's the first part of our read
2393 because it's really just trailing from the last sequence */
2394 if (first && (hithigh || hitlow))
2395 hithigh = hitlow = 0;
2396 else
2397 first = 0;
2398
2399 if (hithigh && hitlow)
2400 break;
2401 }
2402
2403 /* If we didn't hit both high and low peaks, we had a bit transition */
2404 if (!hithigh || !hitlow)
2405 bit ^= 1;
2406
2407 CmdAppendGraph(0, clock, bit);
2408 // for (j = 0; j < (int)(clock/2); j++)
2409 // GraphBuffer[(i * clock) + j] = bit ^ 1;
2410 // for (j = (int)(clock/2); j < clock; j++)
2411 // GraphBuffer[(i * clock) + j] = bit;
2412 }
2413
2414 RepaintGraphWindow();
2415 }
2416
2417 /* Modulate our data into manchester */
2418 static void Cmdmanchestermod(char *str)
2419 {
2420 int i, j;
2421 int clock;
2422 int bit, lastbit, wave;
2423
2424 /* Get our clock */
2425 clock = GetClock(str, 0);
2426
2427 wave = 0;
2428 lastbit = 1;
2429 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
2430 {
2431 bit = GraphBuffer[i * clock] ^ 1;
2432
2433 for (j = 0; j < (int)(clock/2); j++)
2434 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave;
2435 for (j = (int)(clock/2); j < clock; j++)
2436 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1;
2437
2438 /* Keep track of how we start our wave and if we changed or not this time */
2439 wave ^= bit ^ lastbit;
2440 lastbit = bit;
2441 }
2442
2443 RepaintGraphWindow();
2444 }
2445
2446 /*
2447 * Manchester demodulate a bitstream. The bitstream needs to be already in
2448 * the GraphBuffer as 0 and 1 values
2449 *
2450 * Give the clock rate as argument in order to help the sync - the algorithm
2451 * resyncs at each pulse anyway.
2452 *
2453 * Not optimized by any means, this is the 1st time I'm writing this type of
2454 * routine, feel free to improve...
2455 *
2456 * 1st argument: clock rate (as number of samples per clock rate)
2457 * Typical values can be 64, 32, 128...
2458 */
2459 static void Cmdmanchesterdemod(char *str) {
2460 int i, j, invert= 0;
2461 int bit;
2462 int clock;
2463 int lastval;
2464 int low = 0;
2465 int high = 0;
2466 int hithigh, hitlow, first;
2467 int lc = 0;
2468 int bitidx = 0;
2469 int bit2idx = 0;
2470 int warnings = 0;
2471
2472 /* check if we're inverting output */
2473 if(*str == 'i')
2474 {
2475 PrintToScrollback("Inverting output");
2476 invert= 1;
2477 do
2478 ++str;
2479 while(*str == ' '); // in case a 2nd argument was given
2480 }
2481
2482 /* Holds the decoded bitstream: each clock period contains 2 bits */
2483 /* later simplified to 1 bit after manchester decoding. */
2484 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
2485 /* int BitStream[GraphTraceLen*2/clock+10]; */
2486
2487 /* But it does not work if compiling on WIndows: therefore we just allocate a */
2488 /* large array */
2489 int BitStream[MAX_GRAPH_TRACE_LEN];
2490
2491 /* Detect high and lows */
2492 for (i = 0; i < GraphTraceLen; i++)
2493 {
2494 if (GraphBuffer[i] > high)
2495 high = GraphBuffer[i];
2496 else if (GraphBuffer[i] < low)
2497 low = GraphBuffer[i];
2498 }
2499
2500 /* Get our clock */
2501 clock = GetClock(str, high);
2502
2503 int tolerance = clock/4;
2504
2505 /* Detect first transition */
2506 /* Lo-Hi (arbitrary) */
2507 /* skip to the first high */
2508 for (i= 0; i < GraphTraceLen; i++)
2509 if(GraphBuffer[i] == high)
2510 break;
2511 /* now look for the first low */
2512 for (; i < GraphTraceLen; i++)
2513 {
2514 if (GraphBuffer[i] == low)
2515 {
2516 lastval = i;
2517 break;
2518 }
2519 }
2520
2521 /* If we're not working with 1/0s, demod based off clock */
2522 if (high != 1)
2523 {
2524 bit = 0; /* We assume the 1st bit is zero, it may not be
2525 * the case: this routine (I think) has an init problem.
2526 * Ed.
2527 */
2528 for (; i < (int)(GraphTraceLen / clock); i++)
2529 {
2530 hithigh = 0;
2531 hitlow = 0;
2532 first = 1;
2533
2534 /* Find out if we hit both high and low peaks */
2535 for (j = 0; j < clock; j++)
2536 {
2537 if (GraphBuffer[(i * clock) + j] == high)
2538 hithigh = 1;
2539 else if (GraphBuffer[(i * clock) + j] == low)
2540 hitlow = 1;
2541
2542 /* it doesn't count if it's the first part of our read
2543 because it's really just trailing from the last sequence */
2544 if (first && (hithigh || hitlow))
2545 hithigh = hitlow = 0;
2546 else
2547 first = 0;
2548
2549 if (hithigh && hitlow)
2550 break;
2551 }
2552
2553 /* If we didn't hit both high and low peaks, we had a bit transition */
2554 if (!hithigh || !hitlow)
2555 bit ^= 1;
2556
2557 BitStream[bit2idx++] = bit ^ invert;
2558 }
2559 }
2560
2561 /* standard 1/0 bitstream */
2562 else
2563 {
2564
2565 /* Then detect duration between 2 successive transitions */
2566 for (bitidx = 1; i < GraphTraceLen; i++)
2567 {
2568 if (GraphBuffer[i-1] != GraphBuffer[i])
2569 {
2570 lc = i-lastval;
2571 lastval = i;
2572
2573 // Error check: if bitidx becomes too large, we do not
2574 // have a Manchester encoded bitstream or the clock is really
2575 // wrong!
2576 if (bitidx > (GraphTraceLen*2/clock+8) ) {
2577 PrintToScrollback("Error: the clock you gave is probably wrong, aborting.");
2578 return;
2579 }
2580 // Then switch depending on lc length:
2581 // Tolerance is 1/4 of clock rate (arbitrary)
2582 if (abs(lc-clock/2) < tolerance) {
2583 // Short pulse : either "1" or "0"
2584 BitStream[bitidx++]=GraphBuffer[i-1];
2585 } else if (abs(lc-clock) < tolerance) {
2586 // Long pulse: either "11" or "00"
2587 BitStream[bitidx++]=GraphBuffer[i-1];
2588 BitStream[bitidx++]=GraphBuffer[i-1];
2589 } else {
2590 // Error
2591 warnings++;
2592 PrintToScrollback("Warning: Manchester decode error for pulse width detection.");
2593 PrintToScrollback("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
2594
2595 if (warnings > 100)
2596 {
2597 PrintToScrollback("Error: too many detection errors, aborting.");
2598 return;
2599 }
2600 }
2601 }
2602 }
2603
2604 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
2605 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
2606 // to stop output at the final bitidx2 value, not bitidx
2607 for (i = 0; i < bitidx; i += 2) {
2608 if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) {
2609 BitStream[bit2idx++] = 1 ^ invert;
2610 } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) {
2611 BitStream[bit2idx++] = 0 ^ invert;
2612 } else {
2613 // We cannot end up in this state, this means we are unsynchronized,
2614 // move up 1 bit:
2615 i++;
2616 warnings++;
2617 PrintToScrollback("Unsynchronized, resync...");
2618 PrintToScrollback("(too many of those messages mean the stream is not Manchester encoded)");
2619
2620 if (warnings > 100)
2621 {
2622 PrintToScrollback("Error: too many decode errors, aborting.");
2623 return;
2624 }
2625 }
2626 }
2627 }
2628
2629 PrintToScrollback("Manchester decoded bitstream");
2630 // Now output the bitstream to the scrollback by line of 16 bits
2631 for (i = 0; i < (bit2idx-16); i+=16) {
2632 PrintToScrollback("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
2633 BitStream[i],
2634 BitStream[i+1],
2635 BitStream[i+2],
2636 BitStream[i+3],
2637 BitStream[i+4],
2638 BitStream[i+5],
2639 BitStream[i+6],
2640 BitStream[i+7],
2641 BitStream[i+8],
2642 BitStream[i+9],
2643 BitStream[i+10],
2644 BitStream[i+11],
2645 BitStream[i+12],
2646 BitStream[i+13],
2647 BitStream[i+14],
2648 BitStream[i+15]);
2649 }
2650 }
2651
2652 /*
2653 * Usage ???
2654 */
2655 static void CmdHiddemod(char *str)
2656 {
2657 if(GraphTraceLen < 4800) {
2658 PrintToScrollback("too short; need at least 4800 samples");
2659 return;
2660 }
2661
2662 GraphTraceLen = 4800;
2663 int i;
2664 for(i = 0; i < GraphTraceLen; i++) {
2665 if(GraphBuffer[i] < 0) {
2666 GraphBuffer[i] = 0;
2667 } else {
2668 GraphBuffer[i] = 1;
2669 }
2670 }
2671 RepaintGraphWindow();
2672 }
2673
2674 static void CmdPlot(char *str)
2675 {
2676 ShowGraphWindow();
2677 }
2678
2679 static void CmdGrid(char *str)
2680 {
2681 sscanf(str, "%i %i", &PlotGridX, &PlotGridY);
2682 RepaintGraphWindow();
2683 }
2684
2685 static void CmdHide(char *str)
2686 {
2687 HideGraphWindow();
2688 }
2689
2690 static void CmdScale(char *str)
2691 {
2692 CursorScaleFactor = atoi(str);
2693 if(CursorScaleFactor == 0) {
2694 PrintToScrollback("bad, can't have zero scale");
2695 CursorScaleFactor = 1;
2696 }
2697 RepaintGraphWindow();
2698 }
2699
2700 static void CmdSave(char *str)
2701 {
2702 FILE *f = fopen(str, "w");
2703 if(!f) {
2704 PrintToScrollback("couldn't open '%s'", str);
2705 return;
2706 }
2707 int i;
2708 for(i = 0; i < GraphTraceLen; i++) {
2709 fprintf(f, "%d\n", GraphBuffer[i]);
2710 }
2711 fclose(f);
2712 PrintToScrollback("saved to '%s'", str);
2713 }
2714
2715 static void CmdLoad(char *str)
2716 {
2717 FILE *f = fopen(str, "r");
2718 if(!f) {
2719 PrintToScrollback("couldn't open '%s'", str);
2720 return;
2721 }
2722
2723 GraphTraceLen = 0;
2724 char line[80];
2725 while(fgets(line, sizeof(line), f)) {
2726 GraphBuffer[GraphTraceLen] = atoi(line);
2727 GraphTraceLen++;
2728 }
2729 fclose(f);
2730 PrintToScrollback("loaded %d samples", GraphTraceLen);
2731 RepaintGraphWindow();
2732 }
2733
2734 static void CmdHIDsimTAG(char *str)
2735 {
2736 unsigned int hi=0, lo=0;
2737 int n=0, i=0;
2738
2739 while (sscanf(&str[i++], "%1x", &n ) == 1) {
2740 hi=(hi<<4)|(lo>>28);
2741 lo=(lo<<4)|(n&0xf);
2742 }
2743
2744 PrintToScrollback("Emulating tag with ID %x%16x", hi, lo);
2745
2746 UsbCommand c={CMD_HID_SIM_TAG, {hi, lo, 0}};
2747 SendCommand(&c);
2748 }
2749
2750 static void CmdReadmem(char *str)
2751 {
2752 UsbCommand c={CMD_READ_MEM, {strtol(str, NULL, 0), 0, 0}};
2753 SendCommand(&c);
2754 }
2755
2756 static void CmdVersion(char *str)
2757 {
2758 UsbCommand c={CMD_VERSION};
2759 SendCommand(&c);
2760 }
2761
2762 static void CmdLcdReset(char *str)
2763 {
2764 UsbCommand c={CMD_LCD_RESET, {strtol(str, NULL, 0), 0, 0}};
2765 SendCommand(&c);
2766 }
2767
2768 static void CmdLcd(char *str)
2769 {
2770 int i, j;
2771 UsbCommand c={CMD_LCD};
2772 sscanf(str, "%x %d", &i, &j);
2773 while (j--) {
2774 c.arg[0] = i&0x1ff;
2775 SendCommand(&c);
2776 }
2777 }
2778
2779 /*
2780 * Sets the divisor for LF frequency clock: lets the user choose any LF frequency below
2781 * 600kHz.
2782 */
2783 static void CmdSetDivisor(char *str)
2784 {
2785 UsbCommand c={CMD_SET_LF_DIVISOR, {strtol(str, NULL, 0), 0, 0}};
2786 if (( c.arg[0]<0) || (c.arg[0]>255)) {
2787 PrintToScrollback("divisor must be between 19 and 255");
2788 } else {
2789 SendCommand(&c);
2790 PrintToScrollback("Divisor set, expected freq=%dHz", 12000000/(c.arg[0]+1));
2791 }
2792 }
2793
2794 static void CmdSetMux(char *str)
2795 {
2796 UsbCommand c={CMD_SET_ADC_MUX};
2797 if(strcmp(str, "lopkd") == 0) {
2798 c.arg[0] = 0;
2799 } else if(strcmp(str, "loraw") == 0) {
2800 c.arg[0] = 1;
2801 } else if(strcmp(str, "hipkd") == 0) {
2802 c.arg[0] = 2;
2803 } else if(strcmp(str, "hiraw") == 0) {
2804 c.arg[0] = 3;
2805 }
2806 SendCommand(&c);
2807 }
2808
2809 typedef void HandlerFunction(char *cmdline);
2810
2811 /* in alphabetic order */
2812 static struct {
2813 char *name;
2814 HandlerFunction *handler;
2815 int offline; // 1 if the command can be used when in offline mode
2816 char *docString;
2817 } CommandTable[] = {
2818 /* plot window / data buffer manipulation */
2819 {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"},
2820 {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"},
2821 {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"},
2822 {"amp", CmdAmp, 1, "Amplify peaks"},
2823 {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"},
2824 {"dec", CmdDec, 1, "Decimate samples"},
2825 {"hpf", CmdHpf, 1, "Remove DC offset from trace"},
2826 {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"},
2827 {"norm", CmdNorm, 1, "Normalize max/min to +/-500"},
2828 {"plot", CmdPlot, 1, "Show graph window"},
2829 {"hide", CmdHide, 1, "Hide graph window"},
2830 {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
2831 {"threshold", CmdThreshold, 1, "Maximize/minimize every value in the graph window depending on threshold"},
2832 {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"},
2833 {"scale", CmdScale, 1, "<int> -- Set cursor display scale"},
2834 {"bitstream", Cmdbitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},
2835 {"detectclock", Cmddetectclockrate, 1, "Detect clock rate"},
2836 {"exit", CmdQuit, 1, "Exit program"},
2837 {"quit", CmdQuit, 1, "Exit program"},
2838
2839 /* low-level hardware control */
2840 {"fpgaoff", CmdFPGAOff, 0, "Set FPGA off"},
2841 {"tune", CmdTune, 0, "Measure antenna tuning"},
2842 {"hitune", CmdHiTune, 0, "Continuously measure HF antenna tuning"},
2843 {"readmem", CmdReadmem, 0, "[address] -- Read memory at decimal address from flash"},
2844 {"reset", CmdReset, 0, "Reset the Proxmark3"},
2845 {"setlfdivisor", CmdSetDivisor, 0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"},
2846 {"setmux", CmdSetMux, 0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"},
2847 {"version", CmdVersion, 0, "Show version inforation about the connected Proxmark"},
2848 {"lcd", CmdLcd, 0, "<HEX command> <count> -- Send command/data to LCD"},
2849 {"lcdreset", CmdLcdReset, 0, "Hardware reset LCD"},
2850
2851 /* data transfer functions */
2852
2853 {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"},
2854 {"hexsamples", CmdHexsamples, 0, "<blocks> [<offset>] -- Dump big buffer as hex bytes"},
2855 {"higet", CmdHi14read_sim, 0, "<samples> -- Get samples HF, 'analog'"},
2856 {"hisamples", CmdHisamples, 0, "Get raw samples for HF tag"},
2857 {"hisampless", CmdHisampless, 0, "<samples> -- Get signed raw samples, HF tag"},
2858 {"hisamplest", CmdHi14readt, 0, "Get samples HF, for testing"},
2859 {"losamples", CmdLosamples, 0, "[128 - 16000] -- Get raw samples for LF tag"},
2860 {"detectreader", CmdDetectReader, 0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"},
2861
2862 /* (de-)modulation */
2863
2864 {"askdemod", Cmdaskdemod, 1, "<0|1> -- Attempt to demodulate simple ASK tags"},
2865 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
2866 {"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
2867 {"hi14bdemod", CmdHi14bdemod, 1, "Demodulate ISO14443 Type B from tag"},
2868 {"hi15demod", CmdHi15demod, 1, "Demodulate ISO15693 from tag"},
2869 {"hiddemod", CmdHiddemod, 1, "Demodulate HID Prox Card II (not optimal)"},
2870 {"hidfskdemod", CmdHIDdemodFSK, 0, "Realtime HID FSK demodulator"},
2871 {"indalademod", CmdIndalademod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
2872 {"mandemod", Cmdmanchesterdemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
2873 {"manmod", Cmdmanchestermod, 1, "[clock rate] -- Manchester modulate a binary stream"},
2874 {"tidemod", CmdTIDemod, 1, "Demodulate raw bits for TI-type LF tag"},
2875 {"vchdemod", CmdVchdemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
2876
2877 /* simulation */
2878 {"em410xsim", CmdEM410xsim, 1, "<UID> -- Simulate EM410x tag"},
2879 {"hi14sim", CmdHi14sim, 0, "Fake ISO 14443 tag"},
2880 {"hi14asim", CmdHi14asim, 0, "<UID> -- Fake ISO 14443a tag"},
2881 {"hi15sim", CmdHi15tag, 0, "Fake an ISO15693 tag"},
2882 {"hidsimtag", CmdHIDsimTAG, 0, "<ID> -- HID tag simulator"},
2883 {"hisimlisten", CmdHisimlisten, 0, "Get HF samples as fake tag"},
2884 {"losim", CmdLosim, 0, "Simulate LF tag"},
2885 {"losimbidir", CmdLosimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
2886
2887 /* card reading functions */
2888 {"em410xread", CmdEM410xread, 1, "[clock rate] -- Extract ID from EM410x tag"},
2889 {"em410xwatch", CmdEM410xwatch, 0, "Watches for EM410x tags"},
2890 {"em4x50read", CmdEM4x50read, 1, "Extract data from EM4x50 tag"},
2891 {"hi14alist", CmdHi14alist, 0, "List ISO 14443a history"},
2892 {"hi14amifare", CmdHi14amifare, 0, "Read out sector 0 parity error messages"},
2893 {"hi14areader", CmdHi14areader, 0, "Act like an ISO14443 Type A reader"},
2894 {"hi14asnoop", CmdHi14asnoop, 0, "Eavesdrop ISO 14443 Type A"},
2895 {"hi14list", CmdHi14list, 0, "List ISO 14443 history"},
2896 {"hi14read", CmdHi14read, 0, "Read HF tag (ISO 14443)"},
2897 {"hi14snoop", CmdHi14snoop, 0, "Eavesdrop ISO 14443"},
2898 {"hi15read", CmdHi15read, 0, "Read HF tag (ISO 15693)"},
2899 {"hi15reader", CmdHi15reader, 0, "Act like an ISO15693 reader"},
2900 {"legicrfread", CmdLegicRfRead, 0, "Start the LEGIC RF reader"},
2901 {"locomread", CmdLoCommandRead, 0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"},
2902 {"loread", CmdLoread, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
2903 {"sri512read", CmdSri512read, 0, "<int> -- Read contents of a SRI512 tag"},
2904 {"srix4kread", CmdSrix4kread, 0, "<int> -- Read contents of a SRIX4K tag"},
2905 {"tiread", CmdTIRead, 0, "Read and decode a TI 134 kHz tag"},
2906 {"tiwrite", CmdTIWrite, 0, "Write new data to a r/w TI 134 kHz tag"},
2907 };
2908
2909 static struct {
2910 char *name;
2911 char *args;
2912 char *argshelp;
2913 char *description;
2914 } CommandExtendedHelp[]= {
2915 {"detectreader","'l'|'h'","'l' specifies LF antenna scan only, 'h' specifies HF antenna scan only.","Monitor antenna for changes in voltage. Output is in three fields: CHANGED, CURRENT, PERIOD,\nwhere CHANGED is the value just changed from, CURRENT is the current value and PERIOD is the\nnumber of program loops since the last change.\n\nThe RED LED indicates LF field detected, and the GREEN LED indicates HF field detected."},
2916 {"tune","","","Drive LF antenna at all divisor range values (19 - 255) and store the results in the output\nbuffer. Issuing 'losamples' and then 'plot' commands will display the resulting peak. 12MHz\ndivided by the peak's position plus one gives the antenna's resonant frequency. For convenience,\nthis value is also printed out by the command."},
2917 };
2918
2919 //-----------------------------------------------------------------------------
2920 // Entry point into our code: called whenever the user types a command and
2921 // then presses Enter, which the full command line that they typed.
2922 //-----------------------------------------------------------------------------
2923 void CommandReceived(char *cmd)
2924 {
2925 int i;
2926 char line[256];
2927
2928 PrintToScrollback("> %s", cmd);
2929
2930 if(strcmp(cmd, "help") == 0 || strncmp(cmd,"help ",strlen("help ")) == 0) {
2931 // check if we're doing extended help
2932 if(strlen(cmd) > strlen("help ")) {
2933 cmd += strlen("help ");
2934 for(i = 0; i < sizeof(CommandExtendedHelp) / sizeof(CommandExtendedHelp[0]); i++) {
2935 if(strcmp(CommandExtendedHelp[i].name,cmd) == 0) {
2936 PrintToScrollback("\nExtended help for '%s':\n", cmd);
2937 PrintToScrollback("Args: %s\t- %s\n",CommandExtendedHelp[i].args,CommandExtendedHelp[i].argshelp);
2938 PrintToScrollback(CommandExtendedHelp[i].description);
2939 PrintToScrollback("");
2940 return;
2941 }
2942 }
2943 PrintToScrollback("No extended help available for '%s'", cmd);
2944 return;
2945 }
2946 if (offline) PrintToScrollback("Operating in OFFLINE mode (no device connected)");
2947 PrintToScrollback("\r\nAvailable commands:");
2948 for(i = 0; i < sizeof(CommandTable) / sizeof(CommandTable[0]); i++) {
2949 if (offline && (CommandTable[i].offline==0)) continue;
2950 memset(line, ' ', sizeof(line));
2951 strcpy(line+2, CommandTable[i].name);
2952 line[strlen(line)] = ' ';
2953 sprintf(line+15, " -- %s", CommandTable[i].docString);
2954 PrintToScrollback("%s", line);
2955 }
2956 PrintToScrollback("");
2957 PrintToScrollback("'help <command>' for extended help on that command\n");
2958 return;
2959 }
2960
2961 for(i = 0; i < sizeof(CommandTable) / sizeof(CommandTable[0]); i++) {
2962 char *name = CommandTable[i].name;
2963 if(memcmp(cmd, name, strlen(name))==0 &&
2964 (cmd[strlen(name)] == ' ' || cmd[strlen(name)] == '\0'))
2965 {
2966 cmd += strlen(name);
2967 while(*cmd == ' ') {
2968 cmd++;
2969 }
2970 if (offline && (CommandTable[i].offline==0)) {
2971 PrintToScrollback("Offline mode, cannot use this command.");
2972 return;
2973 }
2974 (CommandTable[i].handler)(cmd);
2975 return;
2976 }
2977 }
2978 PrintToScrollback(">> bad command '%s'", cmd);
2979 }
2980
2981 //-----------------------------------------------------------------------------
2982 // Entry point into our code: called whenever we received a packet over USB
2983 // that we weren't necessarily expecting, for example a debug print.
2984 //-----------------------------------------------------------------------------
2985 void UsbCommandReceived(UsbCommand *c)
2986 {
2987 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
2988 /* If we recognize a response, return to avoid further processing */
2989 switch(c->cmd) {
2990 case CMD_DEBUG_PRINT_STRING: {
2991 char s[100];
2992 if(c->arg[0] > 70 || c->arg[0] < 0) {
2993 c->arg[0] = 0;
2994 }
2995 memcpy(s, c->d.asBytes, c->arg[0]);
2996 s[c->arg[0]] = '\0';
2997 PrintToScrollback("#db# %s", s);
2998 return;
2999 }
3000
3001 case CMD_DEBUG_PRINT_INTEGERS:
3002 PrintToScrollback("#db# %08x, %08x, %08x\r\n", c->arg[0], c->arg[1], c->arg[2]);
3003 return;
3004
3005 case CMD_MEASURED_ANTENNA_TUNING: {
3006 int peakv, peakf;
3007 int vLf125, vLf134, vHf;
3008 vLf125 = c->arg[0] & 0xffff;
3009 vLf134 = c->arg[0] >> 16;
3010 vHf = c->arg[1] & 0xffff;;
3011 peakf = c->arg[2] & 0xffff;
3012 peakv = c->arg[2] >> 16;
3013 PrintToScrollback("");
3014 PrintToScrollback("");
3015 PrintToScrollback("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
3016 PrintToScrollback("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
3017 PrintToScrollback("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
3018 PrintToScrollback("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
3019 if (peakv<2000)
3020 PrintToScrollback("# Your LF antenna is unusable.");
3021 else if (peakv<10000)
3022 PrintToScrollback("# Your LF antenna is marginal.");
3023 if (vHf<2000)
3024 PrintToScrollback("# Your HF antenna is unusable.");
3025 else if (vHf<5000)
3026 PrintToScrollback("# Your HF antenna is marginal.");
3027 return;
3028 }
3029 default:
3030 break;
3031 }
3032 /* Maybe it's a response: */
3033 switch(current_command) {
3034 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
3035 if (c->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response;
3036 int i;
3037 for(i=0; i<48; i++) sample_buf[i] = c->d.asBytes[i];
3038 received_command = c->cmd;
3039 return;
3040 default:
3041 unexpected_response:
3042 PrintToScrollback("unrecognized command %08x\n", c->cmd);
3043 break;
3044 }
3045 }
Impressum, Datenschutz