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