10000 Added Notification/Indication data and status callbacks · flexibity-team/arduino-esp32@bd7b3dc · GitHub
[go: up one dir, main page]

Skip to content

Commit bd7b3dc

Browse files
committed
Added Notification/Indication data and status callbacks
1 parent 090ae85 commit bd7b3dc

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,20 @@ void BLECharacteristic::notify(bool is_notification) {
480480
assert(getService() != nullptr);
481481
assert(getService()->getServer() != nullptr);
482482

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+
483492
GeneralUtils::hexDump((uint8_t*)m_value.getValue().data(), m_value.getValue().length());
484493

485494
if (getService()->getServer()->getConnectedCount() == 0) {
486495
log_v("<< notify: No connected clients.");
496+
callback->onStatus(this, BLECharacteristicCallbacks::Status::ERROR_NO_CLIENT, 0);
487497
return;
488498
}
489499

@@ -494,12 +504,14 @@ void BLECharacteristic::notify(bool is_notification) {
494504
if(is_notification) {
495505
if (p2902 != nullptr && !p2902->getNotifications()) {
496506
log_v("<< notifications disabled; ignoring");
507+
callback->onStatus(this, BLECharacteristicCallbacks::Status::ERROR_NOTIFY_DISABLED, 0); // Invoke the notify callback.
497508
return;
498509
}
499510
}
500511
else{
501512
if (p2902 != nullptr && !p2902->getIndications()) {
502513
log_v("<< indications disabled; ignoring");
514+
callback->onStatus(this, BLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED, 0); // Invoke the notify callback.
503515
return;
504516
}
505517
}
@@ -510,7 +522,7 @@ void BLECharacteristic::notify(bool is_notification) {
510522
}
511523

512524
size_t length = m_value.getValue().length();
513-
if(!is_notification)
525+
if(!is_notification) // is indication
514526
m_semaphoreConfEvt.take("indicate");
515527
esp_err_t errRc = ::esp_ble_gatts_send_indicate(
516528
getService()->getServer()->getGattsIf(),
@@ -519,10 +531,23 @@ void BLECharacteristic::notify(bool is_notification) {
519531
if (errRc != ESP_OK) {
520532
log_e("<< esp_ble_gatts_send_ %s: rc=%d %s",is_notification?"notify":"indicate", errRc, GeneralUtils::errorToString(errRc));
521533
m_semaphoreConfEvt.give();
534+
callback->onStatus(this, BLECharacteristicCallbacks::Status::ERROR_GATT, errRc); // Invoke the notify callback.
522535
return;
523536
}
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+
}
526551
}
527552
log_v("<< notify");
528553
} // Notify
@@ -754,4 +779,27 @@ void BLECharacteristicCallbacks::onWrite(BLECharacteristic* pCharacteristic) {
754779
log_d("BLECharacteristicCallbacks", "<< onWrite");
755780
} // onWrite
756781

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+
757805
#endif /* CONFIG_BT_ENABLED */

libraries/BLE/src/BLECharacteristic.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class BLECharacteristic {
9090
static const uint32_t PROPERTY_INDICATE = 1<<4;
9191
static const uint32_t PROPERTY_WRITE_NR = 1<<5;
9292

93+
static const uint32_t indicationTimeout = 1000;
94+
9395
private:
9496

9597
friend class BLEServer;
@@ -130,9 +132,22 @@ class BLECharacteristic {
130132
*/
131133
class BLECharacteristicCallbacks {
132134
public:
135+
typedef enum {
136+
SUCCESS_INDICATE,
137+
SUCCESS_NOTIFY,
138+
ERROR_INDICATE_DISABLED,
139+
ERROR_NOTIFY_DISABLED,
140+
ERROR_GATT,
141+
ERROR_NO_CLIENT,
142+
ERROR_INDICATE_TIMEOUT,
143+
ERROR_INDICATE_FAILURE
144+
}Status;
145+
133146
virtual ~BLECharacteristicCallbacks();
134147
virtual void onRead(BLECharacteristic* pCharacteristic);
135148
virtual void onWrite(BLECharacteristic* pCharacteristic);
149+
virtual void onNotify(BLECharacteristic* pCharacteristic);
150+
virtual void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code);
136151
};
137152
#endif /* CONFIG_BT_ENABLED */
138153
#endif /* COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_ */

0 commit comments

Comments
 (0)
0