Add typedef for FunctionPointerWithContext and CallChains.

Provide access to Callchains
This commit is contained in:
Vincent Coubard 2015-11-22 20:05:20 +00:00
parent acf4467c44
commit 57ad533486

View file

@ -26,9 +26,18 @@
class GattServer {
public:
/* Event callback handlers. */
typedef void (*EventCallback_t)(GattAttribute::Handle_t attributeHandle);
typedef void (*ServerEventCallback_t)(void); /**< Independent of any particular attribute. */
typedef FunctionPointerWithContext<unsigned> DataSentCallback_t;
typedef CallChainOfFunctionPointersWithContext<unsigned> DataSentCallbackChain_t;
typedef FunctionPointerWithContext<const GattWriteCallbackParams*> DataWrittenCallback_t;
typedef CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams*> DataWrittenCallbackChain_t;
typedef FunctionPointerWithContext<const GattReadCallbackParams*> DataReadCallback_t;
typedef CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> DataReadCallbackChain_t;
typedef FunctionPointerWithContext<GattAttribute::Handle_t> EventCallback_t;
protected:
GattServer() :
@ -238,12 +247,19 @@ public:
* @Note: It is also possible to set up a callback into a member function of
* some object.
*/
void onDataSent(void (*callback)(unsigned count)) {dataSentCallChain.add(callback);}
void onDataSent(const DataSentCallback_t& callback) {dataSentCallChain.add(callback);}
template <typename T>
void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
dataSentCallChain.add(objPtr, memberPtr);
}
/**
* @brief get the callback chain called when the event DATA_EVENT is triggered.
*/
DataSentCallbackChain_t& onDataSent() {
return dataSentCallChain;
}
/**
* Set up a callback for when an attribute has its value updated by or at the
* connected peer. For a peripheral, this callback is triggered when the local
@ -259,12 +275,16 @@ public:
* @Note: It is also possible to set up a callback into a member function of
* some object.
*/
void onDataWritten(void (*callback)(const GattWriteCallbackParams *eventDataP)) {dataWrittenCallChain.add(callback);}
void onDataWritten(const DataWrittenCallback_t& callback) {dataWrittenCallChain.add(callback);}
template <typename T>
void onDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) {
dataWrittenCallChain.add(objPtr, memberPtr);
}
DataWrittenCallbackChain_t& onDataWritten() {
return dataWrittenCallChain;
}
/**
* Setup a callback to be invoked on the peripheral when an attribute is
* being read by a remote client.
@ -284,7 +304,7 @@ public:
* @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available;
* else BLE_ERROR_NONE.
*/
ble_error_t onDataRead(void (*callback)(const GattReadCallbackParams *eventDataP)) {
ble_error_t onDataRead(const DataReadCallback_t& callback) {
if (!isOnDataReadAvailable()) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
@ -302,6 +322,10 @@ public:
return BLE_ERROR_NONE;
}
DataReadCallbackChain_t& onDataRead() {
return dataReadCallChain;
}
/**
* Set up a callback for when notifications or indications are enabled for a
* characteristic on the local GATT server.
@ -323,15 +347,11 @@ public:
/* Entry points for the underlying stack to report events back to the user. */
protected:
void handleDataWrittenEvent(const GattWriteCallbackParams *params) {
if (dataWrittenCallChain.hasCallbacksAttached()) {
dataWrittenCallChain.call(params);
}
dataWrittenCallChain.call(params);
}
void handleDataReadEvent(const GattReadCallbackParams *params) {
if (dataReadCallChain.hasCallbacksAttached()) {
dataReadCallChain.call(params);
}
dataReadCallChain.call(params);
}
void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t attributeHandle) {
@ -357,9 +377,7 @@ protected:
}
void handleDataSentEvent(unsigned count) {
if (dataSentCallChain.hasCallbacksAttached()) {
dataSentCallChain.call(count);
}
dataSentCallChain.call(count);
}
protected:
@ -367,12 +385,12 @@ protected:
uint8_t characteristicCount;
private:
CallChainOfFunctionPointersWithContext<unsigned> dataSentCallChain;
CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams *> dataWrittenCallChain;
CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> dataReadCallChain;
EventCallback_t updatesEnabledCallback;
EventCallback_t updatesDisabledCallback;
EventCallback_t confirmationReceivedCallback;
DataSentCallbackChain_t dataSentCallChain;
DataWrittenCallbackChain_t dataWrittenCallChain;
DataReadCallbackChain_t dataReadCallChain;
EventCallback_t updatesEnabledCallback;
EventCallback_t updatesDisabledCallback;
EventCallback_t confirmationReceivedCallback;
private:
/* Disallow copy and assignment. */