High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/services/HealthThermometerService.h@1035:d8ba3cb5a39a, 2015-12-10 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Dec 10 09:15:04 2015 +0000
- Revision:
- 1035:d8ba3cb5a39a
- Parent:
- 1029:a5d0fec3b17c
- Child:
- 1036:7af23cdcfcac
Synchronized with git rev 01eaeeee
Author: Marcus Chang
Added bit ordering parameter to UUID construction.
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_HEALTH_THERMOMETER_SERVICE_H__ |
rgrover1 | 712:b04b5db36865 | 18 | #define __BLE_HEALTH_THERMOMETER_SERVICE_H__ |
rgrover1 | 712:b04b5db36865 | 19 | |
rgrover1 | 912:f728aa46e7df | 20 | #include "ble/BLE.h" |
rgrover1 | 712:b04b5db36865 | 21 | |
rgrover1 | 712:b04b5db36865 | 22 | /** |
rgrover1 | 712:b04b5db36865 | 23 | * @class HealthThermometerService |
rgrover1 | 1035:d8ba3cb5a39a | 24 | * @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature. <br> |
rgrover1 | 1035:d8ba3cb5a39a | 25 | * Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml <br> |
rgrover1 | 1035:d8ba3cb5a39a | 26 | * Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml <br> |
rgrover1 | 712:b04b5db36865 | 27 | * Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml |
rgrover1 | 712:b04b5db36865 | 28 | */ |
rgrover1 | 712:b04b5db36865 | 29 | class HealthThermometerService { |
rgrover1 | 712:b04b5db36865 | 30 | public: |
rgrover1 | 712:b04b5db36865 | 31 | /** |
rgrover1 | 1035:d8ba3cb5a39a | 32 | * @enum Sensor Location |
rgrover1 | 1035:d8ba3cb5a39a | 33 | * @brief Location of sensor on the body |
rgrover1 | 712:b04b5db36865 | 34 | */ |
rgrover1 | 712:b04b5db36865 | 35 | enum SensorLocation_t { |
rgrover1 | 1035:d8ba3cb5a39a | 36 | LOCATION_ARMPIT = 1, /*!< armpit */ |
rgrover1 | 1035:d8ba3cb5a39a | 37 | LOCATION_BODY, /*!< body */ |
rgrover1 | 1035:d8ba3cb5a39a | 38 | LOCATION_EAR, /*!< ear */ |
rgrover1 | 1035:d8ba3cb5a39a | 39 | LOCATION_FINGER, /*!< finger */ |
rgrover1 | 712:b04b5db36865 | 40 | LOCATION_GI_TRACT, /*!< GI tract */ |
rgrover1 | 1035:d8ba3cb5a39a | 41 | LOCATION_MOUTH, /*!< mouth */ |
rgrover1 | 1035:d8ba3cb5a39a | 42 | LOCATION_RECTUM, /*!< rectum */ |
rgrover1 | 1035:d8ba3cb5a39a | 43 | LOCATION_TOE, /*!< toe */ |
rgrover1 | 1035:d8ba3cb5a39a | 44 | LOCATION_EAR_DRUM, /*!< ear drum */ |
rgrover1 | 712:b04b5db36865 | 45 | }; |
rgrover1 | 712:b04b5db36865 | 46 | |
rgrover1 | 712:b04b5db36865 | 47 | public: |
rgrover1 | 712:b04b5db36865 | 48 | /** |
rgrover1 | 1035:d8ba3cb5a39a | 49 | * @brief Add the Health Thermometer Service to an existing ble object, initialize with temperature and location. |
rgrover1 | 1035:d8ba3cb5a39a | 50 | * @param[ref] _ble reference to the BLE device |
rgrover1 | 1035:d8ba3cb5a39a | 51 | * @param[in] initialTemp initial value in celsius |
rgrover1 | 712:b04b5db36865 | 52 | * @param[in] _location |
rgrover1 | 712:b04b5db36865 | 53 | */ |
rgrover1 | 712:b04b5db36865 | 54 | HealthThermometerService(BLE &_ble, float initialTemp, uint8_t _location) : |
rgrover1 | 712:b04b5db36865 | 55 | ble(_ble), |
rgrover1 | 712:b04b5db36865 | 56 | valueBytes(initialTemp), |
rgrover1 | 712:b04b5db36865 | 57 | tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (TemperatureValueBytes *)valueBytes.getPointer(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
rgrover1 | 712:b04b5db36865 | 58 | tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, &_location) { |
rgrover1 | 712:b04b5db36865 | 59 | |
rgrover1 | 712:b04b5db36865 | 60 | GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, }; |
rgrover1 | 712:b04b5db36865 | 61 | GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); |
rgrover1 | 712:b04b5db36865 | 62 | |
rgrover1 | 712:b04b5db36865 | 63 | ble.addService(hrmService); |
rgrover1 | 712:b04b5db36865 | 64 | } |
rgrover1 | 712:b04b5db36865 | 65 | |
rgrover1 | 712:b04b5db36865 | 66 | /** |
rgrover1 | 1035:d8ba3cb5a39a | 67 | * @brief Update the temperature being broadcast |
rgrover1 | 712:b04b5db36865 | 68 | * |
rgrover1 | 712:b04b5db36865 | 69 | * @param[in] temperature |
rgrover1 | 1035:d8ba3cb5a39a | 70 | * Floating point value of the temperature |
rgrover1 | 712:b04b5db36865 | 71 | * |
rgrover1 | 712:b04b5db36865 | 72 | */ |
rgrover1 | 712:b04b5db36865 | 73 | void updateTemperature(float temperature) { |
rgrover1 | 712:b04b5db36865 | 74 | if (ble.getGapState().connected) { |
rgrover1 | 712:b04b5db36865 | 75 | valueBytes.updateTemperature(temperature); |
rgrover1 | 728:997ba5e7b3b6 | 76 | ble.gattServer().write(tempMeasurement.getValueHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes)); |
rgrover1 | 712:b04b5db36865 | 77 | } |
rgrover1 | 712:b04b5db36865 | 78 | } |
rgrover1 | 712:b04b5db36865 | 79 | |
rgrover1 | 712:b04b5db36865 | 80 | /** |
rgrover1 | 712:b04b5db36865 | 81 | * @brief Update the location. |
rgrover1 | 712:b04b5db36865 | 82 | * @param loc |
rgrover1 | 1035:d8ba3cb5a39a | 83 | * new location value. |
rgrover1 | 712:b04b5db36865 | 84 | */ |
rgrover1 | 712:b04b5db36865 | 85 | void updateLocation(SensorLocation_t loc) { |
rgrover1 | 728:997ba5e7b3b6 | 86 | ble.gattServer().write(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t)); |
rgrover1 | 712:b04b5db36865 | 87 | } |
rgrover1 | 712:b04b5db36865 | 88 | |
rgrover1 | 712:b04b5db36865 | 89 | private: |
rgrover1 | 1035:d8ba3cb5a39a | 90 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
rgrover1 | 712:b04b5db36865 | 91 | struct TemperatureValueBytes { |
rgrover1 | 712:b04b5db36865 | 92 | static const unsigned OFFSET_OF_FLAGS = 0; |
rgrover1 | 712:b04b5db36865 | 93 | static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t); |
rgrover1 | 712:b04b5db36865 | 94 | static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float); |
rgrover1 | 712:b04b5db36865 | 95 | |
rgrover1 | 712:b04b5db36865 | 96 | static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0; |
rgrover1 | 712:b04b5db36865 | 97 | static const unsigned TIMESTAMP_FLAG_POS = 1; |
rgrover1 | 712:b04b5db36865 | 98 | static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2; |
rgrover1 | 712:b04b5db36865 | 99 | |
rgrover1 | 712:b04b5db36865 | 100 | static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0; |
rgrover1 | 712:b04b5db36865 | 101 | static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1; |
rgrover1 | 712:b04b5db36865 | 102 | |
rgrover1 | 712:b04b5db36865 | 103 | TemperatureValueBytes(float initialTemperature) : bytes() { |
rgrover1 | 1035:d8ba3cb5a39a | 104 | /* assumption: temperature values are expressed in Celsius */ |
rgrover1 | 712:b04b5db36865 | 105 | bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) | |
rgrover1 | 712:b04b5db36865 | 106 | (false << TIMESTAMP_FLAG_POS) | |
rgrover1 | 712:b04b5db36865 | 107 | (false << TEMPERATURE_TYPE_FLAG_POS); |
rgrover1 | 712:b04b5db36865 | 108 | updateTemperature(initialTemperature); |
rgrover1 | 712:b04b5db36865 | 109 | } |
rgrover1 | 712:b04b5db36865 | 110 | |
rgrover1 | 712:b04b5db36865 | 111 | void updateTemperature(float temp) { |
rgrover1 | 712:b04b5db36865 | 112 | uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp); |
rgrover1 | 712:b04b5db36865 | 113 | memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float)); |
rgrover1 | 712:b04b5db36865 | 114 | } |
rgrover1 | 712:b04b5db36865 | 115 | |
rgrover1 | 712:b04b5db36865 | 116 | uint8_t *getPointer(void) { |
rgrover1 | 712:b04b5db36865 | 117 | return bytes; |
rgrover1 | 712:b04b5db36865 | 118 | } |
rgrover1 | 712:b04b5db36865 | 119 | |
rgrover1 | 712:b04b5db36865 | 120 | const uint8_t *getPointer(void) const { |
rgrover1 | 712:b04b5db36865 | 121 | return bytes; |
rgrover1 | 712:b04b5db36865 | 122 | } |
rgrover1 | 712:b04b5db36865 | 123 | |
rgrover1 | 712:b04b5db36865 | 124 | private: |
rgrover1 | 712:b04b5db36865 | 125 | /** |
rgrover1 | 712:b04b5db36865 | 126 | * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type. |
rgrover1 | 712:b04b5db36865 | 127 | * @param temperature The temperature as a float. |
rgrover1 | 712:b04b5db36865 | 128 | * @return The temperature in 11073-20601 FLOAT-Type format. |
rgrover1 | 712:b04b5db36865 | 129 | */ |
rgrover1 | 712:b04b5db36865 | 130 | uint32_t quick_ieee11073_from_float(float temperature) { |
rgrover1 | 1035:d8ba3cb5a39a | 131 | uint8_t exponent = 0xFE; //exponent is -2 |
rgrover1 | 712:b04b5db36865 | 132 | uint32_t mantissa = (uint32_t)(temperature * 100); |
rgrover1 | 712:b04b5db36865 | 133 | |
rgrover1 | 712:b04b5db36865 | 134 | return (((uint32_t)exponent) << 24) | mantissa; |
rgrover1 | 712:b04b5db36865 | 135 | } |
rgrover1 | 712:b04b5db36865 | 136 | |
rgrover1 | 712:b04b5db36865 | 137 | private: |
rgrover1 | 1035:d8ba3cb5a39a | 138 | /* First byte = 8-bit flags, Second field is a float holding the temperature value. */ |
rgrover1 | 1035:d8ba3cb5a39a | 139 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
rgrover1 | 712:b04b5db36865 | 140 | uint8_t bytes[SIZEOF_VALUE_BYTES]; |
rgrover1 | 712:b04b5db36865 | 141 | }; |
rgrover1 | 712:b04b5db36865 | 142 | |
rgrover1 | 728:997ba5e7b3b6 | 143 | protected: |
rgrover1 | 712:b04b5db36865 | 144 | BLE &ble; |
rgrover1 | 712:b04b5db36865 | 145 | TemperatureValueBytes valueBytes; |
rgrover1 | 712:b04b5db36865 | 146 | ReadOnlyGattCharacteristic<TemperatureValueBytes> tempMeasurement; |
rgrover1 | 712:b04b5db36865 | 147 | ReadOnlyGattCharacteristic<uint8_t> tempLocation; |
rgrover1 | 712:b04b5db36865 | 148 | }; |
rgrover1 | 712:b04b5db36865 | 149 | |
rgrover1 | 712:b04b5db36865 | 150 | #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/ |