CurrentTimeService.h 3.15 KB
#ifndef __BLE_CURRENT_TIME_SERVICE_H__
#define __BLE_CURRENT_TIME_SERVICE_H__

#include "BLEDevice.h"

/**
* @class CurrentTimeService
* @brief BLE Time Service. This service provides the current time and date.
* Service:  https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml
* Date Time:  https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.date_time.xml
* Day of Week:  https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.day_of_week.xml
*/
class CurrentTimeService {
public:
    const static uint16_t UUID_CURRENT_TIME_SERVICE = 0x1805;
    const static uint16_t UUID_DATE_TIME_CHAR = 0x2A08;
    const static uint16_t UUID_DAY_OF_WEEK_CHAR = 0x2A09;
    
    typedef uint8_t DayOfWeekType_t;
    typedef struct DateTimeType_t{ 
        uint16_t year;
        uint8_t month;
        uint8_t day;
        uint8_t hours;
        uint8_t minutes;
        uint8_t seconds;
    }  DateTimeType_t;

    /**
     * @brief   CurrentTimeService constructor.
     * @param   ble Reference to BLE device.
     */
    CurrentTimeService(BLEDevice& _ble) :
        ble(_ble),
        dateTimeCharacteristic(UUID_DATE_TIME_CHAR, &dateTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
        dayOfWeekCharacteristic(UUID_DAY_OF_WEEK_CHAR, &dayOfWeek, 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[] = { &dateTimeCharacteristic,
                                            &dayOfWeekCharacteristic };

        GattService currentTimeService(UUID_CURRENT_TIME_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));

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

    /**
     * @brief   Update date time characteristic.
     * @param   newDateTimeVal New date time measurement.
     */
    void updateDateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds)
    {
        dateTime = (DateTimeType_t) {
            (uint16_t)year,
            (uint8_t)month,
            (uint8_t)day,
            (uint8_t)hours,
            (uint8_t)minutes,
            (uint8_t)seconds
        };
        ble.updateCharacteristicValue(dateTimeCharacteristic.getValueHandle(), (uint8_t *) &dateTime, sizeof(DateTimeType_t));
    }
    
    /**
     * @brief   Update day of week characteristic.
     * @param   newDayOfWeekVal New day of week measurement.
     */
    void updateDayOfWeek(DayOfWeekType_t day)
    {
        dayOfWeek = (DayOfWeekType_t)day;
        ble.updateCharacteristicValue(dayOfWeekCharacteristic.getValueHandle(), (uint8_t *) &dayOfWeek, sizeof(DayOfWeekType_t));
    }

private:
    BLEDevice& ble;

    DateTimeType_t dateTime;
    DayOfWeekType_t dayOfWeek;

    ReadOnlyGattCharacteristic<DateTimeType_t> dateTimeCharacteristic;
    ReadOnlyGattCharacteristic<DayOfWeekType_t> dayOfWeekCharacteristic;
};

#endif /* #ifndef __BLE_CURRENT_TIME_SERVICE_H__*/