From bc6b65c7943aa7068dc3d7d0f8511a2c71eefb1d Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 9 Jan 2020 17:11:58 +0100 Subject: [PATCH] att_db_util: track number of bytes to include in database hash --- src/ble/att_db_util.c | 41 ++++++++++++++++++++++++++++++ src/ble/att_db_util.h | 5 ++++ test/att_db/att_db_util_test.c | 46 +++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/ble/att_db_util.c b/src/ble/att_db_util.c index de715cbd2..38ba2d9d0 100644 --- a/src/ble/att_db_util.c +++ b/src/ble/att_db_util.c @@ -60,6 +60,7 @@ static uint8_t * att_db; static uint16_t att_db_size; static uint16_t att_db_max_size; static uint16_t att_db_next_handle; +static uint16_t att_db_hash_len; static void att_db_util_set_end_tag(void){ // end tag @@ -79,9 +80,43 @@ void att_db_util_init(void){ att_db[0] = ATT_DB_VERSION; att_db_size = 1; att_db_next_handle = 1; + att_db_hash_len = 0; att_db_util_set_end_tag(); } +static bool att_db_util_hash_include_with_value(uint16_t uuid16){ + /* «Primary Service», «Secondary Service», «Included Service», «Characteristic», or «Characteristic Extended Properties» */ + switch (uuid16){ + case GATT_PRIMARY_SERVICE_UUID: + case GATT_SECONDARY_SERVICE_UUID: + case GATT_INCLUDE_SERVICE_UUID: + case GATT_CHARACTERISTICS_UUID: + case GATT_CHARACTERISTIC_EXTENDED_PROPERTIES: + return true; + default: + return false; + } +} + +static bool att_db_util_hash_include_without_value(uint16_t uuid16){ + /* «Characteristic User Description», «Client Characteristic Configuration», «Server Characteristic Configuration», + * «Characteristic Aggregate Format», «Characteristic Format» */ + switch (uuid16){ + case GATT_CHARACTERISTIC_USER_DESCRIPTION: + case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION: + case GATT_SERVER_CHARACTERISTICS_CONFIGURATION: + case GATT_CHARACTERISTIC_PRESENTATION_FORMAT: + case GATT_CHARACTERISTIC_AGGREGATE_FORMAT: + return true; + default: + return false; + } +} + +uint16_t att_db_util_hash_len(void){ + return att_db_hash_len; +} + /** * asserts that the requested amount of bytes can be stored in the att_db * @returns TRUE if space is available @@ -125,6 +160,12 @@ static void att_db_util_add_attribute_uuid16(uint16_t uuid16, uint16_t flags, ui (void)memcpy(&att_db[att_db_size], data, data_len); att_db_size += data_len; att_db_util_set_end_tag(); + + if (att_db_util_hash_include_with_value(uuid16)){ + att_db_hash_len += 4 + data_len; + } else if (att_db_util_hash_include_without_value(uuid16)){ + att_db_hash_len += 4; + } } static void att_db_util_add_attribute_uuid128(const uint8_t * uuid128, uint16_t flags, uint8_t * data, uint16_t data_len){ diff --git a/src/ble/att_db_util.h b/src/ble/att_db_util.h index 89afaa9d4..03117a2f0 100644 --- a/src/ble/att_db_util.h +++ b/src/ble/att_db_util.h @@ -161,6 +161,11 @@ uint8_t * att_db_util_get_address(void); */ uint16_t att_db_util_get_size(void); +/** + * @brief Get number of bytes that are included in GATT Database Hash + */ +uint16_t att_db_util_hash_len(void); + /* API_END */ #if defined __cplusplus diff --git a/test/att_db/att_db_util_test.c b/test/att_db/att_db_util_test.c index fd8f99a16..090aa4c27 100644 --- a/test/att_db/att_db_util_test.c +++ b/test/att_db/att_db_util_test.c @@ -62,6 +62,17 @@ uint8_t counter_service_uuid[] = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x0 // 0000FF11-0000-1000-8000-00805F9B34FB uint8_t counter_characteristic_uuid[] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; +// gatt database hash test message +static const uint8_t gatt_database_hash_test_message[] = { + 0x01, 0x00, 0x00, 0x28, 0x00, 0x18, 0x02, 0x00, 0x03, 0x28, 0x0A, 0x03, 0x00, 0x00, 0x2A, 0x04, + 0x00, 0x03, 0x28, 0x02, 0x05, 0x00, 0x01, 0x2A, 0x06, 0x00, 0x00, 0x28, 0x01, 0x18, 0x07, 0x00, + 0x03, 0x28, 0x20, 0x08, 0x00, 0x05, 0x2A, 0x09, 0x00, 0x02, 0x29, 0x0A, 0x00, 0x03, 0x28, 0x0A, + 0x0B, 0x00, 0x29, 0x2B, 0x0C, 0x00, 0x03, 0x28, 0x02, 0x0D, 0x00, 0x2A, 0x2B, 0x0E, 0x00, 0x00, + 0x28, 0x08, 0x18, 0x0F, 0x00, 0x02, 0x28, 0x14, 0x00, 0x16, 0x00, 0x0F, 0x18, 0x10, 0x00, 0x03, + 0x28, 0xA2, 0x11, 0x00, 0x18, 0x2A, 0x12, 0x00, 0x02, 0x29, 0x13, 0x00, 0x00, 0x29, 0x00, 0x00, + 0x14, 0x00, 0x01, 0x28, 0x0F, 0x18, 0x15, 0x00, 0x03, 0x28, 0x02, 0x16, 0x00, 0x19, 0x2A +}; + void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){ for (int i=0; i