fork BLE_API to add update adv payload API

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Thu Jul 02 09:06:11 2015 +0100
Revision:
714:a6130aaa0fd9
Synchronized with git rev 7e8977d8
Author: Rohit Grover
Release 0.3.8
=============

This is a minor set of enhancements before we yotta-ize BLE_API.

Enhancements
~~~~~~~~~~~~

* Minor rework for class UUID; added a default and copy constructor; and a != operator.

* Added copy constructor and accessors for GapAdvertisingParams.

* GapScanningParams:: remove unnecessary checks for SCAN_TIMEOUT_MAX.

* Add a comment header block to explain why BLEDevice::init() may not be safe
to call from global static context.

* Introduce GattAttribute::INVALID_HANDLE.

* Replace some deprecated uses of Gap::address_t with Gap::Address_t.

Bugfixes
~~~~~~~~

* None.

Who changed what in which revision?

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