@@ -76,50 +76,34 @@ 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
- }
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);
119
103
}
120
104
121
105
Done:
122
- NIMBLE_LOGD (LOG_TAG, " << Characteristic %sfound" , pChar ? " " : " not " );
106
+ NIMBLE_LOGD (LOG_TAG, " << getCharacteristic: %sfound" , ! pChar ? " not " : " " );
123
107
return pChar;
124
108
} // getCharacteristic
125
109
@@ -143,77 +127,66 @@ const std::vector<NimBLERemoteCharacteristic*>& NimBLERemoteService::getCharacte
143
127
* @brief Callback for Characteristic discovery.
144
128
* @return success == 0 or error code.
145
129
*/
146
- int NimBLERemoteService::characteristicDiscCB (uint16_t conn_handle ,
130
+ int NimBLERemoteService::characteristicDiscCB (uint16_t connHandle ,
147
131
const ble_gatt_error* error,
148
132
const ble_gatt_chr* chr,
149
133
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 ;
154
135
auto pTaskData = (NimBLETaskData*)arg;
155
136
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 );
162
138
163
139
// Make sure the discovery is for this device
164
- if (pSvc->getClient ()->getConnHandle () != conn_handle ) {
140
+ if (pSvc->getClient ()->getConnHandle () != connHandle ) {
165
141
return 0 ;
166
142
}
167
143
168
- if (error-> status == 0 ) {
144
+ if (rc == 0 ) {
169
145
pSvc->m_vChars .push_back (new NimBLERemoteCharacteristic (pSvc, chr));
170
146
return 0 ;
171
147
}
172
148
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 ;
176
152
}
177
153
178
154
/* *
179
155
* @brief Retrieve all the characteristics for this service.
180
156
* 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 .
182
158
*/
183
- bool NimBLERemoteService::retrieveCharacteristics (const NimBLEUUID* uuidFilter ) const {
159
+ bool NimBLERemoteService::retrieveCharacteristics (const NimBLEUUID* uuid, NimBLERemoteCharacteristic* out ) const {
184
160
NIMBLE_LOGD (LOG_TAG, " >> retrieveCharacteristics()" );
185
- int rc = 0 ;
186
161
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 ;
201
169
}
202
170
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
+
203
175
if (rc != 0 ) {
204
176
NIMBLE_LOGE (LOG_TAG, " ble_gattc_disc_chrs rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
205
177
return false ;
206
178
}
207
179
208
180
NimBLEUtils::taskWait (taskData, BLE_NPL_TIME_FOREVER);
209
181
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 ;
213
185
}
214
186
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 ;
217
190
} // retrieveCharacteristics
218
191
219
192
/* *
@@ -231,11 +204,8 @@ NimBLEClient* NimBLERemoteService::getClient() const {
231
204
*/
232
205
NimBLEAttValue NimBLERemoteService::getValue (const NimBLEUUID& uuid) const {
233
206
const auto pChar = getCharacteristic (uuid);
234
- if (pChar) {
235
- return pChar->readValue ();
236
- }
237
-
238
- return NimBLEAttValue{};
207
+ return pChar ? pChar->readValue ()
208
+ : NimBLEAttValue{};
239
209
} // readValue
240
210
241
211
/* *
@@ -246,11 +216,8 @@ NimBLEAttValue NimBLERemoteService::getValue(const NimBLEUUID& uuid) const {
246
216
*/
247
217
bool NimBLERemoteService::setValue (const NimBLEUUID& uuid, const NimBLEAttValue& value) const {
248
218
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 ;
254
221
} // setValue
255
222
256
223
/* *
@@ -260,8 +227,8 @@ bool NimBLERemoteService::setValue(const NimBLEUUID& uuid, const NimBLEAttValue&
260
227
* them. This method does just that.
261
228
*/
262
229
void NimBLERemoteService::deleteCharacteristics () const {
263
- for (const auto & it : m_vChars) {
264
- delete it ;
230
+ for (const auto & chr : m_vChars) {
231
+ delete chr ;
265
232
}
266
233
std::vector<NimBLERemoteCharacteristic*>{}.swap (m_vChars);
267
234
} // deleteCharacteristics
@@ -303,4 +270,4 @@ std::string NimBLERemoteService::toString() const {
303
270
return res;
304
271
} // toString
305
272
306
- #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
273
+ #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
0 commit comments