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 //-----------------------------------------------------------------------------
14 #include "../common/iso14443_crc.c"
16 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
18 static int CmdHisamplest(char *str
, int nrlow
);
20 static void GetFromBigBuf(BYTE
*dest
, int bytes
)
25 PrintToScrollback("bad len in GetFromBigBuf");
30 for(i
= 0; i
< n
; i
+= 12) {
32 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
34 SendCommand(&c
, FALSE
);
36 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
37 PrintToScrollback("bad resp\n");
41 memcpy(dest
+(i
*4), c
.d
.asBytes
, 48);
45 static void CmdQuit(char *str
)
50 static void CmdHIDdemodFSK(char *str
)
53 c
.cmd
= CMD_HID_DEMOD_FSK
;
54 SendCommand(&c
, FALSE
);
57 static void CmdTune(char *str
)
60 c
.cmd
= CMD_MEASURE_ANTENNA_TUNING
;
61 SendCommand(&c
, FALSE
);
64 static void CmdHi15read(char *str
)
67 c
.cmd
= CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
;
68 SendCommand(&c
, FALSE
);
71 static void CmdHi14read(char *str
)
74 c
.cmd
= CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
;
76 SendCommand(&c
, FALSE
);
80 static void CmdHi14areader(char *str
)
83 c
.cmd
= CMD_READER_ISO_14443a
;
85 SendCommand(&c
, FALSE
);
89 static void CmdHi15reader(char *str
)
92 c
.cmd
= CMD_READER_ISO_15693
;
94 SendCommand(&c
, FALSE
);
98 static void CmdHi15tag(char *str
)
101 c
.cmd
= CMD_SIMTAG_ISO_15693
;
103 SendCommand(&c
, FALSE
);
106 static void CmdHi14read_sim(char *str
)
109 c
.cmd
= CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM
;
111 SendCommand(&c
, FALSE
);
114 static void CmdHi14readt(char *str
)
117 c
.cmd
= CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
;
119 SendCommand(&c
, FALSE
);
121 //CmdHisamplest(str);
122 while(CmdHisamplest(str
,atoi(str
))==0) {
123 c
.cmd
= CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
;
125 SendCommand(&c
, FALSE
);
127 RepaintGraphWindow();
130 static void CmdHisimlisten(char *str
)
133 c
.cmd
= CMD_SIMULATE_TAG_HF_LISTEN
;
134 SendCommand(&c
, FALSE
);
137 static void CmdHi14sim(char *str
)
140 c
.cmd
= CMD_SIMULATE_TAG_ISO_14443
;
141 SendCommand(&c
, FALSE
);
146 static void CmdHi14asim(char *str
) // ## simulate iso14443a tag
147 { // ## greg - added ability to specify tag UID
149 unsigned int hi
=0, lo
=0;
153 while (sscanf(&str
[i
++], "%1x", &n
) == 1) {
158 c
.cmd
= CMD_SIMULATE_TAG_ISO_14443a
;
159 // c.ext should be set to *str or convert *str to the correct format for a uid
162 PrintToScrollback("Emulating 14443A TAG with UID %x%16x", hi
, lo
);
163 SendCommand(&c
, FALSE
);
166 static void CmdHi14snoop(char *str
)
169 c
.cmd
= CMD_SNOOP_ISO_14443
;
170 SendCommand(&c
, FALSE
);
173 static void CmdHi14asnoop(char *str
)
176 c
.cmd
= CMD_SNOOP_ISO_14443a
;
177 SendCommand(&c
, FALSE
);
180 static void CmdFPGAOff(char *str
) // ## FPGA Control
183 c
.cmd
= CMD_FPGA_MAJOR_MODE_OFF
;
184 SendCommand(&c
, FALSE
);
187 static void CmdLosim(char *str
)
191 for(i
= 0; i
< GraphTraceLen
; i
+= 48) {
194 for(j
= 0; j
< 48; j
++) {
195 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
197 c
.cmd
= CMD_DOWNLOADED_SIM_SAMPLES_125K
;
199 SendCommand(&c
, FALSE
);
203 c
.cmd
= CMD_SIMULATE_TAG_125K
;
204 c
.ext1
= GraphTraceLen
;
205 SendCommand(&c
, FALSE
);
208 static void CmdLoread(char *str
)
211 // 'h' means higher-low-frequency, 134 kHz
214 } else if (*str
== '\0') {
217 PrintToScrollback("use 'loread' or 'loread h'");
220 c
.cmd
= CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
;
221 SendCommand(&c
, FALSE
);
224 static void CmdLosamples(char *str
)
232 if (n
>16000) n
=16000;
234 for(i
= 0; i
< n
; i
+= 12) {
236 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
238 SendCommand(&c
, FALSE
);
240 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
241 PrintToScrollback("bad resp\n");
245 for(j
= 0; j
< 48; j
++) {
246 GraphBuffer
[cnt
++] = ((int)c
.d
.asBytes
[j
]) - 128;
250 RepaintGraphWindow();
253 static void CmdBitsamples(char *str
)
260 for(i
= 0; i
< n
; i
+= 12) {
262 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
264 SendCommand(&c
, FALSE
);
266 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
267 PrintToScrollback("bad resp\n");
271 for(j
= 0; j
< 48; j
++) {
272 for(k
= 0; k
< 8; k
++) {
273 if(c
.d
.asBytes
[j
] & (1 << (7 - k
))) {
274 GraphBuffer
[cnt
++] = 1;
276 GraphBuffer
[cnt
++] = 0;
282 RepaintGraphWindow();
285 static void CmdHisamples(char *str
)
291 for(i
= 0; i
< n
; i
+= 12) {
293 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
295 SendCommand(&c
, FALSE
);
297 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
298 PrintToScrollback("bad resp\n");
302 for(j
= 0; j
< 48; j
++) {
303 GraphBuffer
[cnt
++] = (int)((BYTE
)c
.d
.asBytes
[j
]);
308 RepaintGraphWindow();
312 static int CmdHisamplest(char *str
, int nrlow
)
324 for(i
= 0; i
< n
; i
+= 12) {
326 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
328 SendCommand(&c
, FALSE
);
330 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
331 PrintToScrollback("bad resp\n");
335 for(j
= 0; j
< 48; j
++) {
336 t2
= (int)((BYTE
)c
.d
.asBytes
[j
]);
337 if((t2
^ 0xC0) & 0xC0) { hasbeennull
++; }
343 t1
= (t2
& 0x80) ^ (t2
& 0x20);
344 t2
= ((t2
<< 1) & 0x80) ^ ((t2
<< 1) & 0x20);
350 t2
= ((t2
<< 1) & 0x80);
356 t2
= ((t2
<< 1) & 0x20);
360 // both, but tag with other algorithm
361 t1
= (t2
& 0x80) ^ (t2
& 0x08);
362 t2
= ((t2
<< 1) & 0x80) ^ ((t2
<< 1) & 0x08);
366 GraphBuffer
[cnt
++] = t1
;
367 GraphBuffer
[cnt
++] = t2
;
372 if(hasbeennull
>nrlow
|| nrlow
==0) {
373 PrintToScrollback("hasbeennull=%d", hasbeennull
);
382 static void CmdHexsamples(char *str
)
393 for(i
= 0; i
< n
; i
+= 12) {
395 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
397 SendCommand(&c
, FALSE
);
399 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
400 PrintToScrollback("bad resp\n");
404 for(j
= 0; j
< 48; j
+= 8) {
405 PrintToScrollback("%02x %02x %02x %02x %02x %02x %02x %02x",
420 static void CmdHisampless(char *str
)
432 for(i
= 0; i
< n
; i
+= 12) {
434 c
.cmd
= CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
;
436 SendCommand(&c
, FALSE
);
438 if(c
.cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
439 PrintToScrollback("bad resp\n");
443 for(j
= 0; j
< 48; j
++) {
444 GraphBuffer
[cnt
++] = (int)((signed char)c
.d
.asBytes
[j
]);
449 RepaintGraphWindow();
452 static WORD
Iso15693Crc(BYTE
*v
, int n
)
458 for(i
= 0; i
< n
; i
++) {
459 reg
= reg
^ ((DWORD
)v
[i
]);
460 for (j
= 0; j
< 8; j
++) {
462 reg
= (reg
>> 1) ^ 0x8408;
472 static void CmdHi14bdemod(char *str
)
477 BOOL negateI
, negateQ
;
482 // As received, the samples are pairs, correlations against I and Q
483 // square waves. So estimate angle of initial carrier (or just
484 // quadrant, actually), and then do the demod.
486 // First, estimate where the tag starts modulating.
487 for(i
= 0; i
< GraphTraceLen
; i
+= 2) {
488 if(abs(GraphBuffer
[i
]) + abs(GraphBuffer
[i
+1]) > 40) {
492 if(i
>= GraphTraceLen
) {
493 PrintToScrollback("too weak to sync");
496 PrintToScrollback("out of weak at %d", i
);
499 // Now, estimate the phase in the initial modulation of the tag
502 for(; i
< (outOfWeakAt
+ 16); i
+= 2) {
503 isum
+= GraphBuffer
[i
+0];
504 qsum
+= GraphBuffer
[i
+1];
506 negateI
= (isum
< 0);
507 negateQ
= (qsum
< 0);
509 // Turn the correlation pairs into soft decisions on the bit.
511 for(i
= 0; i
< GraphTraceLen
/2; i
++) {
512 int si
= GraphBuffer
[j
];
513 int sq
= GraphBuffer
[j
+1];
514 if(negateI
) si
= -si
;
515 if(negateQ
) sq
= -sq
;
516 GraphBuffer
[i
] = si
+ sq
;
522 while(GraphBuffer
[i
] > 0 && i
< GraphTraceLen
)
524 if(i
>= GraphTraceLen
) goto demodError
;
527 while(GraphBuffer
[i
] < 0 && i
< GraphTraceLen
)
529 if(i
>= GraphTraceLen
) goto demodError
;
530 if((i
- iold
) > 23) goto demodError
;
532 PrintToScrollback("make it to demod loop");
536 while(GraphBuffer
[i
] >= 0 && i
< GraphTraceLen
)
538 if(i
>= GraphTraceLen
) goto demodError
;
539 if((i
- iold
) > 6) goto demodError
;
542 if(i
+ 20 >= GraphTraceLen
) goto demodError
;
544 for(j
= 0; j
< 10; j
++) {
545 int soft
= GraphBuffer
[i
] + GraphBuffer
[i
+1];
547 if(abs(soft
) < ((abs(isum
) + abs(qsum
))/20)) {
548 PrintToScrollback("weak bit");
552 if(GraphBuffer
[i
] + GraphBuffer
[i
+1] >= 0) {
559 if( (shiftReg
& 0x200) &&
562 // valid data byte, start and stop bits okay
563 PrintToScrollback(" %02x", (shiftReg
>> 1) & 0xff);
564 data
[dataLen
++] = (shiftReg
>> 1) & 0xff;
565 if(dataLen
>= sizeof(data
)) {
568 } else if(shiftReg
== 0x000) {
577 ComputeCrc14443(CRC_14443_B
, data
, dataLen
-2, &first
, &second
);
578 PrintToScrollback("CRC: %02x %02x (%s)\n", first
, second
,
579 (first
== data
[dataLen
-2] && second
== data
[dataLen
-1]) ?
580 "ok" : "****FAIL****");
582 RepaintGraphWindow();
586 PrintToScrollback("demod error");
587 RepaintGraphWindow();
590 static void CmdHi14list(char *str
)
593 GetFromBigBuf(got
, sizeof(got
));
595 PrintToScrollback("recorded activity:");
596 PrintToScrollback(" time :rssi: who bytes");
597 PrintToScrollback("---------+----+----+-----------");
608 int timestamp
= *((DWORD
*)(got
+i
));
609 if(timestamp
& 0x80000000) {
610 timestamp
&= 0x7fffffff;
615 int metric
= *((DWORD
*)(got
+i
+4));
626 BYTE
*frame
= (got
+i
+9);
628 char line
[1000] = "";
630 for(j
= 0; j
< len
; j
++) {
631 sprintf(line
+(j
*3), "%02x ", frame
[j
]);
637 ComputeCrc14443(CRC_14443_B
, frame
, len
-2, &b1
, &b2
);
638 if(b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
639 crc
= "**FAIL CRC**";
647 char metricString
[100];
649 sprintf(metricString
, "%3d", metric
);
651 strcpy(metricString
, " ");
654 PrintToScrollback(" +%7d: %s: %s %s %s",
655 (prev
< 0 ? 0 : timestamp
- prev
),
657 (isResponse
? "TAG" : " "), line
, crc
);
664 static void CmdHi14alist(char *str
)
667 GetFromBigBuf(got
, sizeof(got
));
669 PrintToScrollback("recorded activity:");
670 PrintToScrollback(" ETU :rssi: who bytes");
671 PrintToScrollback("---------+----+----+-----------");
682 int timestamp
= *((DWORD
*)(got
+i
));
683 if(timestamp
& 0x80000000) {
684 timestamp
&= 0x7fffffff;
691 int parityBits
= *((DWORD
*)(got
+i
+4));
692 // 4 bytes of additional information...
693 // maximum of 32 additional parity bit information
696 // at each quarter bit period we can send power level (16 levels)
697 // or each half bit period in 256 levels.
705 if(i
+ len
>= 1900) {
709 BYTE
*frame
= (got
+i
+9);
711 // Break and stick with current result if buffer was not completely full
712 if(frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
714 char line
[1000] = "";
716 for(j
= 0; j
< len
; j
++) {
717 int oddparity
= 0x01;
721 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
724 //if((parityBits >> (len - j - 1)) & 0x01) {
725 if(isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
726 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
729 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
737 for(j
= 0; j
< (len
- 1); j
++) {
738 // gives problems... search for the reason..
739 /*if(frame[j] == 0xAA) {
742 crc = "[1] Two drops close after each other";
745 crc = "[2] Potential SOC with a drop in second half of bitperiod";
748 crc = "[3] Segment Z after segment X is not possible";
751 crc = "[4] Parity bit of a fully received byte was wrong";
754 crc = "[?] Unknown error";
762 ComputeCrc14443(CRC_14443_A
, frame
, len
-2, &b1
, &b2
);
763 if(b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
764 crc
= (isResponse
& (len
< 6)) ? "" : " !crc";
773 char metricString
[100];
775 sprintf(metricString
, "%3d", metric
);
777 strcpy(metricString
, " ");
780 PrintToScrollback(" +%7d: %s: %s %s %s",
781 (prev
< 0 ? 0 : (timestamp
- prev
)),
783 (isResponse
? "TAG" : " "), line
, crc
);
791 static void CmdHi15demod(char *str
)
793 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
796 // 1) Unmodulated time of 56.64us
797 // 2) 24 pulses of 423.75khz
798 // 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
800 static const int FrameSOF
[] = {
801 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
802 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
803 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
804 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
810 static const int Logic0
[] = {
816 static const int Logic1
[] = {
824 // 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
825 // 2) 24 pulses of 423.75khz
826 // 3) Unmodulated time of 56.64us
828 static const int FrameEOF
[] = {
833 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
834 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
835 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
836 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
844 if(GraphTraceLen
< 1000) return;
846 // First, correlate for SOF
847 for(i
= 0; i
< 100; i
++) {
849 for(j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
850 corr
+= FrameSOF
[j
]*GraphBuffer
[i
+(j
/skip
)];
857 PrintToScrollback("SOF at %d, correlation %d", maxPos
,
858 max
/(arraylen(FrameSOF
)/skip
));
860 i
= maxPos
+ arraylen(FrameSOF
)/skip
;
863 memset(outBuf
, 0, sizeof(outBuf
));
866 int corr0
= 0, corr1
= 0, corrEOF
= 0;
867 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
868 corr0
+= Logic0
[j
]*GraphBuffer
[i
+(j
/skip
)];
870 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
871 corr1
+= Logic1
[j
]*GraphBuffer
[i
+(j
/skip
)];
873 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
874 corrEOF
+= FrameEOF
[j
]*GraphBuffer
[i
+(j
/skip
)];
876 // Even things out by the length of the target waveform.
880 if(corrEOF
> corr1
&& corrEOF
> corr0
) {
881 PrintToScrollback("EOF at %d", i
);
883 } else if(corr1
> corr0
) {
884 i
+= arraylen(Logic1
)/skip
;
887 i
+= arraylen(Logic0
)/skip
;
894 if((i
+(int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
895 PrintToScrollback("ran off end!");
900 PrintToScrollback("error, uneven octet! (discard extra bits!)");
901 PrintToScrollback(" mask=%02x", mask
);
903 PrintToScrollback("%d octets", k
);
905 for(i
= 0; i
< k
; i
++) {
906 PrintToScrollback("# %2d: %02x ", i
, outBuf
[i
]);
908 PrintToScrollback("CRC=%04x", Iso15693Crc(outBuf
, k
-2));
911 static void CmdTiread(char *str
)
914 c
.cmd
= CMD_ACQUIRE_RAW_BITS_TI_TYPE
;
915 SendCommand(&c
, FALSE
);
918 static void CmdTibits(char *str
)
922 for(i
= 0; i
< 1536; i
+= 12) {
924 c
.cmd
= CMD_DOWNLOAD_RAW_BITS_TI_TYPE
;
926 SendCommand(&c
, FALSE
);
928 if(c
.cmd
!= CMD_DOWNLOADED_RAW_BITS_TI_TYPE
) {
929 PrintToScrollback("bad resp\n");
933 for(j
= 0; j
< 12; j
++) {
935 for(k
= 31; k
>= 0; k
--) {
936 if(c
.d
.asDwords
[j
] & (1 << k
)) {
937 GraphBuffer
[cnt
++] = 1;
939 GraphBuffer
[cnt
++] = -1;
944 GraphTraceLen
= 1536*32;
945 RepaintGraphWindow();
948 static void CmdTidemod(char *cmdline
)
950 /* MATLAB as follows:
951 f_s = 2000000; % sampling frequency
952 f_l = 123200; % low FSK tone
953 f_h = 134200; % high FSK tone
955 T_l = 119e-6; % low bit duration
956 T_h = 130e-6; % high bit duration
958 l = 2*pi*ones(1, floor(f_s*T_l))*(f_l/f_s);
959 h = 2*pi*ones(1, floor(f_s*T_h))*(f_h/f_s);
961 l = sign(sin(cumsum(l)));
962 h = sign(sin(cumsum(h)));
964 static const int LowTone
[] = {
965 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1,
966 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
967 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1,
968 -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1,
969 -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1,
970 -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1,
971 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
972 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
973 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1,
974 -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,
975 -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
976 -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1,
977 1, 1, 1, 1, 1, 1, 1, -1, -1, -1,
979 static const int HighTone
[] = {
980 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
981 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1,
982 -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
983 -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
984 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
985 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1,
986 -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
987 -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
988 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
989 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1,
990 -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
991 -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
992 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
993 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1,
996 int convLen
= max(arraylen(HighTone
), arraylen(LowTone
));
999 for(i
= 0; i
< GraphTraceLen
- convLen
; i
++) {
1001 int lowSum
= 0, highSum
= 0;;
1002 int lowLen
= arraylen(LowTone
);
1003 int highLen
= arraylen(HighTone
);
1005 for(j
= 0; j
< lowLen
; j
++) {
1006 lowSum
+= LowTone
[j
]*GraphBuffer
[i
+j
];
1008 for(j
= 0; j
< highLen
; j
++) {
1009 highSum
+= HighTone
[j
]*GraphBuffer
[i
+j
];
1011 lowSum
= abs((100*lowSum
) / lowLen
);
1012 highSum
= abs((100*highSum
) / highLen
);
1013 GraphBuffer
[i
] = (highSum
<< 16) | lowSum
;
1016 for(i
= 0; i
< GraphTraceLen
- convLen
- 16; i
++) {
1018 int lowTot
= 0, highTot
= 0;
1019 // 16 and 15 are f_s divided by f_l and f_h, rounded
1020 for(j
= 0; j
< 16; j
++) {
1021 lowTot
+= (GraphBuffer
[i
+j
] & 0xffff);
1023 for(j
= 0; j
< 15; j
++) {
1024 highTot
+= (GraphBuffer
[i
+j
] >> 16);
1026 GraphBuffer
[i
] = lowTot
- highTot
;
1029 GraphTraceLen
-= (convLen
+ 16);
1031 RepaintGraphWindow();
1033 // Okay, so now we have unsliced soft decisions; find bit-sync, and then
1036 int max
= 0, maxPos
= 0;
1037 for(i
= 0; i
< 6000; i
++) {
1040 for(j
= 0; j
< 8*arraylen(LowTone
); j
++) {
1041 dec
-= GraphBuffer
[i
+j
];
1043 for(; j
< 8*arraylen(LowTone
) + 8*arraylen(HighTone
); j
++) {
1044 dec
+= GraphBuffer
[i
+j
];
1051 GraphBuffer
[maxPos
] = 800;
1052 GraphBuffer
[maxPos
+1] = -800;
1054 maxPos
+= 8*arraylen(LowTone
);
1055 GraphBuffer
[maxPos
] = 800;
1056 GraphBuffer
[maxPos
+1] = -800;
1057 maxPos
+= 8*arraylen(HighTone
);
1059 GraphBuffer
[maxPos
] = 800;
1060 GraphBuffer
[maxPos
+1] = -800;
1062 PrintToScrollback("actual data bits start at sample %d", maxPos
);
1064 PrintToScrollback("length %d/%d", arraylen(HighTone
), arraylen(LowTone
));
1066 GraphBuffer
[maxPos
] = 800;
1067 GraphBuffer
[maxPos
+1] = -800;
1069 BYTE bits
[64+16+8+1];
1070 bits
[sizeof(bits
)-1] = '\0';
1072 for(i
= 0; i
< arraylen(bits
); i
++) {
1076 for(j
= 0; j
< arraylen(LowTone
); j
++) {
1077 low
-= GraphBuffer
[maxPos
+j
];
1079 for(j
= 0; j
< arraylen(HighTone
); j
++) {
1080 high
+= GraphBuffer
[maxPos
+j
];
1084 maxPos
+= arraylen(HighTone
);
1087 maxPos
+= arraylen(LowTone
);
1089 GraphBuffer
[maxPos
] = 800;
1090 GraphBuffer
[maxPos
+1] = -800;
1092 PrintToScrollback("bits: '%s'", bits
);
1095 for(i
= 0; i
< 32; i
++) {
1096 if(bits
[i
] == '1') {
1100 for(i
= 32; i
< 64; i
++) {
1101 if(bits
[i
] == '1') {
1105 PrintToScrollback("hex: %08x %08x", h
, l
);
1108 static void CmdNorm(char *str
)
1111 int max
= INT_MIN
, min
= INT_MAX
;
1112 for(i
= 10; i
< GraphTraceLen
; i
++) {
1113 if(GraphBuffer
[i
] > max
) {
1114 max
= GraphBuffer
[i
];
1116 if(GraphBuffer
[i
] < min
) {
1117 min
= GraphBuffer
[i
];
1121 for(i
= 0; i
< GraphTraceLen
; i
++) {
1122 GraphBuffer
[i
] = (GraphBuffer
[i
] - ((max
+ min
)/2))*1000/
1126 RepaintGraphWindow();
1129 static void CmdDec(char *str
)
1132 for(i
= 0; i
< (GraphTraceLen
/2); i
++) {
1133 GraphBuffer
[i
] = GraphBuffer
[i
*2];
1136 PrintToScrollback("decimated by 2");
1137 RepaintGraphWindow();
1140 static void CmdHpf(char *str
)
1144 for(i
= 10; i
< GraphTraceLen
; i
++) {
1145 accum
+= GraphBuffer
[i
];
1147 accum
/= (GraphTraceLen
- 10);
1148 for(i
= 0; i
< GraphTraceLen
; i
++) {
1149 GraphBuffer
[i
] -= accum
;
1152 RepaintGraphWindow();
1155 static void CmdZerocrossings(char *str
)
1158 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1164 for(i
= 0; i
< GraphTraceLen
; i
++) {
1165 if(GraphBuffer
[i
]*sign
>= 0) {
1166 // No change in sign, reproduce the previous sample count.
1168 GraphBuffer
[i
] = lastZc
;
1170 // Change in sign, reset the sample count.
1172 GraphBuffer
[i
] = lastZc
;
1180 RepaintGraphWindow();
1183 static void CmdLtrim(char *str
)
1188 for(i
= ds
; i
< GraphTraceLen
; i
++) {
1189 GraphBuffer
[i
-ds
] = GraphBuffer
[i
];
1191 GraphTraceLen
-= ds
;
1193 RepaintGraphWindow();
1196 static void CmdAutoCorr(char *str
)
1198 static int CorrelBuffer
[MAX_GRAPH_TRACE_LEN
];
1200 int window
= atoi(str
);
1203 PrintToScrollback("needs a window");
1207 if(window
>= GraphTraceLen
) {
1208 PrintToScrollback("window must be smaller than trace (%d samples)",
1213 PrintToScrollback("performing %d correlations", GraphTraceLen
- window
);
1216 for(i
= 0; i
< GraphTraceLen
- window
; i
++) {
1219 for(j
= 0; j
< window
; j
++) {
1220 sum
+= (GraphBuffer
[j
]*GraphBuffer
[i
+j
]) / 256;
1222 CorrelBuffer
[i
] = sum
;
1224 GraphTraceLen
= GraphTraceLen
- window
;
1225 memcpy(GraphBuffer
, CorrelBuffer
, GraphTraceLen
*sizeof(int));
1227 RepaintGraphWindow();
1230 static void CmdVchdemod(char *str
)
1232 // Is this the entire sync pattern, or does this also include some
1233 // data bits that happen to be the same everywhere? That would be
1235 static const int SyncPattern
[] = {
1236 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1237 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1238 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1239 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1240 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1241 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1242 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1243 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1244 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1245 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1248 // So first, we correlate for the sync pattern, and mark that.
1249 int bestCorrel
= 0, bestPos
= 0;
1251 // It does us no good to find the sync pattern, with fewer than
1252 // 2048 samples after it...
1253 for(i
= 0; i
< (GraphTraceLen
-2048); i
++) {
1256 for(j
= 0; j
< arraylen(SyncPattern
); j
++) {
1257 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
1259 if(sum
> bestCorrel
) {
1264 PrintToScrollback("best sync at %d [metric %d]", bestPos
, bestCorrel
);
1269 int worst
= INT_MAX
;
1272 for(i
= 0; i
< 2048; i
+= 8) {
1275 for(j
= 0; j
< 8; j
++) {
1276 sum
+= GraphBuffer
[bestPos
+i
+j
];
1283 if(abs(sum
) < worst
) {
1288 PrintToScrollback("bits:");
1289 PrintToScrollback("%s", bits
);
1290 PrintToScrollback("worst metric: %d at pos %d", worst
, worstPos
);
1292 if(strcmp(str
, "clone")==0) {
1295 for(s
= bits
; *s
; s
++) {
1297 for(j
= 0; j
< 16; j
++) {
1298 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
1301 RepaintGraphWindow();
1305 static void CmdFlexdemod(char *str
)
1308 for(i
= 0; i
< GraphTraceLen
; i
++) {
1309 if(GraphBuffer
[i
] < 0) {
1310 GraphBuffer
[i
] = -1;
1316 #define LONG_WAIT 100
1318 for(start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
1319 int first
= GraphBuffer
[start
];
1320 for(i
= start
; i
< start
+ LONG_WAIT
; i
++) {
1321 if(GraphBuffer
[i
] != first
) {
1325 if(i
== (start
+ LONG_WAIT
)) {
1329 if(start
== GraphTraceLen
- LONG_WAIT
) {
1330 PrintToScrollback("nothing to wait for");
1334 GraphBuffer
[start
] = 2;
1335 GraphBuffer
[start
+1] = -2;
1341 for(bit
= 0; bit
< 64; bit
++) {
1344 for(j
= 0; j
< 16; j
++) {
1345 sum
+= GraphBuffer
[i
++];
1352 PrintToScrollback("bit %d sum %d", bit
, sum
);
1355 for(bit
= 0; bit
< 64; bit
++) {
1358 for(j
= 0; j
< 16; j
++) {
1359 sum
+= GraphBuffer
[i
++];
1361 if(sum
> 0 && bits
[bit
] != 1) {
1362 PrintToScrollback("oops1 at %d", bit
);
1364 if(sum
< 0 && bits
[bit
] != 0) {
1365 PrintToScrollback("oops2 at %d", bit
);
1369 GraphTraceLen
= 32*64;
1372 for(bit
= 0; bit
< 64; bit
++) {
1373 if(bits
[bit
] == 0) {
1379 for(j
= 0; j
< 32; j
++) {
1380 GraphBuffer
[i
++] = phase
;
1385 RepaintGraphWindow();
1389 * Generic command to demodulate ASK. bit length in argument.
1390 * Giving the bit length helps discriminate ripple effects
1391 * upon zero crossing for noisy traces.
1393 * Second is convention: positive or negative (High mod means zero
1394 * or high mod means one)
1396 * Updates the Graph trace with 0/1 values
1399 * sl : bit length in terms of number of samples per bit
1400 * (use yellow/purple markers to compute).
1404 static void Cmdaskdemod(char *str
) {
1411 // TODO: complain if we do not give 2 arguments here !
1412 sscanf(str
, "%i %i", &n
, &c
);
1419 if (GraphBuffer
[0]*c
> 0) {
1424 for(i
=1;i
<GraphTraceLen
;i
++) {
1425 /* Analyse signal within the symbol length */
1426 /* Decide if we crossed a zero */
1427 if (GraphBuffer
[i
]*sign
< 0) {
1428 /* Crossed a zero, check if this is a ripple or not */
1429 if ( (i
-t1
) > n
/4 ) {
1432 if (GraphBuffer
[i
]*c
> 0){
1438 /* This is a ripple, set the current sample value
1439 to the same as previous */
1440 GraphBuffer
[i
] = GraphBuffer
[i
-1];
1443 GraphBuffer
[i
] = GraphBuffer
[i
-1];
1446 RepaintGraphWindow();
1451 * Manchester demodulate a bitstream. The bitstream needs to be already in
1452 * the GraphBuffer as 0 and 1 values
1454 * Give the clock rate as argument in order to help the sync - the algorithm
1455 * resyncs at each pulse anyway.
1457 * Not optimized by any means, this is the 1st time I'm writing this type of
1458 * routine, feel free to improve...
1460 * 1st argument: clock rate (as number of samples per clock rate)
1462 static void Cmdmanchesterdemod(char *str
) {
1472 sscanf(str
, "%i", &clock
);
1474 int tolerance
= clock
/4;
1475 /* Holds the decoded bitstream. */
1476 int BitStream
[MAX_GRAPH_TRACE_LEN
*2];
1477 int BitStream2
[MAX_GRAPH_TRACE_LEN
];
1479 /* Detect first transition */
1480 /* Lo-Hi (arbitrary) */
1481 for(i
=1;i
<GraphTraceLen
;i
++) {
1482 if (GraphBuffer
[i
-1]<GraphBuffer
[i
]) {
1484 BitStream
[0]=0; // Previous state = 0;
1489 /* Then detect duration between 2 successive transitions */
1490 /* At this stage, GraphTrace is either 0 or 1 */
1491 for(bitidx
= 1 ;i
<GraphTraceLen
;i
++) {
1492 if (GraphBuffer
[i
-1] != GraphBuffer
[i
]) {
1495 // Then switch depending on lc length:
1496 // Tolerance is 1/4 of clock rate (arbitrary)
1497 if ((lc
-clock
/2) < tolerance
) {
1499 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1500 } else if ((lc
-clock
) < tolerance
) {
1502 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1503 BitStream
[bitidx
++]=GraphBuffer
[i
-1];
1506 PrintToScrollback("Warning: Manchester decode error for pulse width detection.");
1507 PrintToScrollback("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
1512 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
1513 for (bitidx2
= 0; bitidx2
<bitidx
; bitidx2
+= 2) {
1514 if ((BitStream
[bitidx2
] == 0) && (BitStream
[bitidx2
+1] == 1)) {
1515 BitStream2
[bitidx2
/2] = 1;
1516 } else if ((BitStream
[bitidx2
] == 1) && (BitStream
[bitidx2
+1] == 0)) {
1517 BitStream2
[bitidx2
/2] = 0;
1519 // We cannot end up in this stage, this means we are unsynchronized,
1522 PrintToScrollback("Unsynchronized, resync...");
1523 PrintToScrollback("(too many of those messages mean the stream is not Manchester encoded)");
1526 PrintToScrollback("Manchester decoded bitstream \n---------");
1527 // Now output the bitstream to the scrollback by line of 16 bits
1528 for (i
= 0; i
<bitidx
/2; i
+=16) {
1529 PrintToScrollback("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
1554 static void CmdHiddemod(char *str
)
1556 if(GraphTraceLen
< 4800) {
1557 PrintToScrollback("too short; need at least 4800 samples");
1561 GraphTraceLen
= 4800;
1563 for(i
= 0; i
< GraphTraceLen
; i
++) {
1564 if(GraphBuffer
[i
] < 0) {
1570 RepaintGraphWindow();
1573 static void CmdPlot(char *str
)
1578 static void CmdHide(char *str
)
1583 static void CmdScale(char *str
)
1585 CursorScaleFactor
= atoi(str
);
1586 if(CursorScaleFactor
== 0) {
1587 PrintToScrollback("bad, can't have zero scale");
1588 CursorScaleFactor
= 1;
1590 RepaintGraphWindow();
1593 static void CmdSave(char *str
)
1595 FILE *f
= fopen(str
, "w");
1597 PrintToScrollback("couldn't open '%s'", str
);
1601 for(i
= 0; i
< GraphTraceLen
; i
++) {
1602 fprintf(f
, "%d\n", GraphBuffer
[i
]);
1605 PrintToScrollback("saved to '%s'", str
);
1608 static void CmdLoad(char *str
)
1610 FILE *f
= fopen(str
, "r");
1612 PrintToScrollback("couldn't open '%s'", str
);
1618 while(fgets(line
, sizeof(line
), f
)) {
1619 GraphBuffer
[GraphTraceLen
] = atoi(line
);
1623 PrintToScrollback("loaded %d samples", GraphTraceLen
);
1624 RepaintGraphWindow();
1627 static void CmdHIDsimTAG(char *str
)
1629 unsigned int hi
=0, lo
=0;
1633 while (sscanf(&str
[i
++], "%1x", &n
) == 1) {
1634 hi
=(hi
<<4)|(lo
>>28);
1638 PrintToScrollback("Emulating tag with ID %x%16x", hi
, lo
);
1640 c
.cmd
= CMD_HID_SIM_TAG
;
1643 SendCommand(&c
, FALSE
);
1646 static void CmdLcdReset(char *str
)
1649 c
.cmd
= CMD_LCD_RESET
;
1651 SendCommand(&c
, FALSE
);
1654 static void CmdLcd(char *str
)
1659 sscanf(str
, "%x %d", &i
, &j
);
1662 SendCommand(&c
, FALSE
);
1666 static void CmdTest(char *str
)
1670 typedef void HandlerFunction(char *cmdline
);
1674 HandlerFunction
*handler
;
1676 } CommandTable
[] = {
1677 "tune", CmdTune
, "measure antenna tuning",
1678 "tiread", CmdTiread
, "read a TI-type 134 kHz tag",
1679 "tibits", CmdTibits
, "get raw bits for TI-type LF tag",
1680 "tidemod", CmdTidemod
, "demod raw bits for TI-type LF tag",
1681 "vchdemod", CmdVchdemod
, "demod samples for VeriChip",
1682 "plot", CmdPlot
, "show graph window",
1683 "hide", CmdHide
, "hide graph window",
1684 "losim", CmdLosim
, "simulate LF tag",
1685 "loread", CmdLoread
, "read (125/134 kHz) LF ID-only tag",
1686 "losamples", CmdLosamples
, "get raw samples for LF tag",
1687 "hisamples", CmdHisamples
, "get raw samples for HF tag",
1688 "hisampless", CmdHisampless
, "get signed raw samples, HF tag",
1689 "hisamplest", CmdHi14readt
, "get samples HF, for testing",
1690 "higet", CmdHi14read_sim
, "get samples HF, 'analog'",
1691 "bitsamples", CmdBitsamples
, "get raw samples as bitstring",
1692 "hexsamples", CmdHexsamples
, "dump big buffer as hex bytes",
1693 "hi15read", CmdHi15read
, "read HF tag (ISO 15693)",
1694 "hi15reader", CmdHi15reader
, "act like an ISO15693 reader", // new command greg
1695 "hi15sim", CmdHi15tag
, "fake an ISO15693 tag", // new command greg
1696 "hi14read", CmdHi14read
, "read HF tag (ISO 14443)",
1697 "hi14areader", CmdHi14areader
, "act like an ISO14443 Type A reader", // ## New reader command
1698 "hi15demod", CmdHi15demod
, "demod ISO15693 from tag",
1699 "hi14bdemod", CmdHi14bdemod
, "demod ISO14443 Type B from tag",
1700 "autocorr", CmdAutoCorr
, "autocorrelation over window",
1701 "norm", CmdNorm
, "normalize max/min to +/-500",
1702 "dec", CmdDec
, "decimate",
1703 "hpf", CmdHpf
, "remove DC offset from trace",
1704 "zerocrossings", CmdZerocrossings
, "count time between zero-crossings",
1705 "ltrim", CmdLtrim
, "trim from left of trace",
1706 "scale", CmdScale
, "set cursor display scale",
1707 "flexdemod", CmdFlexdemod
, "demod samples for FlexPass",
1708 "save", CmdSave
, "save trace (from graph window)",
1709 "load", CmdLoad
, "load trace (to graph window",
1710 "hisimlisten", CmdHisimlisten
, "get HF samples as fake tag",
1711 "hi14sim", CmdHi14sim
, "fake ISO 14443 tag",
1712 "hi14asim", CmdHi14asim
, "fake ISO 14443a tag", // ## Simulate 14443a tag
1713 "hi14snoop", CmdHi14snoop
, "eavesdrop ISO 14443",
1714 "hi14asnoop", CmdHi14asnoop
, "eavesdrop ISO 14443 Type A", // ## New snoop command
1715 "hi14list", CmdHi14list
, "list ISO 14443 history",
1716 "hi14alist", CmdHi14alist
, "list ISO 14443a history", // ## New list command
1717 "hiddemod", CmdHiddemod
, "HID Prox Card II (not optimal)",
1718 "hidfskdemod", CmdHIDdemodFSK
, "HID FSK demodulator",
1719 "askdemod", Cmdaskdemod
, "Attempt to demodulate simple ASK tags",
1720 "hidsimtag", CmdHIDsimTAG
, "HID tag simulator",
1721 "mandemod", Cmdmanchesterdemod
, "Try a Manchester demodulation on a binary stream",
1722 "fpgaoff", CmdFPGAOff
, "set FPGA off", // ## FPGA Control
1723 "lcdreset", CmdLcdReset
, "Hardware reset LCD",
1724 "lcd", CmdLcd
, "Send command/data to LCD",
1725 "test", CmdTest
, "Placeholder command for testing new code",
1726 "quit", CmdQuit
, "quit program"
1729 //-----------------------------------------------------------------------------
1730 // Entry point into our code: called whenever the user types a command and
1731 // then presses Enter, which the full command line that they typed.
1732 //-----------------------------------------------------------------------------
1733 void CommandReceived(char *cmd
)
1737 PrintToScrollback("> %s", cmd
);
1739 if(strcmp(cmd
, "help")==0) {
1740 PrintToScrollback("\r\nAvailable commands:");
1741 for(i
= 0; i
< sizeof(CommandTable
) / sizeof(CommandTable
[0]); i
++) {
1743 memset(line
, ' ', sizeof(line
));
1744 strcpy(line
+2, CommandTable
[i
].name
);
1745 line
[strlen(line
)] = ' ';
1746 sprintf(line
+15, " -- %s", CommandTable
[i
].docString
);
1747 PrintToScrollback("%s", line
);
1749 PrintToScrollback("");
1750 PrintToScrollback("and also: help, cls");
1754 for(i
= 0; i
< sizeof(CommandTable
) / sizeof(CommandTable
[0]); i
++) {
1755 char *name
= CommandTable
[i
].name
;
1756 if(memcmp(cmd
, name
, strlen(name
))==0 &&
1757 (cmd
[strlen(name
)] == ' ' || cmd
[strlen(name
)] == '\0'))
1759 cmd
+= strlen(name
);
1760 while(*cmd
== ' ') {
1763 (CommandTable
[i
].handler
)(cmd
);
1767 PrintToScrollback(">> bad command '%s'", cmd
);
1770 //-----------------------------------------------------------------------------
1771 // Entry point into our code: called whenever we received a packet over USB
1772 // that we weren't necessarily expecting, for example a debug print.
1773 //-----------------------------------------------------------------------------
1774 void UsbCommandReceived(UsbCommand
*c
)
1777 case CMD_DEBUG_PRINT_STRING
: {
1779 if(c
->ext1
> 70 || c
->ext1
< 0) {
1782 memcpy(s
, c
->d
.asBytes
, c
->ext1
);
1784 PrintToScrollback("#db# %s", s
);
1788 case CMD_DEBUG_PRINT_INTEGERS
:
1789 PrintToScrollback("#db# %08x, %08x, %08x\r\n", c
->ext1
, c
->ext2
, c
->ext3
);
1792 case CMD_MEASURED_ANTENNA_TUNING
: {
1794 int vLf125
, vLf134
, vHf
;
1795 vLf125
= c
->ext1
& 0xffff;
1796 vLf134
= c
->ext1
>> 16;
1798 zLf
= c
->ext3
& 0xffff;
1799 zHf
= c
->ext3
>> 16;
1800 PrintToScrollback("# LF antenna @ %3d mA / %5d mV [%d ohms] 125Khz",
1801 vLf125
/zLf
, vLf125
, zLf
);
1802 PrintToScrollback("# LF antenna @ %3d mA / %5d mV [%d ohms] 134Khz",
1803 vLf134
/((zLf
*125)/134), vLf134
, (zLf
*125)/134);
1804 PrintToScrollback("# HF antenna @ %3d mA / %5d mV [%d ohms] 13.56Mhz",
1809 PrintToScrollback("unrecognized command %08x\n", c
->cmd
);