forked from h2zero/esp-nimble-cpp
Add clearAll parameter to deinit() (#22)
* Add clearAll parameter to deinit() 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. * Add bool deleteCallbacks parameter to NimBLEServer::setCallbacks, if true (default) will delete the callback class when server is destructed. * Delete scan results when scan is destructed.
This commit is contained in:
+39
-1
@@ -539,8 +539,10 @@ void NimBLEDevice::stopAdvertising() {
|
||||
|
||||
/**
|
||||
* @brief Shutdown the NimBLE stack/controller.
|
||||
* @param [in] clearAll If true, deletes all server/advertising/scan/client objects after deinitializing.
|
||||
* @note If clearAll is true when called, any references to the created objects become invalid.
|
||||
*/
|
||||
/* STATIC */ void NimBLEDevice::deinit() {
|
||||
/* STATIC */ void NimBLEDevice::deinit(bool clearAll) {
|
||||
int ret = nimble_port_stop();
|
||||
if (ret == 0) {
|
||||
nimble_port_deinit();
|
||||
@@ -552,6 +554,42 @@ void NimBLEDevice::stopAdvertising() {
|
||||
|
||||
initialized = false;
|
||||
m_synced = false;
|
||||
|
||||
if(clearAll) {
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
if(NimBLEDevice::m_pServer != nullptr) {
|
||||
delete NimBLEDevice::m_pServer;
|
||||
NimBLEDevice::m_pServer = nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
||||
if(NimBLEDevice::m_bleAdvertising != nullptr) {
|
||||
delete NimBLEDevice::m_bleAdvertising;
|
||||
NimBLEDevice::m_bleAdvertising = nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
||||
if(NimBLEDevice::m_pScan != nullptr) {
|
||||
delete NimBLEDevice::m_pScan;
|
||||
NimBLEDevice::m_pScan= nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
||||
for(auto &it : m_cList) {
|
||||
deleteClient(it);
|
||||
m_cList.clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
m_ignoreList.clear();
|
||||
|
||||
if(m_securityCallbacks != nullptr) {
|
||||
delete m_securityCallbacks;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // deinit
|
||||
|
||||
|
||||
Reference in New Issue
Block a user