From c9d0ff86747ba9fd02cb47bf5f98346f5abcc48e Mon Sep 17 00:00:00 2001 From: Paul Szczepanek Date: Wed, 23 Jun 2021 22:05:20 +0100 Subject: [PATCH] ble security db complete sync implementation --- .../source/generic/FileSecurityDb.cpp | 16 ++++++++++++++++ .../source/generic/KVStoreSecurityDb.cpp | 16 ++++++++++++++++ .../FEATURE_BLE/source/generic/SecurityDb.h | 6 +++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/connectivity/FEATURE_BLE/source/generic/FileSecurityDb.cpp b/connectivity/FEATURE_BLE/source/generic/FileSecurityDb.cpp index 6ad4fd45b00..09f77638d09 100644 --- a/connectivity/FEATURE_BLE/source/generic/FileSecurityDb.cpp +++ b/connectivity/FEATURE_BLE/source/generic/FileSecurityDb.cpp @@ -361,6 +361,22 @@ void FileSecurityDb::restore() void FileSecurityDb::sync(entry_handle_t db_handle) { + /* if no entry is selected we will sync all entries */ + if (db_handle == invalid_entry_handle) { + /* only write the connected devices as others are already written */ + for (size_t i = 0; i < get_entry_count(); i++) { + entry_handle_t db_handle = get_entry_handle_by_index(i); + SecurityDistributionFlags_t* flags = get_distribution_flags(db_handle); + + if (flags && flags->connected) { + sync(db_handle); + } + } + /* global sync triggers a flush */ + fflush(_db_file); + return; + } + entry_t *entry = as_entry(db_handle); if (!entry) { return; diff --git a/connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.cpp b/connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.cpp index 7a186203425..011ecfbfb00 100644 --- a/connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.cpp +++ b/connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.cpp @@ -333,6 +333,22 @@ void KVStoreSecurityDb::restore() void KVStoreSecurityDb::sync(entry_handle_t db_handle) { + /* storage synchronisation is handled by KVStore itself, no explicit flushing */ + + /* if no entry is selected we will sync all entries */ + if (db_handle == invalid_entry_handle) { + /* only write the connected devices as others are already written */ + for (size_t i = 0; i < get_entry_count(); i++) { + entry_handle_t db_handle = get_entry_handle_by_index(i); + SecurityDistributionFlags_t* flags = get_distribution_flags(db_handle); + + if (flags && flags->connected) { + sync(db_handle); + } + } + return; + } + entry_t *entry = as_entry(db_handle); if (!entry) { return; diff --git a/connectivity/FEATURE_BLE/source/generic/SecurityDb.h b/connectivity/FEATURE_BLE/source/generic/SecurityDb.h index eec3b7d7b88..4f7ae5060b0 100644 --- a/connectivity/FEATURE_BLE/source/generic/SecurityDb.h +++ b/connectivity/FEATURE_BLE/source/generic/SecurityDb.h @@ -522,7 +522,11 @@ class SecurityDb { virtual void restore(); /** - * Flush all values which might be stored in memory into NVM. + * Write all values and attempt to sync persistent storage. Passing in an optional valid + * db_handle will only write the given entry and not attempt to flush buffers. + * + * @param db_handle database entry to write. If invalid all entries are written and + * persistent storage attempts to sync (flush buffers). */ virtual void sync(entry_handle_t db_handle = invalid_entry_handle);