Skip to content

Commit 2a15a63

Browse files
jori-nordiccarlescufi
authored andcommitted
Bluetooth: host: don't store hash in db_hash_gen()
Move out the storage of the calculated hash from the fn that calculates it (db_hash_gen). Signed-off-by: Jonathan Rico <[email protected]>
1 parent b60b0f3 commit 2a15a63

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

subsys/bluetooth/host/gatt.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ static uint8_t gen_hash_m(const struct bt_gatt_attr *attr, uint16_t handle,
826826

827827
static void db_hash_store(void)
828828
{
829+
#if defined(CONFIG_BT_SETTINGS)
829830
int err;
830831

831832
err = settings_save_one("bt/hash", &db_hash.hash, sizeof(db_hash.hash));
@@ -834,9 +835,10 @@ static void db_hash_store(void)
834835
}
835836

836837
LOG_DBG("Database Hash stored");
838+
#endif /* CONFIG_BT_SETTINGS */
837839
}
838840

839-
static void db_hash_gen(bool store)
841+
static void db_hash_gen(void)
840842
{
841843
uint8_t key[16] = {};
842844
struct tc_aes_key_sched_struct sched;
@@ -865,11 +867,6 @@ static void db_hash_gen(bool store)
865867

866868
LOG_HEXDUMP_DBG(db_hash.hash, sizeof(db_hash.hash), "Hash: ");
867869

868-
if (IS_ENABLED(CONFIG_BT_SETTINGS) && store) {
869-
set_all_change_unaware();
870-
db_hash_store();
871-
}
872-
873870
atomic_set_bit(gatt_sc.flags, DB_HASH_VALID);
874871
}
875872

@@ -879,6 +876,10 @@ static void sc_indicate(uint16_t start, uint16_t end);
879876

880877
static void db_hash_process(struct k_work *work)
881878
{
879+
if (!atomic_test_bit(gatt_sc.flags, DB_HASH_VALID)) {
880+
db_hash_gen();
881+
}
882+
882883
#if defined(CONFIG_BT_SETTINGS)
883884
bool hash_loaded_from_settings =
884885
atomic_test_bit(gatt_sc.flags, DB_HASH_LOAD);
@@ -889,23 +890,22 @@ static void db_hash_process(struct k_work *work)
889890
/* we want to generate the hash, but not overwrite the hash
890891
* stored in settings, that we haven't yet loaded.
891892
*/
892-
db_hash_gen(false);
893-
} else if (already_processed) {
893+
return;
894+
}
895+
896+
if (!already_processed) {
894897
/* hash has been loaded from settings and we have already
895898
* executed the special case below once. we can now safely save
896899
* the calculated hash to settings.
897900
*/
898-
db_hash_gen(true);
901+
set_all_change_unaware();
902+
db_hash_store();
899903
} else {
900904
/* this is only supposed to run once, on bootup, after the hash
901905
* has been loaded from settings.
902906
*/
903907
atomic_set_bit(gatt_sc.flags, DB_HASH_LOAD_PROC);
904908

905-
if (!atomic_test_bit(gatt_sc.flags, DB_HASH_VALID)) {
906-
db_hash_gen(false);
907-
}
908-
909909
/* Check if hash matches then skip SC update */
910910
if (!memcmp(db_hash.stored_hash, db_hash.hash,
911911
sizeof(db_hash.stored_hash))) {
@@ -933,8 +933,6 @@ static void db_hash_process(struct k_work *work)
933933
set_all_change_unaware();
934934
db_hash_store();
935935
}
936-
#else
937-
db_hash_gen(true);
938936
#endif /* defined(CONFIG_BT_SETTINGS) */
939937
}
940938

@@ -949,7 +947,11 @@ static ssize_t db_hash_read(struct bt_conn *conn,
949947
*/
950948
(void)k_work_cancel_delayable_sync(&db_hash.work, &db_hash.sync);
951949
if (!atomic_test_bit(gatt_sc.flags, DB_HASH_VALID)) {
952-
db_hash_gen(true);
950+
db_hash_gen();
951+
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
952+
set_all_change_unaware();
953+
db_hash_store();
954+
}
953955
}
954956

955957
/* BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2347:

0 commit comments

Comments
 (0)