add ISO15693 stay_quiet (close) support (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         char 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 UUID: %s\n", i, ret,
252                                         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                         handle->uid_len = ISO15693_UID_LEN;
267                         num_valid++;
268                 } else if (uuid_list_valid[i] < 0) {
269                         DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
270                                 i,uuid_list_valid[i]*-1,
271                                 (uuid_list_valid[i]*-1)/8,
272                                 (uuid_list_valid[i]*-1)%8,
273                         rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
274                 }
275         }
276
277         if (num_valid == 0)
278                 return -1;
279
280         return num_valid;
281 }
282
283 static int
284 iso15693_select(struct rfid_layer2_handle *handle)
285 {
286         struct iso15693_request_adressed tx_req;
287         int ret;
288         unsigned int rx_len, tx_len;
289
290         struct {
291                 struct iso15693_response head;
292                 u_int8_t error;
293                 unsigned char crc[2];
294         } rx_buf;
295         rx_len = sizeof(rx_buf);
296
297         tx_req.head.command = ISO15693_CMD_SELECT;
298         tx_req.head.flags = RFID_15693_F4_ADDRESS | RFID_15693_F_SUBC_TWO ;
299         tx_req.uid = 0xE0070000020C1F18;
300         //req.uid = 0x181F0C02000007E0;
301         //req.uid = 0xe004010001950837;
302         //req.uid = 0x37089501000104e0;
303         tx_len = sizeof(tx_req);
304         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
305         ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
306                                   tx_len, (u_int8_t*)&rx_buf, &rx_len, 50,0);
307         DEBUGP("ret: %d, error_flag: %d -> error: %02x\n", ret,
308                 rx_buf.head.flags&RFID_15693_RF_ERROR, rx_buf.error);
309         return -1;
310 }
311
312 static int
313 iso15693_stay_quiet(struct rfid_layer2_handle *l2h)
314 {
315         struct iso15693_request_adressed tx_req;
316         int ret;
317         unsigned int rx_len, tx_len;
318
319         struct {
320                 struct iso15693_response head;
321                 u_int8_t error;
322                 unsigned char crc[2];
323         } rx_buf;
324         rx_len = sizeof(rx_buf);
325
326         tx_req.head.command = ISO15693_CMD_STAY_QUIET;
327
328         tx_req.head.flags = RFID_15693_F4_ADDRESS;
329         if (l2h->priv.iso15693.vicc_fast)
330                 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
331         if (l2h->priv.iso15693.vicc_two_subc)
332                 tx_req.head.flags |= RFID_15693_F_SUBC_TWO;
333         memcpy(&tx_req.uid, l2h->uid, ISO15693_UID_LEN);
334         tx_len = sizeof(tx_req);
335
336         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
337
338         ret = iso15693_transceive(l2h, RFID_15693_FRAME, (u_int8_t*)&tx_req,
339                                   tx_len, (u_int8_t*)&rx_buf, &rx_len, 10,0);
340
341         l2h->priv.iso15693.state = RFID_15693_STATE_QUIET;
342
343         DEBUGP("ret: %d%s, error_flag: %d", ret,(ret==-ETIMEDOUT)?"(TIMEOUT)":"",
344                         rx_buf.head.flags&RFID_15693_RF_ERROR);
345         if (rx_buf.head.flags&RFID_15693_RF_ERROR)
346                 DEBUGPC(" -> error: %02x\n", rx_buf.error);
347         else
348                 DEBUGPC("\n");
349
350         return 0;
351 }
352
353 static int
354 iso15693_getopt(struct rfid_layer2_handle *handle,
355                 int optname, void *optval, unsigned int *optlen)
356 {
357         unsigned int *val = optval;
358         u_int8_t *val_u8 = optval;
359
360         if (!optlen || !optval || *optlen < sizeof(unsigned int))
361                 return -EINVAL;
362         
363         *optlen = sizeof(unsigned int);
364
365         switch (optname) {
366         case RFID_OPT_15693_MOD_DEPTH:
367                 if (handle->priv.iso15693.vcd_ask100)
368                         *val = RFID_15693_MOD_100ASK;
369                 else
370                         *val = RFID_15693_MOD_10ASK;
371                 break;
372         case RFID_OPT_15693_VCD_CODING:
373                 if (handle->priv.iso15693.vcd_out256)
374                         *val = RFID_15693_VCD_CODING_1OUT256;
375                 else
376                         *val = RFID_15693_VCD_CODING_1OUT4;
377                 break;
378         case RFID_OPT_15693_VICC_SUBC:
379                 if (handle->priv.iso15693.vicc_two_subc)
380                         *val = RFID_15693_VICC_SUBC_DUAL;
381                 else
382                         *val = RFID_15693_VICC_SUBC_SINGLE;
383                 break;
384         case RFID_OPT_15693_VICC_SPEED:
385                 if (handle->priv.iso15693.vicc_fast)
386                         *val = RFID_15693_VICC_SPEED_FAST;
387                 else
388                         *val = RFID_15693_VICC_SPEED_SLOW;
389                 break;
390         case RFID_OPT_15693_VCD_SLOTS:
391                 if (handle->priv.iso15693.single_slot)
392                         *val = 1;
393                 else
394                         *val = 16;
395                 break;
396         case RFID_OPT_15693_USE_AFI:
397                 if (handle->priv.iso15693.use_afi)
398                         *val = 1;
399                 else
400                         *val = 0;
401                 break;
402         case RFID_OPT_15693_AFI:
403                 *val_u8 = handle->priv.iso15693.afi;
404                 *optlen = sizeof(u_int8_t);
405                 break;
406         default:
407                 return -EINVAL;
408                 break;
409         }
410
411         return 0;
412 }
413
414 static int
415 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
416                 const void *optval, unsigned int optlen)
417 {
418         unsigned int val;
419         
420         if (optlen < sizeof(u_int8_t) || !optval)
421                 return -EINVAL;
422
423         if (optlen == sizeof(u_int8_t))
424                 val = *((u_int8_t *) optval);
425         if (optlen == sizeof(u_int16_t))
426                 val = *((u_int16_t *) optval);
427         if (optlen == sizeof(unsigned int))
428                 val = *((unsigned int *) optval);
429
430         switch (optname) {
431         case RFID_OPT_15693_MOD_DEPTH:
432                 switch (val) {
433                 case RFID_15693_MOD_10ASK:
434                         handle->priv.iso15693.vcd_ask100 = 0;
435                         break;
436                 case RFID_15693_MOD_100ASK:
437                         handle->priv.iso15693.vcd_ask100 = 1;
438                         break;
439                 default:
440                         return -EINVAL;
441                 }
442                 break;
443         case RFID_OPT_15693_VCD_CODING:
444                 switch (val) {
445                 case RFID_15693_VCD_CODING_1OUT256:
446                         handle->priv.iso15693.vcd_out256 = 1;
447                         break;
448                 case RFID_15693_VCD_CODING_1OUT4:
449                         handle->priv.iso15693.vcd_out256 = 0;
450                         break;
451                 default:
452                         return -EINVAL;
453                 }
454                 break;
455         case RFID_OPT_15693_VICC_SUBC:
456                 switch (val) {
457                 case RFID_15693_VICC_SUBC_SINGLE:
458                         handle->priv.iso15693.vicc_two_subc = 0;
459                         break;
460                 case RFID_15693_VICC_SUBC_DUAL:
461                         handle->priv.iso15693.vicc_two_subc = 1;
462                         break;
463                 default:
464                         return -EINVAL;
465                 }
466                 break;
467         case RFID_OPT_15693_VICC_SPEED:
468                 switch (val) {
469                 case RFID_15693_VICC_SPEED_SLOW:
470                         handle->priv.iso15693.vicc_fast = 0;
471                         break;
472                 case RFID_15693_VICC_SPEED_FAST:
473                         handle->priv.iso15693.vicc_fast = 1;
474                         break;
475                 default:
476                         return -EINVAL;
477                 }
478         case RFID_OPT_15693_VCD_SLOTS:
479                 switch (val) {
480                 case 16:
481                         handle->priv.iso15693.single_slot = 0;
482                         break;
483                 case 1:
484                         handle->priv.iso15693.single_slot = 1;
485                         break;
486                 default:
487                         return -EINVAL;
488                 }
489                 break;
490         case RFID_OPT_15693_USE_AFI:
491                 if (val)
492                         handle->priv.iso15693.use_afi = 1;
493                 else
494                         handle->priv.iso15693.use_afi = 0;
495                 break;
496         case RFID_OPT_15693_AFI:
497                 if (val > 0xff)
498                         return -EINVAL;
499                 handle->priv.iso15693.afi = val;
500                 break;
501         default:
502                 return -EINVAL;
503         }
504         return 0;
505 }
506
507 static int transceive_inventory(struct rfid_layer2_handle *l2h)
508 {
509         return -1;
510 }
511
512 static struct rfid_layer2_handle *
513 iso15693_init(struct rfid_reader_handle *rh)
514 {
515         int ret;
516         struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
517         if (!h)
518                 return NULL;
519
520         h->l2 = &rfid_layer2_iso15693;
521         h->rh = rh;
522         h->priv.iso15693.state = ISO15693_STATE_NONE;
523         h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
524         h->priv.iso15693.vicc_two_subc = 0;
525         h->priv.iso15693.vicc_fast = 1;
526         h->priv.iso15693.single_slot = 1;
527         h->priv.iso15693.vcd_out256 = 0;
528         h->priv.iso15693.use_afi = 0;   /* not all VICC support AFI */
529         h->priv.iso15693.afi = 0;
530
531         ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
532         if (ret < 0) {
533                 free_layer2_handle(h);
534                 return NULL;
535         }
536
537         return h;
538 }
539
540 static int
541 iso15693_fini(struct rfid_layer2_handle *handle)
542 {
543         free_layer2_handle(handle);
544         return 0;
545 }
546
547
548 const struct rfid_layer2 rfid_layer2_iso15693 = {
549         .id     = RFID_LAYER2_ISO15693,
550         .name   = "ISO 15693",
551         .fn     = {
552                 .init           = &iso15693_init,
553                 .open           = &iso15693_anticol,
554                 //.open         = &iso15693_select,
555                 //.transceive   = &iso15693_transceive,
556                 .close          = &iso15693_stay_quiet,
557                 .fini           = &iso15693_fini,
558                 .setopt         = &iso15693_setopt,
559                 .getopt         = &iso15693_getopt,
560         },
561 };
562