first beta - patch sent to librfid list
[rfid/librfid.git] / 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 /* Transcieve a 7-bit short frame */
35 static int
36 iso14443a_transcieve_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.transcieve_sf(handle->rh, cmd, atqa);
43 }
44
45 /* Transmit an anticollission bit frame */
46 static int
47 iso14443a_transcieve_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.transcieve_acf(handle->rh, acf, bit_of_col);
54 }
55
56 /* Transmit a regular frame */
57 static int 
58 iso14443a_transcieve(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->transcieve(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_transcieve_sf(handle, ISO14443A_SF_CMD_REQA, &atqa);
115         if (ret < 0) {
116                 h->state = ISO14443A_STATE_REQA_SENT;
117                 DEBUGP("error during transcieve_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         iso14443a_code_nvb_bits(&acf.nvb, 16);
144
145         ret = iso14443a_transcieve_acf(handle, &acf, &bit_of_col);
146         if (ret < 0)
147                 return ret;
148         DEBUGP("bit_of_col = %u\n", bit_of_col);
149         
150         while (bit_of_col != ISO14443A_BITOFCOL_NONE) {
151                 set_bit_in_field(&acf.uid_bits[0], bit_of_col-16);
152                 iso14443a_code_nvb_bits(&acf.nvb, bit_of_col);
153                 ret = iso14443a_transcieve_acf(handle, &acf, &bit_of_col);
154                 DEBUGP("bit_of_col = %u\n", bit_of_col);
155                 if (ret < 0)
156                         return ret;
157         }
158
159         iso14443a_code_nvb_bits(&acf.nvb, 7*8);
160         ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR,
161                                    (unsigned char *)&acf, 7, 
162                                    (unsigned char *) &sak, &rx_len,
163                                    TIMEOUT, 0);
164         if (ret < 0)
165                 return ret;
166
167         if (sak[0] & 0x04) {
168                 /* Cascade bit set, UID not complete */
169                 switch (acf.sel_code) {
170                 case ISO14443A_AC_SEL_CODE_CL1:
171                         /* cascading from CL1 to CL2 */
172                         if (acf.uid_bits[0] != 0x88) {
173                                 DEBUGP("Cascade bit set, but UID0 != 0x88\n");
174                                 return -1;
175                         }
176                         memcpy(&handle->uid[0], &acf.uid_bits[1], 3);
177                         acf.sel_code = ISO14443A_AC_SEL_CODE_CL2;
178                         h->level = ISO14443A_LEVEL_CL2;
179                         break;
180                 case ISO14443A_AC_SEL_CODE_CL2:
181                         /* cascading from CL2 to CL3 */
182                         memcpy(&handle->uid[3], &acf.uid_bits[1], 3);
183                         acf.sel_code = ISO14443A_AC_SEL_CODE_CL3;
184                         h->level = ISO14443A_LEVEL_CL3;
185                         break;
186                 default:
187                         DEBUGP("cannot cascade any further than CL3\n");
188                         h->state = ISO14443A_STATE_ERROR;
189                         return -1;
190                         break;
191                 }
192                 goto cascade;
193
194         } else {
195                 switch (acf.sel_code) {
196                 case ISO14443A_AC_SEL_CODE_CL1:
197                         /* single size UID (4 bytes) */
198                         memcpy(&handle->uid[0], &acf.uid_bits[0], 4);
199                         break;
200                 case ISO14443A_AC_SEL_CODE_CL2:
201                         /* double size UID (7 bytes) */
202                         memcpy(&handle->uid[3], &acf.uid_bits[0], 4);
203                         break;
204                 case ISO14443A_AC_SEL_CODE_CL3:
205                         /* triple size UID (10 bytes) */
206                         memcpy(&handle->uid[6], &acf.uid_bits[0], 4);
207                         break;
208                 }
209         }
210
211         h->level = ISO14443A_LEVEL_NONE;
212         h->state = ISO14443A_STATE_SELECTED;
213
214         {
215                 if (uid_size == 1)
216                         handle->uid_len = 4;
217                 else if (uid_size == 2)
218                         handle->uid_len = 7;
219                 else 
220                         handle->uid_len = 10;
221
222                 DEBUGP("UID %s\n", rfid_hexdump(handle->uid, handle->uid_len));
223         }
224
225         if (sak[0] & 0x20) {
226                 DEBUGP("we have a T=CL compliant PICC\n");
227                 h->tcl_capable = 1;
228         } else {
229                 DEBUGP("we have a T!=CL PICC\n");
230                 h->tcl_capable = 0;
231         }
232
233         return 0;
234 }
235
236 static int
237 iso14443a_hlta(struct rfid_layer2_handle *handle)
238 {
239         int ret;
240         unsigned char tx_buf[2] = { 0x50, 0x00 };
241         unsigned char rx_buf[10];
242         unsigned int rx_len = sizeof(rx_buf);
243
244         ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR,
245                                    tx_buf, sizeof(tx_buf),
246                                    rx_buf, &rx_len, 1000 /* 1ms */, 0);
247         if (ret < 0) {
248                 /* "error" case: we don't get somethng back from the card */
249                 return 0;
250         }
251         return -1;
252 }
253
254 static int
255 iso14443a_setopt(struct rfid_layer2_handle *handle, int optname,
256                  const void *optval, unsigned int optlen)
257 {
258         int ret = -EINVAL;
259         struct rfid_reader *rdr = handle->rh->reader;
260         unsigned int speed;
261
262         switch (optname) {
263         case RFID_OPT_14443A_SPEED_RX:
264                 if (!rdr->iso14443a.set_speed)
265                         return -ENOTSUP;
266                 speed = *(unsigned int *)optval;
267                 ret = rdr->iso14443a.set_speed(handle->rh, 0, speed);
268                 break;
269         case RFID_OPT_14443A_SPEED_TX:
270                 if (!rdr->iso14443a.set_speed)
271                         return -ENOTSUP;
272                 speed = *(unsigned int *)optval;
273                 ret = rdr->iso14443a.set_speed(handle->rh, 1, speed);
274                 break;
275         };
276
277         return ret;
278 }
279
280
281 static struct rfid_layer2_handle *
282 iso14443a_init(struct rfid_reader_handle *rh)
283 {
284         int ret;
285         struct rfid_layer2_handle *h = malloc(sizeof(*h));
286         if (!h)
287                 return NULL;
288
289         h->l2 = &rfid_layer2_iso14443a;
290         h->rh = rh;
291         h->priv.iso14443a.state = ISO14443A_STATE_NONE;
292         h->priv.iso14443a.level = ISO14443A_LEVEL_NONE;
293
294         ret = h->rh->reader->iso14443a.init(h->rh);
295         if (ret < 0) {
296                 free(h);
297                 return NULL;
298         }
299
300         return h;
301 }
302
303 static int
304 iso14443a_fini(struct rfid_layer2_handle *handle)
305 {
306         free(handle);
307         return 0;
308 }
309
310
311 struct rfid_layer2 rfid_layer2_iso14443a = {
312         .id     = RFID_LAYER2_ISO14443A,
313         .name   = "ISO 14443-3 A",
314         .fn     = {
315                 .init           = &iso14443a_init,
316                 .open           = &iso14443a_anticol,
317                 .transcieve     = &iso14443a_transcieve,
318                 .close          = &iso14443a_hlta,
319                 .fini           = &iso14443a_fini,
320                 .setopt         = &iso14443a_setopt,
321         },
322 };
323