From 143631d327e91c08b11e98fdfe7810c73438e878 Mon Sep 17 00:00:00 2001 From: h2zero Date: Fri, 29 May 2020 21:21:56 -0600 Subject: [PATCH] Make attribute delete functions public and selective by UUID * Make remote attribute delete functions public. * Rename clear...() functions to delete...(), with ... equals Service(-s), Characteristic(-s) or Descriptor(-s), depending on what the function actually deletes --- src/NimBLEClient.cpp | 38 +++++++++++++++++++++++------- src/NimBLEClient.h | 4 +++- src/NimBLERemoteCharacteristic.cpp | 32 +++++++++++++++++++++---- src/NimBLERemoteCharacteristic.h | 3 ++- src/NimBLERemoteService.cpp | 36 +++++++++++++++++++++++----- src/NimBLERemoteService.h | 3 ++- 6 files changed, 95 insertions(+), 21 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 9dcc43e..e23ed69 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -74,7 +74,7 @@ NimBLEClient::NimBLEClient() NimBLEClient::~NimBLEClient() { // We may have allocated service references associated with this client. // Before we are finished with the client, we must release resources. - clearServices(); + deleteServices(); if(m_deleteCallbacks && m_pClientCallbacks != &defaultCallbacks) { delete m_pClientCallbacks; @@ -84,18 +84,40 @@ NimBLEClient::~NimBLEClient() { /** - * @brief Clear any existing services. + * @brief Delete any existing services. */ -void NimBLEClient::clearServices() { - NIMBLE_LOGD(LOG_TAG, ">> clearServices"); +void NimBLEClient::deleteServices() { + NIMBLE_LOGD(LOG_TAG, ">> deleteServices"); // Delete all the services. for(auto &it: m_servicesVector) { delete it; } m_servicesVector.clear(); - NIMBLE_LOGD(LOG_TAG, "<< clearServices"); -} // clearServices + NIMBLE_LOGD(LOG_TAG, "<< deleteServices"); +} // deleteServices + + +/** + * @brief Delete service by UUID + * @param [in] uuid The UUID of the service to be deleted from the local database. + * @return Number of services left. + */ +size_t NimBLEClient::deleteService(const NimBLEUUID &uuid) { + NIMBLE_LOGD(LOG_TAG, ">> deleteService"); + // Delete the requested service. + for(auto it = m_servicesVector.begin(); it != m_servicesVector.end(); ++it) { + if((*it)->getUUID() == uuid) { + delete *it; + m_servicesVector.erase(it); + break; + } + } + + NIMBLE_LOGD(LOG_TAG, "<< deleteService"); + + return m_servicesVector.size(); +} // deleteServices /** @@ -178,7 +200,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr if(refreshServices) { NIMBLE_LOGD(LOG_TAG, "Refreshing Services for: (%s)", address.toString().c_str()); - clearServices(); + deleteServices(); } m_pClientCallbacks->onConnect(this); @@ -399,7 +421,7 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) { */ std::vector* NimBLEClient::getServices(bool refresh) { if(refresh) { - clearServices(); + deleteServices(); } if(m_servicesVector.empty()) { diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 4bf2c33..289c739 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -21,6 +21,7 @@ #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #include "NimBLEAddress.h" +#include "NimBLEUUID.h" #include "NimBLEAdvertisedDevice.h" #include "NimBLERemoteService.h" @@ -52,6 +53,8 @@ public: std::vector::iterator end(); NimBLERemoteService* getService(const char* uuid); NimBLERemoteService* getService(const NimBLEUUID &uuid); + void deleteServices(); + size_t deleteService(const NimBLEUUID &uuid); std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value); @@ -82,7 +85,6 @@ private: const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg); - void clearServices(); bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr); NimBLEAddress m_peerAddress = NimBLEAddress(""); diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index 5e4a7cb..fb5d3b4 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -67,7 +67,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic"; *@brief Destructor. */ NimBLERemoteCharacteristic::~NimBLERemoteCharacteristic() { - removeDescriptors(); // Release resources for any descriptor information we may have allocated. + deleteDescriptors(); // Release resources for any descriptor information we may have allocated. } // ~NimBLERemoteCharacteristic /* @@ -269,7 +269,7 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU */ std::vector* NimBLERemoteCharacteristic::getDescriptors(bool refresh) { if(refresh) { - removeDescriptors(); + deleteDescriptors(); } if(m_descriptorVector.empty()) { @@ -535,13 +535,37 @@ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallbac * them. This method does just that. * @return N/A. */ -void NimBLERemoteCharacteristic::removeDescriptors() { +void NimBLERemoteCharacteristic::deleteDescriptors() { + NIMBLE_LOGD(LOG_TAG, ">> deleteDescriptors"); // Iterate through all the descriptors releasing their storage and erasing them from the vector. for(auto &it: m_descriptorVector) { delete it; } m_descriptorVector.clear(); -} // removeCharacteristics + NIMBLE_LOGD(LOG_TAG, "<< deleteDescriptors"); +} // deleteDescriptors + + +/** + * @brief Delete descriptor by UUID + * @param [in] uuid The UUID of the descriptor to be deleted. + * @return Number of services left. + */ +size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID &uuid) { + NIMBLE_LOGD(LOG_TAG, ">> deleteDescriptor"); + // Delete the requested descriptor. + for(auto it = m_descriptorVector.begin(); it != m_descriptorVector.end(); ++it) { + if((*it)->getUUID() == uuid) { + delete *it; + m_descriptorVector.erase(it); + break; + } + } + + NIMBLE_LOGD(LOG_TAG, "<< deleteDescriptor"); + + return m_descriptorVector.size(); +} // deleteDescriptor /** diff --git a/src/NimBLERemoteCharacteristic.h b/src/NimBLERemoteCharacteristic.h index 4b66458..6a17a55 100644 --- a/src/NimBLERemoteCharacteristic.h +++ b/src/NimBLERemoteCharacteristic.h @@ -50,6 +50,8 @@ public: std::vector::iterator end(); NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid); std::vector* getDescriptors(bool refresh = false); + void deleteDescriptors(); + size_t deleteDescriptor(const NimBLEUUID &uuid); uint16_t getHandle(); uint16_t getDefHandle(); NimBLEUUID getUUID(); @@ -98,7 +100,6 @@ private: friend class NimBLERemoteDescriptor; // Private member functions - void removeDescriptors(); bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr); static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg); diff --git a/src/NimBLERemoteService.cpp b/src/NimBLERemoteService.cpp index 193a40b..ea3a5b2 100644 --- a/src/NimBLERemoteService.cpp +++ b/src/NimBLERemoteService.cpp @@ -58,7 +58,7 @@ NimBLERemoteService::NimBLERemoteService(NimBLEClient* pClient, const struct ble * Also release any semaphores they may be holding. */ NimBLERemoteService::~NimBLERemoteService() { - removeCharacteristics(); + deleteCharacteristics(); } @@ -125,7 +125,7 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU std::vector* NimBLERemoteService::getCharacteristics(bool refresh) { if(refresh) { - removeCharacteristics(); + deleteCharacteristics(); } if(m_characteristicVector.empty()) { @@ -181,7 +181,7 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle, /* Error; abort discovery. */ // pass non-zero to semaphore on error to indicate an error finding characteristics // release memory from any characteristics we created - //service->removeCharacteristics(); --this will now be done when we clear services on returning with error + //service->deleteCharacteristics(); --this will now be done when we clear services on returning with error NIMBLE_LOGE(LOG_TAG, "characteristicDiscCB() rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); service->m_semaphoreGetCharEvt.give(1); } @@ -199,7 +199,7 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter) NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str()); int rc = 0; - //removeCharacteristics(); // Forget any previous characteristics. + //deleteCharacteristics(); // Forget any previous characteristics. m_semaphoreGetCharEvt.take("retrieveCharacteristics"); @@ -316,12 +316,36 @@ bool NimBLERemoteService::setValue(const NimBLEUUID &characteristicUuid, const s * them. This method does just that. * @return N/A. */ -void NimBLERemoteService::removeCharacteristics() { +void NimBLERemoteService::deleteCharacteristics() { + NIMBLE_LOGD(LOG_TAG, ">> deleteCharacteristics"); for(auto &it: m_characteristicVector) { delete it; } m_characteristicVector.clear(); // Clear the vector -} // removeCharacteristics + NIMBLE_LOGD(LOG_TAG, "<< deleteCharacteristics"); +} // deleteCharacteristics + + +/** + * @brief Delete characteristic by UUID + * @param [in] uuid The UUID of the characteristic to be cleared. + * @return Number of characteristics left. + */ +size_t NimBLERemoteService::deleteCharacteristic(const NimBLEUUID &uuid) { + NIMBLE_LOGD(LOG_TAG, ">> deleteCharacteristic"); + // Delete the requested characteristic. + for(auto it = m_characteristicVector.begin(); it != m_characteristicVector.end(); ++it) { + if((*it)->getUUID() == uuid) { + delete *it; + m_characteristicVector.erase(it); + break; + } + } + + NIMBLE_LOGD(LOG_TAG, "<< deleteCharacteristic"); + + return m_characteristicVector.size(); +} // deleteCharacteristic /** diff --git a/src/NimBLERemoteService.h b/src/NimBLERemoteService.h index 3803498..12bd3ff 100644 --- a/src/NimBLERemoteService.h +++ b/src/NimBLERemoteService.h @@ -43,6 +43,8 @@ public: std::vector::iterator end(); NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid); + void deleteCharacteristics(); + size_t deleteCharacteristic(const NimBLEUUID &uuid); NimBLEClient* getClient(void); uint16_t getHandle(); NimBLEUUID getUUID(void); @@ -70,7 +72,6 @@ private: uint16_t getStartHandle(); uint16_t getEndHandle(); void releaseSemaphores(); - void removeCharacteristics(); // Properties