Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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