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 */
63 static int iso15693_transceive(struct rfid_layer2_handle *handle,
64 enum rfid_frametype frametype,
65 const unsigned char *tx_buf, unsigned int tx_len,
66 unsigned char *rx_buf, unsigned int *rx_len,
67 u_int64_t timeout, unsigned int flags)
69 return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
70 tx_len, rx_buf, rx_len, timeout, flags);
73 /* Transmit an anticollission frame */
75 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
76 const struct iso15693_anticol_cmd *acf,
78 struct iso15693_anticol_resp *resp,
79 unsigned int *rx_len, char *bit_of_col)
81 const struct rfid_reader *rdr = handle->rh->reader;
82 if (!rdr->iso15693.transceive_ac)
84 return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
90 iso15693_read_block(struct rfid_layer2_handle *handle,
91 u_int8_t blocknr, u_int32_t *data)
94 struct iso15693_request_read req;
95 u_int8_t resp[ISO15693_RESP_SIZE_MAX];
98 req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
99 memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
100 req.blocknum = blocknr;
102 /* FIXME: fill CRC if required */
104 rc = iso15693_transceive(... &req, ..., );
109 memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
115 iso15693_write_block()
117 struct iso16593_request_read *rreq;
118 u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
120 rreq = (struct iso15693_request_read *) req;
123 rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
124 memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
125 rreq->blocknum = blocknr;
131 iso15693_lock_block()
137 /* Helper function to build an ISO 15693 anti collision frame */
139 iso15693_build_acf(u_int8_t *target, u_int8_t flags, u_int8_t afi,
140 u_int8_t mask_len, u_int8_t *mask)
142 struct iso15693_request *req = (struct iso15693_request *) target;
146 req->command = ISO15693_CMD_INVENTORY;
147 if (flags & RFID_15693_F5_AFI_PRES)
148 req->data[i++] = afi;
149 req->data[i++] = mask_len;
151 for (j = 0; j < mask_len; j++)
152 req->data[i++] = mask[j];
154 return i + sizeof(*req);
158 iso15693_anticol(struct rfid_layer2_handle *handle)
164 struct iso15693_anticol_cmd_afi w_afi;
165 struct iso15693_anticol_cmd no_afi;
168 struct iso15693_anticol_resp resp;
172 int num_slots = MAX_SLOTS;
174 u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
175 int uuid_list_valid[MAX_SLOTS];
183 memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
184 memset(uuid_list, 0, sizeof(uuid_list));
186 //memset(&acf, 0, sizeof(acf));
188 /* FIXME: we can't use multiple slots at this point, since the RC632
189 * with librfid on the host PC has too much latency between 'EOF pulse
190 * to mark start of next slot' and 'receive data' commands :( */
192 flags = RFID_15693_F_INV_TABLE_5;
193 if (handle->priv.iso15693.vicc_fast)
194 flags |= RFID_15693_F_RATE_HIGH;
195 if (handle->priv.iso15693.vicc_two_subc)
196 flags |= RFID_15693_F_SUBC_TWO;
197 if (handle->priv.iso15693.single_slot) {
198 flags |= RFID_15693_F5_NSLOTS_1;
201 if (handle->priv.iso15693.use_afi)
202 flags |= RFID_15693_F5_AFI_PRES;
204 tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
205 handle->priv.iso15693.afi, 0, NULL);
207 for (i = 0; i < num_slots; i++) {
208 rx_len = sizeof(resp);
209 ret = iso15693_transceive_acf(handle, (u_int8_t *) &acf, tx_len, &resp, &rx_len, &boc);
210 if (ret == -ETIMEDOUT) {
211 DEBUGP("no answer from vicc in slot %d\n", i);
212 uuid_list_valid[i] = MY_NONE;
213 } else if (ret < 0) {
214 DEBUGP("ERROR ret: %d, slot %d\n", ret, i);
215 uuid_list_valid[i] = MY_NONE;
219 DEBUGP("Collision during anticol. slot %d bit %d\n",
221 uuid_list_valid[i] = -boc;
222 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
224 DEBUGP("Slot %d ret: %d UUID: %s\n", i, ret,
225 rfid_hexdump(resp.uuid, ISO15693_UID_LEN));
226 uuid_list_valid[i] = MY_UUID;
227 memcpy(&uuid_list[i][0], resp.uuid, ISO15693_UID_LEN);
232 for (i = 0; i < num_slots; i++) {
233 if (uuid_list_valid[i] == MY_NONE) {
234 DEBUGP("slot[%d]: timeout\n",i);
235 } else if (uuid_list_valid[i] == MY_UUID) {
236 DEBUGP("slot[%d]: VALID uuid: %s\n", i,
237 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
239 } else if (uuid_list_valid[i] < 0) {
240 DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
241 i,uuid_list_valid[i]*-1,
242 (uuid_list_valid[i]*-1)/8,
243 (uuid_list_valid[i]*-1)%8,
244 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
255 iso15693_select(struct rfid_layer2_handle *handle)
257 struct iso15693_request_adressed tx_req;
259 unsigned int rx_len, tx_len;
262 struct iso15693_response head;
264 unsigned char crc[2];
266 rx_len = sizeof(rx_buf);
268 tx_req.head.command = ISO15693_CMD_SELECT;
269 tx_req.head.flags = RFID_15693_F4_ADDRESS | RFID_15693_F_SUBC_TWO ;
270 tx_req.uid = 0xE0070000020C1F18;
271 //req.uid = 0x181F0C02000007E0;
272 //req.uid = 0xe004010001950837;
273 //req.uid = 0x37089501000104e0;
274 tx_len = sizeof(tx_req);
275 DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
276 ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
277 tx_len, (u_int8_t*)&rx_buf, &rx_len, 50,0);
278 DEBUGP("ret: %d, error_flag: %d error: %d\n", ret,
279 rx_buf.head.flags&RFID_15693_RF_ERROR, 0);
284 iso15693_getopt(struct rfid_layer2_handle *handle,
285 int optname, void *optval, unsigned int *optlen)
287 unsigned int *val = optval;
288 u_int8_t *val_u8 = optval;
290 if (!optlen || !optval || *optlen < sizeof(unsigned int))
293 *optlen = sizeof(unsigned int);
296 case RFID_OPT_15693_MOD_DEPTH:
297 if (handle->priv.iso15693.vcd_ask100)
298 *val = RFID_15693_MOD_100ASK;
300 *val = RFID_15693_MOD_10ASK;
302 case RFID_OPT_15693_VCD_CODING:
303 if (handle->priv.iso15693.vcd_out256)
304 *val = RFID_15693_VCD_CODING_1OUT256;
306 *val = RFID_15693_VCD_CODING_1OUT4;
308 case RFID_OPT_15693_VICC_SUBC:
309 if (handle->priv.iso15693.vicc_two_subc)
310 *val = RFID_15693_VICC_SUBC_DUAL;
312 *val = RFID_15693_VICC_SUBC_SINGLE;
314 case RFID_OPT_15693_VICC_SPEED:
315 if (handle->priv.iso15693.vicc_fast)
316 *val = RFID_15693_VICC_SPEED_FAST;
318 *val = RFID_15693_VICC_SPEED_SLOW;
320 case RFID_OPT_15693_VCD_SLOTS:
321 if (handle->priv.iso15693.single_slot)
326 case RFID_OPT_15693_USE_AFI:
327 if (handle->priv.iso15693.use_afi)
332 case RFID_OPT_15693_AFI:
333 *val_u8 = handle->priv.iso15693.afi;
334 *optlen = sizeof(u_int8_t);
345 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
346 const void *optval, unsigned int optlen)
350 if (optlen < sizeof(u_int8_t) || !optval)
353 if (optlen == sizeof(u_int8_t))
354 val = *((u_int8_t *) optval);
355 if (optlen == sizeof(u_int16_t))
356 val = *((u_int16_t *) optval);
357 if (optlen == sizeof(unsigned int))
358 val = *((unsigned int *) optval);
361 case RFID_OPT_15693_MOD_DEPTH:
363 case RFID_15693_MOD_10ASK:
364 handle->priv.iso15693.vcd_ask100 = 0;
366 case RFID_15693_MOD_100ASK:
367 handle->priv.iso15693.vcd_ask100 = 1;
373 case RFID_OPT_15693_VCD_CODING:
375 case RFID_15693_VCD_CODING_1OUT256:
376 handle->priv.iso15693.vcd_out256 = 1;
378 case RFID_15693_VCD_CODING_1OUT4:
379 handle->priv.iso15693.vcd_out256 = 0;
385 case RFID_OPT_15693_VICC_SUBC:
387 case RFID_15693_VICC_SUBC_SINGLE:
388 handle->priv.iso15693.vicc_two_subc = 0;
390 case RFID_15693_VICC_SUBC_DUAL:
391 handle->priv.iso15693.vicc_two_subc = 1;
397 case RFID_OPT_15693_VICC_SPEED:
399 case RFID_15693_VICC_SPEED_SLOW:
400 handle->priv.iso15693.vicc_fast = 0;
402 case RFID_15693_VICC_SPEED_FAST:
403 handle->priv.iso15693.vicc_fast = 1;
408 case RFID_OPT_15693_VCD_SLOTS:
411 handle->priv.iso15693.single_slot = 0;
414 handle->priv.iso15693.single_slot = 1;
420 case RFID_OPT_15693_USE_AFI:
422 handle->priv.iso15693.use_afi = 1;
424 handle->priv.iso15693.use_afi = 0;
426 case RFID_OPT_15693_AFI:
429 handle->priv.iso15693.afi = val;
437 static int transceive_inventory(struct rfid_layer2_handle *l2h)
442 static struct rfid_layer2_handle *
443 iso15693_init(struct rfid_reader_handle *rh)
446 struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
450 h->l2 = &rfid_layer2_iso15693;
452 h->priv.iso15693.state = ISO15693_STATE_NONE;
453 h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
454 h->priv.iso15693.vicc_two_subc = 0;
455 h->priv.iso15693.vicc_fast = 1;
456 h->priv.iso15693.single_slot = 1;
457 h->priv.iso15693.vcd_out256 = 0;
458 h->priv.iso15693.use_afi = 0; /* not all VICC support AFI */
459 h->priv.iso15693.afi = 0;
461 ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
463 free_layer2_handle(h);
471 iso15693_fini(struct rfid_layer2_handle *handle)
473 free_layer2_handle(handle);
478 const struct rfid_layer2 rfid_layer2_iso15693 = {
479 .id = RFID_LAYER2_ISO15693,
482 .init = &iso15693_init,
483 .open = &iso15693_anticol,
484 //.open = &iso15693_select,
485 //.transceive = &iso15693_transceive,
486 //.close = &iso14443a_hlta,
487 .fini = &iso15693_fini,
488 .setopt = &iso15693_setopt,
489 .getopt = &iso15693_getopt,