From 206bdbf5eb94ac967284862b5d7a270ed50195db Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Mon, 4 May 2020 21:43:51 +0100 Subject: Move PinetimeService to CurrentTimeService witha view to implement CTS for time handling --- src/Components/Ble/CurrentTimeService.cpp | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/Components/Ble/CurrentTimeService.cpp (limited to 'src/Components/Ble/CurrentTimeService.cpp') diff --git a/src/Components/Ble/CurrentTimeService.cpp b/src/Components/Ble/CurrentTimeService.cpp new file mode 100644 index 00000000..0af20dd3 --- /dev/null +++ b/src/Components/Ble/CurrentTimeService.cpp @@ -0,0 +1,69 @@ +#include "CurrentTimeService.h" +#include + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t CurrentTimeService::ctsUuid; +constexpr ble_uuid16_t CurrentTimeService::ctChrUuid; + + +int CTSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto cts = static_cast(arg); + return cts->OnTimeAccessed(conn_handle, attr_handle, ctxt); +} + +void CurrentTimeService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int CurrentTimeService::OnTimeAccessed(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) { + CtsData result; + os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); + + NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, + result.month, result.dayofmonth, + result.hour, result.minute, result.second); + + m_dateTimeController.SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + + } + //!TODO need to support reading the time. + return 0; +} + +CurrentTimeService::CurrentTimeService(DateTime &dateTimeController) : m_dateTimeController{dateTimeController}, + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &ctChrUuid, + .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 = (ble_uuid_t *) &ctsUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + -- cgit v1.2.3-70-g09d2 From b4e82dd11f11611911d371ce41c01c69ca8f3de3 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Tue, 5 May 2020 20:53:31 +0100 Subject: Add support for reading the current time --- src/Components/Ble/CurrentTimeService.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/Components/Ble/CurrentTimeService.cpp') diff --git a/src/Components/Ble/CurrentTimeService.cpp b/src/Components/Ble/CurrentTimeService.cpp index 0af20dd3..a44a18a5 100644 --- a/src/Components/Ble/CurrentTimeService.cpp +++ b/src/Components/Ble/CurrentTimeService.cpp @@ -34,7 +34,22 @@ int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handl m_dateTimeController.SetTime(result.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) { + CtsData currentDateTime; + currentDateTime.year = m_dateTimeController.Year(); + currentDateTime.month = static_cast(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; + } + //!TODO need to support reading the time. return 0; } -- cgit v1.2.3-70-g09d2 From ca0ef77368855459fae6cbcef93545aa3d2d69af Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Thu, 7 May 2020 20:06:50 +0100 Subject: TODO is done --- src/Components/Ble/CurrentTimeService.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/Components/Ble/CurrentTimeService.cpp') diff --git a/src/Components/Ble/CurrentTimeService.cpp b/src/Components/Ble/CurrentTimeService.cpp index a44a18a5..9ae4c3b8 100644 --- a/src/Components/Ble/CurrentTimeService.cpp +++ b/src/Components/Ble/CurrentTimeService.cpp @@ -50,7 +50,6 @@ int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handl } - //!TODO need to support reading the time. return 0; } -- cgit v1.2.3-70-g09d2