first beta - patch sent to librfid list
[rfid/librfid.git] / src / rfid_protocol.c
1 /* librfid - layer 3 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 <unistd.h>
22 #include <errno.h>
23
24 #include <librfid/rfid_layer2.h>
25 #include <librfid/rfid_protocol.h>
26
27 static struct rfid_protocol *rfid_protocol_list;
28
29 struct rfid_protocol_handle *
30 rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id)
31 {
32         struct rfid_protocol *p;
33         struct rfid_protocol_handle *ph = NULL;
34
35         for (p = rfid_protocol_list; p; p = p->next) {
36                 if (p->id == id) {
37                         ph = p->fn.init(l2h);
38                         break;
39                 }
40         }
41
42         if (!ph)
43                 return NULL;
44
45         ph->proto = p;
46         ph->l2h = l2h;
47
48         return ph;
49 }
50
51 int
52 rfid_protocol_open(struct rfid_protocol_handle *ph)
53 {
54         if (ph->proto->fn.open)
55                 return ph->proto->fn.open(ph);
56         return 0;
57 }
58
59 int
60 rfid_protocol_transcieve(struct rfid_protocol_handle *ph,
61                          const unsigned char *tx_buf, unsigned int len,
62                          unsigned char *rx_buf, unsigned int *rx_len,
63                          unsigned int timeout, unsigned int flags)
64 {
65         return ph->proto->fn.transcieve(ph, tx_buf, len, rx_buf, rx_len,
66                                         timeout, flags);
67 }
68
69 int
70 rfid_protocol_read(struct rfid_protocol_handle *ph,
71                    unsigned int page,
72                    unsigned char *rx_data,
73                    unsigned int *rx_len)
74 {
75         if (ph->proto->fn.read)
76                 return ph->proto->fn.read(ph, page, rx_data, rx_len);
77         else
78                 return -EINVAL;
79 }
80
81 int
82 rfid_protocol_write(struct rfid_protocol_handle *ph,
83                    unsigned int page,
84                    unsigned char *tx_data,
85                    unsigned int tx_len)
86 {
87         if (ph->proto->fn.write)
88                 return ph->proto->fn.write(ph, page, tx_data, tx_len);
89         else
90                 return -EINVAL;
91 }
92
93 int rfid_protocol_fini(struct rfid_protocol_handle *ph)
94 {
95         return ph->proto->fn.fini(ph);
96 }
97
98 int
99 rfid_protocol_close(struct rfid_protocol_handle *ph)
100 {
101         if (ph->proto->fn.close)
102                 return ph->proto->fn.close(ph);
103         return 0;
104 }
105
106 int
107 rfid_protocol_register(struct rfid_protocol *p)
108 {
109         p->next = rfid_protocol_list;
110         rfid_protocol_list = p;
111
112         return 0;
113 }