diff --git a/src/NimBLEUtils.cpp b/src/NimBLEUtils.cpp index 6e9f771..b23a3fa 100644 --- a/src/NimBLEUtils.cpp +++ b/src/NimBLEUtils.cpp @@ -20,6 +20,9 @@ # include "NimBLEUtils.h" # include "NimBLEAddress.h" +# include "NimBLERemoteService.h" +# include "NimBLERemoteDescriptor.h" +# include "NimBLERemoteCharacteristic.h" # include "NimBLELog.h" # if defined(CONFIG_NIMBLE_CPP_IDF) @@ -575,4 +578,51 @@ NimBLEAddress NimBLEUtils::generateAddr(bool nrpa) { return NimBLEAddress{addr}; } // generateAddr +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); + } +} + +template void NimBLEUtils::getAttr( + const NimBLEUUID&, + NimBLERemoteService**, + const std::vector&, + const std::function&); + +template void NimBLEUtils::getAttr( + const NimBLEUUID&, + NimBLERemoteDescriptor**, + const std::vector&, + const std::function&); + +template void NimBLEUtils::getAttr( + const NimBLEUUID&, + NimBLERemoteCharacteristic**, + const std::vector&, + const std::function&); + #endif // CONFIG_BT_ENABLED diff --git a/src/NimBLEUtils.h b/src/NimBLEUtils.h index fe63a56..bfd3e36 100644 --- a/src/NimBLEUtils.h +++ b/src/NimBLEUtils.h @@ -58,33 +58,7 @@ class NimBLEUtils { 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) { - // 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); - } - } + static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector& vec, const std::function& getter); }; #endif // CONFIG_BT_ENABLED