14 #include "cmdlfem4x.h"
16 static int CmdHelp(const char *Cmd
);
18 /* send a command before reading */
19 int CmdLFCommandRead(const char *Cmd
)
25 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
26 sscanf(Cmd
, "%i %i %i %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2], (char *) &c
.d
.asBytes
,(char *) &dummy
+1);
27 // in case they specified 'h'
28 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
33 int CmdFlexdemod(const char *Cmd
)
36 for (i
= 0; i
< GraphTraceLen
; ++i
) {
37 if (GraphBuffer
[i
] < 0) {
46 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
47 int first
= GraphBuffer
[start
];
48 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
49 if (GraphBuffer
[i
] != first
) {
53 if (i
== (start
+ LONG_WAIT
)) {
57 if (start
== GraphTraceLen
- LONG_WAIT
) {
58 PrintAndLog("nothing to wait for");
62 GraphBuffer
[start
] = 2;
63 GraphBuffer
[start
+1] = -2;
69 for (bit
= 0; bit
< 64; bit
++) {
72 for (j
= 0; j
< 16; j
++) {
73 sum
+= GraphBuffer
[i
++];
80 PrintAndLog("bit %d sum %d", bit
, sum
);
83 for (bit
= 0; bit
< 64; bit
++) {
86 for (j
= 0; j
< 16; j
++) {
87 sum
+= GraphBuffer
[i
++];
89 if (sum
> 0 && bits
[bit
] != 1) {
90 PrintAndLog("oops1 at %d", bit
);
92 if (sum
< 0 && bits
[bit
] != 0) {
93 PrintAndLog("oops2 at %d", bit
);
97 GraphTraceLen
= 32*64;
100 for (bit
= 0; bit
< 64; bit
++) {
101 if (bits
[bit
] == 0) {
107 for (j
= 0; j
< 32; j
++) {
108 GraphBuffer
[i
++] = phase
;
113 RepaintGraphWindow();
117 int CmdIndalaDemod(const char *Cmd
)
119 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
124 // worst case with GraphTraceLen=64000 is < 4096
125 // under normal conditions it's < 2048
126 uint8_t rawbits
[4096];
128 int worst
= 0, worstPos
= 0;
129 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
130 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
132 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
134 for (j
= 0; j
< count
- 8; j
+= 16) {
135 rawbits
[rawbit
++] = 0;
137 if ((abs(count
- j
)) > worst
) {
138 worst
= abs(count
- j
);
144 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
146 for (j
= 0; j
< count
- 8; j
+= 16) {
147 rawbits
[rawbit
++] = 1;
149 if ((abs(count
- j
)) > worst
) {
150 worst
= abs(count
- j
);
158 PrintAndLog("Recovered %d raw bits", rawbit
);
159 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
161 // Finding the start of a UID
162 int uidlen
, long_wait
;
163 if (strcmp(Cmd
, "224") == 0) {
172 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
173 first
= rawbits
[start
];
174 for (i
= start
; i
< start
+ long_wait
; i
++) {
175 if (rawbits
[i
] != first
) {
179 if (i
== (start
+ long_wait
)) {
183 if (start
== rawbit
- uidlen
+ 1) {
184 PrintAndLog("nothing to wait for");
188 // Inverting signal if needed
190 for (i
= start
; i
< rawbit
; i
++) {
191 rawbits
[i
] = !rawbits
[i
];
198 showbits
[uidlen
]='\0';
202 if (uidlen
> rawbit
) {
203 PrintAndLog("Warning: not enough raw bits to get a full UID");
204 for (bit
= 0; bit
< rawbit
; bit
++) {
205 bits
[bit
] = rawbits
[i
++];
206 // As we cannot know the parity, let's use "." and "/"
207 showbits
[bit
] = '.' + bits
[bit
];
209 showbits
[bit
+1]='\0';
210 PrintAndLog("Partial UID=%s", showbits
);
213 for (bit
= 0; bit
< uidlen
; bit
++) {
214 bits
[bit
] = rawbits
[i
++];
215 showbits
[bit
] = '0' + bits
[bit
];
219 PrintAndLog("UID=%s", showbits
);
221 // Checking UID against next occurences
222 for (; i
+ uidlen
<= rawbit
;) {
224 for (bit
= 0; bit
< uidlen
; bit
++) {
225 if (bits
[bit
] != rawbits
[i
++]) {
235 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
237 // Remodulating for tag cloning
238 GraphTraceLen
= 32*uidlen
;
241 for (bit
= 0; bit
< uidlen
; bit
++) {
242 if (bits
[bit
] == 0) {
248 for (j
= 0; j
< 32; j
++) {
249 GraphBuffer
[i
++] = phase
;
254 RepaintGraphWindow();
258 int CmdLFRead(const char *Cmd
)
260 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
261 // 'h' means higher-low-frequency, 134 kHz
264 } else if (*Cmd
== '\0') {
267 PrintAndLog("use 'read' or 'read h'");
271 WaitForResponse(CMD_ACK
);
275 static void ChkBitstream(const char *str
)
279 /* convert to bitstream if necessary */
280 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
282 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
290 int CmdLFSim(const char *Cmd
)
295 sscanf(Cmd
, "%i", &gap
);
297 /* convert to bitstream if necessary */
300 PrintAndLog("Sending data, please wait...");
301 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
302 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
304 for (j
= 0; j
< 48; j
++) {
305 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
308 WaitForResponse(CMD_ACK
);
311 PrintAndLog("Starting simulator...");
312 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
317 int CmdLFSimBidir(const char *Cmd
)
319 /* Set ADC to twice the carrier for a slight supersampling */
320 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
325 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
326 int CmdLFSimManchester(const char *Cmd
)
328 static int clock
, gap
;
329 static char data
[1024], gapstring
[8];
331 /* get settings/bits */
332 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
334 /* clear our graph */
337 /* fill it with our bitstream */
338 for (int i
= 0; i
< strlen(data
) ; ++i
)
339 AppendGraph(0, clock
, data
[i
]- '0');
342 CmdManchesterMod("");
344 /* show what we've done */
345 RepaintGraphWindow();
348 sprintf(&gapstring
[0], "%i", gap
);
353 int CmdVchDemod(const char *Cmd
)
355 // Is this the entire sync pattern, or does this also include some
356 // data bits that happen to be the same everywhere? That would be
358 static const int SyncPattern
[] = {
359 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
360 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
361 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
362 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
363 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
364 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
365 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
366 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
367 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
368 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
371 // So first, we correlate for the sync pattern, and mark that.
372 int bestCorrel
= 0, bestPos
= 0;
374 // It does us no good to find the sync pattern, with fewer than
375 // 2048 samples after it...
376 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
379 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
380 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
382 if (sum
> bestCorrel
) {
387 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
395 for (i
= 0; i
< 2048; i
+= 8) {
398 for (j
= 0; j
< 8; j
++) {
399 sum
+= GraphBuffer
[bestPos
+i
+j
];
406 if(abs(sum
) < worst
) {
411 PrintAndLog("bits:");
412 PrintAndLog("%s", bits
);
413 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
415 if (strcmp(Cmd
, "clone")==0) {
418 for(s
= bits
; *s
; s
++) {
420 for(j
= 0; j
< 16; j
++) {
421 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
424 RepaintGraphWindow();
429 static command_t CommandTable
[] =
431 {"help", CmdHelp
, 1, "This help"},
432 {"cmdread", CmdLFCommandRead
, 0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"},
433 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
434 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
435 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
436 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
437 {"read", CmdLFRead
, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
438 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
439 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
440 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
441 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
442 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
443 {NULL
, NULL
, 0, NULL
}
446 int CmdLF(const char *Cmd
)
448 CmdsParse(CommandTable
, Cmd
);
452 int CmdHelp(const char *Cmd
)
454 CmdsHelp(CommandTable
);