add missing semicolon
[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 static int iso15693_transceive(struct rfid_layer2_handle *handle,
64                                enum rfid_frametype frametype,
65                                const unsigned char *tx_buf, unsigned int tx_len,
66                                unsigned char *rx_buf, unsigned int *rx_len,
67                                u_int64_t timeout, unsigned int flags)
68 {
69         return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
70                                         tx_len, rx_buf, rx_len, timeout, flags);
71 }
72
73 /* Transmit an anticollission frame */
74 static int
75 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
76                         const struct iso15693_anticol_cmd *acf,
77                         unsigned int acf_len,
78                         struct iso15693_anticol_resp *resp,
79                         unsigned int *rx_len, char *bit_of_col)
80 {
81         const struct rfid_reader *rdr = handle->rh->reader;
82         if (!rdr->iso15693.transceive_ac)
83                 return -1;
84         return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
85 }
86
87 #if 0
88
89 static int
90 iso15693_read_block(struct rfid_layer2_handle *handle,
91                     u_int8_t blocknr, u_int32_t *data)
92 {
93         int rc;
94         struct iso15693_request_read req;
95         u_int8_t resp[ISO15693_RESP_SIZE_MAX];
96
97         req.req.flags = 0;
98         req.command = ISO15693_CMD_READ_BLOCK_SINGLE;
99         memcpy(&req.uid, handle->..., ISO15693_UID_LEN);
100         req.blocknum = blocknr;
101
102         /* FIXME: fill CRC if required */
103
104         rc = iso15693_transceive(... &req, ...,  );
105
106         if (rc < 0)
107                 return rc;
108
109         memcpy(data, resp+1, rc-1); /* FIXME rc-3 in case of CRC */
110
111         return rc-1;
112 }
113
114 static int
115 iso15693_write_block()
116 {
117         struct iso16593_request_read *rreq;
118         u_int32_t buf[sizeof(req)+ISO15693_BLOCK_SIZE_MAX];
119
120         rreq = (struct iso15693_request_read *) req;
121
122         rreq->req.flags = ;
123         rreq->req.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
124         memcpy(rreq->uid, handle->, ISO15693_UID_LEN);
125         rreq->blocknum = blocknr;
126         memcpy(rreq->);
127
128 }
129
130 static int
131 iso15693_lock_block()
132 {
133 }
134
135 #endif
136
137 /* Helper function to build an ISO 15693 anti collision frame */
138 static int
139 iso15693_build_acf(u_int8_t *target, u_int8_t flags, u_int8_t afi,
140                    u_int8_t mask_len, u_int8_t *mask)
141 {
142         struct iso15693_request *req = (struct iso15693_request *) target;
143         int i = 0, j;
144
145         req->flags = flags;
146         req->command = ISO15693_CMD_INVENTORY;
147         if (flags & RFID_15693_F5_AFI_PRES)
148                 req->data[i++] = afi;
149         req->data[i++] = mask_len;
150
151         for (j = 0; j < mask_len; j++)
152                 req->data[i++] = mask[j];
153         
154         return i + sizeof(*req);
155 }
156
157 static int
158 iso15693_anticol(struct rfid_layer2_handle *handle)
159 {
160         int i, ret;
161         int tx_len, rx_len;
162         int num_valid = 0;
163         union {
164                 struct iso15693_anticol_cmd_afi w_afi;
165                 struct iso15693_anticol_cmd no_afi;
166         } acf;
167
168         struct iso15693_anticol_resp resp;
169                 
170         char boc;
171 #define MAX_SLOTS 16    
172         int num_slots = MAX_SLOTS;
173
174         u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
175         int uuid_list_valid[MAX_SLOTS];
176
177         u_int8_t flags;
178
179 #define MY_NONE 0
180 #define MY_COLL 1
181 #define MY_UUID 2
182
183         memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
184         memset(uuid_list, 0, sizeof(uuid_list));
185
186         //memset(&acf, 0, sizeof(acf));
187
188         /* FIXME: we can't use multiple slots at this point, since the RC632
189          * with librfid on the host PC has too much latency between 'EOF pulse
190          * to mark start of next slot' and 'receive data' commands :( */
191
192         flags = RFID_15693_F_INV_TABLE_5;
193         if (handle->priv.iso15693.vicc_fast)
194                 flags |= RFID_15693_F_RATE_HIGH;
195         if (handle->priv.iso15693.vicc_two_subc)
196                 flags |= RFID_15693_F_SUBC_TWO;
197         if (handle->priv.iso15693.single_slot) {
198                 flags |= RFID_15693_F5_NSLOTS_1;
199                 num_slots = 1;
200         }
201         if (handle->priv.iso15693.use_afi)
202                 flags |= RFID_15693_F5_AFI_PRES;
203
204         tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
205                                     handle->priv.iso15693.afi, 0, NULL);
206
207         for (i = 0; i < num_slots; i++) {
208                 rx_len = sizeof(resp);
209                 ret = iso15693_transceive_acf(handle, (u_int8_t *) &acf, tx_len, &resp, &rx_len, &boc);
210                 if (ret == -ETIMEDOUT) {
211                         DEBUGP("no answer from vicc in slot %d\n", i);
212                         uuid_list_valid[i] = MY_NONE;
213                 } else if (ret < 0) {
214                         DEBUGP("ERROR ret: %d, slot %d\n", ret, i);
215                         uuid_list_valid[i] = MY_NONE;
216                 } else {
217
218                         if (boc) {
219                                 DEBUGP("Collision during anticol. slot %d bit %d\n",
220                                         i, boc);
221                                 uuid_list_valid[i] = -boc;
222                                 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
223                         } else {
224                                 DEBUGP("Slot %d ret: %d UUID: %s\n", i, ret,
225                                         rfid_hexdump(resp.uuid, ISO15693_UID_LEN));
226                                 uuid_list_valid[i] = MY_UUID;
227                                 memcpy(&uuid_list[i][0], resp.uuid, ISO15693_UID_LEN);
228                         }
229                 }
230         }
231
232         for (i = 0; i < num_slots; i++) {
233                 if (uuid_list_valid[i] == MY_NONE) {
234                         DEBUGP("slot[%d]: timeout\n",i);
235                 } else if (uuid_list_valid[i] == MY_UUID) {
236                         DEBUGP("slot[%d]: VALID uuid: %s\n", i,
237                                 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
238                         num_valid++;
239                 } else if (uuid_list_valid[i] < 0) {
240                         DEBUGP("slot[%d]: collision(%d %d,%d) uuid: %s\n",
241                                 i,uuid_list_valid[i]*-1,
242                                 (uuid_list_valid[i]*-1)/8,
243                                 (uuid_list_valid[i]*-1)%8,
244                         rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
245                 }
246         }
247
248         if (num_valid == 0)
249                 return -1;
250
251         return num_valid;
252 }
253
254 static int
255 iso15693_select(struct rfid_layer2_handle *handle)
256 {
257         struct iso15693_request_adressed tx_req;
258         int ret;
259         unsigned int rx_len, tx_len;
260
261         struct {
262                 struct iso15693_response head;
263                 u_int8_t error;
264                 unsigned char crc[2];
265         } rx_buf;
266         rx_len = sizeof(rx_buf);
267
268         tx_req.head.command = ISO15693_CMD_SELECT;
269         tx_req.head.flags = RFID_15693_F4_ADDRESS | RFID_15693_F_SUBC_TWO ;
270         tx_req.uid = 0xE0070000020C1F18;
271         //req.uid = 0x181F0C02000007E0;
272         //req.uid = 0xe004010001950837;
273         //req.uid = 0x37089501000104e0;
274         tx_len = sizeof(tx_req);
275         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
276         ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
277                                   tx_len, (u_int8_t*)&rx_buf, &rx_len, 50,0);
278         DEBUGP("ret: %d, error_flag: %d error: %d\n", ret,
279                 rx_buf.head.flags&RFID_15693_RF_ERROR, 0);
280         return -1;
281 }
282
283 static int
284 iso15693_getopt(struct rfid_layer2_handle *handle,
285                 int optname, void *optval, unsigned int *optlen)
286 {
287         unsigned int *val = optval;
288         u_int8_t *val_u8 = optval;
289
290         if (!optlen || !optval || *optlen < sizeof(unsigned int))
291                 return -EINVAL;
292         
293         *optlen = sizeof(unsigned int);
294
295         switch (optname) {
296         case RFID_OPT_15693_MOD_DEPTH:
297                 if (handle->priv.iso15693.vcd_ask100)
298                         *val = RFID_15693_MOD_100ASK;
299                 else
300                         *val = RFID_15693_MOD_10ASK;
301                 break;
302         case RFID_OPT_15693_VCD_CODING:
303                 if (handle->priv.iso15693.vcd_out256)
304                         *val = RFID_15693_VCD_CODING_1OUT256;
305                 else
306                         *val = RFID_15693_VCD_CODING_1OUT4;
307                 break;
308         case RFID_OPT_15693_VICC_SUBC:
309                 if (handle->priv.iso15693.vicc_two_subc)
310                         *val = RFID_15693_VICC_SUBC_DUAL;
311                 else
312                         *val = RFID_15693_VICC_SUBC_SINGLE;
313                 break;
314         case RFID_OPT_15693_VICC_SPEED:
315                 if (handle->priv.iso15693.vicc_fast)
316                         *val = RFID_15693_VICC_SPEED_FAST;
317                 else
318                         *val = RFID_15693_VICC_SPEED_SLOW;
319                 break;
320         case RFID_OPT_15693_VCD_SLOTS:
321                 if (handle->priv.iso15693.single_slot)
322                         *val = 1;
323                 else
324                         *val = 16;
325                 break;
326         case RFID_OPT_15693_USE_AFI:
327                 if (handle->priv.iso15693.use_afi)
328                         *val = 1;
329                 else
330                         *val = 0;
331                 break;
332         case RFID_OPT_15693_AFI:
333                 *val_u8 = handle->priv.iso15693.afi;
334                 *optlen = sizeof(u_int8_t);
335                 break;
336         default:
337                 return -EINVAL;
338                 break;
339         }
340
341         return 0;
342 }
343
344 static int
345 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
346                 const void *optval, unsigned int optlen)
347 {
348         unsigned int val;
349         
350         if (optlen < sizeof(u_int8_t) || !optval)
351                 return -EINVAL;
352
353         if (optlen == sizeof(u_int8_t))
354                 val = *((u_int8_t *) optval);
355         if (optlen == sizeof(u_int16_t))
356                 val = *((u_int16_t *) optval);
357         if (optlen == sizeof(unsigned int))
358                 val = *((unsigned int *) optval);
359
360         switch (optname) {
361         case RFID_OPT_15693_MOD_DEPTH:
362                 switch (val) {
363                 case RFID_15693_MOD_10ASK:
364                         handle->priv.iso15693.vcd_ask100 = 0;
365                         break;
366                 case RFID_15693_MOD_100ASK:
367                         handle->priv.iso15693.vcd_ask100 = 1;
368                         break;
369                 default:
370                         return -EINVAL;
371                 }
372                 break;
373         case RFID_OPT_15693_VCD_CODING:
374                 switch (val) {
375                 case RFID_15693_VCD_CODING_1OUT256:
376                         handle->priv.iso15693.vcd_out256 = 1;
377                         break;
378                 case RFID_15693_VCD_CODING_1OUT4:
379                         handle->priv.iso15693.vcd_out256 = 0;
380                         break;
381                 default:
382                         return -EINVAL;
383                 }
384                 break;
385         case RFID_OPT_15693_VICC_SUBC:
386                 switch (val) {
387                 case RFID_15693_VICC_SUBC_SINGLE:
388                         handle->priv.iso15693.vicc_two_subc = 0;
389                         break;
390                 case RFID_15693_VICC_SUBC_DUAL:
391                         handle->priv.iso15693.vicc_two_subc = 1;
392                         break;
393                 default:
394                         return -EINVAL;
395                 }
396                 break;
397         case RFID_OPT_15693_VICC_SPEED:
398                 switch (val) {
399                 case RFID_15693_VICC_SPEED_SLOW:
400                         handle->priv.iso15693.vicc_fast = 0;
401                         break;
402                 case RFID_15693_VICC_SPEED_FAST:
403                         handle->priv.iso15693.vicc_fast = 1;
404                         break;
405                 default:
406                         return -EINVAL;
407                 }
408         case RFID_OPT_15693_VCD_SLOTS:
409                 switch (val) {
410                 case 16:
411                         handle->priv.iso15693.single_slot = 0;
412                         break;
413                 case 1:
414                         handle->priv.iso15693.single_slot = 1;
415                         break;
416                 default:
417                         return -EINVAL;
418                 }
419                 break;
420         case RFID_OPT_15693_USE_AFI:
421                 if (val)
422                         handle->priv.iso15693.use_afi = 1;
423                 else
424                         handle->priv.iso15693.use_afi = 0;
425                 break;
426         case RFID_OPT_15693_AFI:
427                 if (val > 0xff)
428                         return -EINVAL;
429                 handle->priv.iso15693.afi = val;
430                 break;
431         default:
432                 return -EINVAL;
433         }
434         return 0;
435 }
436
437 static int transceive_inventory(struct rfid_layer2_handle *l2h)
438 {
439         return -1;
440 }
441
442 static struct rfid_layer2_handle *
443 iso15693_init(struct rfid_reader_handle *rh)
444 {
445         int ret;
446         struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
447         if (!h)
448                 return NULL;
449
450         h->l2 = &rfid_layer2_iso15693;
451         h->rh = rh;
452         h->priv.iso15693.state = ISO15693_STATE_NONE;
453         h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
454         h->priv.iso15693.vicc_two_subc = 0;
455         h->priv.iso15693.vicc_fast = 1;
456         h->priv.iso15693.single_slot = 1;
457         h->priv.iso15693.vcd_out256 = 0;
458         h->priv.iso15693.use_afi = 0;   /* not all VICC support AFI */
459         h->priv.iso15693.afi = 0;
460
461         ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
462         if (ret < 0) {
463                 free_layer2_handle(h);
464                 return NULL;
465         }
466
467         return h;
468 }
469
470 static int
471 iso15693_fini(struct rfid_layer2_handle *handle)
472 {
473         free_layer2_handle(handle);
474         return 0;
475 }
476
477
478 const struct rfid_layer2 rfid_layer2_iso15693 = {
479         .id     = RFID_LAYER2_ISO15693,
480         .name   = "ISO 15693",
481         .fn     = {
482                 .init           = &iso15693_init,
483                 .open           = &iso15693_anticol,
484                 //.open         = &iso15693_select,
485                 //.transceive   = &iso15693_transceive,
486                 //.close                = &iso14443a_hlta,
487                 .fini           = &iso15693_fini,
488                 .setopt         = &iso15693_setopt,
489                 .getopt         = &iso15693_getopt,
490         },
491 };
492