Created NimBLERemoteGattUtils and moved getAttr inside

This commit is contained in:
thekurtovic 2025-01-20 17:52:57 -05:00
parent 310a60a487
commit 2c97bb8334
8 changed files with 115 additions and 51 deletions

View file

@ -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"

View file

@ -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<NimBLERemoteService>(uuid, &pSvc, m_svcVec, [this](const NimBLEUUID* u, NimBLERemoteService** svc) {
NimBLERemoteGattUtils::getAttr<NimBLERemoteService>(uuid, &pSvc, m_svcVec,
[this](const NimBLEUUID* u, NimBLERemoteService** svc) {
return retrieveServices(u, svc);
});

View file

@ -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<NimBLERemoteDescriptor>(uuid, &pDsc, m_vDescriptors, [this](const NimBLEUUID* u, NimBLERemoteDescriptor** dsc) {
NimBLERemoteGattUtils::getAttr<NimBLERemoteDescriptor>(uuid, &pDsc, m_vDescriptors,
[this](const NimBLEUUID* u, NimBLERemoteDescriptor** dsc) {
return retrieveDescriptors(u, dsc);
});

View file

@ -0,0 +1,66 @@
/*
* Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> 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 <typename T>
void NimBLERemoteGattUtils::getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec, const std::function<bool(const NimBLEUUID*, T**)>& 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<svc>(const NimBLEUUID&, svc**, const std::vector<svc*>&, const std::function<bool(const NimBLEUUID*, svc**)>&);
template void NimBLERemoteGattUtils::getAttr<chr>(const NimBLEUUID&, chr**, const std::vector<chr*>&, const std::function<bool(const NimBLEUUID*, chr**)>&);
template void NimBLERemoteGattUtils::getAttr<dsc>(const NimBLEUUID&, dsc**, const std::vector<dsc*>&, const std::function<bool(const NimBLEUUID*, dsc**)>&);
#endif // CONFIG_BT_ENABLED

View file

@ -0,0 +1,39 @@
/*
* Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> 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 <functional>
# include <vector>
# include <string>
/**
* @brief A BLE Remote GATT Utility class for getting an attribute.
*/
class NimBLERemoteGattUtils {
public:
template <typename T>
static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec,
const std::function<bool(const NimBLEUUID*, T**)>& getter);
}; // NimBLERemoteGattUtils
#endif /* CONFIG_BT_ENABLED */
#endif /* NIMBLE_CPP_GATT_UTILS_H_ */

View file

@ -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<NimBLERemoteCharacteristic>(uuid, &pChar, m_vChars, [this](const NimBLEUUID* u, NimBLERemoteCharacteristic** chr) {
NimBLERemoteGattUtils::getAttr<NimBLERemoteCharacteristic>(uuid, &pChar, m_vChars,
[this](const NimBLEUUID* u, NimBLERemoteCharacteristic** chr) {
return retrieveCharacteristics(u, chr);
});

View file

@ -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 <typename T>
void NimBLEUtils::getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec, const std::function<bool(const NimBLEUUID*, T**)>& 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<svc>(const NimBLEUUID&, svc**, const std::vector<svc*>&, const std::function<bool(const NimBLEUUID*, svc**)>&);
template void NimBLEUtils::getAttr<chr>(const NimBLEUUID&, chr**, const std::vector<chr*>&, const std::function<bool(const NimBLEUUID*, chr**)>&);
template void NimBLEUtils::getAttr<dsc>(const NimBLEUUID&, dsc**, const std::vector<dsc*>&, const std::function<bool(const NimBLEUUID*, dsc**)>&);
#endif // CONFIG_BT_ENABLED

View file

@ -20,9 +20,6 @@
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED)
# include "NimBLEUUID.h"
# include <functional>
# include <vector>
# include <string>
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 <typename T>
static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec,
const std::function<bool(const NimBLEUUID*, T**)>& getter);
};
#endif // CONFIG_BT_ENABLED