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
This commit is contained in:
h2zero 2020-05-29 21:21:56 -06:00
parent ffcb325aea
commit 143631d327
6 changed files with 95 additions and 21 deletions

View file

@ -74,7 +74,7 @@ NimBLEClient::NimBLEClient()
NimBLEClient::~NimBLEClient() { NimBLEClient::~NimBLEClient() {
// We may have allocated service references associated with this client. // We may have allocated service references associated with this client.
// Before we are finished with the client, we must release resources. // Before we are finished with the client, we must release resources.
clearServices(); deleteServices();
if(m_deleteCallbacks && m_pClientCallbacks != &defaultCallbacks) { if(m_deleteCallbacks && m_pClientCallbacks != &defaultCallbacks) {
delete m_pClientCallbacks; delete m_pClientCallbacks;
@ -84,18 +84,40 @@ NimBLEClient::~NimBLEClient() {
/** /**
* @brief Clear any existing services. * @brief Delete any existing services.
*/ */
void NimBLEClient::clearServices() { void NimBLEClient::deleteServices() {
NIMBLE_LOGD(LOG_TAG, ">> clearServices"); NIMBLE_LOGD(LOG_TAG, ">> deleteServices");
// Delete all the services. // Delete all the services.
for(auto &it: m_servicesVector) { for(auto &it: m_servicesVector) {
delete it; delete it;
} }
m_servicesVector.clear(); m_servicesVector.clear();
NIMBLE_LOGD(LOG_TAG, "<< clearServices"); NIMBLE_LOGD(LOG_TAG, "<< deleteServices");
} // clearServices } // 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) { if(refreshServices) {
NIMBLE_LOGD(LOG_TAG, "Refreshing Services for: (%s)", address.toString().c_str()); NIMBLE_LOGD(LOG_TAG, "Refreshing Services for: (%s)", address.toString().c_str());
clearServices(); deleteServices();
} }
m_pClientCallbacks->onConnect(this); m_pClientCallbacks->onConnect(this);
@ -399,7 +421,7 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
*/ */
std::vector<NimBLERemoteService*>* NimBLEClient::getServices(bool refresh) { std::vector<NimBLERemoteService*>* NimBLEClient::getServices(bool refresh) {
if(refresh) { if(refresh) {
clearServices(); deleteServices();
} }
if(m_servicesVector.empty()) { if(m_servicesVector.empty()) {

View file

@ -21,6 +21,7 @@
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#include "NimBLEUUID.h"
#include "NimBLEAdvertisedDevice.h" #include "NimBLEAdvertisedDevice.h"
#include "NimBLERemoteService.h" #include "NimBLERemoteService.h"
@ -52,6 +53,8 @@ public:
std::vector<NimBLERemoteService*>::iterator end(); std::vector<NimBLERemoteService*>::iterator end();
NimBLERemoteService* getService(const char* uuid); NimBLERemoteService* getService(const char* uuid);
NimBLERemoteService* getService(const NimBLEUUID &uuid); NimBLERemoteService* getService(const NimBLEUUID &uuid);
void deleteServices();
size_t deleteService(const NimBLEUUID &uuid);
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID);
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID,
const std::string &value); const std::string &value);
@ -82,7 +85,6 @@ private:
const struct ble_gatt_error *error, const struct ble_gatt_error *error,
const struct ble_gatt_svc *service, const struct ble_gatt_svc *service,
void *arg); void *arg);
void clearServices();
bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr); bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr);
NimBLEAddress m_peerAddress = NimBLEAddress(""); NimBLEAddress m_peerAddress = NimBLEAddress("");

View file

@ -67,7 +67,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
*@brief Destructor. *@brief Destructor.
*/ */
NimBLERemoteCharacteristic::~NimBLERemoteCharacteristic() { 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 } // ~NimBLERemoteCharacteristic
/* /*
@ -269,7 +269,7 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
*/ */
std::vector<NimBLERemoteDescriptor*>* NimBLERemoteCharacteristic::getDescriptors(bool refresh) { std::vector<NimBLERemoteDescriptor*>* NimBLERemoteCharacteristic::getDescriptors(bool refresh) {
if(refresh) { if(refresh) {
removeDescriptors(); deleteDescriptors();
} }
if(m_descriptorVector.empty()) { if(m_descriptorVector.empty()) {
@ -535,13 +535,37 @@ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallbac
* them. This method does just that. * them. This method does just that.
* @return N/A. * @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. // Iterate through all the descriptors releasing their storage and erasing them from the vector.
for(auto &it: m_descriptorVector) { for(auto &it: m_descriptorVector) {
delete it; delete it;
} }
m_descriptorVector.clear(); 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
/** /**

View file

@ -50,6 +50,8 @@ public:
std::vector<NimBLERemoteDescriptor*>::iterator end(); std::vector<NimBLERemoteDescriptor*>::iterator end();
NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid); NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid);
std::vector<NimBLERemoteDescriptor*>* getDescriptors(bool refresh = false); std::vector<NimBLERemoteDescriptor*>* getDescriptors(bool refresh = false);
void deleteDescriptors();
size_t deleteDescriptor(const NimBLEUUID &uuid);
uint16_t getHandle(); uint16_t getHandle();
uint16_t getDefHandle(); uint16_t getDefHandle();
NimBLEUUID getUUID(); NimBLEUUID getUUID();
@ -98,7 +100,6 @@ private:
friend class NimBLERemoteDescriptor; friend class NimBLERemoteDescriptor;
// Private member functions // Private member functions
void removeDescriptors();
bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr); bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
struct ble_gatt_attr *attr, void *arg); struct ble_gatt_attr *attr, void *arg);

View file

@ -58,7 +58,7 @@ NimBLERemoteService::NimBLERemoteService(NimBLEClient* pClient, const struct ble
* Also release any semaphores they may be holding. * Also release any semaphores they may be holding.
*/ */
NimBLERemoteService::~NimBLERemoteService() { NimBLERemoteService::~NimBLERemoteService() {
removeCharacteristics(); deleteCharacteristics();
} }
@ -125,7 +125,7 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU
std::vector<NimBLERemoteCharacteristic*>* NimBLERemoteService::getCharacteristics(bool refresh) { std::vector<NimBLERemoteCharacteristic*>* NimBLERemoteService::getCharacteristics(bool refresh) {
if(refresh) { if(refresh) {
removeCharacteristics(); deleteCharacteristics();
} }
if(m_characteristicVector.empty()) { if(m_characteristicVector.empty()) {
@ -181,7 +181,7 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle,
/* Error; abort discovery. */ /* Error; abort discovery. */
// pass non-zero to semaphore on error to indicate an error finding characteristics // pass non-zero to semaphore on error to indicate an error finding characteristics
// release memory from any characteristics we created // 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)); NIMBLE_LOGE(LOG_TAG, "characteristicDiscCB() rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
service->m_semaphoreGetCharEvt.give(1); 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()); NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str());
int rc = 0; int rc = 0;
//removeCharacteristics(); // Forget any previous characteristics. //deleteCharacteristics(); // Forget any previous characteristics.
m_semaphoreGetCharEvt.take("retrieveCharacteristics"); m_semaphoreGetCharEvt.take("retrieveCharacteristics");
@ -316,12 +316,36 @@ bool NimBLERemoteService::setValue(const NimBLEUUID &characteristicUuid, const s
* them. This method does just that. * them. This method does just that.
* @return N/A. * @return N/A.
*/ */
void NimBLERemoteService::removeCharacteristics() { void NimBLERemoteService::deleteCharacteristics() {
NIMBLE_LOGD(LOG_TAG, ">> deleteCharacteristics");
for(auto &it: m_characteristicVector) { for(auto &it: m_characteristicVector) {
delete it; delete it;
} }
m_characteristicVector.clear(); // Clear the vector 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
/** /**

View file

@ -43,6 +43,8 @@ public:
std::vector<NimBLERemoteCharacteristic*>::iterator end(); std::vector<NimBLERemoteCharacteristic*>::iterator end();
NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); NimBLERemoteCharacteristic* getCharacteristic(const char* uuid);
NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid); NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid);
void deleteCharacteristics();
size_t deleteCharacteristic(const NimBLEUUID &uuid);
NimBLEClient* getClient(void); NimBLEClient* getClient(void);
uint16_t getHandle(); uint16_t getHandle();
NimBLEUUID getUUID(void); NimBLEUUID getUUID(void);
@ -70,7 +72,6 @@ private:
uint16_t getStartHandle(); uint16_t getStartHandle();
uint16_t getEndHandle(); uint16_t getEndHandle();
void releaseSemaphores(); void releaseSemaphores();
void removeCharacteristics();
// Properties // Properties