- use C99 compiler flags
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 static 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                 l2h = rfid_layer2_scan1(rh, i);
55                 if (l2h)
56                         return l2h;
57         }
58
59         return NULL;
60 }
61
62 static struct rfid_protocol_handle *
63 rfid_protocol_scan1(struct rfid_layer2_handle *l2h, int proto)
64 {
65         struct rfid_protocol_handle *ph;
66
67         if (l2h->rh->reader->proto_supported & (1 << proto) &&
68             l2h->proto_supported & (1 << proto)) {
69                 ph = rfid_protocol_init(l2h, proto);
70                 if (!ph)
71                         return NULL;
72                 if (rfid_protocol_open(ph) < 0) {
73                         rfid_protocol_fini(ph);
74                         return NULL;
75                 } else
76                         return ph;
77         }
78
79         return NULL;
80 }
81
82 static struct rfid_protocol_handle *
83 rfid_protocol_scan(struct rfid_layer2_handle *l2h)
84 {
85         struct rfid_protocol_handle *ph;
86         int i;
87
88 #define RFID_PROTOCOL_MAX 16
89         for (i = 0; i < RFID_PROTOCOL_MAX; i++) {
90                 ph = rfid_protocol_scan1(l2h, i);
91                 if (ph)
92                         return ph;
93         }
94
95         return NULL;
96 }
97
98 /* Scan for any RFID transponder within range of the given reader.
99  * Abort after the first successfully found transponder. */
100 int rfid_scan(struct rfid_reader_handle *rh,
101               struct rfid_layer2_handle **l2h,
102               struct rfid_protocol_handle **ph)
103 {
104         *l2h = rfid_layer2_scan(rh);
105         if (!*l2h)
106                 return 0;
107         
108         *ph = rfid_protocol_scan(l2h);
109         if (!*ph)
110                 return 2;
111         
112         return 3;
113 }