InfiniTime.git

ref: fdc89f1a20889c0295534f58dc2e82ce8c55a18b

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
#include "components/ble/CurrentTimeService.h"
#include <hal/nrf_rtc.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;

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(conn_handle, attr_handle, ctxt);
}

int CurrentTimeService::OnCurrentTimeServiceAccessed(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
  switch (ble_uuid_u16(ctxt->chr->uuid)) {
    case ctsCurrentTimeCharId:
      return OnCurrentTimeAccessed(conn_handle, attr_handle, ctxt);
    case ctsLocalTimeCharId:
      return OnLocalTimeAccessed(conn_handle, attr_handle, 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(uint16_t conn_handle, uint16_t attr_handle, 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, 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG));

  } 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(uint16_t conn_handle, uint16_t attr_handle, 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;
}

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},

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