Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / btdevctl / db.c
blobb5da377806de00e09072e01d4a207ed6ff2e4281
1 /* $NetBSD: db.c,v 1.3 2007/04/21 06:15:24 plunky Exp $ */
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
7 * Written by Iain Hibbert for Itronix Inc.
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.
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.
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: db.c,v 1.3 2007/04/21 06:15:24 plunky Exp $");
37 #include <bluetooth.h>
38 #include <err.h>
39 #include <stdlib.h>
40 #include <stdbool.h>
42 #include <prop/proplib.h>
44 #include <dev/bluetooth/btdev.h>
45 #include <dev/bluetooth/bthidev.h>
46 #include <dev/bluetooth/btsco.h>
48 #include "btdevctl.h"
50 #define BTDEVCTL_PLIST "/var/db/btdevctl.plist"
51 #define BTDEVCTL_VERSION 2
53 static prop_dictionary_t db = NULL;
54 static bool db_flush = true; /* write db on set */
56 static void db_update0(void);
57 static void db_update1(void);
60 * lookup laddr/raddr/service in database and return dictionary
62 prop_dictionary_t
63 db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
65 prop_dictionary_t ldev, rdev, dev;
66 prop_object_t obj;
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");
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;
84 default:
85 errx(EXIT_FAILURE, "unknown btdevctl-version");
90 ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
91 if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY)
92 return NULL;
94 rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
95 if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY)
96 return NULL;
98 dev = prop_dictionary_get(rdev, service);
99 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
100 return NULL;
102 return dev;
106 * store dictionary in database at laddr/raddr/service
109 db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
111 prop_dictionary_t ldev, rdev;
112 prop_number_t version;
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;
120 if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev))
121 return 0;
123 prop_object_release(ldev);
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;
132 if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev))
133 return 0;
135 prop_object_release(rdev);
138 if (!prop_dictionary_set(rdev, service, dev))
139 return 0;
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");
146 if (!prop_dictionary_set(db, "btdevctl-version", version))
147 err(EXIT_FAILURE, "prop_dictionary_set");
149 prop_object_release(version);
151 if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST))
152 warn("%s", BTDEVCTL_PLIST);
155 return 1;
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.
165 static void
166 db_update0(void)
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;
175 db_flush = false; /* no write on set */
176 old = db;
178 db = prop_dictionary_create();
179 if (db == NULL)
180 err(EXIT_FAILURE, "prop_dictionary_create");
182 iter = prop_dictionary_iterator(old);
183 if (iter == NULL)
184 err(EXIT_FAILURE, "prop_dictionary_iterator");
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");
191 obj = prop_dictionary_get(dev, BTDEVladdr);
192 if (prop_data_size(obj) != sizeof(laddr))
193 errx(EXIT_FAILURE, "invalid %s", BTDEVladdr);
195 bdaddr_copy(&laddr, prop_data_data_nocopy(obj));
196 prop_dictionary_remove(dev, BTDEVladdr);
198 obj = prop_dictionary_get(dev, BTDEVraddr);
199 if (prop_data_size(obj) != sizeof(raddr))
200 errx(EXIT_FAILURE, "invalid %s", BTDEVraddr);
202 bdaddr_copy(&raddr, prop_data_data_nocopy(obj));
203 prop_dictionary_remove(dev, BTDEVraddr);
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);
217 if (!db_set(dev, &laddr, &raddr, service))
218 err(EXIT_FAILURE, "service store failed");
221 prop_object_iterator_release(iter);
222 prop_object_release(old);
224 db_flush = true; /* write on set */
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.
232 static void
233 db_update1(void)
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;
241 iter0 = prop_dictionary_iterator(db);
242 if (iter0 == NULL)
243 err(EXIT_FAILURE, "prop_dictionary_iterator");
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;
251 iter1 = prop_dictionary_iterator(ldev);
252 if (iter1 == NULL)
253 err(EXIT_FAILURE, "prop_dictionary_iterator");
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;
261 srv = prop_dictionary_get(rdev, "HID");
262 if (prop_object_type(srv) != PROP_TYPE_DICTIONARY)
263 continue;
265 obj = prop_dictionary_get(srv, BTHIDEVdescriptor);
266 if (prop_object_type(obj) != PROP_TYPE_DATA)
267 continue;
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);
273 prop_object_release(obj);
276 prop_object_iterator_release(iter1);
279 prop_object_iterator_release(iter0);