just a fork

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Mon Nov 02 09:09:08 2015 +0000
Revision:
866:a2a40c69eec0
Parent:
865:0f44d2aaf201
Synchronized with git rev be09e516
Author: Rohit Grover
use types like ReadOnlyGattCharacteristic<TemperatureType_t>

Who changed what in which revision?

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