1 /* ISO 15693 anticollision implementation
3 * (C) 2005-2008 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2007 by Bjoern Riemer <bjoern.riemer@web.de>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <librfid/rfid.h>
30 #include <librfid/rfid_layer2.h>
31 #include <librfid/rfid_reader.h>
32 #include <librfid/rfid_layer2_iso15693.h>
34 struct iso15693_request_read {
35 struct iso15693_request req;
38 } __attribute__ ((packed));
40 struct iso15693_request_adressed {
41 struct iso15693_request head;
43 } __attribute__ ((packed));
45 #define ISO15693_BLOCK_SIZE_MAX (256/8)
46 #define ISO15693_RESP_SIZE_MAX (4+ISO15693_BLOCK_SIZE_MAX)
48 const unsigned int iso15693_timing[2][5] = {
50 [ISO15693_T1] = 1216, /* max time after VCD EOF before VICC SOF */
51 [ISO15693_T2] = 1200, /* min time before VCD EOF after VICC response */
52 [ISO15693_T3] = 1502, /* min time after VCD EOF before next EOF if no VICC response */
53 [ISO15693_T4] = 1216, /* time after wich VICC transmits after VCD EOF */
54 [ISO15693_T4_WRITE]=20000, /* time after wich VICC transmits after VCD EOF */
57 [ISO15693_T1] = 304, /* max time after VCD EOF before VICC SOF */
58 [ISO15693_T2] = 300, /* min time before VCD EOF after VICC response */
59 [ISO15693_T3] = 602, /* min time after VCD EOF before next EOF if no VICC response */
60 [ISO15693_T4] = 304, /* time after wich VICC transmits after VCD EOF */
61 [ISO15693_T4_WRITE]=20000, /* time after wich VICC transmits after VCD EOF */
65 static int iso15693_transceive(struct rfid_layer2_handle *handle,
66 enum rfid_frametype frametype,
67 const unsigned char *tx_buf, unsigned int tx_len,
68 unsigned char *rx_buf, unsigned int *rx_len,
69 u_int64_t timeout, unsigned int flags)
71 return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
72 tx_len, rx_buf, rx_len, timeout, flags);
75 /* Transmit an anticollission frame */
77 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
78 const struct iso15693_anticol_cmd *acf,
80 struct iso15693_anticol_resp *resp,
81 unsigned int *rx_len, char *bit_of_col)
83 const struct rfid_reader *rdr = handle->rh->reader;
84 if (!rdr->iso15693.transceive_ac)
86 return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
92 iso15693_read_block(struct rfid_layer2_handle *handle,
93 u_int8_t blocknr, u_int32_t *data)
96 struct iso15693_request_read req;
97 u_int8_t resp[ISO15693_RESP_SIZE_MAX];
100 req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
101 memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
102 req.blocknum = blocknr;
104 /* FIXME: fill CRC if required */
106 rc = iso15693_transceive(... &req, ..., );
111 memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
117 iso15693_write_block()
119 struct iso16593_request_read *rreq;
120 u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
122 rreq = (struct iso15693_request_read *) req;
125 rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
126 memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
127 rreq->blocknum = blocknr;
133 iso15693_lock_block()
139 /* Helper function to build an ISO 15693 anti collision frame */
141 iso15693_build_acf(u_int8_t *target, u_int8_t flags, u_int8_t afi,
142 u_int8_t mask_len, u_int8_t *mask)
144 struct iso15693_request *req = (struct iso15693_request *) target;
148 req->command = ISO15693_CMD_INVENTORY;
149 if (flags & RFID_15693_F5_AFI_PRES)
150 req->data[i++] = afi;
151 req->data[i++] = mask_len;
153 for (j = 0; j < mask_len; j++)
154 req->data[i++] = mask[j];
156 return i + sizeof(*req);
160 iso15693_anticol(struct rfid_layer2_handle *handle)
166 struct iso15693_anticol_cmd_afi w_afi;
167 struct iso15693_anticol_cmd no_afi;
170 struct iso15693_anticol_resp resp;
174 int num_slots = MAX_SLOTS;
176 u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
177 int uuid_list_valid[MAX_SLOTS];
185 memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
186 memset(uuid_list, 0, sizeof(uuid_list));
188 //memset(&acf, 0, sizeof(acf));
190 /* FIXME: we can't use multiple slots at this point, since the RC632
191 * with librfid on the host PC has too much latency between 'EOF pulse
192 * to mark start of next slot' and 'receive data' commands :( */
194 flags = RFID_15693_F_INV_TABLE_5;
195 if (handle->priv.iso15693.vicc_fast)
196 flags |= RFID_15693_F_RATE_HIGH;
197 if (handle->priv.iso15693.vicc_two_subc)
198 flags |= RFID_15693_F_SUBC_TWO;
199 if (handle->priv.iso15693.single_slot) {
200 flags |= RFID_15693_F5_NSLOTS_1;
203 if (handle->priv.iso15693.use_afi)
204 flags |= RFID_15693_F5_AFI_PRES;
206 tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
207 handle->priv.iso15693.afi, 0, NULL);
209 for (i = 0; i < num_slots; i++) {
210 rx_len = sizeof(resp);
211 ret = iso15693_transceive_acf(handle, (u_int8_t *) &acf, tx_len, &resp, &rx_len, &boc);
212 if (ret == -ETIMEDOUT) {
213 DEBUGP("no answer from vicc in slot %d\n", i);
214 uuid_list_valid[i] = MY_NONE;
215 } else if (ret < 0) {
216 DEBUGP("ERROR ret: %d, slot %d\n", ret, i);
217 uuid_list_valid[i] = MY_NONE;
221 DEBUGP("Collision during anticol. slot %d bit %d\n",
223 uuid_list_valid[i] = -boc;
224 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
226 DEBUGP("Slot %d ret: %d UUID: %s\n", i, ret,
227 rfid_hexdump(resp.uuid, ISO15693_UID_LEN));
228 uuid_list_valid[i] = MY_UUID;
229 memcpy(&uuid_list[i][0], resp.uuid, ISO15693_UID_LEN);
234 for (i = 0; i < num_slots; i++) {
235 if (uuid_list_valid[i] == MY_NONE) {
236 DEBUGP("slot[%d]: timeout\n",i);
237 } else if (uuid_list_valid[i] == MY_UUID) {
238 DEBUGP("slot[%d]: VALID uuid: %s\n", i,
239 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
241 } else if (uuid_list_valid[i] < 0) {
242 DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
243 i,uuid_list_valid[i]*-1,
244 (uuid_list_valid[i]*-1)/8,
245 (uuid_list_valid[i]*-1)%8,
246 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
257 iso15693_select(struct rfid_layer2_handle *handle)
259 struct iso15693_request_adressed tx_req;
261 unsigned int rx_len, tx_len;
264 struct iso15693_response head;
266 unsigned char crc[2];
268 rx_len = sizeof(rx_buf);
270 tx_req.head.command = ISO15693_CMD_SELECT;
271 tx_req.head.flags = RFID_15693_F4_ADDRESS | RFID_15693_F_SUBC_TWO ;
272 tx_req.uid = 0xE0070000020C1F18;
273 //req.uid = 0x181F0C02000007E0;
274 //req.uid = 0xe004010001950837;
275 //req.uid = 0x37089501000104e0;
276 tx_len = sizeof(tx_req);
277 DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
278 ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
279 tx_len, (u_int8_t*)&rx_buf, &rx_len, 50,0);
280 DEBUGP("ret: %d, error_flag: %d error: %d\n", ret,
281 rx_buf.head.flags&RFID_15693_RF_ERROR, 0);
286 iso15693_getopt(struct rfid_layer2_handle *handle,
287 int optname, void *optval, unsigned int *optlen)
289 unsigned int *val = optval;
290 u_int8_t *val_u8 = optval;
292 if (!optlen || !optval || *optlen < sizeof(unsigned int))
295 *optlen = sizeof(unsigned int);
298 case RFID_OPT_15693_MOD_DEPTH:
299 if (handle->priv.iso15693.vcd_ask100)
300 *val = RFID_15693_MOD_100ASK;
302 *val = RFID_15693_MOD_10ASK;
304 case RFID_OPT_15693_VCD_CODING:
305 if (handle->priv.iso15693.vcd_out256)
306 *val = RFID_15693_VCD_CODING_1OUT256;
308 *val = RFID_15693_VCD_CODING_1OUT4;
310 case RFID_OPT_15693_VICC_SUBC:
311 if (handle->priv.iso15693.vicc_two_subc)
312 *val = RFID_15693_VICC_SUBC_DUAL;
314 *val = RFID_15693_VICC_SUBC_SINGLE;
316 case RFID_OPT_15693_VICC_SPEED:
317 if (handle->priv.iso15693.vicc_fast)
318 *val = RFID_15693_VICC_SPEED_FAST;
320 *val = RFID_15693_VICC_SPEED_SLOW;
322 case RFID_OPT_15693_VCD_SLOTS:
323 if (handle->priv.iso15693.single_slot)
328 case RFID_OPT_15693_USE_AFI:
329 if (handle->priv.iso15693.use_afi)
334 case RFID_OPT_15693_AFI:
335 *val_u8 = handle->priv.iso15693.afi;
336 *optlen = sizeof(u_int8_t);
347 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
348 const void *optval, unsigned int optlen)
352 if (optlen < sizeof(u_int8_t) || !optval)
355 if (optlen == sizeof(u_int8_t))
356 val = *((u_int8_t *) optval);
357 if (optlen == sizeof(u_int16_t))
358 val = *((u_int16_t *) optval);
359 if (optlen == sizeof(unsigned int))
360 val = *((unsigned int *) optval);
363 case RFID_OPT_15693_MOD_DEPTH:
365 case RFID_15693_MOD_10ASK:
366 handle->priv.iso15693.vcd_ask100 = 0;
368 case RFID_15693_MOD_100ASK:
369 handle->priv.iso15693.vcd_ask100 = 1;
375 case RFID_OPT_15693_VCD_CODING:
377 case RFID_15693_VCD_CODING_1OUT256:
378 handle->priv.iso15693.vcd_out256 = 1;
380 case RFID_15693_VCD_CODING_1OUT4:
381 handle->priv.iso15693.vcd_out256 = 0;
387 case RFID_OPT_15693_VICC_SUBC:
389 case RFID_15693_VICC_SUBC_SINGLE:
390 handle->priv.iso15693.vicc_two_subc = 0;
392 case RFID_15693_VICC_SUBC_DUAL:
393 handle->priv.iso15693.vicc_two_subc = 1;
399 case RFID_OPT_15693_VICC_SPEED:
401 case RFID_15693_VICC_SPEED_SLOW:
402 handle->priv.iso15693.vicc_fast = 0;
404 case RFID_15693_VICC_SPEED_FAST:
405 handle->priv.iso15693.vicc_fast = 1;
410 case RFID_OPT_15693_VCD_SLOTS:
413 handle->priv.iso15693.single_slot = 0;
416 handle->priv.iso15693.single_slot = 1;
422 case RFID_OPT_15693_USE_AFI:
424 handle->priv.iso15693.use_afi = 1;
426 handle->priv.iso15693.use_afi = 1;
428 case RFID_OPT_15693_AFI:
431 handle->priv.iso15693.afi = val;
439 static int transceive_inventory(struct rfid_layer2_handle *l2h)
444 static struct rfid_layer2_handle *
445 iso15693_init(struct rfid_reader_handle *rh)
448 struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
452 h->l2 = &rfid_layer2_iso15693;
454 h->priv.iso15693.state = ISO15693_STATE_NONE;
455 h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
456 h->priv.iso15693.vicc_two_subc = 0;
457 h->priv.iso15693.vicc_fast = 1;
458 h->priv.iso15693.single_slot = 1;
459 h->priv.iso15693.vcd_out256 = 0;
460 h->priv.iso15693.use_afi = 0; /* not all VICC support AFI */
461 h->priv.iso15693.afi = 0;
463 ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
465 free_layer2_handle(h);
473 iso15693_fini(struct rfid_layer2_handle *handle)
475 free_layer2_handle(handle);
480 const struct rfid_layer2 rfid_layer2_iso15693 = {
481 .id = RFID_LAYER2_ISO15693,
484 .init = &iso15693_init,
485 .open = &iso15693_anticol,
486 //.open = &iso15693_select,
487 //.transceive = &iso15693_transceive,
488 //.close = &iso14443a_hlta,
489 .fini = &iso15693_fini,
490 .setopt = &iso15693_setopt,
491 .getopt = &iso15693_getopt,