Bluetooth: Introduce bt_id_delete() API

This API makes it possible to delete an existing identity and to flag
its storage slot as unused.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2018-08-02 11:25:57 +03:00 committed by Johan Hedberg
commit 346f815d9e
2 changed files with 68 additions and 6 deletions

View file

@ -105,6 +105,9 @@ int bt_set_id_addr(const bt_addr_le_t *addr);
* identifier that some APIs expect (such as advertising parameters) is
* simply the index of the identity in the @a addrs array.
*
* Note: Deleted identities may show up as BT_LE_ADDR_ANY in the returned
* array.
*
* @param addrs Array where to store the configured identities.
* @param count Should be initialized to the array size. Once the function
* returns it will contain the number of returned identies.
@ -183,6 +186,23 @@ int bt_id_create(bt_addr_le_t *addr, u8_t *irk);
*/
int bt_id_reset(u8_t id, bt_addr_le_t *addr, u8_t *irk);
/** @brief Delete an identity.
*
* When given a valid identity this function will disconnect any connections
* created using it, remove any pairing keys or other data associated with
* it, and then flag is as deleted, so that it can not be used for any
* operations. To take back into use the slot the identity was occupying the
* bt_id_reset() API needs to be used.
*
* Note: the default identity (BT_ID_DEFAULT) cannot be deleted, i.e. this
* API will return an error if asked to do that.
*
* @param id Existing identity identifier.
*
* @return 0 in case of success, or a negative error code on failure.
*/
int bt_id_delete(u8_t id);
/* Advertising API */
/** Description of different data types that can be encoded into

View file

@ -4924,8 +4924,6 @@ int bt_id_create(bt_addr_le_t *addr, u8_t *irk)
int bt_id_reset(u8_t id, bt_addr_le_t *addr, u8_t *irk)
{
int err;
if (addr && bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
if (addr->type != BT_ADDR_LE_RANDOM ||
!BT_ADDR_IS_STATIC(&addr->a)) {
@ -4951,9 +4949,13 @@ int bt_id_reset(u8_t id, bt_addr_le_t *addr, u8_t *irk)
return -EBUSY;
}
err = bt_unpair(id, NULL);
if (err) {
return err;
if (bt_addr_le_cmp(&bt_dev.id_addr[id], BT_ADDR_LE_ANY)) {
int err;
err = bt_unpair(id, NULL);
if (err) {
return err;
}
}
id_create(id, addr, irk);
@ -4961,6 +4963,45 @@ int bt_id_reset(u8_t id, bt_addr_le_t *addr, u8_t *irk)
return id;
}
int bt_id_delete(u8_t id)
{
int err;
if (id == BT_ID_DEFAULT || id >= bt_dev.id_count) {
return -EINVAL;
}
if (!bt_addr_le_cmp(&bt_dev.id_addr[id], BT_ADDR_LE_ANY)) {
return -EALREADY;
}
if (id == bt_dev.adv_id && atomic_test_bit(bt_dev.flags,
BT_DEV_ADVERTISING)) {
return -EBUSY;
}
err = bt_unpair(id, NULL);
if (err) {
return err;
}
#if defined(CONFIG_BT_PRIVACY)
memset(bt_dev.irk[id], 0, 16);
#endif
bt_addr_le_copy(&bt_dev.id_addr[id], BT_ADDR_LE_ANY);
if (id == bt_dev.id_count - 1) {
bt_dev.id_count--;
}
if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
bt_settings_save_id();
}
return 0;
}
bool bt_addr_le_is_bonded(u8_t id, const bt_addr_le_t *addr)
{
if (IS_ENABLED(CONFIG_BT_SMP)) {
@ -4975,7 +5016,8 @@ bool bt_addr_le_is_bonded(u8_t id, const bt_addr_le_t *addr)
static bool valid_adv_param(const struct bt_le_adv_param *param)
{
if (param->id >= bt_dev.id_count) {
if (param->id >= bt_dev.id_count ||
!bt_addr_le_cmp(&bt_dev.id_addr[param->id], BT_ADDR_LE_ANY)) {
return false;
}