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 10 07:32:12 2013 +0000
Parent:
1:fd3ec64b2345
Child:
3:46de446e82ed
Commit message:
UART Tests

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
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
GattCharacteristic.cpp Show annotated file Show diff for this revision Revisions of this file
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
blecharacteristic.cpp Show diff for this revision Revisions of this file
blecharacteristic.h Show diff for this revision Revisions of this file
blecommon.h Show annotated file Show diff for this revision Revisions of this file
bleservice.cpp Show diff for this revision Revisions of this file
bleservice.h Show diff for this revision Revisions of this file
hw/bleradio.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
mbed-src-flowcontrol.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GapAdvertisingData.cpp	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "GapAdvertisingData.h"
+
+/**************************************************************************/
+/*!
+    @brief  Creates a new GapAdvertisingData instance
+
+    @section EXAMPLE
+
+    @code
+
+    @endcode
+*/
+/**************************************************************************/
+GapAdvertisingData::GapAdvertisingData(void)
+{
+}
+
+/**************************************************************************/
+/*!
+    Destructor
+*/
+/**************************************************************************/
+GapAdvertisingData::~GapAdvertisingData(void)
+{
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GapAdvertisingData.h	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,15 @@
+#ifndef __GAP_ADVERTISING_DATA_H__
+#define __GAP_ADVERTISING_DATA_H__
+
+#include "blecommon.h"
+
+class GapAdvertisingData
+{
+public:
+    GapAdvertisingData(void);
+    virtual ~GapAdvertisingData(void);
+    
+private:
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GapAdvertisingParams.cpp	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "GapAdvertisingParams.h"
+
+/**************************************************************************/
+/*!
+    @brief  Creates a new GapAdvertisingParams
+
+    @section EXAMPLE
+
+    @code
+
+    @endcode
+*/
+/**************************************************************************/
+GapAdvertisingParams::GapAdvertisingParams(uint8_t advType, uint16_t interval, uint16_t timeout)
+{
+    _advType = advType;
+    _interval = interval;
+    _timeout = timeout;
+}
+
+/**************************************************************************/
+/*!
+    Destructor
+*/
+/**************************************************************************/
+GapAdvertisingParams::~GapAdvertisingParams(void)
+{
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GapAdvertisingParams.h	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,18 @@
+#ifndef __GAP_ADVERTISING_PARAMS_H__
+#define __GAP_ADVERTISING_PARAMS_H__
+
+#include "blecommon.h"
+
+class GapAdvertisingParams
+{
+public:
+    GapAdvertisingParams(uint8_t advType, uint16_t interval, uint16_t timeout);
+    virtual ~GapAdvertisingParams(void);
+
+private:
+    uint8_t  _advType;
+    uint16_t _interval;
+    uint16_t _timeout;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GattCharacteristic.cpp	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "GattCharacteristic.h"
+
+/**************************************************************************/
+/*!
+    @brief  Creates a new GattCharacteristic using the specified 16-bit
+            UUID, value length, and properties
+            
+    @note   The UUID value must be unique in the service and is normally >1
+
+    @param[in]  id
+                The 16-bit UUID to use for this characteristic
+    @param[in]  minLen
+                The min length in bytes of this characteristic's value
+    @param[in]  maxLen
+                The max length in bytes of this characteristic's value
+    @param[in]  props
+                The 8-bit bit field containing the characteristic's
+                properties
+
+    @section EXAMPLE
+
+    @code
+
+    // UUID = 0x2A19, Min length 2, Max len = 2, Properties = write
+    GattCharacteristic c = GattCharacteristic( 0x2A19, 2, 2, 0x08 );
+   
+    @endcode
+*/
+/**************************************************************************/
+GattCharacteristic::GattCharacteristic(uint16_t id, uint16_t minLen, uint16_t maxLen, uint8_t props)
+{
+    uuid = id;
+    memcpy(&properties, &props, 1);
+    lenMin = minLen;
+    lenMax = maxLen;
+}
+
+/**************************************************************************/
+/*!
+    Destructor
+*/
+/**************************************************************************/
+GattCharacteristic::~GattCharacteristic(void)
+{
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GattCharacteristic.h	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,33 @@
+#ifndef __GATT_CHARACTERISTIC_H__
+#define __GATT_CHARACTERISTIC_H__
+
+#include "blecommon.h"
+#include "uuid.h"
+
+class GattCharacteristic
+{
+private:
+
+public:
+    GattCharacteristic(uint16_t uuid, uint16_t minLen, uint16_t maxLen, uint8_t properties);
+    virtual ~GattCharacteristic(void);
+
+    uint16_t uuid;              /* Characteristic UUID */
+    uint16_t lenMin;            /* Minimum length of the value */
+    uint16_t lenMax;            /* Maximum length of the value */
+    uint8_t  index;
+    
+    struct Properties
+    {
+        /* Standard properties */
+        uint8_t broadcast       :1;         /**< Broadcasting of value permitted. */
+        uint8_t read            :1;         /**< Reading value permitted. */
+        uint8_t write_wo_resp   :1;         /**< Writing value with Write Command permitted. */
+        uint8_t write           :1;         /**< Writing value with Write Request permitted. */
+        uint8_t notify          :1;         /**< Notications of value permitted. */ // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
+        uint8_t indicate        :1;         /**< Indications of value permitted. */ // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
+        uint8_t auth_signed_wr  :1;         /**< Writing value with Signed Write Command permitted. */
+    } properties;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GattService.cpp	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,116 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "GattService.h"
+
+/**************************************************************************/
+/*!
+    @brief  Creates a new GattService using the specified 128-bit UUID
+            
+    @note   The UUID value must be unique on the device
+
+    @param[in]  uuid
+                The 16 byte (128-bit) UUID to use for this characteristic
+
+    @section EXAMPLE
+
+    @code
+   
+    @endcode
+*/
+/**************************************************************************/
+GattService::GattService(uint8_t base_uuid[16])
+{
+    primaryServiceID.update(base_uuid);
+    characteristicCount = 0;
+    memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
+    index = 0;
+}
+
+/**************************************************************************/
+/*!
+    @brief  Creates a new GattService using the specified 16-bit BLE UUID
+            
+    @param[in]  ble_uuid
+                The standardised 16-bit (2 byte) BLE UUID to use for this
+                characteristic
+
+    @section EXAMPLE
+
+    @code
+   
+    @endcode
+*/
+/**************************************************************************/
+GattService::GattService(uint16_t ble_uuid)
+{
+    primaryServiceID.update( ble_uuid );
+    characteristicCount = 0;
+    memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
+    index = 0;
+}
+
+/**************************************************************************/
+/*!
+    @brief  Destructor
+*/
+/**************************************************************************/
+GattService::~GattService(void)
+{
+}
+
+/**************************************************************************/
+/*!
+    @brief  Adds a GattCharacterisic to the service, serialising the
+            essential data for the characteristic.
+            
+    @note   The GattService does not store a reference to the source
+            GattCharacteristic, only a serialised version of the key
+            properties required to create the characteristic on the
+            target radio board.
+            
+    @note   This function will update the .index field in the
+            GattCharacteristic to indicate where this characteristic was
+            stored in the GattService's characteristic array.
+
+    @param[in]  characteristic
+                The GattCharacteristic object describing the characteristic
+                to add to this service
+
+    @returns    BLE_ERROR_NONE (0) if everything executed correctly, or an
+                error code if there was a problem
+    @retval     BLE_ERROR_NONE
+                Everything executed correctly
+
+    @section EXAMPLE
+
+    @code
+   
+    @endcode
+*/
+/**************************************************************************/
+ble_error_t GattService::addCharacteristic(GattCharacteristic & characteristic)
+{
+    /* ToDo: Make sure we don't overflow the array, etc. */
+    /* ToDo: Make sure this characteristic UUID doesn't already exist */
+    /* ToDo: Basic validation */
+    
+    serialisedChar_t c;
+    
+    /* Serialise the source GattCharacteristic */
+    memcpy(&c.id, &characteristic.uuid, 2);
+    memcpy(&c.lenMin, &characteristic.lenMin, 2);
+    memcpy(&c.lenMax, &characteristic.lenMax, 2);
+    memcpy(&c.properties, &characteristic.properties, 2);
+    memset(&c.reserved, 0, 1);
+    
+    /* Insert the serialised object into the buffer */
+    memcpy(&characteristics[characteristicCount], &c, sizeof(serialisedChar_t));
+    
+    /* Update the index value */
+    characteristic.index = characteristicCount;
+    
+    characteristicCount++;
+    
+    return BLE_ERROR_NONE;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GattService.h	Tue Dec 10 07:32:12 2013 +0000
@@ -0,0 +1,36 @@
+#ifndef __GATT_SERVICE_H__
+#define __GATT_SERVICE_H__
+
+#include "blecommon.h"
+#include "uuid.h"
+#include "GattCharacteristic.h"
+
+#define BLE_SERVICE_MAX_CHARACTERISTICS (5)
+
+class GattService
+{
+private:
+
+public:
+    typedef struct
+    {
+        uint16_t    id;
+        uint16_t    lenMin;
+        uint16_t    lenMax;
+        uint8_t     properties;
+        uint8_t     reserved;
+    } serialisedChar_t;
+    
+    GattService(uint8_t[16]);  /* 128-bit Base UUID */
+    GattService(uint16_t);     /* 16-bit BLE UUID */
+    virtual ~GattService(void);
+
+    UUID                primaryServiceID;
+    uint8_t             characteristicCount;
+    serialisedChar_t    characteristics[BLE_SERVICE_MAX_CHARACTERISTICS];
+    uint8_t             index;
+
+    ble_error_t         addCharacteristic(GattCharacteristic &);
+};
+
+#endif
--- a/blecharacteristic.cpp	Wed Dec 04 07:20:24 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-#include "blecharacteristic.h"
-
-/**************************************************************************/
-/*!
-    @brief  Creates a new BLECharacteristic using the specified 16-bit
-            UUID, value length, and properties
-            
-    @note   The UUID value must be unique in the service and is normally >1
-
-    @param[in]  id
-                The 16-bit UUID to use for this characteristic
-    @param[in]  minLen
-                The min length in bytes of this characteristic's value
-    @param[in]  maxLen
-                The max length in bytes of this characteristic's value
-    @param[in]  props
-                The 8-bit bit field containing the characteristic's
-                properties
-
-    @section EXAMPLE
-
-    @code
-
-    // UUID = 0x2A19, Min length 2, Max len = 2, Properties = write
-    BLECharacteristic c = BLECharacteristic( 0x2A19, 2, 2, 0x08 );
-   
-    @endcode
-*/
-/**************************************************************************/
-BLECharacteristic::BLECharacteristic(uint16_t id, uint16_t minLen, uint16_t maxLen, uint8_t props)
-{
-    uuid = id;
-    memcpy(&properties, &props, 1);
-    lenMin = minLen;
-    lenMax = maxLen;
-}
-
-/**************************************************************************/
-/*!
-    Destructor
-*/
-/**************************************************************************/
-BLECharacteristic::~BLECharacteristic(void)
-{
-}
--- a/blecharacteristic.h	Wed Dec 04 07:20:24 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-#ifndef __BLE_CHARACTERISTIC_H__
-#define __BLE_CHARACTERISTIC_H__
-
-#include "blecommon.h"
-#include "uuid.h"
-
-class BLECharacteristic
-{
-private:
-
-public:
-    BLECharacteristic(uint16_t uuid, uint16_t minLen, uint16_t maxLen, uint8_t properties);
-    virtual ~BLECharacteristic(void);
-
-    uint16_t uuid;              /* Characteristic UUID */
-    uint16_t lenMin;            /* Minimum length of the value */
-    uint16_t lenMax;            /* Maximum length of the value */
-    uint8_t  index;
-    
-    struct Properties
-    {
-        /* Standard properties */
-        uint8_t broadcast       :1;         /**< Broadcasting of value permitted. */
-        uint8_t read            :1;         /**< Reading value permitted. */
-        uint8_t write_wo_resp   :1;         /**< Writing value with Write Command permitted. */
-        uint8_t write           :1;         /**< Writing value with Write Request permitted. */
-        uint8_t notify          :1;         /**< Notications of value permitted. */ // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
-        uint8_t indicate        :1;         /**< Indications of value permitted. */ // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
-        uint8_t auth_signed_wr  :1;         /**< Writing value with Signed Write Command permitted. */
-    } properties;
-};
-
-#endif
--- a/blecommon.h	Wed Dec 04 07:20:24 2013 +0000
+++ b/blecommon.h	Tue Dec 10 07:32:12 2013 +0000
@@ -8,6 +8,32 @@
   BLE_ERROR_NONE = 0
 } ble_error_t;
 
+typedef enum ble_gap_adv_flags_e
+{
+    BLE_GAP_ADV_FLAGS_
+} ble_gap_adv_flags_t;
+
+// https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
+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_t;
+
 // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
 typedef enum ble_gap_char_appearance_e
 {
@@ -52,8 +78,8 @@
   BLE_GAP_CHAR_APPEARANCE_CYCLING_POWER_SENSOR                            = 1156,
   BLE_GAP_CHAR_APPEARANCE_CYCLING_SPEED_AND_CADENCE_SENSOR                = 1157,
   BLE_GAP_CHAR_APPEARANCE_PULSE_OXIMETER_GENERIC                          = 3136,
-  BLE_GAP_CHAR_APPEARANCE_PULSE_OXIMETERFINGERTIP                         = 3137,
-  BLE_GAP_CHAR_APPEARANCE_PULSE_OXIMETERWRIST_WORN                        = 3138,
+  BLE_GAP_CHAR_APPEARANCE_PULSE_OXIMETER_FINGERTIP                        = 3137,
+  BLE_GAP_CHAR_APPEARANCE_PULSE_OXIMETER_WRIST_WORN                       = 3138,
   BLE_GAP_CHAR_APPEARANCE_OUTDOOR_GENERIC                                 = 5184,
   BLE_GAP_CHAR_APPEARANCE_OUTDOOR_LOCATION_DISPLAY_DEVICE                 = 5185,
   BLE_GAP_CHAR_APPEARANCE_OUTDOOR_LOCATION_AND_NAVIGATION_DISPLAY_DEVICE  = 5186,
@@ -205,13 +231,13 @@
   BLE_GATT_FORMAT_DUINT16             = 0x18, /**< IEEE-20601 format. */
   BLE_GATT_FORMAT_UTF8S               = 0x19, /**< UTF-8 string. */
   BLE_GATT_FORMAT_UTF16S              = 0x1A, /**< UTF-16 string. */
-  BLE_GATT_FORMAT_STRUCT              = 0x1B /**< Opaque Structure. */
+  BLE_GATT_FORMAT_STRUCT              = 0x1B  /**< Opaque Structure. */
 } ble_gatt_format_t;
 
 struct UTF8String
 {
   uint16_t  length;         /**< String length. */
-  uint8_t  str[32];         /**< String data. */
+  uint8_t   str[32];        /**< String data. */
 };
 
 struct UTF16String
@@ -227,13 +253,13 @@
 };
 
 // See https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
-struct PresentationFormat
+typedef struct PresentationFormat
 {
-  uint8_t   format;         /**< Format of the value, see @ref ble_gatt_format_t. */
+  uint8_t   gatt_format;    /**< Format of the value, see @ref ble_gatt_format_t. */
   int8_t    exponent;       /**< Exponent for integer data types. */
-  uint16_t  unit;           /**< UUID from Bluetooth Assigned Numbers, see @ref ble_gatt_unit_t. */
-  uint8_t   name_space;     /**< Namespace from Bluetooth Assigned Numbers, see @ref BLE_GATT_CPF_NAMESPACES. */
-  uint16_t  desc;           /**< Namespace description from Bluetooth Assigned Numbers, see @ref BLE_GATT_CPF_NAMESPACES. */
-};
+  uint16_t  gatt_unit;      /**< UUID from Bluetooth Assigned Numbers, see @ref ble_gatt_unit_t. */
+  uint8_t   gatt_namespace; /**< Namespace from Bluetooth Assigned Numbers, normally '1',  see @ref BLE_GATT_CPF_NAMESPACES. */
+  uint16_t  gatt_nsdesc;    /**< Namespace description from Bluetooth Assigned Numbers, normally '0', see @ref BLE_GATT_CPF_NAMESPACES. */
+} presentation_format_t;
 
 #endif
--- a/bleservice.cpp	Wed Dec 04 07:20:24 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,116 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-
-#include "bleservice.h"
-
-/**************************************************************************/
-/*!
-    @brief  Creates a new BLEService using the specified 16 byte UUID
-            
-    @note   The UUID value must be unique on the device
-
-    @param[in]  uuid
-                The 16 byte (128-bit) UUID to use for this characteristic
-
-    @section EXAMPLE
-
-    @code
-   
-    @endcode
-*/
-/**************************************************************************/
-BLEService::BLEService(uint8_t base_uuid[16])
-{
-    primaryServiceID.update(base_uuid);
-    characteristicCount = 0;
-    memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
-    index = 0;
-}
-
-/**************************************************************************/
-/*!
-    @brief  Creates a new BLEService using the specified 2 byte BLE UUID
-            
-    @param[in]  ble_uuid
-                The standardised 16-bit (2 byte) BLE UUID to use for this
-                characteristic
-
-    @section EXAMPLE
-
-    @code
-   
-    @endcode
-*/
-/**************************************************************************/
-BLEService::BLEService(uint16_t ble_uuid)
-{
-    primaryServiceID.update( ble_uuid );
-    characteristicCount = 0;
-    memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
-    index = 0;
-}
-
-/**************************************************************************/
-/*!
-    @brief  Destructor
-*/
-/**************************************************************************/
-BLEService::~BLEService(void)
-{
-}
-
-/**************************************************************************/
-/*!
-    @brief  Adds a BLECharacterisic to the service, serialising the
-            essential data for the characteristic.
-            
-    @note   The BLEService does not store a reference to the source
-            BLECharacteristic, only a serialised version of the key
-            properties required to create the characteristic on the
-            target radio board.
-            
-    @note   This function will update the .index field in the
-            BLECharacteristic to indicate where this characteristic was
-            stored in the BLEService's characteristic array.
-
-    @param[in]  characteristic
-                The BLECharacteristic object describing the characteristic
-                to add to this service
-
-    @returns    BLE_ERROR_NONE (0) if everything executed correctly, or an
-                error code if there was a problem
-    @retval     BLE_ERROR_NONE
-                Everything executed correctly
-
-    @section EXAMPLE
-
-    @code
-   
-    @endcode
-*/
-/**************************************************************************/
-ble_error_t BLEService::addCharacteristic(BLECharacteristic & characteristic)
-{
-    /* ToDo: Make sure we don't overflow the array, etc. */
-    /* ToDo: Make sure this characteristic UUID doesn't already exist */
-    /* ToDo: Basic validation */
-    
-    serialisedChar_t c;
-    
-    /* Serialise the source BLECharacteristic */
-    memcpy(&c.id, &characteristic.uuid, 2);
-    memcpy(&c.lenMin, &characteristic.lenMin, 2);
-    memcpy(&c.lenMax, &characteristic.lenMax, 2);
-    memcpy(&c.properties, &characteristic.properties, 2);
-    memset(&c.reserved, 0, 1);
-    
-    /* Insert the serialised object into the buffer */
-    memcpy(&characteristics[characteristicCount], &c, sizeof(serialisedChar_t));
-    
-    /* Update the index value */
-    characteristic.index = characteristicCount;
-    
-    characteristicCount++;
-    
-    return BLE_ERROR_NONE;
-}
--- a/bleservice.h	Wed Dec 04 07:20:24 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-#ifndef __BLE_SERVICE_H__
-#define __BLE_SERVICE_H__
-
-#include "blecommon.h"
-#include "uuid.h"
-#include "blecharacteristic.h"
-
-#define BLE_SERVICE_MAX_CHARACTERISTICS (5)
-
-class BLEService
-{
-private:
-
-public:
-    typedef struct
-    {
-        uint16_t    id;
-        uint16_t    lenMin;
-        uint16_t    lenMax;
-        uint8_t     properties;
-        uint8_t     reserved;
-    } serialisedChar_t;
-    
-    BLEService(uint8_t[16]);  /* 128-bit Base UUID */
-    BLEService(uint16_t);     /* 16-bit BLE UUID */
-    virtual ~BLEService(void);
-
-    UUID                primaryServiceID;
-    uint8_t             characteristicCount;
-    serialisedChar_t    characteristics[BLE_SERVICE_MAX_CHARACTERISTICS];
-    uint8_t             index;
-
-    ble_error_t         addCharacteristic(BLECharacteristic &);
-};
-
-#endif
--- a/hw/bleradio.h	Wed Dec 04 07:20:24 2013 +0000
+++ b/hw/bleradio.h	Tue Dec 10 07:32:12 2013 +0000
@@ -2,10 +2,15 @@
 #define __BLE_RADIO_H__
 
 #include "blecommon.h"
-#include "bleservice.h"
+#include "GattService.h"
+#include "GapAdvertisingData.h"
+#include "GapAdvertisingParams.h"
 
 class BLERadio
 {
+    protected:
+        FunctionPointer _callback_event;
+        
     public:
         typedef enum radio_event_e
         {
@@ -20,12 +25,23 @@
         /* ToDo: Force constructor with event handler callback */
 
         /* These functions must be defined in the sub-class */
-        //virtual ble_error_t attach(void (*fptr)(void));
-        virtual ble_error_t addService(BLEService &) = 0;
-        virtual ble_error_t updateValue(uint8_t, uint8_t, uint8_t[], uint16_t) = 0;
+        virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &) = 0;
+        virtual ble_error_t addService(GattService &) = 0;
+        virtual ble_error_t readCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t) = 0;
+        virtual ble_error_t writeCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t) = 0;
         virtual ble_error_t start(void) = 0;
         virtual ble_error_t stop(void) = 0;
         virtual ble_error_t reset(void) = 0;
+
+        /* BLE event callback (connect, disconnect, etc.) */
+        void attach(void (*function)(void)) { 
+            _callback_event.attach( function ); 
+        }
+ 
+        template<typename T>
+        void attach(T *object, void (T::*member)(void)) { 
+            _callback_event.attach( object, member );
+        }
 };
 
 #endif
--- a/hw/nrf51822.cpp	Wed Dec 04 07:20:24 2013 +0000
+++ b/hw/nrf51822.cpp	Tue Dec 10 07:32:12 2013 +0000
@@ -17,9 +17,6 @@
 /**************************************************************************/
 /*!
     @brief  Constructor
-    
-    @args   fptr[in]    Pointer to the callback function when any radio
-                        event is raised by the radio HW.
 */
 /**************************************************************************/
 //nRF51822::nRF51822() : uart(P0_4, P0_0) /* LPC812 */
@@ -27,14 +24,14 @@
 {
     /* Setup the nRF UART interface */
     uart.baud(9600);
- 
+
     /* Echo data on the debug CDC port */
     uart.attach(this, &nRF51822::uartCallback);
     
     /* Add flow control for UART (required by the nRF51822) */
     //uart.set_flow_control(Serial::RTSCTS, P0_6, P0_8);  /* LPC812 */
     uart.set_flow_control(Serial::RTSCTS, p30, p29);      /* LPC1768 */
-    
+
     /* Reset the service counter */
     serviceCount = 0;
 }
@@ -53,17 +50,40 @@
 
 */
 /**************************************************************************/
-//ble_error_t nRF51822::attach(void (*fptr)(void))
-//{
-//    return BLE_ERROR_NONE;
-//}
+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();
+    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();
+}
 
 /**************************************************************************/
 /*!
 
 */
 /**************************************************************************/
-ble_error_t nRF51822::addService(BLEService & service)
+ble_error_t nRF51822::setAdvertising(GapAdvertisingParams &, GapAdvertisingData &)
+{
+    return BLE_ERROR_NONE;
+}
+
+/**************************************************************************/
+/*!
+
+*/
+/**************************************************************************/
+ble_error_t nRF51822::addService(GattService & service)
 {
     /* ToDo: Make sure we don't overflow the array, etc. */
     /* ToDo: Make sure this service UUID doesn't already exist (?) */
@@ -128,13 +148,36 @@
 
 /**************************************************************************/
 /*!
+    @brief  Reads the value of a characteristic, based on the service
+            and characteristic index fields
+
+    @param[in]  service
+                The GattService to read from
+    @param[in]  characteristic
+                The GattCharacteristic to read from
+    @param[in]  buffer
+                Buffer to hold the the characteristic's value
+                (raw byte array in LSB format)
+    @param[in]  len
+                The number of bytes read into the buffer
+*/
+/**************************************************************************/
+ble_error_t nRF51822::readCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
+{
+    /* ToDo */
+    
+    return BLE_ERROR_NONE;
+}
+
+/**************************************************************************/
+/*!
     @brief  Updates the value of a characteristic, based on the service
             and characteristic index fields
             
-    @param[in]  sIndex  
-                The BLEService's index value (.index)
-    @param[in]  cIndex
-                The BLECharacteristic's index value (.index)
+    @param[in]  service
+                The GattService to write to
+    @param[in]  characteristic
+                The GattCharacteristic to write to
     @param[in]  buffer
                 Data to use when updating the characteristic's value
                 (raw byte array in LSB format)
@@ -142,10 +185,10 @@
                 The number of bytes in buffer
 */
 /**************************************************************************/
-ble_error_t nRF51822::updateValue(uint8_t sIndex, uint8_t cIndex, uint8_t buffer[], uint16_t len)
+ble_error_t nRF51822::writeCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
 {
     /* Command ID = 0x0006, Payload = Service ID, Characteristic ID, Value */
-    uart.printf("10 06 00 %02X %02X %02X", len + 2, cIndex, sIndex);
+    uart.printf("10 06 00 %02X %02X %02X", len + 2, characteristic.index, service.index);
     for (uint16_t i = 0; i<len; i++)
     {
         uart.printf(" %02X", buffer[i]);
--- a/hw/nrf51822.h	Wed Dec 04 07:20:24 2013 +0000
+++ b/hw/nrf51822.h	Tue Dec 10 07:32:12 2013 +0000
@@ -3,8 +3,8 @@
 
 #include "mbed.h"
 #include "blecommon.h"
-#include "bleservice.h"
 #include "bleradio.h"
+#include "GattService.h"
 
 class nRF51822 : public BLERadio
 {
@@ -13,18 +13,21 @@
         virtual ~nRF51822(void);
 
         /* Functions that mus be implemented from NRFRadio */
-        //virtual ble_error_t attach(void (*fptr)(void));
-        virtual ble_error_t addService(BLEService &);
-        virtual ble_error_t updateValue(uint8_t, uint8_t, uint8_t[], uint16_t);
+        virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &);
+        virtual ble_error_t addService(GattService &);
+        virtual ble_error_t readCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t);
+        virtual ble_error_t writeCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t);
         virtual ble_error_t start(void);
         virtual ble_error_t stop(void);
         virtual ble_error_t reset(void);
         
+        void test(void);
+
+    private:
+        Serial uart;
+
         /* nRF51 Functions */
         void uartCallback(void);
-
-    private:
-        Serial uart;        
 };
 
 #endif
--- a/main.cpp	Wed Dec 04 07:20:24 2013 +0000
+++ b/main.cpp	Tue Dec 10 07:32:12 2013 +0000
@@ -1,8 +1,8 @@
 #include "mbed.h"
 
 #include "uuid.h"
-#include "bleservice.h"
-#include "blecharacteristic.h"
+#include "GattService.h"
+#include "GattCharacteristic.h"
 #include "hw/nrf51822.h"
 
 DigitalOut        myled ( LED1 );
@@ -12,27 +12,30 @@
 
 /* Battery Level Service */
 /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml */
-BLEService        battService   ( 0x180F );
-BLECharacteristic battLevel     ( 0x2A19, 1, 1, 0x10 | 0x02);   /* Read + Notify */
+GattService        battService   ( 0x180F );
+GattCharacteristic battLevel     ( 0x2A19, 1, 1, 0x10 | 0x02);   /* Read + Notify */
 
 /* Heart Rate Monitor Service */
 /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
-BLEService        hrmService    ( 0x180D );
-BLECharacteristic hrmRate       ( 0x2A37, 2, 3, 0x10 );         /* Notify */
-BLECharacteristic hrmLocation   ( 0x2A39, 1, 1, 0x02 );         /* Read */
+GattService        hrmService    ( 0x180D );
+GattCharacteristic hrmRate       ( 0x2A37, 2, 3, 0x10 );         /* Notify */
+GattCharacteristic hrmLocation   ( 0x2A39, 1, 1, 0x02 );         /* Read */
 
 /* Health Thermometer Service */
 /* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.health_thermometer.xml */
-BLEService        thermService  ( 0x1809 );
-BLECharacteristic thermTemp     ( 0x2A1C, 5, 13, 0x20 );         /* Indicate */
-BLECharacteristic thermType     ( 0x2A1D, 1, 1, 0x02 );          /* Read */
-BLECharacteristic thermInterval ( 0x2A21, 2, 2, 0x02 );          /* Read */
+GattService        thermService  ( 0x1809 );
+GattCharacteristic thermTemp     ( 0x2A1C, 5, 13, 0x20 );         /* Indicate */
+GattCharacteristic thermType     ( 0x2A1D, 1, 1, 0x02 );          /* Read */
+GattCharacteristic thermInterval ( 0x2A21, 2, 2, 0x02 );          /* Read */
 
 /* Notify   = device (server) sends data when it changes */
 /* Indicate = device (server) sends data when it changes and client confirms reception */ 
  
 int main()
 {
+    radio.test();
+    while(1);
+
     /* Add the battery level characteristic to the battery service          */
     /* Note: This will also update the characteristic's .index field        */
     /* so that we know where it's stored in the BLEService.characteristics  */
@@ -63,20 +66,20 @@
     /* Set the heart rate monitor location (one time only) */
     /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
     uint8_t location = 0x01; /* Chest */
-    radio.updateValue(hrmService.index, hrmLocation.index, (uint8_t*)&location, sizeof(location));
+    radio.writeCharacteristic(hrmService, hrmLocation, (uint8_t*)&location, sizeof(location));
 
     /* Update the battery level */
     /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml */
     uint8_t batt = 72; /* Percentage (0..100) */
-    radio.updateValue(battService.index, battLevel.index, (uint8_t*)&batt, sizeof(batt));
+    radio.writeCharacteristic(battService, battLevel, (uint8_t*)&batt, sizeof(batt));
 
     /* Update the fixed health thermometer characteristics */
     /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml */
     uint8_t thermLocation = 6; /* Location = mouth */
-    radio.updateValue(thermService.index, thermType.index, (uint8_t*)&thermLocation, sizeof(thermLocation));
+    radio.writeCharacteristic(thermService, thermType, (uint8_t*)&thermLocation, sizeof(thermLocation));
     /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.measurement_interval.xml */
     uint16_t thermDelay = 5;
-    radio.updateValue(thermService.index, thermInterval.index, (uint8_t*)&thermDelay, sizeof(thermDelay));
+    radio.writeCharacteristic(thermService, thermInterval, (uint8_t*)&thermDelay, sizeof(thermDelay));
     
     /* Blinky + value updates */
     uint8_t hrmCounter = 100;
@@ -94,7 +97,7 @@
         hrmCounter++;
         if (hrmCounter == 175) hrmCounter = 100;
         uint8_t bpm[2] = { 0x00, hrmCounter };
-        radio.updateValue(hrmService.index, hrmRate.index, bpm, 2);
+        radio.writeCharacteristic(hrmService, hrmRate, bpm, 2);
         
         /* Update the Health Thermometer measurement */
         
@@ -109,6 +112,6 @@
         uint8_t temperature[5] = { 0x00, 0x00, 0x00, 0x00, 0xFF };
         // Use the hrm counter to provide a shifting temperature value (175 = 17.5C, etc.)
         memcpy (temperature+1, &hrmCounter, 1);
-        radio.updateValue(thermService.index, thermTemp.index, temperature, 5);
+        radio.writeCharacteristic(thermService, thermTemp, temperature, 5);
     }
 }
--- a/mbed-src-flowcontrol.lib	Wed Dec 04 07:20:24 2013 +0000
+++ b/mbed-src-flowcontrol.lib	Tue Dec 10 07:32:12 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/bogdanm/code/mbed-src-flowcontrol/#d8b836b18f9c
+http://mbed.org/users/bogdanm/code/mbed-src-flowcontrol/#5b5370bf691e