Skip to content

Commit 7b4d1d5

Browse files
add new BLE API call to sync security db with persisten storage
1 parent cecc47b commit 7b4d1d5

File tree

9 files changed

+45
-3
lines changed

9 files changed

+45
-3
lines changed

UNITTESTS/fakes/ble/SecurityManagerImpl_mock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class SecurityManagerMock : public ble::impl::SecurityManager {
3434
MOCK_METHOD(ble_error_t, init, (bool enableBonding, bool requireMITM, SecurityIOCapabilities_t iocaps, const Passkey_t passkey, bool signing, const char *dbFilepath), (override));
3535
MOCK_METHOD(ble_error_t, setDatabaseFilepath, (const char *dbFilepath), (override));
3636
MOCK_METHOD(ble_error_t, preserveBondingStateOnReset, (bool enable), (override));
37+
MOCK_METHOD(ble_error_t, writeBondingStateToPersistentStorage, (), (override));
3738
MOCK_METHOD(ble_error_t, purgeAllBondingState, (), (override));
3839
MOCK_METHOD(ble_error_t, generateWhitelistFromBondTable, (::ble::whitelist_t *whitelist), (const, override));
3940
MOCK_METHOD(ble_error_t, requestPairing, (ble::connection_handle_t connectionHandle), (override));

UNITTESTS/fakes/ble/source/generic/SecurityManagerImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class SecurityManager {
5252

5353
virtual ble_error_t preserveBondingStateOnReset(bool enable) { return BLE_ERROR_NONE; };
5454

55+
virtual ble_error_t writeBondingStateToPersistentStorage() { return BLE_ERROR_NONE; };
56+
5557
////////////////////////////////////////////////////////////////////////////
5658
// List management
5759
//

connectivity/FEATURE_BLE/include/ble/SecurityManager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,29 @@ class SecurityManager
501501
* Normally all bonding information is lost when device is reset, this requests that the stack
502502
* attempts to save the information and reload it during initialisation. This is not guaranteed.
503503
*
504+
* @note This option is itself saved together with bonding data. When data is read after reset,
505+
* the state of this option decides if data should be restored. If this option has not been saved
506+
* the data will not be restored even if partial data is present.
507+
*
504508
* @param[in] enable if true the stack will attempt to preserve bonding information on reset.
505509
* @return BLE_ERROR_NONE or appropriate error code indicating the failure reason.
506510
*/
507511
ble_error_t preserveBondingStateOnReset(bool enable);
508512

513+
/**
514+
* Some or all of bonding information may be stored in memory while in use. This will write
515+
* bonding data to persistent storage. This will have no effect if no persistent storage is enabled.
516+
*
517+
* @note You still need to call preserveBondingStateOnReset(true) before reset happens for data to be
518+
* loaded when it's read.
519+
*
520+
* @note Depending on the driver used to implement the storage solution used this may be a disruptive
521+
* operation and may cause active connections to drop due to failed processing deadlines.
522+
*
523+
* @return BLE_ERROR_NONE or appropriate error code indicating the failure reason.
524+
*/
525+
ble_error_t writeBondingStateToPersistentStorage();
526+
509527
////////////////////////////////////////////////////////////////////////////
510528
// List management
511529
//

connectivity/FEATURE_BLE/source/SecurityManager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ ble_error_t SecurityManager::preserveBondingStateOnReset(bool enable)
4747
return impl->preserveBondingStateOnReset(enable);
4848
}
4949

50+
ble_error_t SecurityManager::writeBondingStateToPersistentStorage()
51+
{
52+
return impl->writeBondingStateToPersistentStorage();
53+
}
54+
5055
ble_error_t SecurityManager::purgeAllBondingState()
5156
{
5257
return impl->purgeAllBondingState();

connectivity/FEATURE_BLE/source/generic/FileSecurityDb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class FileSecurityDb : public SecurityDb {
136136

137137
virtual void restore();
138138

139-
virtual void sync(entry_handle_t db_handle);
139+
virtual void sync(entry_handle_t db_handle = invalid_entry_handle);
140140

141141
virtual void set_restore(bool reload);
142142

connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class KVStoreSecurityDb : public SecurityDb {
200200

201201
virtual void restore();
202202

203-
virtual void sync(entry_handle_t db_handle);
203+
virtual void sync(entry_handle_t db_handle = invalid_entry_handle);
204204

205205
virtual void set_restore(bool reload);
206206

connectivity/FEATURE_BLE/source/generic/SecurityDb.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class SecurityDb {
113113
*/
114114
typedef void* entry_handle_t;
115115

116+
static constexpr entry_handle_t invalid_entry_handle = nullptr;
117+
116118
/* callbacks for asynchronous data retrieval from the security db */
117119

118120
typedef mbed::Callback<void(entry_handle_t, const SecurityEntryKeys_t*)>
@@ -522,7 +524,7 @@ class SecurityDb {
522524
/**
523525
* Flush all values which might be stored in memory into NVM.
524526
*/
525-
virtual void sync(entry_handle_t db_handle);
527+
virtual void sync(entry_handle_t db_handle = invalid_entry_handle);
526528

527529
/**
528530
* Toggle whether values should be preserved across resets.

connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ ble_error_t SecurityManager::preserveBondingStateOnReset(bool enabled)
222222
return BLE_ERROR_NONE;
223223
}
224224

225+
226+
ble_error_t SecurityManager::writeBondingStateToPersistentStorage()
227+
{
228+
tr_info("Writing bonding to storage");
229+
if (!_db) {
230+
tr_error("Failure, DB not initialized");
231+
return BLE_ERROR_INITIALIZATION_INCOMPLETE;
232+
}
233+
_db->sync();
234+
return BLE_ERROR_NONE;
235+
}
236+
225237
////////////////////////////////////////////////////////////////////////////
226238
// List management
227239
//

connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class SecurityManager :
8585

8686
ble_error_t preserveBondingStateOnReset(bool enable);
8787

88+
ble_error_t writeBondingStateToPersistentStorage();
89+
8890
////////////////////////////////////////////////////////////////////////////
8991
// List management
9092
//

0 commit comments

Comments
 (0)