Skip to content

Commit 439d002

Browse files
new API call to change db at runtime
1 parent 608ad33 commit 439d002

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

features/FEATURE_BLE/ble/SecurityManager.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class SecurityManager {
441441
* support out-of-band exchanges of security data.
442442
* @param[in] passkey To specify a static passkey.
443443
* @param[in] signing Generate and distribute signing key during pairing
444-
* @param[in] dbPath Path to the folder used to store keys in the filesystem,
444+
* @param[in] dbPath Path to the file used to store keys in the filesystem,
445445
* if NULL keys will be only stored in memory
446446
*
447447
*
@@ -452,17 +452,31 @@ class SecurityManager {
452452
SecurityIOCapabilities_t iocaps = IO_CAPS_NONE,
453453
const Passkey_t passkey = NULL,
454454
bool signing = true,
455-
const char *dbPath = NULL) {
455+
const char *dbFilepath = NULL) {
456456
/* Avoid compiler warnings about unused variables. */
457457
(void)enableBonding;
458458
(void)requireMITM;
459459
(void)iocaps;
460460
(void)passkey;
461-
(void)dbPath;
461+
(void)dbFilepath;
462462

463463
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */
464464
}
465465

466+
/**
467+
* Change the file used for the security datagse. If path is invalid or a NULL is passed
468+
* keys will only be stored in memory.
469+
*
470+
* @param[in] dbPath Path to the file used to store keys in the filesystem,
471+
* if NULL keys will be only stored in memory
472+
*
473+
* @return BLE_ERROR_NONE on success.
474+
*/
475+
virtual ble_error_t setDatabaseFile(const char *dbFilepath = NULL) {
476+
(void)dbFilepath;
477+
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */
478+
}
479+
466480
/**
467481
* Notify all registered onShutdown callbacks that the SecurityManager is
468482
* about to be shutdown and clear all SecurityManager state of the

features/FEATURE_BLE/ble/generic/GenericSecurityManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class GenericSecurityManager : public SecurityManager,
5353
const char* db_path = NULL
5454
);
5555

56+
virtual ble_error_t setDatabaseFile(const char *db_path = NULL);
57+
5658
virtual ble_error_t reset();
5759

5860
virtual ble_error_t preserveBondingStateOnReset(

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,16 @@ ble_error_t GenericSecurityManager::init(
4343
const char* db_path
4444
) {
4545

46-
ble_error_t err = _pal.initialize();
47-
if (err) {
48-
return err;
46+
ble_error_t result = _pal.initialize();
47+
if (result != BLE_ERROR_NONE) {
48+
return result;
4949
}
5050

51-
if (_db) {
52-
delete _db;
53-
}
54-
55-
FILE* db_file = FileSecurityDb::open_db_file(db_path);
56-
57-
if (db_file) {
58-
_db = new (std::nothrow) FileSecurityDb(db_file);
59-
} else {
60-
_db = new (std::nothrow) MemorySecurityDb();
51+
result = setDatabaseFile(db_path);
52+
if (result != BLE_ERROR_NONE) {
53+
return result;
6154
}
6255

63-
if (!_db) {
64-
return BLE_ERROR_NO_MEM;
65-
}
66-
67-
_db->restore();
68-
6956
_pal.set_io_capability((io_capability_t::type) iocaps);
7057

7158
if (passkey) {
@@ -115,6 +102,30 @@ ble_error_t GenericSecurityManager::init(
115102
return BLE_ERROR_NONE;
116103
}
117104

105+
ble_error_t GenericSecurityManager::setDatabaseFile(
106+
const char *db_path
107+
) {
108+
if (_db) {
109+
delete _db;
110+
}
111+
112+
FILE* db_file = FileSecurityDb::open_db_file(db_path);
113+
114+
if (db_file) {
115+
_db = new (std::nothrow) FileSecurityDb(db_file);
116+
} else {
117+
_db = new (std::nothrow) MemorySecurityDb();
118+
}
119+
120+
if (!_db) {
121+
return BLE_ERROR_NO_MEM;
122+
}
123+
124+
_db->restore();
125+
126+
return BLE_ERROR_NONE;
127+
}
128+
118129
ble_error_t GenericSecurityManager::reset(void) {
119130
_pal.reset();
120131
SecurityManager::reset();

0 commit comments

Comments
 (0)