initial checkin of harald welte's original librfid project
[rfid/librfid.git] / src / rfid_layer2.c
1 /* librfid - layer 2 protocol handler 
2  * (C) 2005 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 struct rfid_layer2 *rfid_layer2_list;
28
29 struct rfid_layer2_handle *
30 rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id)
31 {
32         struct rfid_layer2 *p;
33
34         for (p = rfid_layer2_list; p; p = p->next)
35                 if (p->id == id)
36                         return p->fn.init(rh);
37
38         DEBUGP("unable to find matching layer2 protocol\n");
39         return NULL;
40 }
41
42 int
43 rfid_layer2_open(struct rfid_layer2_handle *ph)
44 {
45         if (!ph->l2->fn.open)
46                 return 0;
47
48         return ph->l2->fn.open(ph);
49 }
50
51 int
52 rfid_layer2_transcieve(struct rfid_layer2_handle *ph,
53                         enum rfid_frametype frametype,
54                          const unsigned char *tx_buf, unsigned int len,
55                          unsigned char *rx_buf, unsigned int *rx_len,
56                          u_int64_t timeout, unsigned int flags)
57 {
58         if (!ph->l2->fn.transcieve)
59                 return -EIO;
60
61         return ph->l2->fn.transcieve(ph, frametype, tx_buf, len, rx_buf,
62                                      rx_len, timeout, flags);
63 }
64
65 int rfid_layer2_fini(struct rfid_layer2_handle *ph)
66 {
67         if (!ph->l2->fn.fini)
68                 return 0;
69
70         return ph->l2->fn.fini(ph);
71 }
72
73 int
74 rfid_layer2_close(struct rfid_layer2_handle *ph)
75 {
76         if (!ph->l2->fn.close)
77                 return 0;
78
79         return ph->l2->fn.close(ph);
80 }
81
82 int
83 rfid_layer2_register(struct rfid_layer2 *p)
84 {
85         p->next = rfid_layer2_list;
86         rfid_layer2_list = p;
87
88         return 0;
89 }
90
91 int
92 rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname,
93                    void *optval, unsigned int *optlen)
94 {
95         if (!ph->l2->fn.getopt)
96                 return -EINVAL;
97
98         return ph->l2->fn.getopt(ph, optname, optval, optlen);
99 }
100
101 int
102 rfid_layer2_setopt(struct rfid_layer2_handle *ph, int optname,
103                    const void *optval, unsigned int optlen)
104 {
105         if (!ph->l2->fn.setopt)
106                 return -EINVAL;
107
108         return ph->l2->fn.setopt(ph, optname, optval, optlen);
109 }