Bluetooth: GATT: Fix check if device is bonded

Keys storage is present also for non bonded devices. Add helper
that checks for actual keys being present in storage that may be
used inline.

Change-Id: Icfc758e3ac89e88ca48948e5193878cf5689611a
Signed-off-by: Szymon Janc <ext.szymon.janc@tieto.com>
This commit is contained in:
Szymon Janc 2016-02-18 13:08:34 +01:00
commit c1c21965d6
2 changed files with 15 additions and 14 deletions

View file

@ -306,13 +306,24 @@ static void gatt_ccc_changed(struct _bt_gatt_ccc *ccc)
} }
} }
static bool is_bonded(const bt_addr_le_t *addr)
{
#if defined(CONFIG_BLUETOOTH_SMP)
struct bt_keys *keys = bt_keys_find_addr(addr);
/* if there are any keys stored then device is bonded */
return keys && keys->keys;
#else
return false;
#endif /* defined(CONFIG_BLUETOOTH_SMP) */
}
int bt_gatt_attr_write_ccc(struct bt_conn *conn, int bt_gatt_attr_write_ccc(struct bt_conn *conn,
const struct bt_gatt_attr *attr, const void *buf, const struct bt_gatt_attr *attr, const void *buf,
uint16_t len, uint16_t offset) uint16_t len, uint16_t offset)
{ {
struct _bt_gatt_ccc *ccc = attr->user_data; struct _bt_gatt_ccc *ccc = attr->user_data;
const uint16_t *data = buf; const uint16_t *data = buf;
bool bonded;
size_t i; size_t i;
if (offset > sizeof(*data)) { if (offset > sizeof(*data)) {
@ -323,11 +334,6 @@ int bt_gatt_attr_write_ccc(struct bt_conn *conn,
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
} }
if (bt_keys_find_addr(&conn->le.dst))
bonded = true;
else
bonded = false;
for (i = 0; i < ccc->cfg_len; i++) { for (i = 0; i < ccc->cfg_len; i++) {
/* Check for existing configuration */ /* Check for existing configuration */
if (!bt_addr_le_cmp(&ccc->cfg[i].peer, &conn->le.dst)) { if (!bt_addr_le_cmp(&ccc->cfg[i].peer, &conn->le.dst)) {
@ -341,7 +347,7 @@ int bt_gatt_attr_write_ccc(struct bt_conn *conn,
if (!ccc->cfg[i].valid) { if (!ccc->cfg[i].valid) {
bt_addr_le_copy(&ccc->cfg[i].peer, &conn->le.dst); bt_addr_le_copy(&ccc->cfg[i].peer, &conn->le.dst);
/* Only set valid if bonded */ /* Only set valid if bonded */
ccc->cfg[i].valid = bonded; ccc->cfg[i].valid = is_bonded(&conn->le.dst);
break; break;
} }
} }
@ -1624,8 +1630,8 @@ void bt_gatt_disconnected(struct bt_conn *conn)
bt_gatt_foreach_attr(0x0001, 0xffff, disconnected_cb, conn); bt_gatt_foreach_attr(0x0001, 0xffff, disconnected_cb, conn);
#if defined(CONFIG_BLUETOOTH_GATT_CLIENT) #if defined(CONFIG_BLUETOOTH_GATT_CLIENT)
/* If paired don't remove subscriptions */ /* If bonded don't remove subscriptions */
if (bt_keys_find_addr(&conn->le.dst)) { if (is_bonded(&conn->le.dst)) {
return; return;
} }

View file

@ -96,9 +96,4 @@ void bt_keys_clear(struct bt_keys *keys, int type);
struct bt_keys *bt_keys_find(int type, const bt_addr_le_t *addr); struct bt_keys *bt_keys_find(int type, const bt_addr_le_t *addr);
struct bt_keys *bt_keys_find_irk(const bt_addr_le_t *addr); struct bt_keys *bt_keys_find_irk(const bt_addr_le_t *addr);
struct bt_keys *bt_keys_find_addr(const bt_addr_le_t *addr); struct bt_keys *bt_keys_find_addr(const bt_addr_le_t *addr);
#else
static inline struct bt_keys *bt_keys_find_addr(const bt_addr_le_t *addr)
{
return NULL;
}
#endif /* CONFIG_BLUETOOTH_SMP || CONFIG_BLUETOOTH_BREDR */ #endif /* CONFIG_BLUETOOTH_SMP || CONFIG_BLUETOOTH_BREDR */