EnvironmentalSensingService.h 3.77 KB
/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__
#define __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__

#include "BLEDevice.h"

/**
* @class EnvironmentalSensingService
* @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
* Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
* Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
* Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
* Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
*/
class EnvironmentalSensingService {
public:
    const static uint16_t UUID_ENVIRONMENTAL_SENSING_SERVICE = 0x181A;
    const static uint16_t UUID_HUMIDITY_CHAR = 0x2A6F;
    const static uint16_t UUID_PRESSURE_CHAR = 0x2A6D;
    
    typedef uint16_t HumidityType_t;
    typedef uint32_t PressureType_t;

    /**
     * @brief   EnvironmentalService constructor.
     * @param   ble Reference to BLE device.
     */
    EnvironmentalSensingService(BLEDevice& _ble) :
        ble(_ble),
        humidityCharacteristic(UUID_HUMIDITY_CHAR, &humidity, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        pressureCharacteristic(UUID_PRESSURE_CHAR, &pressure, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
    {
        static bool serviceAdded = false; /* We should only ever need to add the information service once. */
        if (serviceAdded) {
            return;
        }

        GattCharacteristic *charTable[] = { &pressureCharacteristic,
                                            &humidityCharacteristic                                             
                                            };

        GattService environmentalSensingService(UUID_ENVIRONMENTAL_SENSING_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));

        ble.addService(environmentalSensingService);
        serviceAdded = true;
    }

    /**
     * @brief   Update humidity characteristic.
     * @param   newHumidityVal New humidity measurement.
     */
    void updateHumidity(double newHumidityVal)
    {
        humidity = (HumidityType_t) (newHumidityVal * 100);
        ble.updateCharacteristicValue(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
    }

    /**
     * @brief   Update pressure characteristic.
     * @param   newPressureVal New pressure measurement.
     */
    void updatePressure(double newPressureVal)
    {
        pressure = (PressureType_t) (newPressureVal * 10);
        ble.updateCharacteristicValue(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
    }


private:
    BLEDevice& ble;

    HumidityType_t    humidity;
    PressureType_t    pressure;

    ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
    ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
};

#endif /* #ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__*/