1 /* ISO 14443-3 A anticollision implementation
3 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <librfid/rfid.h>
28 #include <librfid/rfid_layer2.h>
29 #include <librfid/rfid_reader.h>
30 #include <librfid/rfid_layer2_iso14443a.h>
34 /* Transcieve a 7-bit short frame */
36 iso14443a_transcieve_sf(struct rfid_layer2_handle *handle,
38 struct iso14443a_atqa *atqa)
40 struct rfid_reader *rdr = handle->rh->reader;
42 return rdr->iso14443a.transcieve_sf(handle->rh, cmd, atqa);
45 /* Transmit an anticollission bit frame */
47 iso14443a_transcieve_acf(struct rfid_layer2_handle *handle,
48 struct iso14443a_anticol_cmd *acf,
49 unsigned int *bit_of_col)
51 struct rfid_reader *rdr = handle->rh->reader;
53 return rdr->iso14443a.transcieve_acf(handle->rh, acf, bit_of_col);
56 /* Transmit a regular frame */
58 iso14443a_transcieve(struct rfid_layer2_handle *handle,
59 enum rfid_frametype frametype,
60 const unsigned char *tx_buf, unsigned int tx_len,
61 unsigned char *rx_buf, unsigned int *rx_len,
62 u_int64_t timeout, unsigned int flags)
64 return handle->rh->reader->transcieve(handle->rh, frametype, tx_buf,
65 tx_len, rx_buf, rx_len, timeout, flags);
69 iso14443a_code_nvb_bits(unsigned char *nvb, unsigned int bits)
71 unsigned int byte_count = bits / 8;
72 unsigned int bit_count = bits % 8;
74 if (byte_count < 2 || byte_count > 7)
77 *nvb = ((byte_count & 0xf) << 4) | bit_count;
82 /* first bit is '1', second bit '2' */
84 set_bit_in_field(unsigned char *bitfield, unsigned int bit)
86 unsigned int byte_count = bit / 8;
87 unsigned int bit_count = bit % 8;
89 DEBUGP("bitfield=%p, byte_count=%u, bit_count=%u\n",
90 bitfield, byte_count, bit_count);
91 DEBUGP("%p = 0x%02x\n", (bitfield+byte_count), *(bitfield+byte_count));
92 *(bitfield+byte_count) |= 1 << (bit_count-1);
93 DEBUGP("%p = 0x%02x\n", (bitfield+byte_count), *(bitfield+byte_count));
97 iso14443a_anticol(struct rfid_layer2_handle *handle)
100 unsigned int uid_size;
101 struct iso14443a_handle *h = &handle->priv.iso14443a;
102 struct iso14443a_atqa atqa;
103 struct iso14443a_anticol_cmd acf;
104 unsigned int bit_of_col;
105 unsigned char sak[3];
106 unsigned int rx_len = sizeof(sak);
107 char *aqptr = (char *) &atqa;
109 memset(handle->uid, 0, sizeof(handle->uid));
110 memset(sak, 0, sizeof(sak));
111 memset(&atqa, 0, sizeof(atqa));
112 memset(&acf, 0, sizeof(acf));
114 ret = iso14443a_transcieve_sf(handle, ISO14443A_SF_CMD_REQA, &atqa);
116 h->state = ISO14443A_STATE_REQA_SENT;
117 DEBUGP("error during transcieve_sf: %d\n", ret);
120 h->state = ISO14443A_STATE_ATQA_RCVD;
122 DEBUGP("ATQA: 0x%02x 0x%02x\n", *aqptr, *(aqptr+1));
124 if (!atqa.bf_anticol) {
125 h->state = ISO14443A_STATE_NO_BITFRAME_ANTICOL;
126 DEBUGP("no bitframe anticollission bits set, aborting\n");
130 if (atqa.uid_size == 2 || atqa.uid_size == 3)
132 else if (atqa.uid_size == 1)
137 acf.sel_code = ISO14443A_AC_SEL_CODE_CL1;
139 h->state = ISO14443A_STATE_ANTICOL_RUNNING;
140 h->level = ISO14443A_LEVEL_CL1;
143 iso14443a_code_nvb_bits(&acf.nvb, 16);
145 ret = iso14443a_transcieve_acf(handle, &acf, &bit_of_col);
148 DEBUGP("bit_of_col = %u\n", bit_of_col);
150 while (bit_of_col != ISO14443A_BITOFCOL_NONE) {
151 set_bit_in_field(&acf.uid_bits[0], bit_of_col-16);
152 iso14443a_code_nvb_bits(&acf.nvb, bit_of_col);
153 ret = iso14443a_transcieve_acf(handle, &acf, &bit_of_col);
154 DEBUGP("bit_of_col = %u\n", bit_of_col);
159 iso14443a_code_nvb_bits(&acf.nvb, 7*8);
160 ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR,
161 (unsigned char *)&acf, 7,
162 (unsigned char *) &sak, &rx_len,
168 /* Cascade bit set, UID not complete */
169 switch (acf.sel_code) {
170 case ISO14443A_AC_SEL_CODE_CL1:
171 /* cascading from CL1 to CL2 */
172 if (acf.uid_bits[0] != 0x88) {
173 DEBUGP("Cascade bit set, but UID0 != 0x88\n");
176 memcpy(&handle->uid[0], &acf.uid_bits[1], 3);
177 acf.sel_code = ISO14443A_AC_SEL_CODE_CL2;
178 h->level = ISO14443A_LEVEL_CL2;
180 case ISO14443A_AC_SEL_CODE_CL2:
181 /* cascading from CL2 to CL3 */
182 memcpy(&handle->uid[3], &acf.uid_bits[1], 3);
183 acf.sel_code = ISO14443A_AC_SEL_CODE_CL3;
184 h->level = ISO14443A_LEVEL_CL3;
187 DEBUGP("cannot cascade any further than CL3\n");
188 h->state = ISO14443A_STATE_ERROR;
195 switch (acf.sel_code) {
196 case ISO14443A_AC_SEL_CODE_CL1:
197 /* single size UID (4 bytes) */
198 memcpy(&handle->uid[0], &acf.uid_bits[0], 4);
200 case ISO14443A_AC_SEL_CODE_CL2:
201 /* double size UID (7 bytes) */
202 memcpy(&handle->uid[3], &acf.uid_bits[0], 4);
204 case ISO14443A_AC_SEL_CODE_CL3:
205 /* triple size UID (10 bytes) */
206 memcpy(&handle->uid[6], &acf.uid_bits[0], 4);
211 h->level = ISO14443A_LEVEL_NONE;
212 h->state = ISO14443A_STATE_SELECTED;
217 else if (uid_size == 2)
220 handle->uid_len = 10;
222 DEBUGP("UID %s\n", rfid_hexdump(handle->uid, handle->uid_len));
226 DEBUGP("we have a T=CL compliant PICC\n");
229 DEBUGP("we have a T!=CL PICC\n");
237 iso14443a_hlta(struct rfid_layer2_handle *handle)
240 unsigned char tx_buf[2] = { 0x50, 0x00 };
241 unsigned char rx_buf[10];
242 unsigned int rx_len = sizeof(rx_buf);
244 ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR,
245 tx_buf, sizeof(tx_buf),
246 rx_buf, &rx_len, 1000 /* 1ms */, 0);
248 /* "error" case: we don't get somethng back from the card */
255 iso14443a_setopt(struct rfid_layer2_handle *handle, int optname,
256 const void *optval, unsigned int optlen)
259 struct rfid_reader *rdr = handle->rh->reader;
263 case RFID_OPT_14443A_SPEED_RX:
264 if (!rdr->iso14443a.set_speed)
266 speed = *(unsigned int *)optval;
267 ret = rdr->iso14443a.set_speed(handle->rh, 0, speed);
269 case RFID_OPT_14443A_SPEED_TX:
270 if (!rdr->iso14443a.set_speed)
272 speed = *(unsigned int *)optval;
273 ret = rdr->iso14443a.set_speed(handle->rh, 1, speed);
281 static struct rfid_layer2_handle *
282 iso14443a_init(struct rfid_reader_handle *rh)
285 struct rfid_layer2_handle *h = malloc(sizeof(*h));
289 h->l2 = &rfid_layer2_iso14443a;
291 h->priv.iso14443a.state = ISO14443A_STATE_NONE;
292 h->priv.iso14443a.level = ISO14443A_LEVEL_NONE;
294 ret = h->rh->reader->iso14443a.init(h->rh);
304 iso14443a_fini(struct rfid_layer2_handle *handle)
311 struct rfid_layer2 rfid_layer2_iso14443a = {
312 .id = RFID_LAYER2_ISO14443A,
313 .name = "ISO 14443-3 A",
315 .init = &iso14443a_init,
316 .open = &iso14443a_anticol,
317 .transcieve = &iso14443a_transcieve,
318 .close = &iso14443a_hlta,
319 .fini = &iso14443a_fini,
320 .setopt = &iso14443a_setopt,