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