Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.
Fork of BLE_API by
services/HealthThermometerService.h@118:620d28e7a1ba, 2014-09-22 (annotated)
- Committer:
- Rohit Grover
- Date:
- Mon Sep 22 10:59:09 2014 +0100
- Revision:
- 118:620d28e7a1ba
- Child:
- 235:448f29f4ae7f
Release 0.2.0
=============
Highlights:
Introducing standard services to simplify applications.
Add support for over-the-air firmware updates.
Features
~~~~~~~~
- This release introduces 'templates' for common services such as heart-rate,
battery-level, device-info, UART, device-firmware-update etc. These services
take the shape of class declarations within header files aggregated under a
new folder called 'services/'. These service-classes provide a high-level
API hopefully easing the burden of developing BLE applications. The
underlying APIs to work with characteristics and services are still
available to allow greater control if needed. We expect to grow the
supported services to include all SIG defined BLE profiles.
- WriteCallbackParams now includes the characteristic's value-attribute
handle; this changes the signature of onDataWritten().
- BLEDevice::onDataWritten() now allows chaining of callbacks--this means that
it is possible to chain together multiple onDataWritten callbacks
(potentially from different modules of an application) to receive updates to
characteristics. Many services, such as DFU and UART add their own
onDataWritten callbacks behind the scenes to trap interesting events. It is
also possible to chain callbacks to functions within objects.
- Added the following expectation for GattCharacteristic: If valuePtr ==
NULL, initialLength == 0, and properties == READ for the value attribute of
a characteristic, then that particular characteristic may be considered
optional and dropped while instantiating the service with the underlying BLE
stack.
- Introducing the typedef GattAttribute::Handle_t to capture Attribute handles.
Bugfixes
~~~~~~~~
None.
Compatibility
~~~~~~~~~~~~~
The signature of onDataWritten() has seen a change; so application programs
using this new version of the BLE API will need minor modifications. Please
refer to sample programs under BLE team page.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
118:620d28e7a1ba | 1 | /* mbed Microcontroller Library |
Rohit Grover |
118:620d28e7a1ba | 2 | * Copyright (c) 2006-2013 ARM Limited |
Rohit Grover |
118:620d28e7a1ba | 3 | * |
Rohit Grover |
118:620d28e7a1ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
118:620d28e7a1ba | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
118:620d28e7a1ba | 6 | * You may obtain a copy of the License at |
Rohit Grover |
118:620d28e7a1ba | 7 | * |
Rohit Grover |
118:620d28e7a1ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
118:620d28e7a1ba | 9 | * |
Rohit Grover |
118:620d28e7a1ba | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
118:620d28e7a1ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
118:620d28e7a1ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
118:620d28e7a1ba | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
118:620d28e7a1ba | 14 | * limitations under the License. |
Rohit Grover |
118:620d28e7a1ba | 15 | */ |
Rohit Grover |
118:620d28e7a1ba | 16 | |
Rohit Grover |
118:620d28e7a1ba | 17 | #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__ |
Rohit Grover |
118:620d28e7a1ba | 18 | #define __BLE_HEALTH_THERMOMETER_SERVICE_H__ |
Rohit Grover |
118:620d28e7a1ba | 19 | |
Rohit Grover |
118:620d28e7a1ba | 20 | #include "BLEDevice.h" |
Rohit Grover |
118:620d28e7a1ba | 21 | |
Rohit Grover |
118:620d28e7a1ba | 22 | /* Health Thermometer Service */ |
Rohit Grover |
118:620d28e7a1ba | 23 | /* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml */ |
Rohit Grover |
118:620d28e7a1ba | 24 | /* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
Rohit Grover |
118:620d28e7a1ba | 25 | /* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml */ |
Rohit Grover |
118:620d28e7a1ba | 26 | class HealthThermometerService { |
Rohit Grover |
118:620d28e7a1ba | 27 | public: |
Rohit Grover |
118:620d28e7a1ba | 28 | enum { |
Rohit Grover |
118:620d28e7a1ba | 29 | LOCATION_ARMPIT = 1, |
Rohit Grover |
118:620d28e7a1ba | 30 | LOCATION_BODY, |
Rohit Grover |
118:620d28e7a1ba | 31 | LOCATION_EAR, |
Rohit Grover |
118:620d28e7a1ba | 32 | LOCATION_FINGER, |
Rohit Grover |
118:620d28e7a1ba | 33 | LOCATION_GI_TRACT, |
Rohit Grover |
118:620d28e7a1ba | 34 | LOCATION_MOUTH, |
Rohit Grover |
118:620d28e7a1ba | 35 | LOCATION_RECTUM, |
Rohit Grover |
118:620d28e7a1ba | 36 | LOCATION_TOE, |
Rohit Grover |
118:620d28e7a1ba | 37 | LOCATION_EAR_DRUM, |
Rohit Grover |
118:620d28e7a1ba | 38 | }; |
Rohit Grover |
118:620d28e7a1ba | 39 | |
Rohit Grover |
118:620d28e7a1ba | 40 | public: |
Rohit Grover |
118:620d28e7a1ba | 41 | |
Rohit Grover |
118:620d28e7a1ba | 42 | /** |
Rohit Grover |
118:620d28e7a1ba | 43 | * @param[in] _ble reference to the BLE device |
Rohit Grover |
118:620d28e7a1ba | 44 | * @param[in] initialTemp initial value in celsius |
Rohit Grover |
118:620d28e7a1ba | 45 | * @param[in] _location |
Rohit Grover |
118:620d28e7a1ba | 46 | */ |
Rohit Grover |
118:620d28e7a1ba | 47 | HealthThermometerService(BLEDevice &_ble, float initialTemp, uint8_t _location) : |
Rohit Grover |
118:620d28e7a1ba | 48 | ble(_ble), |
Rohit Grover |
118:620d28e7a1ba | 49 | valueBytes(initialTemp), |
Rohit Grover |
118:620d28e7a1ba | 50 | tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
Rohit Grover |
118:620d28e7a1ba | 51 | sizeof(TemperatureValueBytes), sizeof(TemperatureValueBytes), |
Rohit Grover |
118:620d28e7a1ba | 52 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
Rohit Grover |
118:620d28e7a1ba | 53 | tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location), |
Rohit Grover |
118:620d28e7a1ba | 54 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { |
Rohit Grover |
118:620d28e7a1ba | 55 | |
Rohit Grover |
118:620d28e7a1ba | 56 | GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, }; |
Rohit Grover |
118:620d28e7a1ba | 57 | GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); |
Rohit Grover |
118:620d28e7a1ba | 58 | |
Rohit Grover |
118:620d28e7a1ba | 59 | ble.addService(hrmService); |
Rohit Grover |
118:620d28e7a1ba | 60 | } |
Rohit Grover |
118:620d28e7a1ba | 61 | |
Rohit Grover |
118:620d28e7a1ba | 62 | void updateTemperature(float temperature) { |
Rohit Grover |
118:620d28e7a1ba | 63 | if (ble.getGapState().connected) { |
Rohit Grover |
118:620d28e7a1ba | 64 | valueBytes.updateTemperature(temperature); |
Rohit Grover |
118:620d28e7a1ba | 65 | ble.updateCharacteristicValue(tempMeasurement.getValueAttribute().getHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes)); |
Rohit Grover |
118:620d28e7a1ba | 66 | } |
Rohit Grover |
118:620d28e7a1ba | 67 | } |
Rohit Grover |
118:620d28e7a1ba | 68 | |
Rohit Grover |
118:620d28e7a1ba | 69 | private: |
Rohit Grover |
118:620d28e7a1ba | 70 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
Rohit Grover |
118:620d28e7a1ba | 71 | struct TemperatureValueBytes { |
Rohit Grover |
118:620d28e7a1ba | 72 | static const unsigned OFFSET_OF_FLAGS = 0; |
Rohit Grover |
118:620d28e7a1ba | 73 | static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t); |
Rohit Grover |
118:620d28e7a1ba | 74 | static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float); |
Rohit Grover |
118:620d28e7a1ba | 75 | |
Rohit Grover |
118:620d28e7a1ba | 76 | static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0; |
Rohit Grover |
118:620d28e7a1ba | 77 | static const unsigned TIMESTAMP_FLAG_POS = 1; |
Rohit Grover |
118:620d28e7a1ba | 78 | static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2; |
Rohit Grover |
118:620d28e7a1ba | 79 | |
Rohit Grover |
118:620d28e7a1ba | 80 | static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0; |
Rohit Grover |
118:620d28e7a1ba | 81 | static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1; |
Rohit Grover |
118:620d28e7a1ba | 82 | |
Rohit Grover |
118:620d28e7a1ba | 83 | TemperatureValueBytes(float initialTemperature) : bytes() { |
Rohit Grover |
118:620d28e7a1ba | 84 | /* assumption: temperature values are expressed in Celsius */ |
Rohit Grover |
118:620d28e7a1ba | 85 | bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) | |
Rohit Grover |
118:620d28e7a1ba | 86 | (false << TIMESTAMP_FLAG_POS) | |
Rohit Grover |
118:620d28e7a1ba | 87 | (false << TEMPERATURE_TYPE_FLAG_POS); |
Rohit Grover |
118:620d28e7a1ba | 88 | updateTemperature(initialTemperature); |
Rohit Grover |
118:620d28e7a1ba | 89 | } |
Rohit Grover |
118:620d28e7a1ba | 90 | |
Rohit Grover |
118:620d28e7a1ba | 91 | void updateTemperature(float temp) { |
Rohit Grover |
118:620d28e7a1ba | 92 | uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp); |
Rohit Grover |
118:620d28e7a1ba | 93 | memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float)); |
Rohit Grover |
118:620d28e7a1ba | 94 | } |
Rohit Grover |
118:620d28e7a1ba | 95 | |
Rohit Grover |
118:620d28e7a1ba | 96 | uint8_t *getPointer(void) { |
Rohit Grover |
118:620d28e7a1ba | 97 | return bytes; |
Rohit Grover |
118:620d28e7a1ba | 98 | } |
Rohit Grover |
118:620d28e7a1ba | 99 | |
Rohit Grover |
118:620d28e7a1ba | 100 | const uint8_t *getPointer(void) const { |
Rohit Grover |
118:620d28e7a1ba | 101 | return bytes; |
Rohit Grover |
118:620d28e7a1ba | 102 | } |
Rohit Grover |
118:620d28e7a1ba | 103 | |
Rohit Grover |
118:620d28e7a1ba | 104 | private: |
Rohit Grover |
118:620d28e7a1ba | 105 | /** |
Rohit Grover |
118:620d28e7a1ba | 106 | * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type. |
Rohit Grover |
118:620d28e7a1ba | 107 | * @param temperature The temperature as a float. |
Rohit Grover |
118:620d28e7a1ba | 108 | * @return The temperature in 11073-20601 FLOAT-Type format. |
Rohit Grover |
118:620d28e7a1ba | 109 | */ |
Rohit Grover |
118:620d28e7a1ba | 110 | uint32_t quick_ieee11073_from_float(float temperature) { |
Rohit Grover |
118:620d28e7a1ba | 111 | uint8_t exponent = 0xFE; //exponent is -2 |
Rohit Grover |
118:620d28e7a1ba | 112 | uint32_t mantissa = (uint32_t)(temperature * 100); |
Rohit Grover |
118:620d28e7a1ba | 113 | |
Rohit Grover |
118:620d28e7a1ba | 114 | return (((uint32_t)exponent) << 24) | mantissa; |
Rohit Grover |
118:620d28e7a1ba | 115 | } |
Rohit Grover |
118:620d28e7a1ba | 116 | |
Rohit Grover |
118:620d28e7a1ba | 117 | |
Rohit Grover |
118:620d28e7a1ba | 118 | private: |
Rohit Grover |
118:620d28e7a1ba | 119 | /* First byte = 8-bit flags, Second field is a float holding the temperature value. */ |
Rohit Grover |
118:620d28e7a1ba | 120 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
Rohit Grover |
118:620d28e7a1ba | 121 | uint8_t bytes[SIZEOF_VALUE_BYTES]; |
Rohit Grover |
118:620d28e7a1ba | 122 | }; |
Rohit Grover |
118:620d28e7a1ba | 123 | |
Rohit Grover |
118:620d28e7a1ba | 124 | private: |
Rohit Grover |
118:620d28e7a1ba | 125 | BLEDevice &ble; |
Rohit Grover |
118:620d28e7a1ba | 126 | TemperatureValueBytes valueBytes; |
Rohit Grover |
118:620d28e7a1ba | 127 | GattCharacteristic tempMeasurement; |
Rohit Grover |
118:620d28e7a1ba | 128 | GattCharacteristic tempLocation; |
Rohit Grover |
118:620d28e7a1ba | 129 | }; |
Rohit Grover |
118:620d28e7a1ba | 130 | |
Rohit Grover |
118:620d28e7a1ba | 131 | #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/ |