diff options
author | JF <jf@codingfield.com> | 2020-05-17 10:29:13 +0200 |
---|---|---|
committer | Gitea <gitea@fake.local> | 2020-05-17 10:29:13 +0200 |
commit | 8a94750e30399bfb204cbec59a769d9d1b6b5baa (patch) | |
tree | 8a1a58beae54e238d28aff116c900f3b428b7db4 /src/Components | |
parent | 86d5732b960fbe7f81ed711b2de7e6b79293c96a (diff) | |
parent | be1ad9b07083e656a649d223750ff4b14b781b7b (diff) |
Merge branch 'develop' of JF/PineTime into master
Diffstat (limited to 'src/Components')
-rw-r--r-- | src/Components/Ble/AlertNotificationClient.cpp | 138 | ||||
-rw-r--r-- | src/Components/Ble/AlertNotificationClient.h | 82 | ||||
-rw-r--r-- | src/Components/Ble/AlertNotificationService.cpp | 73 | ||||
-rw-r--r-- | src/Components/Ble/AlertNotificationService.h | 39 | ||||
-rw-r--r-- | src/Components/Ble/BleController.h | 2 | ||||
-rw-r--r-- | src/Components/Ble/CurrentTimeClient.cpp | 77 | ||||
-rw-r--r-- | src/Components/Ble/CurrentTimeClient.h | 55 | ||||
-rw-r--r-- | src/Components/Ble/CurrentTimeService.cpp | 83 | ||||
-rw-r--r-- | src/Components/Ble/CurrentTimeService.h | 48 | ||||
-rw-r--r-- | src/Components/Ble/DeviceInformationService.cpp | 101 | ||||
-rw-r--r-- | src/Components/Ble/DeviceInformationService.h | 67 | ||||
-rw-r--r-- | src/Components/Ble/DfuService.cpp | 101 | ||||
-rw-r--r-- | src/Components/Ble/DfuService.h | 67 | ||||
-rw-r--r-- | src/Components/Ble/NimbleController.cpp | 316 | ||||
-rw-r--r-- | src/Components/Ble/NimbleController.h | 47 |
15 files changed, 1295 insertions, 1 deletions
diff --git a/src/Components/Ble/AlertNotificationClient.cpp b/src/Components/Ble/AlertNotificationClient.cpp new file mode 100644 index 00000000..6e096353 --- /dev/null +++ b/src/Components/Ble/AlertNotificationClient.cpp @@ -0,0 +1,138 @@ +#include <SystemTask/SystemTask.h> +#include "NotificationManager.h" + +#include "AlertNotificationClient.h" + + +using namespace Pinetime::Controllers; +constexpr ble_uuid16_t AlertNotificationClient::ansServiceUuid; + +constexpr ble_uuid16_t AlertNotificationClient::supportedNewAlertCategoryUuid; +constexpr ble_uuid16_t AlertNotificationClient::supportedUnreadAlertCategoryUuid ; +constexpr ble_uuid16_t AlertNotificationClient::newAlertUuid; +constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid; +constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid; + +int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) { + auto client = static_cast<AlertNotificationClient*>(arg); + return client->OnNewAlertSubcribe(conn_handle, error, attr); +} + +AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NotificationManager& notificationManager) : + systemTask{systemTask}, notificationManager{notificationManager}{ + +} + +bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("ANS Discovery complete"); + return true; + } + + if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ansServiceUuid), &service->uuid.u) == 0) { + NRF_LOG_INFO("ANS discovered : 0x%x", service->start_handle); + ansStartHandle = service->start_handle; + ansEndHandle = service->end_handle; + isDiscovered = true; + } + return false; +} + +void AlertNotificationClient::Init() { + +} + +int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(error->status != 0 && error->status != BLE_HS_EDONE) { + NRF_LOG_INFO("ANS Characteristic discovery ERROR"); + return 0; + } + + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("ANS Characteristic discovery complete"); + } else { + if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : supportedNewAlertCategoryUuid"); + supportedNewAlertCategoryHandle = characteristic->val_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : supportedUnreadAlertCategoryUuid"); + supportedUnreadAlertCategoryHandle = characteristic->val_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&newAlertUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : newAlertUuid"); + newAlertHandle = characteristic->val_handle; + newAlertDefHandle = characteristic->def_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&unreadAlertStatusUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : unreadAlertStatusUuid"); + unreadAlertStatusHandle = characteristic->val_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&controlPointUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : controlPointUuid"); + controlPointHandle = characteristic->val_handle; + }else + NRF_LOG_INFO("ANS Characteristic discovered : 0x%x", characteristic->val_handle); + } + return 0; +} + +int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error, + ble_gatt_attr *attribute) { + if(error->status == 0) { + NRF_LOG_INFO("ANS New alert subscribe OK"); + } else { + NRF_LOG_INFO("ANS New alert subscribe ERROR"); + } + + return 0; +} + +int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, + const ble_gatt_dsc *descriptor) { + if(error->status == 0) { + if(characteristicValueHandle == newAlertHandle && ble_uuid_cmp(((ble_uuid_t*)&newAlertUuid), &descriptor->uuid.u)) { + if(newAlertDescriptorHandle == 0) { + NRF_LOG_INFO("ANS Descriptor discovered : %d", descriptor->handle); + newAlertDescriptorHandle = descriptor->handle; + uint8_t value[2]; + value[0] = 1; + value[1] = 0; + ble_gattc_write_flat(connectionHandle, newAlertDescriptorHandle, value, sizeof(value), NewAlertSubcribeCallback, this); + } + } + } + return 0; +} + +void AlertNotificationClient::OnNotification(ble_gap_event *event) { + if(event->notify_rx.attr_handle == newAlertHandle) { + size_t notifSize = OS_MBUF_PKTLEN(event->notify_rx.om); + uint8_t data[notifSize + 1]; + data[notifSize] = '\0'; + os_mbuf_copydata(event->notify_rx.om, 0, notifSize, data); + char *s = (char *) &data[2]; + NRF_LOG_INFO("DATA : %s", s); + + notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, s, notifSize + 1); + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); + } +} + +bool AlertNotificationClient::IsDiscovered() const { + return isDiscovered; +} + +uint16_t AlertNotificationClient::StartHandle() const { + return ansStartHandle; +} + +uint16_t AlertNotificationClient::EndHandle() const { + return ansEndHandle; +} + +uint16_t AlertNotificationClient::NewAlerthandle() const { + return newAlertHandle; +} diff --git a/src/Components/Ble/AlertNotificationClient.h b/src/Components/Ble/AlertNotificationClient.h new file mode 100644 index 00000000..7a085b7e --- /dev/null +++ b/src/Components/Ble/AlertNotificationClient.h @@ -0,0 +1,82 @@ +#pragma once + +#include <cstdint> +#include <array> +#include <host/ble_gap.h> + + +namespace Pinetime { + namespace Controllers { + int NewAlertSubcribeCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg); + + class AlertNotificationClient { + public: + explicit AlertNotificationClient(Pinetime::System::SystemTask &systemTask, + Pinetime::Controllers::NotificationManager ¬ificationManager); + void Init(); + + bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); + int OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute); + int OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); + void OnNotification(ble_gap_event *event); + bool IsDiscovered() const; + uint16_t StartHandle() const; + uint16_t EndHandle() const; + + static constexpr const ble_uuid16_t &Uuid() { return ansServiceUuid; } + + uint16_t NewAlerthandle() const; + private: + static constexpr uint16_t ansServiceId{0x1811}; + static constexpr uint16_t supportedNewAlertCategoryId = 0x2a47; + static constexpr uint16_t supportedUnreadAlertCategoryId = 0x2a48; + static constexpr uint16_t newAlertId = 0x2a46; + static constexpr uint16_t unreadAlertStatusId = 0x2a45; + static constexpr uint16_t controlPointId = 0x2a44; + + static constexpr ble_uuid16_t ansServiceUuid{ + .u {.type = BLE_UUID_TYPE_16}, + .value = ansServiceId + }; + static constexpr ble_uuid16_t supportedNewAlertCategoryUuid{ + .u {.type = BLE_UUID_TYPE_16}, + .value = supportedNewAlertCategoryId + }; + static constexpr ble_uuid16_t supportedUnreadAlertCategoryUuid{ + .u {.type = BLE_UUID_TYPE_16}, + .value = supportedUnreadAlertCategoryId + }; + static constexpr ble_uuid16_t newAlertUuid{ + .u {.type = BLE_UUID_TYPE_16}, + .value = newAlertId + }; + static constexpr ble_uuid16_t unreadAlertStatusUuid{ + .u {.type = BLE_UUID_TYPE_16}, + .value = unreadAlertStatusId + }; + static constexpr ble_uuid16_t controlPointUuid{ + .u {.type = BLE_UUID_TYPE_16}, + .value = controlPointId + }; + + uint16_t ansStartHandle; + uint16_t ansEndHandle; + uint16_t supportedNewAlertCategoryHandle; + uint16_t supportedUnreadAlertCategoryHandle; + uint16_t newAlertHandle; + uint16_t newAlertDescriptorHandle = 0; + uint16_t newAlertDefHandle; + uint16_t unreadAlertStatusHandle; + uint16_t controlPointHandle; + bool isDiscovered = false; + Pinetime::System::SystemTask &systemTask; + Pinetime::Controllers::NotificationManager ¬ificationManager; + }; + } +}
\ No newline at end of file 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 <hal/nrf_rtc.h> +#include "NotificationManager.h" +#include <SystemTask/SystemTask.h> + +#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<AlertNotificationService*>(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 <cstdint> +#include <array> +#include <host/ble_gap.h> + +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; + }; + } +} diff --git a/src/Components/Ble/BleController.h b/src/Components/Ble/BleController.h index 31d66986..f2bd77e0 100644 --- a/src/Components/Ble/BleController.h +++ b/src/Components/Ble/BleController.h @@ -1,6 +1,6 @@ #pragma once -#include <FreeRTOS.h>> +#include <FreeRTOS.h> #include <queue.h> namespace Pinetime { diff --git a/src/Components/Ble/CurrentTimeClient.cpp b/src/Components/Ble/CurrentTimeClient.cpp new file mode 100644 index 00000000..fdebc084 --- /dev/null +++ b/src/Components/Ble/CurrentTimeClient.cpp @@ -0,0 +1,77 @@ +#include <hal/nrf_rtc.h> +#include "CurrentTimeClient.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid; +constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid; + +CurrentTimeClient::CurrentTimeClient(DateTime& dateTimeController) : dateTimeController{dateTimeController} { + +} + +void CurrentTimeClient::Init() { + +} + +bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("CTS Discovery complete"); + return true; + } + + if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsServiceUuid), &service->uuid.u) == 0) { + NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); + isDiscovered = true; + ctsStartHandle = service->start_handle; + ctsEndHandle = service->end_handle; + return false; + } + return false; +} + +int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("CTS Characteristic discovery complete"); + return 0; + } + + if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); + currentTimeHandle = characteristic->val_handle; + } + return 0; +} + +int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) { + if(error->status == 0) { + // TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent + CtsData result; + os_mbuf_copydata(attribute->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); + dateTimeController.SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + } else { + NRF_LOG_INFO("Error retrieving current time: %d", error->status); + } + return 0; +} + +bool CurrentTimeClient::IsDiscovered() const { + return isDiscovered; +} + +uint16_t CurrentTimeClient::StartHandle() const { + return ctsStartHandle; +} + +uint16_t CurrentTimeClient::EndHandle() const { + return ctsEndHandle; +} + +uint16_t CurrentTimeClient::CurrentTimeHandle() const { + return currentTimeHandle; +} diff --git a/src/Components/Ble/CurrentTimeClient.h b/src/Components/Ble/CurrentTimeClient.h new file mode 100644 index 00000000..2278ef15 --- /dev/null +++ b/src/Components/Ble/CurrentTimeClient.h @@ -0,0 +1,55 @@ +#pragma once +#include <cstdint> +#include <array> +#include <Components/DateTime/DateTimeController.h> +#include <host/ble_gap.h> + +namespace Pinetime { + namespace Controllers { + + class CurrentTimeClient { + public: + explicit CurrentTimeClient(DateTime& dateTimeController); + void Init(); + bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); + int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); + bool IsDiscovered() const; + uint16_t StartHandle() const; + uint16_t EndHandle() const; + uint16_t CurrentTimeHandle() const; + static constexpr const ble_uuid16_t* Uuid() { return &CurrentTimeClient::ctsServiceUuid; } + static constexpr const ble_uuid16_t* CurrentTimeCharacteristicUuid() { return &CurrentTimeClient::currentTimeCharacteristicUuid; } + private: + 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; + + static constexpr uint16_t ctsServiceId {0x1805}; + static constexpr uint16_t currentTimeCharacteristicId {0x2a2b}; + + static constexpr ble_uuid16_t ctsServiceUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ctsServiceId + }; + static constexpr ble_uuid16_t currentTimeCharacteristicUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = currentTimeCharacteristicId + }; + + uint16_t currentTimeHandle; + DateTime& dateTimeController; + bool isDiscovered = false; + uint16_t ctsStartHandle; + uint16_t ctsEndHandle; + }; + } +}
\ No newline at end of file diff --git a/src/Components/Ble/CurrentTimeService.cpp b/src/Components/Ble/CurrentTimeService.cpp new file mode 100644 index 00000000..9ae4c3b8 --- /dev/null +++ b/src/Components/Ble/CurrentTimeService.cpp @@ -0,0 +1,83 @@ +#include "CurrentTimeService.h" +#include <hal/nrf_rtc.h> + +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<CurrentTimeService*>(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)); + + } 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; + + } + + 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 <cstdint> +#include <array> +#include <Components/DateTime/DateTimeController.h> +#include <host/ble_gap.h> + +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/DeviceInformationService.cpp b/src/Components/Ble/DeviceInformationService.cpp new file mode 100644 index 00000000..3099b1bf --- /dev/null +++ b/src/Components/Ble/DeviceInformationService.cpp @@ -0,0 +1,101 @@ +#include "DeviceInformationService.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t DeviceInformationService::manufacturerNameUuid; +constexpr ble_uuid16_t DeviceInformationService::modelNumberUuid; +constexpr ble_uuid16_t DeviceInformationService::serialNumberUuid; +constexpr ble_uuid16_t DeviceInformationService::fwRevisionUuid; +constexpr ble_uuid16_t DeviceInformationService::deviceInfoUuid; +constexpr ble_uuid16_t DeviceInformationService::hwRevisionUuid; + +int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto deviceInformationService = static_cast<DeviceInformationService*>(arg); + return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt); +} + +void DeviceInformationService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + const char *str; + + switch (ble_uuid_u16(ctxt->chr->uuid)) { + case manufacturerNameId: + str = manufacturerName; + break; + case modelNumberId: + str = modelNumber; + break; + case serialNumberId: + str = serialNumber; + break; + case fwRevisionId: + str = fwRevision; + break; + case hwRevisionId: + str = hwRevision; + break; + default: + return BLE_ATT_ERR_UNLIKELY; + } + + int res = os_mbuf_append(ctxt->om, str, strlen(str)); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + +DeviceInformationService::DeviceInformationService() : + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &manufacturerNameUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &modelNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &serialNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &fwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &hwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &deviceInfoUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + diff --git a/src/Components/Ble/DeviceInformationService.h b/src/Components/Ble/DeviceInformationService.h new file mode 100644 index 00000000..6249893d --- /dev/null +++ b/src/Components/Ble/DeviceInformationService.h @@ -0,0 +1,67 @@ +#pragma once +#include <cstdint> +#include <array> + +#include <host/ble_gap.h> + +namespace Pinetime { + namespace Controllers { + class DeviceInformationService { + public: + DeviceInformationService(); + void Init(); + + int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + private: + static constexpr uint16_t deviceInfoId {0x180a}; + static constexpr uint16_t manufacturerNameId {0x2a29}; + static constexpr uint16_t modelNumberId {0x2a24}; + static constexpr uint16_t serialNumberId {0x2a25}; + static constexpr uint16_t fwRevisionId {0x2a26}; + static constexpr uint16_t hwRevisionId {0x2a27}; + + static constexpr char* manufacturerName = "Codingfield"; + static constexpr char* modelNumber = "1"; + static constexpr char* serialNumber = "9.8.7.6.5.4"; + static constexpr char* fwRevision = "0.5.0"; + static constexpr char* hwRevision = "1.0.0"; + + static constexpr ble_uuid16_t deviceInfoUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = deviceInfoId + }; + + static constexpr ble_uuid16_t manufacturerNameUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = manufacturerNameId + }; + + static constexpr ble_uuid16_t modelNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = modelNumberId + }; + + static constexpr ble_uuid16_t serialNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = serialNumberId + }; + + static constexpr ble_uuid16_t fwRevisionUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = fwRevisionId + }; + + static constexpr ble_uuid16_t hwRevisionUuid { + .u {.type = BLE_UUID_TYPE_16}, + .value = hwRevisionId + }; + + struct ble_gatt_chr_def characteristicDefinition[6]; + struct ble_gatt_svc_def serviceDefinition[2]; + + + }; + } +}
\ No newline at end of file diff --git a/src/Components/Ble/DfuService.cpp b/src/Components/Ble/DfuService.cpp new file mode 100644 index 00000000..3099b1bf --- /dev/null +++ b/src/Components/Ble/DfuService.cpp @@ -0,0 +1,101 @@ +#include "DeviceInformationService.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t DeviceInformationService::manufacturerNameUuid; +constexpr ble_uuid16_t DeviceInformationService::modelNumberUuid; +constexpr ble_uuid16_t DeviceInformationService::serialNumberUuid; +constexpr ble_uuid16_t DeviceInformationService::fwRevisionUuid; +constexpr ble_uuid16_t DeviceInformationService::deviceInfoUuid; +constexpr ble_uuid16_t DeviceInformationService::hwRevisionUuid; + +int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto deviceInformationService = static_cast<DeviceInformationService*>(arg); + return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt); +} + +void DeviceInformationService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + const char *str; + + switch (ble_uuid_u16(ctxt->chr->uuid)) { + case manufacturerNameId: + str = manufacturerName; + break; + case modelNumberId: + str = modelNumber; + break; + case serialNumberId: + str = serialNumber; + break; + case fwRevisionId: + str = fwRevision; + break; + case hwRevisionId: + str = hwRevision; + break; + default: + return BLE_ATT_ERR_UNLIKELY; + } + + int res = os_mbuf_append(ctxt->om, str, strlen(str)); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + +DeviceInformationService::DeviceInformationService() : + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &manufacturerNameUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &modelNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &serialNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &fwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &hwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &deviceInfoUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + diff --git a/src/Components/Ble/DfuService.h b/src/Components/Ble/DfuService.h new file mode 100644 index 00000000..6249893d --- /dev/null +++ b/src/Components/Ble/DfuService.h @@ -0,0 +1,67 @@ +#pragma once +#include <cstdint> +#include <array> + +#include <host/ble_gap.h> + +namespace Pinetime { + namespace Controllers { + class DeviceInformationService { + public: + DeviceInformationService(); + void Init(); + + int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + private: + static constexpr uint16_t deviceInfoId {0x180a}; + static constexpr uint16_t manufacturerNameId {0x2a29}; + static constexpr uint16_t modelNumberId {0x2a24}; + static constexpr uint16_t serialNumberId {0x2a25}; + static constexpr uint16_t fwRevisionId {0x2a26}; + static constexpr uint16_t hwRevisionId {0x2a27}; + + static constexpr char* manufacturerName = "Codingfield"; + static constexpr char* modelNumber = "1"; + static constexpr char* serialNumber = "9.8.7.6.5.4"; + static constexpr char* fwRevision = "0.5.0"; + static constexpr char* hwRevision = "1.0.0"; + + static constexpr ble_uuid16_t deviceInfoUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = deviceInfoId + }; + + static constexpr ble_uuid16_t manufacturerNameUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = manufacturerNameId + }; + + static constexpr ble_uuid16_t modelNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = modelNumberId + }; + + static constexpr ble_uuid16_t serialNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = serialNumberId + }; + + static constexpr ble_uuid16_t fwRevisionUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = fwRevisionId + }; + + static constexpr ble_uuid16_t hwRevisionUuid { + .u {.type = BLE_UUID_TYPE_16}, + .value = hwRevisionId + }; + + struct ble_gatt_chr_def characteristicDefinition[6]; + struct ble_gatt_svc_def serviceDefinition[2]; + + + }; + } +}
\ No newline at end of file diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp new file mode 100644 index 00000000..2fe03571 --- /dev/null +++ b/src/Components/Ble/NimbleController.cpp @@ -0,0 +1,316 @@ + +#include <Components/DateTime/DateTimeController.h> + +#include <SystemTask/SystemTask.h> +#include <Components/Ble/NotificationManager.h> +#include <hal/nrf_rtc.h> + +#include "NimbleController.h" +#include <services/gatt/ble_svc_gatt.h> +#include <services/gap/ble_svc_gap.h> +#include <host/util/util.h> +#include <host/ble_hs_id.h> +#include <host/ble_hs.h> +#include <host/ble_gap.h> + + + +using namespace Pinetime::Controllers; + +// TODO I'm not satisfied by how this code looks like (AlertNotificationClient and CurrentTimeClient must +// expose too much data, too many callbacks -> NimbleController -> CTS/ANS client. +// Let's try to improve this code (and keep it working!) + +NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::Ble& bleController, + DateTime& dateTimeController, + Pinetime::Controllers::NotificationManager& notificationManager) : + systemTask{systemTask}, + bleController{bleController}, + dateTimeController{dateTimeController}, + notificationManager{notificationManager}, + currentTimeClient{dateTimeController}, + alertNotificationClient{systemTask, notificationManager}, + anService{systemTask, notificationManager}, + currentTimeService{dateTimeController} { + +} + +int GAPEventCallback(struct ble_gap_event *event, void *arg) { + auto nimbleController = static_cast<NimbleController*>(arg); + return nimbleController->OnGAPEvent(event); +} + +int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto client = static_cast<NimbleController*>(arg); + return client->OnCTSCharacteristicDiscoveryEvent(conn_handle, error, chr); +} + +int AlertNotificationCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto client = static_cast<NimbleController*>(arg); + return client->OnANSCharacteristicDiscoveryEvent(conn_handle, error, chr); +} + +int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg) { + auto client = static_cast<NimbleController*>(arg); + return client->OnCurrentTimeReadResult(conn_handle, error, attr); +} + +int AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t chr_val_handle, + const struct ble_gatt_dsc *dsc, + void *arg) { + auto client = static_cast<NimbleController*>(arg); + return client->OnANSDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); +} + +void NimbleController::Init() { + while (!ble_hs_synced()) {} + + ble_svc_gap_init(); + ble_svc_gatt_init(); + + deviceInformationService.Init(); + currentTimeClient.Init(); + currentTimeService.Init(); + + anService.Init(); + + int res; + res = ble_hs_util_ensure_addr(0); + ASSERT(res == 0); + res = ble_hs_id_infer_auto(0, &addrType); + ASSERT(res == 0); + res = ble_svc_gap_device_name_set(deviceName); + + ASSERT(res == 0); + res = ble_gatts_start(); + ASSERT(res == 0); +} + +void NimbleController::StartAdvertising() { + ble_svc_gap_device_name_set("Pinetime-JF"); + + /* set adv parameters */ + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + /* advertising payload is split into advertising data and advertising + response, because all data cannot fit into single packet; name of device + is sent as response to scan request */ + struct ble_hs_adv_fields rsp_fields; + + /* fill all fields and parameters with zeros */ + memset(&adv_params, 0, sizeof(adv_params)); + memset(&fields, 0, sizeof(fields)); + memset(&rsp_fields, 0, sizeof(rsp_fields)); + + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + + fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; +// fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE( +// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, +// 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff)); + fields.num_uuids128 = 0; + fields.uuids128_is_complete = 0;; + fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + + rsp_fields.name = (uint8_t *)"Pinetime-JF"; + rsp_fields.name_len = strlen("Pinetime-JF"); + rsp_fields.name_is_complete = 1; + + int res; + res = ble_gap_adv_set_fields(&fields); + //ASSERT(res == 0); + + res = ble_gap_adv_rsp_set_fields(&rsp_fields); + //ASSERT(res == 0); + + res = ble_gap_adv_start(addrType, NULL, 10000, + &adv_params, GAPEventCallback, this); + //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, + const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, + void *arg) { + auto nimbleController = static_cast<NimbleController*>(arg); + return nimbleController->OnDiscoveryEvent(conn_handle, error, service); + return 0; +} + +int NimbleController::OnGAPEvent(ble_gap_event *event) { + switch (event->type) { + case BLE_GAP_EVENT_ADV_COMPLETE: + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE"); + NRF_LOG_INFO("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status); + StartAdvertising(); + break; + case BLE_GAP_EVENT_CONNECT: { + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT"); + + /* A new connection was established or a connection attempt failed. */ + NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", + event->connect.status); + + if (event->connect.status != 0) { + /* Connection failed; resume advertising. */ + StartAdvertising(); + bleController.Disconnect(); + } else { + bleController.Connect(); + connectionHandle = event->connect.conn_handle; + ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this); + } + } + break; + case BLE_GAP_EVENT_DISCONNECT: + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT"); + NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason); + + /* Connection terminated; resume advertising. */ + bleController.Disconnect(); + StartAdvertising(); + break; + case BLE_GAP_EVENT_CONN_UPDATE: + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONN_UPDATE"); + /* The central has updated the connection parameters. */ + NRF_LOG_INFO("connection updated; status=%d ", event->conn_update.status); + break; + case BLE_GAP_EVENT_ENC_CHANGE: + /* Encryption has been enabled or disabled for this connection. */ + NRF_LOG_INFO("encryption change event; status=%d ", event->enc_change.status); + return 0; + case BLE_GAP_EVENT_SUBSCRIBE: + NRF_LOG_INFO("subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=???\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate); + return 0; + case BLE_GAP_EVENT_MTU: + NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + return 0; + + case BLE_GAP_EVENT_REPEAT_PAIRING: { + /* We already have a bond with the peer, but it is attempting to + * establish a new secure link. This app sacrifices security for + * convenience: just throw away the old bond and accept the new link. + */ + + /* Delete the old bond. */ + struct ble_gap_conn_desc desc; + ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); + ble_store_util_delete_peer(&desc.peer_id_addr); + + /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should + * continue with the pairing operation. + */ + } + return BLE_GAP_REPEAT_PAIRING_RETRY; + + case BLE_GAP_EVENT_NOTIFY_RX: { + /* Peer sent us a notification or indication. */ + size_t notifSize = OS_MBUF_PKTLEN(event->notify_rx.om); + + NRF_LOG_INFO("received %s; conn_handle=%d attr_handle=%d " + "attr_len=%d", + event->notify_rx.indication ? + "indication" : + "notification", + event->notify_rx.conn_handle, + event->notify_rx.attr_handle, + notifSize); + + alertNotificationClient.OnNotification(event); + return 0; + } + /* Attribute data is contained in event->notify_rx.attr_data. */ + + default: + NRF_LOG_INFO("Advertising event : %d", event->type); + break; + } + return 0; +} + +int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("Service Discovery complete"); + if(currentTimeClient.IsDiscovered()) { + ble_gattc_disc_all_chrs(connectionHandle, currentTimeClient.StartHandle(), currentTimeClient.EndHandle(), + CurrentTimeCharacteristicDiscoveredCallback, this); + + } else if(alertNotificationClient.IsDiscovered()) { + ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), alertNotificationClient.EndHandle(), + AlertNotificationCharacteristicDiscoveredCallback, this); + } + return 0; + } + + alertNotificationClient.OnDiscoveryEvent(i, error, service); + currentTimeClient.OnDiscoveryEvent(i, error, service); + return 0; +} + +int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("CTS characteristic Discovery complete"); + ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this); + return 0; + } + return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic); +} + +int NimbleController::OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("ANS characteristic Discovery complete"); + ble_gattc_disc_all_dscs(connectionHandle, + alertNotificationClient.NewAlerthandle(), alertNotificationClient.EndHandle(), + AlertNotificationDescriptorDiscoveryEventCallback, this); + return 0; + } + return alertNotificationClient.OnCharacteristicsDiscoveryEvent(connectionHandle, error, characteristic); +} + +int NimbleController::OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute) { + currentTimeClient.OnCurrentTimeReadResult(connectionHandle, error, attribute); + + if (alertNotificationClient.IsDiscovered()) { + ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), + alertNotificationClient.EndHandle(), + AlertNotificationCharacteristicDiscoveredCallback, this); + } + return 0; +} + +int NimbleController::OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, + const ble_gatt_dsc *descriptor) { + return alertNotificationClient.OnDescriptorDiscoveryEventCallback(connectionHandle, error, characteristicValueHandle, descriptor); +} + + + + diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h new file mode 100644 index 00000000..44fbbe2c --- /dev/null +++ b/src/Components/Ble/NimbleController.h @@ -0,0 +1,47 @@ +#pragma once + +#include <cstdint> +#include "AlertNotificationService.h" +#include "AlertNotificationClient.h" +#include "DeviceInformationService.h" +#include "CurrentTimeClient.h" +#include "CurrentTimeService.h" +#include <host/ble_gap.h> + +namespace Pinetime { + namespace Controllers { + class DateTime; + class NimbleController { + + public: + NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); + void Init(); + void StartAdvertising(); + int OnGAPEvent(ble_gap_event *event); + + int OnDiscoveryEvent(uint16_t i, const ble_gatt_error *pError, const ble_gatt_svc *pSvc); + int OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute); + int OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); + private: + static constexpr char* deviceName = "Pinetime-JF"; + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::Ble& bleController; + DateTime& dateTimeController; + Pinetime::Controllers::NotificationManager& notificationManager; + + DeviceInformationService deviceInformationService; + CurrentTimeClient currentTimeClient; + AlertNotificationService anService; + AlertNotificationClient alertNotificationClient; + CurrentTimeService currentTimeService; + + uint8_t addrType; + uint16_t connectionHandle; + }; + } +} |