Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EnvironmentalService.h Source File

EnvironmentalService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
00018 #define __BLE_ENVIRONMENTAL_SERVICE_H__
00019 
00020 #include "ble/BLE.h"
00021 
00022 #if BLE_FEATURE_GATT_SERVER
00023 
00024 /**
00025 * @class EnvironmentalService
00026 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
00027 * Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
00028 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
00029 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
00030 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
00031 */
00032 class EnvironmentalService {
00033 public:
00034     typedef int16_t  TemperatureType_t;
00035     typedef uint16_t HumidityType_t;
00036     typedef uint32_t PressureType_t;
00037 
00038     /**
00039      * @brief   EnvironmentalService constructor.
00040      * @param   _ble Reference to BLE device.
00041      */
00042     EnvironmentalService(BLE& _ble) :
00043         ble(_ble),
00044         temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
00045         humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
00046         pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
00047     {
00048         static bool serviceAdded = false; /* We should only ever need to add the information service once. */
00049         if (serviceAdded) {
00050             return;
00051         }
00052 
00053         GattCharacteristic *charTable[] = { &humidityCharacteristic,
00054                                             &pressureCharacteristic,
00055                                             &temperatureCharacteristic };
00056 
00057         GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00058 
00059         ble.gattServer().addService(environmentalService);
00060         serviceAdded = true;
00061     }
00062 
00063     /**
00064      * @brief   Update humidity characteristic.
00065      * @param   newHumidityVal New humidity measurement.
00066      */
00067     void updateHumidity(HumidityType_t newHumidityVal)
00068     {
00069         humidity = (HumidityType_t) (newHumidityVal * 100);
00070         ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
00071     }
00072 
00073     /**
00074      * @brief   Update pressure characteristic.
00075      * @param   newPressureVal New pressure measurement.
00076      */
00077     void updatePressure(PressureType_t newPressureVal)
00078     {
00079         pressure = (PressureType_t) (newPressureVal * 10);
00080         ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
00081     }
00082 
00083     /**
00084      * @brief   Update temperature characteristic.
00085      * @param   newTemperatureVal New temperature measurement.
00086      */
00087     void updateTemperature(float newTemperatureVal)
00088     {
00089         temperature = (TemperatureType_t) (newTemperatureVal * 100);
00090         ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
00091     }
00092 
00093 private:
00094     BLE& ble;
00095 
00096     TemperatureType_t temperature;
00097     HumidityType_t    humidity;
00098     PressureType_t    pressure;
00099 
00100     ReadOnlyGattCharacteristic<TemperatureType_t>  temperatureCharacteristic;
00101     ReadOnlyGattCharacteristic<HumidityType_t>     humidityCharacteristic;
00102     ReadOnlyGattCharacteristic<PressureType_t>     pressureCharacteristic;
00103 };
00104 
00105 #endif // BLE_FEATURE_GATT_SERVER
00106 
00107 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/