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:
ktownsend
Date:
Mon Dec 16 19:43:33 2013 +0000
Parent:
8:780bfa26d0ee
Child:
10:f905b036bb07
Commit message:
Preview code enabling most advertising features (requires new nRF51 firmware)

Changed in this revision

GapAdvertisingParams.cpp Show annotated file Show diff for this revision Revisions of this file
GapAdvertisingParams.h Show annotated file Show diff for this revision Revisions of this file
blecommon.h Show annotated file Show diff for this revision Revisions of this file
hw/nrf51822.cpp Show annotated file Show diff for this revision Revisions of this file
hw/nrf51822.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GapAdvertisingParams.cpp	Mon Dec 16 18:16:01 2013 +0000
+++ b/GapAdvertisingParams.cpp	Mon Dec 16 19:43:33 2013 +0000
@@ -111,3 +111,23 @@
 {
     return _advType;
 }
+
+/**************************************************************************/
+/*!
+    @brief returns the current Advertising Delay (in units of 0.625ms)
+*/
+/**************************************************************************/
+uint16_t GapAdvertisingParams::getInterval(void)
+{
+    return _interval;
+}
+
+/**************************************************************************/
+/*!
+    @brief returns the current Advertising Timeout (in seconds)
+*/
+/**************************************************************************/
+uint16_t GapAdvertisingParams::getTimeout(void)
+{
+    return _timeout;
+}
--- a/GapAdvertisingParams.h	Mon Dec 16 18:16:01 2013 +0000
+++ b/GapAdvertisingParams.h	Mon Dec 16 19:43:33 2013 +0000
@@ -26,6 +26,8 @@
     virtual ~GapAdvertisingParams(void);
     
     virtual AdvertisingType getAdvertisingType(void);
+    virtual uint16_t        getInterval(void);
+    virtual uint16_t        getTimeout(void);
 
   private:
     AdvertisingType  _advType;
--- a/blecommon.h	Mon Dec 16 18:16:01 2013 +0000
+++ b/blecommon.h	Mon Dec 16 19:43:33 2013 +0000
@@ -11,7 +11,8 @@
 {
   BLE_ERROR_NONE                    = 0,
   BLE_ERROR_BUFFER_OVERFLOW         = 1,
-  BLE_ERROR_BUFFER_INVALID_PARAM    = 2
+  BLE_ERROR_NOT_IMPLEMENTED         = 2,    /* Requested a feature that isn't yet implement or isn't supported by the target HW */
+  BLE_ERROR_PARAM_OUT_OF_RANGE      = 3
 } ble_error_t;
 
 /* https://developer.bluetooth.org/gatt/units/Pages/default.aspx */
--- a/hw/nrf51822.cpp	Mon Dec 16 18:16:01 2013 +0000
+++ b/hw/nrf51822.cpp	Mon Dec 16 19:43:33 2013 +0000
@@ -19,7 +19,7 @@
     @brief  Constructor
 */
 /**************************************************************************/
-nRF51822::nRF51822() : uart(p9, p10)      /* LPC1768 using apps board */
+nRF51822::nRF51822() : uart(p9, p10)
 {
     /* Setup the nRF UART interface */
     uart.baud(9600);
@@ -28,7 +28,7 @@
     uart.attach(this, &nRF51822::uartCallback);
     
     /* Add flow control for UART (required by the nRF51822) */
-    uart.set_flow_control(Serial::RTSCTS, p30, p29);      /* LPC1768 */
+    uart.set_flow_control(Serial::RTSCTS, p30, p29);
 
     /* Reset the service counter */
     serviceCount = 0;
@@ -45,24 +45,6 @@
 
 /**************************************************************************/
 /*!
-
-*/
-/**************************************************************************/
-void nRF51822::test(void)
-{
-    /* Send iBeacon data as a test */
-    uart.printf("10 0a 00 1e 02 01 04 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8\r\n");
-    /* ToDo: Check response */
-    wait(0.1);
-    
-    /* Start the radio */
-    uart.printf("10 03 00 00\r\n");
-    /* ToDo: Check response */
-    wait(0.1);
-}
-
-/**************************************************************************/
-/*!
     @brief  Sets the advertising parameters and payload for the device
 
     @param[in]  params
@@ -75,15 +57,22 @@
                 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
                 in \ref GapAdveritinngParams
             
-    @returns    ble_error_t
+    @returns    \ref ble_error_t
     
     @retval     BLE_ERROR_NONE
                 Everything executed properly
 
     @retval     BLE_ERROR_BUFFER_OVERFLOW
                 The proposed action would cause a buffer overflow.  All
-                advertising payloads must be <= 31 bytes.
+                advertising payloads must be <= 31 bytes, for example.
                 
+    @retval     BLE_ERROR_NOT_IMPLEMENTED
+                A feature was requested that is not yet supported in the
+                nRF51 firmware or hardware.
+
+    @retval     BLE_ERROR_PARAM_OUT_OF_RANGE
+                One of the proposed values is outside the valid range.
+
     @section EXAMPLE
 
     @code
@@ -95,18 +84,77 @@
 {
     uint8_t len = 0;
     uint8_t *buffer;
-
-    /* ToDo: Send advertising params, Command ID = 0x000x */
+    
+    /* Make sure we support the advertising type */
+    if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
+    {
+        return BLE_ERROR_NOT_IMPLEMENTED;
+    }
+    
+    /* Check interval range */
+    if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
+        (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
+    {
+        return BLE_ERROR_PARAM_OUT_OF_RANGE;
+    }
+    
+    /* Check timeout is zero for Connectable Directed */
+    if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) ||
+        (params.getTimeout() != 0))
+    {
+        /* Timeout must be 0 with this type, although we'll never get here */
+        /* since this isn't implemented yet anyway */
+        return BLE_ERROR_PARAM_OUT_OF_RANGE;
+    }
+    
+    /* Check timeout for other advertising types */
+    if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) ||
+        (params.getTimeout() == 0))
+    {
+        return BLE_ERROR_PARAM_OUT_OF_RANGE;
+    }
 
-    /* Send advertising data, Command ID = 0x000A */
-    len = advData.getPayloadLen();
-    buffer = advData.getPayload(); 
-       
-    if (len > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
+    /* Make sure we don't exceed the advertising payload length */
+    if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
     {
         return BLE_ERROR_BUFFER_OVERFLOW;
     }
     
+    /* Make sure we don't exceed the scan response payload length */
+    if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
+    {
+        if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
+        {
+            return BLE_ERROR_BUFFER_OVERFLOW;
+        }
+    }
+    
+    /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
+    /* contains a flags AD type, etc. */
+
+    /* ToDo: Refactor these actions into separate private functions */
+
+    /* 1.) Send advertising params, Command IDs = 0x000C, 0x000D, 0x000E */
+    /* A.) Command ID = 0x000C, Advertising Interval, uint16_t */
+    uart.printf("10 0C 00 02 %02X %02X\r\n", (uint8_t)(params.getInterval() & 0xFF),
+                                             (uint8_t)(params.getInterval() >> 8));
+    /* ToDo: Check response */
+    wait(0.5);
+    
+    /* B.) Command ID = 0x000D, Advertising Timeout, uint16_t */
+    uart.printf("10 0D 00 02 %02X %02X\r\n", (uint8_t)(params.getTimeout() & 0xFF),
+                                             (uint8_t)(params.getTimeout() >> 8));
+    /* ToDo: Check response */
+    wait(0.5);
+    
+    /* C.) Command ID = 0x000E, Advertising Type, uint8_t */
+    uart.printf("10 0E 00 01 %02X\r\n", (uint8_t)(params.getAdvertisingType()));
+    /* ToDo: Check response */
+    wait(0.5);    
+
+    /* 2.) Send advertising data, Command ID = 0x000A */
+    len = advData.getPayloadLen();
+    buffer = advData.getPayload();     
     uart.printf("10 0A 00 %02X ", len);
     for (uint16_t i = 0; i < len; i++)
     {
@@ -117,17 +165,11 @@
     /* ToDo: Check response */
     wait(0.1);
 
-    /* Send scan response data, Command ID = 0x000x */
+    /* 3.) Send scan response data, Command ID = 0x000x */
     if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
     {
         len = advData.getPayloadLen();
         buffer = advData.getPayload();
-        
-        if (len > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
-        {
-            return BLE_ERROR_BUFFER_OVERFLOW;
-        }
-    
         uart.printf("10 0A 00 %02X ", len);
         for (uint16_t i = 0; i < len; i++)
         {
--- a/hw/nrf51822.h	Mon Dec 16 18:16:01 2013 +0000
+++ b/hw/nrf51822.h	Mon Dec 16 19:43:33 2013 +0000
@@ -12,7 +12,7 @@
         nRF51822();
         virtual ~nRF51822(void);
 
-        /* Functions that mus be implemented from NRFRadio */
+        /* Functions that mus be implemented from BLERadio */
         virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &, GapAdvertisingData &);
         virtual ble_error_t addService(GattService &);
         virtual ble_error_t readCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t);
@@ -21,8 +21,6 @@
         virtual ble_error_t stop(void);
         virtual ble_error_t reset(void);
         
-        void test(void);
-
     private:
         Serial uart;
 
--- a/main.cpp	Mon Dec 16 18:16:01 2013 +0000
+++ b/main.cpp	Mon Dec 16 19:43:33 2013 +0000
@@ -53,7 +53,6 @@
 int main()
 {
     wait(2);
-    // radio.test();
     startBeacon();
     while(1);