diff --git a/src/Components/Ble/AlertNotificationClient.cpp b/src/Components/Ble/AlertNotificationClient.cpp index e6683e99..6e096353 100644 --- a/src/Components/Ble/AlertNotificationClient.cpp +++ b/src/Components/Ble/AlertNotificationClient.cpp @@ -13,19 +13,6 @@ constexpr ble_uuid16_t AlertNotificationClient::newAlertUuid; constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid; constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid; -int Pinetime::Controllers::AlertNotificationDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg) { - auto client = static_cast(arg); - return client->OnDiscoveryEvent(conn_handle, error, service); -} - -int Pinetime::Controllers::AlertNotificationCharacteristicsDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast(arg); - return client->OnCharacteristicsDiscoveryEvent(conn_handle, error, chr); -} - int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, @@ -34,37 +21,23 @@ int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle, return client->OnNewAlertSubcribe(conn_handle, error, attr); } -int Pinetime::Controllers::AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - uint16_t chr_val_handle, - const struct ble_gatt_dsc *dsc, - void *arg) { - NRF_LOG_INFO("ANS VCS"); - auto client = static_cast(arg); - return client->OnDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); -} - AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager) : systemTask{systemTask}, notificationManager{notificationManager}{ } -void AlertNotificationClient::StartDiscovery(uint16_t connectionHandle) { - ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ansServiceUuid), AlertNotificationDiscoveryEventCallback, this); -} - 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"); - ble_gattc_disc_all_dscs(connectionHandle, newAlertHandle, ansEndHandle, AlertNotificationDescriptorDiscoveryEventCallback, this); 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); - ble_gattc_disc_all_chrs(connectionHandle, service->start_handle, service->end_handle, AlertNotificationCharacteristicsDiscoveryEventCallback, this); - ansEndHandle = service->end_handle; + ansStartHandle = service->start_handle; + ansEndHandle = service->end_handle; + isDiscovered = true; } return false; } @@ -147,3 +120,19 @@ void AlertNotificationClient::OnNotification(ble_gap_event *event) { 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 index 595dbe37..7a085b7e 100644 --- a/src/Components/Ble/AlertNotificationClient.h +++ b/src/Components/Ble/AlertNotificationClient.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -6,29 +7,17 @@ namespace Pinetime { namespace Controllers { - int AlertNotificationDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg); - int AlertNotificationCharacteristicsDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg); int NewAlertSubcribeCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, - void *arg); - - 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); + 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& notificationManager); + explicit AlertNotificationClient(Pinetime::System::SystemTask &systemTask, + Pinetime::Controllers::NotificationManager ¬ificationManager); void Init(); - - void StartDiscovery(uint16_t connectionHandle); 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); @@ -36,39 +25,47 @@ namespace Pinetime { 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 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 }, + 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 }, + 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 }, + 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 }, + 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 }, + 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 }, + 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; @@ -77,12 +74,9 @@ namespace Pinetime { uint16_t newAlertDefHandle; uint16_t unreadAlertStatusHandle; uint16_t controlPointHandle; - bool discoveryDone = false; - Pinetime::System::SystemTask& systemTask; - Pinetime::Controllers::NotificationManager& notificationManager; - + bool isDiscovered = false; + Pinetime::System::SystemTask &systemTask; + Pinetime::Controllers::NotificationManager ¬ificationManager; }; - - } } \ No newline at end of file diff --git a/src/Components/Ble/CurrentTimeClient.cpp b/src/Components/Ble/CurrentTimeClient.cpp index 44065bce..fdebc084 100644 --- a/src/Components/Ble/CurrentTimeClient.cpp +++ b/src/Components/Ble/CurrentTimeClient.cpp @@ -6,25 +6,6 @@ using namespace Pinetime::Controllers; constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid; constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid; -int Pinetime::Controllers::CurrentTimeDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg) { - auto client = static_cast(arg); - return client->OnDiscoveryEvent(conn_handle, error, service); -} - -int Pinetime::Controllers::CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast(arg); - return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); -} - -int Pinetime::Controllers::CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, void *arg) { - auto client = static_cast(arg); - return client->OnCurrentTimeReadResult(conn_handle, error, attr); -} - - CurrentTimeClient::CurrentTimeClient(DateTime& dateTimeController) : dateTimeController{dateTimeController} { } @@ -33,10 +14,6 @@ void CurrentTimeClient::Init() { } -void CurrentTimeClient::StartDiscovery(uint16_t connectionHandle) { - ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsServiceUuid), CurrentTimeDiscoveryEventCallback, this); -} - 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"); @@ -45,20 +22,24 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsServiceUuid), &service->uuid.u) == 0) { NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); - ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)¤tTimeCharacteristicUuid), CurrentTimeCharacteristicDiscoveredCallback, this); + 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) + 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); - - ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this); + currentTimeHandle = characteristic->val_handle; } return 0; } @@ -78,3 +59,19 @@ int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_g } 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 index 43deadcc..2278ef15 100644 --- a/src/Components/Ble/CurrentTimeClient.h +++ b/src/Components/Ble/CurrentTimeClient.h @@ -6,12 +6,6 @@ namespace Pinetime { namespace Controllers { - int CurrentTimeDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg); - int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg); - int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, void *arg); class CurrentTimeClient { public: @@ -21,9 +15,12 @@ namespace Pinetime { 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); - - - void StartDiscovery(uint16_t connectionHandle); + 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; @@ -48,7 +45,11 @@ namespace Pinetime { .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/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 5e3d58eb..7894ff43 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -17,8 +17,9 @@ using namespace Pinetime::Controllers; -// TODO c++ify the following code -// - cts should be in it own class +// 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, DateTime& dateTimeController, @@ -36,6 +37,33 @@ int GAPEventCallback(struct ble_gap_event *event, void *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(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(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(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(arg); + return client->OnANSDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); +} + void NimbleController::Init() { while (!ble_hs_synced()) {} @@ -203,11 +231,63 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { } 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); + 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 index c4eab52b..7a7a94c9 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -10,6 +10,7 @@ namespace Pinetime { namespace Controllers { class DateTime; class NimbleController { + public: NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); void Init(); @@ -17,6 +18,13 @@ namespace Pinetime { 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;