From fd1be25d180b83fa8818fbefe35e2873a2c6906e Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Mon, 5 Mar 2018 15:03:32 +0100 Subject: [PATCH] att_db: add att db version as first byte, report error if version differs (also works for older db without version byte) --- src/ble/att_db.c | 6 ++++++ src/ble/att_db_util.c | 6 ++++-- src/btstack_defines.h | 1 + tool/compile_gatt.py | 12 +++++++++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ble/att_db.c b/src/ble/att_db.c index 979c90e51..101e6c08b 100644 --- a/src/ble/att_db.c +++ b/src/ble/att_db.c @@ -200,6 +200,12 @@ static int att_copy_value(att_iterator_t *it, uint16_t offset, uint8_t * buffer, } void att_set_db(uint8_t const * db){ + // validate db version + if (db == NULL) return; + if (*db++ != ATT_DB_VERSION){ + log_error("ATT DB version differs, please regenerate .h from .gatt file or update att_db_util.c"); + return; + } att_db = db; } diff --git a/src/ble/att_db_util.c b/src/ble/att_db_util.c index 81d84fa43..e3fbb2097 100644 --- a/src/ble/att_db_util.c +++ b/src/ble/att_db_util.c @@ -52,7 +52,7 @@ #ifdef MAX_ATT_DB_SIZE static uint8_t att_db_storage[MAX_ATT_DB_SIZE]; #else -#error Neither HAVE_MALLOC] nor MAX_ATT_DB_SIZE is defined. +#error Neither HAVE_MALLOC nor MAX_ATT_DB_SIZE is defined. #endif #endif @@ -75,7 +75,9 @@ void att_db_util_init(void){ att_db = att_db_storage; att_db_max_size = sizeof(att_db_storage); #endif - att_db_size = 0; + // store att version + att_db[0] = ATT_DB_VERSION; + att_db_size = 1; att_db_next_handle = 1; att_db_util_set_end_tag(); } diff --git a/src/btstack_defines.h b/src/btstack_defines.h index d049622ec..1c78e4d22 100644 --- a/src/btstack_defines.h +++ b/src/btstack_defines.h @@ -301,6 +301,7 @@ typedef uint8_t sm_key_t[16]; // .. // Internal properties reuse some GATT Characteristic Properties fields +#define ATT_DB_VERSION 0x01 // EVENTS diff --git a/tool/compile_gatt.py b/tool/compile_gatt.py index c3a788f63..8f4e1f5fe 100755 --- a/tool/compile_gatt.py +++ b/tool/compile_gatt.py @@ -17,9 +17,10 @@ import sys header = ''' // {0} generated from {1} for BTstack +// att db format version 1 -// binary representation -// attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...) +// binary attribute representation: +// - size in bytes (16), flags(16), handle (16), uuid (16/128), value(...) #include @@ -825,7 +826,12 @@ def parse(fname_in, fin, fname_out, fout): fout.write(header.format(fname_out, fname_in)) fout.write('{\n') - + write_indent(fout) + fout.write('// ATT DB Version\n') + write_indent(fout) + fout.write('1,\n') + fout.write("\n") + parseLines(fname_in, fin, fout) serviceDefinitionComplete(fout)