1 /* $NetBSD: db.c,v 1.3 2007/04/21 06:15:24 plunky Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
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
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>
42 #include <prop/proplib.h>
44 #include <dev/bluetooth/btdev.h>
45 #include <dev/bluetooth/bthidev.h>
46 #include <dev/bluetooth/btsco.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
63 db_get(bdaddr_t
*laddr
, bdaddr_t
*raddr
, const char *service
)
65 prop_dictionary_t ldev
, rdev
, dev
;
69 db
= prop_dictionary_internalize_from_file(BTDEVCTL_PLIST
);
71 db
= prop_dictionary_create();
73 err(EXIT_FAILURE
, "prop_dictionary_create");
77 obj
= prop_dictionary_get(db
, "btdevctl-version");
78 switch(prop_number_integer_value(obj
)) {
81 case BTDEVCTL_VERSION
:
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
)
94 rdev
= prop_dictionary_get(ldev
, bt_ntoa(raddr
, NULL
));
95 if (prop_object_type(rdev
) != PROP_TYPE_DICTIONARY
)
98 dev
= prop_dictionary_get(rdev
, service
);
99 if (prop_object_type(dev
) != PROP_TYPE_DICTIONARY
)
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
));
116 ldev
= prop_dictionary_create();
120 if (!prop_dictionary_set(db
, bt_ntoa(laddr
, NULL
), ldev
))
123 prop_object_release(ldev
);
126 rdev
= prop_dictionary_get(ldev
, bt_ntoa(raddr
, NULL
));
128 rdev
= prop_dictionary_create();
132 if (!prop_dictionary_set(ldev
, bt_ntoa(raddr
, NULL
), rdev
))
135 prop_object_release(rdev
);
138 if (!prop_dictionary_set(rdev
, service
, dev
))
141 if (db_flush
== true) {
142 version
= prop_number_create_integer(BTDEVCTL_VERSION
);
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
);
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.
168 prop_dictionary_t old
, dev
;
169 prop_dictionary_keysym_t key
;
170 prop_object_iterator_t iter
;
172 bdaddr_t laddr
, raddr
;
175 db_flush
= false; /* no write on set */
178 db
= prop_dictionary_create();
180 err(EXIT_FAILURE
, "prop_dictionary_create");
182 iter
= prop_dictionary_iterator(old
);
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"))
208 else if (prop_string_equals_cstring(obj
, "btsco")) {
209 obj
= prop_dictionary_get(dev
, BTSCOlisten
);
210 if (prop_bool_true(obj
))
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.
235 prop_dictionary_t ldev
, rdev
, srv
;
236 prop_object_iterator_t iter0
, iter1
;
237 prop_dictionary_keysym_t key
;
241 iter0
= prop_dictionary_iterator(db
);
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
))
251 iter1
= prop_dictionary_iterator(ldev
);
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
))
261 srv
= prop_dictionary_get(rdev
, "HID");
262 if (prop_object_type(srv
) != PROP_TYPE_DICTIONARY
)
265 obj
= prop_dictionary_get(srv
, BTHIDEVdescriptor
);
266 if (prop_object_type(obj
) != PROP_TYPE_DATA
)
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
);