partial ISO15693 support (based on patch by Bjoern Kaiser)
[librfid] / src / rfid_layer2_iso15693.c
1 /* ISO 15693 anticollision implementation
2  *
3  * (C) 2005-2008 by Harald Welte <laforge@gnumonks.org>
4  * (C) 2007 by Bjoern Riemer <bjoern.riemer@web.de>
5  */
6
7 /*
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
11  *
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.
16  *
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
20  */
21
22 #define DEBUG_LIBRFID
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <librfid/rfid.h>
30 #include <librfid/rfid_layer2.h>
31 #include <librfid/rfid_reader.h>
32 #include <librfid/rfid_layer2_iso15693.h>
33
34 struct iso15693_request_read {
35         struct iso15693_request req;
36         u_int64_t uid;
37         u_int8_t blocknum;
38 } __attribute__ ((packed));
39
40 struct iso15693_request_adressed {
41         struct iso15693_request head;
42         u_int64_t uid;
43 } __attribute__ ((packed));
44
45 #define ISO15693_BLOCK_SIZE_MAX (256/8)
46 #define ISO15693_RESP_SIZE_MAX  (4+ISO15693_BLOCK_SIZE_MAX)
47
48 #define TIMEOUT 200
49
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)
55 {
56         return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
57                                         tx_len, rx_buf, rx_len, timeout, flags);
58 }
59
60 /* Transmit an anticollission frame */
61 static int
62 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
63                         struct iso15693_anticol_cmd *acf,
64                         unsigned char uuid[ISO15693_UID_LEN],
65                         char *bit_of_col)
66 {
67         struct rfid_reader *rdr = handle->rh->reader;
68         if (!rdr->iso15693.transceive_ac)
69                 return -1;
70         return rdr->iso15693.transceive_ac(handle->rh, acf, uuid, bit_of_col);
71 }
72
73 #if 0
74
75 static int
76 iso15693_read_block(struct rfid_layer2_handle *handle,
77                     u_int8_t blocknr, u_int32_t *data)
78 {
79         int rc;
80         struct iso15693_request_read req;
81         u_int8_t resp[ISO15693_RESP_SIZE_MAX];
82
83         req.req.flags = 0;
84         req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
85         memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
86         req.blocknum = blocknr;
87
88         /* FIXME: fill CRC if required */
89
90         rc = iso15693_transceive(... &req, ...,  );
91
92         if (rc < 0)
93                 return rc;
94
95         memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
96
97         return rc-1;
98 }
99
100 static int
101 iso15693_write_block()
102 {
103         struct iso16593_request_read *rreq;
104         u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
105
106         rreq = (struct iso15693_request_read *) req;
107
108         rreq->req.flags = ;
109         rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
110         memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
111         rreq->blocknum = blocknr;
112         memcpy(rreq->);
113
114 }
115
116 static int
117 iso15693_lock_block()
118 {
119 }
120
121 #endif
122
123 static int
124 iso15693_anticol(struct rfid_layer2_handle *handle)
125 {
126         int i, ret;
127         int rx_len = 0;
128         int num_valid = 0;
129         struct iso15693_anticol_cmd acf;
130         char uuid[ISO15693_UID_LEN];
131         char boc;
132
133         char uuid_list[16][ISO15693_UID_LEN];
134         int uuid_list_valid[16];
135
136 #define MY_NONE 0
137 #define MY_COLL 1
138 #define MY_UUID 2
139
140         memset(uuid_list_valid, MY_NONE, 16);
141         memset(uuid_list, 0, ISO15693_UID_LEN * 16);
142
143         memset(&acf, 0, sizeof(struct iso15693_anticol_cmd));
144         acf.afi = 0;
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
149         acf.mask_len = 0;
150         //acf.mask_bits[0] = 3;
151         acf.current_slot = 0;
152
153         if (acf.flags & RFID_15693_F5_NSLOTS_1)
154                 i = 1;
155         else
156                 i = 16;
157         for (; i >=1; i--) {
158                 //acf.current_slot=0;
159                 ret = iso15693_transceive_acf(handle, &acf, &uuid[0], &boc);
160                 switch (ret) {
161                 case -ETIMEDOUT:
162                         DEBUGP("no answer from vicc in slot %d\n",
163                                 acf.current_slot);
164                         uuid_list_valid[acf.current_slot] = MY_NONE;
165                         break;
166                 case -ECOLLISION:
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);
171                         break;
172                 default:
173                         if (ret < 0) {
174                                 DEBUGP("ERROR ret: %d, slot %d\n", ret,
175                                         acf.current_slot);
176                                 uuid_list_valid[acf.current_slot] = MY_NONE;
177                         } else {
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,
183                                         ISO15693_UID_LEN);
184                         }
185                 }
186                 usleep(1000*200);
187         }
188         if (acf.flags & RFID_15693_F5_NSLOTS_1)
189                 i = 1;
190         else
191                 i = 16;
192
193         while (i) {
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));
199                         num_valid++;
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));
206                 }
207                 i--;
208         }
209         if (num_valid == 0)
210                 return -1;
211
212         return num_valid;
213 }
214
215 static int
216 iso15693_select(struct rfid_layer2_handle *handle)
217 {
218         struct iso15693_request_adressed tx_req;
219         int ret;
220         unsigned int rx_len, tx_len;
221
222         struct {
223                 struct iso15693_response head;
224                 u_int8_t error;
225                 unsigned char crc[2];
226         } rx_buf;
227         rx_len = sizeof(rx_buf);
228
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);
241         return -1;
242 }
243
244 static int
245 iso15693_getopt(struct rfid_layer2_handle *handle,
246                 int optname, void *optval, unsigned int *optlen)
247 {
248         switch (optname) {
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:
253         default:
254                 return -EINVAL;
255                 break;
256         }
257         return 0;
258 }
259
260 static int
261 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
262                 const void *optval, unsigned int optlen)
263 {
264         switch (optname) {
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:
269         default:
270                 return -EINVAL;
271                 break;
272         }
273         return 0;
274 }
275
276 static int transceive_inventory(struct rfid_layer2_handle *l2h)
277 {
278         return -1;
279 }
280
281 static struct rfid_layer2_handle *
282 iso15693_init(struct rfid_reader_handle *rh)
283 {
284         int ret;
285         struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
286         if (!h)
287                 return NULL;
288
289         h->l2 = &rfid_layer2_iso15693;
290         h->rh = rh;
291         h->priv.iso15693.state = ISO15693_STATE_NONE;
292         ret = h->rh->reader->iso15693.init(h->rh);
293         if (ret < 0) {
294                 free_layer2_handle(h);
295                 return NULL;
296         }
297
298         return h;
299 }
300
301 static int
302 iso15693_fini(struct rfid_layer2_handle *handle)
303 {
304         free_layer2_handle(handle);
305         return 0;
306 }
307
308
309 const struct rfid_layer2 rfid_layer2_iso15693 = {
310         .id     = RFID_LAYER2_ISO15693,
311         .name   = "ISO 15693",
312         .fn     = {
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,
321         },
322 };
323