From 2c97bb8334a9cf8f52fc62ceeca1a0fdb522c59f Mon Sep 17 00:00:00 2001 From: thekurtovic <40248206+thekurtovic@users.noreply.github.com> Date: Mon, 20 Jan 2025 17:52:57 -0500 Subject: [PATCH] Created NimBLERemoteGattUtils and moved getAttr inside --- CMakeLists.txt | 1 + src/NimBLEClient.cpp | 4 +- src/NimBLERemoteCharacteristic.cpp | 4 +- src/NimBLERemoteGattUtils.cpp | 66 ++++++++++++++++++++++++++++++ src/NimBLERemoteGattUtils.h | 39 ++++++++++++++++++ src/NimBLERemoteService.cpp | 4 +- src/NimBLEUtils.cpp | 42 ------------------- src/NimBLEUtils.h | 6 --- 8 files changed, 115 insertions(+), 51 deletions(-) create mode 100644 src/NimBLERemoteGattUtils.cpp create mode 100644 src/NimBLERemoteGattUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 60df811..3cccafa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ idf_component_register( "src/NimBLEHIDDevice.cpp" "src/NimBLERemoteCharacteristic.cpp" "src/NimBLERemoteDescriptor.cpp" + "src/NimBLERemoteGattUtils.cpp" "src/NimBLERemoteService.cpp" "src/NimBLERemoteValueAttribute.cpp" "src/NimBLEScan.cpp" diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 5d82e91..f4ac514 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -21,6 +21,7 @@ # include "NimBLEClient.h" # include "NimBLERemoteService.h" # include "NimBLERemoteCharacteristic.h" +# include "NimBLERemoteGattUtils.h" # include "NimBLEDevice.h" # include "NimBLELog.h" @@ -632,7 +633,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, NimBLERemoteService** svc) { + NimBLERemoteGattUtils::getAttr(uuid, &pSvc, m_svcVec, + [this](const NimBLEUUID* u, NimBLERemoteService** svc) { return retrieveServices(u, svc); }); diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index 6b877de..46e8f86 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -20,6 +20,7 @@ # include "NimBLERemoteCharacteristic.h" # include "NimBLERemoteDescriptor.h" +# include "NimBLERemoteGattUtils.h" # include "NimBLERemoteService.h" # include "NimBLEClient.h" # include "NimBLEUtils.h" @@ -140,7 +141,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, NimBLERemoteDescriptor** dsc) { + NimBLERemoteGattUtils::getAttr(uuid, &pDsc, m_vDescriptors, + [this](const NimBLEUUID* u, NimBLERemoteDescriptor** dsc) { return retrieveDescriptors(u, dsc); }); diff --git a/src/NimBLERemoteGattUtils.cpp b/src/NimBLERemoteGattUtils.cpp new file mode 100644 index 0000000..657ffe1 --- /dev/null +++ b/src/NimBLERemoteGattUtils.cpp @@ -0,0 +1,66 @@ +/* + * Copyright 2020-2024 Ryan Powell and + * esp-nimble-cpp, NimBLE-Arduino contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nimconfig.h" +#if defined(CONFIG_BT_ENABLED) + +# include "NimBLERemoteGattUtils.h" +# include "NimBLEAddress.h" +# include "NimBLERemoteService.h" +# include "NimBLERemoteDescriptor.h" +# include "NimBLERemoteCharacteristic.h" + +/** + * @brief Get an attribute matching a uuid. + * @param [in] uuid Search for this uuid. + * @param [in] attr Pointer to hold result. + * @param [in] vec Vector to search through before trying to get attribute. + * @param [in] getter Attribute getter function to call. + */ +template +void NimBLERemoteGattUtils::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; + return; + } + } + // Exit if request failed or uuid was found. + if (!getter(&uuid, attr) || *attr) { + return; + } + // Try again with 128 bit uuid if request succeeded with no uuid found. + if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) { + NimBLEUUID uuid128 = NimBLEUUID(uuid).to128(); + getter(&uuid128, attr); + return; + } + // Try again with 16 bit uuid if request succeeded with no uuid found. + // If the uuid was 128 bit but not of the BLE base type this check will fail. + NimBLEUUID uuid16 = NimBLEUUID(uuid).to16(); + if (uuid16.bitSize() == BLE_UUID_TYPE_16) { + getter(&uuid16, attr); + } +} + +using svc = NimBLERemoteService; using chr = NimBLERemoteCharacteristic; using dsc = NimBLERemoteDescriptor; +template void NimBLERemoteGattUtils::getAttr(const NimBLEUUID&, svc**, const std::vector&, const std::function&); +template void NimBLERemoteGattUtils::getAttr(const NimBLEUUID&, chr**, const std::vector&, const std::function&); +template void NimBLERemoteGattUtils::getAttr(const NimBLEUUID&, dsc**, const std::vector&, const std::function&); + +#endif // CONFIG_BT_ENABLED diff --git a/src/NimBLERemoteGattUtils.h b/src/NimBLERemoteGattUtils.h new file mode 100644 index 0000000..47709d5 --- /dev/null +++ b/src/NimBLERemoteGattUtils.h @@ -0,0 +1,39 @@ +/* + * Copyright 2020-2024 Ryan Powell and + * esp-nimble-cpp, NimBLE-Arduino contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NIMBLE_CPP_GATT_UTILS_H_ +#define NIMBLE_CPP_GATT_UTILS_H_ + +#include "nimconfig.h" +#if defined(CONFIG_BT_ENABLED) +# include "NimBLEUUID.h" +# include +# include +# include + +/** + * @brief A BLE Remote GATT Utility class for getting an attribute. + */ +class NimBLERemoteGattUtils { + public: + template + static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector& vec, + const std::function& getter); +}; // NimBLERemoteGattUtils + +#endif /* CONFIG_BT_ENABLED */ +#endif /* NIMBLE_CPP_GATT_UTILS_H_ */ diff --git a/src/NimBLERemoteService.cpp b/src/NimBLERemoteService.cpp index a19a346..7afe9a6 100644 --- a/src/NimBLERemoteService.cpp +++ b/src/NimBLERemoteService.cpp @@ -20,6 +20,7 @@ # include "NimBLERemoteService.h" # include "NimBLERemoteCharacteristic.h" +# include "NimBLERemoteGattUtils.h" # include "NimBLEClient.h" # include "NimBLEAttValue.h" # include "NimBLEUtils.h" @@ -78,7 +79,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, NimBLERemoteCharacteristic** chr) { + NimBLERemoteGattUtils::getAttr(uuid, &pChar, m_vChars, + [this](const NimBLEUUID* u, NimBLERemoteCharacteristic** chr) { return retrieveCharacteristics(u, chr); }); diff --git a/src/NimBLEUtils.cpp b/src/NimBLEUtils.cpp index d5911b0..6e9f771 100644 --- a/src/NimBLEUtils.cpp +++ b/src/NimBLEUtils.cpp @@ -20,9 +20,6 @@ # include "NimBLEUtils.h" # include "NimBLEAddress.h" -# include "NimBLERemoteService.h" -# include "NimBLERemoteDescriptor.h" -# include "NimBLERemoteCharacteristic.h" # include "NimBLELog.h" # if defined(CONFIG_NIMBLE_CPP_IDF) @@ -578,43 +575,4 @@ NimBLEAddress NimBLEUtils::generateAddr(bool nrpa) { return NimBLEAddress{addr}; } // generateAddr -/** - * @brief Get an attribute matching a uuid. - * @param [in] uuid Search for this uuid. - * @param [in] attr Pointer to hold result. - * @param [in] vec Vector to search through before trying to get attribute. - * @param [in] getter Attribute getter function to call. - */ -template -void NimBLEUtils::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; - return; - } - } - // Exit if request failed or uuid was found. - if (!getter(&uuid, attr) || *attr) { - return; - } - // Try again with 128 bit uuid if request succeeded with no uuid found. - if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) { - NimBLEUUID uuid128 = NimBLEUUID(uuid).to128(); - getter(&uuid128, attr); - return; - } - // Try again with 16 bit uuid if request succeeded with no uuid found. - // If the uuid was 128 bit but not of the BLE base type this check will fail. - NimBLEUUID uuid16 = NimBLEUUID(uuid).to16(); - if (uuid16.bitSize() == BLE_UUID_TYPE_16) { - getter(&uuid16, attr); - } -} - -using svc = NimBLERemoteService; using chr = NimBLERemoteCharacteristic; using dsc = NimBLERemoteDescriptor; -template void NimBLEUtils::getAttr(const NimBLEUUID&, svc**, const std::vector&, const std::function&); -template void NimBLEUtils::getAttr(const NimBLEUUID&, chr**, const std::vector&, const std::function&); -template void NimBLEUtils::getAttr(const NimBLEUUID&, dsc**, const std::vector&, const std::function&); - #endif // CONFIG_BT_ENABLED diff --git a/src/NimBLEUtils.h b/src/NimBLEUtils.h index 92637f0..e56f568 100644 --- a/src/NimBLEUtils.h +++ b/src/NimBLEUtils.h @@ -20,9 +20,6 @@ #include "nimconfig.h" #if defined(CONFIG_BT_ENABLED) -# include "NimBLEUUID.h" -# include -# include # include class NimBLEAddress; @@ -56,9 +53,6 @@ class NimBLEUtils { static NimBLEAddress generateAddr(bool nrpa); 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); }; #endif // CONFIG_BT_ENABLED