Skip to content

Commit

Permalink
Bluetooth: GATT: Fix not clearing Client Features
Browse files Browse the repository at this point in the history
When a device is considered unpaired any configuration set in Client
Features shall also be removed.

Fixes #15329

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
  • Loading branch information
Vudentz committed Apr 11, 2019
1 parent 686f5c7 commit c309cfc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
69 changes: 55 additions & 14 deletions subsys/bluetooth/host/gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,7 @@ static u8_t remove_peer_from_attr(const struct bt_gatt_attr *attr,
return BT_GATT_ITER_CONTINUE;
}

int bt_gatt_clear_ccc(u8_t id, const bt_addr_le_t *addr)
static int bt_gatt_clear_ccc(u8_t id, const bt_addr_le_t *addr)
{
char key[BT_SETTINGS_KEY_MAX];

Expand All @@ -3124,6 +3124,60 @@ int bt_gatt_clear_ccc(u8_t id, const bt_addr_le_t *addr)
return settings_delete(key);
}

#if defined(CONFIG_BT_GATT_CACHING)
static struct gatt_cf_cfg *find_cf_cfg_by_addr(const bt_addr_le_t *addr)
{
int i;

for (i = 0; i < ARRAY_SIZE(cf_cfg); i++) {
if (!bt_addr_le_cmp(addr, &cf_cfg[i].peer)) {
return &cf_cfg[i];
}
}

return NULL;
}
#endif /* CONFIG_BT_GATT_CACHING */

static int bt_gatt_clear_cf(u8_t id, const bt_addr_le_t *addr)
{
#if defined(CONFIG_BT_GATT_CACHING)
char key[BT_SETTINGS_KEY_MAX];
struct gatt_cf_cfg *cfg;

if (id) {
char id_str[4];

snprintk(id_str, sizeof(id_str), "%u", id);
bt_settings_encode_key(key, sizeof(key), "cf",
(bt_addr_le_t *)addr, id_str);
} else {
bt_settings_encode_key(key, sizeof(key), "cf",
(bt_addr_le_t *)addr, NULL);
}

cfg = find_cf_cfg_by_addr(addr);
if (cfg) {
clear_cf_cfg(cfg);
}

return settings_delete(key);
#endif /* CONFIG_BT_GATT_CACHING */
return 0;
}

int bt_gatt_clear(u8_t id, const bt_addr_le_t *addr)
{
int err;

err = bt_gatt_clear_ccc(id, addr);
if (err < 0) {
return err;
}

return bt_gatt_clear_cf(id, addr);
}

static void ccc_clear(struct _bt_gatt_ccc *ccc, bt_addr_le_t *addr)
{
struct bt_gatt_ccc_cfg *cfg;
Expand Down Expand Up @@ -3254,19 +3308,6 @@ static int ccc_set(int argc, char **argv, void *val_ctx)
BT_SETTINGS_DEFINE(ccc, ccc_set, NULL, NULL);

#if defined(CONFIG_BT_GATT_CACHING)
static struct gatt_cf_cfg *find_cf_cfg_by_addr(const bt_addr_le_t *addr)
{
int i;

for (i = 0; i < ARRAY_SIZE(cf_cfg); i++) {
if (!bt_addr_le_cmp(addr, &cf_cfg[i].peer)) {
return &cf_cfg[i];
}
}

return NULL;
}

static int cf_set(int argc, char **argv, void *val_ctx)
{
struct gatt_cf_cfg *cfg;
Expand Down
3 changes: 2 additions & 1 deletion subsys/bluetooth/host/gatt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ void bt_gatt_disconnected(struct bt_conn *conn);
bool bt_gatt_change_aware(struct bt_conn *conn, bool req);

int bt_gatt_store_ccc(u8_t id, const bt_addr_le_t *addr);
int bt_gatt_clear_ccc(u8_t id, const bt_addr_le_t *addr);

int bt_gatt_clear(u8_t id, const bt_addr_le_t *addr);

#if defined(CONFIG_BT_GATT_CLIENT)
void bt_gatt_notification(struct bt_conn *conn, u16_t handle,
Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ int bt_unpair(u8_t id, const bt_addr_le_t *addr)
}

if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
bt_gatt_clear_ccc(id, addr);
bt_gatt_clear(id, addr);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/host/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ static void keys_clear_id(struct bt_keys *keys, void *data)

if (*id == keys->id) {
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
bt_gatt_clear_ccc(*id, &keys->addr);
bt_gatt_clear(*id, &keys->addr);
}

bt_keys_clear(keys);
Expand Down

0 comments on commit c309cfc

Please sign in to comment.