Updated

Fork of BLE_API by Bluetooth Low Energy

Committer:
vbahl2
Date:
Tue May 09 03:03:23 2017 +0000
Revision:
1210:1f56dc951637
Parent:
1036:7af23cdcfcac
Child:
1042:21a86ac7f5b1
j

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 906:8ddab3271c2f 1 /* mbed Microcontroller Library
rgrover1 906:8ddab3271c2f 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 906:8ddab3271c2f 3 *
rgrover1 906:8ddab3271c2f 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 906:8ddab3271c2f 5 * you may not use this file except in compliance with the License.
rgrover1 906:8ddab3271c2f 6 * You may obtain a copy of the License at
rgrover1 906:8ddab3271c2f 7 *
rgrover1 906:8ddab3271c2f 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 906:8ddab3271c2f 9 *
rgrover1 906:8ddab3271c2f 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 906:8ddab3271c2f 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 906:8ddab3271c2f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 906:8ddab3271c2f 13 * See the License for the specific language governing permissions and
rgrover1 906:8ddab3271c2f 14 * limitations under the License.
rgrover1 906:8ddab3271c2f 15 */
rgrover1 906:8ddab3271c2f 16
rgrover1 906:8ddab3271c2f 17 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
rgrover1 906:8ddab3271c2f 18 #define __BLE_ENVIRONMENTAL_SERVICE_H__
rgrover1 906:8ddab3271c2f 19
rgrover1 906:8ddab3271c2f 20 #include "ble/BLE.h"
rgrover1 906:8ddab3271c2f 21
rgrover1 906:8ddab3271c2f 22 /**
rgrover1 906:8ddab3271c2f 23 * @class EnvironmentalService
rgrover1 1036:7af23cdcfcac 24 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
rgrover1 1036:7af23cdcfcac 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
rgrover1 1036:7af23cdcfcac 26 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
rgrover1 1036:7af23cdcfcac 27 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
rgrover1 906:8ddab3271c2f 28 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
rgrover1 906:8ddab3271c2f 29 */
rgrover1 906:8ddab3271c2f 30 class EnvironmentalService {
rgrover1 906:8ddab3271c2f 31 public:
rgrover1 906:8ddab3271c2f 32 typedef int16_t TemperatureType_t;
rgrover1 906:8ddab3271c2f 33 typedef uint16_t HumidityType_t;
rgrover1 906:8ddab3271c2f 34 typedef uint32_t PressureType_t;
rgrover1 906:8ddab3271c2f 35
rgrover1 906:8ddab3271c2f 36 /**
rgrover1 906:8ddab3271c2f 37 * @brief EnvironmentalService constructor.
rgrover1 906:8ddab3271c2f 38 * @param ble Reference to BLE device.
rgrover1 906:8ddab3271c2f 39 * @param temperature_en Enable this characteristic.
rgrover1 906:8ddab3271c2f 40 * @param humidity_en Enable this characteristic.
rgrover1 906:8ddab3271c2f 41 * @param pressure_en Enable this characteristic.
rgrover1 906:8ddab3271c2f 42 */
rgrover1 906:8ddab3271c2f 43 EnvironmentalService(BLE& _ble) :
rgrover1 906:8ddab3271c2f 44 ble(_ble),
rgrover1 906:8ddab3271c2f 45 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
rgrover1 906:8ddab3271c2f 46 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
rgrover1 906:8ddab3271c2f 47 pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
rgrover1 906:8ddab3271c2f 48 {
rgrover1 906:8ddab3271c2f 49 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
rgrover1 906:8ddab3271c2f 50 if (serviceAdded) {
rgrover1 906:8ddab3271c2f 51 return;
rgrover1 906:8ddab3271c2f 52 }
rgrover1 906:8ddab3271c2f 53
rgrover1 906:8ddab3271c2f 54 GattCharacteristic *charTable[] = { &humidityCharacteristic,
rgrover1 906:8ddab3271c2f 55 &pressureCharacteristic,
rgrover1 906:8ddab3271c2f 56 &temperatureCharacteristic };
rgrover1 906:8ddab3271c2f 57
rgrover1 906:8ddab3271c2f 58 GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 906:8ddab3271c2f 59
rgrover1 906:8ddab3271c2f 60 ble.gattServer().addService(environmentalService);
rgrover1 906:8ddab3271c2f 61 serviceAdded = true;
rgrover1 906:8ddab3271c2f 62 }
rgrover1 906:8ddab3271c2f 63
rgrover1 906:8ddab3271c2f 64 /**
rgrover1 906:8ddab3271c2f 65 * @brief Update humidity characteristic.
rgrover1 906:8ddab3271c2f 66 * @param newHumidityVal New humidity measurement.
rgrover1 906:8ddab3271c2f 67 */
rgrover1 906:8ddab3271c2f 68 void updateHumidity(HumidityType_t newHumidityVal)
rgrover1 906:8ddab3271c2f 69 {
rgrover1 906:8ddab3271c2f 70 humidity = (HumidityType_t) (newHumidityVal * 100);
rgrover1 906:8ddab3271c2f 71 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
rgrover1 906:8ddab3271c2f 72 }
rgrover1 906:8ddab3271c2f 73
rgrover1 906:8ddab3271c2f 74 /**
rgrover1 906:8ddab3271c2f 75 * @brief Update pressure characteristic.
rgrover1 906:8ddab3271c2f 76 * @param newPressureVal New pressure measurement.
rgrover1 906:8ddab3271c2f 77 */
rgrover1 906:8ddab3271c2f 78 void updatePressure(PressureType_t newPressureVal)
rgrover1 906:8ddab3271c2f 79 {
rgrover1 906:8ddab3271c2f 80 pressure = (PressureType_t) (newPressureVal * 10);
rgrover1 906:8ddab3271c2f 81 ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
rgrover1 906:8ddab3271c2f 82 }
rgrover1 906:8ddab3271c2f 83
rgrover1 906:8ddab3271c2f 84 /**
rgrover1 906:8ddab3271c2f 85 * @brief Update temperature characteristic.
rgrover1 906:8ddab3271c2f 86 * @param newTemperatureVal New temperature measurement.
rgrover1 906:8ddab3271c2f 87 */
rgrover1 906:8ddab3271c2f 88 void updateTemperature(float newTemperatureVal)
rgrover1 906:8ddab3271c2f 89 {
rgrover1 906:8ddab3271c2f 90 temperature = (TemperatureType_t) (newTemperatureVal * 100);
rgrover1 906:8ddab3271c2f 91 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
rgrover1 906:8ddab3271c2f 92 }
rgrover1 906:8ddab3271c2f 93
rgrover1 906:8ddab3271c2f 94 private:
rgrover1 906:8ddab3271c2f 95 BLE& ble;
rgrover1 906:8ddab3271c2f 96
rgrover1 906:8ddab3271c2f 97 TemperatureType_t temperature;
rgrover1 906:8ddab3271c2f 98 HumidityType_t humidity;
rgrover1 906:8ddab3271c2f 99 PressureType_t pressure;
rgrover1 906:8ddab3271c2f 100
rgrover1 906:8ddab3271c2f 101 ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
rgrover1 906:8ddab3271c2f 102 ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
rgrover1 906:8ddab3271c2f 103 ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
rgrover1 906:8ddab3271c2f 104 };
rgrover1 906:8ddab3271c2f 105
rgrover1 906:8ddab3271c2f 106 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/