High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Revision:
15:327d7329072c
Parent:
14:6ea5d1012a64
Child:
16:542a9db01350
--- a/hw/nrf51822.cpp	Wed Dec 18 11:52:37 2013 +0000
+++ b/hw/nrf51822.cpp	Wed Dec 18 12:07:38 2013 +0000
@@ -1,9 +1,6 @@
 #include "nrf51822.h"
 #include "mbed.h"
 
-/* Enables debug output over USB CDC at 9600 bps */
-#define NRF51822_DEBUG_MODE (1)
-
 /**************************************************************************/
 /*!
     @brief  UART callback function
@@ -14,11 +11,7 @@
     /* ToDo: Check responses and set a flag for success/error/etc. */
     
     /* Read serial to clear the RX interrupt */
-    #if NRF51822_DEBUG_MODE
-    printf("%c", uart.getc());
-    #else
     uart.getc();
-    #endif
 }
 
 /**************************************************************************/
@@ -92,39 +85,35 @@
     uint8_t len = 0;
     uint8_t *buffer;
     
-    #if NRF51822_DEBUG_MODE
-    printf("%-40s", "Configuring Advertising");
-    #endif
-    
     /* Make sure we support the advertising type */
     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
     {
-        #if NRF51822_DEBUG_MODE
-        printf("[BLE_ERROR = 0x%04X]\r\n", (uint16_t)BLE_ERROR_NOT_IMPLEMENTED);
-        printf("--> ADV_CONNECTABLE_DIRECTED not supported\r\n");
-        #endif
         return BLE_ERROR_NOT_IMPLEMENTED;
     }
     
     /* Check interval range */
-    if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
-        (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
+    if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED)
     {
-        #if NRF51822_DEBUG_MODE
-        printf("[BLE_ERROR = 0x%04X]\r\n", (uint16_t)BLE_ERROR_PARAM_OUT_OF_RANGE);
-        printf("--> Advertising interval out of range\r\n");
-        #endif
-        return BLE_ERROR_PARAM_OUT_OF_RANGE;
+        /* Min delay is slightly longer for unconnectable devices */
+        if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
+            (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
+        {
+            return BLE_ERROR_PARAM_OUT_OF_RANGE;
+        }
+    }
+    else
+    {
+        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))
     {
-        #if NRF51822_DEBUG_MODE
-        printf("[BLE_ERROR = 0x%04X]\r\n", (uint16_t)BLE_ERROR_PARAM_OUT_OF_RANGE);
-        printf("--> Timeout must be 0 with ADV_CONNECTABLE_DIRECTED\r\n");
-        #endif
         /* 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;
@@ -134,20 +123,12 @@
     if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
         (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX))
     {
-        #if NRF51822_DEBUG_MODE
-        printf("[BLE_ERROR = 0x%04X]\r\n", (uint16_t)BLE_ERROR_PARAM_OUT_OF_RANGE);
-        printf("--> Timeout out of range\r\n");
-        #endif
         return BLE_ERROR_PARAM_OUT_OF_RANGE;
     }
 
     /* Make sure we don't exceed the advertising payload length */
     if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
     {
-        #if NRF51822_DEBUG_MODE
-        printf("[BLE_ERROR = 0x%04X]\r\n", (uint16_t)BLE_ERROR_BUFFER_OVERFLOW);
-        printf("--> Advertising payload > 31 bytes\r\n");
-        #endif
         return BLE_ERROR_BUFFER_OVERFLOW;
     }
     
@@ -156,10 +137,6 @@
     {
         if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
         {
-            #if NRF51822_DEBUG_MODE
-            printf("[BLE_ERROR = 0x%04X]\r\n", (uint16_t)BLE_ERROR_BUFFER_OVERFLOW);
-            printf("--> Scan response payload > 31 bytes\r\n");
-            #endif
             return BLE_ERROR_BUFFER_OVERFLOW;
         }
     }
@@ -227,9 +204,6 @@
         wait(0.1);
     }
     
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -251,10 +225,6 @@
 /**************************************************************************/
 ble_error_t nRF51822::addService(GattService & service)
 {
-    #if NRF51822_DEBUG_MODE
-    printf("%-40s", "Adding a service");
-    #endif
-    
     /* ToDo: Make sure we don't overflow the array, etc. */
     /* ToDo: Make sure this service UUID doesn't already exist (?) */
     /* ToDo: Basic validation */
@@ -313,9 +283,6 @@
     service.handle = serviceCount;
     serviceCount++;
     
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -348,15 +315,8 @@
 /**************************************************************************/
 ble_error_t nRF51822::readCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
 {
-    #if NRF51822_DEBUG_MODE
-    printf("Reading characteristic (handle = %d)    ", characteristic.handle);
-    #endif
-
     /* ToDo */
     
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -389,10 +349,6 @@
 /**************************************************************************/
 ble_error_t nRF51822::writeCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
 {
-    #if NRF51822_DEBUG_MODE
-    printf("Writing characteristic (handle = %d)    ", characteristic.handle);
-    #endif
-        
     /* Command ID = 0x0006, Payload = Service ID, Characteristic ID, Value */
     uart.printf("10 06 00 %02X %02X %02X", len + 2, characteristic.handle, service.handle);
     for (uint16_t i = 0; i<len; i++)
@@ -404,9 +360,6 @@
     /* ToDo: Check response */
     wait(0.1);
     
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -431,19 +384,12 @@
 /**************************************************************************/
 ble_error_t nRF51822::start(void)
 {
-    #if NRF51822_DEBUG_MODE
-    printf("%-40s", "Starting the radio");
-    #endif
-    
     /* Command ID = 0x0003, No payload */
     uart.printf("10 03 00 00\r\n");
 
     /* ToDo: Check response */
     wait(0.5);
 
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -465,19 +411,12 @@
 /**************************************************************************/
 ble_error_t nRF51822::stop(void)
 {
-    #if NRF51822_DEBUG_MODE
-    printf("%-40s", "Stopping the radio");
-    #endif
-    
     /* Command ID = 0x0004, No payload */
     uart.printf("10 04 00 00\r\n");
 
     /* ToDo: Check response */
     wait(0.1);
 
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -500,10 +439,6 @@
 /**************************************************************************/
 ble_error_t nRF51822::reset(void)
 {
-    #if NRF51822_DEBUG_MODE
-    printf("%-40s", "Resetting the radio");
-    #endif
-    
     /* Command ID = 0x0005, No payload */
     uart.printf("10 05 00 00\r\n");
 
@@ -513,8 +448,5 @@
     /* Wait for the radio to come back up */    
     wait(1);
     
-    #if NRF51822_DEBUG_MODE
-    printf("[OK]\r\n");
-    #endif
     return BLE_ERROR_NONE;
 }