As Juergen Heinzl pointed out, much of my original fwi calculations evaluated
[librfid] / rfid_layer2_iso14443b.c
1 /* ISO 14443-3 B 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
26 #include <rfid/rfid.h>
27 #include <rfid/rfid_layer2.h>
28 #include <rfid/rfid_reader.h>
29 #include <rfid/rfid_layer2_iso14443b.h>
30
31 #include "rfid_iso14443_common.h"
32
33 #define ATQB_TIMEOUT    ((256*10e6/ISO14443_FREQ_SUBCARRIER)            \
34                          +(200*10e6/ISO14443_FREQ_SUBCARRIER))
35
36 static inline int
37 fwi_to_fwt(struct rfid_layer2_handle *h, unsigned int *fwt, unsigned int fwi)
38 {
39         unsigned int multiplier;
40
41         /* 15 is RFU */
42         if (fwi > 14)
43                 return -1;
44
45         /* According to ISO 14443-3:200(E), Chapter 7.9.4.3, the forumala is
46          * (256 * 16 / fC) * 2^fwi We avoid floating point computations by
47          * shifting everything into the microsecond range.  In integer
48          * calculations 1000000*256*16/13560000 evaluates to 302 (instead of
49          * 302.064897), which provides sufficient precision, IMHO.  The max
50          * result is 302 * 16384 (4947968), which fits well within the 31/32
51          * bit range of an integer */
52
53         multiplier = 1 << fwi;          /* 2 to the power of fwi */
54
55         return (1000000 * 256 * 16 / h->rh->ah->asic->fc) * multiplier
56 }
57
58 static int
59 parse_atqb(struct rfid_layer2_handle *h, struct iso14443b_atqb *atqb)
60 {
61         int ret;
62
63         if (atqb->fifty != 0x50)
64                 return -1; 
65
66         if (atqb->protocol_info.fo & 0x01)
67                 h->priv.iso14443b.flags |= ISO14443B_CID_SUPPORTED;
68         if (atqb->protocol_info.fo & 0x02)
69                 h->priv.iso14443b.flags |= ISO14443B_NAD_SUPPORTED;
70
71         ret = fwi_to_fwt(h, &h->priv.iso14443b.fwt, atqb->protocol_info.fwi);
72         if (ret < 0) {
73                 DEBUGP("invalid fwi %u\n", atqb->protocol_info.fwi);
74                 return ret;
75         }
76
77         if (atqb->protocol_info.protocol_type == 0x1) {
78                 DEBUGP("we have a T=CL compliant PICC\n");
79                 h->priv.iso14443b.tcl_capable = 1;
80         } else {
81                 DEBUGP("we have a T!=CL PICC\n");
82                 h->priv.iso14443b.tcl_capable = 0;
83         }
84
85         iso14443_fsdi_to_fsd(&h->priv.iso14443b.fsc, 
86                              atqb->protocol_info.max_frame_size);
87
88         /* FIXME: speed capability */
89
90         memcpy(h->priv.iso14443b.pupi, atqb->pupi,
91                 sizeof(h->priv.iso14443b.pupi));
92
93         return 0;
94 }
95
96 static int
97 send_reqb(struct rfid_layer2_handle *h, unsigned char afi,
98           unsigned int is_wup, unsigned int num_initial_slots)
99 {
100         int ret;
101         unsigned char reqb[3];
102         struct iso14443b_atqb atqb;
103         unsigned int atqb_len = sizeof(atqb);
104         unsigned int num_slot_idx = num_initial_slots;
105
106         reqb[0] = 0x05;
107         reqb[1] = afi;
108
109         for (num_slot_idx = num_initial_slots; num_slot_idx <= 4;
110              num_slot_idx++) {
111                 reqb[2] = num_slot_idx & 0x07;
112                 if (is_wup)
113                         reqb[2] |= 0x80;
114
115                 ret = h->rh->reader->transcieve(h->rh, reqb, sizeof(reqb),
116                                                  (unsigned char *)&atqb, 
117                                                  &atqb_len, ATQB_TIMEOUT, 0);
118                 h->priv.iso14443b.state = ISO14443B_STATE_REQB_SENT;
119                 if (ret < 0) {
120                         DEBUGP("error during transcieve of REQB/WUBP\n");
121                         continue;
122                 }
123
124                 /* FIXME: send N-1 slot marker frames */
125         
126                 if (atqb_len != sizeof(atqb)) {
127                         DEBUGP("error: atqb_len = %u instead of %u\n",
128                                 atqb_len, sizeof(atqb));
129                         continue;
130                 }
131
132                 /* FIXME: how to detect a collission at 14443B ?  I guess we
133                  * can only rely on the CRC checking (CRCErr in ErrorFlag
134                  * register?) */
135
136                 if (parse_atqb(h, &atqb) >= 0) {
137                         h->priv.iso14443b.state = ISO14443B_STATE_ATQB_RCVD;
138                         return 0;
139                 }
140         }
141
142         return -1;
143 }
144
145 static inline unsigned int mbli_to_mbl(struct rfid_layer2_handle *h,
146                                         unsigned int mbli)
147 {
148         return (h->priv.iso14443b.fsc * 2 ^ (mbli-1));
149 }
150
151 static int
152 transcieve_attrib(struct rfid_layer2_handle *h, const unsigned char *inf,
153             unsigned int inf_len, unsigned char *rx_data, unsigned int *rx_len)
154 {
155         struct iso14443b_attrib_hdr *attrib;
156         unsigned int attrib_size = sizeof(*attrib) + inf_len;
157         unsigned char *rx_buf;
158         unsigned char fsdi;
159         int ret = 0;
160         
161         DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
162         attrib = malloc(attrib_size);
163         if (!attrib) {
164                 perror("attrib_alloc");
165                 return -1;
166         }
167
168         DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
169         rx_buf = malloc(*rx_len+1);
170         if (!rx_buf) {
171                 perror("rx_buf malloc");
172                 ret = -1;
173                 goto out_attrib;
174         }
175
176         /* initialize attrib frame */
177         memset(attrib, 0, attrib_size);
178         if (inf_len)
179                 memcpy((unsigned char *)attrib+sizeof(*attrib), inf, inf_len);
180
181         attrib->one_d = 0x1d;
182         memcpy(attrib->identifier, h->priv.iso14443b.pupi, 4);
183
184         /* FIXME: do we want to change TR0/TR1 from its default ? */
185         /* FIXME: do we want to change SOF/EOF from its default ? */
186
187         ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.iso14443b.fsd);
188         if (ret < 0) {
189                 DEBUGP("unable to map FSD(%u) to FSDI\n",
190                         h->priv.iso14443b.fsd);
191                 goto out_rx;
192         }
193         attrib->param2.fsdi = fsdi;
194
195         /* FIXME: spd_in / spd_out */
196         if (h->priv.iso14443b.tcl_capable == 1)
197                 attrib->param3.protocol_type = 0x1;
198
199         *rx_len = *rx_len + 1;
200         ret = h->rh->reader->transcieve(h->rh, (unsigned char *) attrib,
201                                         sizeof(*attrib)+inf_len,
202                                         rx_buf, rx_len, h->priv.iso14443b.fwt,
203                                         0);
204         h->priv.iso14443b.state = ISO14443B_STATE_ATTRIB_SENT;
205         if (ret < 0) {
206                 DEBUGP("transcieve problem\n");
207                 goto out_rx;
208         }
209
210         if ((rx_buf[0] & 0x0f) != h->priv.iso14443b.cid) {
211                 DEBUGP("ATTRIB response with invalid CID %u\n",
212                         rx_buf[0] & 0x0f);
213                 ret = -1;
214                 goto out_rx;
215         }
216
217         h->priv.iso14443b.state = ISO14443B_STATE_SELECTED;
218         
219         h->priv.iso14443b.mbl = mbli_to_mbl(h, (rx_data[0] & 0xf0) >> 4);
220
221         *rx_len = *rx_len - 1;
222         memcpy(rx_data, rx_buf+1, *rx_len);
223
224 out_rx:
225         free(rx_buf);
226 out_attrib:
227         free(attrib);
228
229         return ret;
230 }
231
232 static int
233 iso14443b_hltb(struct rfid_layer2_handle *h)
234 {
235         int ret;
236         unsigned char hltb[5];
237         unsigned char hltb_resp[1];
238         unsigned int hltb_len = 1;
239
240         hltb[0] = 0x50;
241         memcpy(hltb+1, h->priv.iso14443b.pupi, 4);
242
243         ret = h->rh->reader->transcieve(h->rh, hltb, 5,
244                                         hltb_resp, &hltb_len,
245                                         h->priv.iso14443b.fwt, 0);
246         h->priv.iso14443b.state = ISO14443B_STATE_HLTB_SENT;
247         if (ret < 0) {
248                 DEBUGP("transcieve problem\n");
249                 return ret;
250         }
251
252         if (hltb_len != 1 || hltb_resp[0] != 0x00) {
253                 DEBUGP("bad HLTB response\n");
254                 return -1;
255         }
256         h->priv.iso14443b.state = ISO14443B_STATE_HALTED;
257                                                 
258         return 0;
259 }
260
261 static int
262 iso14443b_anticol(struct rfid_layer2_handle *handle)
263 {
264         unsigned char afi = 0; /* FIXME */
265         int ret;
266         char buf[255];
267         unsigned int buf_len = sizeof(buf);
268
269         ret = send_reqb(handle, afi, 0, 0);
270         if (ret < 0)
271                 return ret;
272
273         ret = transcieve_attrib(handle, NULL, 0, buf, &buf_len);
274         if (ret < 0)
275                 return ret;
276
277         return 0;
278 }
279
280 static struct rfid_layer2_handle *
281 iso14443b_init(struct rfid_reader_handle *rh)
282 {
283         int ret;
284         struct rfid_layer2_handle *h = malloc(sizeof(*h));
285         if (!h)
286                 return NULL;
287
288         h->l2 = &rfid_layer2_iso14443b;
289         h->rh = rh;
290         h->priv.iso14443b.state = ISO14443B_STATE_NONE;
291
292         h->priv.iso14443b.fsd = iso14443_fsd_approx(rh->ah->mru);
293         DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
294
295         /* 14443-3 Section 7.1.6 */
296         h->priv.iso14443b.tr0 = (256/ISO14443_FREQ_SUBCARRIER)*10e6;
297         h->priv.iso14443b.tr1 = (200/ISO14443_FREQ_SUBCARRIER)*10e6;
298
299         ret = h->rh->reader->iso14443b.init(h->rh);
300         if (ret < 0) {
301                 DEBUGP("error during reader 14443b init\n");
302                 free(h);
303                 return NULL;
304         }
305
306         return h;
307 }
308
309 static int
310 iso14443b_fini(struct rfid_layer2_handle *handle)
311 {
312         free(handle);
313         return 0;
314 }
315
316 static int
317 iso14443b_transcieve(struct rfid_layer2_handle *handle,
318                      const unsigned char *tx_buf, unsigned int tx_len,
319                      unsigned char *rx_buf, unsigned int *rx_len,
320                      unsigned int timeout, unsigned int flags)
321 {
322         return handle->rh->reader->transcieve(handle->rh, tx_buf, tx_len,
323                                               rx_buf, rx_len, timeout, flags);
324 }
325
326 struct rfid_layer2 rfid_layer2_iso14443b = {
327         .id     = RFID_LAYER2_ISO14443B,
328         .name   = "ISO 14443-3 B",
329         .fn     = {
330                 .init           = &iso14443b_init,
331                 .open           = &iso14443b_anticol,
332                 .transcieve     = &iso14443b_transcieve,
333                 .close          = &iso14443b_hltb,
334                 .fini           = &iso14443b_fini,
335         },
336 };
337