prova invio BLE

Dependents:   BLE_HeartRate_IDB04A1

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:13:46 2016 +0100
Revision:
1131:692ddf04fc42
Parent:
1056:ce2fb3d09929
Child:
1134:d540a48f650d
Synchronized with git rev 13bf70b6
Author: Rohit Grover
Release 2.1.5
=============

A minor release to separate the concept of minlen and len in
GattCharacteristic. Also contains some improvements to documentation.

Who changed what in which revision?

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