diff --git a/net/bluetooth/gatt.c b/net/bluetooth/gatt.c index 7c952c5694e..556db84c4f6 100644 --- a/net/bluetooth/gatt.c +++ b/net/bluetooth/gatt.c @@ -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, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset) { struct _bt_gatt_ccc *ccc = attr->user_data; const uint16_t *data = buf; - bool bonded; size_t i; 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); } - if (bt_keys_find_addr(&conn->le.dst)) - bonded = true; - else - bonded = false; - for (i = 0; i < ccc->cfg_len; i++) { /* Check for existing configuration */ 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) { bt_addr_le_copy(&ccc->cfg[i].peer, &conn->le.dst); /* Only set valid if bonded */ - ccc->cfg[i].valid = bonded; + ccc->cfg[i].valid = is_bonded(&conn->le.dst); break; } } @@ -1624,8 +1630,8 @@ void bt_gatt_disconnected(struct bt_conn *conn) bt_gatt_foreach_attr(0x0001, 0xffff, disconnected_cb, conn); #if defined(CONFIG_BLUETOOTH_GATT_CLIENT) - /* If paired don't remove subscriptions */ - if (bt_keys_find_addr(&conn->le.dst)) { + /* If bonded don't remove subscriptions */ + if (is_bonded(&conn->le.dst)) { return; } diff --git a/net/bluetooth/keys.h b/net/bluetooth/keys.h index bae089d1c4a..4519919b091 100644 --- a/net/bluetooth/keys.h +++ b/net/bluetooth/keys.h @@ -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_irk(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 */