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)
50 static int iso15693_transceive(struct rfid_layer2_handle *handle,
51 enum rfid_frametype frametype,
52 const unsigned char *tx_buf, unsigned int tx_len,
53 unsigned char *rx_buf, unsigned int *rx_len,
54 u_int64_t timeout, unsigned int flags)
56 return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
57 tx_len, rx_buf, rx_len, timeout, flags);
60 /* Transmit an anticollission frame */
62 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
63 struct iso15693_anticol_cmd *acf,
64 unsigned char uuid[ISO15693_UID_LEN],
67 struct rfid_reader *rdr = handle->rh->reader;
68 if (!rdr->iso15693.transceive_ac)
70 return rdr->iso15693.transceive_ac(handle->rh, acf, uuid, bit_of_col);
76 iso15693_read_block(struct rfid_layer2_handle *handle,
77 u_int8_t blocknr, u_int32_t *data)
80 struct iso15693_request_read req;
81 u_int8_t resp[ISO15693_RESP_SIZE_MAX];
84 req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
85 memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
86 req.blocknum = blocknr;
88 /* FIXME: fill CRC if required */
90 rc = iso15693_transceive(... &req, ..., );
95 memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
101 iso15693_write_block()
103 struct iso16593_request_read *rreq;
104 u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
106 rreq = (struct iso15693_request_read *) req;
109 rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
110 memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
111 rreq->blocknum = blocknr;
117 iso15693_lock_block()
124 iso15693_anticol(struct rfid_layer2_handle *handle)
129 struct iso15693_anticol_cmd acf;
130 char uuid[ISO15693_UID_LEN];
133 char uuid_list[16][ISO15693_UID_LEN];
134 int uuid_list_valid[16];
140 memset(uuid_list_valid, MY_NONE, 16);
141 memset(uuid_list, 0, ISO15693_UID_LEN * 16);
143 memset(&acf, 0, sizeof(struct iso15693_anticol_cmd));
145 acf.flags = RFID_15693_F5_NSLOTS_1 | /* comment out for 16 slots */
146 RFID_15693_F_INV_TABLE_5 |
147 RFID_15693_F_RATE_HIGH;
148 //RFID_15693_F_SUBC_TWO
150 //acf.mask_bits[0] = 3;
151 acf.current_slot = 0;
153 if (acf.flags & RFID_15693_F5_NSLOTS_1)
158 //acf.current_slot=0;
159 ret = iso15693_transceive_acf(handle, &acf, &uuid[0], &boc);
162 DEBUGP("no answer from vicc in slot %d\n",
164 uuid_list_valid[acf.current_slot] = MY_NONE;
167 DEBUGP("Collision during anticol. slot %d bit %d\n",
168 acf.current_slot,boc);
169 uuid_list_valid[acf.current_slot] = -boc;
170 memcpy(uuid_list[acf.current_slot], uuid, ISO15693_UID_LEN);
174 DEBUGP("ERROR ret: %d, slot %d\n", ret,
176 uuid_list_valid[acf.current_slot] = MY_NONE;
178 DEBUGP("Slot %d ret: %d UUID: %s\n",
179 acf.current_slot, ret,
180 rfid_hexdump(uuid, ISO15693_UID_LEN));
181 uuid_list_valid[acf.current_slot] = MY_UUID;
182 memcpy(&uuid_list[acf.current_slot][0], uuid,
188 if (acf.flags & RFID_15693_F5_NSLOTS_1)
194 if (uuid_list_valid[i] == MY_NONE) {
195 DEBUGP("slot[%d]: timeout\n",i);
196 } else if (uuid_list_valid[i] == MY_UUID) {
197 DEBUGP("slot[%d]: VALID uuid: %s\n", i,
198 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
200 } else if (uuid_list_valid[i] < 0) {
201 DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
202 i,uuid_list_valid[i]*-1,
203 (uuid_list_valid[i]*-1)/8,
204 (uuid_list_valid[i]*-1)%8,
205 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
216 iso15693_select(struct rfid_layer2_handle *handle)
218 struct iso15693_request_adressed tx_req;
220 unsigned int rx_len, tx_len;
223 struct iso15693_response head;
225 unsigned char crc[2];
227 rx_len = sizeof(rx_buf);
229 tx_req.head.command = ISO15693_CMD_SELECT;
230 tx_req.head.flags = RFID_15693_F4_ADDRESS | RFID_15693_F_SUBC_TWO ;
231 tx_req.uid = 0xE0070000020C1F18;
232 //req.uid = 0x181F0C02000007E0;
233 //req.uid = 0xe004010001950837;
234 //req.uid = 0x37089501000104e0;
235 tx_len = sizeof(tx_req);
236 DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
237 ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
238 tx_len, (u_int8_t*)&rx_buf, &rx_len, 50,0);
239 DEBUGP("ret: %d, error_flag: %d error: %d\n", ret,
240 rx_buf.head.flags&RFID_15693_RF_ERROR, 0);
245 iso15693_getopt(struct rfid_layer2_handle *handle,
246 int optname, void *optval, unsigned int *optlen)
249 case RFID_OPT_15693_MOD_DEPTH:
250 case RFID_OPT_15693_VCD_CODING:
251 case RFID_OPT_15693_VICC_SUBC:
252 case RFID_OPT_15693_VICC_SPEED:
261 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
262 const void *optval, unsigned int optlen)
265 case RFID_OPT_15693_MOD_DEPTH:
266 case RFID_OPT_15693_VCD_CODING:
267 case RFID_OPT_15693_VICC_SUBC:
268 case RFID_OPT_15693_VICC_SPEED:
276 static int transceive_inventory(struct rfid_layer2_handle *l2h)
281 static struct rfid_layer2_handle *
282 iso15693_init(struct rfid_reader_handle *rh)
285 struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
289 h->l2 = &rfid_layer2_iso15693;
291 h->priv.iso15693.state = ISO15693_STATE_NONE;
292 ret = h->rh->reader->iso15693.init(h->rh);
294 free_layer2_handle(h);
302 iso15693_fini(struct rfid_layer2_handle *handle)
304 free_layer2_handle(handle);
309 const struct rfid_layer2 rfid_layer2_iso15693 = {
310 .id = RFID_LAYER2_ISO15693,
313 .init = &iso15693_init,
314 .open = &iso15693_anticol,
315 //.open = &iso15693_select,
316 //.transceive = &iso15693_transceive,
317 //.close = &iso14443a_hlta,
318 .fini = &iso15693_fini,
319 .setopt = &iso15693_setopt,
320 .getopt = &iso15693_getopt,