From 00b4f875b0ef4fc0e1c685f1839a45545164ce03 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 14 Mar 2025 12:37:01 +0100 Subject: [PATCH] att_db: add gatt_server_get_database_hash --- src/ble/att_db.c | 23 +++++++++++++++++++++++ src/ble/att_db.h | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/src/ble/att_db.c b/src/ble/att_db.c index ae85bbc3f..6c34814f8 100644 --- a/src/ble/att_db.c +++ b/src/ble/att_db.c @@ -42,6 +42,7 @@ #include "ble/att_db.h" #include "ble/core.h" #include "bluetooth.h" +#include "bluetooth_gatt.h" #include "btstack_debug.h" #include "btstack_util.h" @@ -1644,6 +1645,28 @@ bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_ return false; } +bool gatt_server_get_database_hash(uint8_t * hash) { + att_iterator_t it; + att_iterator_init(&it); + uint16_t value_handle = 0xffff; + while (att_iterator_has_next(&it)){ + att_iterator_fetch_next(&it); + // Find Characteristic declaration for Database Hash + if ((it.value_len == 5u) && (att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID))){ + uint16_t characteristic_uuid = little_endian_read_16(it.value, 3); + if (characteristic_uuid == ORG_BLUETOOTH_CHARACTERISTIC_DATABASE_HASH){ + value_handle = little_endian_read_16(it.value, 1); + } + } + // Find Characteristic Value for Database Hash and get value + if ((it.handle == value_handle) && (it.value_len == 16u)){ + att_copy_value(&it, 0, hash, 16, HCI_CON_HANDLE_INVALID); + return true; + } + } + return false; +} + // 1-item cache to optimize query during write_callback static void att_persistent_ccc_cache(att_iterator_t * it){ att_persistent_ccc_handle = it->handle; diff --git a/src/ble/att_db.h b/src/ble/att_db.h index fa6d1545d..a96ab0241 100644 --- a/src/ble/att_db.h +++ b/src/ble/att_db.h @@ -420,6 +420,14 @@ uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t s */ uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); +/** + * @brief Get Database Hash provided via GATT Database Hash characteristic + * @note Used by att_server to discard stored CCCD values for bonded devices if hash has changed + * @param hash + * @return true if hash is available + */ +bool gatt_server_get_database_hash(uint8_t * hash); + /* API_END */ // non-user functionality for att_server