High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Revision:
526:caa67c3187a0
Parent:
524:6e97ab392e2a
Child:
527:493185cebc03
diff -r c80c57d748f8 -r caa67c3187a0 public/Gap.h
--- a/public/Gap.h	Fri Jun 19 15:52:06 2015 +0100
+++ b/public/Gap.h	Fri Jun 19 15:52:06 2015 +0100
@@ -17,16 +17,15 @@
 #ifndef __GAP_H__
 #define __GAP_H__
 
+#include "GapAdvertisingData.h"
+#include "GapAdvertisingParams.h"
 #include "GapEvents.h"
 #include "CallChain.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;
-class GapAdvertisingData;
+class GapScanningParams; /* forward declaration */
 
 class Gap {
 public:
@@ -57,9 +56,12 @@
      * transport library.
      */
     enum DisconnectionReason_t {
-        REMOTE_USER_TERMINATED_CONNECTION = 0x13,
-        LOCAL_HOST_TERMINATED_CONNECTION  = 0x16,
-        CONN_INTERVAL_UNACCEPTABLE        = 0x3B,
+        CONNECTION_TIMEOUT                          = 0x08,
+        REMOTE_USER_TERMINATED_CONNECTION           = 0x13,
+        REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES = 0x14,  /**< Remote Device Terminated Connection due to low resources.*/
+        REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF     = 0x15,  /**< Remote Device Terminated Connection due to power off. */
+        LOCAL_HOST_TERMINATED_CONNECTION            = 0x16,
+        CONN_INTERVAL_UNACCEPTABLE                  = 0x3B,
     };
 
     /* Describes the current state of the device (more than one bit can be set) */
@@ -77,49 +79,6 @@
         uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
     } ConnectionParams_t;
 
-    enum Role_t {
-        PERIPHERAL  = 0x1, /**< Peripheral Role. */
-        CENTRAL     = 0x2, /**< Central Role.    */
-    };
-
-    struct AdvertisementCallbackParams_t {
-        Address_t            peerAddr;
-        int8_t               rssi;
-        bool                 isScanResponse;
-        AdvertisementType_t  type;
-        uint8_t              advertisingDataLen;
-        const uint8_t       *advertisingData;
-    };
-    typedef FunctionPointerWithContext<const AdvertisementCallbackParams_t *> AdvertisementReportCallback_t;
-
-    struct ConnectionCallbackParams_t {
-        Handle_t      handle;
-        Role_t        role;
-        AddressType_t peerAddrType;
-        Address_t     peerAddr;
-        AddressType_t ownAddrType;
-        Address_t     ownAddr;
-        const ConnectionParams_t *connectionParams;
-
-        ConnectionCallbackParams_t(Handle_t       handleIn,
-                                   Role_t         roleIn,
-                                   AddressType_t  peerAddrTypeIn,
-                                   const uint8_t *peerAddrIn,
-                                   AddressType_t  ownAddrTypeIn,
-                                   const uint8_t *ownAddrIn,
-                                   const ConnectionParams_t *connectionParamsIn) :
-            handle(handleIn),
-            role(roleIn),
-            peerAddrType(peerAddrTypeIn),
-            peerAddr(),
-            ownAddrType(ownAddrTypeIn),
-            ownAddr(),
-            connectionParams(connectionParamsIn) {
-            memcpy(peerAddr, peerAddrIn, ADDR_LEN);
-            memcpy(ownAddr, ownAddrIn, ADDR_LEN);
-        }
-    };
-
     enum SecurityMode_t {
         SECURITY_MODE_NO_ACCESS,
         SECURITY_MODE_ENCRYPTION_OPEN_LINK, /**< require no protection, open link. */
@@ -185,7 +144,10 @@
     }
 
     typedef void (*EventCallback_t)(void);
-    typedef void (*ConnectionEventCallback_t)(const ConnectionCallbackParams_t *params);
+    typedef void (*ConnectionEventCallback_t)(Handle_t,
+                                              AddressType_t peerAddrType, const Address_t peerAddr,
+                                              AddressType_t ownAddrType,  const Address_t ownAddr,
+                                              const ConnectionParams_t *);
     typedef void (*HandleSpecificEvent_t)(Handle_t handle);
     typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t);
     typedef void (*RadioNotificationEventCallback_t) (bool radio_active); /* gets passed true for ACTIVE; false for INACTIVE. */
@@ -194,12 +156,20 @@
     typedef void (*LinkSecuredCallback_t)(Handle_t handle, SecurityMode_t securityMode);
     typedef void (*PasskeyDisplayCallback_t)(Handle_t handle, const Passkey_t passkey);
 
-    friend class BLEDevice;
+    struct AdvertisementCallbackParams_t {
+        Address_t            peerAddr;
+        int8_t               rssi;
+        bool                 isScanResponse;
+        AdvertisementType_t  type;
+        uint8_t              advertisingDataLen;
+        const uint8_t       *advertisingData;
+    };
+    typedef FunctionPointerWithContext<const AdvertisementCallbackParams_t *> AdvertisementReportCallback_t;
 
-private:
+public:
     /* These functions must be defined in the sub-class */
-    virtual ble_error_t setAddress(AddressType_t type,  const Address_t address)                  = 0;
-    virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address)                        = 0;
+    virtual ble_error_t setAddress(AddressType_t type,   const Address_t address)                    = 0;
+    virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address)                          = 0;
     virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
     virtual ble_error_t startAdvertising(const GapAdvertisingParams &)                             = 0;
     virtual ble_error_t stopAdvertising(void)                                                      = 0;
@@ -207,10 +177,6 @@
     virtual uint16_t    getMinAdvertisingInterval(void) const                                      = 0;
     virtual uint16_t    getMinNonConnectableAdvertisingInterval(void) const                        = 0;
     virtual uint16_t    getMaxAdvertisingInterval(void) const                                      = 0;
-    virtual ble_error_t connect(const Address_t           peerAddr,
-                                Gap::AddressType_t        peerAddrType,
-                                const ConnectionParams_t *connectionParams,
-                                const GapScanningParams  *scanParams) = 0;
     virtual ble_error_t disconnect(DisconnectionReason_t reason)                                   = 0;
     virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params)                   = 0;
     virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params)             = 0;
@@ -224,6 +190,9 @@
     virtual ble_error_t setAppearance(uint16_t appearance)                    = 0;
     virtual ble_error_t getAppearance(uint16_t *appearanceP)                  = 0;
 
+    virtual ble_error_t setTxPower(int8_t txPower)                            = 0;
+    virtual void        getPermittedTxPowerValues(const int8_t **, size_t *)  = 0;
+
     ble_error_t startScan(const GapScanningParams &scanningParams, void (*callback)(const AdvertisementCallbackParams_t *params)) {
         ble_error_t err = BLE_ERROR_NONE;
         if (callback) {
@@ -247,7 +216,7 @@
         return err;
     }
 
-protected:
+public:
     virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) = 0;
 
     /* Event callback handlers */
@@ -311,7 +280,7 @@
     template<typename T>
     void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);}
 
-private:
+public:
     GapState_t getState(void) const {
         return state;
     }
@@ -334,17 +303,10 @@
     }
 
 public:
-    void processConnectionEvent(Handle_t                  handle,
-                                Role_t                    role,
-                                AddressType_t             peerAddrType,
-                                const Address_t           peerAddr,
-                                AddressType_t             ownAddrType,
-                                const Address_t           ownAddr,
-                                const ConnectionParams_t *connectionParams) {
+    void processConnectionEvent(Handle_t handle, AddressType_t peerAddrType, const Address_t peerAddr, AddressType_t ownAddrType, const Address_t ownAddr, const ConnectionParams_t *params) {
         state.connected = 1;
         if (onConnection) {
-            ConnectionCallbackParams_t callbackParams(handle, role, peerAddrType, peerAddr, ownAddrType, ownAddr, connectionParams);
-            onConnection(&callbackParams);
+            onConnection(handle, peerAddrType, peerAddr, ownAddrType, ownAddr, params);
         }
     }