Skip to content

Commit 3e04b9b

Browse files
committed
refactor(RemoteChar): Reduce nesting
* retrieveDescriptors added optional parameter to pass in pointer to get descriptor * General clean-up
1 parent 5ac7272 commit 3e04b9b

File tree

2 files changed

+37
-52
lines changed

2 files changed

+37
-52
lines changed

src/NimBLERemoteCharacteristic.cpp

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
3737
/**
3838
* @brief Constructor.
3939
* @param [in] svc A pointer to the service this characteristic belongs to.
40-
* @param [in] ble_gatt_chr struct defined as:
40+
* @param [in] chr struct defined as:
4141
* struct ble_gatt_chr {
4242
* uint16_t def_handle;
4343
* uint16_t val_handle;
@@ -63,7 +63,7 @@ NimBLERemoteCharacteristic::~NimBLERemoteCharacteristic() {
6363
* @brief Callback used by the API when a descriptor is discovered or search complete.
6464
*/
6565
int NimBLERemoteCharacteristic::descriptorDiscCB(
66-
uint16_t conn_handle, const ble_gatt_error* error, uint16_t chr_val_handle, const ble_gatt_dsc* dsc, void* arg) {
66+
uint16_t connHandle, const ble_gatt_error* error, uint16_t chrHandle, const ble_gatt_dsc* dsc, void* arg) {
6767
int rc = error->status;
6868
NIMBLE_LOGD(LOG_TAG, "Descriptor Discovery >> status: %d handle: %d", rc, (rc == 0) ? dsc->handle : -1);
6969

@@ -72,35 +72,31 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(
7272
const auto pChr = (NimBLERemoteCharacteristic*)pTaskData->m_pInstance;
7373
const NimBLEUUID* uuidFilter = filter->uuid;
7474

75-
if (pChr->getHandle() != chr_val_handle) {
75+
if (pChr->getHandle() != chrHandle) {
7676
return 0; // Descriptor not for this characteristic
7777
}
7878

79-
if (rc == 0) {
80-
if (uuidFilter != nullptr) {
81-
if (ble_uuid_cmp(uuidFilter->getBase(), &dsc->uuid.u) == 0) {
82-
rc = BLE_HS_EDONE; // Found the descriptor, stop the search
83-
} else {
84-
return 0; // Not the descriptor we are looking for
85-
}
86-
}
87-
79+
// Found descriptor, with either no UUID filter or matched.
80+
if (!rc && (!uuidFilter || !ble_uuid_cmp(uuidFilter->getBase(), &dsc->uuid.u))) {
81+
rc = BLE_HS_EDONE; // Indicate that descriptor was found.
8882
pChr->m_vDescriptors.push_back(new NimBLERemoteDescriptor(pChr, dsc));
89-
return 0;
83+
}
84+
else if (rc != 0) {
85+
NimBLEUtils::taskRelease(*pTaskData, rc);
9086
}
9187

92-
NimBLEUtils::taskRelease(*pTaskData, rc);
9388
NIMBLE_LOGD(LOG_TAG, "<< Descriptor Discovery");
9489
return rc;
9590
}
9691

9792
/**
9893
* @brief Populate the descriptors (if any) for this characteristic.
99-
* @param [in] the end handle of the characteristic, or the service, whichever comes first.
94+
* @param [in] uuidFilter The end handle of the characteristic, or the service, whichever comes first.
95+
* @param [out] out Pointer to get the descriptor if found.
96+
* @return True if successfully retrieved, success = 0 or BLE_HS_EDONE.
10097
*/
101-
bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilter) const {
98+
bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilter, NimBLERemoteDescriptor* out) const {
10299
NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str());
103-
104100
NimBLETaskData taskData(const_cast<NimBLERemoteCharacteristic*>(this));
105101
desc_filter_t filter = {uuidFilter, &taskData};
106102

@@ -118,6 +114,9 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte
118114
rc = taskData.m_flags;
119115
if (rc == 0 || rc == BLE_HS_EDONE) {
120116
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): found %d descriptors.", m_vDescriptors.size());
117+
if (out && rc == BLE_HS_EDONE) {
118+
out = m_vDescriptors.back();
119+
}
121120
return true;
122121
}
123122

@@ -132,8 +131,8 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte
132131
*/
133132
NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUUID& uuid) const {
134133
NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
135-
NimBLERemoteDescriptor* pDsc = nullptr;
136-
size_t prev_size = m_vDescriptors.size();
134+
NimBLERemoteDescriptor* pDsc = nullptr;
135+
NimBLEUUID uuidTmp;
137136

138137
for (const auto& it : m_vDescriptors) {
139138
if (it->getUUID() == uuid) {
@@ -142,36 +141,22 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
142141
}
143142
}
144143

145-
if (retrieveDescriptors(&uuid)) {
146-
if (m_vDescriptors.size() > prev_size) {
147-
pDsc = m_vDescriptors.back();
148-
goto Done;
149-
}
150-
151-
// If the request was successful but 16/32 bit uuid not found
152-
// try again with the 128 bit uuid.
153-
if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
154-
NimBLEUUID uuid128(uuid);
155-
uuid128.to128();
156-
if (retrieveDescriptors(&uuid128)) {
157-
if (m_vDescriptors.size() > prev_size) {
158-
pDsc = m_vDescriptors.back();
159-
}
160-
}
161-
} else {
162-
// If the request was successful but the 128 bit uuid not found
163-
// try again with the 16 bit uuid.
164-
NimBLEUUID uuid16(uuid);
165-
uuid16.to16();
166-
// if the uuid was 128 bit but not of the BLE base type this check will fail
167-
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
168-
if (retrieveDescriptors(&uuid16)) {
169-
if (m_vDescriptors.size() > prev_size) {
170-
pDsc = m_vDescriptors.back();
171-
}
172-
}
173-
}
174-
}
144+
if (!retrieveDescriptors(&uuid, pDsc) || pDsc) {
145+
goto Done;
146+
}
147+
// If the request was successful but 16/32 bit uuid not found
148+
// try again with the 128 bit uuid.
149+
if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
150+
uuidTmp = NimBLEUUID(uuid).to128();
151+
retrieveDescriptors(&uuidTmp, pDsc);
152+
goto Done;
153+
}
154+
// If the request was successful but the 128 bit uuid not found
155+
// try again with the 16 bit uuid.
156+
uuidTmp = NimBLEUUID(uuid).to16();
157+
// if the uuid was 128 bit but not of the BLE base type this check will fail
158+
if (uuidTmp.bitSize() == BLE_UUID_TYPE_16) {
159+
retrieveDescriptors(&uuidTmp, pDsc);
175160
}
176161

177162
Done:
@@ -305,7 +290,7 @@ size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID& uuid) cons
305290
* @return True if supported.
306291
*/
307292
bool NimBLERemoteCharacteristic::canBroadcast() const {
308-
return (m_properties & BLE_GATT_CHR_PROP_BROADCAST) != 0;
293+
return (m_properties & BLE_GATT_CHR_PROP_BROADCAST);
309294
};
310295

311296
/**

src/NimBLERemoteCharacteristic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class NimBLERemoteCharacteristic : public NimBLERemoteValueAttribute {
6565
~NimBLERemoteCharacteristic();
6666

6767
bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true) const;
68-
bool retrieveDescriptors(const NimBLEUUID* uuidFilter = nullptr) const;
68+
bool retrieveDescriptors(const NimBLEUUID* uuidFilter = nullptr, NimBLERemoteDescriptor* out = nullptr) const;
6969

7070
static int descriptorDiscCB(
71-
uint16_t conn_handle, const ble_gatt_error* error, uint16_t chr_val_handle, const ble_gatt_dsc* dsc, void* arg);
71+
uint16_t connHandle, const ble_gatt_error* error, uint16_t chrHandle, const ble_gatt_dsc* dsc, void* arg);
7272

7373
const NimBLERemoteService* m_pRemoteService{nullptr};
7474
uint8_t m_properties{0};

0 commit comments

Comments
 (0)