Lancaster University's fork of the mbed BLE API. Lives on github, https://github.com/lancaster-university/BLE_API

Dependents:   microbit-dal microbit-dal microbit-ble-open microbit-dal ... more

Fork of BLE_API by Bluetooth Low Energy

Committer:
LancasterUniversity
Date:
Wed Apr 06 18:40:40 2016 +0100
Revision:
1140:dd2f69fad8c6
Parent:
1134:faa0160c87ff
Synchronized with git rev bbc2dc58
Author: Joe Finney
microbit: Additional callback to indicate to applications when System
Attributes require initialisation from persistent storage.

Who changed what in which revision?

UserRevisionLine numberNew 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
LancasterUniversity 1134:faa0160c87ff 24 * @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature.
LancasterUniversity 1134:faa0160c87ff 25 * Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml
LancasterUniversity 1134:faa0160c87ff 26 * Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
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 /**
LancasterUniversity 1134:faa0160c87ff 32 * @enum Sensor Location.
LancasterUniversity 1134:faa0160c87ff 33 * @brief Location of sensor on the body.
rgrover1 712:b04b5db36865 34 */
rgrover1 712:b04b5db36865 35 enum SensorLocation_t {
LancasterUniversity 1134:faa0160c87ff 36 LOCATION_ARMPIT = 1, /*!< Armpit. */
LancasterUniversity 1134:faa0160c87ff 37 LOCATION_BODY, /*!< Body. */
LancasterUniversity 1134:faa0160c87ff 38 LOCATION_EAR, /*!< Ear. */
LancasterUniversity 1134:faa0160c87ff 39 LOCATION_FINGER, /*!< Finger. */
rgrover1 712:b04b5db36865 40 LOCATION_GI_TRACT, /*!< GI tract */
LancasterUniversity 1134:faa0160c87ff 41 LOCATION_MOUTH, /*!< Mouth. */
LancasterUniversity 1134:faa0160c87ff 42 LOCATION_RECTUM, /*!< Rectum. */
LancasterUniversity 1134:faa0160c87ff 43 LOCATION_TOE, /*!< Toe. */
LancasterUniversity 1134:faa0160c87ff 44 LOCATION_EAR_DRUM, /*!< Eardrum. */
rgrover1 712:b04b5db36865 45 };
rgrover1 712:b04b5db36865 46
rgrover1 712:b04b5db36865 47 public:
rgrover1 712:b04b5db36865 48 /**
LancasterUniversity 1134:faa0160c87ff 49 * @brief Add the Health Thermometer Service to an existing BLE object, initialize with temperature and location.
LancasterUniversity 1134:faa0160c87ff 50 * @param[ref] _ble Reference to the BLE device.
LancasterUniversity 1134:faa0160c87ff 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 /**
LancasterUniversity 1134:faa0160c87ff 67 * @brief Update the temperature being broadcast.
rgrover1 712:b04b5db36865 68 *
rgrover1 712:b04b5db36865 69 * @param[in] temperature
LancasterUniversity 1134:faa0160c87ff 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
LancasterUniversity 1134:faa0160c87ff 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:
LancasterUniversity 1134:faa0160c87ff 90 /* Private internal representation for the bytes used to work with the vaulue of the temperature 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() {
LancasterUniversity 1134:faa0160c87ff 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) {
LancasterUniversity 1134:faa0160c87ff 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:
LancasterUniversity 1134:faa0160c87ff 138 /* First byte: 8-bit flags. Second field is a float holding the temperature value. */
LancasterUniversity 1134:faa0160c87ff 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__*/