Added chained callbacks for onDataSent.

This commit is contained in:
marcuschang 2014-12-15 16:12:16 +00:00
parent 512f1054cf
commit 580f31d86b
2 changed files with 24 additions and 9 deletions

View file

@ -225,8 +225,16 @@ public:
/**
* Setup a callback for the GATT event DATA_SENT.
*
* @Note: it is possible to chain together multiple onDataSent callbacks
* (potentially from different modules of an application) to receive updates
* to characteristics.
*
* @Note: it is also possible to setup a callback into a member function of
* some object.
*/
void onDataSent(GattServer::ServerEventCallbackWithCount_t callback);
void onDataSent(void (*callback)(unsigned count));
template <typename T> void onDataSent(T * objPtr, void (T::*memberPtr)(unsigned count));
/**
* Setup a callback for when a characteristic has its value updated by a
@ -525,11 +533,15 @@ BLEDevice::addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {
}
inline void
BLEDevice::onDataSent(GattServer::ServerEventCallbackWithCount_t callback)
{
BLEDevice::onDataSent(void (*callback)(unsigned count)) {
transport->getGattServer().setOnDataSent(callback);
}
template <typename T> inline void
BLEDevice::onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
transport->getGattServer().setOnDataSent(objPtr, memberPtr);
}
inline void
BLEDevice::onDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) {
transport->getGattServer().setOnDataWritten(callback);

View file

@ -27,10 +27,9 @@ public:
/* Event callback handlers. */
typedef void (*EventCallback_t)(uint16_t attributeHandle);
typedef void (*ServerEventCallback_t)(void); /**< independent of any particular attribute */
typedef void (*ServerEventCallbackWithCount_t)(unsigned count); /**< independent of any particular attribute */
protected:
GattServer() : serviceCount(0), characteristicCount(0), onDataSent(NULL), onDataWritten(), onUpdatesEnabled(NULL), onUpdatesDisabled(NULL), onConfirmationReceived(NULL) {
GattServer() : serviceCount(0), characteristicCount(0), onDataSent(), onDataWritten(), onUpdatesEnabled(NULL), onUpdatesDisabled(NULL), onConfirmationReceived(NULL) {
/* empty */
}
@ -47,7 +46,11 @@ private:
// be sure to call sd_ble_gatts_hvx() twice with notify then indicate!
// Strange use case, but valid and must be covered!
void setOnDataSent(ServerEventCallbackWithCount_t callback) {onDataSent = callback;}
void setOnDataSent(void (*callback)(unsigned count)) {onDataSent.add(callback);}
template <typename T>
void setOnDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
onDataSent.add(objPtr, memberPtr);
}
void setOnDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP)) {onDataWritten.add(callback);}
template <typename T>
void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) {
@ -85,8 +88,8 @@ protected:
}
void handleDataSentEvent(unsigned count) {
if (onDataSent) {
onDataSent(count);
if (onDataSent.hasCallbacksAttached()) {
onDataSent.call(count);
}
}
@ -95,7 +98,7 @@ protected:
uint8_t characteristicCount;
private:
ServerEventCallbackWithCount_t onDataSent;
CallChainOfFunctionPointersWithContext<unsigned> onDataSent;
CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *> onDataWritten;
EventCallback_t onUpdatesEnabled;
EventCallback_t onUpdatesDisabled;