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