Updated
Fork of BLE_API by
Diff: ble/GattServer.h
- Revision:
- 959:d99a4565f872
- Parent:
- 949:1902cbd0dd83
- Child:
- 966:9451b90bbb66
--- a/ble/GattServer.h Thu Nov 26 12:52:33 2015 +0000 +++ b/ble/GattServer.h Thu Nov 26 12:52:34 2015 +0000 @@ -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,13 +247,20 @@ * @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 * GATT server has an attribute updated by a write command from the peer. @@ -259,12 +275,16 @@ * @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 @@ * @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 @@ 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 @@ /* 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 @@ } void handleDataSentEvent(unsigned count) { - if (dataSentCallChain.hasCallbacksAttached()) { - dataSentCallChain.call(count); - } + dataSentCallChain.call(count); } protected: @@ -367,12 +385,12 @@ 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. */