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:
Wed Dec 18 11:52:37 2013 +0000
Parent:
13:a585c98b6abc
Child:
15:327d7329072c
Commit message:
Fixed advertising interval checks

Changed in this revision

GapAdvertisingData.cpp Show annotated file Show diff for this revision Revisions of this file
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
hw/nrf51822.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GapAdvertisingData.cpp	Wed Dec 18 06:22:35 2013 +0000
+++ b/GapAdvertisingData.cpp	Wed Dec 18 11:52:37 2013 +0000
@@ -60,9 +60,18 @@
     /*       value is exclusive or not (flags, etc.) */
     
     /* Make sure we don't exceed the 31 byte payload limit */
-    if (_payloadLen + len >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
+    if (_payloadLen + len + 2 >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
         return BLE_ERROR_BUFFER_OVERFLOW;
+
+    /* Field length */
+    memset(&_payload[_payloadLen], len+1, 1);
+    _payloadLen++;
     
+    /* Field ID */
+    memset(&_payload[_payloadLen], (uint8_t)advDataType, 1); 
+    _payloadLen++;
+       
+    /* Payload */
     memcpy(&_payload[_payloadLen], payload, len);
     _payloadLen += len;
     
--- a/GapAdvertisingParams.cpp	Wed Dec 18 06:22:35 2013 +0000
+++ b/GapAdvertisingParams.cpp	Wed Dec 18 11:52:37 2013 +0000
@@ -35,8 +35,10 @@
                 connection modes
 
     @param[in]  interval
-                Advertising interval between 0x20 and 0x4000 (32 and 16384)
-                in 0.625ms intervals (20ms to 10.24s).
+                Advertising interval between 0x0020 and 0x4000 in 0.625ms
+                units (20ms to 10.24s).  If using non-connectable mode
+                (\ref ADV_NON_CONNECTABLE_UNDIRECTED) this min value is
+                0x00A0 (100ms).
 
                 @para
                 Increasing this value will allow central devices to detect
@@ -45,7 +47,10 @@
                 
                 @note This field must be set to 0 if connectionMode is equal
                 to \ref ADV_CONNECTABLE_DIRECTED
-
+                
+                @note See Bluetooth Core Specification, Vol 3., Part C,
+                Appendix A for suggested advertising intervals:
+                
     @param[in]  timeout
                 Advertising timeout between 0x1 and 0x3FFF (1 and 16383)
                 in seconds.  Enter 0 to disable the advertising timeout.
@@ -69,6 +74,18 @@
         /* Interval must be 0 in directed connectable mode */
         _interval = 0;
     }
+    else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED)
+    {
+        /* Min interval is slightly larger than in other modes */
+        if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON)
+        {
+            _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
+        }
+        if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX)
+        {
+            _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
+        }
+    }
     else
     {
         /* Stay within interval limits */
--- a/GapAdvertisingParams.h	Wed Dec 18 06:22:35 2013 +0000
+++ b/GapAdvertisingParams.h	Wed Dec 18 11:52:37 2013 +0000
@@ -3,9 +3,10 @@
 
 #include "blecommon.h"
 
-#define GAP_ADV_PARAMS_INTERVAL_MIN     (0x0020)
-#define GAP_ADV_PARAMS_INTERVAL_MAX     (0x4000)
-#define GAP_ADV_PARAMS_TIMEOUT_MAX      (0x3FFF)
+#define GAP_ADV_PARAMS_INTERVAL_MIN        (0x0020)
+#define GAP_ADV_PARAMS_INTERVAL_MIN_NONCON (0x00A0)
+#define GAP_ADV_PARAMS_INTERVAL_MAX        (0x1000)
+#define GAP_ADV_PARAMS_TIMEOUT_MAX         (0x3FFF)
 
 class GapAdvertisingParams
 {
@@ -21,7 +22,7 @@
     };
   
     GapAdvertisingParams(AdvertisingType advType = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
-                         uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN,
+                         uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON,
                          uint16_t timeout = 0);
     virtual ~GapAdvertisingParams(void);
     
--- a/hw/nrf51822.cpp	Wed Dec 18 06:22:35 2013 +0000
+++ b/hw/nrf51822.cpp	Wed Dec 18 11:52:37 2013 +0000
@@ -1,6 +1,7 @@
 #include "nrf51822.h"
 #include "mbed.h"
 
+/* Enables debug output over USB CDC at 9600 bps */
 #define NRF51822_DEBUG_MODE (1)
 
 /**************************************************************************/
@@ -170,30 +171,38 @@
 
     /* 1.) Send advertising params, Command IDs = 0x000C, 0x000D, 0x000E */
     /* A.) Command ID = 0x000C, Advertising Interval, uint16_t */
+    printf("10 0C 00 02 %02X %02X\r\n", (uint8_t)(params.getInterval() & 0xFF),
+                                             (uint8_t)(params.getInterval() >> 8));
     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 */
+    printf("10 0D 00 02 %02X %02X\r\n", (uint8_t)(params.getTimeout() & 0xFF),
+                                             (uint8_t)(params.getTimeout() >> 8));
     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 */
+    printf("10 0E 00 01 %02X\r\n", (uint8_t)(params.getAdvertisingType()));
     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);
+    buffer = advData.getPayload();
+    printf("10 0A 00 %02X", len);
+    uart.printf("10 0A 00 %02X", len);
     for (uint16_t i = 0; i < len; i++)
     {
+        printf(" %02X", buffer[i]);
         uart.printf(" %02X", buffer[i]);
     }
+    printf("\r\n");
     uart.printf("\r\n");
     
     /* ToDo: Check response */
@@ -204,11 +213,14 @@
     {
         len = advData.getPayloadLen();
         buffer = advData.getPayload();
-        uart.printf("10 0B 00 %02X ", len);
+        printf("10 0B 00 %02X", len);
+        uart.printf("10 0B 00 %02X", len);
         for (uint16_t i = 0; i < len; i++)
         {
+            printf(" %02X", buffer[i]);
             uart.printf(" %02X", buffer[i]);
         }
+        printf("\r\n");
         uart.printf("\r\n");
 
         /* ToDo: Check response */