Bluetooth: Add support for reading local address from storage
If the local controller doesn't have a public address we need to generate a static random address and use that as our Identity Address. Change-Id: I3db261b630c670d285c6bfacbe090184cccb5e8c Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
parent
7e7eee0fb4
commit
0537c2c579
1 changed files with 63 additions and 0 deletions
|
@ -2904,6 +2904,60 @@ static int set_event_mask(void)
|
|||
return bt_hci_cmd_send_sync(BT_HCI_OP_SET_EVENT_MASK, buf, NULL);
|
||||
}
|
||||
|
||||
static int set_static_addr(void)
|
||||
{
|
||||
struct net_buf *buf;
|
||||
ssize_t err;
|
||||
|
||||
if (storage) {
|
||||
err = storage->read(NULL, BT_STORAGE_ID_ADDR, &bt_dev.id_addr,
|
||||
sizeof(bt_dev.id_addr));
|
||||
if (err == sizeof(bt_dev.id_addr)) {
|
||||
goto set_addr;
|
||||
}
|
||||
}
|
||||
|
||||
BT_DBG("Generating new static random address");
|
||||
|
||||
bt_dev.id_addr.type = BT_ADDR_LE_RANDOM;
|
||||
|
||||
err = bt_rand(bt_dev.id_addr.val, 6);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Make sure the address bits indicate static address */
|
||||
bt_dev.id_addr.val[5] |= 0xc0;
|
||||
|
||||
if (storage) {
|
||||
err = storage->write(NULL, BT_STORAGE_ID_ADDR, &bt_dev.id_addr,
|
||||
sizeof(bt_dev.id_addr));
|
||||
if (err != sizeof(bt_dev.id_addr)) {
|
||||
BT_ERR("Unable to store static address");
|
||||
}
|
||||
} else {
|
||||
BT_WARN("Using temporary static random address");
|
||||
}
|
||||
|
||||
set_addr:
|
||||
if (bt_dev.id_addr.type != BT_ADDR_LE_RANDOM ||
|
||||
(bt_dev.id_addr.val[5] & 0xc0) != 0xc0) {
|
||||
BT_ERR("Only static random address supported as identity");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS,
|
||||
sizeof(bt_dev.id_addr.val));
|
||||
if (!buf) {
|
||||
return -ENOBUFS;
|
||||
}
|
||||
|
||||
memcpy(net_buf_add(buf, sizeof(bt_dev.id_addr.val)),
|
||||
bt_dev.id_addr.val, sizeof(bt_dev.id_addr.val));
|
||||
|
||||
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL);
|
||||
}
|
||||
|
||||
static int hci_init(void)
|
||||
{
|
||||
int err;
|
||||
|
@ -2932,6 +2986,15 @@ static int hci_init(void)
|
|||
return err;
|
||||
}
|
||||
|
||||
if (!bt_addr_le_cmp(&bt_dev.id_addr, BT_ADDR_LE_ANY)) {
|
||||
BT_DBG("No public address. Trying to set static random.");
|
||||
err = set_static_addr();
|
||||
if (err) {
|
||||
BT_ERR("Unable to set identity address");
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
BT_DBG("HCI ver %u rev %u, manufacturer %u", bt_dev.hci_version,
|
||||
bt_dev.hci_revision, bt_dev.manufacturer);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue