db.c revision 1.4 1 /* $NetBSD: db.c,v 1.4 2007/08/17 17:59:16 pavel 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.4 2007/08/17 17:59:16 pavel Exp $");
36
37 #include <bluetooth.h>
38 #include <err.h>
39 #include <stdlib.h>
40 #include <stdbool.h>
41
42 #include <prop/proplib.h>
43
44 #include <dev/bluetooth/btdev.h>
45 #include <dev/bluetooth/bthidev.h>
46 #include <dev/bluetooth/btsco.h>
47
48 #include "btdevctl.h"
49
50 #define BTDEVCTL_PLIST "/var/db/btdevctl.plist"
51 #define BTDEVCTL_VERSION 2
52
53 static prop_dictionary_t db = NULL;
54 static bool db_flush = true; /* write db on set */
55
56 static void db_update0(void);
57 static void db_update1(void);
58
59 /*
60 * lookup laddr/raddr/service in database and return dictionary
61 */
62 prop_dictionary_t
63 db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
64 {
65 prop_dictionary_t ldev, rdev, dev;
66 prop_object_t obj;
67
68 if (db == NULL) {
69 db = prop_dictionary_internalize_from_file(BTDEVCTL_PLIST);
70 if (db == NULL) {
71 db = prop_dictionary_create();
72 if (db == NULL)
73 err(EXIT_FAILURE, "prop_dictionary_create");
74
75 return NULL;
76 } else {
77 obj = prop_dictionary_get(db, "btdevctl-version");
78 switch(prop_number_integer_value(obj)) {
79 case 0: db_update0();
80 case 1: db_update1();
81 case BTDEVCTL_VERSION:
82 break;
83
84 default:
85 errx(EXIT_FAILURE, "unknown btdevctl-version");
86 }
87 }
88 }
89
90 ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
91 if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY)
92 return NULL;
93
94 rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
95 if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY)
96 return NULL;
97
98 dev = prop_dictionary_get(rdev, service);
99 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
100 return NULL;
101
102 return dev;
103 }
104
105 /*
106 * store dictionary in database at laddr/raddr/service
107 */
108 int
109 db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
110 {
111 prop_dictionary_t ldev, rdev;
112 prop_number_t version;
113
114 ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
115 if (ldev == NULL) {
116 ldev = prop_dictionary_create();
117 if (ldev == NULL)
118 return 0;
119
120 if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev))
121 return 0;
122
123 prop_object_release(ldev);
124 }
125
126 rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
127 if (rdev == NULL) {
128 rdev = prop_dictionary_create();
129 if (rdev == NULL)
130 return 0;
131
132 if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev))
133 return 0;
134
135 prop_object_release(rdev);
136 }
137
138 if (!prop_dictionary_set(rdev, service, dev))
139 return 0;
140
141 if (db_flush == true) {
142 version = prop_number_create_integer(BTDEVCTL_VERSION);
143 if (version == NULL)
144 err(EXIT_FAILURE, "prop_number_create_integer");
145
146 if (!prop_dictionary_set(db, "btdevctl-version", version))
147 err(EXIT_FAILURE, "prop_dictionary_set");
148
149 prop_object_release(version);
150
151 if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST))
152 warn("%s", BTDEVCTL_PLIST);
153 }
154
155 return 1;
156 }
157
158 /*
159 * update database from version 0. This was a flat file using
160 * btdevN as an index, and local-bdaddr and remote-bdaddr stored
161 * as data objects. Step through and add them to the new dictionary.
162 * We have to generate the service, but only HID, HF and HSET
163 * were supported, so thats not too difficult.
164 */
165 static void
166 db_update0(void)
167 {
168 prop_dictionary_t old, dev;
169 prop_dictionary_keysym_t key;
170 prop_object_iterator_t iter;
171 prop_object_t obj;
172 bdaddr_t laddr, raddr;
173 const char *service;
174
175 db_flush = false; /* no write on set */
176 old = db;
177
178 db = prop_dictionary_create();
179 if (db == NULL)
180 err(EXIT_FAILURE, "prop_dictionary_create");
181
182 iter = prop_dictionary_iterator(old);
183 if (iter == NULL)
184 err(EXIT_FAILURE, "prop_dictionary_iterator");
185
186 while ((key = prop_object_iterator_next(iter)) != NULL) {
187 dev = prop_dictionary_get_keysym(old, key);
188 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
189 errx(EXIT_FAILURE, "invalid device dictionary");
190
191 obj = prop_dictionary_get(dev, BTDEVladdr);
192 if (prop_data_size(obj) != sizeof(laddr))
193 errx(EXIT_FAILURE, "invalid %s", BTDEVladdr);
194
195 bdaddr_copy(&laddr, prop_data_data_nocopy(obj));
196 prop_dictionary_remove(dev, BTDEVladdr);
197
198 obj = prop_dictionary_get(dev, BTDEVraddr);
199 if (prop_data_size(obj) != sizeof(raddr))
200 errx(EXIT_FAILURE, "invalid %s", BTDEVraddr);
201
202 bdaddr_copy(&raddr, prop_data_data_nocopy(obj));
203 prop_dictionary_remove(dev, BTDEVraddr);
204
205 obj = prop_dictionary_get(dev, BTDEVtype);
206 if (prop_string_equals_cstring(obj, "bthidev"))
207 service = "HID";
208 else if (prop_string_equals_cstring(obj, "btsco")) {
209 obj = prop_dictionary_get(dev, BTSCOlisten);
210 if (prop_bool_true(obj))
211 service = "HF";
212 else
213 service = "HSET";
214 } else
215 errx(EXIT_FAILURE, "invalid %s", BTDEVtype);
216
217 if (!db_set(dev, &laddr, &raddr, service))
218 err(EXIT_FAILURE, "service store failed");
219 }
220
221 prop_object_iterator_release(iter);
222 prop_object_release(old);
223
224 db_flush = true; /* write on set */
225 }
226
227 /*
228 * update database from version 1. Link Mode capability was added.
229 * By default, we request authentication for HIDs, and encryption
230 * is enabled for keyboards.
231 */
232 static void
233 db_update1(void)
234 {
235 prop_dictionary_t ldev, rdev, srv;
236 prop_object_iterator_t iter0, iter1;
237 prop_dictionary_keysym_t key;
238 prop_object_t obj;
239 bdaddr_t bdaddr;
240
241 iter0 = prop_dictionary_iterator(db);
242 if (iter0 == NULL)
243 err(EXIT_FAILURE, "prop_dictionary_iterator");
244
245 while ((key = prop_object_iterator_next(iter0)) != NULL) {
246 ldev = prop_dictionary_get_keysym(db, key);
247 if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY
248 || !bt_aton(prop_dictionary_keysym_cstring_nocopy(key), &bdaddr))
249 continue;
250
251 iter1 = prop_dictionary_iterator(ldev);
252 if (iter1 == NULL)
253 err(EXIT_FAILURE, "prop_dictionary_iterator");
254
255 while ((key = prop_object_iterator_next(iter1)) != NULL) {
256 rdev = prop_dictionary_get_keysym(ldev, key);
257 if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY
258 || !bt_aton(prop_dictionary_keysym_cstring_nocopy(key), &bdaddr))
259 continue;
260
261 srv = prop_dictionary_get(rdev, "HID");
262 if (prop_object_type(srv) != PROP_TYPE_DICTIONARY)
263 continue;
264
265 obj = prop_dictionary_get(srv, BTHIDEVdescriptor);
266 if (prop_object_type(obj) != PROP_TYPE_DATA)
267 continue;
268
269 obj = prop_string_create_cstring_nocopy(hid_mode(obj));
270 if (obj == NULL || !prop_dictionary_set(srv, BTDEVmode, obj))
271 err(EXIT_FAILURE, "Cannot set %s", BTDEVmode);
272
273 prop_object_release(obj);
274 }
275
276 prop_object_iterator_release(iter1);
277 }
278
279 prop_object_iterator_release(iter0);
280 }
281