re-initialize sak rx_len for each cascade level (Henryk Ploetz)
[librfid] / src / rfid_layer2_iso14443a.c
1 /* ISO 14443-3 A anticollision implementation
2  *
3  * (C) 2005 by Harald Welte <laforge@gnumonks.org>
4  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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_iso14443a.h>
31
32 #define TIMEOUT 1236
33
34 /* Transceive a 7-bit short frame */
35 static int
36 iso14443a_transceive_sf(struct rfid_layer2_handle *handle,
37                          unsigned char cmd,
38                          struct iso14443a_atqa *atqa)
39 {
40         struct rfid_reader *rdr = handle->rh->reader;
41
42         return rdr->iso14443a.transceive_sf(handle->rh, cmd, atqa);
43 }
44
45 /* Transmit an anticollission bit frame */
46 static int
47 iso14443a_transceive_acf(struct rfid_layer2_handle *handle,
48                          struct iso14443a_anticol_cmd *acf,
49                          unsigned int *bit_of_col)
50 {
51         struct rfid_reader *rdr = handle->rh->reader;
52
53         return rdr->iso14443a.transceive_acf(handle->rh, acf, bit_of_col);
54 }
55
56 /* Transmit a regular frame */
57 static int 
58 iso14443a_transceive(struct rfid_layer2_handle *handle,
59                      enum rfid_frametype frametype, 
60                         const unsigned char *tx_buf, unsigned int tx_len,
61                         unsigned char *rx_buf, unsigned int *rx_len,
62                         u_int64_t timeout, unsigned int flags)
63 {
64         return handle->rh->reader->transceive(handle->rh, frametype, tx_buf,
65                                         tx_len, rx_buf, rx_len, timeout, flags);
66 }
67
68 static int 
69 iso14443a_code_nvb_bits(unsigned char *nvb, unsigned int bits)
70 {
71         unsigned int byte_count = bits / 8;
72         unsigned int bit_count = bits % 8;
73
74         if (byte_count < 2 || byte_count > 7)
75                 return -1;
76
77         *nvb = ((byte_count & 0xf) << 4) | bit_count;
78
79         return 0;
80 }
81
82 /* first bit is '1', second bit '2' */
83 static void
84 set_bit_in_field(unsigned char *bitfield, unsigned int bit)
85 {
86         unsigned int byte_count = bit / 8;
87         unsigned int bit_count = bit % 8;
88
89         DEBUGP("bitfield=%p, byte_count=%u, bit_count=%u\n",
90                         bitfield, byte_count, bit_count);
91         DEBUGP("%p = 0x%02x\n", (bitfield+byte_count), *(bitfield+byte_count));
92         *(bitfield+byte_count) |= 1 << (bit_count-1);
93         DEBUGP("%p = 0x%02x\n", (bitfield+byte_count), *(bitfield+byte_count));
94 }
95
96 static int
97 iso14443a_anticol(struct rfid_layer2_handle *handle)
98 {
99         int ret;
100         unsigned int uid_size;
101         struct iso14443a_handle *h = &handle->priv.iso14443a;
102         struct iso14443a_atqa atqa;
103         struct iso14443a_anticol_cmd acf;
104         unsigned int bit_of_col;
105         unsigned char sak[3];
106         unsigned int rx_len = sizeof(sak);
107         char *aqptr = (char *) &atqa;
108
109         memset(handle->uid, 0, sizeof(handle->uid));
110         memset(sak, 0, sizeof(sak));
111         memset(&atqa, 0, sizeof(atqa));
112         memset(&acf, 0, sizeof(acf));
113
114         ret = iso14443a_transceive_sf(handle, ISO14443A_SF_CMD_REQA, &atqa);
115         if (ret < 0) {
116                 h->state = ISO14443A_STATE_REQA_SENT;
117                 DEBUGP("error during transceive_sf: %d\n", ret);
118                 return ret;
119         }
120         h->state = ISO14443A_STATE_ATQA_RCVD;
121
122         DEBUGP("ATQA: 0x%02x 0x%02x\n", *aqptr, *(aqptr+1));
123
124         if (!atqa.bf_anticol) {
125                 h->state = ISO14443A_STATE_NO_BITFRAME_ANTICOL;
126                 DEBUGP("no bitframe anticollission bits set, aborting\n");
127                 return -1;
128         }
129
130         if (atqa.uid_size == 2 || atqa.uid_size == 3)
131                 uid_size = 3;
132         else if (atqa.uid_size == 1)
133                 uid_size = 2;
134         else
135                 uid_size = 1;
136         
137         acf.sel_code = ISO14443A_AC_SEL_CODE_CL1;
138
139         h->state = ISO14443A_STATE_ANTICOL_RUNNING;
140         h->level = ISO14443A_LEVEL_CL1;
141
142 cascade:
143         rx_len = sizeof(sak);
144         iso14443a_code_nvb_bits(&acf.nvb, 16);
145
146         ret = iso14443a_transceive_acf(handle, &acf, &bit_of_col);
147         if (ret < 0)
148                 return ret;
149         DEBUGP("bit_of_col = %u\n", bit_of_col);
150         
151         while (bit_of_col != ISO14443A_BITOFCOL_NONE) {
152                 set_bit_in_field(&acf.uid_bits[0], bit_of_col-16);
153                 iso14443a_code_nvb_bits(&acf.nvb, bit_of_col);
154                 ret = iso14443a_transceive_acf(handle, &acf, &bit_of_col);
155                 DEBUGP("bit_of_col = %u\n", bit_of_col);
156                 if (ret < 0)
157                         return ret;
158         }
159
160         iso14443a_code_nvb_bits(&acf.nvb, 7*8);
161         ret = iso14443a_transceive(handle, RFID_14443A_FRAME_REGULAR,
162                                    (unsigned char *)&acf, 7, 
163                                    (unsigned char *) &sak, &rx_len,
164                                    TIMEOUT, 0);
165         if (ret < 0)
166                 return ret;
167
168         if (sak[0] & 0x04) {
169                 /* Cascade bit set, UID not complete */
170                 switch (acf.sel_code) {
171                 case ISO14443A_AC_SEL_CODE_CL1:
172                         /* cascading from CL1 to CL2 */
173                         if (acf.uid_bits[0] != 0x88) {
174                                 DEBUGP("Cascade bit set, but UID0 != 0x88\n");
175                                 return -1;
176                         }
177                         memcpy(&handle->uid[0], &acf.uid_bits[1], 3);
178                         acf.sel_code = ISO14443A_AC_SEL_CODE_CL2;
179                         h->level = ISO14443A_LEVEL_CL2;
180                         break;
181                 case ISO14443A_AC_SEL_CODE_CL2:
182                         /* cascading from CL2 to CL3 */
183                         memcpy(&handle->uid[3], &acf.uid_bits[1], 3);
184                         acf.sel_code = ISO14443A_AC_SEL_CODE_CL3;
185                         h->level = ISO14443A_LEVEL_CL3;
186                         break;
187                 default:
188                         DEBUGP("cannot cascade any further than CL3\n");
189                         h->state = ISO14443A_STATE_ERROR;
190                         return -1;
191                         break;
192                 }
193                 goto cascade;
194
195         } else {
196                 switch (acf.sel_code) {
197                 case ISO14443A_AC_SEL_CODE_CL1:
198                         /* single size UID (4 bytes) */
199                         memcpy(&handle->uid[0], &acf.uid_bits[0], 4);
200                         break;
201                 case ISO14443A_AC_SEL_CODE_CL2:
202                         /* double size UID (7 bytes) */
203                         memcpy(&handle->uid[3], &acf.uid_bits[0], 4);
204                         break;
205                 case ISO14443A_AC_SEL_CODE_CL3:
206                         /* triple size UID (10 bytes) */
207                         memcpy(&handle->uid[6], &acf.uid_bits[0], 4);
208                         break;
209                 }
210         }
211
212         h->level = ISO14443A_LEVEL_NONE;
213         h->state = ISO14443A_STATE_SELECTED;
214
215         {
216                 if (uid_size == 1)
217                         handle->uid_len = 4;
218                 else if (uid_size == 2)
219                         handle->uid_len = 7;
220                 else 
221                         handle->uid_len = 10;
222
223                 DEBUGP("UID %s\n", rfid_hexdump(handle->uid, handle->uid_len));
224         }
225
226         if (sak[0] & 0x20) {
227                 DEBUGP("we have a T=CL compliant PICC\n");
228                 h->tcl_capable = 1;
229         } else {
230                 DEBUGP("we have a T!=CL PICC\n");
231                 h->tcl_capable = 0;
232         }
233
234         return 0;
235 }
236
237 static int
238 iso14443a_hlta(struct rfid_layer2_handle *handle)
239 {
240         int ret;
241         unsigned char tx_buf[2] = { 0x50, 0x00 };
242         unsigned char rx_buf[10];
243         unsigned int rx_len = sizeof(rx_buf);
244
245         ret = iso14443a_transceive(handle, RFID_14443A_FRAME_REGULAR,
246                                    tx_buf, sizeof(tx_buf),
247                                    rx_buf, &rx_len, 1000 /* 1ms */, 0);
248         if (ret < 0) {
249                 /* "error" case: we don't get somethng back from the card */
250                 return 0;
251         }
252         return -1;
253 }
254
255 static int
256 iso14443a_setopt(struct rfid_layer2_handle *handle, int optname,
257                  const void *optval, unsigned int optlen)
258 {
259         int ret = -EINVAL;
260         struct rfid_reader *rdr = handle->rh->reader;
261         unsigned int speed;
262
263         switch (optname) {
264         case RFID_OPT_14443A_SPEED_RX:
265                 if (!rdr->iso14443a.set_speed)
266                         return -ENOTSUP;
267                 speed = *(unsigned int *)optval;
268                 ret = rdr->iso14443a.set_speed(handle->rh, 0, speed);
269                 break;
270         case RFID_OPT_14443A_SPEED_TX:
271                 if (!rdr->iso14443a.set_speed)
272                         return -ENOTSUP;
273                 speed = *(unsigned int *)optval;
274                 ret = rdr->iso14443a.set_speed(handle->rh, 1, speed);
275                 break;
276         };
277
278         return ret;
279 }
280
281
282 static struct rfid_layer2_handle *
283 iso14443a_init(struct rfid_reader_handle *rh)
284 {
285         int ret;
286         struct rfid_layer2_handle *h = malloc(sizeof(*h));
287         if (!h)
288                 return NULL;
289
290         h->l2 = &rfid_layer2_iso14443a;
291         h->rh = rh;
292         h->priv.iso14443a.state = ISO14443A_STATE_NONE;
293         h->priv.iso14443a.level = ISO14443A_LEVEL_NONE;
294
295         ret = h->rh->reader->iso14443a.init(h->rh);
296         if (ret < 0) {
297                 free(h);
298                 return NULL;
299         }
300
301         return h;
302 }
303
304 static int
305 iso14443a_fini(struct rfid_layer2_handle *handle)
306 {
307         free(handle);
308         return 0;
309 }
310
311
312 struct rfid_layer2 rfid_layer2_iso14443a = {
313         .id     = RFID_LAYER2_ISO14443A,
314         .name   = "ISO 14443-3 A",
315         .fn     = {
316                 .init           = &iso14443a_init,
317                 .open           = &iso14443a_anticol,
318                 .transceive     = &iso14443a_transceive,
319                 .close          = &iso14443a_hlta,
320                 .fini           = &iso14443a_fini,
321                 .setopt         = &iso14443a_setopt,
322         },
323 };
324