Holla back

Fork of BLE_API by Bluetooth Low Energy

Branch:
2chains
Revision:
123:d2cdf4ebe524
Parent:
117:0fb20195102b
--- a/public/Gap.h	Tue Sep 30 01:03:56 2014 +0100
+++ b/public/Gap.h	Fri Oct 10 17:32:22 2014 -0700
@@ -22,6 +22,8 @@
 #include "GapAdvertisingData.h"
 #include "GapAdvertisingParams.h"
 #include "GapEvents.h"
+#include "CallChainOfFunctionPointersWithContext.h"
+
 
 /**************************************************************************/
 /*!
@@ -85,31 +87,38 @@
     virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0;
 
     typedef void (*EventCallback_t)(void);
-    typedef void (*ConnectionEventCallback_t)(Handle_t, const ConnectionParams_t *);
-    typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t);
 
     /* Event callback handlers */
     void setOnTimeout(EventCallback_t callback) {
         onTimeout = callback;
     }
-    void setOnConnection(ConnectionEventCallback_t callback) {
-        onConnection = callback;
+    void setOnConnection(void (*callback)(Handle_t handle, const ConnectionParams_t *eventDataP)) {
+        onConnection.add(callback);
+    }
+    template <typename T>
+    void setOnConnection(T *objPtr, void (T::*memberPtr)(Handle_t handle, const ConnectionParams_t *context)) {
+        onConnection.add(objPtr, memberPtr);
     }
-    void setOnDisconnection(DisconnectionEventCallback_t callback) {
-        onDisconnection = callback;
+
+    void setOnDisconnection(void (*callback)(Handle_t handle, DisconnectionReason_t reason)) {
+        onDisconnection.add(callback);
+    }
+    template <typename T>
+    void setOnDisconnection(T *objPtr, void (T::*memberPtr)(Handle_t handle, DisconnectionReason_t reason)) {
+        onDisconnection.add(objPtr, memberPtr);
     }
 
     void processConnectionEvent(Handle_t handle, const ConnectionParams_t *params) {
         state.connected = 1;
-        if (onConnection) {
-            onConnection(handle, params);
+        if (onConnection.hasCallbacksAttached()) {
+            onConnection.call(handle, params);
         }
     }
 
     void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
         state.connected = 0;
-        if (onDisconnection) {
-            onDisconnection(handle, reason);
+        if (onDisconnection.hasCallbacksAttached()) {
+            onDisconnection.call(handle, reason);
         }
     }
 
@@ -129,7 +138,7 @@
     }
 
 protected:
-    Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) {
+    Gap() : state(), onTimeout(NULL), onConnection(), onDisconnection() {
         /* empty */
     }
 
@@ -138,8 +147,8 @@
 
 private:
     EventCallback_t              onTimeout;
-    ConnectionEventCallback_t    onConnection;
-    DisconnectionEventCallback_t onDisconnection;
+    CallChainOfFunctionPointersWithContext<Handle_t, const ConnectionParams_t *> onConnection;
+    CallChainOfFunctionPointersWithContext<Handle_t, DisconnectionReason_t> onDisconnection;
 };
 
 #endif // ifndef __GAP_H__