BLE lib

Fork of Nucleo_BLE_API by STM32 eKairn

Files at this revision

API Documentation at this revision

Comitter:
vijaynvr
Date:
Sun Feb 08 14:26:05 2015 +0000
Parent:
3:a2cd3d33fe44
Commit message:
working

Changed in this revision

public/GattCharacteristic.h Show annotated file Show diff for this revision Revisions of this file
public/GattService.h Show annotated file Show diff for this revision Revisions of this file
services/HeartRateService.h Show annotated file Show diff for this revision Revisions of this file
services/UnicornService.h Show annotated file Show diff for this revision Revisions of this file
diff -r a2cd3d33fe44 -r f1696bf0e6c6 public/GattCharacteristic.h
--- a/public/GattCharacteristic.h	Mon Jan 05 18:45:24 2015 +0000
+++ b/public/GattCharacteristic.h	Sun Feb 08 14:26:05 2015 +0000
@@ -60,6 +60,7 @@
         UUID_HARDWARE_REVISION_STRING_CHAR                = 0x2A27,
         UUID_HEART_RATE_CONTROL_POINT_CHAR                = 0x2A39,
         UUID_HEART_RATE_MEASUREMENT_CHAR                  = 0x2A37,
+        UUID_UNICORN_MEDIA_CONTROL_CHAR                   = 0x2AFA,
         UUID_HID_CONTROL_POINT_CHAR                       = 0x2A4C,
         UUID_HID_INFORMATION_CHAR                         = 0x2A4A,
         UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR = 0x2A2A,
diff -r a2cd3d33fe44 -r f1696bf0e6c6 public/GattService.h
--- a/public/GattService.h	Mon Jan 05 18:45:24 2015 +0000
+++ b/public/GattService.h	Sun Feb 08 14:26:05 2015 +0000
@@ -57,6 +57,7 @@
         UUID_GLUCOSE_SERVICE                = 0x1808,
         UUID_HEALTH_THERMOMETER_SERVICE     = 0x1809,
         UUID_HEART_RATE_SERVICE             = 0x180D,
+        UUID_VIJAYS_UNICORN_SERVICE         = 0x18FF,
         UUID_HUMAN_INTERFACE_DEVICE_SERVICE = 0x1812,
         UUID_IMMEDIATE_ALERT_SERVICE        = 0x1802,
         UUID_LINK_LOSS_SERVICE              = 0x1803,
diff -r a2cd3d33fe44 -r f1696bf0e6c6 services/HeartRateService.h
--- a/services/HeartRateService.h	Mon Jan 05 18:45:24 2015 +0000
+++ b/services/HeartRateService.h	Sun Feb 08 14:26:05 2015 +0000
@@ -62,7 +62,7 @@
     /**
      * Same constructor as above, but with a 16-bit HRM Counter value.
      */
-    HeartRateService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) :
+    HeartRateService(BLEDevice &_ble, uint32_t hrmCounter, uint8_t location) :
         ble(_ble),
         valueBytes(hrmCounter),
         hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
@@ -86,7 +86,7 @@
     /**
      * Set a new 16-bit value for heart rate.
      */
-    void updateHeartRate(uint16_t hrmCounter) {
+    void updateHeartRate(uint32_t hrmCounter) {
         valueBytes.updateHeartRate(hrmCounter);
         ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
     }
@@ -127,7 +127,7 @@
 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 */
+        static const unsigned MAX_VALUE_BYTES  = 5; /* FLAGS + up to two bytes for heart-rate */
         static const unsigned FLAGS_BYTE_INDEX = 0;
 
         static const unsigned VALUE_FORMAT_BITNUM = 0;
@@ -137,7 +137,7 @@
             updateHeartRate(hrmCounter);
         }
 
-        HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() {
+        HeartRateValueBytes(uint32_t hrmCounter) : valueBytes() {
             updateHeartRate(hrmCounter);
         }
 
@@ -146,10 +146,12 @@
             valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
         }
 
-        void updateHeartRate(uint16_t hrmCounter) {
+        void updateHeartRate(uint32_t hrmCounter) {
             valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
             valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
             valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
+            valueBytes[FLAGS_BYTE_INDEX + 3] = (uint8_t)(hrmCounter >> 16);
+            valueBytes[FLAGS_BYTE_INDEX + 4] = (uint8_t)(hrmCounter >> 24);
         }
 
         uint8_t *getPointer(void) {
@@ -161,7 +163,7 @@
         }
 
         unsigned getNumValueBytes(void) const {
-            return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
+            return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint32_t) : sizeof(uint8_t));
         }
 
     private:
diff -r a2cd3d33fe44 -r f1696bf0e6c6 services/UnicornService.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/services/UnicornService.h	Sun Feb 08 14:26:05 2015 +0000
@@ -0,0 +1,182 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __BLE_HEART_RATE_SERVICE_H__
+#define __BLE_HEART_RATE_SERVICE_H__
+
+#include "BLEDevice.h"
+
+/* Heart Rate Service */
+/* Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
+/* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
+/* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
+class UnicornService {
+public:
+    enum {
+        LOCATION_OTHER = 0,
+        LOCATION_CHEST,
+        LOCATION_WRIST,
+        LOCATION_FINGER,
+        LOCATION_HAND,
+        LOCATION_EAR_LOBE,
+        LOCATION_FOOT,
+    };
+
+public:
+    /**
+     * Constructor.
+     *
+     * param[in] _ble
+     *               Reference to the underlying BLEDevice.
+     * param[in] hrmCounter (8-bit)
+     *               initial value for the hrm counter.
+     * param[in] location
+     *               Sensor's location.
+     */
+    UnicornService(BLEDevice &_ble, uint8_t hrmCounter, uint8_t location) :
+        ble(_ble),
+        valueBytes(hrmCounter),
+        hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
+                valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
+                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location),
+                    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
+        controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue,
+                     sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) {
+        setupService();
+    }
+
+    /**
+     * Same constructor as above, but with a 16-bit HRM Counter value.
+     */
+    UnicornService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) :
+        ble(_ble),
+        valueBytes(hrmCounter),
+        hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
+                valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
+                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location),
+                    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
+        controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue,
+                     sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) {
+        setupService();
+    }
+
+    /**
+     * Set a new 8-bit value for heart rate.
+     */
+    void updateHeartRate(uint8_t hrmCounter) {
+        valueBytes.updateHeartRate(hrmCounter);
+        ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
+    }
+
+    /**
+     * Set a new 16-bit value for heart rate.
+     */
+    void updateHeartRate(uint16_t hrmCounter) {
+        valueBytes.updateHeartRate(hrmCounter);
+        ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
+    }
+
+    /**
+     * This callback allows the UART service to receive updates to the
+     * txCharacteristic. The application should forward the call to this
+     * function from the global onDataWritten() callback handler; or if that's
+     * not used, this method can be used as a callback directly.
+     */
+    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
+             * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
+             * your constructor.
+             */
+        }
+    }
+
+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, &UnicornService::onDataWritten);
+    }
+
+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 */
+        static const unsigned FLAGS_BYTE_INDEX = 0;
+
+        static const unsigned VALUE_FORMAT_BITNUM = 0;
+        static const uint8_t  VALUE_FORMAT_FLAG   = (1 << VALUE_FORMAT_BITNUM);
+
+        HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() {
+            updateHeartRate(hrmCounter);
+        }
+
+        HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() {
+            updateHeartRate(hrmCounter);
+        }
+
+        void updateHeartRate(uint8_t hrmCounter) {
+            valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
+            valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
+        }
+
+        void updateHeartRate(uint16_t hrmCounter) {
+            valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
+            valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
+            valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
+        }
+
+        uint8_t *getPointer(void) {
+            return valueBytes;
+        }
+
+        const uint8_t *getPointer(void) const {
+            return valueBytes;
+        }
+
+        unsigned getNumValueBytes(void) const {
+            return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
+        }
+
+    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];
+    };
+
+private:
+    BLEDevice           &ble;
+    HeartRateValueBytes  valueBytes;
+    uint8_t              controlPointValue;
+    GattCharacteristic   hrmRate;
+    GattCharacteristic   hrmLocation;
+    GattCharacteristic   controlPoint;
+};
+
+#endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/