@@ -76,46 +76,31 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* u
76
76
*/
77
77
NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic (const NimBLEUUID& uuid) const {
78
78
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 ;
81
81
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 ;
85
85
goto Done;
86
86
}
87
87
}
88
88
89
- if (retrieveCharacteristics (&uuid)) {
90
- if (m_vChars.size () > prev_size) {
91
- pChar = m_vChars.back ();
92
- goto Done;
93
- }
89
+ if (!retrieveCharacteristics (&uuid, pChar) || pChar) {
90
+ goto Done;
91
+ }
94
92
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
- }
93
+ // Try again with 128 bit uuid if request succeeded with no uuid found.
94
+ if (uuid.bitSize () == BLE_UUID_TYPE_16 || uuid.bitSize () == BLE_UUID_TYPE_32) {
95
+ uuidTmp = NimBLEUUID (uuid).to128 ();
96
+ retrieveCharacteristics (&uuidTmp, pChar);
97
+ goto Done;
98
+ }
99
+ // Try again with 16 bit uuid if request succeeded with no uuid found.
100
+ // If the uuid was 128 bit but not of the BLE base type this check will fail.
101
+ uuidTmp = NimBLEUUID (uuid).to16 ();
102
+ if (uuidTmp.bitSize () == BLE_UUID_TYPE_16) {
103
+ retrieveCharacteristics (&uuidTmp, pChar);
119
104
}
120
105
121
106
Done:
@@ -143,77 +128,67 @@ const std::vector<NimBLERemoteCharacteristic*>& NimBLERemoteService::getCharacte
143
128
* @brief Callback for Characteristic discovery.
144
129
* @return success == 0 or error code.
145
130
*/
146
- int NimBLERemoteService::characteristicDiscCB (uint16_t conn_handle ,
131
+ int NimBLERemoteService::characteristicDiscCB (uint16_t connHandle ,
147
132
const ble_gatt_error* error,
148
133
const ble_gatt_chr* chr,
149
134
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 );
135
+ const int rc = error->status ;
154
136
auto pTaskData = (NimBLETaskData*)arg;
155
137
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
- }
138
+ NIMBLE_LOGD (LOG_TAG, " Characteristic Discovery >> status: %d handle: %d" , rc, (rc == 0 ) ? chr->def_handle : -1 );
162
139
163
140
// Make sure the discovery is for this device
164
- if (pSvc->getClient ()->getConnHandle () != conn_handle ) {
141
+ if (pSvc->getClient ()->getConnHandle () != connHandle ) {
165
142
return 0 ;
166
143
}
167
144
168
- if (error-> status == 0 ) {
145
+ if (rc == 0 ) {
169
146
pSvc->m_vChars .push_back (new NimBLERemoteCharacteristic (pSvc, chr));
170
147
return 0 ;
171
148
}
172
149
173
- NimBLEUtils::taskRelease (*pTaskData, error-> status );
174
- NIMBLE_LOGD (LOG_TAG, " << Characteristic Discovery" );
175
- return error-> status ;
150
+ NimBLEUtils::taskRelease (*pTaskData, rc );
151
+ NIMBLE_LOGD (LOG_TAG, " << Characteristic Discovery%s " , (rc == BLE_HS_ENOTCONN) ? " ; Not connected " : " " );
152
+ return rc ;
176
153
}
177
154
178
155
/* *
179
156
* @brief Retrieve all the characteristics for this service.
180
157
* This function will not return until we have all the characteristics.
181
- * @return True if successful .
158
+ * @return True if successfully retrieved, success = BLE_HS_EDONE .
182
159
*/
183
- bool NimBLERemoteService::retrieveCharacteristics (const NimBLEUUID* uuidFilter ) const {
160
+ bool NimBLERemoteService::retrieveCharacteristics (const NimBLEUUID* uuid, NimBLERemoteCharacteristic* out ) const {
184
161
NIMBLE_LOGD (LOG_TAG, " >> retrieveCharacteristics()" );
185
- int rc = 0 ;
186
162
NimBLETaskData taskData (const_cast <NimBLERemoteService*>(this ));
163
+ const uint16_t connHandle = m_pClient->getConnHandle ();
164
+ const uint16_t endHandle = getEndHandle ();
165
+ const uint16_t handle = getHandle ();
187
166
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);
167
+ // If this is the last handle then there are no more characteristics
168
+ if (handle == endHandle) {
169
+ NIMBLE_LOGD (LOG_TAG, " << retrieveCharacteristics(): found %d characteristics." , m_vChars.size ());
170
+ return true ;
201
171
}
202
172
173
+ int rc = (uuid == nullptr )
174
+ ? ble_gattc_disc_all_chrs (connHandle, handle, endHandle, characteristicDiscCB, &taskData)
175
+ : ble_gattc_disc_chrs_by_uuid (connHandle, handle, endHandle, uuid->getBase (), characteristicDiscCB, &taskData);
176
+
203
177
if (rc != 0 ) {
204
178
NIMBLE_LOGE (LOG_TAG, " ble_gattc_disc_chrs rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
205
179
return false ;
206
180
}
207
181
208
182
NimBLEUtils::taskWait (taskData, BLE_NPL_TIME_FOREVER);
209
183
rc = taskData.m_flags ;
210
- if (rc == 0 || rc = = BLE_HS_EDONE) {
211
- NIMBLE_LOGD (LOG_TAG, " << retrieveCharacteristics()" );
212
- return true ;
184
+ if (rc ! = BLE_HS_EDONE) {
185
+ NIMBLE_LOGE (LOG_TAG, " << retrieveCharacteristics(): failed: rc=%d %s " , rc, NimBLEUtils::returnCodeToString (rc) );
186
+ return false ;
213
187
}
214
188
215
- NIMBLE_LOGE (LOG_TAG, " << retrieveCharacteristics() rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
216
- return false ;
189
+ *out = m_vChars.back ();
190
+ NIMBLE_LOGD (LOG_TAG, " << retrieveCharacteristics(): found %d characteristics." , m_vChars.size ());
191
+ return true ;
217
192
} // retrieveCharacteristics
218
193
219
194
/* *
@@ -231,11 +206,8 @@ NimBLEClient* NimBLERemoteService::getClient() const {
231
206
*/
232
207
NimBLEAttValue NimBLERemoteService::getValue (const NimBLEUUID& uuid) const {
233
208
const auto pChar = getCharacteristic (uuid);
234
- if (pChar) {
235
- return pChar->readValue ();
236
- }
237
-
238
- return NimBLEAttValue{};
209
+ return pChar ? pChar->readValue ()
210
+ : NimBLEAttValue{};
239
211
} // readValue
240
212
241
213
/* *
@@ -246,11 +218,8 @@ NimBLEAttValue NimBLERemoteService::getValue(const NimBLEUUID& uuid) const {
246
218
*/
247
219
bool NimBLERemoteService::setValue (const NimBLEUUID& uuid, const NimBLEAttValue& value) const {
248
220
const auto pChar = getCharacteristic (uuid);
249
- if (pChar) {
250
- return pChar->writeValue (value);
251
- }
252
-
253
- return false ;
221
+ return pChar ? pChar->writeValue (value)
222
+ : false ;
254
223
} // setValue
255
224
256
225
/* *
@@ -260,8 +229,8 @@ bool NimBLERemoteService::setValue(const NimBLEUUID& uuid, const NimBLEAttValue&
260
229
* them. This method does just that.
261
230
*/
262
231
void NimBLERemoteService::deleteCharacteristics () const {
263
- for (const auto & it : m_vChars) {
264
- delete it ;
232
+ for (const auto & chr : m_vChars) {
233
+ delete chr ;
265
234
}
266
235
std::vector<NimBLERemoteCharacteristic*>{}.swap (m_vChars);
267
236
} // deleteCharacteristics
0 commit comments