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:
h2zero 2020-07-28 20:57:33 -06:00 committed by GitHub
parent b2df8384b3
commit abdf6cda35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 5 deletions

View file

@ -539,8 +539,10 @@ void NimBLEDevice::stopAdvertising() {
/** /**
* @brief Shutdown the NimBLE stack/controller. * @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(); int ret = nimble_port_stop();
if (ret == 0) { if (ret == 0) {
nimble_port_deinit(); nimble_port_deinit();
@ -552,6 +554,42 @@ void NimBLEDevice::stopAdvertising() {
initialized = false; initialized = false;
m_synced = 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 } // deinit

View file

@ -91,7 +91,7 @@ extern "C" void ble_store_config_init(void);
class NimBLEDevice { class NimBLEDevice {
public: public:
static void init(const std::string &deviceName); static void init(const std::string &deviceName);
static void deinit(); static void deinit(bool clearAll = false);
static bool getInitialized(); static bool getInitialized();
static NimBLEAddress getAddress(); static NimBLEAddress getAddress();
static std::string toString(); static std::string toString();

View file

@ -44,6 +44,13 @@ NimBLEScan::NimBLEScan() {
} }
/**
* @brief Scan destructor, release any allocated resources.
*/
NimBLEScan::~NimBLEScan() {
clearResults();
}
/** /**
* @brief Handle GAP events related to scans. * @brief Handle GAP events related to scans.
* @param [in] event The event type for this event. * @param [in] event The event type for this event.

View file

@ -77,8 +77,10 @@ public:
private: private:
NimBLEScan();
friend class NimBLEDevice; friend class NimBLEDevice;
NimBLEScan();
~NimBLEScan();
static int handleGapEvent(ble_gap_event* event, void* arg); static int handleGapEvent(ble_gap_event* event, void* arg);
void onHostReset(); void onHostReset();

View file

@ -42,6 +42,7 @@ NimBLEServer::NimBLEServer() {
m_gattsStarted = false; m_gattsStarted = false;
m_advertiseOnDisconnect = true; m_advertiseOnDisconnect = true;
m_svcChanged = false; m_svcChanged = false;
m_deleteCallbacks = true;
} // NimBLEServer } // NimBLEServer
@ -52,6 +53,10 @@ NimBLEServer::~NimBLEServer() {
for(auto &it : m_svcVec) { for(auto &it : m_svcVec) {
delete it; delete it;
} }
if(m_deleteCallbacks && m_pServerCallbacks != &defaultCallbacks) {
delete m_pServerCallbacks;
}
} }
@ -465,10 +470,12 @@ size_t NimBLEServer::getConnectedCount() {
* events are detected. * events are detected.
* *
* @param [in] pCallbacks The callbacks to be invoked. * @param [in] pCallbacks The callbacks to be invoked.
* @param [in] deleteCallbacks if true callback class will be deleted when server is destructed.
*/ */
void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks) { void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks, bool deleteCallbacks) {
if (pCallbacks != nullptr){ if (pCallbacks != nullptr){
m_pServerCallbacks = pCallbacks; m_pServerCallbacks = pCallbacks;
m_deleteCallbacks = deleteCallbacks;
} else { } else {
m_pServerCallbacks = &defaultCallbacks; m_pServerCallbacks = &defaultCallbacks;
} }

View file

@ -44,7 +44,8 @@ public:
void removeService(NimBLEService* service, bool deleteSvc = false); void removeService(NimBLEService* service, bool deleteSvc = false);
void addService(NimBLEService* service); void addService(NimBLEService* service);
NimBLEAdvertising* getAdvertising(); NimBLEAdvertising* getAdvertising();
void setCallbacks(NimBLEServerCallbacks* pCallbacks); void setCallbacks(NimBLEServerCallbacks* pCallbacks,
bool deleteCallbacks = true);
void startAdvertising(); void startAdvertising();
void stopAdvertising(); void stopAdvertising();
void start(); void start();
@ -70,6 +71,7 @@ private:
bool m_advertiseOnDisconnect; bool m_advertiseOnDisconnect;
bool m_svcChanged; bool m_svcChanged;
NimBLEServerCallbacks* m_pServerCallbacks; NimBLEServerCallbacks* m_pServerCallbacks;
bool m_deleteCallbacks;
std::vector<uint16_t> m_connectedPeersVec; std::vector<uint16_t> m_connectedPeersVec;
// uint16_t m_svcChgChrHdl; // Future use // uint16_t m_svcChgChrHdl; // Future use