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 11 22:15:59 2013 +0000
Parent:
2:ffc5216bd2cc
Child:
4:50a31ff5f974
Commit message:
Incremental changes for GAP Advertising API

Changed in this revision

GapAdvertisingData.cpp Show annotated file Show diff for this revision Revisions of this file
GapAdvertisingData.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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src-flowcontrol.lib Show annotated file Show diff for this revision Revisions of this file
--- a/GapAdvertisingData.cpp	Tue Dec 10 07:32:12 2013 +0000
+++ b/GapAdvertisingData.cpp	Wed Dec 11 22:15:59 2013 +0000
@@ -16,6 +16,8 @@
 /**************************************************************************/
 GapAdvertisingData::GapAdvertisingData(void)
 {
+    memset(_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
+    _payloadLen = 0;
 }
 
 /**************************************************************************/
@@ -26,3 +28,48 @@
 GapAdvertisingData::~GapAdvertisingData(void)
 {
 }
+
+/**************************************************************************/
+/*!
+    @brief Returns the current payload length (0..31 bytes)
+*/
+/**************************************************************************/
+uint8_t GapAdvertisingData::getPayloadLen(void)
+{
+    return _payloadLen;
+}
+
+/**************************************************************************/
+/*!
+    @brief Adds advertising data based on the specified AD types
+
+    @args[in]   adType      The advertising data type to add
+    @args[in]   payload     Pointer to the payload contents
+    @args[in]   len         Size of the payload in bytes
+    
+    @returns    ble_error_t
+    
+    @retval     BLE_ERROR_NONE
+                Everything executed properly
+    @retval     BLE_ERROR_BUFFER_OVERFLOW
+                The specified data would cause the advertising buffer
+                to overflow
+    
+    @section EXAMPLE
+
+    @code
+
+    @endcode
+*/
+/**************************************************************************/
+ble_error_t GapAdvertisingData::addData(ble_gap_adv_datatype_t adType, uint8_t * payload, uint8_t len)
+{
+    /* ToDo: Make sure we don't exceed the 31 byte payload limit */
+    if (_payloadLen + len >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
+        return BLE_ERROR_BUFFER_OVERFLOW;
+    
+    memcpy(&_payload[_payloadLen], payload, len);
+    _payloadLen += len;
+    
+    return BLE_ERROR_NONE;
+}
--- a/GapAdvertisingData.h	Tue Dec 10 07:32:12 2013 +0000
+++ b/GapAdvertisingData.h	Wed Dec 11 22:15:59 2013 +0000
@@ -3,13 +3,75 @@
 
 #include "blecommon.h"
 
+#define GAP_ADVERTISING_DATA_MAX_PAYLOAD        (31)
+
+/**************************************************************************/
+/*!
+    @brief
+    This class provides several helper functions to generate properly
+    formatted GAP Advertising and Scan Response data payloads
+    
+    @section Advertising and Scan Response Payloads
+    
+    @para
+    Advertising data and Scan Response data are organized around a set of
+    data types called 'AD types' in the Bluetooth 4.0 specification.
+            
+    @para
+    Each AD type has it's own standardized 'assigned number', as defined
+    by the Bluetooth SIG:
+    https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
+    
+    @para
+    For convenience sake, all appropriate AD types have been encapsulated
+    into an enum at \ref ble_gap_adv_datatype_t.
+    
+    @para
+    Raw Advertising or Scan Response payloads are formatted as follows:
+    
+    - Record length (1 byte)
+    - AD Type (1 byte)
+    - AD payload (optional, only present if record length > 1)
+    
+    @para
+    When multiple AD types are present, the individual records are simply
+    appended one after the other, up to the maximum payload length of 31
+    bytes.
+    
+    @note See Bluetooth Specification 4.0 (Vol. 3) Section 11, 18 for
+    further information on Advertising and Scan Response data.
+    
+    @section Sample Advertising Payload
+    
+    // Two record payload containing BLE_GAP_ADV_DATATYPE_FLAGS (0x01) and 
+    // BLE_GAP_ADV_DATATYPE_COMPLETE_LOCAL_NAME (0x09) fields
+    02 01 01 0A 09 50 65 64 6F 6D 65 74 65 72
+
+    // Record 1 (FLAGS)
+    02 01 01
+    02 = record length (2 bytes)
+    01 = BLE_GAP_ADV_DATATYPE_FLAGS
+    01 = BLE_GAP_ADV_FLAGS_LE_LIMITED_DISCOVERABLE
+    
+    // Record 2 (COMPLETE LOCAL NAME)
+    0A 09 50 65 64 6F 6D 65 74 65 72
+    0A = record length (10 bytes)
+    09 = BLE_GAP_ADV_DATATYPE_COMPLETE_LOCAL_NAME
+    50 65 64 6F 6D 65 74 65 72 = "Pedometer"
+*/
+/**************************************************************************/
 class GapAdvertisingData
 {
-public:
+public:    
     GapAdvertisingData(void);
     virtual ~GapAdvertisingData(void);
+
+    ble_error_t addData(ble_gap_adv_datatype_t, uint8_t *, uint8_t);
+    uint8_t     getPayloadLen(void);
     
 private:
+    uint8_t _payload[GAP_ADVERTISING_DATA_MAX_PAYLOAD];
+    uint8_t _payloadLen;
 };
 
 #endif
--- a/blecommon.h	Tue Dec 10 07:32:12 2013 +0000
+++ b/blecommon.h	Wed Dec 11 22:15:59 2013 +0000
@@ -5,33 +5,40 @@
 
 typedef enum ble_error_e
 {
-  BLE_ERROR_NONE = 0
+  BLE_ERROR_NONE                    = 0,
+  BLE_ERROR_BUFFER_OVERFLOW         = 1
 } ble_error_t;
 
+/* Bluetooth Specification 4.0 (Vol. 3) Section 18.1 */
 typedef enum ble_gap_adv_flags_e
 {
-    BLE_GAP_ADV_FLAGS_
+  BLE_GAP_ADV_FLAGS_LE_LIMITED_DISCOVERABLE                               = 0x01,
+  BLE_GAP_ADV_FLAGS_LE_GENERAL_DISCOVERABLE                               = 0x02,
+  BLE_GAP_ADV_FLAGS_BREDR_NOT_SUPPORTED                                   = 0x04,
+  BLE_GAP_ADV_FLAGS_SIMULTANEOUS_LE_BREDR_C                               = 0x08,
+  BLE_GAP_ADV_FLAGS_SIMULTANEOUS_LE_BREDR_H                               = 0x10
 } ble_gap_adv_flags_t;
 
-// https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
+/* https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile */
+/* Bluetooth Specification 4.0 (Vol. 3) Section 11, 18 */
 typedef enum ble_gap_adv_datatype_e
 {
-    BLE_GAP_ADV_DATATYPE_FLAGS                                            = 0x01,
-    BLE_GAP_ADV_DATATYPE_INCOMPLETE_LIST_16BIT_SERVICE_IDS                = 0x02,
-    BLE_GAP_ADV_DATATYPE_COMPLETE_LIST_16BIT_SERVICE_IDS                  = 0x03,
-    BLE_GAP_ADV_DATATYPE_INCOMPLETE_LIST_32BIT_SERVICE_IDS                = 0x04,
-    BLE_GAP_ADV_DATATYPE_COMPLETE_LIST_32BIT_SERVICE_IDS                  = 0x05,
-    BLE_GAP_ADV_DATATYPE_INCOMPLETE_LIST_128BIT_SERVICE_IDS               = 0x06,
-    BLE_GAP_ADV_DATATYPE_COMPLETE_LIST_128BIT_SERVICE_IDS                 = 0x07,
-    BLE_GAP_ADV_DATATYPE_SHORTENED_LOCAL_NAME                             = 0x08,
-    BLE_GAP_ADV_DATATYPE_COMPLETE_LOCAL_NAME                              = 0x09,
-    BLE_GAP_ADV_DATATYPE_TX_POWER_LEVEL                                   = 0x0A,
-    BLE_GAP_ADV_DATATYPE_DEVICE_ID                                        = 0x10,
-    BLE_GAP_ADV_DATATYPE_SLAVE_CONNECTION_INTERVAL_RANGE                  = 0x12,
-    BLE_GAP_ADV_DATATYPE_SERVICE_DATA                                     = 0x16,
-    BLE_GAP_ADV_DATATYPE_APPEARANCE                                       = 0x19,
-    BLE_GAP_ADV_DATATYPE_ADVERTISING_INTERVAL                             = 0x1A,
-    BLE_GAP_ADV_DATATYPE_MANUFACTURER_SPECIFIC_DATA                       = 0xFF
+  BLE_GAP_ADV_DATATYPE_FLAGS                                              = 0x01,
+  BLE_GAP_ADV_DATATYPE_INCOMPLETE_LIST_16BIT_SERVICE_IDS                  = 0x02,
+  BLE_GAP_ADV_DATATYPE_COMPLETE_LIST_16BIT_SERVICE_IDS                    = 0x03,
+  BLE_GAP_ADV_DATATYPE_INCOMPLETE_LIST_32BIT_SERVICE_IDS                  = 0x04,
+  BLE_GAP_ADV_DATATYPE_COMPLETE_LIST_32BIT_SERVICE_IDS                    = 0x05,
+  BLE_GAP_ADV_DATATYPE_INCOMPLETE_LIST_128BIT_SERVICE_IDS                 = 0x06,
+  BLE_GAP_ADV_DATATYPE_COMPLETE_LIST_128BIT_SERVICE_IDS                   = 0x07,
+  BLE_GAP_ADV_DATATYPE_SHORTENED_LOCAL_NAME                               = 0x08,
+  BLE_GAP_ADV_DATATYPE_COMPLETE_LOCAL_NAME                                = 0x09,
+  BLE_GAP_ADV_DATATYPE_TX_POWER_LEVEL                                     = 0x0A,
+  BLE_GAP_ADV_DATATYPE_DEVICE_ID                                          = 0x10,
+  BLE_GAP_ADV_DATATYPE_SLAVE_CONNECTION_INTERVAL_RANGE                    = 0x12,
+  BLE_GAP_ADV_DATATYPE_SERVICE_DATA                                       = 0x16,
+  BLE_GAP_ADV_DATATYPE_APPEARANCE                                         = 0x19,
+  BLE_GAP_ADV_DATATYPE_ADVERTISING_INTERVAL                               = 0x1A,
+  BLE_GAP_ADV_DATATYPE_MANUFACTURER_SPECIFIC_DATA                         = 0xFF
 } ble_gap_adv_datatype_t;
 
 // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
@@ -56,7 +63,7 @@
   BLE_GAP_CHAR_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT               = 833,
   BLE_GAP_CHAR_APPEARANCE_GENERIC_BLOOD_PRESSURE                          = 896,
   BLE_GAP_CHAR_APPEARANCE_BLOOD_PRESSURE_ARM                              = 897,
-  BLE_GAP_CHAR_APPEARANCE_BLOD_PRESSURE_WRIST                             = 898,
+  BLE_GAP_CHAR_APPEARANCE_BLOOD_PRESSURE_WRIST                            = 898,
   BLE_GAP_CHAR_APPEARANCE_HUMAN_INTERFACE_DEVICE_HID                      = 960,
   BLE_GAP_CHAR_APPEARANCE_KEYBOARD                                        = 961,
   BLE_GAP_CHAR_APPEARANCE_MOUSE                                           = 962,
@@ -234,6 +241,18 @@
   BLE_GATT_FORMAT_STRUCT              = 0x1B  /**< Opaque Structure. */
 } ble_gatt_format_t;
 
+typedef enum ble_gatt_char_properties_e
+{
+  BLE_GATT_CHAR_PROPERTIES_BROADCAST                    = 0x01, /**< Permits broadcasts of the Characteristic Value using Server Characteristic Configuration Descriptor. */
+  BLE_GATT_CHAR_PROPERTIES_READ                         = 0x02, /**< Permits reads of the Characteristic Value. */
+  BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE       = 0x04, /**< Permits writes of the Characteristic Value without response. */
+  BLE_GATT_CHAR_PROPERTIES_WRITE                        = 0x08, /**< Permits writes of the Characteristic Value with response. */
+  BLE_GATT_CHAR_PROPERTIES_NOTIFY                       = 0x10, /**< Permits notifications of a Characteristic Value without acknowledgement. */
+  BLE_GATT_CHAR_PROPERTIES_INDICATE                     = 0x20, /**< Permits indications of a Characteristic Value with acknowledgement. */
+  BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES  = 0x40, /**< Permits signed writes to the Characteristic Value. */
+  BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES          = 0x80  /**< Additional characteristic properties are defined in the Characteristic Extended Properties Descriptor */
+} ble_gatt_char_properties_t;
+
 struct UTF8String
 {
   uint16_t  length;         /**< String length. */
--- a/hw/nrf51822.cpp	Tue Dec 10 07:32:12 2013 +0000
+++ b/hw/nrf51822.cpp	Wed Dec 11 22:15:59 2013 +0000
@@ -19,6 +19,7 @@
     @brief  Constructor
 */
 /**************************************************************************/
+
 //nRF51822::nRF51822() : uart(P0_4, P0_0) /* LPC812 */
 nRF51822::nRF51822() : uart(p9, p10)      /* LPC1768 using apps board */
 {
@@ -53,19 +54,14 @@
 void nRF51822::test(void)
 {
     /* Send iBeacon data as a test */
-    uint8_t response[4];
     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");
-    response[0] = uart.getc();
-    response[1] = uart.getc();
-    response[2] = uart.getc();
-    response[3] = uart.getc();
+    /* ToDo: Check response */
     wait(0.1);
+    
     /* Start the radio */
     uart.printf("10 03 00 00\r\n");
-    response[0] = uart.getc();
-    response[1] = uart.getc();
-    response[2] = uart.getc();
-    response[3] = uart.getc();
+    /* ToDo: Check response */
+    wait(0.1);
 }
 
 /**************************************************************************/
--- a/main.cpp	Tue Dec 10 07:32:12 2013 +0000
+++ b/main.cpp	Wed Dec 11 22:15:59 2013 +0000
@@ -13,26 +13,27 @@
 /* Battery Level Service */
 /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml */
 GattService        battService   ( 0x180F );
-GattCharacteristic battLevel     ( 0x2A19, 1, 1, 0x10 | 0x02);   /* Read + Notify */
+GattCharacteristic battLevel     ( 0x2A19, 1, 1, BLE_GATT_CHAR_PROPERTIES_NOTIFY | BLE_GATT_CHAR_PROPERTIES_READ );
 
 /* Heart Rate Monitor Service */
 /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
 GattService        hrmService    ( 0x180D );
-GattCharacteristic hrmRate       ( 0x2A37, 2, 3, 0x10 );         /* Notify */
-GattCharacteristic hrmLocation   ( 0x2A39, 1, 1, 0x02 );         /* Read */
+GattCharacteristic hrmRate       ( 0x2A37, 2, 3, BLE_GATT_CHAR_PROPERTIES_NOTIFY );
+GattCharacteristic hrmLocation   ( 0x2A39, 1, 1, BLE_GATT_CHAR_PROPERTIES_READ );
 
 /* Health Thermometer Service */
 /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.health_thermometer.xml */
 GattService        thermService  ( 0x1809 );
-GattCharacteristic thermTemp     ( 0x2A1C, 5, 13, 0x20 );         /* Indicate */
-GattCharacteristic thermType     ( 0x2A1D, 1, 1, 0x02 );          /* Read */
-GattCharacteristic thermInterval ( 0x2A21, 2, 2, 0x02 );          /* Read */
+GattCharacteristic thermTemp     ( 0x2A1C, 5, 13, BLE_GATT_CHAR_PROPERTIES_INDICATE );
+GattCharacteristic thermType     ( 0x2A1D, 1, 1, BLE_GATT_CHAR_PROPERTIES_READ );
+GattCharacteristic thermInterval ( 0x2A21, 2, 2, BLE_GATT_CHAR_PROPERTIES_READ );
 
 /* Notify   = device (server) sends data when it changes */
 /* Indicate = device (server) sends data when it changes and client confirms reception */ 
  
 int main()
 {
+    wait(2);
     radio.test();
     while(1);
 
--- a/mbed-src-flowcontrol.lib	Tue Dec 10 07:32:12 2013 +0000
+++ b/mbed-src-flowcontrol.lib	Wed Dec 11 22:15:59 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/bogdanm/code/mbed-src-flowcontrol/#5b5370bf691e
+http://mbed.org/users/bogdanm/code/mbed-src-flowcontrol/#2612d6599730