Add RFID_OPT_14443A_WUPA to request WUPA instead of REQA (Rainer Keller <mail@rainerk...
[librfid] / src / rfid_protocol.c
1 /* librfid - layer 4 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 <unistd.h>
22 #include <errno.h>
23
24 #include <librfid/rfid_layer2.h>
25 #include <librfid/rfid_protocol.h>
26
27 static const struct rfid_protocol *rfid_protocols[] = {
28         [RFID_PROTOCOL_MIFARE_CLASSIC]  = &rfid_protocol_mfcl,
29         [RFID_PROTOCOL_MIFARE_UL]       = &rfid_protocol_mful,
30         [RFID_PROTOCOL_TCL]             = &rfid_protocol_tcl,
31 };
32
33 struct rfid_protocol_handle *
34 rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id)
35 {
36         struct rfid_protocol *p;
37         struct rfid_protocol_handle *ph = NULL;
38
39         if (id >= ARRAY_SIZE(rfid_protocols))
40                 return NULL;
41
42         p = rfid_protocols[id];
43
44         ph = p->fn.init(l2h);
45         if (!ph)
46                 return NULL;
47
48         ph->proto = p;
49         ph->l2h = l2h;
50
51         return ph;
52 }
53
54 int
55 rfid_protocol_open(struct rfid_protocol_handle *ph)
56 {
57         if (ph->proto->fn.open)
58                 return ph->proto->fn.open(ph);
59         return 0;
60 }
61
62 int
63 rfid_protocol_transceive(struct rfid_protocol_handle *ph,
64                          const unsigned char *tx_buf, unsigned int len,
65                          unsigned char *rx_buf, unsigned int *rx_len,
66                          unsigned int timeout, unsigned int flags)
67 {
68         return ph->proto->fn.transceive(ph, tx_buf, len, rx_buf, rx_len,
69                                         timeout, flags);
70 }
71
72 int
73 rfid_protocol_read(struct rfid_protocol_handle *ph,
74                    unsigned int page,
75                    unsigned char *rx_data,
76                    unsigned int *rx_len)
77 {
78         if (ph->proto->fn.read)
79                 return ph->proto->fn.read(ph, page, rx_data, rx_len);
80         else
81                 return -EINVAL;
82 }
83
84 int
85 rfid_protocol_write(struct rfid_protocol_handle *ph,
86                    unsigned int page,
87                    unsigned char *tx_data,
88                    unsigned int tx_len)
89 {
90         if (ph->proto->fn.write)
91                 return ph->proto->fn.write(ph, page, tx_data, tx_len);
92         else
93                 return -EINVAL;
94 }
95
96 int rfid_protocol_fini(struct rfid_protocol_handle *ph)
97 {
98         return ph->proto->fn.fini(ph);
99 }
100
101 int
102 rfid_protocol_close(struct rfid_protocol_handle *ph)
103 {
104         if (ph->proto->fn.close)
105                 return ph->proto->fn.close(ph);
106         return 0;
107 }
108
109 int
110 rfid_protocol_getopt(struct rfid_protocol_handle *ph, int optname,
111                      void *optval, unsigned int *optlen)
112 {
113         if (optname >> 16 == 0) {
114                 unsigned char *optchar = optval;
115
116                 switch (optname) {
117                 default:
118                         return -EINVAL;
119                         break;
120                 }
121         } else {
122                 if (!ph->proto->fn.getopt)
123                         return -EINVAL;
124
125                 return ph->proto->fn.getopt(ph, optname, optval, optlen);
126         }
127         return 0;
128 }
129
130 int
131 rfid_protocol_setopt(struct rfid_protocol_handle *ph, int optname,
132                      const void *optval, unsigned int optlen)
133 {
134         if (optname >> 16 == 0) {
135                 switch (optname) {
136                 default:
137                         return -EINVAL;
138                         break;
139                 }
140         } else {
141                 if (!ph->proto->fn.setopt)
142                         return -EINVAL;
143
144                 return ph->proto->fn.setopt(ph, optname, optval, optlen);
145         }
146         return 0;
147 }
148
149 char *rfid_protocol_name(struct rfid_protocol_handle *ph)
150 {
151         return ph->proto->name;
152 }