Skip to content

Commit 986d242

Browse files
committed
refactor(RemoteChar): Reduce nesting
1 parent 5ac7272 commit 986d242

File tree

2 files changed

+39
-53
lines changed

2 files changed

+39
-53
lines changed

src/NimBLERemoteCharacteristic.cpp

Lines changed: 37 additions & 51 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,33 +72,30 @@ 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
}
78-
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-
88-
pChr->m_vDescriptors.push_back(new NimBLERemoteDescriptor(pChr, dsc));
89-
return 0;
78+
if (rc != 0) {
79+
NimBLEUtils::taskRelease(*pTaskData, rc);
80+
goto Done;
81+
}
82+
if (uuidFilter && ble_uuid_cmp(uuidFilter->getBase(), &dsc->uuid.u)) {
83+
goto Done;
9084
}
9185

92-
NimBLEUtils::taskRelease(*pTaskData, rc);
86+
rc = BLE_HS_EDONE; // Indicate that descriptor was found.
87+
pChr->m_vDescriptors.push_back(new NimBLERemoteDescriptor(pChr, dsc));
88+
Done:
9389
NIMBLE_LOGD(LOG_TAG, "<< Descriptor Discovery");
9490
return rc;
9591
}
9692

9793
/**
9894
* @brief Populate the descriptors (if any) for this characteristic.
99-
* @param [in] the end handle of the characteristic, or the service, whichever comes first.
95+
* @param [in] uuidFilter The end handle of the characteristic, or the service, whichever comes first.
96+
* @param [out] out Pointer to get the descriptor if found.
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());
103100

104101
NimBLETaskData taskData(const_cast<NimBLERemoteCharacteristic*>(this));
@@ -118,6 +115,9 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID* uuidFilte
118115
rc = taskData.m_flags;
119116
if (rc == 0 || rc == BLE_HS_EDONE) {
120117
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): found %d descriptors.", m_vDescriptors.size());
118+
if (out && rc == BLE_HS_EDONE) {
119+
out = m_vDescriptors.back();
120+
}
121121
return true;
122122
}
123123

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

138138
for (const auto& it : m_vDescriptors) {
139139
if (it->getUUID() == uuid) {
@@ -142,36 +142,22 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
142142
}
143143
}
144144

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-
}
145+
if (!retrieveDescriptors(&uuid, pDsc) || pDsc) {
146+
goto Done;
147+
}
148+
// If the request was successful but 16/32 bit uuid not found
149+
// try again with the 128 bit uuid.
150+
else if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
151+
uuidTmp = NimBLEUUID(uuid).to128();
152+
retrieveDescriptors(&uuidTmp, pDsc);
153+
goto Done;
154+
}
155+
// If the request was successful but the 128 bit uuid not found
156+
// try again with the 16 bit uuid.
157+
uuidTmp = NimBLEUUID(uuid).to16();
158+
// if the uuid was 128 bit but not of the BLE base type this check will fail
159+
if (uuidTmp.bitSize() == BLE_UUID_TYPE_16) {
160+
retrieveDescriptors(&uuidTmp, pDsc);
175161
}
176162

177163
Done:
@@ -305,7 +291,7 @@ size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID& uuid) cons
305291
* @return True if supported.
306292
*/
307293
bool NimBLERemoteCharacteristic::canBroadcast() const {
308-
return (m_properties & BLE_GATT_CHR_PROP_BROADCAST) != 0;
294+
return (m_properties & BLE_GATT_CHR_PROP_BROADCAST);
309295
};
310296

311297
/**

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)