fix for omnikey 5321 v2
[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 //#define DEBUG_LIBRFID
22
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include <librfid/rfid.h>
29 #include <librfid/rfid_layer2.h>
30 #include <librfid/rfid_reader.h>
31 #include <librfid/rfid_layer2_iso15693.h>
32
33 /*struct iso15693_request_read {
34         struct iso15693_request head;
35         u_int64_t uid;
36         u_int8_t blocknum;
37 } __attribute__ ((packed));*/
38
39 struct iso15693_request_adressed {
40         struct iso15693_request head;
41         u_int64_t uid;
42 } __attribute__ ((packed));
43
44 struct iso15693_request_block_addressed {
45         struct iso15693_request head;
46         u_int64_t uid;
47         u_int8_t blocknum;
48         u_int8_t data[0];
49 } __attribute__ ((packed));
50
51 struct iso15693_request_block_selected {
52         struct iso15693_request head;
53         u_int8_t blocknum;
54         u_int8_t data[0];
55 } __attribute__ ((packed));
56
57 struct iso15693_err_resp {
58         struct iso15693_response head;
59         u_int8_t error;
60         unsigned char crc[2];
61 } __attribute__ ((packed));
62
63 struct iso15693_response_sec {
64         struct iso15693_response head;
65         u_int8_t block_sec;
66         u_int8_t data[];
67 } __attribute__ ((packed));
68
69 #define ISO15693_BLOCK_SIZE_MAX (256/8)
70 #define ISO15693_RESP_SIZE_MAX  (4+ISO15693_BLOCK_SIZE_MAX)
71
72 const unsigned int iso15693_timing[2][5] = {
73         [ISO15693_T_SLOW] = {
74                 [ISO15693_T1]   = 1216, /* max time after VCD EOF before VICC SOF */
75                 [ISO15693_T2]   = 1200, /* min time before VCD EOF after VICC response */
76                 [ISO15693_T3]   = 1502, /* min time after VCD EOF before next EOF if no VICC response */
77                 [ISO15693_T4]   = 1216, /* time after wich VICC transmits after VCD EOF */
78                 [ISO15693_T4_WRITE]=20000,      /* time after wich VICC transmits after VCD EOF */
79         },
80         [ISO15693_T_FAST] = {
81                 [ISO15693_T1]   = 304,  /* max time after VCD EOF before VICC SOF */
82                 [ISO15693_T2]   = 300,  /* min time before VCD EOF after VICC response */
83                 [ISO15693_T3]   = 602,  /* min time after VCD EOF before next EOF if no VICC response */
84                 [ISO15693_T4]   = 304,  /* time after wich VICC transmits after VCD EOF */
85                 [ISO15693_T4_WRITE]=20000,      /* time after wich VICC transmits after VCD EOF */
86         },
87 };
88
89 char *
90 iso15693_get_response_error_name(u_int8_t error){
91         switch (error){
92                 case RFID_15693_ERR_NOTSUPP:
93                         return "ERR_NOTSUPP";
94                 case RFID_15693_ERR_INVALID: /* command not recognized */
95                         return "ERR_INVALID";
96                 case RFID_15693_ERR_UNKNOWN: /* unknown error */
97                         return "ERR_UNKNOWN";
98         case RFID_15693_ERR_NOTSUPP_OPTION:
99             return "ERR_NotSuppOpt";
100                 case RFID_15693_ERR_BLOCK_NA: /* block not available */
101                         return "ERR_BLOCK_N";
102                 case RFID_15693_ERR_BLOCK_LOCKED:
103                         return "ERR_BLOCK_LOCKE";
104                 case RFID_15693_ERR_BLOCK_LOCKED_CH:
105                         return "ERR_BLOCK_LOCKED_CH";
106                 case RFID_15693_ERR_BLOCK_NOTPROG:
107                         return "ERR_BLOCK_NOTPROG";
108                 case RFID_15693_ERR_BLOCK_NOTLOCK:
109                         return "ERR_BLOCK_NOTLOCK";
110                 case 0xA0: /* until 0xDF*/
111                         return "Custom Command error Code";
112                 case 0xE0:
113                 default:
114                         return "Undef.";
115         }
116 }
117
118 static int iso15693_transceive(struct rfid_layer2_handle *handle,
119                                enum rfid_frametype frametype,
120                                const unsigned char *tx_buf, unsigned int tx_len,
121                                unsigned char *rx_buf, unsigned int *rx_len,
122                                u_int64_t timeout, unsigned int flags)
123 {
124         return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
125                                         tx_len, rx_buf, rx_len, timeout, flags);
126 }
127
128 /* Transmit an anticollission frame */
129 static int
130 iso15693_transceive_acf(struct rfid_layer2_handle *handle,
131                         const struct iso15693_anticol_cmd *acf,
132                         unsigned int acf_len,
133                         struct iso15693_anticol_resp *resp,
134                         unsigned int *rx_len, char *bit_of_col)
135 {
136         const struct rfid_reader *rdr = handle->rh->reader;
137         if (!rdr->iso15693.transceive_ac)
138                 return -1;
139         return rdr->iso15693.transceive_ac(handle->rh, acf, acf_len, resp, rx_len, bit_of_col);
140 }
141
142
143 int
144 iso15693_read_block(struct rfid_layer2_handle *handle,
145                     u_int8_t blocknr, u_int32_t *data, unsigned int len, 
146                     unsigned char *block_sec_out)
147 {
148         union {
149                 struct iso15693_request_block_selected sel;
150                 struct iso15693_request_block_addressed addr;
151         } tx_req;
152
153         int ret;
154         unsigned char *errstr;
155         unsigned int rx_len, tx_len,timeout;
156         u_int8_t resp[ISO15693_RESP_SIZE_MAX];
157         struct iso15693_err_resp *rx_err;
158         struct iso15693_response *rx_pkt;
159         struct iso15693_response_sec *rx_pkt_sec;
160
161         rx_pkt_sec = (struct iso15693_response_sec *)&resp[0];
162         rx_pkt = (struct iso15693_response *)&resp[0];
163         rx_err = (struct iso15693_err_resp *)&resp[0];
164
165         memset(&tx_req,0,sizeof(tx_req));
166
167         rx_len = sizeof(resp);
168
169         tx_req.sel.head.command = ISO15693_CMD_READ_BLOCK_SINGLE;
170
171         if (handle->priv.iso15693.vicc_fast){
172                 tx_req.sel.head.flags |= RFID_15693_F_RATE_HIGH;
173                 timeout=iso15693_timing[ISO15693_T_FAST][ISO15693_T4];
174         }else
175                 timeout=iso15693_timing[ISO15693_T_SLOW][ISO15693_T4];
176
177         if (handle->priv.iso15693.vicc_two_subc)
178                 tx_req.sel.head.flags |= RFID_15693_F_SUBC_TWO;
179
180         if (block_sec_out!=NULL)
181                 tx_req.sel.head.flags |= RFID_15693_F4_CUSTOM;
182
183         if (handle->priv.iso15693.state==RFID_15693_STATE_SELECTED) {
184                 tx_len = sizeof(struct iso15693_request_block_selected);
185                 tx_req.sel.blocknum = blocknr;
186                 tx_req.sel.head.flags |= RFID_15693_F4_SELECTED;
187         } else {
188                 tx_len = sizeof(struct iso15693_request_block_addressed);
189                 memcpy(&tx_req.addr.uid, handle->uid, ISO15693_UID_LEN);
190                 tx_req.addr.head.flags |= RFID_15693_F4_ADDRESS;
191                 tx_req.addr.blocknum = blocknr;
192         }
193
194         //DEBUGP("sizeof: addr: %d sel:%d\n",sizeof(struct iso15693_request_read_addressed),sizeof(struct iso15693_request_read_selected));
195         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
196
197         ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
198                                   tx_len, resp, &rx_len, timeout, 0);
199
200         if (ret==-ETIMEDOUT)
201                 errstr="(TIMEOUT)";
202         else if (ret==-EIO)
203                 errstr="(EIO)";
204         else
205                 errstr="";
206         DEBUGP("length: %d rx_len: %d ret: %d%s\n",len,rx_len,ret,errstr);
207
208         if (ret < 0)
209                 return ret;
210
211         if (rx_len > len+1)
212                 return -1;
213         DEBUGP("error_flag: %d", rx_pkt->flags&RFID_15693_RF_ERROR);
214         if (rx_pkt->flags & RFID_15693_RF_ERROR) {
215                 DEBUGPC(" -> error: %02x '%s'\n", rx_err->error,
216                         iso15693_get_response_error_name(rx_err->error));
217                 return -1;
218         } else if (block_sec_out != NULL) {
219                 DEBUGPC(" block_sec_stat: 0x%02x\n",rx_pkt_sec->block_sec);
220                 memcpy(data, rx_pkt_sec->data, rx_len-2);
221                 return rx_len-2;
222         } else {
223                 memcpy(data, rx_pkt->data, rx_len-1); /* FIXME rc-3 in case of CRC */
224                 return rx_len-1;
225         }
226 }
227
228 int
229 iso15693_write_block(struct rfid_layer2_handle *handle,
230                      u_int8_t blocknr, u_int32_t *data, unsigned int len)
231 {
232     int ret;
233         unsigned char *errstr;
234         unsigned int rx_len, tx_len,timeout;
235
236         union{
237                 struct iso15693_request_block_selected sel;
238                 struct iso15693_request_block_addressed addr;
239         u_int32_t buf[sizeof(struct iso15693_request_block_addressed)+ISO15693_BLOCK_SIZE_MAX];
240         } tx_req;
241
242         u_int8_t resp[ISO15693_RESP_SIZE_MAX];
243         struct iso15693_response *rx_pkt;
244         struct iso15693_err_resp *rx_err;
245
246         rx_pkt = (struct iso15693_response *)&resp[0];
247         rx_err = (struct iso15693_err_resp *)&resp[0];
248         rx_len = sizeof(resp);
249
250         if (len > ISO15693_BLOCK_SIZE_MAX)
251                 return -1;
252
253         //return -1;
254
255         memset(&tx_req,0,sizeof(tx_req));
256         tx_req.sel.head.command = ISO15693_CMD_WRITE_BLOCK_SINGLE;
257
258         if (handle->priv.iso15693.vicc_fast) {
259                 tx_req.sel.head.flags |= RFID_15693_F_RATE_HIGH;
260                 timeout = iso15693_timing[ISO15693_T_FAST][ISO15693_T4_WRITE];
261         } else
262                 timeout = iso15693_timing[ISO15693_T_SLOW][ISO15693_T4_WRITE];
263
264         if (handle->priv.iso15693.vicc_two_subc)
265                 tx_req.sel.head.flags |= RFID_15693_F_SUBC_TWO;
266
267         if (handle->priv.iso15693.state == RFID_15693_STATE_SELECTED) {
268                 tx_len=sizeof(struct iso15693_request_block_selected)+len;
269                 tx_req.sel.head.flags |= RFID_15693_F4_SELECTED;
270                 tx_req.sel.blocknum = blocknr;
271                 memcpy(&tx_req.sel.data,data,len);
272         } else {
273                 memcpy(&tx_req.addr.uid, handle->uid, ISO15693_UID_LEN);
274                 tx_len=sizeof(struct iso15693_request_block_addressed)+len;
275                 tx_req.addr.head.flags |= RFID_15693_F4_ADDRESS;
276                 tx_req.addr.blocknum = blocknr;
277                 memcpy(&tx_req.addr.data,data,len);
278         }
279
280         //DEBUGP("sizeof: addr: %d sel:%d\n",sizeof(struct iso15693_request_read_addressed),sizeof(struct iso15693_request_read_selected));
281         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
282
283         ret = iso15693_transceive(handle, RFID_15693_FRAME, (u_int8_t*)&tx_req,
284                                   tx_len, resp, &rx_len, timeout, 0);
285
286         if (ret == -ETIMEDOUT)
287                 errstr = "(TIMEOUT)";
288         else if (ret == -EIO)
289                 errstr = "(EIO)";
290         else
291                 errstr = "";
292         DEBUGP("length: %d rx_len: %d ret: %d%s\n",len,rx_len,ret,errstr);
293
294         if (ret < 0)
295                 return ret;
296
297         if (rx_len > len+1)
298                 return -1;
299         DEBUGP("error_flag: %d", rx_pkt->flags & RFID_15693_RF_ERROR);
300         if (rx_pkt->flags & RFID_15693_RF_ERROR) {
301                 DEBUGPC(" -> error: %02x '%s'\n", rx_err->error,
302                         iso15693_get_response_error_name(rx_err->error));
303                 return -1;
304         } else {
305                 //DEBUGPC(" block_sec_stat: 0x%02x\n",rx_pkt->data[0]);
306                 //memcpy(data, rx_pkt->data, rx_len-1); /* FIXME rc-3 in case of CRC */
307                 //return rx_len-1;
308         return 0;
309         }
310
311 }
312
313
314 #if 0
315
316 static int
317 iso15693_lock_block()
318 {
319 }
320
321 #endif
322
323 /* Helper function to build an ISO 15693 anti collision frame */
324 static int
325 iso15693_build_acf(u_int8_t *target, u_int8_t flags, u_int8_t afi,
326                    u_int8_t mask_len, u_int8_t *mask)
327 {
328         struct iso15693_request *req = (struct iso15693_request *) target;
329         int i = 0, j, mask_bytes;
330         u_int8_t byte=0;
331         void* mask_p;
332
333         req->flags = flags;
334         req->command = ISO15693_CMD_INVENTORY;
335         if (flags & RFID_15693_F5_AFI_PRES)
336                 req->data[i++] = afi;
337         req->data[i++] = mask_len;
338
339         mask_bytes = mask_len/8 + (mask_len%8)?1:0;
340         mask_p = &req->data[i];
341
342         for (j = 0; j < mask_bytes; j++)
343                 req->data[i++] = mask[j];
344
345         byte = 0xFF >> (8-mask_len%8);
346         req->data[i-1] &= byte;
347
348         DEBUGP("mask_len: %d mask_bytes: %d i: %d return: %d mask:%s\n",
349                 mask_len,mask_bytes,i,i + sizeof(*req),rfid_hexdump(mask_p,mask_bytes));
350         return i + sizeof(*req);
351 }
352
353 static int
354 iso15693_anticol(struct rfid_layer2_handle *handle)
355 {
356         int i, ret, mask_len;
357         int tx_len, rx_len;
358         int num_valid = 0;
359         union {
360                 struct iso15693_anticol_cmd_afi w_afi;
361                 struct iso15693_anticol_cmd no_afi;
362         } acf;
363
364         struct iso15693_anticol_resp resp;
365
366         u_int8_t boc;
367 #define MAX_SLOTS 16
368         int num_slots = MAX_SLOTS;
369
370         u_int8_t uuid_list[MAX_SLOTS][ISO15693_UID_LEN];
371         int uuid_list_valid[MAX_SLOTS];
372
373         u_int8_t flags;
374
375 #define MY_NONE 0
376 #define MY_COLL 1
377 #define MY_UUID 2
378
379         memset(uuid_list_valid, MY_NONE, sizeof(uuid_list_valid));
380         memset(uuid_list, 0, sizeof(uuid_list));
381
382         //memset(&acf, 0, sizeof(acf));
383
384         /* FIXME: we can't use multiple slots at this point, since the RC632
385          * with librfid on the host PC has too much latency between 'EOF pulse
386          * to mark start of next slot' and 'receive data' commands :( */
387
388         flags = RFID_15693_F_INV_TABLE_5;
389         if (handle->priv.iso15693.vicc_fast)
390                 flags |= RFID_15693_F_RATE_HIGH;
391         if (handle->priv.iso15693.vicc_two_subc)
392                 flags |= RFID_15693_F_SUBC_TWO;
393         if (handle->priv.iso15693.single_slot) {
394                 flags |= RFID_15693_F5_NSLOTS_1;
395                 num_slots = 1;
396         }
397         if (handle->priv.iso15693.use_afi)
398                 flags |= RFID_15693_F5_AFI_PRES;
399 #if 1
400         tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
401                                     handle->priv.iso15693.afi, 0, NULL);
402 #else
403         /*FIXME: testcode*/
404         u_int8_t uid[8]={0x1f, 0x1e, 0x95, 0x01, 0x00, 0x01, 0x04, 0xe0};
405         //u_int8_t uid[8]={0xe3, 0xe8, 0xf1, 0x01, 0x00, 0x00, 0x07, 0xe0};
406         tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
407                                     handle->priv.iso15693.afi, 2, uid);
408 #endif
409 start_of_ac_loop:
410         for (i = 0; i < num_slots; i++) {
411                 rx_len = sizeof(resp);
412                 memset(&resp, 0, rx_len);
413                 ret = iso15693_transceive_acf(handle, 
414                                               (struct iso15693_anticol_cmd *) &acf,
415                                               tx_len, &resp, &rx_len, &boc);
416
417                 if (ret == -ETIMEDOUT) {
418                         //DEBUGP("no answer from vicc in slot %d\n", i);
419                         DEBUGP("slot[%d]: timeout\n",i);
420                         uuid_list_valid[i] = MY_NONE;
421                 } else if (ret < 0) {
422                         DEBUGP("slot[%d]: ERROR ret: %d\n", i, ret);
423                         uuid_list_valid[i] = MY_NONE;
424                 } else {
425                         if (ret)
426                                 DEBUGP("iso15693_transceive_acf() ret: %d\n",ret);
427                         if (boc) {
428                                 DEBUGP("slot[%d]: Collision! bit:%d byte:%d,%d (UID bit:%d byte:%d,%d)\n",
429                                         i, boc,boc/8,boc%8,
430                                         boc-16,(boc-16)/8,(boc-16)%8);
431                                 DEBUGP("Slot[%d]: ret: %d DSFID: %02x UUID: %s\n", i, ret,
432                                         resp.dsfid, rfid_hexdump(resp.uuid, ISO15693_UID_LEN));
433
434                                 uuid_list_valid[i]=-boc;
435                                 memcpy(uuid_list[i], resp.uuid, ISO15693_UID_LEN);
436                         } else {
437                                 DEBUGP("Slot[%d]: ret: %d DSFID: %02x UUID: %s\n", i, ret,
438                                         resp.dsfid, rfid_hexdump(resp.uuid, ISO15693_UID_LEN));
439                                 uuid_list_valid[i] = MY_UUID;
440                                 memcpy(&uuid_list[i][0], resp.uuid, ISO15693_UID_LEN);
441
442                                 memcpy(handle->uid,resp.uuid, ISO15693_UID_LEN);
443                                 /* FIXME: move to init_iso15693 */
444                                 handle->uid_len = ISO15693_UID_LEN;
445                                 return 1;
446                         }
447                 }
448         }
449
450
451         for (i = 0; i < num_slots; i++) {
452                 if (uuid_list_valid[i] < 0) {
453                         boc=uuid_list_valid[i]*-1;
454                         if (boc>16){
455                                 boc=boc-16;
456                         }
457                         else
458                                 DEBUGP("slot[%d]:boc is smaller than 2 bytes (collision before uid)!!!!\n",i);
459
460                         if (boc<65){
461                                 tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
462                                     handle->priv.iso15693.afi, boc+1,  resp.uuid);
463                                 boc=0;
464                                 // FIXME: dont use goto
465                                 goto start_of_ac_loop;
466                         }else{
467                                 DEBUGP("slot[%d]:boc is bigger than 64 (uid size)(collision after uid)\n",i);
468                                 memcpy(handle->uid,uuid_list[i],ISO15693_UID_LEN);
469
470                                 /* FIXME: move to init_iso15693 */
471                                 handle->uid_len = ISO15693_UID_LEN;
472                                 return 1;
473                         }
474                 }
475         }
476 #if 0
477         for (i = 0; i < num_slots; i++) {
478                 if (uuid_list_valid[i] == MY_NONE) {
479                         DEBUGP("slot[%d]: timeout\n",i);
480                 } else if (uuid_list_valid[i] == MY_UUID) {
481                         DEBUGP("slot[%d]: VALID uuid: %s\n", i,
482                                 rfid_hexdump(uuid_list[i], ISO15693_UID_LEN));
483                         memcpy(handle->uid, uuid_list[i], ISO15693_UID_LEN);
484                         /* FIXME: move to init_iso15693 */
485                         handle->uid_len = ISO15693_UID_LEN;
486                         num_valid++;
487                 } else if (uuid_list_valid[i] < 0) {
488                                 if (boc>16){
489                                         boc=boc-16;
490                                 }
491                                 else
492                                         DEBUGP("boc is smaller than 2 bytes (collision before uid)!!!!\n");
493
494                                 uuid_list_valid[i] = -boc;
495                                 if (boc<65){
496                                         tx_len = iso15693_build_acf((u_int8_t *)&acf, flags,
497                                             handle->priv.iso15693.afi, boc+1,  resp.uuid);
498                                         boc=0;
499                                         // FIXME: dont use goto
500                                         goto start_of_ac_loop;
501                                 }else{
502                                         DEBUGP("boc is bigger than 64 (uid size)\n");
503                                         uuid_list_valid[i] = MY_UUID;
504                                 }
505                 }
506         }
507 #endif
508         if (num_valid == 0)
509                 return -1;
510
511         return num_valid;
512 }
513
514 int
515 iso15693_select(struct rfid_layer2_handle *l2h)
516 {
517         struct iso15693_request_adressed tx_req;
518         int ret;
519         unsigned int rx_len, tx_len, timeout;
520
521         struct {
522                 struct iso15693_response head;
523                 u_int8_t error;
524                 unsigned char crc[2];
525         } rx_buf;
526         rx_len = sizeof(rx_buf);
527
528         if (l2h->priv.iso15693.vicc_fast) {
529                 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
530                 timeout = iso15693_timing[ISO15693_T_FAST][ISO15693_T4];
531         } else
532                 timeout = iso15693_timing[ISO15693_T_SLOW][ISO15693_T4];
533
534         tx_req.head.command = ISO15693_CMD_SELECT;
535         tx_req.head.flags = RFID_15693_F4_ADDRESS;
536
537         if (l2h->priv.iso15693.vicc_fast)
538                 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
539         if (l2h->priv.iso15693.vicc_two_subc)
540                 tx_req.head.flags |= RFID_15693_F_SUBC_TWO;
541
542         memcpy(&tx_req.uid, l2h->uid, ISO15693_UID_LEN);
543         tx_len = sizeof(tx_req);
544
545         DEBUGP("tx_len=%u, rx_len=%u\n", tx_len,rx_len);
546
547         ret = iso15693_transceive(l2h, RFID_15693_FRAME, (u_int8_t*)&tx_req,
548                                   tx_len, (u_int8_t*)&rx_buf, &rx_len,timeout ,0);
549
550         DEBUGP("ret: %d%s, rx_len: %d, error_flag: %d", ret,
551                 (ret==-ETIMEDOUT)?"(TIMEOUT)":"", rx_len,
552                 rx_buf.head.flags&RFID_15693_RF_ERROR);
553         if (rx_buf.head.flags & RFID_15693_RF_ERROR) {
554                 DEBUGPC(" -> error: %02x '%s'\n", rx_buf.error,
555                         iso15693_get_response_error_name(rx_buf.error));
556                 return -1;
557         } else {
558                 DEBUGPC(" SELECTED\n");
559                 l2h->priv.iso15693.state = RFID_15693_STATE_SELECTED;
560                 return 0;
561         }
562 }
563
564 static int
565 iso15693_stay_quiet(struct rfid_layer2_handle *l2h)
566 {
567         struct iso15693_request_adressed tx_req;
568         int ret;
569         unsigned int rx_len, tx_len;
570
571         struct {
572                 struct iso15693_response head;
573                 u_int8_t error;
574                 unsigned char crc[2];
575         } rx_buf;
576         rx_len = sizeof(rx_buf);
577
578         tx_req.head.command = ISO15693_CMD_STAY_QUIET;
579
580         tx_req.head.flags = RFID_15693_F4_ADDRESS;
581         if (l2h->priv.iso15693.vicc_fast)
582                 tx_req.head.flags |= RFID_15693_F_RATE_HIGH;
583         if (l2h->priv.iso15693.vicc_two_subc)
584                 tx_req.head.flags |= RFID_15693_F_SUBC_TWO;
585         memcpy(&tx_req.uid, l2h->uid, ISO15693_UID_LEN);
586         tx_len = sizeof(tx_req);
587
588         DEBUGP("tx_len=%u", tx_len); DEBUGPC(" rx_len=%u\n",rx_len);
589
590         ret = iso15693_transceive(l2h, RFID_15693_FRAME, (u_int8_t*)&tx_req,
591                                   tx_len, (u_int8_t*)&rx_buf, &rx_len, 30,0);
592
593         l2h->priv.iso15693.state = RFID_15693_STATE_QUIET;
594
595         DEBUGP("ret: %d%s, error_flag: %d", ret,(ret==-ETIMEDOUT)?"(TIMEOUT)":"",
596                         rx_buf.head.flags&RFID_15693_RF_ERROR);
597         if (rx_buf.head.flags&RFID_15693_RF_ERROR)
598                 DEBUGPC(" -> error: %02x\n", rx_buf.error);
599         else
600                 DEBUGPC("\n");
601
602         return 0;
603 }
604
605 static int
606 iso15693_getopt(struct rfid_layer2_handle *handle,
607                 int optname, void *optval, unsigned int *optlen)
608 {
609         unsigned int *val = optval;
610         u_int8_t *val_u8 = optval;
611
612         if (!optlen || !optval || *optlen < sizeof(unsigned int))
613                 return -EINVAL;
614
615         *optlen = sizeof(unsigned int);
616
617         switch (optname) {
618         case RFID_OPT_15693_MOD_DEPTH:
619                 if (handle->priv.iso15693.vcd_ask100)
620                         *val = RFID_15693_MOD_100ASK;
621                 else
622                         *val = RFID_15693_MOD_10ASK;
623                 break;
624         case RFID_OPT_15693_VCD_CODING:
625                 if (handle->priv.iso15693.vcd_out256)
626                         *val = RFID_15693_VCD_CODING_1OUT256;
627                 else
628                         *val = RFID_15693_VCD_CODING_1OUT4;
629                 break;
630         case RFID_OPT_15693_VICC_SUBC:
631                 if (handle->priv.iso15693.vicc_two_subc)
632                         *val = RFID_15693_VICC_SUBC_DUAL;
633                 else
634                         *val = RFID_15693_VICC_SUBC_SINGLE;
635                 break;
636         case RFID_OPT_15693_VICC_SPEED:
637                 if (handle->priv.iso15693.vicc_fast)
638                         *val = RFID_15693_VICC_SPEED_FAST;
639                 else
640                         *val = RFID_15693_VICC_SPEED_SLOW;
641                 break;
642         case RFID_OPT_15693_VCD_SLOTS:
643                 if (handle->priv.iso15693.single_slot)
644                         *val = 1;
645                 else
646                         *val = 16;
647                 break;
648         case RFID_OPT_15693_USE_AFI:
649                 if (handle->priv.iso15693.use_afi)
650                         *val = 1;
651                 else
652                         *val = 0;
653                 break;
654         case RFID_OPT_15693_AFI:
655                 *val_u8 = handle->priv.iso15693.afi;
656                 *optlen = sizeof(u_int8_t);
657                 break;
658         default:
659                 return -EINVAL;
660                 break;
661         }
662
663         return 0;
664 }
665
666 static int
667 iso15693_setopt(struct rfid_layer2_handle *handle, int optname,
668                 const void *optval, unsigned int optlen)
669 {
670         unsigned int val;
671
672         if (optlen < sizeof(u_int8_t) || !optval)
673                 return -EINVAL;
674
675         if (optlen == sizeof(u_int8_t))
676                 val = *((u_int8_t *) optval);
677         if (optlen == sizeof(u_int16_t))
678                 val = *((u_int16_t *) optval);
679         if (optlen == sizeof(unsigned int))
680                 val = *((unsigned int *) optval);
681
682         switch (optname) {
683         case RFID_OPT_15693_MOD_DEPTH:
684                 switch (val) {
685                 case RFID_15693_MOD_10ASK:
686                         handle->priv.iso15693.vcd_ask100 = 0;
687                         break;
688                 case RFID_15693_MOD_100ASK:
689                         handle->priv.iso15693.vcd_ask100 = 1;
690                         break;
691                 default:
692                         return -EINVAL;
693                 }
694                 break;
695         case RFID_OPT_15693_VCD_CODING:
696                 switch (val) {
697                 case RFID_15693_VCD_CODING_1OUT256:
698                         handle->priv.iso15693.vcd_out256 = 1;
699                         break;
700                 case RFID_15693_VCD_CODING_1OUT4:
701                         handle->priv.iso15693.vcd_out256 = 0;
702                         break;
703                 default:
704                         return -EINVAL;
705                 }
706                 break;
707         case RFID_OPT_15693_VICC_SUBC:
708                 switch (val) {
709                 case RFID_15693_VICC_SUBC_SINGLE:
710                         handle->priv.iso15693.vicc_two_subc = 0;
711                         break;
712                 case RFID_15693_VICC_SUBC_DUAL:
713                         handle->priv.iso15693.vicc_two_subc = 1;
714                         break;
715                 default:
716                         return -EINVAL;
717                 }
718                 break;
719         case RFID_OPT_15693_VICC_SPEED:
720                 switch (val) {
721                 case RFID_15693_VICC_SPEED_SLOW:
722                         handle->priv.iso15693.vicc_fast = 0;
723                         break;
724                 case RFID_15693_VICC_SPEED_FAST:
725                         handle->priv.iso15693.vicc_fast = 1;
726                         break;
727                 default:
728                         return -EINVAL;
729                 }
730         case RFID_OPT_15693_VCD_SLOTS:
731                 switch (val) {
732                 case 16:
733                         handle->priv.iso15693.single_slot = 0;
734                         break;
735                 case 1:
736                         handle->priv.iso15693.single_slot = 1;
737                         break;
738                 default:
739                         return -EINVAL;
740                 }
741                 break;
742         case RFID_OPT_15693_USE_AFI:
743                 if (val)
744                         handle->priv.iso15693.use_afi = 1;
745                 else
746                         handle->priv.iso15693.use_afi = 0;
747                 break;
748         case RFID_OPT_15693_AFI:
749                 if (val > 0xff)
750                         return -EINVAL;
751                 handle->priv.iso15693.afi = val;
752                 break;
753         default:
754                 return -EINVAL;
755         }
756         return 0;
757 }
758
759 static int transceive_inventory(struct rfid_layer2_handle *l2h)
760 {
761         return -1;
762 }
763
764 static struct rfid_layer2_handle *
765 iso15693_init(struct rfid_reader_handle *rh)
766 {
767         int ret;
768         struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
769         if (!h)
770                 return NULL;
771
772         h->l2 = &rfid_layer2_iso15693;
773         h->rh = rh;
774         h->priv.iso15693.state = ISO15693_STATE_NONE;
775         h->priv.iso15693.vcd_ask100 = 1; /* 100ASK is easier to generate */
776         h->priv.iso15693.vicc_two_subc = 0;
777         h->priv.iso15693.vicc_fast = 1;
778         h->priv.iso15693.single_slot = 1;
779         h->priv.iso15693.vcd_out256 = 0;
780         h->priv.iso15693.use_afi = 0;   /* not all VICC support AFI */
781         h->priv.iso15693.afi = 0;
782
783         ret = h->rh->reader->init(h->rh, RFID_LAYER2_ISO15693);
784         if (ret < 0) {
785                 free_layer2_handle(h);
786                 return NULL;
787         }
788
789         return h;
790 }
791
792 static int
793 iso15693_fini(struct rfid_layer2_handle *handle)
794 {
795         free_layer2_handle(handle);
796         return 0;
797 }
798
799
800 const struct rfid_layer2 rfid_layer2_iso15693 = {
801         .id     = RFID_LAYER2_ISO15693,
802         .name   = "ISO 15693",
803         .fn     = {
804                 .init           = &iso15693_init,
805                 .open           = &iso15693_anticol,
806                 //.open         = &iso15693_select,
807                 //.transceive   = &iso15693_transceive,
808                 .close          = &iso15693_stay_quiet,
809                 .fini           = &iso15693_fini,
810                 .setopt         = &iso15693_setopt,
811                 .getopt         = &iso15693_getopt,
812         },
813 };
814