Skip to content

Commit b60b0f3

Browse files
jori-nordiccarlescufi
authored andcommitted
Bluetooth: host: don't overwrite GATT DB hash before settings_load
Previously, if the app registered a bunch of services at boot before calling `settings_load()`, then the hash would be silently overwritten. Signed-off-by: Jonathan Rico <[email protected]>
1 parent e8f3ef2 commit b60b0f3

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

subsys/bluetooth/host/gatt.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ enum {
281281
#if defined(CONFIG_BT_GATT_CACHING)
282282
DB_HASH_VALID, /* Database hash needs to be calculated */
283283
DB_HASH_LOAD, /* Database hash loaded from settings. */
284+
DB_HASH_LOAD_PROC, /* DB hash loaded from settings has been processed. */
284285
#endif
285286
/* Total number of flags - must be at the end of the enum */
286287
SC_NUM_FLAGS,
@@ -879,7 +880,28 @@ static void sc_indicate(uint16_t start, uint16_t end);
879880
static void db_hash_process(struct k_work *work)
880881
{
881882
#if defined(CONFIG_BT_SETTINGS)
882-
if (atomic_test_and_clear_bit(gatt_sc.flags, DB_HASH_LOAD)) {
883+
bool hash_loaded_from_settings =
884+
atomic_test_bit(gatt_sc.flags, DB_HASH_LOAD);
885+
bool already_processed =
886+
atomic_test_bit(gatt_sc.flags, DB_HASH_LOAD_PROC);
887+
888+
if (!hash_loaded_from_settings) {
889+
/* we want to generate the hash, but not overwrite the hash
890+
* stored in settings, that we haven't yet loaded.
891+
*/
892+
db_hash_gen(false);
893+
} else if (already_processed) {
894+
/* hash has been loaded from settings and we have already
895+
* executed the special case below once. we can now safely save
896+
* the calculated hash to settings.
897+
*/
898+
db_hash_gen(true);
899+
} else {
900+
/* this is only supposed to run once, on bootup, after the hash
901+
* has been loaded from settings.
902+
*/
903+
atomic_set_bit(gatt_sc.flags, DB_HASH_LOAD_PROC);
904+
883905
if (!atomic_test_bit(gatt_sc.flags, DB_HASH_VALID)) {
884906
db_hash_gen(false);
885907
}
@@ -910,10 +932,10 @@ static void db_hash_process(struct k_work *work)
910932
*/
911933
set_all_change_unaware();
912934
db_hash_store();
913-
return;
914935
}
915-
#endif /* defined(CONFIG_BT_SETTINGS) */
936+
#else
916937
db_hash_gen(true);
938+
#endif /* defined(CONFIG_BT_SETTINGS) */
917939
}
918940

919941
static ssize_t db_hash_read(struct bt_conn *conn,

0 commit comments

Comments
 (0)