From f90f2254f55086589d1d378d35a54085e2620cb6 Mon Sep 17 00:00:00 2001 From: JF Date: Wed, 28 Oct 2020 18:23:09 +0100 Subject: [PATCH] Reset BLE services on disconnect, do not start advertising if a connection is already established. --- .../ble/AlertNotificationClient.cpp | 13 ++++++++ src/components/ble/AlertNotificationClient.h | 17 ++++++----- src/components/ble/CurrentTimeClient.cpp | 30 ++++++++++++------- src/components/ble/CurrentTimeClient.h | 7 ++++- src/components/ble/NimbleController.cpp | 10 ++++--- 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index 40970e0b..29bc2f73 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -139,3 +139,16 @@ uint16_t AlertNotificationClient::EndHandle() const { uint16_t AlertNotificationClient::NewAlerthandle() const { return newAlertHandle; } + +void AlertNotificationClient::Reset() { + ansStartHandle = 0; + ansEndHandle = 0; + supportedNewAlertCategoryHandle = 0; + supportedUnreadAlertCategoryHandle = 0; + newAlertHandle = 0; + newAlertDescriptorHandle = 0; + newAlertDefHandle = 0; + unreadAlertStatusHandle = 0; + controlPointHandle = 0; + isDiscovered = false; +} diff --git a/src/components/ble/AlertNotificationClient.h b/src/components/ble/AlertNotificationClient.h index ca4f4e94..ebd5d56f 100644 --- a/src/components/ble/AlertNotificationClient.h +++ b/src/components/ble/AlertNotificationClient.h @@ -27,6 +27,7 @@ namespace Pinetime { bool IsDiscovered() const; uint16_t StartHandle() const; uint16_t EndHandle() const; + void Reset(); static constexpr const ble_uuid16_t &Uuid() { return ansServiceUuid; } @@ -64,15 +65,15 @@ namespace Pinetime { .value = controlPointId }; - uint16_t ansStartHandle; - uint16_t ansEndHandle; - uint16_t supportedNewAlertCategoryHandle; - uint16_t supportedUnreadAlertCategoryHandle; - uint16_t newAlertHandle; + uint16_t ansStartHandle = 0; + uint16_t ansEndHandle = 0; + uint16_t supportedNewAlertCategoryHandle = 0; + uint16_t supportedUnreadAlertCategoryHandle = 0; + uint16_t newAlertHandle = 0; uint16_t newAlertDescriptorHandle = 0; - uint16_t newAlertDefHandle; - uint16_t unreadAlertStatusHandle; - uint16_t controlPointHandle; + uint16_t newAlertDefHandle = 0; + uint16_t unreadAlertStatusHandle = 0; + uint16_t controlPointHandle = 0; bool isDiscovered = false; Pinetime::System::SystemTask &systemTask; Pinetime::Controllers::NotificationManager ¬ificationManager; diff --git a/src/components/ble/CurrentTimeClient.cpp b/src/components/ble/CurrentTimeClient.cpp index 7a225f4b..24027e5f 100644 --- a/src/components/ble/CurrentTimeClient.cpp +++ b/src/components/ble/CurrentTimeClient.cpp @@ -32,16 +32,17 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga 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; - } + 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); + isCharacteristicDiscovered = true; + currentTimeHandle = characteristic->val_handle; + } + return 0; } int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) { @@ -74,4 +75,13 @@ uint16_t CurrentTimeClient::EndHandle() const { uint16_t CurrentTimeClient::CurrentTimeHandle() const { return currentTimeHandle; -} \ No newline at end of file +} + +void CurrentTimeClient::Reset() { + isDiscovered = false; + isCharacteristicDiscovered = false; +} + +bool CurrentTimeClient::IsCharacteristicDiscovered() const { + return isCharacteristicDiscovered; +} diff --git a/src/components/ble/CurrentTimeClient.h b/src/components/ble/CurrentTimeClient.h index 639ec831..fcc124c2 100644 --- a/src/components/ble/CurrentTimeClient.h +++ b/src/components/ble/CurrentTimeClient.h @@ -12,11 +12,13 @@ namespace Pinetime { public: explicit CurrentTimeClient(DateTime& dateTimeController); void Init(); + void Reset(); 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; + bool IsCharacteristicDiscovered() const; uint16_t StartHandle() const; uint16_t EndHandle() const; uint16_t CurrentTimeHandle() const; @@ -46,11 +48,14 @@ namespace Pinetime { .value = currentTimeCharacteristicId }; - uint16_t currentTimeHandle; DateTime& dateTimeController; bool isDiscovered = false; uint16_t ctsStartHandle; uint16_t ctsEndHandle; + + bool isCharacteristicDiscovered = false; + uint16_t currentTimeHandle; + }; } } \ No newline at end of file diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 022cc510..577c897a 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -108,7 +108,7 @@ void NimbleController::Init() { } void NimbleController::StartAdvertising() { - if(ble_gap_adv_active()) return; + if(bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active()) return; ble_svc_gap_device_name_set(deviceName); @@ -197,6 +197,8 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason); /* Connection terminated; resume advertising. */ + currentTimeClient.Reset(); + alertNotificationClient.Reset(); connectionHandle = BLE_HS_CONN_HANDLE_NONE; bleController.Disconnect(); StartAdvertising(); @@ -289,10 +291,10 @@ int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error, int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, 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 && currentTimeClient.IsCharacteristicDiscovered()) { NRF_LOG_INFO("CTS characteristic Discovery complete"); - ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this); - return 0; + auto res = ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this); + return res; } return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic); }