project for nrf51822 qfab
Dependencies: eddystone_URL mbed
Fork of eddystone_URL by
BLE_API/services/HealthThermometerService.h@5:267bdacf5508, 2017-11-23 (annotated)
- Committer:
- tridung141196
- Date:
- Thu Nov 23 15:38:48 2017 +0000
- Revision:
- 5:267bdacf5508
- Parent:
- 0:76dfa9657d9d
ibeacon
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:76dfa9657d9d | 1 | /* mbed Microcontroller Library |
jksoft | 0:76dfa9657d9d | 2 | * Copyright (c) 2006-2013 ARM Limited |
jksoft | 0:76dfa9657d9d | 3 | * |
jksoft | 0:76dfa9657d9d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 0:76dfa9657d9d | 5 | * you may not use this file except in compliance with the License. |
jksoft | 0:76dfa9657d9d | 6 | * You may obtain a copy of the License at |
jksoft | 0:76dfa9657d9d | 7 | * |
jksoft | 0:76dfa9657d9d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 0:76dfa9657d9d | 9 | * |
jksoft | 0:76dfa9657d9d | 10 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 0:76dfa9657d9d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 0:76dfa9657d9d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 0:76dfa9657d9d | 13 | * See the License for the specific language governing permissions and |
jksoft | 0:76dfa9657d9d | 14 | * limitations under the License. |
jksoft | 0:76dfa9657d9d | 15 | */ |
jksoft | 0:76dfa9657d9d | 16 | |
jksoft | 0:76dfa9657d9d | 17 | #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__ |
jksoft | 0:76dfa9657d9d | 18 | #define __BLE_HEALTH_THERMOMETER_SERVICE_H__ |
jksoft | 0:76dfa9657d9d | 19 | |
jksoft | 0:76dfa9657d9d | 20 | #include "BLEDevice.h" |
jksoft | 0:76dfa9657d9d | 21 | |
jksoft | 0:76dfa9657d9d | 22 | /* Health Thermometer Service */ |
jksoft | 0:76dfa9657d9d | 23 | /* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml */ |
jksoft | 0:76dfa9657d9d | 24 | /* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
jksoft | 0:76dfa9657d9d | 25 | /* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml */ |
jksoft | 0:76dfa9657d9d | 26 | class HealthThermometerService { |
jksoft | 0:76dfa9657d9d | 27 | public: |
jksoft | 0:76dfa9657d9d | 28 | enum { |
jksoft | 0:76dfa9657d9d | 29 | LOCATION_ARMPIT = 1, |
jksoft | 0:76dfa9657d9d | 30 | LOCATION_BODY, |
jksoft | 0:76dfa9657d9d | 31 | LOCATION_EAR, |
jksoft | 0:76dfa9657d9d | 32 | LOCATION_FINGER, |
jksoft | 0:76dfa9657d9d | 33 | LOCATION_GI_TRACT, |
jksoft | 0:76dfa9657d9d | 34 | LOCATION_MOUTH, |
jksoft | 0:76dfa9657d9d | 35 | LOCATION_RECTUM, |
jksoft | 0:76dfa9657d9d | 36 | LOCATION_TOE, |
jksoft | 0:76dfa9657d9d | 37 | LOCATION_EAR_DRUM, |
jksoft | 0:76dfa9657d9d | 38 | }; |
jksoft | 0:76dfa9657d9d | 39 | |
jksoft | 0:76dfa9657d9d | 40 | public: |
jksoft | 0:76dfa9657d9d | 41 | |
jksoft | 0:76dfa9657d9d | 42 | /** |
jksoft | 0:76dfa9657d9d | 43 | * @param[in] _ble reference to the BLE device |
jksoft | 0:76dfa9657d9d | 44 | * @param[in] initialTemp initial value in celsius |
jksoft | 0:76dfa9657d9d | 45 | * @param[in] _location |
jksoft | 0:76dfa9657d9d | 46 | */ |
jksoft | 0:76dfa9657d9d | 47 | HealthThermometerService(BLEDevice &_ble, float initialTemp, uint8_t _location) : |
jksoft | 0:76dfa9657d9d | 48 | ble(_ble), |
jksoft | 0:76dfa9657d9d | 49 | valueBytes(initialTemp), |
jksoft | 0:76dfa9657d9d | 50 | tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
jksoft | 0:76dfa9657d9d | 51 | sizeof(TemperatureValueBytes), sizeof(TemperatureValueBytes), |
jksoft | 0:76dfa9657d9d | 52 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
jksoft | 0:76dfa9657d9d | 53 | tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location), |
jksoft | 0:76dfa9657d9d | 54 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { |
jksoft | 0:76dfa9657d9d | 55 | |
jksoft | 0:76dfa9657d9d | 56 | GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, }; |
jksoft | 0:76dfa9657d9d | 57 | GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); |
jksoft | 0:76dfa9657d9d | 58 | |
jksoft | 0:76dfa9657d9d | 59 | ble.addService(hrmService); |
jksoft | 0:76dfa9657d9d | 60 | } |
jksoft | 0:76dfa9657d9d | 61 | |
jksoft | 0:76dfa9657d9d | 62 | void updateTemperature(float temperature) { |
jksoft | 0:76dfa9657d9d | 63 | if (ble.getGapState().connected) { |
jksoft | 0:76dfa9657d9d | 64 | valueBytes.updateTemperature(temperature); |
jksoft | 0:76dfa9657d9d | 65 | ble.updateCharacteristicValue(tempMeasurement.getValueAttribute().getHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes)); |
jksoft | 0:76dfa9657d9d | 66 | } |
jksoft | 0:76dfa9657d9d | 67 | } |
jksoft | 0:76dfa9657d9d | 68 | |
jksoft | 0:76dfa9657d9d | 69 | private: |
jksoft | 0:76dfa9657d9d | 70 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
jksoft | 0:76dfa9657d9d | 71 | struct TemperatureValueBytes { |
jksoft | 0:76dfa9657d9d | 72 | static const unsigned OFFSET_OF_FLAGS = 0; |
jksoft | 0:76dfa9657d9d | 73 | static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t); |
jksoft | 0:76dfa9657d9d | 74 | static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float); |
jksoft | 0:76dfa9657d9d | 75 | |
jksoft | 0:76dfa9657d9d | 76 | static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0; |
jksoft | 0:76dfa9657d9d | 77 | static const unsigned TIMESTAMP_FLAG_POS = 1; |
jksoft | 0:76dfa9657d9d | 78 | static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2; |
jksoft | 0:76dfa9657d9d | 79 | |
jksoft | 0:76dfa9657d9d | 80 | static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0; |
jksoft | 0:76dfa9657d9d | 81 | static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1; |
jksoft | 0:76dfa9657d9d | 82 | |
jksoft | 0:76dfa9657d9d | 83 | TemperatureValueBytes(float initialTemperature) : bytes() { |
jksoft | 0:76dfa9657d9d | 84 | /* assumption: temperature values are expressed in Celsius */ |
jksoft | 0:76dfa9657d9d | 85 | bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) | |
jksoft | 0:76dfa9657d9d | 86 | (false << TIMESTAMP_FLAG_POS) | |
jksoft | 0:76dfa9657d9d | 87 | (false << TEMPERATURE_TYPE_FLAG_POS); |
jksoft | 0:76dfa9657d9d | 88 | updateTemperature(initialTemperature); |
jksoft | 0:76dfa9657d9d | 89 | } |
jksoft | 0:76dfa9657d9d | 90 | |
jksoft | 0:76dfa9657d9d | 91 | void updateTemperature(float temp) { |
jksoft | 0:76dfa9657d9d | 92 | uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp); |
jksoft | 0:76dfa9657d9d | 93 | memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float)); |
jksoft | 0:76dfa9657d9d | 94 | } |
jksoft | 0:76dfa9657d9d | 95 | |
jksoft | 0:76dfa9657d9d | 96 | uint8_t *getPointer(void) { |
jksoft | 0:76dfa9657d9d | 97 | return bytes; |
jksoft | 0:76dfa9657d9d | 98 | } |
jksoft | 0:76dfa9657d9d | 99 | |
jksoft | 0:76dfa9657d9d | 100 | const uint8_t *getPointer(void) const { |
jksoft | 0:76dfa9657d9d | 101 | return bytes; |
jksoft | 0:76dfa9657d9d | 102 | } |
jksoft | 0:76dfa9657d9d | 103 | |
jksoft | 0:76dfa9657d9d | 104 | private: |
jksoft | 0:76dfa9657d9d | 105 | /** |
jksoft | 0:76dfa9657d9d | 106 | * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type. |
jksoft | 0:76dfa9657d9d | 107 | * @param temperature The temperature as a float. |
jksoft | 0:76dfa9657d9d | 108 | * @return The temperature in 11073-20601 FLOAT-Type format. |
jksoft | 0:76dfa9657d9d | 109 | */ |
jksoft | 0:76dfa9657d9d | 110 | uint32_t quick_ieee11073_from_float(float temperature) { |
jksoft | 0:76dfa9657d9d | 111 | uint8_t exponent = 0xFE; //exponent is -2 |
jksoft | 0:76dfa9657d9d | 112 | uint32_t mantissa = (uint32_t)(temperature * 100); |
jksoft | 0:76dfa9657d9d | 113 | |
jksoft | 0:76dfa9657d9d | 114 | return (((uint32_t)exponent) << 24) | mantissa; |
jksoft | 0:76dfa9657d9d | 115 | } |
jksoft | 0:76dfa9657d9d | 116 | |
jksoft | 0:76dfa9657d9d | 117 | |
jksoft | 0:76dfa9657d9d | 118 | private: |
jksoft | 0:76dfa9657d9d | 119 | /* First byte = 8-bit flags, Second field is a float holding the temperature value. */ |
jksoft | 0:76dfa9657d9d | 120 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
jksoft | 0:76dfa9657d9d | 121 | uint8_t bytes[SIZEOF_VALUE_BYTES]; |
jksoft | 0:76dfa9657d9d | 122 | }; |
jksoft | 0:76dfa9657d9d | 123 | |
jksoft | 0:76dfa9657d9d | 124 | private: |
jksoft | 0:76dfa9657d9d | 125 | BLEDevice &ble; |
jksoft | 0:76dfa9657d9d | 126 | TemperatureValueBytes valueBytes; |
jksoft | 0:76dfa9657d9d | 127 | GattCharacteristic tempMeasurement; |
jksoft | 0:76dfa9657d9d | 128 | GattCharacteristic tempLocation; |
jksoft | 0:76dfa9657d9d | 129 | }; |
jksoft | 0:76dfa9657d9d | 130 | |
jksoft | 0:76dfa9657d9d | 131 | #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/ |