Skip to content

Commit ed27507

Browse files
committed
Added callbacks to BLERemoteCharacteristic
1 parent dd513df commit ed27507

File tree

2 files changed

+85
-29
lines changed

2 files changed

+85
-29
lines changed

libraries/BLE/src/BLERemoteCharacteristic.cpp

+68-29
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(
3939
m_charProp = charProp;
4040
m_pRemoteService = pRemoteService;
4141
m_notifyCallback = nullptr;
42+
m_pRemoteCharacteristicCallbacks = nullptr;
4243
m_rawData = nullptr;
4344
m_auth = ESP_GATT_AUTH_REQ_NONE;
4445

@@ -164,6 +165,10 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event,
164165
log_d("Invoking callback for notification on characteristic %s", toString().c_str());
165166
m_notifyCallback(this, evtParam->notify.value, evtParam->notify.value_len, evtParam->notify.is_notify);
166167
} // 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+
}
167172
break;
168173
} // ESP_GATTC_NOTIFY_EVT
169174

@@ -461,44 +466,43 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
461466
m_semaphoreRegForNotifyEvt.take("registerForNotify");
462467

463468
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);
479470
} // End Register
480471
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);
495473
} // End Unregister
496474

497475
m_semaphoreRegForNotifyEvt.wait("registerForNotify");
498476

499477
log_v("<< registerForNotify()");
500478
} // registerForNotify
501479

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+
502506

503507
/**
504508
* @brief Delete the descriptors in the descriptor map.
@@ -516,6 +520,41 @@ void BLERemoteCharacteristic::removeDescriptors() {
516520
m_descriptorMap.clear(); // Technically not neeeded, but just to be sure.
517521
} // removeCharacteristics
518522

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+
519558

520559
/**
521560
* @brief Convert a BLERemoteCharacteristic to a string representation;

libraries/BLE/src/BLERemoteCharacteristic.h

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
class BLERemoteService;
2323
class BLERemoteDescriptor;
24+
class BLERemoteCharacteristicCallbacks;
2425
typedef void (*notify_callback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
2526

2627
/**
@@ -48,6 +49,7 @@ class BLERemoteCharacteristic {
4849
uint32_t readUInt32();
4950
float readFloat();
5051
void registerForNotify(notify_callback _callback, bool notifications = true, bool descriptorRequiresRegistration = true);
52+
void setCallbacks(BLERemoteCharacteristicCallbacks* pCallbacks, bool notifications = true, bool descriptorRequiresRegistration = true);
5153
void writeValue(uint8_t* data, size_t length, bool response = false);
5254
void writeValue(std::string newValue, bool response = false);
5355
void writeValue(uint8_t newValue, bool response = false);
@@ -66,6 +68,8 @@ class BLERemoteCharacteristic {
6668

6769
void removeDescriptors();
6870
void retrieveDescriptors();
71+
void registerNotifications(bool notifications, bool descriptorRequiresRegistration);
72+
void unregisterNotifications(bool descriptorRequiresRegistration);
6973

7074
// Private properties
7175
BLEUUID m_uuid;
@@ -79,9 +83,22 @@ class BLERemoteCharacteristic {
7983
std::string m_value;
8084
uint8_t *m_rawData;
8185
notify_callback m_notifyCallback;
86+
BLERemoteCharacteristicCallbacks* m_pRemoteCharacteristicCallbacks;
8287

8388
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
8489
std::map<std::string, BLERemoteDescriptor*> m_descriptorMap;
8590
}; // BLERemoteCharacteristic
91+
92+
/**
93+
* @brief Callbacks associated with a remote %BLE characteristic.
94+
*/
95+
class BLERemoteCharacteristicCallbacks {
96+
public:
97+
virtual ~BLERemoteCharacteristicCallbacks() {};
98+
virtual void onNotify(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) = 0;
99+
100+
}; // BLERemoteCharacteristicCallbacks
101+
102+
86103
#endif /* CONFIG_BT_ENABLED */
87104
#endif /* COMPONENTS_CPP_UTILS_BLEREMOTECHARACTERISTIC_H_ */

0 commit comments

Comments
 (0)