- add support for toggle bit
[librfid] / rfid_proto_tcl.c
1 /* ISO 14443-4 (T=CL) implementation, PCD side.
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 <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include <rfid/rfid.h>
29 #include <rfid/rfid_protocol_tcl.h>
30 #include <rfid/rfid_protocol.h>
31 #include <rfid/rfid_layer2.h>
32 #include <rfid/rfid_layer2_iso14443b.h>
33
34 #include <rfid/rfid_asic.h>
35 #include <rfid/rfid_reader.h>
36
37 #include "rfid_iso14443_common.h"
38
39
40 static unsigned int sfgi_to_sfgt(struct rfid_protocol_handle *h, 
41                                  unsigned char sfgi)
42 {
43         /* ISO 14443-4:2000(E) Section 5.2.5. */
44         return (256 * 16 / h->l2h->rh->ah->fc) * (2 ^ sfgi);
45 }
46
47 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h, 
48                                 unsigned char fwi)
49 {
50         /* ISO 14443-4:2000(E) Section 7.2. */
51         return (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi);
52 }
53
54 #define activation_fwt(x) (65536 / x->l2h->rh->ah->fc)
55 #define deactivation_fwt(x) activation_fwt(x)
56
57 static int
58 tcl_parse_ats(struct rfid_protocol_handle *h, 
59                 unsigned char *ats, unsigned int size)
60 {
61         unsigned char len = ats[0];
62         unsigned char t0;
63         unsigned char *cur;
64
65         if (len == 0 || size == 0) 
66                 return -1;
67
68         if (size < len)
69                 len = size;
70
71         if (len == 1) {
72                 /* FIXME: assume some default values */
73                 h->priv.tcl.fsc = 32;
74                 h->priv.tcl.ta = 0;
75                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
76                 if (1 /* FIXME: is_iso14443a */) {
77                         /* Section 7.2: fwi default for type A is 4 */
78                         h->priv.tcl.fwt = fwi_to_fwt(h, 4);
79                 } else {
80                         /* Section 7.2: fwi for type B is always in ATQB */
81                         /* FIXME */
82                 }
83                 return 0;
84         }
85
86         /* guarateed to be at least 2 bytes in size */
87
88         t0 = ats[1];
89         cur = &ats[2];
90
91         iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
92
93         if (t0 & (1 << 4)) {
94                 /* TA is transmitted */
95                 h->priv.tcl.ta = *cur++;
96         }
97
98         if (t0 & (1 << 5)) {
99                 /* TB is transmitted */
100                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
101                 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
102                 cur++;
103         }
104
105         if (t0 & (1 << 6)) {
106                 /* TC is transmitted */
107                 if (*cur & 0x01)
108                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
109                 if (*cur & 0x02)
110                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
111                 cur++;
112         }
113
114         h->priv.tcl.historical_len = (ats+len) - cur;
115         h->priv.tcl.historical_bytes = cur;
116
117         return 0;
118 }
119
120
121 /* request an ATS from the PICC */
122 static int
123 tcl_request_ats(struct rfid_protocol_handle *h)
124 {
125         int ret;
126         unsigned char rats[2];
127         unsigned char fsdi;
128
129         if (h->priv.tcl.state != TCL_STATE_INITIAL)
130                 return -1;
131
132         ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
133         if (ret < 0) {
134                 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
135                 return ret;
136         }
137
138         rats[0] = 0xe0;
139         rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
140
141         /* transcieve (with CRC) */
142         ret = h->l2h->l2->fn.transcieve(h->l2h, rats, 2, h->priv.tcl.ats,
143                                        &h->priv.tcl.ats_len, activation_fwt(h),
144                                        TCL_TRANSP_F_TX_CRC);
145         if (ret < 0) {
146                 DEBUGP("transcieve of rats failed\n");
147                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
148                 /* FIXME: retransmit */
149                 return ret;
150         }
151         h->priv.tcl.state = TCL_STATE_ATS_RCVD;
152
153         ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
154         if (ret < 0) {
155                 DEBUGP("parsing of ats failed\n");
156                 return ret;
157         }
158
159         return 0;
160 }
161 /* start a PSS run (autimatically configure highest possible speed */
162 static int 
163 tcl_do_pss(struct rfid_protocol_handle *h)
164 {
165         unsigned char ppss[3];
166         //unsigned char pps_response[1];
167
168         if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
169                 return -1;
170
171         /* ISO 14443-4:2000(E) Section 5.3. */
172
173         ppss[0] = 0xd0 & (h->priv.tcl.cid & 0x0f);
174         ppss[1] = 0x11;
175
176         //ppss[2] = 0x00 & foo;
177
178         // FIXME: finish
179         
180         return -1;
181         
182         h->priv.tcl.state = TCL_STATE_ESTABLISHED;
183 }
184
185
186 static int
187 tcl_build_prologue2(struct tcl_handle *th, 
188                     unsigned char *prlg, unsigned int *prlg_len, 
189                     unsigned char pcb)
190 {
191         *prlg_len = 1;
192
193         *prlg = pcb;
194
195         if (th->toggle) {
196                 /* we've sent a toggle bit last time */
197                 th->toggle = 0;
198         } else {
199                 /* we've not sent a toggle last time: send one */
200                 th->toggle = 1;
201                 *prlg |= 0x01;
202         }
203
204         if (th->flags & TCL_HANDLE_F_CID_USED) {
205                 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
206                 *prlg |= TCL_PCB_CID_FOLLOWING;
207                 (*prlg_len)++;
208                 prlg[*prlg_len] = th->cid & 0x0f;
209         }
210
211         /* nad only for I-block (0xc0 == 00) */
212         if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
213             ((pcb & 0xc0) == 0x00)) {
214                 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
215                 /* FIXME: in case of chaining only for first frame */
216                 *prlg |= TCL_PCB_NAD_FOLLOWING;
217                 prlg[*prlg_len] = th->nad;
218                 (*prlg_len)++;
219         }
220
221         return 0;
222 }
223
224 static int
225 tcl_build_prologue_i(struct tcl_handle *th,
226                      unsigned char *prlg, unsigned int *prlg_len)
227 {
228         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
229         return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
230 }
231
232 static int
233 tcl_build_prologue_r(struct tcl_handle *th,
234                      unsigned char *prlg, unsigned int *prlg_len,
235                      unsigned int nak)
236 {
237         unsigned char pcb = 0xa2;
238         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
239
240         if (nak)
241                 pcb |= 0x10;
242
243         return tcl_build_prologue2(th, prlg, prlg_len, pcb);
244 }
245
246 static int
247 tcl_build_prologue_s(struct tcl_handle *th,
248                      unsigned char *prlg, unsigned int *prlg_len)
249 {
250         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
251
252         /* the only S-block from PCD->PICC is DESELECT,
253          * well, actually there is the S(WTX) response. */
254         return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
255 }
256
257 /* FIXME: WTXM implementation */
258
259 static int tcl_prlg_len(struct tcl_handle *th)
260 {
261         int prlg_len = 1;
262
263         if (th->flags & TCL_HANDLE_F_CID_USED)
264                 prlg_len++;
265
266         if (th->flags & TCL_HANDLE_F_NAD_USED)
267                 prlg_len++;
268
269         return prlg_len;
270 }
271
272 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
273
274 static int
275 tcl_connect(struct rfid_protocol_handle *h)
276 {
277         int ret; 
278
279         if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
280             h->priv.tcl.state != TCL_STATE_INITIAL)
281                 return -1;
282
283         switch (h->l2h->l2->id) {
284         case RFID_LAYER2_ISO14443A:
285                 /* Start Type A T=CL Activation Sequence */
286                 ret = tcl_request_ats(h);
287                 if (ret < 0)
288                         return ret;
289
290                 if (0 /* FIXME */) {
291                         ret = tcl_do_pss(h);
292                         if (ret < 0)
293                                 return -1;
294                 }
295                 break;
296         case RFID_LAYER2_ISO14443B:
297                 /* initialized T=CL state from Type B Activation Data */
298                 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
299                 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
300                 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
301                 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
302
303                 /* what about ta? sfgt? */
304
305                 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
306                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
307                 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
308                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
309
310                 switch (h->l2h->priv.iso14443b.state) {
311                         case ISO14443B_STATE_SELECTED:
312                                 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
313                                 break;
314                         case ISO14443B_STATE_ATTRIB_SENT:
315                                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
316                                 break;
317                 }
318
319                 /* PUPI will be presented as ATS/historical bytes */
320                 memcpy(h->priv.tcl.ats, h->l2h->priv.iso14443b.pupi, 4);
321                 h->priv.tcl.ats_len = 4;
322                 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
323
324                 break;
325         default:
326                 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
327                 return -1;
328                 break;
329         }
330
331         return 0;
332 }
333
334 static int
335 tcl_deselect(struct rfid_protocol_handle *h)
336 {
337         /* ISO 14443-4:2000(E) Section 8 */
338         int ret;
339         unsigned char frame[3];         /* 3 bytes prologue, no information */
340         unsigned char rx[3];
341         unsigned int rx_len = sizeof(rx);
342         unsigned int prlg_len;
343         struct tcl_handle *th = &h->priv.tcl;
344
345         if (th->state != TCL_STATE_ESTABLISHED) {
346                 /* FIXME: not sure whether deselect is possible here,
347                  * probably better send a HLTA? */
348         }
349
350         /* build DESELECT S-block */
351         ret = tcl_build_prologue_s(th, frame, &prlg_len);
352         if (ret < 0)
353                 return ret;
354
355         ret = h->l2h->l2->fn.transcieve(h->l2h, frame, prlg_len, rx,
356                                      &rx_len, deactivation_fwt(h),
357                                      TCL_TRANSP_F_TX_CRC);
358         if (ret < 0) {
359                 /* FIXME: retransmit, HLT(A|B) */
360                 return ret;
361         }
362
363         th->state = TCL_STATE_DESELECTED;
364
365         return 0;
366 }
367
368 #define is_s_block(x) ((x & 0xc0) == 0xc0)
369 #define is_r_block(x) ((x & 0xc0) == 0x80)
370 #define is_i_block(x) ((x & 0xc0) == 0x00)
371
372 static int
373 tcl_transcieve(struct rfid_protocol_handle *h,
374                 const unsigned char *tx_data, unsigned int tx_len,
375                 unsigned char *rx_data, unsigned int *rx_len,
376                 unsigned int timeout, unsigned int flags)
377 {
378         int ret;
379         unsigned char *tx_buf, *rx_buf;
380         unsigned int prlg_len;
381         struct tcl_handle *th = &h->priv.tcl;
382
383         unsigned char *_tx;
384         unsigned int _tx_len, _timeout;
385         unsigned char wtx_resp[3];
386
387         if (tx_len > max_net_tx_framesize(th)) {
388                 /* slow path: we need to use chaining */
389                 return -1;
390         }
391
392         tx_buf = malloc(tcl_prlg_len(th) + tx_len);
393         if (!tx_buf) {
394                 ret = -ENOMEM;
395                 goto out;
396         }
397         rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
398         if (!rx_buf) {
399                 ret = -ENOMEM;
400                 goto out_txb;
401         }
402
403         if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
404                 ret = -1;
405                 goto out_rxb;
406         }
407         memcpy(tx_buf + prlg_len, tx_data, tx_len);
408
409         /* intialize to data-to-be-transferred */
410         _tx = tx_buf;
411         _tx_len = tx_len+prlg_len;
412         _timeout = th->fwt;
413
414 do_tx:
415         ret = h->l2h->l2->fn.transcieve(h->l2h, _tx, _tx_len,
416                                         rx_buf, rx_len, _timeout, 0);
417         if (ret < 0)
418                 goto out_rxb;
419
420         if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
421                 DEBUGP("response with wrong toggle bit\n");
422                 goto out_rxb;
423         }
424
425         //if (*rx_len )
426         //
427
428         if (is_r_block(*rx_buf)) {
429                 unsigned int txed = _tx - tx_buf;
430                 /* Handle ACK frame in case of chaining */
431                 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
432                         if (*(rx_buf+1) != h->priv.tcl.cid) {
433                                 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
434                                 goto out_rxb;
435                         }
436                 }
437                 /* set up parameters for next frame in chain */
438                 if (txed < tx_len) {
439                         /* move tx pointer by the amount of bytes transferred
440                          * in last frame */
441                         _tx += _tx_len;
442                         _tx_len = (tx_len - txed);
443                         if (_tx_len > max_net_tx_framesize(th)) {
444                                 /* not last frame in chain */
445                                 _tx_len = max_net_tx_framesize(th);
446                         } else {
447                                 /* last frame in chain */
448                         }
449                         goto do_tx;
450                 } else {
451                         DEBUGP("Received ACK in response to last frame in "
452                                "chain?!? Expected I-frame.\n");
453                         ret = -1;
454                         goto out_rxb;
455                 }
456         } else if (is_s_block(*rx_buf)) {
457                 unsigned char inf;
458                 unsigned int prlg_len;
459
460                 /* Handle Wait Time Extension */
461                 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
462                         if (*rx_len < 3) {
463                                 DEBUGP("S-Block with CID but short len\n");
464                                 ret = -1;
465                                 goto out_rxb;
466                         }
467                         if (*(rx_buf+1) != h->priv.tcl.cid) {
468                                 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
469                                 goto out_rxb;
470                         }
471                         inf = *(rx_buf+2);
472                 } else
473                         inf = *(rx_buf+1);
474
475                 if ((*rx_buf & 0x30) != 0x30) {
476                         DEBUGP("S-Block but not WTX?\n");
477                         ret = -1;
478                         goto out_rxb;
479                 }
480                 inf &= 0x3f;    /* only lower 6 bits code WTXM */
481                 if (inf == 0 || (inf >= 60 && inf <= 63)) {
482                         DEBUGP("WTXM %u is RFU!\n", inf);
483                         ret = -1;
484                         goto out_rxb;
485                 }
486                 
487                 /* Acknowledge WTXM */
488                 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
489                 /* set two bits that make this block a wtx */
490                 wtx_resp[0] |= 0x30;
491                 wtx_resp[prlg_len] = inf;
492                 _tx = wtx_resp;
493                 _tx_len = prlg_len+1;
494                 _timeout = th->fwt * inf;
495
496                 /* start over with next transcieve */
497                 goto do_tx; /* FIXME: do transcieve locally since we use
498                                 totally different buffer */
499
500         } else if (is_i_block(*rx_buf)) {
501                 unsigned char *inf = rx_buf+1;
502                 /* we're actually receiving payload data */
503
504                 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
505                         if (*(rx_buf+1) != h->priv.tcl.cid) {
506                                 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
507                                 goto out_rxb;
508                         }
509                         inf++;
510                 }
511                 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
512                         inf++;
513                 }
514                 memcpy(rx_data, inf, *rx_len - (inf - rx_buf));
515
516                 if (*rx_buf & 0x10) {
517                         /* we're not the last frame in the chain, continue */
518                         goto do_tx;
519                 }
520         }
521
522 out_rxb:
523         free(rx_buf);
524 out_txb:
525         free(tx_buf);
526 out:
527         return ret;
528 }
529
530 #if 0
531 int
532 tcl_send(struct tcl_handle *th)
533 {
534         return -1;
535 }
536
537 int
538 tcl_recv()
539 {
540         return -1;
541 }
542 #endif
543
544 static struct rfid_protocol_handle *
545 tcl_init(struct rfid_layer2_handle *l2h)
546 {
547         struct rfid_protocol_handle *th;
548         unsigned int mru = l2h->rh->ah->mru;
549
550         th = malloc(sizeof(struct rfid_protocol_handle) + mru);
551         if (!th)
552                 return NULL;
553
554         /* FIXME: mru should be attribute of layer2 (in case it adds/removes
555          * some overhead */
556         memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
557
558         /* maximum received ats length equals mru of asic/reader */
559         th->priv.tcl.state = TCL_STATE_INITIAL;
560         th->priv.tcl.ats_len = mru;
561         th->priv.tcl.toggle = 1;
562         th->l2h = l2h;
563         th->proto = &rfid_protocol_tcl;
564
565         th->priv.tcl.fsd = iso14443_fsd_approx(mru);
566
567         return th;
568 }
569
570 static int
571 tcl_fini(struct rfid_protocol_handle *ph)
572 {
573         free(ph);
574         return 0;
575 }
576
577 struct rfid_protocol rfid_protocol_tcl = {
578         .id     = RFID_PROTOCOL_TCL,
579         .name   = "ISO 14443-4 / T=CL",
580         .fn     = {
581                 .init = &tcl_init,
582                 .open = &tcl_connect,
583                 .transcieve = &tcl_transcieve,
584                 .close = &tcl_deselect,
585                 .fini = &tcl_fini,
586         },
587 };