prova

Fork of BLE_API by Bluetooth Low Energy

Committer:
andreasortino
Date:
Thu Sep 28 13:22:57 2017 +0000
Revision:
1209:b8e423d6b91b
Parent:
1134:d540a48f650d
ertrfgnbc

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.
andreasortino 1209:b8e423d6b91b 55 ************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 56 * ORIGINALE
andreasortino 1209:b8e423d6b91b 57 ***********************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 58
vcoubard 1131:692ddf04fc42 59 */
vcoubard 1131:692ddf04fc42 60 HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) :
vcoubard 1131:692ddf04fc42 61 ble(_ble),
vcoubard 1131:692ddf04fc42 62 valueBytes(hrmCounter),
andreasortino 1209:b8e423d6b91b 63 hrmRate(GattCharacteristic:: UUID_HEART_RATE_MEASUREMENT_CHAR , valueBytes.getPointer(),
vcoubard 1131:692ddf04fc42 64 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
vcoubard 1131:692ddf04fc42 65 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
vcoubard 1131:692ddf04fc42 66 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
vcoubard 1131:692ddf04fc42 67 controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
vcoubard 1131:692ddf04fc42 68 setupService();
vcoubard 1131:692ddf04fc42 69 }
andreasortino 1209:b8e423d6b91b 70 /*
andreasortino 1209:b8e423d6b91b 71 ***************************************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 72 mODIFICATO
vcoubard 1131:692ddf04fc42 73
andreasortino 1209:b8e423d6b91b 74 ***************************************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 75 8-bit
andreasortino 1209:b8e423d6b91b 76 */ /*
andreasortino 1209:b8e423d6b91b 77 HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) :
andreasortino 1209:b8e423d6b91b 78 ble(_ble),
andreasortino 1209:b8e423d6b91b 79 valueBytes(hrmCounter),
andreasortino 1209:b8e423d6b91b 80 hrmRate(GattCharacteristic:: UUID_TEMPERATURE_MEASUREMENT_CHAR , valueBytes.getPointer(),
andreasortino 1209:b8e423d6b91b 81 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
andreasortino 1209:b8e423d6b91b 82 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
andreasortino 1209:b8e423d6b91b 83 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
andreasortino 1209:b8e423d6b91b 84 controlPoint(GattCharacteristic::UUID_TEMPERATURE_CHAR , &controlPointValue) {
andreasortino 1209:b8e423d6b91b 85 setupService();
andreasortino 1209:b8e423d6b91b 86
andreasortino 1209:b8e423d6b91b 87 }
andreasortino 1209:b8e423d6b91b 88 // 16-bit
andreasortino 1209:b8e423d6b91b 89
andreasortino 1209:b8e423d6b91b 90
andreasortino 1209:b8e423d6b91b 91 HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) :
andreasortino 1209:b8e423d6b91b 92 ble(_ble),
andreasortino 1209:b8e423d6b91b 93 valueBytes(hrmCounter),
andreasortino 1209:b8e423d6b91b 94 hrmRate(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, valueBytes.getPointer(),
andreasortino 1209:b8e423d6b91b 95 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
andreasortino 1209:b8e423d6b91b 96 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
andreasortino 1209:b8e423d6b91b 97 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
andreasortino 1209:b8e423d6b91b 98 controlPoint(GattCharacteristic::UUID_TEMPERATURE_CHAR, &controlPointValue) {
andreasortino 1209:b8e423d6b91b 99 setupService();
andreasortino 1209:b8e423d6b91b 100 }*/
vcoubard 1131:692ddf04fc42 101 /**
vcoubard 1131:692ddf04fc42 102 * @brief Constructor with a 16-bit HRM Counter value.
vcoubard 1131:692ddf04fc42 103 *
vcoubard 1131:692ddf04fc42 104 * @param[in] _ble
vcoubard 1131:692ddf04fc42 105 * Reference to the underlying BLE.
vcoubard 1131:692ddf04fc42 106 * @param[in] hrmCounter (8-bit)
vcoubard 1134:d540a48f650d 107 * Initial value for the HRM counter.
vcoubard 1131:692ddf04fc42 108 * @param[in] location
vcoubard 1131:692ddf04fc42 109 * Sensor's location.
andreasortino 1209:b8e423d6b91b 110 ***************************************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 111 ***************************************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 112 ***************************************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 113 SOTTO IL 16 BIT DISABILITATO
andreasortino 1209:b8e423d6b91b 114
andreasortino 1209:b8e423d6b91b 115 ***************************************************************************************************************************************************************************
andreasortino 1209:b8e423d6b91b 116 ***************************************************************************************************************************************************************************
vcoubard 1131:692ddf04fc42 117 */
andreasortino 1209:b8e423d6b91b 118
andreasortino 1209:b8e423d6b91b 119
andreasortino 1209:b8e423d6b91b 120 HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) :
vcoubard 1131:692ddf04fc42 121 ble(_ble),
vcoubard 1131:692ddf04fc42 122 valueBytes(hrmCounter),
vcoubard 1131:692ddf04fc42 123 hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
vcoubard 1131:692ddf04fc42 124 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
vcoubard 1131:692ddf04fc42 125 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
vcoubard 1131:692ddf04fc42 126 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
vcoubard 1131:692ddf04fc42 127 controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
vcoubard 1131:692ddf04fc42 128 setupService();
vcoubard 1131:692ddf04fc42 129 }
vcoubard 1131:692ddf04fc42 130
vcoubard 1131:692ddf04fc42 131 /**
vcoubard 1134:d540a48f650d 132 * @brief Set a new 8-bit value for the heart rate.
vcoubard 1131:692ddf04fc42 133 *
vcoubard 1131:692ddf04fc42 134 * @param[in] hrmCounter
vcoubard 1134:d540a48f650d 135 * Heart rate in BPM.
vcoubard 1131:692ddf04fc42 136 */
vcoubard 1131:692ddf04fc42 137 void updateHeartRate(uint8_t hrmCounter) {
vcoubard 1131:692ddf04fc42 138 valueBytes.updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 139 ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
vcoubard 1131:692ddf04fc42 140 }
vcoubard 1131:692ddf04fc42 141
vcoubard 1131:692ddf04fc42 142 /**
vcoubard 1134:d540a48f650d 143 * Set a new 16-bit value for the heart rate.
vcoubard 1131:692ddf04fc42 144 *
vcoubard 1131:692ddf04fc42 145 * @param[in] hrmCounter
vcoubard 1134:d540a48f650d 146 * Heart rate in BPM.
vcoubard 1131:692ddf04fc42 147 */
vcoubard 1131:692ddf04fc42 148 void updateHeartRate(uint16_t hrmCounter) {
vcoubard 1131:692ddf04fc42 149 valueBytes.updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 150 ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
vcoubard 1131:692ddf04fc42 151 }
vcoubard 1131:692ddf04fc42 152
vcoubard 1131:692ddf04fc42 153 /**
vcoubard 1134:d540a48f650d 154 * This callback allows the heart rate service to receive updates to the
vcoubard 1134:d540a48f650d 155 * controlPoint characteristic.
vcoubard 1131:692ddf04fc42 156 *
vcoubard 1131:692ddf04fc42 157 * @param[in] params
vcoubard 1131:692ddf04fc42 158 * Information about the characterisitc being updated.
vcoubard 1131:692ddf04fc42 159 */
vcoubard 1131:692ddf04fc42 160 virtual void onDataWritten(const GattWriteCallbackParams *params) {
vcoubard 1131:692ddf04fc42 161 if (params->handle == controlPoint.getValueAttribute().getHandle()) {
vcoubard 1131:692ddf04fc42 162 /* Do something here if the new value is 1; else you can override this method by
vcoubard 1131:692ddf04fc42 163 * extending this class.
vcoubard 1134:d540a48f650d 164 * @NOTE: If you are extending this class, be sure to also call
vcoubard 1131:692ddf04fc42 165 * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
vcoubard 1131:692ddf04fc42 166 * your constructor.
vcoubard 1131:692ddf04fc42 167 */
vcoubard 1131:692ddf04fc42 168 }
vcoubard 1131:692ddf04fc42 169 }
vcoubard 1131:692ddf04fc42 170
vcoubard 1131:692ddf04fc42 171 protected:
vcoubard 1131:692ddf04fc42 172 void setupService(void) {
vcoubard 1131:692ddf04fc42 173 GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint};
andreasortino 1209:b8e423d6b91b 174 //GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE , charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); //Modificato
andreasortino 1209:b8e423d6b91b 175
andreasortino 1209:b8e423d6b91b 176 GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); //ORIGILAE
vcoubard 1131:692ddf04fc42 177
vcoubard 1131:692ddf04fc42 178 ble.addService(hrmService);
vcoubard 1131:692ddf04fc42 179 ble.onDataWritten(this, &HeartRateService::onDataWritten);
vcoubard 1131:692ddf04fc42 180 }
vcoubard 1131:692ddf04fc42 181
vcoubard 1131:692ddf04fc42 182 protected:
vcoubard 1134:d540a48f650d 183 /* Private internal representation for the bytes used to work with the value of the heart rate characteristic. */
vcoubard 1131:692ddf04fc42 184 struct HeartRateValueBytes {
vcoubard 1134:d540a48f650d 185 static const unsigned MAX_VALUE_BYTES = 3; /* Flags, and up to two bytes for heart rate. */
vcoubard 1131:692ddf04fc42 186 static const unsigned FLAGS_BYTE_INDEX = 0;
vcoubard 1131:692ddf04fc42 187
vcoubard 1131:692ddf04fc42 188 static const unsigned VALUE_FORMAT_BITNUM = 0;
vcoubard 1131:692ddf04fc42 189 static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM);
vcoubard 1131:692ddf04fc42 190
vcoubard 1131:692ddf04fc42 191 HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() {
vcoubard 1131:692ddf04fc42 192 updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 193 }
vcoubard 1131:692ddf04fc42 194
vcoubard 1131:692ddf04fc42 195 HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() {
vcoubard 1131:692ddf04fc42 196 updateHeartRate(hrmCounter);
vcoubard 1131:692ddf04fc42 197 }
vcoubard 1131:692ddf04fc42 198
vcoubard 1131:692ddf04fc42 199 void updateHeartRate(uint8_t hrmCounter) {
vcoubard 1131:692ddf04fc42 200 valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
vcoubard 1131:692ddf04fc42 201 valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
vcoubard 1131:692ddf04fc42 202 }
vcoubard 1131:692ddf04fc42 203
vcoubard 1131:692ddf04fc42 204 void updateHeartRate(uint16_t hrmCounter) {
vcoubard 1131:692ddf04fc42 205 valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
vcoubard 1131:692ddf04fc42 206 valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
vcoubard 1131:692ddf04fc42 207 valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
vcoubard 1131:692ddf04fc42 208 }
vcoubard 1131:692ddf04fc42 209
vcoubard 1131:692ddf04fc42 210 uint8_t *getPointer(void) {
vcoubard 1131:692ddf04fc42 211 return valueBytes;
vcoubard 1131:692ddf04fc42 212 }
vcoubard 1131:692ddf04fc42 213
vcoubard 1131:692ddf04fc42 214 const uint8_t *getPointer(void) const {
vcoubard 1131:692ddf04fc42 215 return valueBytes;
vcoubard 1131:692ddf04fc42 216 }
vcoubard 1131:692ddf04fc42 217
vcoubard 1131:692ddf04fc42 218 unsigned getNumValueBytes(void) const {
vcoubard 1131:692ddf04fc42 219 return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
vcoubard 1131:692ddf04fc42 220 }
vcoubard 1131:692ddf04fc42 221
vcoubard 1131:692ddf04fc42 222 private:
vcoubard 1134:d540a48f650d 223 /* First byte: 8-bit values, no extra info. Second byte: uint8_t HRM value */
vcoubard 1134:d540a48f650d 224 /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
vcoubard 1131:692ddf04fc42 225 uint8_t valueBytes[MAX_VALUE_BYTES];
vcoubard 1131:692ddf04fc42 226 };
vcoubard 1131:692ddf04fc42 227
vcoubard 1131:692ddf04fc42 228 protected:
vcoubard 1131:692ddf04fc42 229 BLE &ble;
vcoubard 1131:692ddf04fc42 230
vcoubard 1131:692ddf04fc42 231 HeartRateValueBytes valueBytes;
vcoubard 1131:692ddf04fc42 232 uint8_t controlPointValue;
vcoubard 1131:692ddf04fc42 233
vcoubard 1131:692ddf04fc42 234 GattCharacteristic hrmRate;
vcoubard 1131:692ddf04fc42 235 ReadOnlyGattCharacteristic<uint8_t> hrmLocation;
vcoubard 1131:692ddf04fc42 236 WriteOnlyGattCharacteristic<uint8_t> controlPoint;
vcoubard 1131:692ddf04fc42 237 };
vcoubard 1131:692ddf04fc42 238
rgrover1 712:b04b5db36865 239 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/