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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.