From b760b3f98ca786bbe0d586e30fed2847525b0ae4 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Tue, 28 Apr 2020 11:21:35 +0100 Subject: Add a simple service to allow setting the time from a controlling application --- src/Components/Ble/NimbleController.cpp | 3 ++ src/Components/Ble/NimbleController.h | 2 + src/Components/Ble/PinetimeService.cpp | 73 +++++++++++++++++++++++++++++++++ src/Components/Ble/PinetimeService.h | 50 ++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 src/Components/Ble/PinetimeService.cpp create mode 100644 src/Components/Ble/PinetimeService.h (limited to 'src/Components/Ble') diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 02f99180..77bab5de 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -74,6 +74,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 99e0c811..d315e97b 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 { @@ -35,6 +36,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; + }; + } +} -- cgit v1.2.3 From 49a9a93ceffac325b008bc9fbfc41d571fa7e110 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Tue, 28 Apr 2020 18:31:58 +0100 Subject: Add a very basic alert nofification service --- src/Components/Ble/AlertNotificationService.cpp | 73 +++++++++++++++++++++++++ src/Components/Ble/AlertNotificationService.h | 39 +++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/Components/Ble/AlertNotificationService.cpp create mode 100644 src/Components/Ble/AlertNotificationService.h (limited to 'src/Components/Ble') diff --git a/src/Components/Ble/AlertNotificationService.cpp b/src/Components/Ble/AlertNotificationService.cpp new file mode 100644 index 00000000..8e3b712d --- /dev/null +++ b/src/Components/Ble/AlertNotificationService.cpp @@ -0,0 +1,73 @@ + +#include +#include "NotificationManager.h" +#include + +#include "AlertNotificationService.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t AlertNotificationService::ansUuid; +constexpr ble_uuid16_t AlertNotificationService::ansCharUuid; + + +int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto anService = static_cast(arg); + return anService->OnAlert(conn_handle, attr_handle, ctxt); +} + +void AlertNotificationService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + +AlertNotificationService::AlertNotificationService ( Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager ) : m_systemTask{systemTask}, m_notificationManager{notificationManager}, + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &ansCharUuid, + .access_cb = AlertNotificationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &ansUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } +{ +} + +int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); + uint8_t data[notifSize + 1]; + data[notifSize] = '\0'; + os_mbuf_copydata(ctxt->om, 0, notifSize, data); + char *s = (char *) &data[3]; + NRF_LOG_INFO("DATA : %s", s); + + for(int i = 0; i <= notifSize; i++) + { + if(s[i] == 0x00) + { + s[i] = 0x0A; + } + } + + m_notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, s, notifSize + 1); + m_systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); + } + return 0; +} diff --git a/src/Components/Ble/AlertNotificationService.h b/src/Components/Ble/AlertNotificationService.h new file mode 100644 index 00000000..53cb44cc --- /dev/null +++ b/src/Components/Ble/AlertNotificationService.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include + +namespace Pinetime { + namespace Controllers { + class AlertNotificationService { + public: + AlertNotificationService(Pinetime::System::SystemTask &systemTask, + Pinetime::Controllers::NotificationManager ¬ificationManager); + void Init(); + + int OnAlert(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + + private: + static constexpr uint16_t ansId {0x1811}; + static constexpr uint16_t ansCharId {0x2a46}; + + static constexpr ble_uuid16_t ansUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ansId + }; + + static constexpr ble_uuid16_t ansCharUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ansCharId + }; + + struct ble_gatt_chr_def characteristicDefinition[2]; + struct ble_gatt_svc_def serviceDefinition[2]; + + Pinetime::System::SystemTask &m_systemTask; + NotificationManager &m_notificationManager; + }; + } +} -- cgit v1.2.3 From d33be52bc9cec10e530e1390f1cf812c12cd830e Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Tue, 28 Apr 2020 18:39:26 +0100 Subject: Run the alert notification service and simplify trhe pinetime service initialization --- src/CMakeLists.txt | 1 + src/Components/Ble/NimbleController.cpp | 7 +++++-- src/Components/Ble/NimbleController.h | 3 +++ src/Components/Ble/PinetimeService.cpp | 23 +++++++++-------------- src/Components/Ble/PinetimeService.h | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src/Components/Ble') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2e14c02b..777faefc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -320,6 +320,7 @@ list(APPEND SOURCE_FILES Components/Ble/CurrentTimeClient.cpp Components/Ble/AlertNotificationClient.cpp Components/Ble/PinetimeService.cpp + Components/Ble/AlertNotificationService.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 77bab5de..eb765b80 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -30,7 +30,9 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, dateTimeController{dateTimeController}, notificationManager{notificationManager}, currentTimeClient{dateTimeController}, - alertNotificationClient{systemTask, notificationManager} { + alertNotificationClient{systemTask, notificationManager}, + anService{systemTask, notificationManager}, + pinetimeService{dateTimeController} { } @@ -75,7 +77,8 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); pinetimeService.Init(); - pinetimeService.setDateTimeController(&dateTimeController); + + anService.Init(); int res; res = ble_hs_util_ensure_addr(0); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index d315e97b..820b1c77 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -1,6 +1,7 @@ #pragma once #include +#include "AlertNotificationService.h" #include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" @@ -35,8 +36,10 @@ namespace Pinetime { DeviceInformationService deviceInformationService; CurrentTimeClient currentTimeClient; + AlertNotificationService anService; AlertNotificationClient alertNotificationClient; PinetimeService pinetimeService; + uint8_t addrType; uint16_t connectionHandle; }; diff --git a/src/Components/Ble/PinetimeService.cpp b/src/Components/Ble/PinetimeService.cpp index 5bcb36d1..e18d78aa 100644 --- a/src/Components/Ble/PinetimeService.cpp +++ b/src/Components/Ble/PinetimeService.cpp @@ -24,22 +24,21 @@ int PinetimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, 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); + 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); + 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)); - 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() : +PinetimeService::PinetimeService(DateTime &dateTimeController) : m_dateTimeController{dateTimeController}, characteristicDefinition{ { .uuid = (ble_uuid_t *) &timeUuid, @@ -67,7 +66,3 @@ PinetimeService::PinetimeService() : } -void PinetimeService::setDateTimeController(DateTime *dateTimeController) -{ - m_dateTimeController = dateTimeController; -} diff --git a/src/Components/Ble/PinetimeService.h b/src/Components/Ble/PinetimeService.h index d4f8ee2b..0cae8345 100644 --- a/src/Components/Ble/PinetimeService.h +++ b/src/Components/Ble/PinetimeService.h @@ -8,7 +8,7 @@ namespace Pinetime { namespace Controllers { class PinetimeService { public: - PinetimeService(); + PinetimeService(DateTime &dateTimeController); void Init(); int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, @@ -44,7 +44,7 @@ namespace Pinetime { uint8_t reason; } CtsData; - DateTime *m_dateTimeController = nullptr; + DateTime &m_dateTimeController; }; } } -- cgit v1.2.3 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/CMakeLists.txt | 2 +- src/Components/Ble/CurrentTimeService.cpp | 69 +++++++++++++++++++++++++++++++ src/Components/Ble/CurrentTimeService.h | 48 +++++++++++++++++++++ src/Components/Ble/NimbleController.cpp | 4 +- src/Components/Ble/NimbleController.h | 4 +- src/Components/Ble/PinetimeService.cpp | 68 ------------------------------ src/Components/Ble/PinetimeService.h | 50 ---------------------- 7 files changed, 122 insertions(+), 123 deletions(-) create mode 100644 src/Components/Ble/CurrentTimeService.cpp create mode 100644 src/Components/Ble/CurrentTimeService.h delete mode 100644 src/Components/Ble/PinetimeService.cpp delete mode 100644 src/Components/Ble/PinetimeService.h (limited to 'src/Components/Ble') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 777faefc..4b1f7c16 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -319,7 +319,7 @@ list(APPEND SOURCE_FILES Components/Ble/DeviceInformationService.cpp Components/Ble/CurrentTimeClient.cpp Components/Ble/AlertNotificationClient.cpp - Components/Ble/PinetimeService.cpp + Components/Ble/CurrentTimeService.cpp Components/Ble/AlertNotificationService.cpp drivers/Cst816s.cpp FreeRTOS/port.c 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 + }, + } + { + +} + diff --git a/src/Components/Ble/CurrentTimeService.h b/src/Components/Ble/CurrentTimeService.h new file mode 100644 index 00000000..58bc5ba6 --- /dev/null +++ b/src/Components/Ble/CurrentTimeService.h @@ -0,0 +1,48 @@ +#pragma once +#include +#include +#include +#include + +namespace Pinetime { + namespace Controllers { + class CurrentTimeService { + public: + CurrentTimeService(DateTime &dateTimeController); + void Init(); + + int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + private: + static constexpr uint16_t ctsId {0x1805}; + static constexpr uint16_t ctsCharId {0x2a2b}; + + static constexpr ble_uuid16_t ctsUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ctsId + }; + + static constexpr ble_uuid16_t ctChrUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ctsCharId + }; + + 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; + }; + } +} diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index eb765b80..826d255b 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -32,7 +32,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, currentTimeClient{dateTimeController}, alertNotificationClient{systemTask, notificationManager}, anService{systemTask, notificationManager}, - pinetimeService{dateTimeController} { + currentTimeService{dateTimeController} { } @@ -76,7 +76,7 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); - pinetimeService.Init(); + currentTimeService.Init(); anService.Init(); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 820b1c77..44fbbe2c 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -5,7 +5,7 @@ #include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" -#include "PinetimeService.h" +#include "CurrentTimeService.h" #include namespace Pinetime { @@ -38,7 +38,7 @@ namespace Pinetime { CurrentTimeClient currentTimeClient; AlertNotificationService anService; AlertNotificationClient alertNotificationClient; - PinetimeService pinetimeService; + CurrentTimeService currentTimeService; uint8_t addrType; uint16_t connectionHandle; diff --git a/src/Components/Ble/PinetimeService.cpp b/src/Components/Ble/PinetimeService.cpp deleted file mode 100644 index e18d78aa..00000000 --- a/src/Components/Ble/PinetimeService.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#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) { - 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(DateTime &dateTimeController) : m_dateTimeController{dateTimeController}, - 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 - }, - } - { - -} - diff --git a/src/Components/Ble/PinetimeService.h b/src/Components/Ble/PinetimeService.h deleted file mode 100644 index 0cae8345..00000000 --- a/src/Components/Ble/PinetimeService.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace Pinetime { - namespace Controllers { - class PinetimeService { - public: - PinetimeService(DateTime &dateTimeController); - 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; - }; - } -} -- cgit v1.2.3 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') 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 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') 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 From d6c6ac4cf5801e17caf7bfc0878423703ed0413b Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 16 May 2020 16:13:22 +0200 Subject: Remove reference to NRF Softdevice in CMake and documentation. Update documentation. Remove Asserts when starting advertising to prevent crash (known bug). Set version 0.5.0. --- CMakeLists.txt | 2 +- README.md | 26 ++----- cmake-nRF5x/CMake_nRF5x.cmake | 124 +------------------------------- src/Components/Ble/NimbleController.cpp | 13 +++- src/main.cpp | 1 - 5 files changed, 19 insertions(+), 147 deletions(-) (limited to 'src/Components/Ble') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5af971f0..f803cecc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(pinetime VERSION 0.4.0 LANGUAGES C CXX ASM) +project(pinetime VERSION 0.5.0 LANGUAGES C CXX ASM) set(NRF_TARGET "nrf52") diff --git a/README.md b/README.md index 790e3de1..4c970f8e 100644 --- a/README.md +++ b/README.md @@ -27,14 +27,16 @@ I've tested this project on the actual PineTime hardware. * Project builds and runs on the Pinetime; * Logs available via JLink RTT; * SPI (DMA & IRQ based) LCD driver; - * BLE advertising, connection and bonding; + * Open source BLE stack : [NimBLE](https://github.com/apache/mynewt-nimble); + * BLE advertising and connection connection; * BLE CTS client (retrieves the time from the connected device if it implements a CTS server); * Push button to go to disable screen (and go to low power mode) / enable screen (and wake-up) and UI navigation * Touch panel support; * Rich user interface (using [LittleVGL](https://littlevgl.com/)) via display, touchpanel and push button. * Digital watch face and 4 demo applications (spinning meter, analog gauche, push button and message box); * Watchdog (automatic reset in case of firmware crash) and reset support (push and hold the button for 7 - 10s); - * BLE Notification support (still Work-In-Progress, [companion app](https://github.com/JF002/gobbledegook) needed). + * BLE Notification support (still Work-In-Progress, [companion app](https://github.com/JF002/gobbledegook) needed); + * Supported by companion app [Amazfish](https://openrepos.net/content/piggz/amazfish) (time synchronization and notifications are integrated). ## Documentation @@ -77,10 +79,6 @@ See [this page](./doc/PinetimeStubWithNrf52DK.md) - -DOPENOCD_BIN_PATH=[path to openocd] - * Optionally, you can define MERGEHEX with the path to the ```mergehex``` tool from [NRF5X Command Line Tools](https://infocenter.nordicsemi.com/index.jsp?topic=%2Fug_nrf5x_cltools%2FUG%2Fcltools%2Fnrf5x_command_line_tools_lpage.html&cp=6_1) to be able to merge the application and softdevice into one HEX file. In this case the merged file is generated in src/pinetime-app-full.hex - - - -DMERGEHEX=[Path to the mergehex executable] - JLINK ``` $ mkdir build @@ -116,18 +114,6 @@ $ make -j pinetime-app $ make FLASH_ERASE ``` -* Flash softdevice & application - -``` -$ make FLASH_SOFTDEVICE -$ make FLASH_pinetime-app -``` - -Or, with ```mergehex``` - -``` -$ make FLASH_MERGED_pinetime-app -``` * For your information : list make targets : @@ -208,7 +194,7 @@ $ JLinkRTTClient - https://github.com/eliotstock/memory : display the memory usage (FLASH/RAM) using the .map file from GCC. -## BLE connection, bonding and time synchronization +## BLE connection and time synchronization At runtime, BLE advertising is started. You can then use a smartphone or computer to connect and bond to your Pinetime. As soon as a device is bonded, Pinetime will look for a **CTS** server (**C**urrent **T**ime **S**ervice) on the connected device. @@ -223,7 +209,7 @@ Here is how to do it with an Android smartphone running NRFConnect: - Select server configuration "Current Time Service" and tap OK * Go back to the main screen and scan for BLE devices. A device called "PineTime" should appear * Tap the button "Connect" next to the PineTime device. It should connect to the PineTime and switch to a new tab. -* On this tab, on the top right, there is a 3 dots button. Tap on it and select Bond. The bonding process begins, and if it is sucessful, the PineTime should update its time and display it on the screen. +* If a CTS server is found, the Pinetime should update its time with the time provided by the server. ### Using Linux and bluetoothctl * Ensure that your bluetooth controller is enabled and working fine. I've tested this on a x86 Debian computer and on a RaspberryPi 3. diff --git a/cmake-nRF5x/CMake_nRF5x.cmake b/cmake-nRF5x/CMake_nRF5x.cmake index ec4b47db..c1785d36 100755 --- a/cmake-nRF5x/CMake_nRF5x.cmake +++ b/cmake-nRF5x/CMake_nRF5x.cmake @@ -5,10 +5,6 @@ if (NOT NRF5_SDK_PATH) message(FATAL_ERROR "The path to the nRF5 SDK (NRF5_SDK_PATH) must be set.") endif () -#if (NOT NRFJPROG) -# message(FATAL_ERROR "The path to the nrfjprog utility (NRFJPROG) must be set.") -#endif () - # convert toolchain path to bin path if(DEFINED ARM_NONE_EABI_TOOLCHAIN_PATH) set(ARM_NONE_EABI_TOOLCHAIN_BIN_PATH ${ARM_NONE_EABI_TOOLCHAIN_PATH}/bin) @@ -70,23 +66,15 @@ macro(nRF5x_setup) endif() set(CPU_FLAGS "-mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16") add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 -DNRF52_PAN_12 -DNRF52_PAN_58 -DNRF52_PAN_54 -DNRF52_PAN_31 -DNRF52_PAN_51 -DNRF52_PAN_36 -DNRF52_PAN_15 -DNRF52_PAN_20 -DNRF52_PAN_55 -DBOARD_PCA10040) -# add_definitions(-DSOFTDEVICE_PRESENT -DS132 -DSWI_DISABLE0 -DBLE_STACK_SUPPORT_REQD -DNRF_SD_BLE_API_VERSION=6) add_definitions(-DFREERTOS) add_definitions(-DDEBUG_NRF_USER) -# add_definitions(-D__STARTUP_CLEAR_BSS) -# add_definitions(-D__HEAP_SIZE=8192) -# add_definitions(-D__STACK_SIZE=2048) - include_directories( -# "${NRF5_SDK_PATH}/components/softdevice/s132/headers" -# "${NRF5_SDK_PATH}/components/softdevice/s132/headers/nrf52" "${NRF5_SDK_PATH}/components/drivers_nrf/nrf_soc_nosd" ) list(APPEND SDK_SOURCE_FILES "${NRF5_SDK_PATH}/modules/nrfx/mdk/system_nrf52.c" "${NRF5_SDK_PATH}/modules/nrfx/mdk/gcc_startup_nrf52.S" ) -# set(SOFTDEVICE_PATH "${NRF5_SDK_PATH}/components/softdevice/s132/hex/s132_nrf52_6.1.1_softdevice.hex") endif () set(COMMON_FLAGS "-MP -MD -mthumb -mabi=aapcs -Wall -g3 -ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin --short-enums ${CPU_FLAGS} -Wreturn-type -Werror=return-type") @@ -243,7 +231,6 @@ macro(nRF5x_setup) # Other external include_directories( "${NRF5_SDK_PATH}/external/fprintf/" -# "${NRF5_SDK_PATH}/external/utf_converter/" ) list(APPEND SDK_SOURCE_FILES @@ -255,103 +242,25 @@ macro(nRF5x_setup) # LCD/GFX include_directories( "${NRF5_SDK_PATH}/external/thedotfactory_fonts" -# "${NRF5_SDK_PATH}/components/ble/ble_db_discovery" ) - list(APPEND SDK_SOURCE_FILES -# "${NRF5_SDK_PATH}/components/ble/ble_db_discovery/ble_db_discovery.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_cts_c/ble_cts_c.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_ans_c/ble_ans_c.c" -# "${NRF5_SDK_PATH}/external/thedotfactory_fonts/orkney24pts.c" - ) - - #BLE S132 -# include_directories( -# "${NRF5_SDK_PATH}/components/ble/common" -# "${NRF5_SDK_PATH}/components/ble/ble_advertising" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt" -# "${NRF5_SDK_PATH}/components/libraries/sensorsim" -# "${NRF5_SDK_PATH}/components/ble/peer_manager" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr" -# ) - - LIST(APPEND SDK_SOURCE_FILES -# "${NRF5_SDK_PATH}//components/ble/common/ble_srv_common.c" -# "${NRF5_SDK_PATH}/components/ble/ble_advertising/ble_advertising.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_advdata.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas/ble_bas.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs/ble_hrs.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis/ble_dis.c" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt/nrf_ble_gatt.c" -# "${NRF5_SDK_PATH}/components/libraries/sensorsim/sensorsim.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr/nrf_ble_qwr.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/auth_status_tracker.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/gatt_cache_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/gatts_cache_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/id_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_data_storage.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_database.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_id.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager_handler.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/pm_buffer.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/security_dispatcher.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/security_manager.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_params.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" -# "${NRF5_SDK_PATH}/components/libraries/atomic_flags/nrf_atflags.c" -# "${NRF5_SDK_PATH}/components/libraries/fds/fds.c" -# "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage.c" -# "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage_sd.c" -# "${NRF5_SDK_PATH}/components/libraries/atomic_fifo/nrf_atfifo.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_ble.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_freertos.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_soc.c" -# "${NRF5_SDK_PATH}/components/libraries/experimental_section_vars/nrf_section_iter.c" -# "${NRF5_SDK_PATH}/components/libraries/bsp/bsp_btn_ble.c" -# "${NRF5_SDK_PATH}/components/libraries/hardfault/hardfault_implementation.c" -# "${NRF5_SDK_PATH}/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c" - ) - LIST(APPEND SDK_SOURCE_FILES "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_twi.c" ) - # adds target for erasing and flashing the board with a softdevice + # adds target for erasing if(USE_JLINK) - add_custom_target(FLASH_SOFTDEVICE - COMMAND ${NRFJPROG} --program ${SOFTDEVICE_PATH} -f ${NRF_TARGET} --sectorerase - COMMAND sleep 0.5s - COMMAND ${NRFJPROG} --reset -f ${NRF_TARGET} - COMMENT "flashing SoftDevice" - ) - add_custom_target(FLASH_ERASE COMMAND ${NRFJPROG} --eraseall -f ${NRF_TARGET} COMMENT "erasing flashing" ) elseif(USE_GDB_CLIENT) - add_custom_target(FLASH_SOFTDEVICE - COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'load' -ex 'kill' ${SOFTDEVICE_PATH} - COMMENT "flashing SoftDevice" - ) add_custom_target(FLASH_ERASE COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'mon erase_mass' COMMENT "erasing flashing" ) elseif(USE_OPENOCD) - add_custom_target(FLASH_SOFTDEVICE - COMMAND ${OPENOCD_BIN_PATH} -c "tcl_port disabled" -c "gdb_port 3333" -c "telnet_port 4444" -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c "program \"${SOFTDEVICE_PATH}\"" -c reset -c shutdown - COMMENT "flashing SoftDevice" - ) - add_custom_target(FLASH_ERASE + add_custom_target(FLASH_ERASE COMMAND ${OPENOCD_BIN_PATH} -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c init -c halt -c 'nrf5 mass_erase' -c reset -c shutdown COMMENT "erasing flashing" ) @@ -392,35 +301,6 @@ macro(nRF5x_addExecutable EXECUTABLE_NAME SOURCE_FILES) COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_NAME}.out "${EXECUTABLE_NAME}.hex" COMMENT "post build steps for ${EXECUTABLE_NAME}") - if(MERGEHEX) - add_custom_command(TARGET ${EXECUTABLE_NAME} - POST_BUILD - COMMAND ${MERGEHEX} --merge ${EXECUTABLE_NAME}.hex ${NRF5_SDK_PATH}/components/softdevice/s132/hex/s132_nrf52_6.1.1_softdevice.hex --output ${EXECUTABLE_NAME}-full.hex - COMMENT "merging HEX files") - - if(USE_JLINK) - add_custom_target("FLASH_MERGED_${EXECUTABLE_NAME}" - DEPENDS ${EXECUTABLE_NAME} - COMMAND ${NRFJPROG} --program ${EXECUTABLE_NAME}-full.hex -f ${NRF_TARGET} --sectorerase - COMMAND sleep 0.5s - COMMAND ${NRFJPROG} --reset -f ${NRF_TARGET} - COMMENT "flashing ${EXECUTABLE_NAME}-full.hex" - ) - elseif(USE_GDB_CLIENT) - add_custom_target("FLASH_MERGED_${EXECUTABLE_NAME}" - DEPENDS ${EXECUTABLE_NAME} - COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'load' -ex 'kill' ${EXECUTABLE_NAME}-full.hex - COMMENT "flashing ${EXECUTABLE_NAME}-full.hex" - ) - elseif(USE_OPENOCD) - add_custom_target("FLASH_MERGED_${EXECUTABLE_NAME}" - DEPENDS ${EXECUTABLE_NAME} - COMMAND ${OPENOCD_BIN_PATH} -c "tcl_port disabled" -c "gdb_port 3333" -c "telnet_port 4444" -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c "program \"${EXECUTABLE_NAME}-full.hex\"" -c reset -c shutdown - COMMENT "flashing ${EXECUTABLE_NAME}-full.hex" - ) - endif() - endif() - # custom target for flashing the board if(USE_JLINK) add_custom_target("FLASH_${EXECUTABLE_NAME}" diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 172bcb9f..2fe03571 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -126,14 +126,21 @@ void NimbleController::StartAdvertising() { int res; res = ble_gap_adv_set_fields(&fields); - ASSERT(res == 0); + //ASSERT(res == 0); res = ble_gap_adv_rsp_set_fields(&rsp_fields); - ASSERT(res == 0); + //ASSERT(res == 0); res = ble_gap_adv_start(addrType, NULL, 10000, &adv_params, GAPEventCallback, this); - ASSERT(res == 0); + //ASSERT(res == 0); + + // TODO I've disabled these ASSERT as they sometime asserts and reset the mcu. + // For now, the advertising is restarted as soon as it ends. There may be a race condition + // that prevent the advertising from restarting reliably. + // I remove the assert to prevent this uncesseray crash, but in the long term, the management of + // the advertising should be improve (better error handling, and advertise for 3 minutes after + // the application has been woken up, for example. } int OnAllSvrDisco(uint16_t conn_handle, diff --git a/src/main.cpp b/src/main.cpp index e0e9b65e..797495bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -195,7 +195,6 @@ void nimble_port_init(void) { } void nimble_port_ll_task_func(void *args) { -// extern void ble_ll_task(void *arg); ble_ll_task(args); } } -- cgit v1.2.3