Mbed OS Reference
Loading...
Searching...
No Matches
HealthThermometerService.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 __BLE_HEALTH_THERMOMETER_SERVICE_H__
23#define __BLE_HEALTH_THERMOMETER_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* @class HealthThermometerService
33* @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature.
34* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml
35* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
36* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml
37*/
39public:
40 /**
41 * @brief Location of sensor on the body.
42 */
44 LOCATION_ARMPIT = 1, /*!< Armpit. */
45 LOCATION_BODY, /*!< Body. */
46 LOCATION_EAR, /*!< Ear. */
47 LOCATION_FINGER, /*!< Finger. */
48 LOCATION_GI_TRACT, /*!< GI tract */
49 LOCATION_MOUTH, /*!< Mouth. */
50 LOCATION_RECTUM, /*!< Rectum. */
51 LOCATION_TOE, /*!< Toe. */
52 LOCATION_EAR_DRUM, /*!< Eardrum. */
53 };
54
55public:
56 /**
57 * @brief Add the Health Thermometer Service to an existing BLE object, initialize with temperature and location.
58 * @param[in] _ble Reference to the BLE device.
59 * @param[in] initialTemp Initial value in celsius.
60 * @param[in] _location
61 */
62 HealthThermometerService(BLE &_ble, float initialTemp, uint8_t _location) :
63 ble(_ble),
64 valueBytes(initialTemp),
65 tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (TemperatureValueBytes *)valueBytes.getPointer(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE),
66 tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, &_location) {
67
68 GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, };
69 GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(hrmChars[0]));
70
71 ble.gattServer().addService(hrmService);
72 }
73
74 /**
75 * @brief Update the temperature being broadcast.
76 *
77 * @param[in] temperature
78 * Floating point value of the temperature.
79 *
80 */
81 void updateTemperature(float temperature) {
82 valueBytes.updateTemperature(temperature);
83 ble.gattServer().write(tempMeasurement.getValueHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes));
84 }
85
86 /**
87 * @brief Update the location.
88 * @param loc
89 * New location value.
90 */
92 ble.gattServer().write(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t));
93 }
94
95private:
96 /* Private internal representation for the bytes used to work with the vaulue of the temperature characteristic. */
97 struct TemperatureValueBytes {
98 static const unsigned OFFSET_OF_FLAGS = 0;
99 static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t);
100 static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float);
101
102 static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0;
103 static const unsigned TIMESTAMP_FLAG_POS = 1;
104 static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2;
105
106 static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
107 static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
108
109 TemperatureValueBytes(float initialTemperature) : bytes() {
110 /* Assumption: temperature values are expressed in celsius */
111 bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) |
112 (false << TIMESTAMP_FLAG_POS) |
113 (false << TEMPERATURE_TYPE_FLAG_POS);
114 updateTemperature(initialTemperature);
115 }
116
117 void updateTemperature(float temp) {
118 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp);
119 memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float));
120 }
121
122 uint8_t *getPointer() {
123 return bytes;
124 }
125
126 const uint8_t *getPointer() const {
127 return bytes;
128 }
129
130private:
131 /**
132 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
133 * @param temperature The temperature as a float.
134 * @return The temperature in 11073-20601 FLOAT-Type format.
135 */
136 static uint32_t quick_ieee11073_from_float(float temperature) {
137 uint8_t exponent = 0xFE; //Exponent is -2
138 uint32_t mantissa = (uint32_t)(temperature * 100);
139
140 return (((uint32_t)exponent) << 24) | mantissa;
141 }
142
143private:
144 /* First byte: 8-bit flags. Second field is a float holding the temperature value. */
145 /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
146 uint8_t bytes[SIZEOF_VALUE_BYTES];
147 };
148
149protected:
150 BLE &ble;
151 TemperatureValueBytes valueBytes;
154};
155
156#endif // BLE_FEATURE_GATT_SERVER
157
158#endif /* #ifndef __BLE_HEALTH_THERMOMETER_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_HEALTH_THERMOMETER_SERVICE
UUID of the health thermometer.
BLE Health Thermometer Service.
SensorLocation_t
Location of sensor on the body.
HealthThermometerService(BLE &_ble, float initialTemp, uint8_t _location)
Add the Health Thermometer Service to an existing BLE object, initialize with temperature and locatio...
void updateLocation(SensorLocation_t loc)
Update the location.
void updateTemperature(float temperature)
Update the temperature being broadcast.
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.