remove autogenerated file
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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         [RFID_PROTOCOL_TAGIT]           = &rfid_protocol_tagit,
32 };
33
34 struct rfid_protocol_handle *
35 rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id)
36 {
37         const struct rfid_protocol *p;
38         struct rfid_protocol_handle *ph = NULL;
39
40         if (id >= ARRAY_SIZE(rfid_protocols))
41                 return NULL;
42
43         p = rfid_protocols[id];
44
45         ph = p->fn.init(l2h);
46         if (!ph)
47                 return NULL;
48
49         ph->proto = p;
50         ph->l2h = l2h;
51
52         return ph;
53 }
54
55 int
56 rfid_protocol_open(struct rfid_protocol_handle *ph)
57 {
58         if (ph->proto->fn.open)
59                 return ph->proto->fn.open(ph);
60         return 0;
61 }
62
63 int
64 rfid_protocol_transceive(struct rfid_protocol_handle *ph,
65                          const unsigned char *tx_buf, unsigned int len,
66                          unsigned char *rx_buf, unsigned int *rx_len,
67                          unsigned int timeout, unsigned int flags)
68 {
69         return ph->proto->fn.transceive(ph, tx_buf, len, rx_buf, rx_len,
70                                         timeout, flags);
71 }
72
73 int
74 rfid_protocol_read(struct rfid_protocol_handle *ph,
75                    unsigned int page,
76                    unsigned char *rx_data,
77                    unsigned int *rx_len)
78 {
79         if (ph->proto->fn.read)
80                 return ph->proto->fn.read(ph, page, rx_data, rx_len);
81         else
82                 return -EINVAL;
83 }
84
85 int
86 rfid_protocol_write(struct rfid_protocol_handle *ph,
87                    unsigned int page,
88                    unsigned char *tx_data,
89                    unsigned int tx_len)
90 {
91         if (ph->proto->fn.write)
92                 return ph->proto->fn.write(ph, page, tx_data, tx_len);
93         else
94                 return -EINVAL;
95 }
96
97 int rfid_protocol_fini(struct rfid_protocol_handle *ph)
98 {
99         return ph->proto->fn.fini(ph);
100 }
101
102 int
103 rfid_protocol_close(struct rfid_protocol_handle *ph)
104 {
105         if (ph->proto->fn.close)
106                 return ph->proto->fn.close(ph);
107         return 0;
108 }
109
110 int
111 rfid_protocol_getopt(struct rfid_protocol_handle *ph, int optname,
112                      void *optval, unsigned int *optlen)
113 {
114         if (optname >> 16 == 0) {
115                 unsigned char *optchar = optval;
116
117                 switch (optname) {
118                 default:
119                         return -EINVAL;
120                         break;
121                 }
122         } else {
123                 if (!ph->proto->fn.getopt)
124                         return -EINVAL;
125
126                 return ph->proto->fn.getopt(ph, optname, optval, optlen);
127         }
128         return 0;
129 }
130
131 int
132 rfid_protocol_setopt(struct rfid_protocol_handle *ph, int optname,
133                      const void *optval, unsigned int optlen)
134 {
135         if (optname >> 16 == 0) {
136                 switch (optname) {
137                 default:
138                         return -EINVAL;
139                         break;
140                 }
141         } else {
142                 if (!ph->proto->fn.setopt)
143                         return -EINVAL;
144
145                 return ph->proto->fn.setopt(ph, optname, optval, optlen);
146         }
147         return 0;
148 }
149
150 char *rfid_protocol_name(struct rfid_protocol_handle *ph)
151 {
152         return ph->proto->name;
153 }