22
22
23
23
#define NULL_HANDLE (0xffff )
24
24
25
+ static BLECharacteristicCallbacks defaultCallback; // null-object-pattern
25
26
26
27
/* *
27
28
* @brief Construct a characteristic
@@ -40,7 +41,7 @@ BLECharacteristic::BLECharacteristic(BLEUUID uuid, uint32_t properties) {
40
41
m_bleUUID = uuid;
41
42
m_handle = NULL_HANDLE;
42
43
m_properties = (esp_gatt_char_prop_t )0 ;
43
- m_pCallbacks = nullptr ;
44
+ m_pCallbacks = &defaultCallback ;
44
45
45
46
setBroadcastProperty ((properties & PROPERTY_BROADCAST) != 0 );
46
47
setReadProperty ((properties & PROPERTY_READ) != 0 );
@@ -220,9 +221,7 @@ void BLECharacteristic::handleGATTServerEvent(
220
221
case ESP_GATTS_EXEC_WRITE_EVT: {
221
222
if (param->exec_write .exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
222
223
m_value.commit ();
223
- if (m_pCallbacks != nullptr ) {
224
- m_pCallbacks->onWrite (this ); // Invoke the onWrite callback handler.
225
- }
224
+ m_pCallbacks->onWrite (this ); // Invoke the onWrite callback handler.
226
225
} else {
227
226
m_value.cancel ();
228
227
}
@@ -307,7 +306,7 @@ void BLECharacteristic::handleGATTServerEvent(
307
306
}
308
307
} // Response needed
309
308
310
- if (m_pCallbacks != nullptr && param->write .is_prep != true ) {
309
+ if (param->write .is_prep != true ) {
311
310
m_pCallbacks->onWrite (this ); // Invoke the onWrite callback handler.
312
311
}
313
312
} // Match on handles.
@@ -378,9 +377,9 @@ void BLECharacteristic::handleGATTServerEvent(
378
377
}
379
378
} else { // read.is_long == false
380
379
381
- if (m_pCallbacks != nullptr ) { // If is.long is false then this is the first (or only) request to read data, so invoke the callback
382
- m_pCallbacks-> onRead ( this ); // Invoke the read callback.
383
- }
380
+ // If is.long is false then this is the first (or only) request to read data, so invoke the callback
381
+ // Invoke the read callback.
382
+ m_pCallbacks-> onRead ( this );
384
383
385
384
std::string value = m_value.getValue ();
386
385
@@ -480,10 +479,13 @@ void BLECharacteristic::notify(bool is_notification) {
480
479
assert (getService () != nullptr );
481
480
assert (getService ()->getServer () != nullptr );
482
481
482
+ m_pCallbacks->onNotify (this ); // Invoke the notify callback.
483
+
483
484
GeneralUtils::hexDump ((uint8_t *)m_value.getValue ().data (), m_value.getValue ().length ());
484
485
485
486
if (getService ()->getServer ()->getConnectedCount () == 0 ) {
486
487
log_v (" << notify: No connected clients." );
488
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_NO_CLIENT, 0 );
487
489
return ;
488
490
}
489
491
@@ -494,12 +496,14 @@ void BLECharacteristic::notify(bool is_notification) {
494
496
if (is_notification) {
495
497
if (p2902 != nullptr && !p2902->getNotifications ()) {
496
498
log_v (" << notifications disabled; ignoring" );
499
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_NOTIFY_DISABLED, 0 ); // Invoke the notify callback.
497
500
return ;
498
501
}
499
502
}
500
503
else {
501
504
if (p2902 != nullptr && !p2902->getIndications ()) {
502
505
log_v (" << indications disabled; ignoring" );
506
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED, 0 ); // Invoke the notify callback.
503
507
return ;
504
508
}
505
509
}
@@ -510,7 +514,7 @@ void BLECharacteristic::notify(bool is_notification) {
510
514
}
511
515
512
516
size_t length = m_value.getValue ().length ();
513
- if (!is_notification)
517
+ if (!is_notification) // is indication
514
518
m_semaphoreConfEvt.take (" indicate" );
515
519
esp_err_t errRc = ::esp_ble_gatts_send_indicate (
516
520
getService ()->getServer ()->getGattsIf (),
@@ -519,10 +523,23 @@ void BLECharacteristic::notify(bool is_notification) {
519
523
if (errRc != ESP_OK) {
520
524
log_e (" << esp_ble_gatts_send_ %s: rc=%d %s" ,is_notification?" notify" :" indicate" , errRc, GeneralUtils::errorToString (errRc));
521
525
m_semaphoreConfEvt.give ();
526
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_GATT, errRc); // Invoke the notify callback.
522
527
return ;
523
528
}
524
- if (!is_notification)
525
- m_semaphoreConfEvt.wait (" indicate" );
529
+ if (!is_notification){ // is indication
530
+ if (!m_semaphoreConfEvt.timedWait (" indicate" , indicationTimeout)){
531
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_INDICATE_TIMEOUT, 0 ); // Invoke the notify callback.
532
+ } else {
533
+ auto code = (esp_gatt_status_t ) m_semaphoreConfEvt.value ();
534
+ if (code == ESP_GATT_OK) {
535
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::SUCCESS_INDICATE, code); // Invoke the notify callback.
536
+ } else {
537
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::ERROR_INDICATE_FAILURE, code);
538
+ }
539
+ }
540
+ } else {
541
+ m_pCallbacks->onStatus (this , BLECharacteristicCallbacks::Status::SUCCESS_NOTIFY, 0 ); // Invoke the notify callback.
542
+ }
526
543
}
527
544
log_v (" << notify" );
528
545
} // Notify
@@ -551,7 +568,11 @@ void BLECharacteristic::setBroadcastProperty(bool value) {
551
568
*/
552
569
void BLECharacteristic::setCallbacks (BLECharacteristicCallbacks* pCallbacks) {
553
570
log_v (" >> setCallbacks: 0x%x" , (uint32_t )pCallbacks);
554
- m_pCallbacks = pCallbacks;
571
+ if (pCallbacks != nullptr ){
572
+ m_pCallbacks = pCallbacks;
573
+ } else {
574
+ m_pCallbacks = &defaultCallback;
575
+ }
555
576
log_v (" << setCallbacks" );
556
577
} // setCallbacks
557
578
@@ -754,4 +775,27 @@ void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic) {
754
775
log_d (" BLECharacteristicCallbacks" , " << onWrite" );
755
776
} // onWrite
756
777
778
+
779
+ /* *
780
+ * @brief Callback function to support a Notify request.
781
+ * @param [in] pCharacteristic The characteristic that is the source of the event.
782
+ */
783
+ void BLECharacteristicCallbacks::onNotify (BLECharacteristic* pCharacteristic) {
784
+ log_d (" BLECharacteristicCallbacks" , " >> onNotify: default" );
785
+ log_d (" BLECharacteristicCallbacks" , " << onNotify" );
786
+ } // onNotify
787
+
788
+
789
+ /* *
790
+ * @brief Callback function to support a Notify/Indicate Status report.
791
+ * @param [in] pCharacteristic The characteristic that is the source of the event.
792
+ * @param [in] s Status of the notification/indication
793
+ * @param [in] code Additional code of underlying errors
794
+ */
795
+ void BLECharacteristicCallbacks::onStatus (BLECharacteristic* pCharacteristic, Status s, uint32_t code) {
796
+ log_d (" BLECharacteristicCallbacks" , " >> onStatus: default" );
797
+ log_d (" BLECharacteristicCallbacks" , " << onStatus" );
798
+ } // onStatus
799
+
800
+
757
801
#endif /* CONFIG_BT_ENABLED */
0 commit comments