From 05bb0774f31c259220d44f3699790fec1e6f57e6 Mon Sep 17 00:00:00 2001 From: thekurtovic <40248206+thekurtovic@users.noreply.github.com> Date: Thu, 16 Jan 2025 17:15:08 -0500 Subject: [PATCH] fix: Use double pointer --- src/NimBLEClient.cpp | 8 ++++---- src/NimBLEClient.h | 12 ++++++------ src/NimBLERemoteCharacteristic.cpp | 8 ++++---- src/NimBLERemoteCharacteristic.h | 5 +++-- src/NimBLERemoteService.cpp | 8 ++++---- src/NimBLERemoteService.h | 5 +++-- src/NimBLEUtils.h | 8 ++++---- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 6b66f99..ee90b27 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -632,8 +632,8 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID& uuid) { NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str()); NimBLERemoteService *pSvc = nullptr; - NimBLEUtils::getAttr(uuid, pSvc, m_svcVec, [this](const NimBLEUUID* u, void* arg) { - return retrieveServices(u, static_cast(arg)); + NimBLEUtils::getAttr(uuid, &pSvc, m_svcVec, [this](const NimBLEUUID* u, NimBLERemoteService** arg) { + return retrieveServices(u, arg); }); NIMBLE_LOGD(LOG_TAG, "<< getService: %sfound", !pSvc ? "not " : ""); @@ -690,7 +690,7 @@ bool NimBLEClient::discoverAttributes() { * * Here we ask the server for its set of services and wait until we have received them all. * @return true on success otherwise false if an error occurred */ -bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter, NimBLERemoteService *out) { +bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter, NimBLERemoteService **out) { if (!isConnected()) { NIMBLE_LOGE(LOG_TAG, "Disconnected, could not retrieve services -aborting"); return false; @@ -719,7 +719,7 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID* uuidFilter, NimBLERemoteSe return false; } - out = m_svcVec.back(); + *out = m_svcVec.back(); NIMBLE_LOGD(LOG_TAG, "<< retrieveServices(): found %d services.", m_svcVec.size()); return true; } // getServices diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index eba7165..f94204d 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -116,13 +116,13 @@ class NimBLEClient { NimBLEClient(const NimBLEClient&) = delete; NimBLEClient& operator=(const NimBLEClient&) = delete; - bool retrieveServices(const NimBLEUUID* uuidFilter = nullptr); + bool retrieveServices(const NimBLEUUID* uuidFilter = nullptr, NimBLERemoteService **out = nullptr); static int handleGapEvent(struct ble_gap_event* event, void* arg); - static int exchangeMTUCb(uint16_t conn_handle, const ble_gatt_error* error, uint16_t mtu, void* arg); - static int serviceDiscoveredCB(uint16_t connHandle, - const struct ble_gatt_error* error, - const struct ble_gatt_svc* service, - void* arg); + static int exchangeMTUCB(uint16_t connHandle, const ble_gatt_error* error, uint16_t mtu, void* arg); + static int serviceDiscCB(uint16_t connHandle, + const struct ble_gatt_error* error, + const struct ble_gatt_svc* service, + void* arg); NimBLEAddress m_peerAddress; mutable int m_lastErr; diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index eb5fd04..f3a552c 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -95,7 +95,7 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(uint16_t connHandle, * @param [in] filter Structure containing pointers to descriptor, UUID, and task data. * @return True if successfully retrieved, success = BLE_HS_EDONE. */ -bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilter, NimBLERemoteDescriptor* out) const { +bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilter, NimBLERemoteDescriptor** out) const { NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str()); NimBLETaskData taskData(const_cast(this)); @@ -126,7 +126,7 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte return false; } - out = m_vDescriptors.back(); + *out = m_vDescriptors.back(); NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): found %d descriptors.", m_vDescriptors.size()); return true; } // retrieveDescriptors @@ -140,8 +140,8 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str()); NimBLERemoteDescriptor* pDsc = nullptr; - NimBLEUtils::getAttr(uuid, pDsc, m_vDescriptors, [this](const NimBLEUUID* u, void* arg) { - return retrieveDescriptors(u, static_cast(arg)); + NimBLEUtils::getAttr(uuid, &pDsc, m_vDescriptors, [this](const NimBLEUUID* u, NimBLERemoteDescriptor** arg) { + return retrieveDescriptors(u, arg); }); NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: %sfound", !pDsc ? "not " : ""); diff --git a/src/NimBLERemoteCharacteristic.h b/src/NimBLERemoteCharacteristic.h index 532d814..bc3c294 100644 --- a/src/NimBLERemoteCharacteristic.h +++ b/src/NimBLERemoteCharacteristic.h @@ -65,10 +65,11 @@ class NimBLERemoteCharacteristic : public NimBLERemoteValueAttribute { ~NimBLERemoteCharacteristic(); bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true) const; - bool retrieveDescriptors(const NimBLEUUID* uuidFilter = nullptr) const; + bool retrieveDescriptors(const NimBLEUUID* uuidFilter = nullptr, + NimBLERemoteDescriptor** out = nullptr) const; static int descriptorDiscCB( - uint16_t conn_handle, const ble_gatt_error* error, uint16_t chr_val_handle, const ble_gatt_dsc* dsc, void* arg); + uint16_t connHandle, const ble_gatt_error* error, uint16_t chrHandle, const ble_gatt_dsc* dsc, void* arg); const NimBLERemoteService* m_pRemoteService{nullptr}; uint8_t m_properties{0}; diff --git a/src/NimBLERemoteService.cpp b/src/NimBLERemoteService.cpp index f62f902..3ca75ed 100644 --- a/src/NimBLERemoteService.cpp +++ b/src/NimBLERemoteService.cpp @@ -78,8 +78,8 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str()); NimBLERemoteCharacteristic* pChar = nullptr; - NimBLEUtils::getAttr(uuid, pChar, m_vChars, [this](const NimBLEUUID* u, void* arg) { - return retrieveCharacteristics(u, static_cast(arg)); + NimBLEUtils::getAttr(uuid, &pChar, m_vChars, [this](const NimBLEUUID* u, NimBLERemoteCharacteristic** arg) { + return retrieveCharacteristics(u, arg); }); NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: %sfound", !pChar ? "not " : ""); @@ -135,7 +135,7 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t connHandle, * This function will not return until we have all the characteristics. * @return True if successfully retrieved, success = BLE_HS_EDONE. */ -bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID* uuidFilter, NimBLERemoteCharacteristic* out) const { +bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID* uuidFilter, NimBLERemoteCharacteristic** out) const { NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics()"); NimBLETaskData taskData(const_cast(this)); int rc = 0; @@ -167,7 +167,7 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID* uuidFilter, return false; } - out = m_vChars.back(); + *out = m_vChars.back(); NIMBLE_LOGD(LOG_TAG, "<< retrieveCharacteristics(): found %d characteristics.", m_vChars.size()); return true; } // retrieveCharacteristics diff --git a/src/NimBLERemoteService.h b/src/NimBLERemoteService.h index 8045788..1233356 100644 --- a/src/NimBLERemoteService.h +++ b/src/NimBLERemoteService.h @@ -53,8 +53,9 @@ class NimBLERemoteService : public NimBLEAttribute { NimBLERemoteService(NimBLEClient* pClient, const struct ble_gatt_svc* service); ~NimBLERemoteService(); - bool retrieveCharacteristics(const NimBLEUUID* uuidFilter = nullptr) const; - static int characteristicDiscCB(uint16_t conn_handle, + bool retrieveCharacteristics(const NimBLEUUID* uuidFilter = nullptr, + NimBLERemoteCharacteristic** out = nullptr) const; + static int characteristicDiscCB(uint16_t connHandle, const struct ble_gatt_error* error, const struct ble_gatt_chr* chr, void* arg); diff --git a/src/NimBLEUtils.h b/src/NimBLEUtils.h index dfa4b9e..51bc3e1 100644 --- a/src/NimBLEUtils.h +++ b/src/NimBLEUtils.h @@ -56,18 +56,18 @@ class NimBLEUtils { static bool taskWait(const NimBLETaskData& taskData, uint32_t timeout); static void taskRelease(const NimBLETaskData& taskData, int rc = 0); - template - static void getAttr(const NimBLEUUID& uuid, T* attr, const std::vector& vec, const std::function& getter) { + template + static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector& vec, const std::function& getter) { // Check if already exists. for (const auto& v : vec) { if (v->getUUID() == uuid) { - attr = v; + *attr = v; return; } } // Exit if request failed or uuid was found. - if (!getter(&uuid, attr) || attr) { + if (!getter(&uuid, attr) || *attr) { return; }