remove autogenerated file
[librfid] / src / rfid_scan.c
1 /* RFID scanning implementation
2  *
3  * (C) 2006 by Harald Welte <laforge@gnumonks.org>
4  *
5  */
6
7 /*
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 
10  *  as published by the Free Software Foundation
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <librfid/rfid.h>
23 #include <librfid/rfid_layer2.h>
24 #include <librfid/rfid_reader.h>
25 #include <librfid/rfid_protocol.h>
26
27 static struct rfid_layer2_handle *
28 rfid_layer2_scan1(struct rfid_reader_handle *rh, int l2)
29 {
30         struct rfid_layer2_handle *l2h;
31
32         if (rh->reader->l2_supported & (1 << l2)) {
33                 l2h = rfid_layer2_init(rh, l2);
34                 if (!l2h)
35                         return NULL;
36                 if (rfid_layer2_open(l2h) < 0) {
37                         rfid_layer2_fini(l2h);
38                         return NULL;
39                 } else 
40                         return l2h;
41         }
42
43         return NULL;
44 }
45
46 struct rfid_layer2_handle *
47 rfid_layer2_scan(struct rfid_reader_handle *rh)
48 {
49         struct rfid_layer2_handle *l2h;
50         int i;
51
52 #define RFID_LAYER2_MAX 16
53         for (i = 0; i < RFID_LAYER2_MAX; i++) {
54                 DEBUGP("testing l2 %u\n", i);
55                 l2h = rfid_layer2_scan1(rh, i);
56                 if (l2h)
57                         return l2h;
58         }
59
60         return NULL;
61 }
62
63 static struct rfid_protocol_handle *
64 rfid_protocol_scan1(struct rfid_layer2_handle *l2h, int proto)
65 {
66         struct rfid_protocol_handle *ph;
67
68         if (l2h->rh->reader->proto_supported & (1 << proto) &&
69             l2h->proto_supported & (1 << proto)) {
70                 ph = rfid_protocol_init(l2h, proto);
71                 if (!ph)
72                         return NULL;
73                 if (rfid_protocol_open(ph) < 0) {
74                         rfid_protocol_fini(ph);
75                         return NULL;
76                 } else
77                         return ph;
78         }
79
80         return NULL;
81 }
82
83 struct rfid_protocol_handle *
84 rfid_protocol_scan(struct rfid_layer2_handle *l2h)
85 {
86         struct rfid_protocol_handle *ph;
87         int i;
88
89 #define RFID_PROTOCOL_MAX 16
90         for (i = 0; i < RFID_PROTOCOL_MAX; i++) {
91                 DEBUGP("testing proto %u\n", i);
92                 ph = rfid_protocol_scan1(l2h, i);
93                 if (ph)
94                         return ph;
95         }
96
97         return NULL;
98 }
99
100 /* Scan for any RFID transponder within range of the given reader.
101  * Abort after the first successfully found transponder. */
102 int rfid_scan(struct rfid_reader_handle *rh,
103               struct rfid_layer2_handle **l2h,
104               struct rfid_protocol_handle **ph)
105 {
106         *l2h = rfid_layer2_scan(rh);
107         if (!*l2h)
108                 return 0;
109         
110         *ph = rfid_protocol_scan(*l2h);
111         if (!*ph)
112                 return 2;
113
114         return 3;
115 }