Mbed OS Reference
Loading...
Searching...
No Matches
HeartRateService.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2020 ARM Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/* MBED_DEPRECATED */
20#warning "These services are deprecated and will be removed. Please see services.md for details about replacement services."
21
22#ifndef MBED_BLE_HEART_RATE_SERVICE_H__
23#define MBED_BLE_HEART_RATE_SERVICE_H__
24
25#include "ble/BLE.h"
26#include "ble/Gap.h"
27#include "ble/GattServer.h"
28
29#if BLE_FEATURE_GATT_SERVER
30
31/**
32 * BLE Heart Rate Service.
33 *
34 * @par purpose
35 *
36 * Fitness applications use the heart rate service to expose the heart
37 * beat per minute measured by a heart rate sensor.
38 *
39 * Clients can read the intended location of the sensor and the last heart rate
40 * value measured. Additionally, clients can subscribe to server initiated
41 * updates of the heart rate value measured by the sensor. The service delivers
42 * these updates to the subscribed client in a notification packet.
43 *
44 * The subscription mechanism is useful to save power; it avoids unecessary data
45 * traffic between the client and the server, which may be induced by polling the
46 * value of the heart rate measurement characteristic.
47 *
48 * @par usage
49 *
50 * When this class is instantiated, it adds a heart rate service in the GattServer.
51 * The service contains the location of the sensor and the initial value measured
52 * by the sensor.
53 *
54 * Application code can invoke updateHeartRate() when a new heart rate measurement
55 * is acquired; this function updates the value of the heart rate measurement
56 * characteristic and notifies the new value to subscribed clients.
57 *
58 * @note You can find specification of the heart rate service here:
59 * https://www.bluetooth.com/specifications/gatt
60 *
61 * @attention The service does not expose information related to the sensor
62 * contact, the accumulated energy expanded or the interbeat intervals.
63 *
64 * @attention The heart rate profile limits the number of instantiations of the
65 * heart rate services to one.
66 */
68public:
69 /**
70 * Intended location of the heart rate sensor.
71 */
73 /**
74 * Other location.
75 */
77
78 /**
79 * Chest.
80 */
82
83 /**
84 * Wrist.
85 */
87
88 /**
89 * Finger.
90 */
92
93 /**
94 * Hand.
95 */
97
98 /**
99 * Earlobe.
100 */
102
103 /**
104 * Foot.
105 */
107 };
108
109public:
110 /**
111 * Construct and initialize a heart rate service.
112 *
113 * The construction process adds a GATT heart rate service in @p _ble
114 * GattServer, sets the value of the heart rate measurement characteristic
115 * to @p hrmCounter and the value of the body sensor location characteristic
116 * to @p location.
117 *
118 * @param[in] _ble BLE device that hosts the heart rate service.
119 * @param[in] hrmCounter Heart beats per minute measured by the heart rate
120 * sensor.
121 * @param[in] location Intended location of the heart rate sensor.
122 */
123 HeartRateService(BLE &_ble, uint16_t hrmCounter, BodySensorLocation location) :
124 ble(_ble),
125 valueBytes(hrmCounter),
126 hrmRate(
127 GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR,
128 valueBytes.getPointer(),
129 valueBytes.getNumValueBytes(),
130 HeartRateValueBytes::MAX_VALUE_BYTES,
131 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
132 ),
133 hrmLocation(
134 GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR,
135 reinterpret_cast<uint8_t*>(&location)
136 )
137 {
138 setupService();
139 }
140
141 /**
142 * Update the heart rate that the service exposes.
143 *
144 * The server sends a notification of the new value to clients that have
145 * subscribed to updates of the heart rate measurement characteristic; clients
146 * reading the heart rate measurement characteristic after the update obtain
147 * the updated value.
148 *
149 * @param[in] hrmCounter Heart rate measured in BPM.
150 *
151 * @attention This function must be called in the execution context of the
152 * BLE stack.
153 */
154 void updateHeartRate(uint16_t hrmCounter) {
155 valueBytes.updateHeartRate(hrmCounter);
156 ble.gattServer().write(
157 hrmRate.getValueHandle(),
158 valueBytes.getPointer(),
159 valueBytes.getNumValueBytes()
160 );
161 }
162
163protected:
164 /**
165 * Construct and add to the GattServer the heart rate service.
166 */
168 GattCharacteristic *charTable[] = {
169 &hrmRate,
170 &hrmLocation
171 };
172 GattService hrmService(
174 charTable,
175 sizeof(charTable) / sizeof(charTable[0])
176 );
177
178 ble.gattServer().addService(hrmService);
179 }
180
181protected:
182 /*
183 * Heart rate measurement value.
184 */
186 /* 1 byte for the Flags, and up to two bytes for heart rate value. */
187 static const unsigned MAX_VALUE_BYTES = 3;
188 static const unsigned FLAGS_BYTE_INDEX = 0;
189
190 static const unsigned VALUE_FORMAT_BITNUM = 0;
191 static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM);
192
193 HeartRateValueBytes(uint16_t hrmCounter) : valueBytes()
194 {
195 updateHeartRate(hrmCounter);
196 }
197
198 void updateHeartRate(uint16_t hrmCounter)
199 {
200 if (hrmCounter <= 255) {
201 valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
202 valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
203 } else {
204 valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
205 valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
206 valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
207 }
208 }
209
210 uint8_t *getPointer()
211 {
212 return valueBytes;
213 }
214
215 const uint8_t *getPointer() const
216 {
217 return valueBytes;
218 }
219
220 unsigned getNumValueBytes() const
221 {
222 if (valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) {
223 return 1 + sizeof(uint16_t);
224 } else {
225 return 1 + sizeof(uint8_t);
226 }
227 }
228
229 private:
230 uint8_t valueBytes[MAX_VALUE_BYTES];
231 };
232
233protected:
234 BLE &ble;
235 HeartRateValueBytes valueBytes;
236 GattCharacteristic hrmRate;
238};
239
240#endif // BLE_FEATURE_GATT_SERVER
241
242#endif /* #ifndef MBED_BLE_HEART_RATE_SERVICE_H__*/
Representation of a GattServer characteristic.
GattAttribute::Handle_t getValueHandle() const
Get the characteristic's value attribute handle in the ATT table.
Representation of a GattServer service.
@ UUID_HEART_RATE_SERVICE
UUID of the Heart Rate service.
BLE Heart Rate Service.
HeartRateService(BLE &_ble, uint16_t hrmCounter, BodySensorLocation location)
Construct and initialize a heart rate service.
BodySensorLocation
Intended location of the heart rate sensor.
@ LOCATION_FINGER
Finger.
@ LOCATION_EAR_LOBE
Earlobe.
@ LOCATION_OTHER
Other location.
void updateHeartRate(uint16_t hrmCounter)
Update the heart rate that the service exposes.
void setupService()
Construct and add to the GattServer the heart rate service.
Helper class that represents a read only GattCharacteristic.
Abstract away BLE-capable radio transceivers or SOCs.
Definition: BLE.h:137
Entry namespace for all BLE API definitions.