test

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_SecureHeartRate by Bluetooth Low Energy

Committer:
rgrover1
Date:
Thu Jun 04 08:02:36 2015 +0000
Revision:
3:b852bf8d6fad
Child:
5:ed2cb43f3589
minor mods based on Xiao's original work.

Who changed what in which revision?

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