db.c revision 1.1.4.1 1 /* $NetBSD: db.c,v 1.1.4.1 2007/07/19 16:04:22 liamjfoy Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: db.c,v 1.1.4.1 2007/07/19 16:04:22 liamjfoy Exp $");
36
37 #include <bluetooth.h>
38 #include <err.h>
39 #include <stdlib.h>
40
41 #include <prop/proplib.h>
42
43 #include <dev/bluetooth/btdev.h>
44 #include <dev/bluetooth/bthidev.h>
45 #include <dev/bluetooth/btsco.h>
46
47 #include "btdevctl.h"
48
49 #define BTDEVCTL_PLIST "/var/db/btdevctl.plist"
50 #define BTDEVCTL_VERSION 2
51
52 static prop_dictionary_t db = NULL;
53 static int db_flush = TRUE; /* write db on set */
54
55 static void db_update0(void);
56 static void db_update1(void);
57
58 /*
59 * lookup laddr/raddr/service in database and return dictionary
60 */
61 prop_dictionary_t
62 db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
63 {
64 prop_dictionary_t ldev, rdev, dev;
65 prop_object_t obj;
66
67 if (db == NULL) {
68 db = prop_dictionary_internalize_from_file(BTDEVCTL_PLIST);
69 if (db == NULL) {
70 db = prop_dictionary_create();
71 if (db == NULL)
72 err(EXIT_FAILURE, "prop_dictionary_create");
73
74 return NULL;
75 } else {
76 obj = prop_dictionary_get(db, "btdevctl-version");
77 switch(prop_number_integer_value(obj)) {
78 case 0: db_update0();
79 case 1: db_update1();
80 case BTDEVCTL_VERSION:
81 break;
82
83 default:
84 errx(EXIT_FAILURE, "unknown btdevctl-version");
85 }
86 }
87 }
88
89 ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
90 if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY)
91 return NULL;
92
93 rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
94 if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY)
95 return NULL;
96
97 dev = prop_dictionary_get(rdev, service);
98 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
99 return NULL;
100
101 return dev;
102 }
103
104 /*
105 * store dictionary in database at laddr/raddr/service
106 */
107 int
108 db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
109 {
110 prop_dictionary_t ldev, rdev;
111 prop_number_t version;
112
113 ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
114 if (ldev == NULL) {
115 ldev = prop_dictionary_create();
116 if (ldev == NULL)
117 return 0;
118
119 if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev))
120 return 0;
121
122 prop_object_release(ldev);
123 }
124
125 rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
126 if (rdev == NULL) {
127 rdev = prop_dictionary_create();
128 if (rdev == NULL)
129 return 0;
130
131 if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev))
132 return 0;
133
134 prop_object_release(rdev);
135 }
136
137 if (!prop_dictionary_set(rdev, service, dev))
138 return 0;
139
140 if (db_flush == TRUE) {
141 version = prop_number_create_integer(BTDEVCTL_VERSION);
142 if (version == NULL)
143 err(EXIT_FAILURE, "prop_number_create_integer");
144
145 if (!prop_dictionary_set(db, "btdevctl-version", version))
146 err(EXIT_FAILURE, "prop_dictionary_set");
147
148 prop_object_release(version);
149
150 if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST))
151 warn("%s", BTDEVCTL_PLIST);
152 }
153
154 return 1;
155 }
156
157 /*
158 * update database from version 0. This was a flat file using
159 * btdevN as an index, and local-bdaddr and remote-bdaddr stored
160 * as data objects. Step through and add them to the new dictionary.
161 * We have to generate the service, but only HID, HF and HSET
162 * were supported, so thats not too difficult.
163 */
164 static void
165 db_update0(void)
166 {
167 prop_dictionary_t old, dev;
168 prop_dictionary_keysym_t key;
169 prop_object_iterator_t iter;
170 prop_object_t obj;
171 bdaddr_t laddr, raddr;
172 char *service;
173
174 db_flush = FALSE; /* no write on set */
175 old = db;
176
177 db = prop_dictionary_create();
178 if (db == NULL)
179 err(EXIT_FAILURE, "prop_dictionary_create");
180
181 iter = prop_dictionary_iterator(old);
182 if (iter == NULL)
183 err(EXIT_FAILURE, "prop_dictionary_iterator");
184
185 while ((key = prop_object_iterator_next(iter)) != NULL) {
186 dev = prop_dictionary_get_keysym(old, key);
187 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
188 errx(EXIT_FAILURE, "invalid device dictionary");
189
190 obj = prop_dictionary_get(dev, BTDEVladdr);
191 if (prop_data_size(obj) != sizeof(laddr))
192 errx(EXIT_FAILURE, "invalid %s", BTDEVladdr);
193
194 bdaddr_copy(&laddr, prop_data_data_nocopy(obj));
195 prop_dictionary_remove(dev, BTDEVladdr);
196
197 obj = prop_dictionary_get(dev, BTDEVraddr);
198 if (prop_data_size(obj) != sizeof(raddr))
199 errx(EXIT_FAILURE, "invalid %s", BTDEVraddr);
200
201 bdaddr_copy(&raddr, prop_data_data_nocopy(obj));
202 prop_dictionary_remove(dev, BTDEVraddr);
203
204 obj = prop_dictionary_get(dev, BTDEVtype);
205 if (prop_string_equals_cstring(obj, "bthidev"))
206 service = "HID";
207 else if (prop_string_equals_cstring(obj, "btsco")) {
208 obj = prop_dictionary_get(dev, BTSCOlisten);
209 if (prop_bool_true(obj))
210 service = "HF";
211 else
212 service = "HSET";
213 } else
214 errx(EXIT_FAILURE, "invalid %s", BTDEVtype);
215
216 if (!db_set(dev, &laddr, &raddr, service))
217 err(EXIT_FAILURE, "service store failed");
218 }
219
220 prop_object_iterator_release(iter);
221 prop_object_release(old);
222
223 db_flush = TRUE; /* write on set */
224 }
225
226 /*
227 * update database from version 1. Link Mode capability was added.
228 * By default, we request authentication for HIDs, and encryption
229 * is enabled for keyboards.
230 */
231 static void
232 db_update1(void)
233 {
234 prop_dictionary_t ldev, rdev, srv;
235 prop_object_iterator_t iter0, iter1;
236 prop_dictionary_keysym_t key;
237 prop_object_t obj;
238 bdaddr_t bdaddr;
239
240 iter0 = prop_dictionary_iterator(db);
241 if (iter0 == NULL)
242 err(EXIT_FAILURE, "prop_dictionary_iterator");
243
244 while ((key = prop_object_iterator_next(iter0)) != NULL) {
245 ldev = prop_dictionary_get_keysym(db, key);
246 if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY
247 || !bt_aton(prop_dictionary_keysym_cstring_nocopy(key), &bdaddr))
248 continue;
249
250 iter1 = prop_dictionary_iterator(ldev);
251 if (iter1 == NULL)
252 err(EXIT_FAILURE, "prop_dictionary_iterator");
253
254 while ((key = prop_object_iterator_next(iter1)) != NULL) {
255 rdev = prop_dictionary_get_keysym(ldev, key);
256 if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY
257 || !bt_aton(prop_dictionary_keysym_cstring_nocopy(key), &bdaddr))
258 continue;
259
260 srv = prop_dictionary_get(rdev, "HID");
261 if (prop_object_type(srv) != PROP_TYPE_DICTIONARY)
262 continue;
263
264 obj = prop_dictionary_get(srv, BTHIDEVdescriptor);
265 if (prop_object_type(obj) != PROP_TYPE_DATA)
266 continue;
267
268 obj = prop_string_create_cstring_nocopy(hid_mode(obj));
269 if (obj == NULL || !prop_dictionary_set(srv, BTDEVmode, obj))
270 err(EXIT_FAILURE, "Cannot set %s", BTDEVmode);
271
272 prop_object_release(obj);
273 }
274
275 prop_object_iterator_release(iter1);
276 }
277
278 prop_object_iterator_release(iter0);
279 }
280