High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
Rohit Grover
Date:
Wed Jun 04 10:06:19 2014 +0100
Parent:
63:653378e782ea
Child:
65:100b50423049
Commit message:
adding BLEDevice APIs for GATTServer callbacks

Changed in this revision

hw/BLEDevice.h Show annotated file Show diff for this revision Revisions of this file
hw/GattServer.h Show annotated file Show diff for this revision Revisions of this file
--- a/hw/BLEDevice.h	Wed Jun 04 09:36:31 2014 +0100
+++ b/hw/BLEDevice.h	Wed Jun 04 10:06:19 2014 +0100
@@ -129,10 +129,18 @@
 
     ble_error_t disconnect(void);
 
+    /* APIs to set GAP callbacks. */
     void onTimeout(Gap::EventCallback_t       timeoutCallback);
     void onConnection(Gap::EventCallback_t    connectionCallback);
     void onDisconnection(Gap::EventCallback_t disconnectionCallback);
 
+    /* APIs to set GATT server callbacks */
+    void onDataSent(GattServer::EventCallback_t callback);
+    void onDataWritten(GattServer::EventCallback_t callback);
+    void onUpdatesEnabled(GattServer::EventCallback_t callback);
+    void onUpdatesDisabled(GattServer::EventCallback_t callback);
+    void onConfirmationReceived(GattServer::EventCallback_t callback);
+
 private:
     /**
      * Internal helper to udpate the transport backend with advertising data
@@ -283,6 +291,46 @@
     return transport->getGap().setAdvertisingData(advPayload, scanResponse);
 }
 
+inline void
+BLEDevice::onTimeout(Gap::EventCallback_t timeoutCallback) {
+    transport->getGap().setOnTimeout(timeoutCallback);
+}
+
+inline void
+BLEDevice::onConnection(Gap::EventCallback_t connectionCallback) {
+    transport->getGap().setOnConnection(connectionCallback);
+}
+
+inline void
+BLEDevice::onDisconnection(Gap::EventCallback_t disconnectionCallback) {
+    transport->getGap().setOnDisconnection(disconnectionCallback);
+}
+
+inline void
+BLEDevice::onDataSent(GattServer::EventCallback_t callback) {
+    transport->getGattServer().setOnDataSent(callback);
+}
+
+inline void
+BLEDevice::onDataWritten(GattServer::EventCallback_t callback) {
+    transport->getGattServer().setOnDataWritten(callback);
+}
+
+inline void
+BLEDevice::onUpdatesEnabled(GattServer::EventCallback_t callback) {
+    transport->getGattServer().setOnUpdatesEnabled(callback);
+}
+
+inline void
+BLEDevice::onUpdatesDisabled(GattServer::EventCallback_t callback) {
+    transport->getGattServer().setOnUpdatesDisabled(callback);
+}
+
+inline void
+BLEDevice::onConfirmationReceived(GattServer::EventCallback_t callback) {
+    transport->getGattServer().setOnConfirmationReceived(callback);
+}
+
 /*
  * ALL OF THE FOLLOWING METHODS ARE DEPRECATED
  */
@@ -304,20 +352,4 @@
     return transport->getGap().startAdvertising(_advParams);
 }
 
-inline void
-BLEDevice::onTimeout(Gap::EventCallback_t timeoutCallback) {
-    transport->getGap().setOnTimeout(timeoutCallback);
-}
-
-inline void
-BLEDevice::onConnection(Gap::EventCallback_t connectionCallback) {
-    transport->getGap().setOnConnection(connectionCallback);
-}
-
-inline void
-BLEDevice::onDisconnection(Gap::EventCallback_t disconnectionCallback) {
-    transport->getGap().setOnDisconnection(disconnectionCallback);
-}
-
-
 #endif // ifndef __BLE_DEVICE_H__
--- a/hw/GattServer.h	Wed Jun 04 09:36:31 2014 +0100
+++ b/hw/GattServer.h	Wed Jun 04 10:06:19 2014 +0100
@@ -31,9 +31,6 @@
 /**************************************************************************/
 class GattServer
 {
-private:
-    GattServerEvents *m_pEventHandler;
-
 public:
     /* These functions must be defined in the sub-class */
     virtual ble_error_t addService(GattService &) = 0;
@@ -49,36 +46,76 @@
     // be sure to call sd_ble_gatts_hvx() twice with notify then indicate!
     // Strange use case, but valid and must be covered!
 
-    /* Event callback handlers */
-    void setEventHandler(GattServerEvents *pEventHandler) {
-        m_pEventHandler = pEventHandler;
+    /* Event callback handlers. */
+    typedef void (*EventCallback_t)(uint16_t attributeHandle);
+    void setOnDataSent(EventCallback_t callback) {
+        onDataSent = callback;
+    }
+    void setOnDataWritten(EventCallback_t callback) {
+        onDataWritten = callback;
+    }
+    void setOnUpdatesEnabled(EventCallback_t callback) {
+        onUpdatesEnabled = callback;
+    }
+    void setOnUpdatesDisabled(EventCallback_t callback) {
+        onUpdatesDisabled = callback;
+    }
+    void setOnConfirmationReceived(EventCallback_t callback) {
+        onConfirmationReceived = callback;
     }
 
     void handleEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
-        if (NULL == m_pEventHandler) {
-            return;
-        }
         switch (type) {
         case GattServerEvents::GATT_EVENT_DATA_SENT:
-            m_pEventHandler->onDataSent(charHandle);
+            if (onDataSent) {
+                onDataSent(charHandle);
+            }
             break;
         case GattServerEvents::GATT_EVENT_DATA_WRITTEN:
-            m_pEventHandler->onDataWritten(charHandle);
+            if (onDataWritten) {
+                onDataWritten(charHandle);
+            }
             break;
         case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
-            m_pEventHandler->onUpdatesEnabled(charHandle);
+            if (onUpdatesEnabled) {
+                onUpdatesEnabled(charHandle);
+            }
             break;
         case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
-            m_pEventHandler->onUpdatesDisabled(charHandle);
+            if (onUpdatesDisabled) {
+                onUpdatesDisabled(charHandle);
+            }
             break;
         case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
-            m_pEventHandler->onConfirmationReceived(charHandle);
+            if (onConfirmationReceived) {
+                onConfirmationReceived(charHandle);
+            }
             break;
         }
     }
 
+protected:
+    GattServer() :
+        serviceCount(0),
+        characteristicCount(0),
+        onDataSent(NULL),
+        onDataWritten(NULL),
+        onUpdatesEnabled(NULL),
+        onUpdatesDisabled(NULL),
+        onConfirmationReceived(NULL) {
+        /* empty */
+    }
+
+protected:
     uint8_t serviceCount;
     uint8_t characteristicCount;
+
+private:
+    EventCallback_t onDataSent;
+    EventCallback_t onDataWritten;
+    EventCallback_t onUpdatesEnabled;
+    EventCallback_t onUpdatesDisabled;
+    EventCallback_t onConfirmationReceived;
 };
 
 #endif // ifndef __GATT_SERVER_H__