High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/services/HealthThermometerService.h@716:11b41f651697, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 09:06:11 2015 +0100
- Revision:
- 716:11b41f651697
- Parent:
- 712:b04b5db36865
- Child:
- 722:eab9499e4250
Synchronized with git rev d80fec88
Author: Rohit Grover
Release 0.4.0
=============
This is a major release which introduces the GATT Client functionality. It
also aligns BLE_API with builds using our new package manager: yotta
(https://github.com/armmbed/yotta).
Many APIs have seen some redesign. We encourage our users to pay attention to
the changes and migrate appropriately over time. We've also taken care to
ensure that existing code continues to work the same way. There's more
documentation in the form of comment headers for APIs to explain proper usage;
in many cases comment headers suggest alternative use of APIs.
Enhancements
~~~~~~~~~~~~
* Introduce GattClient. This includes functionality for service-discovery,
connections, and attribute-reads and writes. You'll find a demo program for
LEDBlinker on the mbed.org Bluetooth team page to use the new APIs. Some of
the GATT client functionality hasn't been implemented yet, but the APIs have
been added.
* Most APIs in the abstract base classes like Gap and GattServer return
BLE_ERROR_NOT_IMPLEMENTED. Previously many APIs were pure-virtual, which did
not permit partial ports to compile.
* We've added a new abstract base class for SecurityManager. All security
related APIs have been moved into that.
* BLEDevice has been renamed as BLE. A deprecated alias for BLEDevice is
available to support existing code.
* There has been a major cleanup of APIs under BLE. APIs have now been
categorized as belonging to Gap, GattServer, GattClient, or SecurityManager.
There are accessors to get references for Gap, GattServer, GattClient, and
SecurityManager. A former call to ble.setAddress(...) is now expected to be
achieved with ble.gap().setAddress(...).
* We've cleaned up our APIs, and this has resulted in dropping some APIs like
BLE::reset().
* We've also dropped GattServer::initializeGattDatabase(). THis was added at
some point to support controllers where a commit point was needed to
indicate when the application had finished constructing the GATT database.
This API would get called internally before Gap::startAdvertising(). We now
expect the underlying port to do the equivalent of initializeGattDatabase()
implicitly upon Gap::startAdvertising().
* The callback for BLE.onTimeout() now receives a TimeoutSource_t to indicate
the cause of the timeout. This is perhaps the only breaking API change. We
expect it to have very little disruptive effect.
* We've added a version of Gap::disconnect() which takes a connection handle.
The previous API (which did not take a connection handle) has been
deprecated; it will still work for situations where there's only a single
active connection. We hold on to that API to allow existing code to migrate
to the new API.
Bugfixes
~~~~~~~~
* None.
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 | 712:b04b5db36865 | 20 | #include "BLE.h" |
rgrover1 | 712:b04b5db36865 | 21 | |
rgrover1 | 712:b04b5db36865 | 22 | /** |
rgrover1 | 712:b04b5db36865 | 23 | * @class HealthThermometerService |
rgrover1 | 712:b04b5db36865 | 24 | * @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature. <br> |
rgrover1 | 712:b04b5db36865 | 25 | * Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml <br> |
rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 32 | * @enum Sensor Location |
rgrover1 | 712:b04b5db36865 | 33 | * @brief Location of sensor on the body |
rgrover1 | 712:b04b5db36865 | 34 | */ |
rgrover1 | 712:b04b5db36865 | 35 | enum SensorLocation_t { |
rgrover1 | 712:b04b5db36865 | 36 | LOCATION_ARMPIT = 1, /*!< armpit */ |
rgrover1 | 712:b04b5db36865 | 37 | LOCATION_BODY, /*!< body */ |
rgrover1 | 712:b04b5db36865 | 38 | LOCATION_EAR, /*!< ear */ |
rgrover1 | 712:b04b5db36865 | 39 | LOCATION_FINGER, /*!< finger */ |
rgrover1 | 712:b04b5db36865 | 40 | LOCATION_GI_TRACT, /*!< GI tract */ |
rgrover1 | 712:b04b5db36865 | 41 | LOCATION_MOUTH, /*!< mouth */ |
rgrover1 | 712:b04b5db36865 | 42 | LOCATION_RECTUM, /*!< rectum */ |
rgrover1 | 712:b04b5db36865 | 43 | LOCATION_TOE, /*!< toe */ |
rgrover1 | 712:b04b5db36865 | 44 | LOCATION_EAR_DRUM, /*!< ear drum */ |
rgrover1 | 712:b04b5db36865 | 45 | }; |
rgrover1 | 712:b04b5db36865 | 46 | |
rgrover1 | 712:b04b5db36865 | 47 | public: |
rgrover1 | 712:b04b5db36865 | 48 | /** |
rgrover1 | 712:b04b5db36865 | 49 | * @brief Add the Health Thermometer Service to an existing ble object, initialize with temperature and location. |
rgrover1 | 712:b04b5db36865 | 50 | * @param[ref] _ble reference to the BLE device |
rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 67 | * @brief Update the temperature being broadcast |
rgrover1 | 712:b04b5db36865 | 68 | * |
rgrover1 | 712:b04b5db36865 | 69 | * @param[in] temperature |
rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 76 | ble.updateCharacteristicValue(tempMeasurement.getValueAttribute().getHandle(), 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 | 712:b04b5db36865 | 83 | * new location value. |
rgrover1 | 712:b04b5db36865 | 84 | */ |
rgrover1 | 712:b04b5db36865 | 85 | void updateLocation(SensorLocation_t loc) { |
rgrover1 | 712:b04b5db36865 | 86 | ble.updateCharacteristicValue(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t)); |
rgrover1 | 712:b04b5db36865 | 87 | } |
rgrover1 | 712:b04b5db36865 | 88 | |
rgrover1 | 712:b04b5db36865 | 89 | private: |
rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 138 | /* First byte = 8-bit flags, Second field is a float holding the temperature value. */ |
rgrover1 | 712:b04b5db36865 | 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 | 712:b04b5db36865 | 143 | private: |
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__*/ |