|
8 | 8 |
|
9 | 9 | using namespace std::chrono_literals; |
10 | 10 |
|
| 11 | +// make a callback for managing the pairing table when it fills up |
| 12 | +class BleDeviceCallbacks : public NimBLEDeviceCallbacks { |
| 13 | + espp::Logger logger = |
| 14 | + espp::Logger({.tag = "NimBLEDeviceCallbacks", .level = espp::Logger::Verbosity::INFO}); |
| 15 | + |
| 16 | +public: |
| 17 | + /** |
| 18 | + * @brief Indicates an inability to perform a store operation. |
| 19 | + * This callback should do one of two things: |
| 20 | + * -Address the problem and return 0, indicating that the store operation |
| 21 | + * should proceed. |
| 22 | + * -Return nonzero to indicate that the store operation should be aborted. |
| 23 | + * @param event Describes the store event being reported. |
| 24 | + * BLE_STORE_EVENT_FULL; or |
| 25 | + * BLE_STORE_EVENT_OVERFLOW |
| 26 | + * @return 0 if the store operation should proceed; |
| 27 | + * nonzero if the store operation should be aborted. |
| 28 | + */ |
| 29 | + virtual int onStoreStatus(struct ble_store_status_event *event, void *arg) { |
| 30 | + // see |
| 31 | + // https://github.com/apache/mynewt-nimble/blob/master/nimble/host/include/host/ble_store.h#L180 |
| 32 | + // for definition of ble_store_status_event |
| 33 | + if (event->event_code == BLE_STORE_EVENT_FULL) { |
| 34 | + logger.info("Store full event: {}", event->event_code); |
| 35 | + // if the store is full, then we should delete some old devices |
| 36 | + // to make room for new ones |
| 37 | + // |
| 38 | + // the connection handle for the connection which prompted the write is |
| 39 | + // found at event->full->conn_handle. |
| 40 | + return ble_store_util_status_rr(event, arg); |
| 41 | + } else if (event->event_code == BLE_STORE_EVENT_OVERFLOW) { |
| 42 | + logger.info("Store overflow event: {}", event->event_code); |
| 43 | + // if the store overflows, then we should delete some old devices |
| 44 | + // to make room for new ones |
| 45 | + // |
| 46 | + // The object that failed to be written is found in |
| 47 | + // event->overflow->value (ble_store_value*) |
| 48 | + return ble_store_util_status_rr(event, arg); |
| 49 | + } else { |
| 50 | + logger.error("Unknown store event: {}", event->event_code); |
| 51 | + return ble_store_util_status_rr(event, arg); |
| 52 | + } |
| 53 | + } |
| 54 | +}; |
| 55 | +static BleDeviceCallbacks device_callbacks; |
| 56 | + |
11 | 57 | extern "C" void app_main(void) { |
12 | 58 | espp::Logger logger({.tag = "Hid Service Example", .level = espp::Logger::Verbosity::INFO}); |
13 | 59 | logger.info("Starting"); |
@@ -53,6 +99,21 @@ extern "C" void app_main(void) { |
53 | 99 | ble_gatt_server.set_advertise_on_disconnect(true); |
54 | 100 | #endif |
55 | 101 |
|
| 102 | + // NOTE: for information about the low-level NVS storage on esp chips, see |
| 103 | + // ~/esp/esp-idf/components/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_nvs.c |
| 104 | + // |
| 105 | + // now set the device callbacks to override the default one from esp-nimble-cpp |
| 106 | + NimBLEDevice::setDeviceCallbacks(&device_callbacks); |
| 107 | + uint8_t max_bonds = MYNEWT_VAL(BLE_STORE_MAX_BONDS); |
| 108 | + logger.info("Max bonds: {}", max_bonds); |
| 109 | + |
| 110 | + // print the bonded devices as well |
| 111 | + auto paired_device_addresses = ble_gatt_server.get_paired_devices(); |
| 112 | + logger.info("Paired devices: {}", paired_device_addresses.size()); |
| 113 | + for (const auto &addr : paired_device_addresses) { |
| 114 | + logger.info(" Addr: {}", addr.toString()); |
| 115 | + } |
| 116 | + |
56 | 117 | // for HID we need to set some security |
57 | 118 | bool bonding = true; |
58 | 119 | bool mitm = false; |
@@ -259,6 +320,13 @@ extern "C" void app_main(void) { |
259 | 320 | logger.info(" Model: {}", model_number); |
260 | 321 | logger.info(" PnP: {}", pnp_id); |
261 | 322 | } |
| 323 | + |
| 324 | + // print the bonded devices as well |
| 325 | + paired_device_addresses = ble_gatt_server.get_paired_devices(); |
| 326 | + logger.info("Paired devices: {}", paired_device_addresses.size()); |
| 327 | + for (const auto &addr : paired_device_addresses) { |
| 328 | + logger.info(" Addr: {}", addr.toString()); |
| 329 | + } |
262 | 330 | } |
263 | 331 | } else if (!ble_gatt_server.is_connected()) { |
264 | 332 | was_connected = false; |
|
0 commit comments