Skip to content

Commit 81ad797

Browse files
add persistence demonstration to SM example
1 parent 65afa82 commit 81ad797

File tree

1 file changed

+65
-9
lines changed

1 file changed

+65
-9
lines changed

BLE_SM/source/main.cpp

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "ble/BLE.h"
2020
#include "SecurityManager.h"
2121

22+
#include "LittleFileSystem.h"
23+
#include "HeapBlockDevice.h"
24+
2225
/** This example demonstrates all the basic setup required
2326
* for pairing and setting up link security both as a central and peripheral
2427
*
@@ -159,9 +162,19 @@ class SMDevice : private mbed::NonCopyable<SMDevice>,
159162
return;
160163
}
161164

165+
/* This path will be used to store bonding information but will fallback
166+
* to storing in memory if file access fails (for example due to lack of a filesystem) */
167+
const char* db_path = "/fs/bt_sec_db";
162168
/* If the security manager is required this needs to be called before any
163169
* calls to the Security manager happen. */
164-
error = _ble.securityManager().init();
170+
error = _ble.securityManager().init(
171+
true,
172+
false,
173+
SecurityManager::IO_CAPS_NONE,
174+
NULL,
175+
false,
176+
db_path
177+
);
165178

166179
if (error) {
167180
printf("Error during init %d\r\n", error);
@@ -396,21 +409,64 @@ class SMDeviceCentral : public SMDevice {
396409
};
397410
};
398411

412+
bool create_filesystem()
413+
{
414+
static LittleFileSystem fs("fs");
415+
416+
/* replace this with any physical block device your board supports (like an SD card) */
417+
static HeapBlockDevice bd(4096, 256);
418+
419+
int err = bd.init();
420+
421+
if (err) {
422+
return false;
423+
}
424+
425+
err = bd.erase(0, bd.size());
426+
427+
if (err) {
428+
return false;
429+
}
430+
431+
err = fs.mount(&bd);
432+
433+
if (err) {
434+
/* Reformat if we can't mount the filesystem */
435+
printf("No filesystem found, formatting...\r\n");
436+
437+
err = fs.reformat(&bd);
438+
439+
if (err) {
440+
return false;
441+
}
442+
}
443+
444+
return true;
445+
}
446+
399447
int main()
400448
{
401449
BLE& ble = BLE::Instance();
402450
events::EventQueue queue;
403451

404-
{
405-
printf("\r\n PERIPHERAL \r\n\r\n");
406-
SMDevicePeripheral peripheral(ble, queue, peer_address);
407-
peripheral.run();
452+
/* if filesystem creation fails or there is no filesystem the security manager
453+
* will fallback to storing the security databse in memory */
454+
if (!create_filesystem()) {
455+
printf("Filesystem creation failed, will use memory storage\r\n");
408456
}
409457

410-
{
411-
printf("\r\n CENTRAL \r\n\r\n");
412-
SMDeviceCentral central(ble, queue, peer_address);
413-
central.run();
458+
while(1) {
459+
{
460+
printf("\r\n PERIPHERAL \r\n\r\n");
461+
SMDevicePeripheral peripheral(ble, queue, peer_address);
462+
peripheral.run();
463+
}
464+
465+
{
466+
printf("\r\n CENTRAL \r\n\r\n");
467+
SMDeviceCentral central(ble, queue, peer_address);
468+
central.run();
469+
}
414470
}
415471

416472
return 0;

0 commit comments

Comments
 (0)