Blame view

Mbed_compiler/Capteur_pollution/CurrentTimeService.h 3.15 KB
1b3a0906   mbutaye   Ajout des fichier...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  #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__*/