High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
Diff: ble/Gap.h
- Revision:
- 829:9072de50087b
- Parent:
- 816:2b4f0ef8c06e
- Child:
- 859:2a1cb15098ba
diff -r d22ab1419e92 -r 9072de50087b ble/Gap.h --- a/ble/Gap.h Tue Sep 29 09:54:18 2015 +0100 +++ b/ble/Gap.h Tue Sep 29 09:54:18 2015 +0100 @@ -21,11 +21,9 @@ #include "GapAdvertisingParams.h" #include "GapScanningParams.h" #include "GapEvents.h" -#include "CallChain.h" +#include "CallChainOfFunctionPointersWithContext.h" #include "FunctionPointerWithContext.h" -using namespace mbed; - /* Forward declarations for classes which will only be used for pointers or references in the following. */ class GapAdvertisingParams; class GapScanningParams; @@ -126,6 +124,17 @@ } }; + struct DisconnectionCallbackParams_t { + Handle_t handle; + DisconnectionReason_t reason; + + DisconnectionCallbackParams_t(Handle_t handleIn, + DisconnectionReason_t reasonIn) : + handle(handleIn), + reason(reasonIn) + {} + }; + static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */ static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) { return (durationInMillis * 1000) / UNIT_1_25_MS; @@ -134,7 +143,7 @@ typedef void (*TimeoutEventCallback_t)(TimeoutSource_t source); typedef void (*ConnectionEventCallback_t)(const ConnectionCallbackParams_t *params); - typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t); + typedef void (*DisconnectionEventCallback_t)(const DisconnectionCallbackParams_t *params); typedef FunctionPointerWithContext<bool> RadioNotificationEventCallback_t; /* @@ -214,7 +223,7 @@ * @param scanParams * Paramters to be used while scanning for the peer. * @return BLE_ERROR_NONE if connection establishment procedure is started - * successfully. The connectionCallback (if set) will be invoked upon + * successfully. The connectionCallChain (if set) will be invoked upon * a connection event. */ virtual ble_error_t connect(const Address_t peerAddr, @@ -891,36 +900,20 @@ void onTimeout(TimeoutEventCallback_t callback) {timeoutCallback = callback;} /** - * Setup a callback for connection events. Refer to ConnectionEventCallback_t. + * Append to a chain of callbacks to be invoked upon GAP connection. */ - void onConnection(ConnectionEventCallback_t callback) {connectionCallback = callback;} + void onConnection(ConnectionEventCallback_t callback) {connectionCallChain.add(callback);} - /** - * Set the application callback for disconnection events. - * @param callback - * Pointer to the unique callback. - */ - void onDisconnection(DisconnectionEventCallback_t callback) {disconnectionCallback = callback;} + template<typename T> + void onConnection(T *tptr, void (T::*mptr)(const ConnectionCallbackParams_t*)) {connectionCallChain.add(tptr, mptr);} /** - * Append to a chain of callbacks to be invoked upon connection; these - * callbacks receive no context and are therefore different from the - * connectionCallback callback. - * @param callback - * function pointer to be invoked upon connection; receives no context. + * Append to a chain of callbacks to be invoked upon GAP disconnection. */ - template<typename T> - void addToConnectionCallChain(T *tptr, void (T::*mptr)(void)) {connectionCallChain.add(tptr, mptr);} + void onDisconnection(DisconnectionEventCallback_t callback) {disconnectionCallChain.add(callback);} - /** - * Append to a chain of callbacks to be invoked upon disconnection; these - * callbacks receive no context and are therefore different from the - * disconnectionCallback callback. - * @param callback - * function pointer to be invoked upon disconnection; receives no context. - */ template<typename T> - void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);} + void onDisconnection(T *tptr, void (T::*mptr)(const DisconnectionCallbackParams_t*)) {disconnectionCallChain.add(tptr, mptr);} /** * Set the application callback for radio-notification events. @@ -968,8 +961,6 @@ state(), scanningActive(false), timeoutCallback(NULL), - connectionCallback(NULL), - disconnectionCallback(NULL), radioNotificationCallback(), onAdvertisementReport(), connectionCallChain(), @@ -988,19 +979,14 @@ const Address_t ownAddr, const ConnectionParams_t *connectionParams) { state.connected = 1; - if (connectionCallback) { - ConnectionCallbackParams_t callbackParams(handle, role, peerAddrType, peerAddr, ownAddrType, ownAddr, connectionParams); - connectionCallback(&callbackParams); - } - connectionCallChain.call(); + ConnectionCallbackParams_t callbackParams(handle, role, peerAddrType, peerAddr, ownAddrType, ownAddr, connectionParams); + connectionCallChain.call(&callbackParams); } void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) { state.connected = 0; - if (disconnectionCallback) { - disconnectionCallback(handle, reason); - } - disconnectionCallChain.call(); + DisconnectionCallbackParams_t callbackParams(handle, reason); + disconnectionCallChain.call(&callbackParams); } void processAdvertisementReport(const Address_t peerAddr, @@ -1036,12 +1022,10 @@ protected: TimeoutEventCallback_t timeoutCallback; - ConnectionEventCallback_t connectionCallback; - DisconnectionEventCallback_t disconnectionCallback; RadioNotificationEventCallback_t radioNotificationCallback; AdvertisementReportCallback_t onAdvertisementReport; - CallChain connectionCallChain; - CallChain disconnectionCallChain; + CallChainOfFunctionPointersWithContext<const ConnectionCallbackParams_t*> connectionCallChain; + CallChainOfFunctionPointersWithContext<const DisconnectionCallbackParams_t*> disconnectionCallChain; private: /* disallow copy and assignment */