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
27 #include <librfid/rfid.h>
28 #include <librfid/rfid_layer2.h>
29 #include <librfid/rfid_reader.h>
30 #include <librfid/rfid_layer2_iso15693.h>
32 struct iso15693_request_read {
33 struct iso15693_request req;
36 } __attribute__ ((packed));
38 struct iso15693_request_adressed {
39 struct iso15693_request head;
41 } __attribute__ ((packed));
43 #define ISO15693_BLOCK_SIZE_MAX (256/8)
44 #define ISO15693_RESP_SIZE_MAX (4+ISO15693_BLOCK_SIZE_MAX)
46 const unsigned int iso15693_timing[2][5] = {
48 [ISO15693_T1] = 1216, /* max time after VCD EOF before VICC SOF */
49 [ISO15693_T2] = 1200, /* min time before VCD EOF after VICC response */
50 [ISO15693_T3] = 1502, /* min time after VCD EOF before next EOF if no VICC response */
51 [ISO15693_T4] = 1216, /* time after wich VICC transmits after VCD EOF */
52 [ISO15693_T4_WRITE]=20000, /* time after wich VICC transmits after VCD EOF */
55 [ISO15693_T1] = 304, /* max time after VCD EOF before VICC SOF */
56 [ISO15693_T2] = 300, /* min time before VCD EOF after VICC response */
57 [ISO15693_T3] = 602, /* min time after VCD EOF before next EOF if no VICC response */
58 [ISO15693_T4] = 304, /* time after wich VICC transmits after VCD EOF */
59 [ISO15693_T4_WRITE]=20000, /* time after wich VICC transmits after VCD EOF */
64 iso15693_get_response_error_name(u_int8_t error){
66 case RFID_15693_ERR_NOTSUPP:
68 case RFID_15693_ERR_INVALID: /* command not recognized */
70 case RFID_15693_ERR_UNKNOWN: /* unknown error */
72 case RFID_15693_ERR_BLOCK_NA: /* block not available */
74 case RFID_15693_ERR_BLOCK_LOCKED:
75 return "ERR_BLOCK_LOCKE";
76 case RFID_15693_ERR_BLOCK_LOCKED_CH:
77 return "ERR_BLOCK_LOCKED_CH";
78 case RFID_15693_ERR_BLOCK_NOTPROG:
79 return "ERR_BLOCK_NOTPROG";
80 case RFID_15693_ERR_BLOCK_NOTLOCK:
81 return "ERR_BLOCK_NOTLOCK";
82 case 0xA0: /* until 0xDF*/
83 return "Custom Command error Code";
90 static int iso15693_transceive(struct rfid_layer2_handle *handle,
91 enum rfid_frametype frametype,
92 const unsigned char *tx_buf, unsigned int tx_len,
93 unsigned char *rx_buf, unsigned int *rx_len,
94 u_int64_t timeout, unsigned int flags)
96 return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
97 tx_len, rx_buf, rx_len, timeout, flags);
100 /* Transmit an anticollission frame */
102 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
103 const struct iso15693_anticol_cmd *acf,
104 unsigned int acf_len,
105 struct iso15693_anticol_resp *resp,
106 unsigned int *rx_len, char *bit_of_col)
108 const struct rfid_reader *rdr = handle->rh->reader;
109 if (!rdr->iso15693.transceive_ac)
111 return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
117 iso15693_read_block(struct rfid_layer2_handle *handle,
118 u_int8_t blocknr, u_int32_t *data)
121 struct iso15693_request_read req;
122 u_int8_t resp[ISO15693_RESP_SIZE_MAX];
125 req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
126 memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
127 req.blocknum = blocknr;
129 /* FIXME: fill CRC if required */
131 rc = iso15693_transceive(... &req, ..., );
136 memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
142 iso15693_write_block()
144 struct iso16593_request_read *rreq;
145 u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
147 rreq = (struct iso15693_request_read *) req;
150 rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
151 memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
152 rreq->blocknum = blocknr;
158 iso15693_lock_block()
164 /* Helper function to build an ISO 15693 anti collision frame */
166 iso15693_build_acf(u_int8_t *target, u_int8_t flags, u_int8_t afi,
167 u_int8_t mask_len, u_int8_t *mask)
169 struct iso15693_request *req = (struct iso15693_request *) target;
173 req->command = ISO15693_CMD_INVENTORY;
174 if (flags & RFID_15693_F5_AFI_PRES)
175 req->data[i++] = afi;
176 req->data[i++] = mask_len;
178 for (j = 0; j < mask_len; j++)
179 req->data[i++] = mask[j];
181 return i + sizeof(*req);
185 iso15693_anticol(struct rfid_layer2_handle *handle)
191 struct iso15693_anticol_cmd_afi w_afi;
192 struct iso15693_anticol_cmd no_afi;
195 struct iso15693_anticol_resp resp;
199 int num_slots = MAX_SLOTS;
201 u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
202 int uuid_list_valid[MAX_SLOTS];
210 memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
211 memset(uuid_list, 0, sizeof(uuid_list));
213 //memset(&acf, 0, sizeof(acf));
215 /* FIXME: we can't use multiple slots at this point, since the RC632
216 * with librfid on the host PC has too much latency between 'EOF pulse
217 * to mark start of next slot' and 'receive data' commands :( */
219 flags = RFID_15693_F_INV_TABLE_5;
220 if (handle->priv.iso15693.vicc_fast)
221 flags |= RFID_15693_F_RATE_HIGH;
222 if (handle->priv.iso15693.vicc_two_subc)
223 flags |= RFID_15693_F_SUBC_TWO;
224 if (handle->priv.iso15693.single_slot) {
225 flags |= RFID_15693_F5_NSLOTS_1;
228 if (handle->priv.iso15693.use_afi)
229 flags |= RFID_15693_F5_AFI_PRES;
231 tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
232 handle->priv.iso15693.afi, 0, NULL);
234 for (i = 0; i < num_slots; i++) {
235 rx_len = sizeof(resp);
236 ret = iso15693_transceive_acf(handle, (u_int8_t *) &acf, tx_len, &resp, &rx_len, &boc);
237 if (ret == -ETIMEDOUT) {
238 DEBUGP("no answer from vicc in slot %d\n", i);
239 uuid_list_valid[i] = MY_NONE;
240 } else if (ret < 0) {
241 DEBUGP("ERROR ret: %d, slot %d\n", ret, i);
242 uuid_list_valid[i] = MY_NONE;
246 DEBUGP("Collision during anticol. slot %d bit %d\n",
248 uuid_list_valid[i] = -boc;
249 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
251 DEBUGP("Slot %d ret: %d UUID: %s\n", i, ret,
252 rfid_hexdump(resp.uuid, ISO15693_UID_LEN));
253 uuid_list_valid[i] = MY_UUID;
254 memcpy(&uuid_list[i][0], resp.uuid, ISO15693_UID_LEN);
259 for (i = 0; i < num_slots; i++) {
260 if (uuid_list_valid[i] == MY_NONE) {
261 DEBUGP("slot[%d]: timeout\n",i);
262 } else if (uuid_list_valid[i] == MY_UUID) {
263 DEBUGP("slot[%d]: VALID uuid: %s\n", i,
264 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
265 memcpy(handle->uid, uuid_list[i], ISO15693_UID_LEN);
266 handle->uid_len = ISO15693_UID_LEN;
268 } else if (uuid_list_valid[i] < 0) {
269 DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
270 i,uuid_list_valid[i]*-1,
271 (uuid_list_valid[i]*-1)/8,
272 (uuid_list_valid[i]*-1)%8,
273 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
284 iso15693_select(struct rfid_layer2_handle *handle)
286 struct iso15693_request_adressed tx_req;
288 unsigned int rx_len, tx_len;
291 struct iso15693_response head;
293 unsigned char crc[2];
295 rx_len = sizeof(rx_buf);
297 tx_req.head.command = ISO15693_CMD_SELECT;
298 tx_req.head.flags = RFID_15693_F4_ADDRESS | RFID_15693_F_SUBC_TWO ;
299 tx_req.uid = 0xE0070000020C1F18;
300 //req.uid = 0x181F0C02000007E0;
301 //req.uid = 0xe004010001950837;
302 //req.uid = 0x37089501000104e0;
303 tx_len = sizeof(tx_req);
304 DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
305 ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
306 tx_len, (u_int8_t*)&rx_buf, &rx_len, 50,0);
307 DEBUGP("ret: %d, error_flag: %d -> error: %02x\n", ret,
308 rx_buf.head.flags&RFID_15693_RF_ERROR, rx_buf.error);
313 iso15693_stay_quiet(struct rfid_layer2_handle *l2h)
315 struct iso15693_request_adressed tx_req;
317 unsigned int rx_len, tx_len;
320 struct iso15693_response head;
322 unsigned char crc[2];
324 rx_len = sizeof(rx_buf);
326 tx_req.head.command = ISO15693_CMD_STAY_QUIET;
328 tx_req.head.flags = RFID_15693_F4_ADDRESS;
329 if (l2h->priv.iso15693.vicc_fast)
330 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
331 if (l2h->priv.iso15693.vicc_two_subc)
332 tx_req.head.flags |= RFID_15693_F_SUBC_TWO;
333 memcpy(&tx_req.uid, l2h->uid, ISO15693_UID_LEN);
334 tx_len = sizeof(tx_req);
336 DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
338 ret = iso15693_transceive(l2h, RFID_15693_FRAME, (u_int8_t*)&tx_req,
339 tx_len, (u_int8_t*)&rx_buf, &rx_len, 10,0);
341 l2h->priv.iso15693.state = RFID_15693_STATE_QUIET;
343 DEBUGP("ret: %d%s, error_flag: %d", ret,(ret==-ETIMEDOUT)?"(TIMEOUT)":"",
344 rx_buf.head.flags&RFID_15693_RF_ERROR);
345 if (rx_buf.head.flags&RFID_15693_RF_ERROR)
346 DEBUGPC(" -> error: %02x\n", rx_buf.error);
354 iso15693_getopt(struct rfid_layer2_handle *handle,
355 int optname, void *optval, unsigned int *optlen)
357 unsigned int *val = optval;
358 u_int8_t *val_u8 = optval;
360 if (!optlen || !optval || *optlen < sizeof(unsigned int))
363 *optlen = sizeof(unsigned int);
366 case RFID_OPT_15693_MOD_DEPTH:
367 if (handle->priv.iso15693.vcd_ask100)
368 *val = RFID_15693_MOD_100ASK;
370 *val = RFID_15693_MOD_10ASK;
372 case RFID_OPT_15693_VCD_CODING:
373 if (handle->priv.iso15693.vcd_out256)
374 *val = RFID_15693_VCD_CODING_1OUT256;
376 *val = RFID_15693_VCD_CODING_1OUT4;
378 case RFID_OPT_15693_VICC_SUBC:
379 if (handle->priv.iso15693.vicc_two_subc)
380 *val = RFID_15693_VICC_SUBC_DUAL;
382 *val = RFID_15693_VICC_SUBC_SINGLE;
384 case RFID_OPT_15693_VICC_SPEED:
385 if (handle->priv.iso15693.vicc_fast)
386 *val = RFID_15693_VICC_SPEED_FAST;
388 *val = RFID_15693_VICC_SPEED_SLOW;
390 case RFID_OPT_15693_VCD_SLOTS:
391 if (handle->priv.iso15693.single_slot)
396 case RFID_OPT_15693_USE_AFI:
397 if (handle->priv.iso15693.use_afi)
402 case RFID_OPT_15693_AFI:
403 *val_u8 = handle->priv.iso15693.afi;
404 *optlen = sizeof(u_int8_t);
415 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
416 const void *optval, unsigned int optlen)
420 if (optlen < sizeof(u_int8_t) || !optval)
423 if (optlen == sizeof(u_int8_t))
424 val = *((u_int8_t *) optval);
425 if (optlen == sizeof(u_int16_t))
426 val = *((u_int16_t *) optval);
427 if (optlen == sizeof(unsigned int))
428 val = *((unsigned int *) optval);
431 case RFID_OPT_15693_MOD_DEPTH:
433 case RFID_15693_MOD_10ASK:
434 handle->priv.iso15693.vcd_ask100 = 0;
436 case RFID_15693_MOD_100ASK:
437 handle->priv.iso15693.vcd_ask100 = 1;
443 case RFID_OPT_15693_VCD_CODING:
445 case RFID_15693_VCD_CODING_1OUT256:
446 handle->priv.iso15693.vcd_out256 = 1;
448 case RFID_15693_VCD_CODING_1OUT4:
449 handle->priv.iso15693.vcd_out256 = 0;
455 case RFID_OPT_15693_VICC_SUBC:
457 case RFID_15693_VICC_SUBC_SINGLE:
458 handle->priv.iso15693.vicc_two_subc = 0;
460 case RFID_15693_VICC_SUBC_DUAL:
461 handle->priv.iso15693.vicc_two_subc = 1;
467 case RFID_OPT_15693_VICC_SPEED:
469 case RFID_15693_VICC_SPEED_SLOW:
470 handle->priv.iso15693.vicc_fast = 0;
472 case RFID_15693_VICC_SPEED_FAST:
473 handle->priv.iso15693.vicc_fast = 1;
478 case RFID_OPT_15693_VCD_SLOTS:
481 handle->priv.iso15693.single_slot = 0;
484 handle->priv.iso15693.single_slot = 1;
490 case RFID_OPT_15693_USE_AFI:
492 handle->priv.iso15693.use_afi = 1;
494 handle->priv.iso15693.use_afi = 0;
496 case RFID_OPT_15693_AFI:
499 handle->priv.iso15693.afi = val;
507 static int transceive_inventory(struct rfid_layer2_handle *l2h)
512 static struct rfid_layer2_handle *
513 iso15693_init(struct rfid_reader_handle *rh)
516 struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
520 h->l2 = &rfid_layer2_iso15693;
522 h->priv.iso15693.state = ISO15693_STATE_NONE;
523 h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
524 h->priv.iso15693.vicc_two_subc = 0;
525 h->priv.iso15693.vicc_fast = 1;
526 h->priv.iso15693.single_slot = 1;
527 h->priv.iso15693.vcd_out256 = 0;
528 h->priv.iso15693.use_afi = 0; /* not all VICC support AFI */
529 h->priv.iso15693.afi = 0;
531 ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
533 free_layer2_handle(h);
541 iso15693_fini(struct rfid_layer2_handle *handle)
543 free_layer2_handle(handle);
548 const struct rfid_layer2 rfid_layer2_iso15693 = {
549 .id = RFID_LAYER2_ISO15693,
552 .init = &iso15693_init,
553 .open = &iso15693_anticol,
554 //.open = &iso15693_select,
555 //.transceive = &iso15693_transceive,
556 .close = &iso15693_stay_quiet,
557 .fini = &iso15693_fini,
558 .setopt = &iso15693_setopt,
559 .getopt = &iso15693_getopt,