BLE
Fork of BLE_API by
Diff: ble/GattClient.h
- Revision:
- 1053:b55e1ad3e1b3
- Parent:
- 1052:62b44b3be31b
- Child:
- 1054:ec4a5b9b254e
diff -r 62b44b3be31b -r b55e1ad3e1b3 ble/GattClient.h --- a/ble/GattClient.h Mon Jan 11 08:51:30 2016 +0000 +++ b/ble/GattClient.h Mon Jan 11 08:51:31 2016 +0000 @@ -24,18 +24,23 @@ #include "GattCallbackParamTypes.h" +#include "CallChainOfFunctionPointersWithContext.h" + class GattClient { public: - typedef void (*ReadCallback_t)(const GattReadCallbackParams *params); + typedef FunctionPointerWithContext<const GattReadCallbackParams*> ReadCallback_t; + typedef CallChainOfFunctionPointersWithContext<const GattReadCallbackParams*> ReadCallbackChain_t; enum WriteOp_t { GATT_OP_WRITE_REQ = 0x01, /**< Write request. */ GATT_OP_WRITE_CMD = 0x02, /**< Write command. */ }; - typedef void (*WriteCallback_t)(const GattWriteCallbackParams *params); + typedef FunctionPointerWithContext<const GattWriteCallbackParams*> WriteCallback_t; + typedef CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams*> WriteCallbackChain_t; - typedef void (*HVXCallback_t)(const GattHVXCallbackParams *params); + typedef FunctionPointerWithContext<const GattHVXCallbackParams*> HVXCallback_t; + typedef CallChainOfFunctionPointersWithContext<const GattHVXCallbackParams*> HVXCallbackChain_t; /* * The following functions are meant to be overridden in the platform-specific sub-class. @@ -242,18 +247,42 @@ /* Event callback handlers. */ public: /** - * Set up a callback for read response events. + * Set up a callback for read response events. + * It is possible to remove registered callbacks using + * onDataRead().detach(callbackToRemove) */ void onDataRead(ReadCallback_t callback) { - onDataReadCallback = callback; + onDataReadCallbackChain.add(callback); + } + + /** + * @brief provide access to the callchain of read callbacks + * It is possible to register callbacks using onDataRead().add(callback); + * It is possible to unregister callbacks using onDataRead().detach(callback) + * @return The read callbacks chain + */ + ReadCallbackChain_t& onDataRead() { + return onDataReadCallbackChain; } /** * Set up a callback for write response events. + * It is possible to remove registered callbacks using + * onDataWritten().detach(callbackToRemove). * @Note: Write commands (issued using writeWoResponse) don't generate a response. */ void onDataWritten(WriteCallback_t callback) { - onDataWriteCallback = callback; + onDataWriteCallbackChain.add(callback); + } + + /** + * @brief provide access to the callchain of data written callbacks + * It is possible to register callbacks using onDataWritten().add(callback); + * It is possible to unregister callbacks using onDataWritten().detach(callback) + * @return The data written callbacks chain + */ + WriteCallbackChain_t& onDataWritten() { + return onDataWriteCallbackChain; } /** @@ -327,9 +356,21 @@ * Set up a callback for when the GATT client receives an update event * corresponding to a change in the value of a characteristic on the remote * GATT server. + * It is possible to remove registered callbacks using onHVX().detach(callbackToRemove). */ void onHVX(HVXCallback_t callback) { - onHVXCallback = callback; + onHVXCallbackChain.add(callback); + } + + + /** + * @brief provide access to the callchain of HVX callbacks + * It is possible to register callbacks using onHVX().add(callback); + * It is possible to unregister callbacks using onHVX().detach(callback) + * @return The HVX callbacks chain + */ + HVXCallbackChain_t& onHVX() { + return onHVXCallbackChain; } protected: @@ -340,27 +381,23 @@ /* Entry points for the underlying stack to report events back to the user. */ public: void processReadResponse(const GattReadCallbackParams *params) { - if (onDataReadCallback) { - onDataReadCallback(params); - } + onDataReadCallbackChain(params); } void processWriteResponse(const GattWriteCallbackParams *params) { - if (onDataWriteCallback) { - onDataWriteCallback(params); - } + onDataWriteCallbackChain(params); } void processHVXEvent(const GattHVXCallbackParams *params) { - if (onHVXCallback) { - onHVXCallback(params); + if (onHVXCallbackChain) { + onHVXCallbackChain(params); } } protected: - ReadCallback_t onDataReadCallback; - WriteCallback_t onDataWriteCallback; - HVXCallback_t onHVXCallback; + ReadCallbackChain_t onDataReadCallbackChain; + WriteCallbackChain_t onDataWriteCallbackChain; + HVXCallbackChain_t onHVXCallbackChain; private: /* Disallow copy and assignment. */