First programBLE
Fork of Nucleo_BLE_API by
services/HeartRateService.h@1:79e5c08cbcc7, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 19:52:49 2014 +0000
- Revision:
- 1:79e5c08cbcc7
- Parent:
- 0:289fd2dae405
change the USARTService->write() method access permission to public
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:289fd2dae405 | 1 | /* mbed Microcontroller Library |
sjallouli | 0:289fd2dae405 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:289fd2dae405 | 3 | * |
sjallouli | 0:289fd2dae405 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:289fd2dae405 | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:289fd2dae405 | 6 | * You may obtain a copy of the License at |
sjallouli | 0:289fd2dae405 | 7 | * |
sjallouli | 0:289fd2dae405 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:289fd2dae405 | 9 | * |
sjallouli | 0:289fd2dae405 | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:289fd2dae405 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:289fd2dae405 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:289fd2dae405 | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:289fd2dae405 | 14 | * limitations under the License. |
sjallouli | 0:289fd2dae405 | 15 | */ |
sjallouli | 0:289fd2dae405 | 16 | |
sjallouli | 0:289fd2dae405 | 17 | #ifndef __BLE_HEART_RATE_SERVICE_H__ |
sjallouli | 0:289fd2dae405 | 18 | #define __BLE_HEART_RATE_SERVICE_H__ |
sjallouli | 0:289fd2dae405 | 19 | |
sjallouli | 0:289fd2dae405 | 20 | #include "BLEDevice.h" |
sjallouli | 0:289fd2dae405 | 21 | |
sjallouli | 0:289fd2dae405 | 22 | /* Heart Rate Service */ |
sjallouli | 0:289fd2dae405 | 23 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
sjallouli | 0:289fd2dae405 | 24 | /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
sjallouli | 0:289fd2dae405 | 25 | /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ |
sjallouli | 0:289fd2dae405 | 26 | class HeartRateService { |
sjallouli | 0:289fd2dae405 | 27 | public: |
sjallouli | 0:289fd2dae405 | 28 | enum { |
sjallouli | 0:289fd2dae405 | 29 | LOCATION_OTHER = 0, |
sjallouli | 0:289fd2dae405 | 30 | LOCATION_CHEST, |
sjallouli | 0:289fd2dae405 | 31 | LOCATION_WRIST, |
sjallouli | 0:289fd2dae405 | 32 | LOCATION_FINGER, |
sjallouli | 0:289fd2dae405 | 33 | LOCATION_HAND, |
sjallouli | 0:289fd2dae405 | 34 | LOCATION_EAR_LOBE, |
sjallouli | 0:289fd2dae405 | 35 | LOCATION_FOOT, |
sjallouli | 0:289fd2dae405 | 36 | }; |
sjallouli | 0:289fd2dae405 | 37 | |
sjallouli | 0:289fd2dae405 | 38 | public: |
sjallouli | 0:289fd2dae405 | 39 | /** |
sjallouli | 0:289fd2dae405 | 40 | * Constructor. |
sjallouli | 0:289fd2dae405 | 41 | * |
sjallouli | 0:289fd2dae405 | 42 | * param[in] _ble |
sjallouli | 0:289fd2dae405 | 43 | * Reference to the underlying BLEDevice. |
sjallouli | 0:289fd2dae405 | 44 | * param[in] hrmCounter (8-bit) |
sjallouli | 0:289fd2dae405 | 45 | * initial value for the hrm counter. |
sjallouli | 0:289fd2dae405 | 46 | * param[in] location |
sjallouli | 0:289fd2dae405 | 47 | * Sensor's location. |
sjallouli | 0:289fd2dae405 | 48 | */ |
sjallouli | 0:289fd2dae405 | 49 | HeartRateService(BLEDevice &_ble, uint8_t hrmCounter, uint8_t location) : |
sjallouli | 0:289fd2dae405 | 50 | ble(_ble), |
sjallouli | 0:289fd2dae405 | 51 | valueBytes(hrmCounter), |
sjallouli | 0:289fd2dae405 | 52 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
sjallouli | 0:289fd2dae405 | 53 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
sjallouli | 0:289fd2dae405 | 54 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
sjallouli | 0:289fd2dae405 | 55 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location), |
sjallouli | 0:289fd2dae405 | 56 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
sjallouli | 0:289fd2dae405 | 57 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue, |
sjallouli | 0:289fd2dae405 | 58 | sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) { |
sjallouli | 0:289fd2dae405 | 59 | setupService(); |
sjallouli | 0:289fd2dae405 | 60 | } |
sjallouli | 0:289fd2dae405 | 61 | |
sjallouli | 0:289fd2dae405 | 62 | /** |
sjallouli | 0:289fd2dae405 | 63 | * Same constructor as above, but with a 16-bit HRM Counter value. |
sjallouli | 0:289fd2dae405 | 64 | */ |
sjallouli | 0:289fd2dae405 | 65 | HeartRateService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) : |
sjallouli | 0:289fd2dae405 | 66 | ble(_ble), |
sjallouli | 0:289fd2dae405 | 67 | valueBytes(hrmCounter), |
sjallouli | 0:289fd2dae405 | 68 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
sjallouli | 0:289fd2dae405 | 69 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
sjallouli | 0:289fd2dae405 | 70 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
sjallouli | 0:289fd2dae405 | 71 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location), |
sjallouli | 0:289fd2dae405 | 72 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
sjallouli | 0:289fd2dae405 | 73 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, (uint8_t *)&controlPointValue, |
sjallouli | 0:289fd2dae405 | 74 | sizeof(controlPointValue), sizeof(controlPointValue), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) { |
sjallouli | 0:289fd2dae405 | 75 | setupService(); |
sjallouli | 0:289fd2dae405 | 76 | } |
sjallouli | 0:289fd2dae405 | 77 | |
sjallouli | 0:289fd2dae405 | 78 | /** |
sjallouli | 0:289fd2dae405 | 79 | * Set a new 8-bit value for heart rate. |
sjallouli | 0:289fd2dae405 | 80 | */ |
sjallouli | 0:289fd2dae405 | 81 | void updateHeartRate(uint8_t hrmCounter) { |
sjallouli | 0:289fd2dae405 | 82 | valueBytes.updateHeartRate(hrmCounter); |
sjallouli | 0:289fd2dae405 | 83 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
sjallouli | 0:289fd2dae405 | 84 | } |
sjallouli | 0:289fd2dae405 | 85 | |
sjallouli | 0:289fd2dae405 | 86 | /** |
sjallouli | 0:289fd2dae405 | 87 | * Set a new 16-bit value for heart rate. |
sjallouli | 0:289fd2dae405 | 88 | */ |
sjallouli | 0:289fd2dae405 | 89 | void updateHeartRate(uint16_t hrmCounter) { |
sjallouli | 0:289fd2dae405 | 90 | valueBytes.updateHeartRate(hrmCounter); |
sjallouli | 0:289fd2dae405 | 91 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
sjallouli | 0:289fd2dae405 | 92 | } |
sjallouli | 0:289fd2dae405 | 93 | |
sjallouli | 0:289fd2dae405 | 94 | /** |
sjallouli | 0:289fd2dae405 | 95 | * This callback allows the UART service to receive updates to the |
sjallouli | 0:289fd2dae405 | 96 | * txCharacteristic. The application should forward the call to this |
sjallouli | 0:289fd2dae405 | 97 | * function from the global onDataWritten() callback handler; or if that's |
sjallouli | 0:289fd2dae405 | 98 | * not used, this method can be used as a callback directly. |
sjallouli | 0:289fd2dae405 | 99 | */ |
sjallouli | 0:289fd2dae405 | 100 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
sjallouli | 0:289fd2dae405 | 101 | if (params->charHandle == controlPoint.getValueAttribute().getHandle()) { |
sjallouli | 0:289fd2dae405 | 102 | /* Do something here if the new value is 1; else you can override this method by |
sjallouli | 0:289fd2dae405 | 103 | * extending this class. |
sjallouli | 0:289fd2dae405 | 104 | * @NOTE: if you are extending this class, be sure to also call |
sjallouli | 0:289fd2dae405 | 105 | * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in |
sjallouli | 0:289fd2dae405 | 106 | * your constructor. |
sjallouli | 0:289fd2dae405 | 107 | */ |
sjallouli | 0:289fd2dae405 | 108 | } |
sjallouli | 0:289fd2dae405 | 109 | } |
sjallouli | 0:289fd2dae405 | 110 | |
sjallouli | 0:289fd2dae405 | 111 | private: |
sjallouli | 0:289fd2dae405 | 112 | void setupService(void) { |
sjallouli | 0:289fd2dae405 | 113 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
sjallouli | 0:289fd2dae405 | 114 | if (serviceAdded) { |
sjallouli | 0:289fd2dae405 | 115 | return; |
sjallouli | 0:289fd2dae405 | 116 | } |
sjallouli | 0:289fd2dae405 | 117 | |
sjallouli | 0:289fd2dae405 | 118 | GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint}; |
sjallouli | 0:289fd2dae405 | 119 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
sjallouli | 0:289fd2dae405 | 120 | |
sjallouli | 0:289fd2dae405 | 121 | ble.addService(hrmService); |
sjallouli | 0:289fd2dae405 | 122 | serviceAdded = true; |
sjallouli | 0:289fd2dae405 | 123 | |
sjallouli | 0:289fd2dae405 | 124 | ble.onDataWritten(this, &HeartRateService::onDataWritten); |
sjallouli | 0:289fd2dae405 | 125 | } |
sjallouli | 0:289fd2dae405 | 126 | |
sjallouli | 0:289fd2dae405 | 127 | private: |
sjallouli | 0:289fd2dae405 | 128 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
sjallouli | 0:289fd2dae405 | 129 | struct HeartRateValueBytes { |
sjallouli | 0:289fd2dae405 | 130 | static const unsigned MAX_VALUE_BYTES = 3; /* FLAGS + up to two bytes for heart-rate */ |
sjallouli | 0:289fd2dae405 | 131 | static const unsigned FLAGS_BYTE_INDEX = 0; |
sjallouli | 0:289fd2dae405 | 132 | |
sjallouli | 0:289fd2dae405 | 133 | static const unsigned VALUE_FORMAT_BITNUM = 0; |
sjallouli | 0:289fd2dae405 | 134 | static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); |
sjallouli | 0:289fd2dae405 | 135 | |
sjallouli | 0:289fd2dae405 | 136 | HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() { |
sjallouli | 0:289fd2dae405 | 137 | updateHeartRate(hrmCounter); |
sjallouli | 0:289fd2dae405 | 138 | } |
sjallouli | 0:289fd2dae405 | 139 | |
sjallouli | 0:289fd2dae405 | 140 | HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() { |
sjallouli | 0:289fd2dae405 | 141 | updateHeartRate(hrmCounter); |
sjallouli | 0:289fd2dae405 | 142 | } |
sjallouli | 0:289fd2dae405 | 143 | |
sjallouli | 0:289fd2dae405 | 144 | void updateHeartRate(uint8_t hrmCounter) { |
sjallouli | 0:289fd2dae405 | 145 | valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; |
sjallouli | 0:289fd2dae405 | 146 | valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; |
sjallouli | 0:289fd2dae405 | 147 | } |
sjallouli | 0:289fd2dae405 | 148 | |
sjallouli | 0:289fd2dae405 | 149 | void updateHeartRate(uint16_t hrmCounter) { |
sjallouli | 0:289fd2dae405 | 150 | valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; |
sjallouli | 0:289fd2dae405 | 151 | valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); |
sjallouli | 0:289fd2dae405 | 152 | valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); |
sjallouli | 0:289fd2dae405 | 153 | } |
sjallouli | 0:289fd2dae405 | 154 | |
sjallouli | 0:289fd2dae405 | 155 | uint8_t *getPointer(void) { |
sjallouli | 0:289fd2dae405 | 156 | return valueBytes; |
sjallouli | 0:289fd2dae405 | 157 | } |
sjallouli | 0:289fd2dae405 | 158 | |
sjallouli | 0:289fd2dae405 | 159 | const uint8_t *getPointer(void) const { |
sjallouli | 0:289fd2dae405 | 160 | return valueBytes; |
sjallouli | 0:289fd2dae405 | 161 | } |
sjallouli | 0:289fd2dae405 | 162 | |
sjallouli | 0:289fd2dae405 | 163 | unsigned getNumValueBytes(void) const { |
sjallouli | 0:289fd2dae405 | 164 | return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t)); |
sjallouli | 0:289fd2dae405 | 165 | } |
sjallouli | 0:289fd2dae405 | 166 | |
sjallouli | 0:289fd2dae405 | 167 | private: |
sjallouli | 0:289fd2dae405 | 168 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
sjallouli | 0:289fd2dae405 | 169 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
sjallouli | 0:289fd2dae405 | 170 | uint8_t valueBytes[MAX_VALUE_BYTES]; |
sjallouli | 0:289fd2dae405 | 171 | }; |
sjallouli | 0:289fd2dae405 | 172 | |
sjallouli | 0:289fd2dae405 | 173 | private: |
sjallouli | 0:289fd2dae405 | 174 | BLEDevice &ble; |
sjallouli | 0:289fd2dae405 | 175 | HeartRateValueBytes valueBytes; |
sjallouli | 0:289fd2dae405 | 176 | uint8_t controlPointValue; |
sjallouli | 0:289fd2dae405 | 177 | GattCharacteristic hrmRate; |
sjallouli | 0:289fd2dae405 | 178 | GattCharacteristic hrmLocation; |
sjallouli | 0:289fd2dae405 | 179 | GattCharacteristic controlPoint; |
sjallouli | 0:289fd2dae405 | 180 | }; |
sjallouli | 0:289fd2dae405 | 181 | |
sjallouli | 0:289fd2dae405 | 182 | #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/ |