1 /* $NetBSD: db.c,v 1.1 2006/09/10 15:45:56 plunky 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 2006/09/10 15:45:56 plunky 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/btsco.h> 45 46 #include "btdevctl.h" 47 48 #define BTDEVCTL_PLIST "/var/db/btdevctl.plist" 49 #define BTDEVCTL_VERSION 1 50 51 static prop_dictionary_t db = NULL; 52 static int db_flush = TRUE; /* write db on set */ 53 54 static void db_update0(void); 55 56 /* 57 * lookup laddr/raddr/service in database and return dictionary 58 */ 59 prop_dictionary_t 60 db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service) 61 { 62 prop_dictionary_t ldev, rdev, dev; 63 prop_object_t obj; 64 65 if (db == NULL) { 66 db = prop_dictionary_internalize_from_file(BTDEVCTL_PLIST); 67 if (db == NULL) { 68 db = prop_dictionary_create(); 69 if (db == NULL) 70 err(EXIT_FAILURE, "prop_dictionary_create"); 71 72 return NULL; 73 } else { 74 obj = prop_dictionary_get(db, "btdevctl-version"); 75 switch(prop_number_integer_value(obj)) { 76 case 0: 77 db_update0(); 78 break; 79 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