High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Revision:
710:b2e1a2660ec2
Parent:
671:33ec93d25695
--- a/services/HeartRateService.h	Fri Jun 19 15:53:06 2015 +0100
+++ b/services/HeartRateService.h	Fri Jun 19 15:53:28 2015 +0100
@@ -17,7 +17,7 @@
 #ifndef __BLE_HEART_RATE_SERVICE_H__
 #define __BLE_HEART_RATE_SERVICE_H__
 
-#include "BLE.h"
+#include "BLEDevice.h"
 
 /**
 * @class HeartRateService
@@ -47,13 +47,13 @@
      * @brief Constructor with 8bit HRM Counter value.
      *
      * @param[ref] _ble
-     *               Reference to the underlying BLE.
+     *               Reference to the underlying BLEDevice.
      * @param[in] hrmCounter (8-bit)
      *               initial value for the hrm counter.
      * @param[in] location
      *               Sensor's location.
      */
-    HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) :
+    HeartRateService(BLEDevice &_ble, uint8_t hrmCounter, uint8_t location) :
         ble(_ble),
         valueBytes(hrmCounter),
         hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
@@ -68,13 +68,13 @@
      * @brief Constructor with a 16-bit HRM Counter value.
      *
      * @param[in] _ble
-     *               Reference to the underlying BLE.
+     *               Reference to the underlying BLEDevice.
      * @param[in] hrmCounter (8-bit)
      *               initial value for the hrm counter.
      * @param[in] location
      *               Sensor's location.
      */
-    HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) :
+    HeartRateService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) :
         ble(_ble),
         valueBytes(hrmCounter),
         hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
@@ -114,8 +114,8 @@
      * @param[in] params
      *     Information about the characterisitc being updated.
      */
-    virtual void onDataWritten(const GattWriteCallbackParams *params) {
-        if (params->handle == controlPoint.getValueAttribute().getHandle()) {
+    virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
+        if (params->charHandle == controlPoint.getValueAttribute().getHandle()) {
             /* Do something here if the new value is 1; else you can override this method by
              * extending this class.
              * @NOTE: if you are extending this class, be sure to also call
@@ -125,16 +125,23 @@
         }
     }
 
-protected:
+private:
     void setupService(void) {
+        static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
+        if (serviceAdded) {
+            return;
+        }
+
         GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint};
         GattService         hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
         ble.addService(hrmService);
+        serviceAdded = true;
+
         ble.onDataWritten(this, &HeartRateService::onDataWritten);
     }
 
-protected:
+private:
     /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */
     struct HeartRateValueBytes {
         static const unsigned MAX_VALUE_BYTES  = 3; /* FLAGS + up to two bytes for heart-rate */
@@ -174,14 +181,14 @@
             return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
         }
 
-    private:
+private:
         /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
         /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
         uint8_t valueBytes[MAX_VALUE_BYTES];
     };
 
-protected:
-    BLE                 &ble;
+private:
+    BLEDevice           &ble;
 
     HeartRateValueBytes  valueBytes;
     uint8_t              controlPointValue;