Skip to content

Commit eec32d5

Browse files
committed
refactor(RemoteServ): Reduce nesting
* Adds parameter out to retrieveCharacteristics, default value works as the original method. * out: if not nullptr, will allow caller to obtain the characteristic * Add check to retrieveCharacteristics, return if found the last handle * General cleanup
1 parent 5ac7272 commit eec32d5

File tree

2 files changed

+58
-90
lines changed

2 files changed

+58
-90
lines changed

src/NimBLERemoteService.cpp

Lines changed: 54 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -76,50 +76,34 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* u
7676
*/
7777
NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEUUID& uuid) const {
7878
NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str());
79-
NimBLERemoteCharacteristic* pChar = nullptr;
80-
size_t prev_size = m_vChars.size();
79+
NimBLERemoteCharacteristic* pChar = nullptr;
80+
NimBLEUUID uuidTmp;
8181

82-
for (const auto& it : m_vChars) {
83-
if (it->getUUID() == uuid) {
84-
pChar = it;
82+
for (const auto& chr : m_vChars) {
83+
if (chr->getUUID() == uuid) {
84+
pChar = chr;
8585
goto Done;
8686
}
8787
}
8888

89-
if (retrieveCharacteristics(&uuid)) {
90-
if (m_vChars.size() > prev_size) {
91-
pChar = m_vChars.back();
92-
goto Done;
93-
}
94-
95-
// If the request was successful but 16/32 bit uuid not found
96-
// try again with the 128 bit uuid.
97-
if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
98-
NimBLEUUID uuid128(uuid);
99-
uuid128.to128();
100-
if (retrieveCharacteristics(&uuid128)) {
101-
if (m_vChars.size() > prev_size) {
102-
pChar = m_vChars.back();
103-
}
104-
}
105-
} else {
106-
// If the request was successful but the 128 bit uuid not found
107-
// try again with the 16 bit uuid.
108-
NimBLEUUID uuid16(uuid);
109-
uuid16.to16();
110-
// if the uuid was 128 bit but not of the BLE base type this check will fail
111-
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
112-
if (retrieveCharacteristics(&uuid16)) {
113-
if (m_vChars.size() > prev_size) {
114-
pChar = m_vChars.back();
115-
}
116-
}
117-
}
118-
}
89+
if (!retrieveCharacteristics(&uuid, pChar) || pChar) {
90+
goto Done;
91+
}
92+
// Try again with 128 bit uuid if request succeeded with no uuid found.
93+
if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
94+
uuidTmp = NimBLEUUID(uuid).to128();
95+
retrieveCharacteristics(&uuidTmp, pChar);
96+
goto Done;
97+
}
98+
// Try again with 16 bit uuid if request succeeded with no uuid found.
99+
// If the uuid was 128 bit but not of the BLE base type this check will fail.
100+
uuidTmp = NimBLEUUID(uuid).to16();
101+
if (uuidTmp.bitSize() == BLE_UUID_TYPE_16) {
102+
retrieveCharacteristics(&uuidTmp, pChar);
119103
}
120104

121105
Done:
122-
NIMBLE_LOGD(LOG_TAG, "<< Characteristic %sfound", pChar ? "" : "not ");
106+
NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: %sfound", !pChar ? "not " : "");
123107
return pChar;
124108
} // getCharacteristic
125109

@@ -143,77 +127,66 @@ const std::vector<NimBLERemoteCharacteristic*>& NimBLERemoteService::getCharacte
143127
* @brief Callback for Characteristic discovery.
144128
* @return success == 0 or error code.
145129
*/
146-
int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle,
130+
int NimBLERemoteService::characteristicDiscCB(uint16_t connHandle,
147131
const ble_gatt_error* error,
148132
const ble_gatt_chr* chr,
149133
void* arg) {
150-
NIMBLE_LOGD(LOG_TAG,
151-
"Characteristic Discovery >> status: %d handle: %d",
152-
error->status,
153-
(error->status == 0) ? chr->def_handle : -1);
134+
const int rc = error->status;
154135
auto pTaskData = (NimBLETaskData*)arg;
155136
const auto pSvc = (NimBLERemoteService*)pTaskData->m_pInstance;
156-
157-
if (error->status == BLE_HS_ENOTCONN) {
158-
NIMBLE_LOGE(LOG_TAG, "<< Characteristic Discovery; Not connected");
159-
NimBLEUtils::taskRelease(*pTaskData, error->status);
160-
return error->status;
161-
}
137+
NIMBLE_LOGD(LOG_TAG, "Characteristic Discovery >> status: %d handle: %d", rc, (rc == 0) ? chr->def_handle : -1);
162138

163139
// Make sure the discovery is for this device
164-
if (pSvc->getClient()->getConnHandle() != conn_handle) {
140+
if (pSvc->getClient()->getConnHandle() != connHandle) {
165141
return 0;
166142
}
167143

168-
if (error->status == 0) {
144+
if (rc == 0) {
169145
pSvc->m_vChars.push_back(new NimBLERemoteCharacteristic(pSvc, chr));
170146
return 0;
171147
}
172148

173-
NimBLEUtils::taskRelease(*pTaskData, error->status);
174-
NIMBLE_LOGD(LOG_TAG, "<< Characteristic Discovery");
175-
return error->status;
149+
NimBLEUtils::taskRelease(*pTaskData, rc);
150+
NIMBLE_LOGD(LOG_TAG, "<< Characteristic Discovery%s", (rc == BLE_HS_ENOTCONN) ? "; Not connected" : "");
151+
return rc;
176152
}
177153

178154
/**
179155
* @brief Retrieve all the characteristics for this service.
180156
* This function will not return until we have all the characteristics.
181-
* @return True if successful.
157+
* @return True if successfully retrieved, success = BLE_HS_EDONE.
182158
*/
183-
bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID* uuidFilter) const {
159+
bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID* uuid, NimBLERemoteCharacteristic* out) const {
184160
NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics()");
185-
int rc = 0;
186161
NimBLETaskData taskData(const_cast<NimBLERemoteService*>(this));
187-
188-
if (uuidFilter == nullptr) {
189-
rc = ble_gattc_disc_all_chrs(m_pClient->getConnHandle(),
190-
getHandle(),
191-
getEndHandle(),
192-
NimBLERemoteService::characteristicDiscCB,
193-
&taskData);
194-
} else {
195-
rc = ble_gattc_disc_chrs_by_uuid(m_pClient->getConnHandle(),
196-
getHandle(),
197-
getEndHandle(),
198-
uuidFilter->getBase(),
199-
NimBLERemoteService::characteristicDiscCB,
200-
&taskData);
162+
const uint16_t connHandle = m_pClient->getConnHandle();
163+
const uint16_t endHandle = getEndHandle();
164+
const uint16_t handle = getHandle();
165+
// If this is the last handle then there are no more characteristics
166+
if (handle == endHandle) {
167+
NIMBLE_LOGD(LOG_TAG, "<< retrieveCharacteristics(): found %d characteristics.", m_vChars.size());
168+
return true;
201169
}
202170

171+
int rc = (uuid == nullptr)
172+
? ble_gattc_disc_all_chrs(connHandle, handle, endHandle, characteristicDiscCB, &taskData)
173+
: ble_gattc_disc_chrs_by_uuid(connHandle, handle, endHandle, uuid->getBase(), characteristicDiscCB, &taskData);
174+
203175
if (rc != 0) {
204176
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_chrs rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
205177
return false;
206178
}
207179

208180
NimBLEUtils::taskWait(taskData, BLE_NPL_TIME_FOREVER);
209181
rc = taskData.m_flags;
210-
if (rc == 0 || rc == BLE_HS_EDONE) {
211-
NIMBLE_LOGD(LOG_TAG, "<< retrieveCharacteristics()");
212-
return true;
182+
if (rc != BLE_HS_EDONE) {
183+
NIMBLE_LOGE(LOG_TAG, "<< retrieveCharacteristics(): failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
184+
return false;
213185
}
214186

215-
NIMBLE_LOGE(LOG_TAG, "<< retrieveCharacteristics() rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
216-
return false;
187+
out = m_vChars.back();
188+
NIMBLE_LOGD(LOG_TAG, "<< retrieveCharacteristics(): found %d characteristics.", m_vChars.size());
189+
return true;
217190
} // retrieveCharacteristics
218191

219192
/**
@@ -231,11 +204,8 @@ NimBLEClient* NimBLERemoteService::getClient() const {
231204
*/
232205
NimBLEAttValue NimBLERemoteService::getValue(const NimBLEUUID& uuid) const {
233206
const auto pChar = getCharacteristic(uuid);
234-
if (pChar) {
235-
return pChar->readValue();
236-
}
237-
238-
return NimBLEAttValue{};
207+
return pChar ? pChar->readValue()
208+
: NimBLEAttValue{};
239209
} // readValue
240210

241211
/**
@@ -246,11 +216,8 @@ NimBLEAttValue NimBLERemoteService::getValue(const NimBLEUUID& uuid) const {
246216
*/
247217
bool NimBLERemoteService::setValue(const NimBLEUUID& uuid, const NimBLEAttValue& value) const {
248218
const auto pChar = getCharacteristic(uuid);
249-
if (pChar) {
250-
return pChar->writeValue(value);
251-
}
252-
253-
return false;
219+
return pChar ? pChar->writeValue(value)
220+
: false;
254221
} // setValue
255222

256223
/**
@@ -260,8 +227,8 @@ bool NimBLERemoteService::setValue(const NimBLEUUID& uuid, const NimBLEAttValue&
260227
* them. This method does just that.
261228
*/
262229
void NimBLERemoteService::deleteCharacteristics() const {
263-
for (const auto& it : m_vChars) {
264-
delete it;
230+
for (const auto& chr : m_vChars) {
231+
delete chr;
265232
}
266233
std::vector<NimBLERemoteCharacteristic*>{}.swap(m_vChars);
267234
} // deleteCharacteristics
@@ -303,4 +270,4 @@ std::string NimBLERemoteService::toString() const {
303270
return res;
304271
} // toString
305272

306-
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
273+
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */

src/NimBLERemoteService.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class NimBLERemoteService : public NimBLEAttribute {
5353

5454
NimBLERemoteService(NimBLEClient* pClient, const struct ble_gatt_svc* service);
5555
~NimBLERemoteService();
56-
bool retrieveCharacteristics(const NimBLEUUID* uuidFilter = nullptr) const;
57-
static int characteristicDiscCB(uint16_t conn_handle,
56+
bool retrieveCharacteristics(const NimBLEUUID* uuidFilter = nullptr,
57+
NimBLERemoteCharacteristic* out = nullptr) const;
58+
static int characteristicDiscCB(uint16_t connHandle,
5859
const struct ble_gatt_error* error,
5960
const struct ble_gatt_chr* chr,
6061
void* arg);
@@ -65,4 +66,4 @@ class NimBLERemoteService : public NimBLEAttribute {
6566
}; // NimBLERemoteService
6667

6768
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
68-
#endif /* NIMBLE_CPP_REMOTE_SERVICE_H_*/
69+
#endif /* NIMBLE_CPP_REMOTE_SERVICE_H_*/

0 commit comments

Comments
 (0)