implement SELECT command for ISO15693 (Bjoern Riemer)
[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 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <librfid/rfid.h>
28 #include <librfid/rfid_layer2.h>
29 #include <librfid/rfid_reader.h>
30 #include <librfid/rfid_layer2_iso15693.h>
31
32 struct iso15693_request_read {
33         struct iso15693_request req;
34         u_int64_t uid;
35         u_int8_t blocknum;
36 } __attribute__ ((packed));
37
38 struct iso15693_request_adressed {
39         struct iso15693_request head;
40         u_int64_t uid;
41 } __attribute__ ((packed));
42
43 #define ISO15693_BLOCK_SIZE_MAX (256/8)
44 #define ISO15693_RESP_SIZE_MAX  (4+ISO15693_BLOCK_SIZE_MAX)
45
46 const unsigned int iso15693_timing[2][5] = {
47         [ISO15693_T_SLOW] = {
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 */
53         },
54         [ISO15693_T_FAST] = {
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 */
60         },
61 };
62
63 unsigned char
64 iso15693_get_response_error_name(u_int8_t error){
65         switch (error){
66                 case RFID_15693_ERR_NOTSUPP:
67                         return "ERR_NOTSUPP";
68                 case RFID_15693_ERR_INVALID: /* command not recognized */
69                         return "ERR_INVALID";
70                 case RFID_15693_ERR_UNKNOWN: /* unknown error */
71                         return "ERR_UNKNOWN";
72                 case RFID_15693_ERR_BLOCK_NA: /* block not available */
73                         return "ERR_BLOCK_N";
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";
84                 case 0xE0:
85                 default:
86                         return "Undef.";
87         }
88 }
89
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)
95 {
96         return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
97                                         tx_len, rx_buf, rx_len, timeout, flags);
98 }
99
100 /* Transmit an anticollission frame */
101 static int
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)
107 {
108         const struct rfid_reader *rdr = handle->rh->reader;
109         if (!rdr->iso15693.transceive_ac)
110                 return -1;
111         return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
112 }
113
114 #if 0
115
116 static int
117 iso15693_read_block(struct rfid_layer2_handle *handle,
118                     u_int8_t blocknr, u_int32_t *data)
119 {
120         int rc;
121         struct iso15693_request_read req;
122         u_int8_t resp[ISO15693_RESP_SIZE_MAX];
123
124         req.req.flags = 0;
125         req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
126         memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
127         req.blocknum = blocknr;
128
129         /* FIXME: fill CRC if required */
130
131         rc = iso15693_transceive(... &req, ...,  );
132
133         if (rc < 0)
134                 return rc;
135
136         memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
137
138         return rc-1;
139 }
140
141 static int
142 iso15693_write_block()
143 {
144         struct iso16593_request_read *rreq;
145         u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
146
147         rreq = (struct iso15693_request_read *) req;
148
149         rreq->req.flags = ;
150         rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
151         memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
152         rreq->blocknum = blocknr;
153         memcpy(rreq->);
154
155 }
156
157 static int
158 iso15693_lock_block()
159 {
160 }
161
162 #endif
163
164 /* Helper function to build an ISO 15693 anti collision frame */
165 static int
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)
168 {
169         struct iso15693_request *req = (struct iso15693_request *) target;
170         int i = 0, j;
171
172         req->flags = flags;
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;
177
178         for (j = 0; j < mask_len; j++)
179                 req->data[i++] = mask[j];
180         
181         return i + sizeof(*req);
182 }
183
184 static int
185 iso15693_anticol(struct rfid_layer2_handle *handle)
186 {
187         int i, ret;
188         int tx_len, rx_len;
189         int num_valid = 0;
190         union {
191                 struct iso15693_anticol_cmd_afi w_afi;
192                 struct iso15693_anticol_cmd no_afi;
193         } acf;
194
195         struct iso15693_anticol_resp resp;
196                 
197         u_int8_t boc;
198 #define MAX_SLOTS 16    
199         int num_slots = MAX_SLOTS;
200
201         u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
202         int uuid_list_valid[MAX_SLOTS];
203
204         u_int8_t flags;
205
206 #define MY_NONE 0
207 #define MY_COLL 1
208 #define MY_UUID 2
209
210         memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
211         memset(uuid_list, 0, sizeof(uuid_list));
212
213         //memset(&acf, 0, sizeof(acf));
214
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 :( */
218
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;
226                 num_slots = 1;
227         }
228         if (handle->priv.iso15693.use_afi)
229                 flags |= RFID_15693_F5_AFI_PRES;
230
231         tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
232                                     handle->priv.iso15693.afi, 0, NULL);
233
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;
243                 } else {
244
245                         if (boc) {
246                                 DEBUGP("Collision during anticol. slot %d bit %d\n",
247                                         i, boc);
248                                 uuid_list_valid[i] = -boc;
249                                 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
250                         } else {
251                                 DEBUGP("Slot %d ret: %d DSFID: %02x UUID: %s\n", i, ret,
252                                         resp.dsfid, 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);
255                         }
256                 }
257         }
258
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                         /* FIXME: move to init */
267                         handle->uid_len = ISO15693_UID_LEN;
268                         num_valid++;
269                 } else if (uuid_list_valid[i] < 0) {
270                         DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
271                                 i,uuid_list_valid[i]*-1,
272                                 (uuid_list_valid[i]*-1)/8,
273                                 (uuid_list_valid[i]*-1)%8,
274                         rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
275                 }
276         }
277
278         if (num_valid == 0)
279                 return -1;
280
281         return num_valid;
282 }
283
284 static int
285 iso15693_select(struct rfid_layer2_handle *l2h)
286 {
287         struct iso15693_request_adressed tx_req;
288         int ret;
289         unsigned int rx_len, tx_len;
290
291         struct {
292                 struct iso15693_response head;
293                 u_int8_t error;
294                 unsigned char crc[2];
295         } rx_buf;
296         rx_len = sizeof(rx_buf);
297
298         tx_req.head.command = ISO15693_CMD_SELECT;
299         tx_req.head.flags = RFID_15693_F4_ADDRESS;
300         if (l2h->priv.iso15693.vicc_fast)
301                 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
302         if (l2h->priv.iso15693.vicc_two_subc)
303                 tx_req.head.flags |= RFID_15693_F_SUBC_TWO;
304         memcpy(&tx_req.uid, l2h->uid, ISO15693_UID_LEN);
305         tx_len = sizeof(tx_req);
306
307         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
308
309         DEBUGP("ret: %d%s, error_flag: %d", ret,(ret==-ETIMEDOUT)?"(TIMEOUT)":"",
310                         rx_buf.head.flags&RFID_15693_RF_ERROR);
311         if (rx_buf.head.flags&RFID_15693_RF_ERROR){
312                 DEBUGPC(" -> error: %02x '%s'\n", rx_buf.error,
313                         iso15693_get_response_error_name(rx_buf.error));
314                 l2h->priv.iso15693.state = RFID_15693_STATE_SELECTED;
315                 return 0;
316         }else{
317                 DEBUGPC("\n");
318                 return -1;
319         }
320 }
321
322 static int
323 iso15693_stay_quiet(struct rfid_layer2_handle *l2h)
324 {
325         struct iso15693_request_adressed tx_req;
326         int ret;
327         unsigned int rx_len, tx_len;
328
329         struct {
330                 struct iso15693_response head;
331                 u_int8_t error;
332                 unsigned char crc[2];
333         } rx_buf;
334         rx_len = sizeof(rx_buf);
335
336         tx_req.head.command = ISO15693_CMD_STAY_QUIET;
337
338         tx_req.head.flags = RFID_15693_F4_ADDRESS;
339         if (l2h->priv.iso15693.vicc_fast)
340                 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
341         if (l2h->priv.iso15693.vicc_two_subc)
342                 tx_req.head.flags |= RFID_15693_F_SUBC_TWO;
343         memcpy(&tx_req.uid, l2h->uid, ISO15693_UID_LEN);
344         tx_len = sizeof(tx_req);
345
346         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
347
348         ret = iso15693_transceive(l2h, RFID_15693_FRAME, (u_int8_t*)&tx_req,
349                                   tx_len, (u_int8_t*)&rx_buf, &rx_len, 10,0);
350
351         l2h->priv.iso15693.state = RFID_15693_STATE_QUIET;
352
353         DEBUGP("ret: %d%s, error_flag: %d", ret,(ret==-ETIMEDOUT)?"(TIMEOUT)":"",
354                         rx_buf.head.flags&RFID_15693_RF_ERROR);
355         if (rx_buf.head.flags&RFID_15693_RF_ERROR)
356                 DEBUGPC(" -> error: %02x\n", rx_buf.error);
357         else
358                 DEBUGPC("\n");
359
360         return 0;
361 }
362
363 static int
364 iso15693_getopt(struct rfid_layer2_handle *handle,
365                 int optname, void *optval, unsigned int *optlen)
366 {
367         unsigned int *val = optval;
368         u_int8_t *val_u8 = optval;
369
370         if (!optlen || !optval || *optlen < sizeof(unsigned int))
371                 return -EINVAL;
372         
373         *optlen = sizeof(unsigned int);
374
375         switch (optname) {
376         case RFID_OPT_15693_MOD_DEPTH:
377                 if (handle->priv.iso15693.vcd_ask100)
378                         *val = RFID_15693_MOD_100ASK;
379                 else
380                         *val = RFID_15693_MOD_10ASK;
381                 break;
382         case RFID_OPT_15693_VCD_CODING:
383                 if (handle->priv.iso15693.vcd_out256)
384                         *val = RFID_15693_VCD_CODING_1OUT256;
385                 else
386                         *val = RFID_15693_VCD_CODING_1OUT4;
387                 break;
388         case RFID_OPT_15693_VICC_SUBC:
389                 if (handle->priv.iso15693.vicc_two_subc)
390                         *val = RFID_15693_VICC_SUBC_DUAL;
391                 else
392                         *val = RFID_15693_VICC_SUBC_SINGLE;
393                 break;
394         case RFID_OPT_15693_VICC_SPEED:
395                 if (handle->priv.iso15693.vicc_fast)
396                         *val = RFID_15693_VICC_SPEED_FAST;
397                 else
398                         *val = RFID_15693_VICC_SPEED_SLOW;
399                 break;
400         case RFID_OPT_15693_VCD_SLOTS:
401                 if (handle->priv.iso15693.single_slot)
402                         *val = 1;
403                 else
404                         *val = 16;
405                 break;
406         case RFID_OPT_15693_USE_AFI:
407                 if (handle->priv.iso15693.use_afi)
408                         *val = 1;
409                 else
410                         *val = 0;
411                 break;
412         case RFID_OPT_15693_AFI:
413                 *val_u8 = handle->priv.iso15693.afi;
414                 *optlen = sizeof(u_int8_t);
415                 break;
416         default:
417                 return -EINVAL;
418                 break;
419         }
420
421         return 0;
422 }
423
424 static int
425 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
426                 const void *optval, unsigned int optlen)
427 {
428         unsigned int val;
429         
430         if (optlen < sizeof(u_int8_t) || !optval)
431                 return -EINVAL;
432
433         if (optlen == sizeof(u_int8_t))
434                 val = *((u_int8_t *) optval);
435         if (optlen == sizeof(u_int16_t))
436                 val = *((u_int16_t *) optval);
437         if (optlen == sizeof(unsigned int))
438                 val = *((unsigned int *) optval);
439
440         switch (optname) {
441         case RFID_OPT_15693_MOD_DEPTH:
442                 switch (val) {
443                 case RFID_15693_MOD_10ASK:
444                         handle->priv.iso15693.vcd_ask100 = 0;
445                         break;
446                 case RFID_15693_MOD_100ASK:
447                         handle->priv.iso15693.vcd_ask100 = 1;
448                         break;
449                 default:
450                         return -EINVAL;
451                 }
452                 break;
453         case RFID_OPT_15693_VCD_CODING:
454                 switch (val) {
455                 case RFID_15693_VCD_CODING_1OUT256:
456                         handle->priv.iso15693.vcd_out256 = 1;
457                         break;
458                 case RFID_15693_VCD_CODING_1OUT4:
459                         handle->priv.iso15693.vcd_out256 = 0;
460                         break;
461                 default:
462                         return -EINVAL;
463                 }
464                 break;
465         case RFID_OPT_15693_VICC_SUBC:
466                 switch (val) {
467                 case RFID_15693_VICC_SUBC_SINGLE:
468                         handle->priv.iso15693.vicc_two_subc = 0;
469                         break;
470                 case RFID_15693_VICC_SUBC_DUAL:
471                         handle->priv.iso15693.vicc_two_subc = 1;
472                         break;
473                 default:
474                         return -EINVAL;
475                 }
476                 break;
477         case RFID_OPT_15693_VICC_SPEED:
478                 switch (val) {
479                 case RFID_15693_VICC_SPEED_SLOW:
480                         handle->priv.iso15693.vicc_fast = 0;
481                         break;
482                 case RFID_15693_VICC_SPEED_FAST:
483                         handle->priv.iso15693.vicc_fast = 1;
484                         break;
485                 default:
486                         return -EINVAL;
487                 }
488         case RFID_OPT_15693_VCD_SLOTS:
489                 switch (val) {
490                 case 16:
491                         handle->priv.iso15693.single_slot = 0;
492                         break;
493                 case 1:
494                         handle->priv.iso15693.single_slot = 1;
495                         break;
496                 default:
497                         return -EINVAL;
498                 }
499                 break;
500         case RFID_OPT_15693_USE_AFI:
501                 if (val)
502                         handle->priv.iso15693.use_afi = 1;
503                 else
504                         handle->priv.iso15693.use_afi = 0;
505                 break;
506         case RFID_OPT_15693_AFI:
507                 if (val > 0xff)
508                         return -EINVAL;
509                 handle->priv.iso15693.afi = val;
510                 break;
511         default:
512                 return -EINVAL;
513         }
514         return 0;
515 }
516
517 static int transceive_inventory(struct rfid_layer2_handle *l2h)
518 {
519         return -1;
520 }
521
522 static struct rfid_layer2_handle *
523 iso15693_init(struct rfid_reader_handle *rh)
524 {
525         int ret;
526         struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
527         if (!h)
528                 return NULL;
529
530         h->l2 = &rfid_layer2_iso15693;
531         h->rh = rh;
532         h->priv.iso15693.state = ISO15693_STATE_NONE;
533         h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
534         h->priv.iso15693.vicc_two_subc = 0;
535         h->priv.iso15693.vicc_fast = 1;
536         h->priv.iso15693.single_slot = 1;
537         h->priv.iso15693.vcd_out256 = 0;
538         h->priv.iso15693.use_afi = 0;   /* not all VICC support AFI */
539         h->priv.iso15693.afi = 0;
540
541         ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
542         if (ret < 0) {
543                 free_layer2_handle(h);
544                 return NULL;
545         }
546
547         return h;
548 }
549
550 static int
551 iso15693_fini(struct rfid_layer2_handle *handle)
552 {
553         free_layer2_handle(handle);
554         return 0;
555 }
556
557
558 const struct rfid_layer2 rfid_layer2_iso15693 = {
559         .id     = RFID_LAYER2_ISO15693,
560         .name   = "ISO 15693",
561         .fn     = {
562                 .init           = &iso15693_init,
563                 .open           = &iso15693_anticol,
564                 //.open         = &iso15693_select,
565                 //.transceive   = &iso15693_transceive,
566                 .close          = &iso15693_stay_quiet,
567                 .fini           = &iso15693_fini,
568                 .setopt         = &iso15693_setopt,
569                 .getopt         = &iso15693_getopt,
570         },
571 };
572