Correctly initialise rx_len in mfcl_write() (Pierrick Hascoet <pierrick.hascoet...
[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
24 #include <librfid/rfid.h>
25 #include <librfid/rfid_layer2.h>
26
27 static const struct rfid_layer2 *rfid_layer2s[] = {
28         [RFID_LAYER2_ISO14443A] = &rfid_layer2_iso14443a,
29         [RFID_LAYER2_ISO14443B] = &rfid_layer2_iso14443b,
30         [RFID_LAYER2_ISO15693]  = &rfid_layer2_iso15693,
31 };
32
33 struct rfid_layer2_handle *
34 rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id)
35 {
36         struct rfid_layer2 *p;
37
38         if (id >= ARRAY_SIZE(rfid_layer2s)) {
39                 DEBUGP("unable to find matching layer2 protocol\n");
40                 return NULL;
41         }
42
43         p = rfid_layer2s[id];
44         return p->fn.init(rh);
45 }
46
47 int
48 rfid_layer2_open(struct rfid_layer2_handle *ph)
49 {
50         if (!ph->l2->fn.open)
51                 return 0;
52
53         return ph->l2->fn.open(ph);
54 }
55
56 int
57 rfid_layer2_transceive(struct rfid_layer2_handle *ph,
58                         enum rfid_frametype frametype,
59                          const unsigned char *tx_buf, unsigned int len,
60                          unsigned char *rx_buf, unsigned int *rx_len,
61                          u_int64_t timeout, unsigned int flags)
62 {
63         if (!ph->l2->fn.transceive)
64                 return -EIO;
65
66         return ph->l2->fn.transceive(ph, frametype, tx_buf, len, rx_buf,
67                                      rx_len, timeout, flags);
68 }
69
70 int rfid_layer2_fini(struct rfid_layer2_handle *ph)
71 {
72         if (!ph->l2->fn.fini)
73                 return 0;
74
75         return ph->l2->fn.fini(ph);
76 }
77
78 int
79 rfid_layer2_close(struct rfid_layer2_handle *ph)
80 {
81         if (!ph->l2->fn.close)
82                 return 0;
83
84         return ph->l2->fn.close(ph);
85 }
86
87 int
88 rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname,
89                    void *optval, unsigned int *optlen)
90 {
91         if (optname >> 16 == 0) {
92                 unsigned char *optchar = optval;
93
94                 switch (optname) {
95                 case RFID_OPT_LAYER2_UID:
96                         if (ph->uid_len < *optlen)
97                                 *optlen = ph->uid_len;
98                         memcpy(optchar, ph->uid, *optlen);
99                         break;
100                 default:
101                         return -EINVAL;
102                         break;
103                 }
104         } else {
105                 if (!ph->l2->fn.getopt)
106                         return -EINVAL;
107
108                 return ph->l2->fn.getopt(ph, optname, optval, optlen);
109         }
110         return 0;
111 }
112
113 int
114 rfid_layer2_setopt(struct rfid_layer2_handle *ph, int optname,
115                    const void *optval, unsigned int optlen)
116 {
117         if (optname >> 16 == 0) {
118                 switch (optname) {
119                 default:
120                         return -EINVAL;
121                         break;
122                 }
123         } else {
124                 if (!ph->l2->fn.setopt)
125                         return -EINVAL;
126
127                 return ph->l2->fn.setopt(ph, optname, optval, optlen);
128         }
129         return 0;
130 }
131
132 char *rfid_layer2_name(struct rfid_layer2_handle *l2h)
133 {
134         return l2h->l2->name;
135 }