Skip to content

Commit 550ef9f

Browse files
committed
Add clearAll parameter to deinit(), Fixes #12.
Adds the ability to clear all resources consumed during BLE operation when deinitializing and shutting down BLE. Useful when BLE is used intermittently or in a task that initializes, performs operations then deinitializes. By setting the clearAll parameter to true all created BLE objects will be deleted, freeing the memory for other tasks. Warning: This will invalidate any pointers that may be referencing the deleted objects.
1 parent e0e940a commit 550ef9f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/NimBLEDevice.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ void NimBLEDevice::stopAdvertising() {
540540
/**
541541
* @brief Shutdown the NimBLE stack/controller.
542542
*/
543-
/* STATIC */ void NimBLEDevice::deinit() {
543+
/* STATIC */ void NimBLEDevice::deinit(bool clearAll) {
544544
int ret = nimble_port_stop();
545545
if (ret == 0) {
546546
nimble_port_deinit();
@@ -552,6 +552,42 @@ void NimBLEDevice::stopAdvertising() {
552552

553553
initialized = false;
554554
m_synced = false;
555+
556+
if(clearAll) {
557+
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
558+
if(NimBLEDevice::m_pServer != nullptr) {
559+
delete NimBLEDevice::m_pServer;
560+
NimBLEDevice::m_pServer = nullptr;
561+
}
562+
#endif
563+
564+
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
565+
if(NimBLEDevice::m_bleAdvertising != nullptr) {
566+
delete NimBLEDevice::m_bleAdvertising;
567+
NimBLEDevice::m_bleAdvertising = nullptr;
568+
}
569+
#endif
570+
571+
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
572+
if(NimBLEDevice::m_pScan != nullptr) {
573+
delete NimBLEDevice::m_pScan;
574+
NimBLEDevice::m_pScan= nullptr;
575+
}
576+
#endif
577+
578+
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
579+
for(auto &it : m_cList) {
580+
deleteClient(it);
581+
m_cList.clear();
582+
}
583+
#endif
584+
585+
m_ignoreList.clear();
586+
587+
if(m_securityCallbacks != nullptr) {
588+
delete m_securityCallbacks;
589+
}
590+
}
555591
}
556592
} // deinit
557593

src/NimBLEDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern "C" void ble_store_config_init(void);
9191
class NimBLEDevice {
9292
public:
9393
static void init(const std::string &deviceName);
94-
static void deinit();
94+
static void deinit(bool clearAll = false);
9595
static bool getInitialized();
9696
static NimBLEAddress getAddress();
9797
static std::string toString();

0 commit comments

Comments
 (0)