@@ -268,42 +268,58 @@ bool NimBLECharacteristic::sendValue(const uint8_t* value, size_t length, bool i
268
268
int rc = 0 ;
269
269
270
270
if (value != nullptr && length > 0 ) { // custom notification value
271
- // Notify all connected peers unless a specific handle is provided
272
- for (const auto & ch : NimBLEDevice::getServer ()->getPeerDevices ()) {
273
- if (connHandle != BLE_HS_CONN_HANDLE_NONE && ch != connHandle) {
274
- continue ; // only send to the specific handle, minor inefficiency but saves code.
271
+ os_mbuf* om = nullptr ;
272
+
273
+ if (connHandle != BLE_HS_CONN_HANDLE_NONE) { // only sending to specific peer
274
+ om = ble_hs_mbuf_from_flat (value, length);
275
+ if (!om) {
276
+ rc = BLE_HS_ENOMEM;
277
+ goto done;
278
+ }
279
+
280
+ // Null buffer will read the value from the characteristic
281
+ if (isNotification) {
282
+ rc = ble_gattc_notify_custom (connHandle, m_handle, om);
283
+ } else {
284
+ rc = ble_gattc_indicate_custom (connHandle, m_handle, om);
275
285
}
276
286
287
+ goto done;
288
+ }
289
+
290
+ // Notify all connected peers unless a specific handle is provided
291
+ for (const auto & ch : NimBLEDevice::getServer ()->getPeerDevices ()) {
277
292
// Must re-create the data buffer on each iteration because it is freed by the calls bellow.
278
- os_mbuf* om = ble_hs_mbuf_from_flat (value, length);
293
+ om = ble_hs_mbuf_from_flat (value, length);
279
294
if (!om) {
280
- NIMBLE_LOGE (LOG_TAG, " << sendValue: failed to allocate mbuf " ) ;
281
- return false ;
295
+ rc = BLE_HS_ENOMEM ;
296
+ goto done ;
282
297
}
283
298
284
299
if (isNotification) {
285
300
rc = ble_gattc_notify_custom (ch, m_handle, om);
286
301
} else {
287
302
rc = ble_gattc_indicate_custom (ch, m_handle, om);
288
303
}
289
-
290
- if (rc != 0 ) {
291
- NIMBLE_LOGE (LOG_TAG, " << sendValue: failed to send value, rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
292
- break ;
293
- }
294
304
}
295
- } else if (connHandle != BLE_HS_CONN_HANDLE_NONE) { // only sending to specific peer
305
+ } else if (connHandle != BLE_HS_CONN_HANDLE_NONE) {
296
306
// Null buffer will read the value from the characteristic
297
307
if (isNotification) {
298
- rc = ble_gattc_notify_custom (connHandle, m_handle, NULL );
308
+ rc = ble_gattc_notify_custom (connHandle, m_handle, nullptr );
299
309
} else {
300
- rc = ble_gattc_indicate_custom (connHandle, m_handle, NULL );
310
+ rc = ble_gattc_indicate_custom (connHandle, m_handle, nullptr );
301
311
}
302
312
} else { // Notify or indicate to all connected peers the characteristic value
303
313
ble_gatts_chr_updated (m_handle);
304
314
}
305
315
306
- return rc == 0 ;
316
+ done:
317
+ if (rc != 0 ) {
318
+ NIMBLE_LOGE (LOG_TAG, " failed to send value, rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
319
+ return false ;
320
+ }
321
+
322
+ return true ;
307
323
} // sendValue
308
324
309
325
void NimBLECharacteristic::readEvent (NimBLEConnInfo& connInfo) {
0 commit comments