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:
Tue Dec 17 17:28:41 2013 +0000
Parent:
10:f905b036bb07
Child:
12:2c6b1059ed36
Commit message:
Added debug info to nrf51822.cpp

Changed in this revision

GattCharacteristic.h Show annotated file Show diff for this revision Revisions of this file
GattService.cpp Show annotated file Show diff for this revision Revisions of this file
GattService.h Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib 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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GattCharacteristic.h	Mon Dec 16 20:00:05 2013 +0000
+++ b/GattCharacteristic.h	Tue Dec 17 17:28:41 2013 +0000
@@ -15,7 +15,7 @@
     uint16_t uuid;              /* Characteristic UUID */
     uint16_t lenMin;            /* Minimum length of the value */
     uint16_t lenMax;            /* Maximum length of the value */
-    uint8_t  index;
+    uint8_t  handle;
     uint8_t  properties;
 };
 
--- a/GattService.cpp	Mon Dec 16 20:00:05 2013 +0000
+++ b/GattService.cpp	Tue Dec 17 17:28:41 2013 +0000
@@ -24,7 +24,7 @@
     primaryServiceID.update(base_uuid);
     characteristicCount = 0;
     memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
-    index = 0;
+    handle = 0;
 }
 
 /**************************************************************************/
@@ -47,7 +47,7 @@
     primaryServiceID.update( ble_uuid );
     characteristicCount = 0;
     memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
-    index = 0;
+    handle = 0;
 }
 
 /**************************************************************************/
@@ -69,7 +69,7 @@
             properties required to create the characteristic on the
             target radio board.
             
-    @note   This function will update the .index field in the
+    @note   This function will update the .handle field in the
             GattCharacteristic to indicate where this characteristic was
             stored in the GattService's characteristic array.
 
@@ -107,8 +107,8 @@
     /* Insert the serialised object into the buffer */
     memcpy(&characteristics[characteristicCount], &c, sizeof(serialisedChar_t));
     
-    /* Update the index value */
-    characteristic.index = characteristicCount;
+    /* Update the handle value */
+    characteristic.handle = characteristicCount;
     
     characteristicCount++;
     
--- a/GattService.h	Mon Dec 16 20:00:05 2013 +0000
+++ b/GattService.h	Tue Dec 17 17:28:41 2013 +0000
@@ -28,7 +28,7 @@
     UUID                primaryServiceID;
     uint8_t             characteristicCount;
     serialisedChar_t    characteristics[BLE_SERVICE_MAX_CHARACTERISTICS];
-    uint8_t             index;
+    uint8_t             handle;
 
     ble_error_t         addCharacteristic(GattCharacteristic &);
 };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Dec 17 17:28:41 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#4f6df64750bd
--- a/hw/nrf51822.cpp	Mon Dec 16 20:00:05 2013 +0000
+++ b/hw/nrf51822.cpp	Tue Dec 17 17:28:41 2013 +0000
@@ -1,6 +1,8 @@
 #include "nrf51822.h"
 #include "mbed.h"
 
+#define NRF51822_DEBUG_MODE (1)
+
 /**************************************************************************/
 /*!
     @brief  UART callback function
@@ -11,7 +13,11 @@
     /* 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
 }
 
 /**************************************************************************/
@@ -85,9 +91,16 @@
     uint8_t len = 0;
     uint8_t *buffer;
     
+    #if NRF51822_DEBUG_MODE
+    printf("Configuring Advertising ... ");
+    #endif
+    
     /* Make sure we support the advertising type */
     if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
     {
+        #if NRF51822_DEBUG_MODE
+        printf("ADV_CONNECTABLE_DIRECTED not supported (BLE_ERROR = 0x%04X)\r\n", (uint16_t)BLE_ERROR_NOT_IMPLEMENTED);
+        #endif
         return BLE_ERROR_NOT_IMPLEMENTED;
     }
     
@@ -95,6 +108,9 @@
     if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
         (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
     {
+        #if NRF51822_DEBUG_MODE
+        printf("Advertising interval out of range (BLE_ERROR = 0x%04X)\r\n", (uint16_t)BLE_ERROR_PARAM_OUT_OF_RANGE);
+        #endif
         return BLE_ERROR_PARAM_OUT_OF_RANGE;
     }
     
@@ -102,6 +118,9 @@
     if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) ||
         (params.getTimeout() != 0))
     {
+        #if NRF51822_DEBUG_MODE
+        printf("Timeout must be 0 with ADV_CONNECTABLE_DIRECTED (BLE_ERROR = 0x%04X)\r\n", (uint16_t)BLE_ERROR_PARAM_OUT_OF_RANGE);
+        #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;
@@ -109,14 +128,20 @@
     
     /* Check timeout for other advertising types */
     if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) ||
-        (params.getTimeout() == 0))
+        (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX))
     {
+        #if NRF51822_DEBUG_MODE
+        printf("Timeout out of range (BLE_ERROR = 0x%04X)\r\n", (uint16_t)BLE_ERROR_PARAM_OUT_OF_RANGE);
+        #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("Advertising payload > 31 bytes (BLE_ERROR = 0x%04X)\r\n", (uint16_t)BLE_ERROR_BUFFER_OVERFLOW);
+        #endif
         return BLE_ERROR_BUFFER_OVERFLOW;
     }
     
@@ -125,6 +150,9 @@
     {
         if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
         {
+            #if NRF51822_DEBUG_MODE
+            printf("Scan response payload > 31 bytes (BLE_ERROR = 0x%04X)\r\n", (uint16_t)BLE_ERROR_BUFFER_OVERFLOW);
+            #endif
             return BLE_ERROR_BUFFER_OVERFLOW;
         }
     }
@@ -181,6 +209,9 @@
         wait(0.1);
     }
     
+    #if NRF51822_DEBUG_MODE
+    printf("OK!\r\n");
+    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -202,6 +233,10 @@
 /**************************************************************************/
 ble_error_t nRF51822::addService(GattService & service)
 {
+    #if NRF51822_DEBUG_MODE
+    printf("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 */
@@ -257,9 +292,12 @@
     }    
 
     /* Update the service index value */
-    service.index = serviceCount;
+    service.handle = serviceCount;
     serviceCount++;
     
+    #if NRF51822_DEBUG_MODE
+    printf("OK!\r\n");
+    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -292,8 +330,15 @@
 /**************************************************************************/
 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;
 }
 
@@ -326,8 +371,12 @@
 /**************************************************************************/
 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.index, service.index);
+    uart.printf("10 06 00 %02X %02X %02X", len + 2, characteristic.handle, service.handle);
     for (uint16_t i = 0; i<len; i++)
     {
         uart.printf(" %02X", buffer[i]);
@@ -337,6 +386,9 @@
     /* ToDo: Check response */
     wait(0.1);
     
+    #if NRF51822_DEBUG_MODE
+    printf("OK!\r\n");
+    #endif
     return BLE_ERROR_NONE;
 }
 
@@ -361,12 +413,19 @@
 /**************************************************************************/
 ble_error_t nRF51822::start(void)
 {
+    #if NRF51822_DEBUG_MODE
+    printf("Initialising 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;
 }
 
@@ -388,12 +447,19 @@
 /**************************************************************************/
 ble_error_t nRF51822::stop(void)
 {
+    #if NRF51822_DEBUG_MODE
+    printf("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;
 }
 
@@ -416,6 +482,10 @@
 /**************************************************************************/
 ble_error_t nRF51822::reset(void)
 {
+    #if NRF51822_DEBUG_MODE
+    printf("Restting the radio ... ");
+    #endif
+    
     /* Command ID = 0x0005, No payload */
     uart.printf("10 05 00 00\r\n");
 
@@ -425,5 +495,8 @@
     /* Wait for the radio to come back up */    
     wait(1);
     
+    #if NRF51822_DEBUG_MODE
+    printf("OK!\r\n");
+    #endif
     return BLE_ERROR_NONE;
 }
--- a/main.cpp	Mon Dec 16 20:00:05 2013 +0000
+++ b/main.cpp	Tue Dec 17 17:28:41 2013 +0000
@@ -40,14 +40,15 @@
 
 void startBeacon(void)
 {
+    ble_error_t error;
+    
     /* iBeacon includes the FLAG and MSD fields */
     advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, 25);
     
-    wait(2);
-    radio.reset();
-    radio.setAdvertising(advParams, advData, scanResponse);
-    radio.start();
+    error = radio.reset();
+    error = radio.setAdvertising(advParams, advData, scanResponse);
+    error = radio.start();
 }
 
 int main()