Skip to content

Commit 51a4423

Browse files
thekurtovich2zero
authored andcommitted
feat(Device): Implement store status handler
1 parent 9397a49 commit 51a4423

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/NimBLEDevice.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern "C" void ble_store_config_init(void);
7676
/**
7777
* Singletons for the NimBLEDevice.
7878
*/
79+
NimBLEDeviceCallbacks* NimBLEDevice::m_pDeviceCallbacks = nullptr;
80+
7981
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
8082
NimBLEScan* NimBLEDevice::m_pScan = nullptr;
8183
# endif
@@ -900,7 +902,9 @@ bool NimBLEDevice::init(const std::string& deviceName) {
900902
// Setup callbacks for host events
901903
ble_hs_cfg.reset_cb = NimBLEDevice::onReset;
902904
ble_hs_cfg.sync_cb = NimBLEDevice::onSync;
903-
ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /*TODO: Implement handler for this*/
905+
ble_hs_cfg.store_status_cb = [](struct ble_store_status_event* event, void* arg) {
906+
return m_pDeviceCallbacks->onStoreStatus(event, arg);
907+
};
904908

905909
// Set initial security capabilities
906910
ble_hs_cfg.sm_io_cap = BLE_HS_IO_NO_INPUT_OUTPUT;
@@ -1262,4 +1266,13 @@ void nimble_cpp_assert(const char* file, unsigned line) {
12621266
}
12631267
# endif // CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED
12641268

1269+
void NimBLEDevice::setDeviceCallbacks(NimBLEDeviceCallbacks* cb) {
1270+
m_pDeviceCallbacks = cb ? cb : &defaultDeviceCallbacks;
1271+
}
1272+
1273+
int NimBLEDeviceCallbacks::onStoreStatus(struct ble_store_status_event* event, void* arg) {
1274+
NIMBLE_LOGD("NimBLEDeviceCallbacks", "onStoreStatus: default");
1275+
return ble_store_util_status_rr(event, arg);
1276+
}
1277+
12651278
#endif // CONFIG_BT_ENABLED

src/NimBLEDevice.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class NimBLEConnInfo;
6666
# endif
6767

6868
class NimBLEAddress;
69+
class NimBLEDeviceCallbacks;
6970

7071
# define BLEDevice NimBLEDevice
7172
# define BLEClient NimBLEClient
@@ -129,6 +130,7 @@ class NimBLEDevice {
129130
static bool setOwnAddrType(uint8_t type);
130131
static bool setOwnAddr(const NimBLEAddress& addr);
131132
static bool setOwnAddr(const uint8_t* addr);
133+
static void setDeviceCallbacks(NimBLEDeviceCallbacks* cb);
132134
static void setScanDuplicateCacheSize(uint16_t cacheSize);
133135
static void setScanFilterMode(uint8_t type);
134136
static bool setCustomGapHandler(gap_event_handler handler);
@@ -213,6 +215,8 @@ class NimBLEDevice {
213215
static ble_gap_event_listener m_listener;
214216
static uint8_t m_ownAddrType;
215217
static std::vector<NimBLEAddress> m_whiteList;
218+
static NimBLEDeviceCallbacks* m_pDeviceCallbacks;
219+
static NimBLEDeviceCallbacks defaultDeviceCallbacks;
216220

217221
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
218222
static NimBLEScan* m_pScan;
@@ -295,5 +299,27 @@ class NimBLEDevice {
295299

296300
# include "NimBLEUtils.h"
297301

302+
/**
303+
* @brief Callbacks associated with a BLE device.
304+
*/
305+
class NimBLEDeviceCallbacks {
306+
public:
307+
virtual ~NimBLEDeviceCallbacks() {};
308+
309+
/**
310+
* @brief Indicates an inability to perform a store operation.
311+
* This callback should do one of two things:
312+
* -Address the problem and return 0, indicating that the store operation
313+
* should proceed.
314+
* -Return nonzero to indicate that the store operation should be aborted.
315+
* @param event Describes the store event being reported.
316+
* BLE_STORE_EVENT_FULL; or
317+
* BLE_STORE_EVENT_OVERFLOW
318+
* @return 0 if the store operation should proceed;
319+
* nonzero if the store operation should be aborted.
320+
*/
321+
virtual int onStoreStatus(struct ble_store_status_event* event, void* arg);
322+
};
323+
298324
#endif // CONFIG_BT_ENABLED
299325
#endif // NIMBLE_CPP_DEVICE_H_

0 commit comments

Comments
 (0)