diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 01cc079c..31b5b262 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -76,6 +76,8 @@ extern "C" void ble_store_config_init(void); /** * Singletons for the NimBLEDevice. */ +NimBLEDeviceCallbacks* NimBLEDevice::m_pDeviceCallbacks = nullptr; + # if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) NimBLEScan* NimBLEDevice::m_pScan = nullptr; # endif @@ -900,7 +902,9 @@ bool NimBLEDevice::init(const std::string& deviceName) { // Setup callbacks for host events ble_hs_cfg.reset_cb = NimBLEDevice::onReset; ble_hs_cfg.sync_cb = NimBLEDevice::onSync; - ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /*TODO: Implement handler for this*/ + ble_hs_cfg.store_status_cb = [](struct ble_store_status_event *event, void *arg) { + return m_pDeviceCallbacks->onStoreStatus(event); + }; // Set initial security capabilities ble_hs_cfg.sm_io_cap = BLE_HS_IO_NO_INPUT_OUTPUT; @@ -1262,4 +1266,15 @@ void nimble_cpp_assert(const char* file, unsigned line) { } # endif // CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED +void NimBLEDevice::setDeviceCallbacks(NimBLEDeviceCallbacks* cb) { + m_pDeviceCallbacks = cb ? cb : &defaultDeviceCallbacks; +} + +static const char* CB_TAG = "NimBLEDeviceCallbacks"; + +int NimBLEDeviceCallbacks::onStoreStatus(struct ble_store_status_event *event) { + NIMBLE_LOGD(CB_TAG, "onStoreStatus: default"); + return 1; +} + #endif // CONFIG_BT_ENABLED diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index 9f9f52b2..0aa4697e 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -66,6 +66,7 @@ class NimBLEConnInfo; # endif class NimBLEAddress; +class NimBLEDeviceCallbacks; # define BLEDevice NimBLEDevice # define BLEClient NimBLEClient @@ -129,6 +130,7 @@ class NimBLEDevice { static bool setOwnAddrType(uint8_t type); static bool setOwnAddr(const NimBLEAddress& addr); static bool setOwnAddr(const uint8_t* addr); + static void setDeviceCallbacks(NimBLEDeviceCallbacks* cb); static void setScanDuplicateCacheSize(uint16_t cacheSize); static void setScanFilterMode(uint8_t type); static bool setCustomGapHandler(gap_event_handler handler); @@ -213,6 +215,8 @@ class NimBLEDevice { static ble_gap_event_listener m_listener; static uint8_t m_ownAddrType; static std::vector m_whiteList; + static NimBLEDeviceCallbacks* m_pDeviceCallbacks; + static NimBLEDeviceCallbacks defaultDeviceCallbacks; # if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) static NimBLEScan* m_pScan; @@ -295,5 +299,23 @@ class NimBLEDevice { # include "NimBLEUtils.h" +/** + * @brief Callbacks associated with a BLE device. + */ +class NimBLEDeviceCallbacks { + public: + virtual ~NimBLEDeviceCallbacks() {}; + + /** @brief Storage Status callback. + * This callback gets executed when a persistence operation cannot be + * performed or a persistence failure is imminent. For example, if is + * insufficient storage capacity for a record to be persisted, this + * function gets called to give the application the opportunity to make + * room. + * @param [in] event BLE_STORE_EVENT_FULL or BLE_STORE_EVENT_OVERFLOW. + */ + virtual int onStoreStatus(struct ble_store_status_event *event); +}; + #endif // CONFIG_BT_ENABLED #endif // NIMBLE_CPP_DEVICE_H_