#include <sys/cdefs.h>
__RCSID("$NetBSD: db.c,v 1.6 2020/06/07 00:12:00 thorpej Exp $");
#include <bluetooth.h>
#include <err.h>
#include <stdlib.h>
#include <stdbool.h>
#include <prop/proplib.h>
#include <dev/bluetooth/btdev.h>
#include <dev/bluetooth/bthidev.h>
#include <dev/bluetooth/btsco.h>
#include "btdevctl.h"
#define BTDEVCTL_PLIST "/var/db/btdevctl.plist"
#define BTDEVCTL_VERSION 2
static prop_dictionary_t db = NULL;
static bool db_flush = true;
static void db_update0(void);
static void db_update1(void);
prop_dictionary_t
db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
{
prop_dictionary_t ldev, rdev, dev;
prop_object_t obj;
if (db == NULL) {
db = prop_dictionary_internalize_from_file(BTDEVCTL_PLIST);
if (db == NULL) {
db = prop_dictionary_create();
if (db == NULL)
err(EXIT_FAILURE, "prop_dictionary_create");
return NULL;
} else {
obj = prop_dictionary_get(db, "btdevctl-version");
switch(prop_number_signed_value(obj)) {
case 0: db_update0();
case 1: db_update1();
case BTDEVCTL_VERSION:
break;
default:
errx(EXIT_FAILURE, "unknown btdevctl-version");
}
}
}
ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY)
return NULL;
rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY)
return NULL;
dev = prop_dictionary_get(rdev, service);
if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
return NULL;
return dev;
}
int
db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
{
prop_dictionary_t ldev, rdev;
ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
if (ldev == NULL) {
ldev = prop_dictionary_create();
if (ldev == NULL)
return 0;
if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev))
return 0;
prop_object_release(ldev);
}
rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
if (rdev == NULL) {
rdev = prop_dictionary_create();
if (rdev == NULL)
return 0;
if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev))
return 0;
prop_object_release(rdev);
}
if (!prop_dictionary_set(rdev, service, dev))
return 0;
if (db_flush == true) {
if (!prop_dictionary_set_int(db, "btdevctl-version",
BTDEVCTL_VERSION))
err(EXIT_FAILURE, "prop_dictionary_set_int");
if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST))
warn("%s", BTDEVCTL_PLIST);
}
return 1;
}
static void
db_update0(void)
{
prop_dictionary_t old, dev;
prop_dictionary_keysym_t key;
prop_object_iterator_t iter;
prop_object_t obj;
bdaddr_t laddr, raddr;
const char *service;
db_flush = false;
old = db;
db = prop_dictionary_create();
if (db == NULL)
err(EXIT_FAILURE, "prop_dictionary_create");
iter = prop_dictionary_iterator(old);
if (iter == NULL)
err(EXIT_FAILURE, "prop_dictionary_iterator");
while ((key = prop_object_iterator_next(iter)) != NULL) {
dev = prop_dictionary_get_keysym(old, key);
if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
errx(EXIT_FAILURE, "invalid device dictionary");
obj = prop_dictionary_get(dev, BTDEVladdr);
if (prop_data_size(obj) != sizeof(laddr))
errx(EXIT_FAILURE, "invalid %s", BTDEVladdr);
bdaddr_copy(&laddr, prop_data_value(obj));
prop_dictionary_remove(dev, BTDEVladdr);
obj = prop_dictionary_get(dev, BTDEVraddr);
if (prop_data_size(obj) != sizeof(raddr))
errx(EXIT_FAILURE, "invalid %s", BTDEVraddr);
bdaddr_copy(&raddr, prop_data_value(obj));
prop_dictionary_remove(dev, BTDEVraddr);
obj = prop_dictionary_get(dev, BTDEVtype);
if (prop_string_equals_string(obj, "bthidev"))
service = "HID";
else if (prop_string_equals_string(obj, "btsco")) {
obj = prop_dictionary_get(dev, BTSCOlisten);
if (prop_bool_true(obj))
service = "HF";
else
service = "HSET";
} else
errx(EXIT_FAILURE, "invalid %s", BTDEVtype);
if (!db_set(dev, &laddr, &raddr, service))
err(EXIT_FAILURE, "service store failed");
}
prop_object_iterator_release(iter);
prop_object_release(old);
db_flush = true;
}
static void
db_update1(void)
{
prop_dictionary_t ldev, rdev, srv;
prop_object_iterator_t iter0, iter1;
prop_dictionary_keysym_t key;
prop_object_t obj;
bdaddr_t bdaddr;
iter0 = prop_dictionary_iterator(db);
if (iter0 == NULL)
err(EXIT_FAILURE, "prop_dictionary_iterator");
while ((key = prop_object_iterator_next(iter0)) != NULL) {
ldev = prop_dictionary_get_keysym(db, key);
if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY
|| !bt_aton(prop_dictionary_keysym_value(key), &bdaddr))
continue;
iter1 = prop_dictionary_iterator(ldev);
if (iter1 == NULL)
err(EXIT_FAILURE, "prop_dictionary_iterator");
while ((key = prop_object_iterator_next(iter1)) != NULL) {
rdev = prop_dictionary_get_keysym(ldev, key);
if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY
|| !bt_aton(prop_dictionary_keysym_value(key), &bdaddr))
continue;
srv = prop_dictionary_get(rdev, "HID");
if (prop_object_type(srv) != PROP_TYPE_DICTIONARY)
continue;
obj = prop_dictionary_get(srv, BTHIDEVdescriptor);
if (prop_object_type(obj) != PROP_TYPE_DATA)
continue;
if (!prop_dictionary_set_string_nocopy(srv, BTDEVmode,
hid_mode(obj)))
err(EXIT_FAILURE, "Cannot set %s", BTDEVmode);
}
prop_object_iterator_release(iter1);
}
prop_object_iterator_release(iter0);
}