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