Holla back

Fork of BLE_API by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
jakerosenthal@gmail.com
Date:
Fri Oct 10 17:32:22 2014 -0700
Branch:
2chains
Parent:
122:4b68a819ab4f
Commit message:
first attempt at chaining onconnect and ondisconnect

Changed in this revision

public/BLEDevice.h Show annotated file Show diff for this revision Revisions of this file
public/Gap.h Show annotated file Show diff for this revision Revisions of this file
--- a/public/BLEDevice.h	Tue Sep 30 01:03:56 2014 +0100
+++ b/public/BLEDevice.h	Fri Oct 10 17:32:22 2014 -0700
@@ -199,11 +199,14 @@
     /* APIs to set GAP callbacks. */
     void onTimeout(Gap::EventCallback_t timeoutCallback);
 
-    void onConnection(Gap::ConnectionEventCallback_t connectionCallback);
+    void onConnection(void (*callback)(Gap::Handle_t handle, const Gap::ConnectionParams_t *eventDataP));
+    template <typename T> void onConnection(T *objPtr, void (T::*memberPtr)(Gap::Handle_t handle, const Gap::ConnectionParams_t *context));
+
     /**
      * Used to setup a callback for GAP disconnection.
      */
-    void onDisconnection(Gap::DisconnectionEventCallback_t disconnectionCallback);
+     void onDisconnection(void (*callback)(Gap::Handle_t handle, Gap::DisconnectionReason_t reason));
+     template <typename T> void onDisconnection(T *objPtr, void (T::*memberPtr)(Gap::Handle_t handle, Gap::DisconnectionReason_t reason));
 
     /**
      * Setup a callback for the GATT event DATA_SENT.
@@ -469,15 +472,23 @@
 }
 
 inline void
-BLEDevice::onConnection(Gap::ConnectionEventCallback_t connectionCallback)
-{
-    transport->getGap().setOnConnection(connectionCallback);
+BLEDevice::onConnection(void (*callback)(Gap::Handle_t handle, const Gap::ConnectionParams_t *eventDataP)) {
+    transport->getGap().setOnConnection(callback);
+}
+
+template <typename T> inline void
+BLEDevice::onConnection(T *objPtr, void (T::*memberPtr)(Gap::Handle_t handle, const Gap::ConnectionParams_t *context)) {
+    transport->getGap().setOnConnection(objPtr, memberPtr);
 }
 
 inline void
-BLEDevice::onDisconnection(Gap::DisconnectionEventCallback_t disconnectionCallback)
-{
-    transport->getGap().setOnDisconnection(disconnectionCallback);
+BLEDevice::onDisconnection(void (*callback)(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)) {
+    transport->getGap().setOnDisconnection(callback);
+}
+
+template <typename T> inline void
+BLEDevice::onDisconnection(T *objPtr, void (T::*memberPtr)(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)) {
+    transport->getGap().setOnDisconnection(objPtr, memberPtr);
 }
 
 inline void
--- 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__