jgh

Dependents:   Migration

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Mon Nov 02 09:09:07 2015 +0000
Revision:
863:4f8e12333453
Child:
864:2bcdb60b537e
Synchronized with git rev e1bce4e5
Author: Rohit Grover
Merge branch 'master' of https://github.com/xcrespo/ble into xcrespo-master

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 863:4f8e12333453 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 863:4f8e12333453 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 863:4f8e12333453 32 /**
rgrover1 863:4f8e12333453 33 * @brief EnvironmentalService constructor.
rgrover1 863:4f8e12333453 34 * @param ble Reference to BLE device.
rgrover1 863:4f8e12333453 35 * @param temperature_en Enable this characteristic.
rgrover1 863:4f8e12333453 36 * @param humidity_en Enable this characteristic.
rgrover1 863:4f8e12333453 37 * @param pressure_en Enable this characteristic.
rgrover1 863:4f8e12333453 38 */
rgrover1 863:4f8e12333453 39 EnvironmentalService(BLE &_ble,
rgrover1 863:4f8e12333453 40 bool temperature_en = false,
rgrover1 863:4f8e12333453 41 bool humidity_en = false,
rgrover1 863:4f8e12333453 42 bool pressure_en = false) :
rgrover1 863:4f8e12333453 43 ble(_ble),
rgrover1 863:4f8e12333453 44 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR,
rgrover1 863:4f8e12333453 45 (uint8_t *) &temperature,
rgrover1 863:4f8e12333453 46 (temperature_en) ? 2 : 0, // minLength
rgrover1 863:4f8e12333453 47 (temperature_en) ? 2 : 0, // maxLength
rgrover1 863:4f8e12333453 48 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
rgrover1 863:4f8e12333453 49 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR,
rgrover1 863:4f8e12333453 50 (uint8_t *) &humidity,
rgrover1 863:4f8e12333453 51 (humidity_en) ? 2 : 0, // minLength
rgrover1 863:4f8e12333453 52 (humidity_en) ? 2 : 0, // maxLength
rgrover1 863:4f8e12333453 53 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
rgrover1 863:4f8e12333453 54 pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR,
rgrover1 863:4f8e12333453 55 (uint8_t *) &pressure,
rgrover1 863:4f8e12333453 56 (pressure_en) ? 4 : 0, // minLength
rgrover1 863:4f8e12333453 57 (pressure_en) ? 4 : 0, // maxLength
rgrover1 863:4f8e12333453 58 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
rgrover1 863:4f8e12333453 59 {
rgrover1 863:4f8e12333453 60 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
rgrover1 863:4f8e12333453 61 if (serviceAdded) {
rgrover1 863:4f8e12333453 62 return;
rgrover1 863:4f8e12333453 63 }
rgrover1 863:4f8e12333453 64
rgrover1 863:4f8e12333453 65 GattCharacteristic *charTable[] = { &humidityCharacteristic,
rgrover1 863:4f8e12333453 66 &pressureCharacteristic,
rgrover1 863:4f8e12333453 67 &temperatureCharacteristic };
rgrover1 863:4f8e12333453 68
rgrover1 863:4f8e12333453 69 GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable,
rgrover1 863:4f8e12333453 70 sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover1 863:4f8e12333453 71
rgrover1 863:4f8e12333453 72 ble.gattServer().addService(environmentalService);
rgrover1 863:4f8e12333453 73 serviceAdded = true;
rgrover1 863:4f8e12333453 74 }
rgrover1 863:4f8e12333453 75
rgrover1 863:4f8e12333453 76 /**
rgrover1 863:4f8e12333453 77 * @brief Update humidity characteristic.
rgrover1 863:4f8e12333453 78 * @param newHumidityVal New humidity measurement.
rgrover1 863:4f8e12333453 79 */
rgrover1 863:4f8e12333453 80 void updateHumidity(uint16_t newHumidityVal)
rgrover1 863:4f8e12333453 81 {
rgrover1 863:4f8e12333453 82 humidity = (uint32_t) (newHumidityVal*100);
rgrover1 863:4f8e12333453 83 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, 2);
rgrover1 863:4f8e12333453 84 }
rgrover1 863:4f8e12333453 85
rgrover1 863:4f8e12333453 86 /**
rgrover1 863:4f8e12333453 87 * @brief Update pressure characteristic.
rgrover1 863:4f8e12333453 88 * @param newPressureVal New pressure measurement.
rgrover1 863:4f8e12333453 89 */
rgrover1 863:4f8e12333453 90 void updatePressure(uint32_t newPressureVal)
rgrover1 863:4f8e12333453 91 {
rgrover1 863:4f8e12333453 92 pressure = (uint32_t) (newPressureVal*10);
rgrover1 863:4f8e12333453 93 ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, 4);
rgrover1 863:4f8e12333453 94 }
rgrover1 863:4f8e12333453 95
rgrover1 863:4f8e12333453 96 /**
rgrover1 863:4f8e12333453 97 * @brief Update temperature characteristic.
rgrover1 863:4f8e12333453 98 * @param newTemperatureVal New temperature measurement.
rgrover1 863:4f8e12333453 99 */
rgrover1 863:4f8e12333453 100 void updateTemperature(float newTemperatureVal)
rgrover1 863:4f8e12333453 101 {
rgrover1 863:4f8e12333453 102 temperature = (int16_t) (newTemperatureVal*100);
rgrover1 863:4f8e12333453 103 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, 2);
rgrover1 863:4f8e12333453 104 }
rgrover1 863:4f8e12333453 105
rgrover1 863:4f8e12333453 106 private:
rgrover1 863:4f8e12333453 107 BLE &ble;
rgrover1 863:4f8e12333453 108
rgrover1 863:4f8e12333453 109 int16_t temperature;
rgrover1 863:4f8e12333453 110 GattCharacteristic temperatureCharacteristic;
rgrover1 863:4f8e12333453 111 uint16_t humidity;
rgrover1 863:4f8e12333453 112 GattCharacteristic humidityCharacteristic;
rgrover1 863:4f8e12333453 113 uint32_t pressure;
rgrover1 863:4f8e12333453 114 GattCharacteristic pressureCharacteristic;
rgrover1 863:4f8e12333453 115 };
rgrover1 863:4f8e12333453 116
rgrover1 863:4f8e12333453 117 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/