first read the register, then print it. not the other way around ;)
[librfid] / 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 <rfid/rfid_layer2.h>
25 #include <rfid/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
34         for (p = rfid_protocol_list; p; p = p->next)
35                 if (p->id == id)
36                         return p->fn.init(l2h);
37
38         return NULL;
39 }
40
41 int
42 rfid_protocol_open(struct rfid_protocol_handle *ph)
43 {
44         if (ph->proto->fn.open)
45                 return ph->proto->fn.open(ph);
46         return 0;
47 }
48
49 int
50 rfid_protocol_transcieve(struct rfid_protocol_handle *ph,
51                          const unsigned char *tx_buf, unsigned int len,
52                          unsigned char *rx_buf, unsigned int *rx_len,
53                          unsigned int timeout, unsigned int flags)
54 {
55         return ph->proto->fn.transcieve(ph, tx_buf, len, rx_buf, rx_len,
56                                         timeout, flags);
57 }
58
59 int
60 rfid_protocol_read(struct rfid_protocol_handle *ph,
61                    unsigned int page,
62                    unsigned char *rx_data,
63                    unsigned int rx_len)
64 {
65         if (ph->proto->fn.read)
66                 return ph->proto->fn.read(ph, page, rx_data, rx_len);
67         else
68                 return -EINVAL;
69 }
70
71 int
72 rfid_protocol_write(struct rfid_protocol_handle *ph,
73                    unsigned int page,
74                    unsigned char *tx_data,
75                    unsigned int tx_len)
76 {
77         if (ph->proto->fn.write)
78                 return ph->proto->fn.write(ph, page, tx_data, tx_len);
79         else
80                 return -EINVAL;
81 }
82
83 int rfid_protocol_fini(struct rfid_protocol_handle *ph)
84 {
85         return ph->proto->fn.fini(ph);
86 }
87
88 int
89 rfid_protocol_close(struct rfid_protocol_handle *ph)
90 {
91         if (ph->proto->fn.close)
92                 return ph->proto->fn.close(ph);
93         return 0;
94 }
95
96 int
97 rfid_protocol_register(struct rfid_protocol *p)
98 {
99         p->next = rfid_protocol_list;
100         rfid_protocol_list = p;
101
102         return 0;
103 }