2020-05-04 16:43:51 -04:00
|
|
|
#include "CurrentTimeService.h"
|
2020-04-28 06:21:35 -04:00
|
|
|
#include <hal/nrf_rtc.h>
|
2021-02-02 16:09:00 -05:00
|
|
|
#include <nrf_log.h>
|
2020-04-28 06:21:35 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2020-05-04 16:43:51 -04:00
|
|
|
constexpr ble_uuid16_t CurrentTimeService::ctsUuid;
|
|
|
|
constexpr ble_uuid16_t CurrentTimeService::ctChrUuid;
|
2020-04-28 06:21:35 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int CTSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
2020-05-04 16:43:51 -04:00
|
|
|
auto cts = static_cast<CurrentTimeService*>(arg);
|
|
|
|
return cts->OnTimeAccessed(conn_handle, attr_handle, ctxt);
|
2020-04-28 06:21:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:43:51 -04:00
|
|
|
void CurrentTimeService::Init() {
|
2020-06-16 14:36:24 -04:00
|
|
|
int res;
|
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
|
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
2020-04-28 06:21:35 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
|
2020-04-28 06:21:35 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
NRF_LOG_INFO("Setting time...");
|
2020-04-28 06:21:35 -04:00
|
|
|
|
|
|
|
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
2020-04-28 13:39:26 -04:00
|
|
|
CtsData result;
|
|
|
|
os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result);
|
2020-04-28 06:21:35 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
NRF_LOG_INFO(
|
|
|
|
"Received data: %d-%d-%d %d:%d:%d", result.year, result.month, result.dayofmonth, result.hour, result.minute, result.second);
|
2020-04-28 13:39:26 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
m_dateTimeController.SetTime(
|
|
|
|
result.year, result.month, result.dayofmonth, 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG));
|
2020-04-28 06:21:35 -04:00
|
|
|
|
2020-05-05 15:53:31 -04:00
|
|
|
} else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
|
|
|
CtsData currentDateTime;
|
|
|
|
currentDateTime.year = m_dateTimeController.Year();
|
|
|
|
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.millis = 0;
|
|
|
|
|
|
|
|
int res = os_mbuf_append(ctxt->om, ¤tDateTime, sizeof(CtsData));
|
|
|
|
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
2020-04-28 06:21:35 -04:00
|
|
|
}
|
2020-05-05 15:53:31 -04:00
|
|
|
|
2020-04-28 06:21:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
CurrentTimeService::CurrentTimeService(DateTime& dateTimeController)
|
2021-07-24 11:40:06 -04:00
|
|
|
: characteristicDefinition {{.uuid = &ctChrUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.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,
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &ctsUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
},
|
|
|
|
m_dateTimeController {dateTimeController} {
|
2020-04-28 06:21:35 -04:00
|
|
|
}
|