1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency ISO15693 commands
9 //-----------------------------------------------------------------------------
19 #include "cmdparser.h"
22 static int CmdHelp(const char *Cmd
);
24 static uint16_t Iso15693Crc(uint8_t *v
, int n
)
30 for (i
= 0; i
< n
; i
++) {
31 reg
= reg
^ ((uint32_t)v
[i
]);
32 for (j
= 0; j
< 8; j
++) {
34 reg
= (reg
>> 1) ^ 0x8408;
41 return (uint16_t)~reg
;
44 int CmdHF15Demod(const char *Cmd
)
46 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
49 // 1) Unmodulated time of 56.64us
50 // 2) 24 pulses of 423.75khz
51 // 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
53 static const int FrameSOF
[] = {
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63 static const int Logic0
[] = {
69 static const int Logic1
[] = {
77 // 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
78 // 2) 24 pulses of 423.75khz
79 // 3) Unmodulated time of 56.64us
81 static const int FrameEOF
[] = {
86 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
87 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
88 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
89 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
93 int max
= 0, maxPos
= 0;
97 if (GraphTraceLen
< 1000) return 0;
99 // First, correlate for SOF
100 for (i
= 0; i
< 100; i
++) {
102 for (j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
103 corr
+= FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
110 PrintAndLog("SOF at %d, correlation %d", maxPos
,
111 max
/ (arraylen(FrameSOF
) / skip
));
113 i
= maxPos
+ arraylen(FrameSOF
) / skip
;
116 memset(outBuf
, 0, sizeof(outBuf
));
119 int corr0
= 0, corr1
= 0, corrEOF
= 0;
120 for (j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
121 corr0
+= Logic0
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
123 for (j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
124 corr1
+= Logic1
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
126 for (j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
127 corrEOF
+= FrameEOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
129 // Even things out by the length of the target waveform.
133 if (corrEOF
> corr1
&& corrEOF
> corr0
) {
134 PrintAndLog("EOF at %d", i
);
136 } else if (corr1
> corr0
) {
137 i
+= arraylen(Logic1
) / skip
;
140 i
+= arraylen(Logic0
) / skip
;
147 if ((i
+ (int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
148 PrintAndLog("ran off end!");
153 PrintAndLog("error, uneven octet! (discard extra bits!)");
154 PrintAndLog(" mask=%02x", mask
);
156 PrintAndLog("%d octets", k
);
158 for (i
= 0; i
< k
; i
++) {
159 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
161 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
165 int CmdHF15Read(const char *Cmd
)
167 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
172 int CmdHF15Reader(const char *Cmd
)
174 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
179 int CmdHF15Sim(const char *Cmd
)
181 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
186 static command_t CommandTable
[] =
188 {"help", CmdHelp
, 1, "This help"},
189 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
190 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
191 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
192 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
193 {NULL
, NULL
, 0, NULL
}
196 int CmdHF15(const char *Cmd
)
198 CmdsParse(CommandTable
, Cmd
);
202 int CmdHelp(const char *Cmd
)
204 CmdsHelp(CommandTable
);