InfiniTime.git

ref: 9e9e348a617da2629ae9ff6ff5686586690e9cfb

src/components/ble/CurrentTimeService.cpp


  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "components/ble/CurrentTimeService.h"
#include <nrf_log.h>

using namespace Pinetime::Controllers;

constexpr ble_uuid16_t CurrentTimeService::ctsUuid;
constexpr ble_uuid16_t CurrentTimeService::ctsCtChrUuid;
constexpr ble_uuid16_t CurrentTimeService::ctsLtChrUuid;

// 0005yyxx-78fc-48fe-8e23-433b3a1942d0
constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
  return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128},
                        .value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, y, x, 0x05, 0x00}};
}

constexpr ble_uuid128_t ctsWorldTimeChrUuid {CharUuid(0x00, 0x01)};

int CTSCallback(uint16_t /*conn_handle*/, uint16_t /*attr_handle*/, struct ble_gatt_access_ctxt* ctxt, void* arg) {
  auto cts = static_cast<CurrentTimeService*>(arg);

  return cts->OnCurrentTimeServiceAccessed(ctxt);
}

int CurrentTimeService::OnCurrentTimeServiceAccessed(struct ble_gatt_access_ctxt* ctxt) {
  switch (ble_uuid_u16(ctxt->chr->uuid)) {
    case ctsCurrentTimeCharId:
      return OnCurrentTimeAccessed(ctxt);
    case ctsLocalTimeCharId:
      return OnLocalTimeAccessed(ctxt);
  }
  if (ble_uuid_cmp(ctxt->chr->uuid, &ctsWorldTimeChrUuid.u) == 0) {
    return OnWorldTimeAccessed(ctxt);
  }
  return -1; // Unknown characteristic
}

void CurrentTimeService::Init() {
  int res;
  res = ble_gatts_count_cfg(serviceDefinition);
  ASSERT(res == 0);

  res = ble_gatts_add_svcs(serviceDefinition);

  ASSERT(res == 0);
}

int CurrentTimeService::OnCurrentTimeAccessed(struct ble_gatt_access_ctxt* ctxt) {

  NRF_LOG_INFO("Setting time...");

  if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
    CtsCurrentTimeData result;
    int res = os_mbuf_copydata(ctxt->om, 0, sizeof(CtsCurrentTimeData), &result);
    if (res < 0) {
      NRF_LOG_ERROR("Error reading BLE Data writing to CTS Current Time (too little data)")
      return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
    }

    uint16_t year = ((uint16_t) result.year_MSO << 8) + result.year_LSO;

    NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", year, result.month, result.dayofmonth, result.hour, result.minute, result.second);

    m_dateTimeController.SetTime(year, result.month, result.dayofmonth, result.hour, result.minute, result.second);

  } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
    CtsCurrentTimeData currentDateTime;
    currentDateTime.year_LSO = m_dateTimeController.Year() & 0xff;
    currentDateTime.year_MSO = (m_dateTimeController.Year() >> 8) & 0xff;
    currentDateTime.month = static_cast<u_int8_t>(m_dateTimeController.Month());
    currentDateTime.dayofmonth = m_dateTimeController.Day();
    currentDateTime.hour = m_dateTimeController.Hours();
    currentDateTime.minute = m_dateTimeController.Minutes();
    currentDateTime.second = m_dateTimeController.Seconds();
    currentDateTime.fractions256 = 0;

    int res = os_mbuf_append(ctxt->om, &currentDateTime, sizeof(CtsCurrentTimeData));
    return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  }

  return 0;
}

int CurrentTimeService::OnLocalTimeAccessed(struct ble_gatt_access_ctxt* ctxt) {
  NRF_LOG_INFO("Setting timezone...");

  if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
    CtsLocalTimeData result;
    int res = os_mbuf_copydata(ctxt->om, 0, sizeof(CtsLocalTimeData), &result);

    if (res < 0) {
      NRF_LOG_ERROR("Error reading BLE Data writing to CTS Local Time (too little data)")
      return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
    }

    NRF_LOG_INFO("Received data: %d %d", result.timezone, result.dst);

    m_dateTimeController.SetTimeZone(result.timezone, result.dst);

  } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
    CtsLocalTimeData currentTimezone;
    currentTimezone.timezone = m_dateTimeController.TzOffset();
    currentTimezone.dst = m_dateTimeController.DstOffset();

    int res = os_mbuf_append(ctxt->om, &currentTimezone, sizeof(currentTimezone));
    return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  }

  return 0;
}

int CurrentTimeService::OnWorldTimeAccessed(struct ble_gatt_access_ctxt* ctxt) {
  NRF_LOG_INFO("Setting world time...");

  if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
    int res = os_mbuf_copydata(ctxt->om, 0, sizeof(worldTimeData), &worldTimeData);

    if (res < 0) {
      NRF_LOG_ERROR("Error reading BLE Data writing to CTS Local Time (too little data)")
      return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
    }

    for (size_t i = 0; i < DateTime::NUMBER_OF_WORLD_TIMES; i++) {
      m_dateTimeController.updateWorldTime(i, worldTimeData[i].offsetUtc, worldTimeData[i].description);
    }
  }

  return 0;
}

CurrentTimeService::CurrentTimeService(DateTime& dateTimeController)
  : characteristicDefinition {

                              {.uuid = &ctsLtChrUuid.u,
                               .access_cb = CTSCallback,
                               .arg = this,
                               .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ},

                               {.uuid = &ctsCtChrUuid.u,
                               .access_cb = CTSCallback,
                               .arg = this,
                               .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ},

                               {.uuid = &ctsWorldTimeChrUuid.u,
                               .access_cb = CTSCallback,
                               .arg = this,
                               .flags = BLE_GATT_CHR_F_WRITE},

                               {0}},
    serviceDefinition {
      {/* Device Information Service */
       .type = BLE_GATT_SVC_TYPE_PRIMARY,
       .uuid = &ctsUuid.u,
       .characteristics = characteristicDefinition},
      {0},
    },
    m_dateTimeController {dateTimeController} {
}