Clean up PR

This commit is contained in:
thekurtovic 2025-01-16 21:40:31 -05:00
parent 74c6c583d2
commit 310a60a487
5 changed files with 21 additions and 16 deletions

View file

@ -632,8 +632,8 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID& uuid) {
NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str());
NimBLERemoteService *pSvc = nullptr; NimBLERemoteService *pSvc = nullptr;
NimBLEUtils::getAttr<NimBLERemoteService>(uuid, &pSvc, m_svcVec, [this](const NimBLEUUID* u, NimBLERemoteService** arg) { NimBLEUtils::getAttr<NimBLERemoteService>(uuid, &pSvc, m_svcVec, [this](const NimBLEUUID* u, NimBLERemoteService** svc) {
return retrieveServices(u, arg); return retrieveServices(u, svc);
}); });
NIMBLE_LOGD(LOG_TAG, "<< getService: %sfound", !pSvc ? "not " : ""); NIMBLE_LOGD(LOG_TAG, "<< getService: %sfound", !pSvc ? "not " : "");

View file

@ -27,10 +27,10 @@
# include <climits> # include <climits>
struct desc_filter_t { typedef struct {
const NimBLEUUID* uuid; const NimBLEUUID* uuid;
void* taskData; void* taskData;
}; } desc_filter_t;
static const char* LOG_TAG = "NimBLERemoteCharacteristic"; static const char* LOG_TAG = "NimBLERemoteCharacteristic";
@ -99,7 +99,7 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte
NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str());
NimBLETaskData taskData(const_cast<NimBLERemoteCharacteristic*>(this)); NimBLETaskData taskData(const_cast<NimBLERemoteCharacteristic*>(this));
desc_filter_t filter = {uuidFilter, &taskData}; desc_filter_t filter = {uuidFilter, &taskData};
const uint16_t handle = getHandle(); const uint16_t handle = getHandle();
const uint16_t svcHandle = getRemoteService()->getEndHandle(); const uint16_t svcHandle = getRemoteService()->getEndHandle();
@ -140,8 +140,8 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
NimBLERemoteDescriptor* pDsc = nullptr; NimBLERemoteDescriptor* pDsc = nullptr;
NimBLEUtils::getAttr<NimBLERemoteDescriptor>(uuid, &pDsc, m_vDescriptors, [this](const NimBLEUUID* u, NimBLERemoteDescriptor** arg) { NimBLEUtils::getAttr<NimBLERemoteDescriptor>(uuid, &pDsc, m_vDescriptors, [this](const NimBLEUUID* u, NimBLERemoteDescriptor** dsc) {
return retrieveDescriptors(u, arg); return retrieveDescriptors(u, dsc);
}); });
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: %sfound", !pDsc ? "not " : ""); NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: %sfound", !pDsc ? "not " : "");

View file

@ -78,8 +78,8 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU
NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str());
NimBLERemoteCharacteristic* pChar = nullptr; NimBLERemoteCharacteristic* pChar = nullptr;
NimBLEUtils::getAttr<NimBLERemoteCharacteristic>(uuid, &pChar, m_vChars, [this](const NimBLEUUID* u, NimBLERemoteCharacteristic** arg) { NimBLEUtils::getAttr<NimBLERemoteCharacteristic>(uuid, &pChar, m_vChars, [this](const NimBLEUUID* u, NimBLERemoteCharacteristic** chr) {
return retrieveCharacteristics(u, arg); return retrieveCharacteristics(u, chr);
}); });
NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: %sfound", !pChar ? "not " : ""); NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: %sfound", !pChar ? "not " : "");

View file

@ -578,6 +578,13 @@ NimBLEAddress NimBLEUtils::generateAddr(bool nrpa) {
return NimBLEAddress{addr}; return NimBLEAddress{addr};
} // generateAddr } // 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> template <typename T>
void NimBLEUtils::getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec, const std::function<bool(const NimBLEUUID*, T**)>& getter) { 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. // Check if already exists.
@ -587,12 +594,10 @@ void NimBLEUtils::getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*
return; return;
} }
} }
// Exit if request failed or uuid was found. // Exit if request failed or uuid was found.
if (!getter(&uuid, attr) || *attr) { if (!getter(&uuid, attr) || *attr) {
return; return;
} }
// Try again with 128 bit uuid if request succeeded with no uuid found. // 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) { if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
NimBLEUUID uuid128 = NimBLEUUID(uuid).to128(); NimBLEUUID uuid128 = NimBLEUUID(uuid).to128();

View file

@ -21,7 +21,7 @@
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
# include "NimBLEUUID.h" # include "NimBLEUUID.h"
# include "functional" # include <functional>
# include <vector> # include <vector>
# include <string> # include <string>
@ -56,9 +56,9 @@ class NimBLEUtils {
static NimBLEAddress generateAddr(bool nrpa); static NimBLEAddress generateAddr(bool nrpa);
static bool taskWait(const NimBLETaskData& taskData, uint32_t timeout); static bool taskWait(const NimBLETaskData& taskData, uint32_t timeout);
static void taskRelease(const NimBLETaskData& taskData, int rc = 0); static void taskRelease(const NimBLETaskData& taskData, int rc = 0);
template <typename T> template <typename T>
static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec, const std::function<bool(const NimBLEUUID*, T**)>& getter); 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 #endif // CONFIG_BT_ENABLED