* implement rfid_reader_{get,set}opt()
[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 const unsigned int iso15693_timing[2][5] = {
49         [ISO15693_T_SLOW] = {
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 */
55         },
56         [ISO15693_T_FAST] = {
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 */
62         },
63 };
64
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)
70 {
71         return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
72                                         tx_len, rx_buf, rx_len, timeout, flags);
73 }
74
75 /* Transmit an anticollission frame */
76 static int
77 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
78                         const struct iso15693_anticol_cmd *acf,
79                         unsigned int acf_len,
80                         struct iso15693_anticol_resp *resp,
81                         unsigned int *rx_len, char *bit_of_col)
82 {
83         const struct rfid_reader *rdr = handle->rh->reader;
84         if (!rdr->iso15693.transceive_ac)
85                 return -1;
86         return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
87 }
88
89 #if 0
90
91 static int
92 iso15693_read_block(struct rfid_layer2_handle *handle,
93                     u_int8_t blocknr, u_int32_t *data)
94 {
95         int rc;
96         struct iso15693_request_read req;
97         u_int8_t resp[ISO15693_RESP_SIZE_MAX];
98
99         req.req.flags = 0;
100         req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
101         memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
102         req.blocknum = blocknr;
103
104         /* FIXME: fill CRC if required */
105
106         rc = iso15693_transceive(... &req, ...,  );
107
108         if (rc < 0)
109                 return rc;
110
111         memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
112
113         return rc-1;
114 }
115
116 static int
117 iso15693_write_block()
118 {
119         struct iso16593_request_read *rreq;
120         u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
121
122         rreq = (struct iso15693_request_read *) req;
123
124         rreq->req.flags = ;
125         rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
126         memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
127         rreq->blocknum = blocknr;
128         memcpy(rreq->);
129
130 }
131
132 static int
133 iso15693_lock_block()
134 {
135 }
136
137 #endif
138
139 /* Helper function to build an ISO 15693 anti collision frame */
140 static int
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)
143 {
144         struct iso15693_request *req = (struct iso15693_request *) target;
145         int i = 0, j;
146
147         req->flags = flags;
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;
152
153         for (j = 0; j < mask_len; j++)
154                 req->data[i++] = mask[j];
155         
156         return i + sizeof(*req);
157 }
158
159 static int
160 iso15693_anticol(struct rfid_layer2_handle *handle)
161 {
162         int i, ret;
163         int tx_len, rx_len;
164         int num_valid = 0;
165         union {
166                 struct iso15693_anticol_cmd_afi w_afi;
167                 struct iso15693_anticol_cmd no_afi;
168         } acf;
169
170         struct iso15693_anticol_resp resp;
171                 
172         char boc;
173 #define MAX_SLOTS 16    
174         int num_slots = MAX_SLOTS;
175
176         u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
177         int uuid_list_valid[MAX_SLOTS];
178
179         u_int8_t flags;
180
181 #define MY_NONE 0
182 #define MY_COLL 1
183 #define MY_UUID 2
184
185         memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
186         memset(uuid_list, 0, sizeof(uuid_list));
187
188         //memset(&acf, 0, sizeof(acf));
189
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 :( */
193
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;
201                 num_slots = 1;
202         }
203         if (handle->priv.iso15693.use_afi)
204                 flags |= RFID_15693_F5_AFI_PRES;
205
206         tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
207                                     handle->priv.iso15693.afi, 0, NULL);
208
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;
218                 } else {
219
220                         if (boc) {
221                                 DEBUGP("Collision during anticol. slot %d bit %d\n",
222                                         i, boc);
223                                 uuid_list_valid[i] = -boc;
224                                 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
225                         } else {
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);
230                         }
231                 }
232         }
233
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));
240                         num_valid++;
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));
247                 }
248         }
249
250         if (num_valid == 0)
251                 return -1;
252
253         return num_valid;
254 }
255
256 static int
257 iso15693_select(struct rfid_layer2_handle *handle)
258 {
259         struct iso15693_request_adressed tx_req;
260         int ret;
261         unsigned int rx_len, tx_len;
262
263         struct {
264                 struct iso15693_response head;
265                 u_int8_t error;
266                 unsigned char crc[2];
267         } rx_buf;
268         rx_len = sizeof(rx_buf);
269
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);
282         return -1;
283 }
284
285 static int
286 iso15693_getopt(struct rfid_layer2_handle *handle,
287                 int optname, void *optval, unsigned int *optlen)
288 {
289         unsigned int *val = optval;
290         u_int8_t *val_u8 = optval;
291
292         if (!optlen || !optval || *optlen < sizeof(unsigned int))
293                 return -EINVAL;
294         
295         *optlen = sizeof(unsigned int);
296
297         switch (optname) {
298         case RFID_OPT_15693_MOD_DEPTH:
299                 if (handle->priv.iso15693.vcd_ask100)
300                         *val = RFID_15693_MOD_100ASK;
301                 else
302                         *val = RFID_15693_MOD_10ASK;
303                 break;
304         case RFID_OPT_15693_VCD_CODING:
305                 if (handle->priv.iso15693.vcd_out256)
306                         *val = RFID_15693_VCD_CODING_1OUT256;
307                 else
308                         *val = RFID_15693_VCD_CODING_1OUT4;
309                 break;
310         case RFID_OPT_15693_VICC_SUBC:
311                 if (handle->priv.iso15693.vicc_two_subc)
312                         *val = RFID_15693_VICC_SUBC_DUAL;
313                 else
314                         *val = RFID_15693_VICC_SUBC_SINGLE;
315                 break;
316         case RFID_OPT_15693_VICC_SPEED:
317                 if (handle->priv.iso15693.vicc_fast)
318                         *val = RFID_15693_VICC_SPEED_FAST;
319                 else
320                         *val = RFID_15693_VICC_SPEED_SLOW;
321                 break;
322         case RFID_OPT_15693_VCD_SLOTS:
323                 if (handle->priv.iso15693.single_slot)
324                         *val = 1;
325                 else
326                         *val = 16;
327                 break;
328         case RFID_OPT_15693_USE_AFI:
329                 if (handle->priv.iso15693.use_afi)
330                         *val = 1;
331                 else
332                         *val = 0;
333                 break;
334         case RFID_OPT_15693_AFI:
335                 *val_u8 = handle->priv.iso15693.afi;
336                 *optlen = sizeof(u_int8_t);
337                 break;
338         default:
339                 return -EINVAL;
340                 break;
341         }
342
343         return 0;
344 }
345
346 static int
347 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
348                 const void *optval, unsigned int optlen)
349 {
350         unsigned int val;
351         
352         if (optlen < sizeof(u_int8_t) || !optval)
353                 return -EINVAL;
354
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);
361
362         switch (optname) {
363         case RFID_OPT_15693_MOD_DEPTH:
364                 switch (val) {
365                 case RFID_15693_MOD_10ASK:
366                         handle->priv.iso15693.vcd_ask100 = 0;
367                         break;
368                 case RFID_15693_MOD_100ASK:
369                         handle->priv.iso15693.vcd_ask100 = 1;
370                         break;
371                 default:
372                         return -EINVAL;
373                 }
374                 break;
375         case RFID_OPT_15693_VCD_CODING:
376                 switch (val) {
377                 case RFID_15693_VCD_CODING_1OUT256:
378                         handle->priv.iso15693.vcd_out256 = 1;
379                         break;
380                 case RFID_15693_VCD_CODING_1OUT4:
381                         handle->priv.iso15693.vcd_out256 = 0;
382                         break;
383                 default:
384                         return -EINVAL;
385                 }
386                 break;
387         case RFID_OPT_15693_VICC_SUBC:
388                 switch (val) {
389                 case RFID_15693_VICC_SUBC_SINGLE:
390                         handle->priv.iso15693.vicc_two_subc = 0;
391                         break;
392                 case RFID_15693_VICC_SUBC_DUAL:
393                         handle->priv.iso15693.vicc_two_subc = 1;
394                         break;
395                 default:
396                         return -EINVAL;
397                 }
398                 break;
399         case RFID_OPT_15693_VICC_SPEED:
400                 switch (val) {
401                 case RFID_15693_VICC_SPEED_SLOW:
402                         handle->priv.iso15693.vicc_fast = 0;
403                         break;
404                 case RFID_15693_VICC_SPEED_FAST:
405                         handle->priv.iso15693.vicc_fast = 1;
406                         break;
407                 default:
408                         return -EINVAL;
409                 }
410         case RFID_OPT_15693_VCD_SLOTS:
411                 switch (val) {
412                 case 16:
413                         handle->priv.iso15693.single_slot = 0;
414                         break;
415                 case 1:
416                         handle->priv.iso15693.single_slot = 1;
417                         break;
418                 default:
419                         return -EINVAL;
420                 }
421                 break;
422         case RFID_OPT_15693_USE_AFI:
423                 if (val)
424                         handle->priv.iso15693.use_afi = 1;
425                 else
426                         handle->priv.iso15693.use_afi = 1;
427                 break;
428         case RFID_OPT_15693_AFI:
429                 if (val > 0xff)
430                         return -EINVAL;
431                 handle->priv.iso15693.afi = val;
432                 break;
433         default:
434                 return -EINVAL;
435         }
436         return 0;
437 }
438
439 static int transceive_inventory(struct rfid_layer2_handle *l2h)
440 {
441         return -1;
442 }
443
444 static struct rfid_layer2_handle *
445 iso15693_init(struct rfid_reader_handle *rh)
446 {
447         int ret;
448         struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
449         if (!h)
450                 return NULL;
451
452         h->l2 = &rfid_layer2_iso15693;
453         h->rh = rh;
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;
462
463         ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
464         if (ret < 0) {
465                 free_layer2_handle(h);
466                 return NULL;
467         }
468
469         return h;
470 }
471
472 static int
473 iso15693_fini(struct rfid_layer2_handle *handle)
474 {
475         free_layer2_handle(handle);
476         return 0;
477 }
478
479
480 const struct rfid_layer2 rfid_layer2_iso15693 = {
481         .id     = RFID_LAYER2_ISO15693,
482         .name   = "ISO 15693",
483         .fn     = {
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,
492         },
493 };
494