|
19 | 19 | #include "ble/BLE.h"
|
20 | 20 | #include "SecurityManager.h"
|
21 | 21 |
|
| 22 | +#include "LittleFileSystem.h" |
| 23 | +#include "HeapBlockDevice.h" |
| 24 | + |
22 | 25 | /** This example demonstrates all the basic setup required
|
23 | 26 | * for pairing and setting up link security both as a central and peripheral
|
24 | 27 | *
|
@@ -159,9 +162,19 @@ class SMDevice : private mbed::NonCopyable<SMDevice>,
|
159 | 162 | return;
|
160 | 163 | }
|
161 | 164 |
|
| 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"; |
162 | 168 | /* If the security manager is required this needs to be called before any
|
163 | 169 | * 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 | + ); |
165 | 178 |
|
166 | 179 | if (error) {
|
167 | 180 | printf("Error during init %d\r\n", error);
|
@@ -396,21 +409,64 @@ class SMDeviceCentral : public SMDevice {
|
396 | 409 | };
|
397 | 410 | };
|
398 | 411 |
|
| 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 | + |
399 | 447 | int main()
|
400 | 448 | {
|
401 | 449 | BLE& ble = BLE::Instance();
|
402 | 450 | events::EventQueue queue;
|
403 | 451 |
|
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"); |
408 | 456 | }
|
409 | 457 |
|
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 | + } |
414 | 470 | }
|
415 | 471 |
|
416 | 472 | return 0;
|
|
0 commit comments