1 /* librfid - layer 2 protocol handler
2 * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
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
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.
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
23 #include <string.h> /* for memcpy */
25 #include <librfid/rfid.h>
26 #include <librfid/rfid_layer2.h>
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,
34 struct rfid_layer2_handle *
35 rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id)
37 struct rfid_layer2 *p;
39 if (id >= ARRAY_SIZE(rfid_layer2s)) {
40 DEBUGP("unable to find matching layer2 protocol\n");
45 return p->fn.init(rh);
49 rfid_layer2_open(struct rfid_layer2_handle *ph)
54 return ph->l2->fn.open(ph);
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)
64 if (!ph->l2->fn.transceive)
67 return ph->l2->fn.transceive(ph, frametype, tx_buf, len, rx_buf,
68 rx_len, timeout, flags);
71 int rfid_layer2_fini(struct rfid_layer2_handle *ph)
76 return ph->l2->fn.fini(ph);
80 rfid_layer2_close(struct rfid_layer2_handle *ph)
82 if (!ph->l2->fn.close)
85 return ph->l2->fn.close(ph);
89 rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname,
90 void *optval, unsigned int *optlen)
92 if (optname >> 16 == 0) {
93 unsigned char *optchar = optval;
96 case RFID_OPT_LAYER2_UID:
97 if (ph->uid_len < *optlen)
98 *optlen = ph->uid_len;
99 memcpy(optchar, ph->uid, *optlen);
106 if (!ph->l2->fn.getopt)
109 return ph->l2->fn.getopt(ph, optname, optval, optlen);
115 rfid_layer2_setopt(struct rfid_layer2_handle *ph, int optname,
116 const void *optval, unsigned int optlen)
118 if (optname >> 16 == 0) {
125 if (!ph->l2->fn.setopt)
128 return ph->l2->fn.setopt(ph, optname, optval, optlen);
133 char *rfid_layer2_name(struct rfid_layer2_handle *l2h)
135 return l2h->l2->name;