2018-04-26 22:05:26 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <zephyr.h>
|
|
|
|
#include <settings/settings.h>
|
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/conn.h>
|
|
|
|
|
|
|
|
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_SETTINGS)
|
2018-07-17 10:35:52 +03:00
|
|
|
#define LOG_MODULE_NAME bt_settings
|
2018-04-26 22:05:26 +03:00
|
|
|
#include "common/log.h"
|
|
|
|
|
|
|
|
#include "hci_core.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2018-04-27 20:01:51 +03:00
|
|
|
void bt_settings_encode_key(char *path, size_t path_size, const char *subsys,
|
|
|
|
bt_addr_le_t *addr, const char *key)
|
|
|
|
{
|
|
|
|
if (key) {
|
|
|
|
snprintk(path, path_size,
|
|
|
|
"bt/%s/%02x%02x%02x%02x%02x%02x%u/%s", subsys,
|
|
|
|
addr->a.val[5], addr->a.val[4], addr->a.val[3],
|
|
|
|
addr->a.val[2], addr->a.val[1], addr->a.val[0],
|
|
|
|
addr->type, key);
|
|
|
|
} else {
|
|
|
|
snprintk(path, path_size,
|
|
|
|
"bt/%s/%02x%02x%02x%02x%02x%02x%u", subsys,
|
|
|
|
addr->a.val[5], addr->a.val[4], addr->a.val[3],
|
|
|
|
addr->a.val[2], addr->a.val[1], addr->a.val[0],
|
|
|
|
addr->type);
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:44:43 +02:00
|
|
|
BT_DBG("Encoded path %s", log_strdup(path));
|
2018-04-27 20:01:51 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
int bt_settings_decode_key(const char *key, bt_addr_le_t *addr)
|
2018-04-27 20:01:51 +03:00
|
|
|
{
|
|
|
|
bool high;
|
|
|
|
int i;
|
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
if (settings_name_next(key, NULL) != 13) {
|
2018-04-27 20:01:51 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key[12] == '0') {
|
|
|
|
addr->type = BT_ADDR_LE_PUBLIC;
|
|
|
|
} else if (key[12] == '1') {
|
|
|
|
addr->type = BT_ADDR_LE_RANDOM;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 5, high = true; i >= 0; key++) {
|
|
|
|
u8_t nibble;
|
|
|
|
|
|
|
|
if (*key >= '0' && *key <= '9') {
|
|
|
|
nibble = *key - '0';
|
|
|
|
} else if (*key >= 'a' && *key <= 'f') {
|
|
|
|
nibble = *key - 'a' + 10;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (high) {
|
|
|
|
addr->a.val[i] = nibble << 4;
|
|
|
|
high = false;
|
|
|
|
} else {
|
|
|
|
addr->a.val[i] |= nibble;
|
|
|
|
high = true;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 11:44:23 +01:00
|
|
|
BT_DBG("Decoded %s as %s", log_strdup(key), bt_addr_le_str(addr));
|
2018-04-27 20:01:51 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
static int set(const char *name, size_t len_rd, settings_read_cb read_cb,
|
2019-04-02 11:06:05 +02:00
|
|
|
void *cb_arg)
|
2018-04-26 22:05:26 +03:00
|
|
|
{
|
2019-05-01 08:47:58 +03:00
|
|
|
ssize_t len;
|
2019-06-14 18:23:39 +02:00
|
|
|
const char *next;
|
2018-04-26 22:05:26 +03:00
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
if (!name) {
|
|
|
|
BT_ERR("Insufficient number of arguments");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2018-04-27 13:24:02 +03:00
|
|
|
|
2019-06-14 18:23:39 +02:00
|
|
|
len = settings_name_next(name, &next);
|
|
|
|
|
|
|
|
if (!strncmp(name, "id", len)) {
|
2018-07-16 14:39:06 +03:00
|
|
|
/* Any previously provided identities supersede flash */
|
|
|
|
if (atomic_test_bit(bt_dev.flags, BT_DEV_PRESET_ID)) {
|
|
|
|
BT_WARN("Ignoring identities stored in flash");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:06:05 +02:00
|
|
|
len = read_cb(cb_arg, &bt_dev.id_addr, sizeof(bt_dev.id_addr));
|
2018-07-04 12:58:10 +03:00
|
|
|
if (len < sizeof(bt_dev.id_addr[0])) {
|
2018-11-24 16:32:41 +01:00
|
|
|
if (len < 0) {
|
|
|
|
BT_ERR("Failed to read ID address from storage"
|
2019-04-02 11:06:05 +02:00
|
|
|
" (err %zu)", len);
|
2018-11-24 16:32:41 +01:00
|
|
|
} else {
|
|
|
|
BT_ERR("Invalid length ID address in storage");
|
|
|
|
BT_HEXDUMP_DBG(&bt_dev.id_addr, len,
|
|
|
|
"data read");
|
|
|
|
}
|
2018-09-11 19:09:03 -07:00
|
|
|
(void)memset(bt_dev.id_addr, 0,
|
|
|
|
sizeof(bt_dev.id_addr));
|
2018-11-29 11:23:03 -08:00
|
|
|
bt_dev.id_count = 0U;
|
2018-06-04 20:56:51 +02:00
|
|
|
} else {
|
2018-07-04 12:58:10 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
bt_dev.id_count = len / sizeof(bt_dev.id_addr[0]);
|
|
|
|
for (i = 0; i < bt_dev.id_count; i++) {
|
2019-03-22 21:11:01 +02:00
|
|
|
BT_DBG("ID[%d] %s", i,
|
2018-07-04 12:58:10 +03:00
|
|
|
bt_addr_le_str(&bt_dev.id_addr[i]));
|
|
|
|
}
|
2018-06-04 20:56:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 22:05:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-31 19:57:08 +02:00
|
|
|
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
2019-06-14 18:23:39 +02:00
|
|
|
if (!strncmp(name, "name", len)) {
|
2019-04-02 11:06:05 +02:00
|
|
|
len = read_cb(cb_arg, &bt_dev.name, sizeof(bt_dev.name) - 1);
|
2018-11-24 16:32:41 +01:00
|
|
|
if (len < 0) {
|
|
|
|
BT_ERR("Failed to read device name from storage"
|
2019-04-02 11:06:05 +02:00
|
|
|
" (err %zu)", len);
|
2018-11-24 16:32:41 +01:00
|
|
|
} else {
|
|
|
|
bt_dev.name[len] = '\0';
|
2018-07-09 15:12:04 +03:00
|
|
|
|
2018-11-24 16:32:41 +01:00
|
|
|
BT_DBG("Name set to %s", bt_dev.name);
|
|
|
|
}
|
2018-07-09 15:12:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-31 19:57:08 +02:00
|
|
|
#endif
|
2018-07-09 15:12:04 +03:00
|
|
|
|
2018-04-26 22:05:26 +03:00
|
|
|
#if defined(CONFIG_BT_PRIVACY)
|
2019-06-14 18:23:39 +02:00
|
|
|
if (!strncmp(name, "irk", len)) {
|
2019-04-02 11:06:05 +02:00
|
|
|
len = read_cb(cb_arg, bt_dev.irk, sizeof(bt_dev.irk));
|
2018-07-04 12:58:10 +03:00
|
|
|
if (len < sizeof(bt_dev.irk[0])) {
|
2018-11-24 16:32:41 +01:00
|
|
|
if (len < 0) {
|
|
|
|
BT_ERR("Failed to read IRK from storage"
|
2019-04-02 11:06:05 +02:00
|
|
|
" (err %zu)", len);
|
2018-11-24 16:32:41 +01:00
|
|
|
} else {
|
|
|
|
BT_ERR("Invalid length IRK in storage");
|
|
|
|
(void)memset(bt_dev.irk, 0, sizeof(bt_dev.irk));
|
|
|
|
}
|
2018-06-04 20:56:51 +02:00
|
|
|
} else {
|
2019-03-22 21:11:01 +02:00
|
|
|
int i, count;
|
|
|
|
|
|
|
|
count = len / sizeof(bt_dev.irk[0]);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
BT_DBG("IRK[%d] %s", i,
|
|
|
|
bt_hex(bt_dev.irk[i], 16));
|
|
|
|
}
|
2018-06-04 20:56:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 22:05:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_BT_PRIVACY */
|
|
|
|
|
2019-04-11 15:15:57 +03:00
|
|
|
return -ENOENT;
|
2018-04-26 22:05:26 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 14:39:06 +03:00
|
|
|
#define ID_DATA_LEN(array) (bt_dev.id_count * sizeof(array[0]))
|
2018-04-26 22:05:26 +03:00
|
|
|
|
2018-07-16 14:39:06 +03:00
|
|
|
static void save_id(struct k_work *work)
|
|
|
|
{
|
2019-03-22 21:11:01 +02:00
|
|
|
int err;
|
2019-06-14 18:23:39 +02:00
|
|
|
BT_INFO("Saving ID");
|
2019-03-22 21:11:01 +02:00
|
|
|
err = settings_save_one("bt/id", &bt_dev.id_addr,
|
|
|
|
ID_DATA_LEN(bt_dev.id_addr));
|
|
|
|
if (err) {
|
|
|
|
BT_ERR("Failed to save ID (err %d)", err);
|
|
|
|
}
|
2018-04-26 22:05:26 +03:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_PRIVACY)
|
2019-03-22 21:11:01 +02:00
|
|
|
err = settings_save_one("bt/irk", bt_dev.irk, ID_DATA_LEN(bt_dev.irk));
|
|
|
|
if (err) {
|
|
|
|
BT_ERR("Failed to save IRK (err %d)", err);
|
|
|
|
}
|
2018-07-16 14:39:06 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
K_WORK_DEFINE(save_id_work, save_id);
|
|
|
|
|
|
|
|
void bt_settings_save_id(void)
|
|
|
|
{
|
|
|
|
k_work_submit(&save_id_work);
|
2018-04-26 22:05:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int commit(void)
|
|
|
|
{
|
|
|
|
BT_DBG("");
|
|
|
|
|
2018-07-31 19:57:08 +02:00
|
|
|
#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
|
2018-07-09 15:12:04 +03:00
|
|
|
if (bt_dev.name[0] == '\0') {
|
|
|
|
bt_set_name(CONFIG_BT_DEVICE_NAME);
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-16 14:39:06 +03:00
|
|
|
if (!bt_dev.id_count) {
|
|
|
|
int err;
|
2018-07-09 15:12:04 +03:00
|
|
|
|
2018-07-16 14:39:06 +03:00
|
|
|
err = bt_setup_id_addr();
|
|
|
|
if (err) {
|
|
|
|
BT_ERR("Unable to setup an identity address");
|
|
|
|
return err;
|
2018-04-26 22:05:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 16:55:47 +02:00
|
|
|
/* Make sure that the identities created by bt_id_create after
|
|
|
|
* bt_enable is saved to persistent storage. */
|
|
|
|
if (!atomic_test_bit(bt_dev.flags, BT_DEV_PRESET_ID)) {
|
|
|
|
bt_settings_save_id();
|
|
|
|
}
|
|
|
|
|
2019-03-22 12:45:09 +02:00
|
|
|
if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
|
|
|
|
bt_finalize_init();
|
|
|
|
}
|
|
|
|
|
2018-04-26 22:05:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:52:18 +02:00
|
|
|
SETTINGS_STATIC_HANDLER_DEFINE(bt, "bt", NULL, set, commit, NULL);
|
2018-04-26 22:05:26 +03:00
|
|
|
|
|
|
|
int bt_settings_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
BT_DBG("");
|
|
|
|
|
|
|
|
err = settings_subsys_init();
|
|
|
|
if (err) {
|
|
|
|
BT_ERR("settings_subsys_init failed (err %d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|