@@ -480,10 +480,20 @@ void BLECharacteristic::notify(bool is_notification) {
480
480
assert (getService () != nullptr );
481
481
assert (getService ()->getServer () != nullptr );
482
482
483
+ auto cb = BLECharacteristicCallbacks ();
484
+
485
+ BLECharacteristicCallbacks * callback = &cb;
486
+ if (m_pCallbacks != nullptr ){
487
+ callback = m_pCallbacks;
488
+ }
489
+
490
+ callback->onNotify (this ); // Invoke the notify callback.
491
+
483
492
GeneralUtils::hexDump ((uint8_t *)m_value.getValue ().data (), m_value.getValue ().length ());
484
493
485
494
if (getService ()->getServer ()->getConnectedCount () == 0 ) {
486
495
log_v (" << notify: No connected clients." );
496
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_NO_CLIENT, 0 );
487
497
return ;
488
498
}
489
499
@@ -494,12 +504,14 @@ void BLECharacteristic::notify(bool is_notification) {
494
504
if (is_notification) {
495
505
if (p2902 != nullptr && !p2902->getNotifications ()) {
496
506
log_v (" << notifications disabled; ignoring" );
507
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_NOTIFY_DISABLED, 0 ); // Invoke the notify callback.
497
508
return ;
498
509
}
499
510
}
500
511
else {
501
512
if (p2902 != nullptr && !p2902->getIndications ()) {
502
513
log_v (" << indications disabled; ignoring" );
514
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED, 0 ); // Invoke the notify callback.
503
515
return ;
504
516
}
505
517
}
@@ -510,7 +522,7 @@ void BLECharacteristic::notify(bool is_notification) {
510
522
}
511
523
512
524
size_t length = m_value.getValue ().length ();
513
- if (!is_notification)
525
+ if (!is_notification) // is indication
514
526
m_semaphoreConfEvt.take (" indicate" );
515
527
esp_err_t errRc = ::esp_ble_gatts_send_indicate (
516
528
getService ()->getServer ()->getGattsIf (),
@@ -519,10 +531,23 @@ void BLECharacteristic::notify(bool is_notification) {
519
531
if (errRc != ESP_OK) {
520
532
log_e (" << esp_ble_gatts_send_ %s: rc=%d %s" ,is_notification?" notify" :" indicate" , errRc, GeneralUtils::errorToString (errRc));
521
533
m_semaphoreConfEvt.give ();
534
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_GATT, errRc); // Invoke the notify callback.
522
535
return ;
523
536
}
524
- if (!is_notification)
525
- m_semaphoreConfEvt.wait (" indicate" );
537
+ if (!is_notification){ // is indication
538
+ if (!m_semaphoreConfEvt.timedWait (" indicate" , indicationTimeout)){
539
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_INDICATE_TIMEOUT, 0 ); // Invoke the notify callback.
540
+ } else {
541
+ auto code = (esp_gatt_status_t ) m_semaphoreConfEvt.value ();
542
+ if (code == ESP_GATT_OK) {
543
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::SUCCESS_INDICATE, code); // Invoke the notify callback.
544
+ } else {
545
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_INDICATE_FAILURE, code);
546
+ }
547
+ }
548
+ } else {
549
+ callback->onStatus (this , BLECharacteristicCallbacks::Status::SUCCESS_NOTIFY, 0 ); // Invoke the notify callback.
550
+ }
526
551
}
527
552
log_v (" << notify" );
528
553
} // Notify
@@ -754,4 +779,27 @@ void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic) {
754
779
log_d (" BLECharacteristicCallbacks" , " << onWrite" );
755
780
} // onWrite
756
781
782
+
783
+ /* *
784
+ * @brief Callback function to support a Notify request.
785
+ * @param [in] pCharacteristic The characteristic that is the source of the event.
786
+ */
787
+ void BLECharacteristicCallbacks::onNotify (BLECharacteristic* pCharacteristic) {
788
+ log_d (" BLECharacteristicCallbacks" , " >> onNotify: default" );
789
+ log_d (" BLECharacteristicCallbacks" , " << onNotify" );
790
+ } // onNotify
791
+
792
+
793
+ /* *
794
+ * @brief Callback function to support a Notify/Indicate Status report.
795
+ * @param [in] pCharacteristic The characteristic that is the source of the event.
796
+ * @param [in] s Status of the notification/indication
797
+ * @param [in] code Additional code of underlying errors
798
+ */
799
+ void BLECharacteristicCallbacks::onStatus (BLECharacteristic* pCharacteristic, Status s, uint32_t code) {
800
+ log_d (" BLECharacteristicCallbacks" , " >> onStatus: default" );
801
+ log_d (" BLECharacteristicCallbacks" , " << onStatus" );
802
+ } // onStatus
803
+
804
+
757
805
#endif /* CONFIG_BT_ENABLED */
0 commit comments