Mistake on this page?
Report an issue in GitHub or email us
HealthThermometerService.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__
18 #define __BLE_HEALTH_THERMOMETER_SERVICE_H__
19 
20 #include "ble/BLE.h"
21 
22 #if BLE_FEATURE_GATT_SERVER
23 
24 /**
25 * @class HealthThermometerService
26 * @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature.
27 * Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml
28 * Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
29 * Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml
30 */
32 public:
33  /**
34  * @brief Location of sensor on the body.
35  */
37  LOCATION_ARMPIT = 1, /*!< Armpit. */
38  LOCATION_BODY, /*!< Body. */
39  LOCATION_EAR, /*!< Ear. */
40  LOCATION_FINGER, /*!< Finger. */
41  LOCATION_GI_TRACT, /*!< GI tract */
42  LOCATION_MOUTH, /*!< Mouth. */
43  LOCATION_RECTUM, /*!< Rectum. */
44  LOCATION_TOE, /*!< Toe. */
45  LOCATION_EAR_DRUM, /*!< Eardrum. */
46  };
47 
48 public:
49  /**
50  * @brief Add the Health Thermometer Service to an existing BLE object, initialize with temperature and location.
51  * @param[in] _ble Reference to the BLE device.
52  * @param[in] initialTemp Initial value in celsius.
53  * @param[in] _location
54  */
55  HealthThermometerService(BLE &_ble, float initialTemp, uint8_t _location) :
56  ble(_ble),
57  valueBytes(initialTemp),
58  tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (TemperatureValueBytes *)valueBytes.getPointer(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE),
59  tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, &_location) {
60 
61  GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, };
62  GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
63 
64  ble.gattServer().addService(hrmService);
65  }
66 
67  /**
68  * @brief Update the temperature being broadcast.
69  *
70  * @param[in] temperature
71  * Floating point value of the temperature.
72  *
73  */
74  void updateTemperature(float temperature) {
75  valueBytes.updateTemperature(temperature);
76  ble.gattServer().write(tempMeasurement.getValueHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes));
77  }
78 
79  /**
80  * @brief Update the location.
81  * @param loc
82  * New location value.
83  */
85  ble.gattServer().write(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t));
86  }
87 
88 private:
89  /* Private internal representation for the bytes used to work with the vaulue of the temperature characteristic. */
90  struct TemperatureValueBytes {
91  static const unsigned OFFSET_OF_FLAGS = 0;
92  static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t);
93  static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float);
94 
95  static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0;
96  static const unsigned TIMESTAMP_FLAG_POS = 1;
97  static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2;
98 
99  static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
100  static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
101 
102  TemperatureValueBytes(float initialTemperature) : bytes() {
103  /* Assumption: temperature values are expressed in celsius */
104  bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) |
105  (false << TIMESTAMP_FLAG_POS) |
106  (false << TEMPERATURE_TYPE_FLAG_POS);
107  updateTemperature(initialTemperature);
108  }
109 
110  void updateTemperature(float temp) {
111  uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp);
112  memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float));
113  }
114 
115  uint8_t *getPointer(void) {
116  return bytes;
117  }
118 
119  const uint8_t *getPointer(void) const {
120  return bytes;
121  }
122 
123 private:
124  /**
125  * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
126  * @param temperature The temperature as a float.
127  * @return The temperature in 11073-20601 FLOAT-Type format.
128  */
129  uint32_t quick_ieee11073_from_float(float temperature) {
130  uint8_t exponent = 0xFE; //Exponent is -2
131  uint32_t mantissa = (uint32_t)(temperature * 100);
132 
133  return (((uint32_t)exponent) << 24) | mantissa;
134  }
135 
136 private:
137  /* First byte: 8-bit flags. Second field is a float holding the temperature value. */
138  /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
139  uint8_t bytes[SIZEOF_VALUE_BYTES];
140  };
141 
142 protected:
143  BLE &ble;
144  TemperatureValueBytes valueBytes;
147 };
148 
149 #endif // BLE_FEATURE_GATT_SERVER
150 
151 #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/
void updateTemperature(float temperature)
Update the temperature being broadcast.
UUID of the health thermometer.
Definition: GattService.h:84
Abstract away BLE-capable radio transceivers or SOCs.
Definition: BLE.h:139
SensorLocation_t
Location of sensor on the body.
BLE Health Thermometer Service.
Representation of a GattServer characteristic.
GattAttribute::Handle_t getValueHandle(void) const
Get the characteristic&#39;s value attribute handle in the ATT table.
void updateLocation(SensorLocation_t loc)
Update the location.
Representation of a GattServer service.
Definition: GattService.h:38
Entry namespace for all BLE API definitions.
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.