Add RFID_OPT_14443A_WUPA to request WUPA instead of REQA (Rainer Keller <mail@rainerk...
[librfid] / src / rfid_layer2.c
1 /* librfid - layer 2 protocol handler 
2  * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
3  */
4
5 /*
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 
8  *  as published by the Free Software Foundation
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <string.h> /* for memcpy */
24
25 #include <librfid/rfid.h>
26 #include <librfid/rfid_layer2.h>
27
28 static const struct rfid_layer2 *rfid_layer2s[] = {
29         [RFID_LAYER2_ISO14443A] = &rfid_layer2_iso14443a,
30         [RFID_LAYER2_ISO14443B] = &rfid_layer2_iso14443b,
31         [RFID_LAYER2_ISO15693]  = &rfid_layer2_iso15693,
32 };
33
34 struct rfid_layer2_handle *
35 rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id)
36 {
37         struct rfid_layer2 *p;
38
39         if (id >= ARRAY_SIZE(rfid_layer2s)) {
40                 DEBUGP("unable to find matching layer2 protocol\n");
41                 return NULL;
42         }
43
44         p = rfid_layer2s[id];
45         return p->fn.init(rh);
46 }
47
48 int
49 rfid_layer2_open(struct rfid_layer2_handle *ph)
50 {
51         if (!ph->l2->fn.open)
52                 return 0;
53
54         return ph->l2->fn.open(ph);
55 }
56
57 int
58 rfid_layer2_transceive(struct rfid_layer2_handle *ph,
59                         enum rfid_frametype frametype,
60                          const unsigned char *tx_buf, unsigned int len,
61                          unsigned char *rx_buf, unsigned int *rx_len,
62                          u_int64_t timeout, unsigned int flags)
63 {
64         if (!ph->l2->fn.transceive)
65                 return -EIO;
66
67         return ph->l2->fn.transceive(ph, frametype, tx_buf, len, rx_buf,
68                                      rx_len, timeout, flags);
69 }
70
71 int rfid_layer2_fini(struct rfid_layer2_handle *ph)
72 {
73         if (!ph->l2->fn.fini)
74                 return 0;
75
76         return ph->l2->fn.fini(ph);
77 }
78
79 int
80 rfid_layer2_close(struct rfid_layer2_handle *ph)
81 {
82         if (!ph->l2->fn.close)
83                 return 0;
84
85         return ph->l2->fn.close(ph);
86 }
87
88 int
89 rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname,
90                    void *optval, unsigned int *optlen)
91 {
92         if (optname >> 16 == 0) {
93                 unsigned char *optchar = optval;
94
95                 switch (optname) {
96                 case RFID_OPT_LAYER2_UID:
97                         if (ph->uid_len < *optlen)
98                                 *optlen = ph->uid_len;
99                         memcpy(optchar, ph->uid, *optlen);
100                         break;
101                 default:
102                         return -EINVAL;
103                         break;
104                 }
105         } else {
106                 if (!ph->l2->fn.getopt)
107                         return -EINVAL;
108
109                 return ph->l2->fn.getopt(ph, optname, optval, optlen);
110         }
111         return 0;
112 }
113
114 int
115 rfid_layer2_setopt(struct rfid_layer2_handle *ph, int optname,
116                    const void *optval, unsigned int optlen)
117 {
118         if (optname >> 16 == 0) {
119                 switch (optname) {
120                 default:
121                         return -EINVAL;
122                         break;
123                 }
124         } else {
125                 if (!ph->l2->fn.setopt)
126                         return -EINVAL;
127
128                 return ph->l2->fn.setopt(ph, optname, optval, optlen);
129         }
130         return 0;
131 }
132
133 char *rfid_layer2_name(struct rfid_layer2_handle *l2h)
134 {
135         return l2h->l2->name;
136 }