@@ -39,6 +39,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(
39
39
m_charProp = charProp;
40
40
m_pRemoteService = pRemoteService;
41
41
m_notifyCallback = nullptr ;
42
+ m_pRemoteCharacteristicCallbacks = nullptr ;
42
43
m_rawData = nullptr ;
43
44
m_auth = ESP_GATT_AUTH_REQ_NONE;
44
45
@@ -164,6 +165,10 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event,
164
165
log_d (" Invoking callback for notification on characteristic %s" , toString ().c_str ());
165
166
m_notifyCallback (this , evtParam->notify .value , evtParam->notify .value_len , evtParam->notify .is_notify );
166
167
} // End we have a callback function ...
168
+ if (m_pRemoteCharacteristicCallbacks != nullptr ) {
169
+ log_d (" Invoking callback for notification on characteristic %s" , toString ().c_str ());
170
+ m_pRemoteCharacteristicCallbacks->onNotify (this , evtParam->notify .value , evtParam->notify .value_len , evtParam->notify .is_notify );
171
+ }
167
172
break ;
168
173
} // ESP_GATTC_NOTIFY_EVT
169
174
@@ -461,44 +466,43 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
461
466
m_semaphoreRegForNotifyEvt.take (" registerForNotify" );
462
467
463
468
if (notifyCallback != nullptr ) { // If we have a callback function, then this is a registration.
464
- esp_err_t errRc = ::esp_ble_gattc_register_for_notify (
465
- m_pRemoteService->getClient ()->getGattcIf (),
466
- *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
467
- getHandle ()
468
- );
469
-
470
- if (errRc != ESP_OK) {
471
- log_e (" esp_ble_gattc_register_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
472
- }
473
-
474
- uint8_t val[] = {0x01 , 0x00 };
475
- if (!notifications) val[0 ] = 0x02 ;
476
- BLERemoteDescriptor* desc = getDescriptor (BLEUUID ((uint16_t )0x2902 ));
477
- if (desc != nullptr && descriptorRequiresRegistration)
478
- desc->writeValue (val, 2 , true );
469
+ registerNotifications (notifications, descriptorRequiresRegistration);
479
470
} // End Register
480
471
else { // If we weren't passed a callback function, then this is an unregistration.
481
- esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify (
482
- m_pRemoteService->getClient ()->getGattcIf (),
483
- *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
484
- getHandle ()
485
- );
486
-
487
- if (errRc != ESP_OK) {
488
- log_e (" esp_ble_gattc_unregister_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
489
- }
490
-
491
- uint8_t val[] = {0x00 , 0x00 };
492
- BLERemoteDescriptor* desc = getDescriptor ((uint16_t )0x2902 );
493
- if (desc != nullptr && descriptorRequiresRegistration)
494
- desc->writeValue (val, 2 , true );
472
+ unregisterNotifications (descriptorRequiresRegistration);
495
473
} // End Unregister
496
474
497
475
m_semaphoreRegForNotifyEvt.wait (" registerForNotify" );
498
476
499
477
log_v (" << registerForNotify()" );
500
478
} // registerForNotify
501
479
480
+ /* *
481
+ * @brief Set the characteristics callbacks.
482
+ *
483
+ * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we are
484
+ * unregistering a notification.
485
+ * @return N/A.
486
+ */
487
+ void BLERemoteCharacteristic::setCallbacks (BLERemoteCharacteristicCallbacks* pCallbacks, bool notifications, bool descriptorRequiresRegistration) {
488
+ log_v (" >> setCallbacks(): %s" , toString ().c_str ());
489
+
490
+ m_pRemoteCharacteristicCallbacks = pCallbacks; // Save the notification callback.
491
+
492
+ m_semaphoreRegForNotifyEvt.take (" registerForNotify" );
493
+
494
+ if (pCallbacks != nullptr ) { // If we have a callback function, then this is a registration.
495
+ registerNotifications (notifications, descriptorRequiresRegistration);
496
+ } // End Register
497
+ else { // If we weren't passed a callback function, then this is an unregistration.
498
+ unregisterNotifications (descriptorRequiresRegistration);
499
+ } // End Unregister
500
+
501
+ m_semaphoreRegForNotifyEvt.wait (" registerForNotify" );
502
+
503
+ log_v (" << setCallbacks()" );
504
+ } // setCallbacks
505
+
502
506
503
507
/* *
504
508
* @brief Delete the descriptors in the descriptor map.
@@ -516,6 +520,41 @@ void BLERemoteCharacteristic::removeDescriptors() {
516
520
m_descriptorMap.clear (); // Technically not neeeded, but just to be sure.
517
521
} // removeCharacteristics
518
522
523
+ void BLERemoteCharacteristic::registerNotifications (bool notifications, bool descriptorRequiresRegistration) {
524
+ esp_err_t errRc = ::esp_ble_gattc_register_for_notify (
525
+ m_pRemoteService->getClient ()->getGattcIf (),
526
+ *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
527
+ getHandle ()
528
+ );
529
+
530
+ if (errRc != ESP_OK) {
531
+ log_e (" esp_ble_gattc_register_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
532
+ }
533
+
534
+ uint8_t val[] = {0x01 , 0x00 };
535
+ if (!notifications) val[0 ] = 0x02 ;
536
+ BLERemoteDescriptor* desc = getDescriptor (BLEUUID ((uint16_t )0x2902 ));
537
+ if (desc != nullptr && descriptorRequiresRegistration)
538
+ desc->writeValue (val, 2 , true );
539
+ } // registerNotifications
540
+
541
+ void BLERemoteCharacteristic::unregisterNotifications (bool descriptorRequiresRegistration) {
542
+ esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify (
543
+ m_pRemoteService->getClient ()->getGattcIf (),
544
+ *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
545
+ getHandle ()
546
+ );
547
+
548
+ if (errRc != ESP_OK) {
549
+ log_e (" esp_ble_gattc_unregister_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
550
+ }
551
+
552
+ uint8_t val[] = {0x00 , 0x00 };
553
+ BLERemoteDescriptor* desc = getDescriptor ((uint16_t )0x2902 );
554
+ if (desc != nullptr && descriptorRequiresRegistration)
555
+ desc->writeValue (val, 2 , true );
556
+ } // unregisterNotifications
557
+
519
558
520
559
/* *
521
560
* @brief Convert a BLERemoteCharacteristic to a string representation;
0 commit comments