Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
ble/services/HeartRateService.h@716:11b41f651697, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 09:06:11 2015 +0100
- Revision:
- 716:11b41f651697
- Parent:
- 712:b04b5db36865
- Child:
- 722:eab9499e4250
Synchronized with git rev d80fec88
Author: Rohit Grover
Release 0.4.0
=============
This is a major release which introduces the GATT Client functionality. It
also aligns BLE_API with builds using our new package manager: yotta
(https://github.com/armmbed/yotta).
Many APIs have seen some redesign. We encourage our users to pay attention to
the changes and migrate appropriately over time. We've also taken care to
ensure that existing code continues to work the same way. There's more
documentation in the form of comment headers for APIs to explain proper usage;
in many cases comment headers suggest alternative use of APIs.
Enhancements
~~~~~~~~~~~~
* Introduce GattClient. This includes functionality for service-discovery,
connections, and attribute-reads and writes. You'll find a demo program for
LEDBlinker on the mbed.org Bluetooth team page to use the new APIs. Some of
the GATT client functionality hasn't been implemented yet, but the APIs have
been added.
* Most APIs in the abstract base classes like Gap and GattServer return
BLE_ERROR_NOT_IMPLEMENTED. Previously many APIs were pure-virtual, which did
not permit partial ports to compile.
* We've added a new abstract base class for SecurityManager. All security
related APIs have been moved into that.
* BLEDevice has been renamed as BLE. A deprecated alias for BLEDevice is
available to support existing code.
* There has been a major cleanup of APIs under BLE. APIs have now been
categorized as belonging to Gap, GattServer, GattClient, or SecurityManager.
There are accessors to get references for Gap, GattServer, GattClient, and
SecurityManager. A former call to ble.setAddress(...) is now expected to be
achieved with ble.gap().setAddress(...).
* We've cleaned up our APIs, and this has resulted in dropping some APIs like
BLE::reset().
* We've also dropped GattServer::initializeGattDatabase(). THis was added at
some point to support controllers where a commit point was needed to
indicate when the application had finished constructing the GATT database.
This API would get called internally before Gap::startAdvertising(). We now
expect the underlying port to do the equivalent of initializeGattDatabase()
implicitly upon Gap::startAdvertising().
* The callback for BLE.onTimeout() now receives a TimeoutSource_t to indicate
the cause of the timeout. This is perhaps the only breaking API change. We
expect it to have very little disruptive effect.
* We've added a version of Gap::disconnect() which takes a connection handle.
The previous API (which did not take a connection handle) has been
deprecated; it will still work for situations where there's only a single
active connection. We hold on to that API to allow existing code to migrate
to the new API.
Bugfixes
~~~~~~~~
* None.
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 | 712:b04b5db36865 | 24 | * @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor, the heartrate in beats per minute. <br> |
| rgrover1 | 712:b04b5db36865 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml <br> |
| rgrover1 | 712:b04b5db36865 | 26 | * HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml <br> |
| 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 | 712:b04b5db36865 | 33 | * @brief Location of HeartRate sensor on body. |
| rgrover1 | 712:b04b5db36865 | 34 | */ |
| rgrover1 | 712:b04b5db36865 | 35 | enum { |
| rgrover1 | 712:b04b5db36865 | 36 | LOCATION_OTHER = 0, /*!< Other Location */ |
| rgrover1 | 712:b04b5db36865 | 37 | LOCATION_CHEST, /*!< Chest */ |
| rgrover1 | 712:b04b5db36865 | 38 | LOCATION_WRIST, /*!< Wrist */ |
| rgrover1 | 712:b04b5db36865 | 39 | LOCATION_FINGER, /*!< Finger */ |
| rgrover1 | 712:b04b5db36865 | 40 | LOCATION_HAND, /*!< Hand */ |
| rgrover1 | 712:b04b5db36865 | 41 | LOCATION_EAR_LOBE, /*!< Earlobe */ |
| rgrover1 | 712:b04b5db36865 | 42 | LOCATION_FOOT, /*!< Foot */ |
| rgrover1 | 712:b04b5db36865 | 43 | }; |
| rgrover1 | 712:b04b5db36865 | 44 | |
| rgrover1 | 712:b04b5db36865 | 45 | public: |
| rgrover1 | 712:b04b5db36865 | 46 | /** |
| rgrover1 | 712:b04b5db36865 | 47 | * @brief Constructor with 8bit 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 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 89 | * @brief Set a new 8-bit value for heart rate. |
| rgrover1 | 712:b04b5db36865 | 90 | * |
| rgrover1 | 712:b04b5db36865 | 91 | * @param[in] hrmCounter |
| rgrover1 | 712:b04b5db36865 | 92 | * HeartRate in bpm. |
| rgrover1 | 712:b04b5db36865 | 93 | */ |
| rgrover1 | 712:b04b5db36865 | 94 | void updateHeartRate(uint8_t hrmCounter) { |
| rgrover1 | 712:b04b5db36865 | 95 | valueBytes.updateHeartRate(hrmCounter); |
| rgrover1 | 712:b04b5db36865 | 96 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
| rgrover1 | 712:b04b5db36865 | 97 | } |
| rgrover1 | 712:b04b5db36865 | 98 | |
| rgrover1 | 712:b04b5db36865 | 99 | /** |
| rgrover1 | 712:b04b5db36865 | 100 | * Set a new 16-bit value for heart rate. |
| rgrover1 | 712:b04b5db36865 | 101 | * |
| rgrover1 | 712:b04b5db36865 | 102 | * @param[in] hrmCounter |
| rgrover1 | 712:b04b5db36865 | 103 | * HeartRate in bpm. |
| rgrover1 | 712:b04b5db36865 | 104 | */ |
| rgrover1 | 712:b04b5db36865 | 105 | void updateHeartRate(uint16_t hrmCounter) { |
| rgrover1 | 712:b04b5db36865 | 106 | valueBytes.updateHeartRate(hrmCounter); |
| rgrover1 | 712:b04b5db36865 | 107 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
| rgrover1 | 712:b04b5db36865 | 108 | } |
| rgrover1 | 712:b04b5db36865 | 109 | |
| rgrover1 | 712:b04b5db36865 | 110 | /** |
| rgrover1 | 712:b04b5db36865 | 111 | * This callback allows the HeartRateService to receive updates to the |
| rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 138 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
| rgrover1 | 712:b04b5db36865 | 139 | struct HeartRateValueBytes { |
| rgrover1 | 712:b04b5db36865 | 140 | static const unsigned MAX_VALUE_BYTES = 3; /* FLAGS + 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 | 712:b04b5db36865 | 178 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
| rgrover1 | 712:b04b5db36865 | 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__*/ |
