diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ad83caee..ede2bade 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -319,6 +319,7 @@ list(APPEND SOURCE_FILES Components/Ble/DeviceInformationService.cpp Components/Ble/CurrentTimeClient.cpp Components/Ble/AlertNotificationClient.cpp + Components/Ble/PinetimeService.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 7894ff43..6a8257b8 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -72,6 +72,9 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); + pinetimeService.Init(); + pinetimeService.setDateTimeController(&dateTimeController); + int res; res = ble_hs_util_ensure_addr(0); res = ble_hs_id_infer_auto(0, &addrType); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 7a7a94c9..dffd80c0 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -4,6 +4,7 @@ #include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" +#include "PinetimeService.h" #include namespace Pinetime { @@ -34,6 +35,7 @@ namespace Pinetime { DeviceInformationService deviceInformationService; CurrentTimeClient currentTimeClient; AlertNotificationClient alertNotificationClient; + PinetimeService pinetimeService; uint8_t addrType; uint16_t connectionHandle; }; diff --git a/src/Components/Ble/PinetimeService.cpp b/src/Components/Ble/PinetimeService.cpp new file mode 100644 index 00000000..5bcb36d1 --- /dev/null +++ b/src/Components/Ble/PinetimeService.cpp @@ -0,0 +1,73 @@ +#include "PinetimeService.h" +#include + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t PinetimeService::pinetimeUuid; +constexpr ble_uuid16_t PinetimeService::timeUuid; + + +int PinetimeTimeCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto pinetimeService = static_cast(arg); + return pinetimeService->OnTimeAccessed(conn_handle, attr_handle, ctxt); +} + +void PinetimeService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int PinetimeService::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) { + if (m_dateTimeController) { + 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)); + } + } + return 0; +} + +PinetimeService::PinetimeService() : + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &timeUuid, + .access_cb = PinetimeTimeCallback, + + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &pinetimeUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + +void PinetimeService::setDateTimeController(DateTime *dateTimeController) +{ + m_dateTimeController = dateTimeController; +} diff --git a/src/Components/Ble/PinetimeService.h b/src/Components/Ble/PinetimeService.h new file mode 100644 index 00000000..d4f8ee2b --- /dev/null +++ b/src/Components/Ble/PinetimeService.h @@ -0,0 +1,50 @@ +#pragma once +#include +#include +#include +#include + +namespace Pinetime { + namespace Controllers { + class PinetimeService { + public: + PinetimeService(); + void Init(); + + int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + void setDateTimeController(DateTime *dateTimeController); + + private: + static constexpr uint16_t pinetimeId {0x6666}; + static constexpr uint16_t timeCharId {0x6667}; + + static constexpr ble_uuid16_t pinetimeUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = pinetimeId + }; + + static constexpr ble_uuid16_t timeUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = timeCharId + }; + + struct ble_gatt_chr_def characteristicDefinition[2]; + struct ble_gatt_svc_def serviceDefinition[2]; + + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; + + DateTime *m_dateTimeController = nullptr; + }; + } +}