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 Jan 07 19:58:06 2014 +0000
Parent:
22:260313bcf4d0
Child:
24:12eb3f19e9a4
Commit message:
Added GattService example

Changed in this revision

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
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
--- a/GattCharacteristic.cpp	Tue Jan 07 10:54:02 2014 +0000
+++ b/GattCharacteristic.cpp	Tue Jan 07 19:58:06 2014 +0000
@@ -36,6 +36,7 @@
     memcpy(&properties, &props, 1);
     lenMin = minLen;
     lenMax = maxLen;
+    handle = 0;
 }
 
 /**************************************************************************/
--- a/GattCharacteristic.h	Tue Jan 07 10:54:02 2014 +0000
+++ b/GattCharacteristic.h	Tue Jan 07 19:58:06 2014 +0000
@@ -9,7 +9,7 @@
 private:
 
 public:
-    GattCharacteristic(uint16_t uuid, uint16_t minLen, uint16_t maxLen, uint8_t properties);
+    GattCharacteristic(uint16_t uuid=0, uint16_t minLen=1, uint16_t maxLen=1, uint8_t properties=0);
     virtual ~GattCharacteristic(void);
 
     uint16_t uuid;              /* Characteristic UUID */
--- a/GattService.cpp	Tue Jan 07 10:54:02 2014 +0000
+++ b/GattService.cpp	Tue Jan 07 19:58:06 2014 +0000
@@ -23,7 +23,6 @@
 {
     primaryServiceID.update(base_uuid);
     characteristicCount = 0;
-    memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
     handle = 0;
 }
 
@@ -46,7 +45,6 @@
 {
     primaryServiceID.update( ble_uuid );
     characteristicCount = 0;
-    memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
     handle = 0;
 }
 
@@ -61,17 +59,11 @@
 
 /**************************************************************************/
 /*!
-    @brief  Adds a GattCharacterisic to the service, serialising the
-            essential data for the characteristic.
+    @brief  Adds a GattCharacterisic to the service.
             
-    @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 .handle field in the
-            GattCharacteristic to indicate where this characteristic was
-            stored in the GattService's characteristic array.
+    @note   This function will not update the .handle field in the
+            GattCharacteristic. This value is updated when the parent
+            service is added via the radio driver. 
 
     @param[in]  characteristic
                 The GattCharacteristic object describing the characteristic
@@ -94,22 +86,8 @@
     /* 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 handle value */
-    characteristic.handle = characteristicCount;
-    
+
+    characteristics[characteristicCount] = characteristic;
     characteristicCount++;
     
     return BLE_ERROR_NONE;
--- a/GattService.h	Tue Jan 07 10:54:02 2014 +0000
+++ b/GattService.h	Tue Jan 07 19:58:06 2014 +0000
@@ -12,22 +12,13 @@
 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];
+    GattCharacteristic  characteristics[BLE_SERVICE_MAX_CHARACTERISTICS];
     uint8_t             handle;
 
     ble_error_t         addCharacteristic(GattCharacteristic &);
--- a/hw/bleradio.h	Tue Jan 07 10:54:02 2014 +0000
+++ b/hw/bleradio.h	Tue Jan 07 19:58:06 2014 +0000
@@ -35,14 +35,15 @@
         } radioEvent_t;
         
         uint8_t serviceCount;
-        
+        uint8_t characteristicCount;
+
         /* ToDo: Force constructor with event handler callback */
 
         /* These functions must be defined in the sub-class */
         virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &, 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 readCharacteristic(uint8_t, uint8_t[], uint16_t) = 0;
+        virtual ble_error_t writeCharacteristic(uint8_t, 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;
--- a/hw/nrf51822.cpp	Tue Jan 07 10:54:02 2014 +0000
+++ b/hw/nrf51822.cpp	Tue Jan 07 19:58:06 2014 +0000
@@ -30,8 +30,9 @@
     /* Add flow control for UART (required by the nRF51822) */
     uart.set_flow_control(RawSerial::RTSCTS, p30, p29);
 
-    /* Reset the service counter */
+    /* Reset the service and characteristic counters */
     serviceCount = 0;
+    characteristicCount = 0;
 }
 
 /**************************************************************************/
@@ -263,8 +264,8 @@
     {
         /* Command ID = 0x0002 */
         uart.printf("10 02 00 0F 01 02 %02X %02X 04 02 %02X %02X 05 02 %02X %02X 03 01 %02X\r\n",
-                    service.characteristics[i].id & 0xFF, 
-                    service.characteristics[i].id >> 8,
+                    service.characteristics[i].uuid & 0xFF, 
+                    service.characteristics[i].uuid >> 8,
                     service.characteristics[i].lenMin & 0xFF,
                     service.characteristics[i].lenMin >> 8,
                     service.characteristics[i].lenMax & 0xFF,
@@ -273,9 +274,13 @@
                     
         /* ToDo: Check response */
         wait(0.1);
-    }    
+        
+        /* Update the characteristic handle */
+        service.characteristics[i].handle = characteristicCount;
+        characteristicCount++;
+    }
 
-    /* Update the service index value */
+    /* Update the service handle */
     service.handle = serviceCount;
     serviceCount++;
     
@@ -287,10 +292,8 @@
     @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]  charHandle
+                The handle of the GattCharacteristic to read from
     @param[in]  buffer
                 Buffer to hold the the characteristic's value
                 (raw byte array in LSB format)
@@ -309,7 +312,7 @@
     @endcode
 */
 /**************************************************************************/
-ble_error_t nRF51822::readCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
+ble_error_t nRF51822::readCharacteristic(uint8_t charHandle, uint8_t buffer[], uint16_t len)
 {
     /* ToDo */
     
@@ -320,11 +323,9 @@
 /*!
     @brief  Updates the value of a characteristic, based on the service
             and characteristic index fields
-            
-    @param[in]  service
-                The GattService to write to
-    @param[in]  characteristic
-                The GattCharacteristic to write to
+
+    @param[in]  charHandle
+                The handle of the GattCharacteristic to write to
     @param[in]  buffer
                 Data to use when updating the characteristic's value
                 (raw byte array in LSB format)
@@ -343,10 +344,10 @@
     @endcode
 */
 /**************************************************************************/
-ble_error_t nRF51822::writeCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
+ble_error_t nRF51822::writeCharacteristic(uint8_t charHandle, 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, characteristic.handle, service.handle);
+    /* Command ID = 0x0006, Payload = Characteristic ID, Value */
+    uart.printf("10 06 00 %02X %02X", len + 1, charHandle);
     for (uint16_t i = 0; i<len; i++)
     {
         uart.printf(" %02X", buffer[i]);
@@ -438,8 +439,9 @@
     /* Command ID = 0x0005, No payload */
     uart.printf("10 05 00 00\r\n");
 
-    /* Reset the service counter */
+    /* Reset the service and characteristic counters */
     serviceCount = 0;
+    characteristicCount = 0;
 
     /* Wait for the radio to come back up */    
     wait(1);
--- a/hw/nrf51822.h	Tue Jan 07 10:54:02 2014 +0000
+++ b/hw/nrf51822.h	Tue Jan 07 19:58:06 2014 +0000
@@ -22,8 +22,8 @@
         /* Functions that mus be implemented from BLERadio */
         virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &, 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 readCharacteristic(uint8_t, uint8_t[], uint16_t);
+        virtual ble_error_t writeCharacteristic(uint8_t, uint8_t[], uint16_t);
         virtual ble_error_t start(void);
         virtual ble_error_t stop(void);
         virtual ble_error_t reset(void);
--- a/main.cpp	Tue Jan 07 10:54:02 2014 +0000
+++ b/main.cpp	Tue Jan 07 19:58:06 2014 +0000
@@ -9,14 +9,13 @@
 
 void startBeacon(void)
 {
+    ble_error_t          error;
     GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED );
     GapAdvertisingData   advData;
     GapAdvertisingData   scanResponse;
     
     uint8_t iBeaconPayload[25] = { 0x4C, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 0x00, 0x00, 0x00, 0x00, 0xC8 };
     
-    /* ToDo: Check error conditions in a shared ASSERT with debug output via printf */
-    ble_error_t error;
     
     /* iBeacon includes the FLAG and MSD fields */
     error = advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
@@ -25,6 +24,31 @@
     error = radio.reset();
     error = radio.setAdvertising(advParams, advData, scanResponse);
     error = radio.start();
+    
+    /* Hang around here for a while */    
+    while(1)
+    {
+    }
+}
+
+void startBatteryService(void)
+{
+    ble_error_t        error;
+    GattService        battService ( 0x180F );
+    GattCharacteristic battLevel   ( 0x2A19, 1, 1, 0x10 | 0x02);
+    
+    error = radio.reset();
+    error = battService.addCharacteristic(battLevel);
+    error = radio.addService(battService);
+    error = radio.start();
+
+    uint8_t batt = 72;
+    error = radio.writeCharacteristic(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
+
+    /* Hang around here for a while */    
+    while(1)
+    {
+    }
 }
 
 int main()
@@ -32,7 +56,8 @@
     /* Give the radio some time to boot up and settle */
     wait(2);
 
-    startBeacon();
+    // startBeacon();
+    startBatteryService();
     
     while(1);
 }