jgh

Dependents:   Migration

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:13:52 2016 +0100
Revision:
1134:d540a48f650d
Parent:
1131:692ddf04fc42
Synchronized with git rev d363d7ee
Author: Rohit Grover
Merge branch 'develop'

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 1131:692ddf04fc42 1 /* mbed Microcontroller Library
vcoubard 1131:692ddf04fc42 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 1131:692ddf04fc42 3 *
vcoubard 1131:692ddf04fc42 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 1131:692ddf04fc42 5 * you may not use this file except in compliance with the License.
vcoubard 1131:692ddf04fc42 6 * You may obtain a copy of the License at
vcoubard 1131:692ddf04fc42 7 *
vcoubard 1131:692ddf04fc42 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 1131:692ddf04fc42 9 *
vcoubard 1131:692ddf04fc42 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 1131:692ddf04fc42 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 1131:692ddf04fc42 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 1131:692ddf04fc42 13 * See the License for the specific language governing permissions and
vcoubard 1131:692ddf04fc42 14 * limitations under the License.
vcoubard 1131:692ddf04fc42 15 */
vcoubard 1131:692ddf04fc42 16
vcoubard 1131:692ddf04fc42 17 #ifndef __BLE_HEART_RATE_SERVICE_H__
vcoubard 1131:692ddf04fc42 18 #define __BLE_HEART_RATE_SERVICE_H__
vcoubard 1131:692ddf04fc42 19
vcoubard 1131:692ddf04fc42 20 #include "ble/BLE.h"
vcoubard 1131:692ddf04fc42 21
vcoubard 1131:692ddf04fc42 22 /**
vcoubard 1131:692ddf04fc42 23 * @class HeartRateService
vcoubard 1134:d540a48f650d 24 * @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor and the heart rate in beats per minute.
vcoubard 1134:d540a48f650d 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml
vcoubard 1134:d540a48f650d 26 * HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
vcoubard 1131:692ddf04fc42 27 * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml
vcoubard 1131:692ddf04fc42 28 */
vcoubard 1131:692ddf04fc42 29 class HeartRateService {
vcoubard 1131:692ddf04fc42 30 public:
vcoubard 1131:692ddf04fc42 31 /**
vcoubard 1131:692ddf04fc42 32 * @enum SensorLocation
vcoubard 1134:d540a48f650d 33 * @brief Location of the heart rate sensor on body.
vcoubard 1131:692ddf04fc42 34 */
vcoubard 1131:692ddf04fc42 35 enum {
vcoubard 1134:d540a48f650d 36 LOCATION_OTHER = 0, /*!< Other location. */
vcoubard 1134:d540a48f650d 37 LOCATION_CHEST, /*!< Chest. */
vcoubard 1134:d540a48f650d 38 LOCATION_WRIST, /*!< Wrist. */
vcoubard 1134:d540a48f650d 39 LOCATION_FINGER, /*!< Finger. */
vcoubard 1134:d540a48f650d 40 LOCATION_HAND, /*!< Hand. */
vcoubard 1134:d540a48f650d 41 LOCATION_EAR_LOBE, /*!< Earlobe. */
vcoubard 1134:d540a48f650d 42 LOCATION_FOOT, /*!< Foot. */
vcoubard 1131:692ddf04fc42 43 };
vcoubard 1131:692ddf04fc42 44
vcoubard 1131:692ddf04fc42 45 public:
vcoubard 1131:692ddf04fc42 46 /**
vcoubard 1134:d540a48f650d 47 * @brief Constructor with 8-bit HRM Counter value.
vcoubard 1131:692ddf04fc42 48 *
vcoubard 1131:692ddf04fc42 49 * @param[ref] _ble
vcoubard 1131:692ddf04fc42 50 * Reference to the underlying BLE.
vcoubard 1131:692ddf04fc42 51 * @param[in] hrmCounter (8-bit)
vcoubard 1134:d540a48f650d 52 * Initial value for the HRM counter.
vcoubard 1131:692ddf04fc42 53 * @param[in] location
vcoubard 1131:692ddf04fc42 54 * Sensor's location.
vcoubard 1131:692ddf04fc42 55 */
vcoubard 1131:692ddf04fc42 56 HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) :
vcoubard 1131:692ddf04fc42 57 ble(_ble),
vcoubard 1131:692ddf04fc42 58 valueBytes(hrmCounter),
vcoubard 1131:692ddf04fc42 59 hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
vcoubard 1131:692ddf04fc42 60 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
vcoubard 1131:692ddf04fc42 61 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
vcoubard 1131:692ddf04fc42 62 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
vcoubard 1131:692ddf04fc42 63 controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
vcoubard 1131:692ddf04fc42 64 setupService();
vcoubard 1131:692ddf04fc42 65 }
vcoubard 1131:692ddf04fc42 66
vcoubard 1131:692ddf04fc42 67 /**
vcoubard 1131:692ddf04fc42 68 * @brief Constructor with a 16-bit HRM Counter value.
vcoubard 1131:692ddf04fc42 69 *
vcoubard 1131:692ddf04fc42 70 * @param[in] _ble
vcoubard 1131:692ddf04fc42 71 * Reference to the underlying BLE.
vcoubard 1131:692ddf04fc42 72 * @param[in] hrmCounter (8-bit)
vcoubard 1134:d540a48f650d 73 * Initial value for the HRM counter.
vcoubard 1131:692ddf04fc42 74 * @param[in] location
vcoubard 1131:692ddf04fc42 75 * Sensor's location.
vcoubard 1131:692ddf04fc42 76 */
vcoubard 1131:692ddf04fc42 77 HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) :
vcoubard 1131:692ddf04fc42 78 ble(_ble),
vcoubard 1131:692ddf04fc42 79 valueBytes(hrmCounter),
vcoubard 1131:692ddf04fc42 80 hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
vcoubard 1131:692ddf04fc42 81 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
vcoubard 1131:692ddf04fc42 82 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
vcoubard 1131:692ddf04fc42 83 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
vcoubard 1131:692ddf04fc42 84 controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
vcoubard 1131:692ddf04fc42 85 setupService();
vcoubard 1131:692ddf04fc42 86 }
vcoubard 1131:692ddf04fc42 87
vcoubard 1131:692ddf04fc42 88 /**
vcoubard 1134:d540a48f650d 89 * @brief Set a new 8-bit value for the heart rate.
vcoubard 1131:692ddf04fc42 90 *
vcoubard 1131:692ddf04fc42 91 * @param[in] hrmCounter
vcoubard 1134:d540a48f650d 92 * Heart rate in BPM.
vcoubard 1131:692ddf04fc42 93 */
vcoubard 1131:692ddf04fc42 94 void updateHeartRate(uint8_t hrmCounter) {
vcoubard 1131:692ddf04fc42 95 valueBytes.updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 96 ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
vcoubard 1131:692ddf04fc42 97 }
vcoubard 1131:692ddf04fc42 98
vcoubard 1131:692ddf04fc42 99 /**
vcoubard 1134:d540a48f650d 100 * Set a new 16-bit value for the heart rate.
vcoubard 1131:692ddf04fc42 101 *
vcoubard 1131:692ddf04fc42 102 * @param[in] hrmCounter
vcoubard 1134:d540a48f650d 103 * Heart rate in BPM.
vcoubard 1131:692ddf04fc42 104 */
vcoubard 1131:692ddf04fc42 105 void updateHeartRate(uint16_t hrmCounter) {
vcoubard 1131:692ddf04fc42 106 valueBytes.updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 107 ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
vcoubard 1131:692ddf04fc42 108 }
vcoubard 1131:692ddf04fc42 109
vcoubard 1131:692ddf04fc42 110 /**
vcoubard 1134:d540a48f650d 111 * This callback allows the heart rate service to receive updates to the
vcoubard 1134:d540a48f650d 112 * controlPoint characteristic.
vcoubard 1131:692ddf04fc42 113 *
vcoubard 1131:692ddf04fc42 114 * @param[in] params
vcoubard 1131:692ddf04fc42 115 * Information about the characterisitc being updated.
vcoubard 1131:692ddf04fc42 116 */
vcoubard 1131:692ddf04fc42 117 virtual void onDataWritten(const GattWriteCallbackParams *params) {
vcoubard 1131:692ddf04fc42 118 if (params->handle == controlPoint.getValueAttribute().getHandle()) {
vcoubard 1131:692ddf04fc42 119 /* Do something here if the new value is 1; else you can override this method by
vcoubard 1131:692ddf04fc42 120 * extending this class.
vcoubard 1134:d540a48f650d 121 * @NOTE: If you are extending this class, be sure to also call
vcoubard 1131:692ddf04fc42 122 * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
vcoubard 1131:692ddf04fc42 123 * your constructor.
vcoubard 1131:692ddf04fc42 124 */
vcoubard 1131:692ddf04fc42 125 }
vcoubard 1131:692ddf04fc42 126 }
vcoubard 1131:692ddf04fc42 127
vcoubard 1131:692ddf04fc42 128 protected:
vcoubard 1131:692ddf04fc42 129 void setupService(void) {
vcoubard 1131:692ddf04fc42 130 GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint};
vcoubard 1131:692ddf04fc42 131 GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
vcoubard 1131:692ddf04fc42 132
vcoubard 1131:692ddf04fc42 133 ble.addService(hrmService);
vcoubard 1131:692ddf04fc42 134 ble.onDataWritten(this, &HeartRateService::onDataWritten);
vcoubard 1131:692ddf04fc42 135 }
vcoubard 1131:692ddf04fc42 136
vcoubard 1131:692ddf04fc42 137 protected:
vcoubard 1134:d540a48f650d 138 /* Private internal representation for the bytes used to work with the value of the heart rate characteristic. */
vcoubard 1131:692ddf04fc42 139 struct HeartRateValueBytes {
vcoubard 1134:d540a48f650d 140 static const unsigned MAX_VALUE_BYTES = 3; /* Flags, and up to two bytes for heart rate. */
vcoubard 1131:692ddf04fc42 141 static const unsigned FLAGS_BYTE_INDEX = 0;
vcoubard 1131:692ddf04fc42 142
vcoubard 1131:692ddf04fc42 143 static const unsigned VALUE_FORMAT_BITNUM = 0;
vcoubard 1131:692ddf04fc42 144 static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM);
vcoubard 1131:692ddf04fc42 145
vcoubard 1131:692ddf04fc42 146 HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() {
vcoubard 1131:692ddf04fc42 147 updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 148 }
vcoubard 1131:692ddf04fc42 149
vcoubard 1131:692ddf04fc42 150 HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() {
vcoubard 1131:692ddf04fc42 151 updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 152 }
vcoubard 1131:692ddf04fc42 153
vcoubard 1131:692ddf04fc42 154 void updateHeartRate(uint8_t hrmCounter) {
vcoubard 1131:692ddf04fc42 155 valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
vcoubard 1131:692ddf04fc42 156 valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
vcoubard 1131:692ddf04fc42 157 }
vcoubard 1131:692ddf04fc42 158
vcoubard 1131:692ddf04fc42 159 void updateHeartRate(uint16_t hrmCounter) {
vcoubard 1131:692ddf04fc42 160 valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
vcoubard 1131:692ddf04fc42 161 valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
vcoubard 1131:692ddf04fc42 162 valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
vcoubard 1131:692ddf04fc42 163 }
vcoubard 1131:692ddf04fc42 164
vcoubard 1131:692ddf04fc42 165 uint8_t *getPointer(void) {
vcoubard 1131:692ddf04fc42 166 return valueBytes;
vcoubard 1131:692ddf04fc42 167 }
vcoubard 1131:692ddf04fc42 168
vcoubard 1131:692ddf04fc42 169 const uint8_t *getPointer(void) const {
vcoubard 1131:692ddf04fc42 170 return valueBytes;
vcoubard 1131:692ddf04fc42 171 }
vcoubard 1131:692ddf04fc42 172
vcoubard 1131:692ddf04fc42 173 unsigned getNumValueBytes(void) const {
vcoubard 1131:692ddf04fc42 174 return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
vcoubard 1131:692ddf04fc42 175 }
vcoubard 1131:692ddf04fc42 176
vcoubard 1131:692ddf04fc42 177 private:
vcoubard 1134:d540a48f650d 178 /* First byte: 8-bit values, no extra info. Second byte: uint8_t HRM value */
vcoubard 1134:d540a48f650d 179 /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
vcoubard 1131:692ddf04fc42 180 uint8_t valueBytes[MAX_VALUE_BYTES];
vcoubard 1131:692ddf04fc42 181 };
vcoubard 1131:692ddf04fc42 182
vcoubard 1131:692ddf04fc42 183 protected:
vcoubard 1131:692ddf04fc42 184 BLE &ble;
vcoubard 1131:692ddf04fc42 185
vcoubard 1131:692ddf04fc42 186 HeartRateValueBytes valueBytes;
vcoubard 1131:692ddf04fc42 187 uint8_t controlPointValue;
vcoubard 1131:692ddf04fc42 188
vcoubard 1131:692ddf04fc42 189 GattCharacteristic hrmRate;
vcoubard 1131:692ddf04fc42 190 ReadOnlyGattCharacteristic<uint8_t> hrmLocation;
vcoubard 1131:692ddf04fc42 191 WriteOnlyGattCharacteristic<uint8_t> controlPoint;
vcoubard 1131:692ddf04fc42 192 };
vcoubard 1131:692ddf04fc42 193
rgrover1 712:b04b5db36865 194 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/