remove autogenerated file
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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                 case RFID_OPT_LAYER2_UID:
121                 printf("----> sizeof(ph->uid): %d\n",sizeof(ph->uid));
122                         if ((ph->uid_len < sizeof(ph->uid)) && (optlen<=sizeof(ph->uid))) {
123                                 //(ph->uid_len<optlen)
124                                 ph->uid_len = optlen;
125                                 memcpy(ph->uid, optval, optlen);
126                         } else
127                                 return -EINVAL;
128                 break;
129                 default:
130                         return -EINVAL;
131                         break;
132                 }
133         } else {
134                 if (!ph->l2->fn.setopt)
135                         return -EINVAL;
136
137                 return ph->l2->fn.setopt(ph, optname, optval, optlen);
138         }
139         return 0;
140 }
141
142 char *rfid_layer2_name(struct rfid_layer2_handle *l2h)
143 {
144         return l2h->l2->name;
145 }