BA / Mbed OS BaBoRo1
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 /**
00023 * @class EnvironmentalService
00024 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
00025 * Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
00026 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
00027 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
00028 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
00029 */
00030 class EnvironmentalService {
00031 public:
00032     typedef int16_t  TemperatureType_t;
00033     typedef uint16_t HumidityType_t;
00034     typedef uint32_t PressureType_t;
00035 
00036     /**
00037      * @brief   EnvironmentalService constructor.
00038      * @param   _ble Reference to BLE device.
00039      */
00040     EnvironmentalService(BLE& _ble) :
00041         ble(_ble),
00042         temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
00043         humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
00044         pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
00045     {
00046         static bool serviceAdded = false; /* We should only ever need to add the information service once. */
00047         if (serviceAdded) {
00048             return;
00049         }
00050 
00051         GattCharacteristic *charTable[] = { &humidityCharacteristic,
00052                                             &pressureCharacteristic,
00053                                             &temperatureCharacteristic };
00054 
00055         GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00056 
00057         ble.gattServer().addService(environmentalService);
00058         serviceAdded = true;
00059     }
00060 
00061     /**
00062      * @brief   Update humidity characteristic.
00063      * @param   newHumidityVal New humidity measurement.
00064      */
00065     void updateHumidity(HumidityType_t newHumidityVal)
00066     {
00067         humidity = (HumidityType_t) (newHumidityVal * 100);
00068         ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
00069     }
00070 
00071     /**
00072      * @brief   Update pressure characteristic.
00073      * @param   newPressureVal New pressure measurement.
00074      */
00075     void updatePressure(PressureType_t newPressureVal)
00076     {
00077         pressure = (PressureType_t) (newPressureVal * 10);
00078         ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
00079     }
00080 
00081     /**
00082      * @brief   Update temperature characteristic.
00083      * @param   newTemperatureVal New temperature measurement.
00084      */
00085     void updateTemperature(float newTemperatureVal)
00086     {
00087         temperature = (TemperatureType_t) (newTemperatureVal * 100);
00088         ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
00089     }
00090 
00091 private:
00092     BLE& ble;
00093 
00094     TemperatureType_t temperature;
00095     HumidityType_t    humidity;
00096     PressureType_t    pressure;
00097 
00098     ReadOnlyGattCharacteristic<TemperatureType_t>  temperatureCharacteristic;
00099     ReadOnlyGattCharacteristic<HumidityType_t>     humidityCharacteristic;
00100     ReadOnlyGattCharacteristic<PressureType_t>     pressureCharacteristic;
00101 };
00102 
00103 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/