InfiniTime.git

commit f90f2254f55086589d1d378d35a54085e2620cb6

Author: JF <jf@codingfield.com>

Reset BLE services on disconnect, do not start advertising if a connection is already established.

 src/components/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 ++++--


diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp
index 40970e0b6daf45015ec661667069290cdfe152e8..29bc2f73a7e315d4806f8391af61a1b1559984b7 100644
--- a/src/components/ble/AlertNotificationClient.cpp
+++ b/src/components/ble/AlertNotificationClient.cpp
@@ -139,3 +139,16 @@
 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 ca4f4e948e228c8351bfedbb119253134e493817..ebd5d56fbe5215d19127874beb80a0b769abf82a 100644
--- a/src/components/ble/AlertNotificationClient.h
+++ b/src/components/ble/AlertNotificationClient.h
@@ -27,6 +27,7 @@         void OnNotification(ble_gap_event *event);
         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 @@                 .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 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 &notificationManager;




diff --git a/src/components/ble/CurrentTimeClient.cpp b/src/components/ble/CurrentTimeClient.cpp
index 7a225f4bad3e6825edddde1af187c983edb08185..24027e5f61fe6d6442a4d289469a7e497ceab3d2 100644
--- a/src/components/ble/CurrentTimeClient.cpp
+++ b/src/components/ble/CurrentTimeClient.cpp
@@ -32,16 +32,17 @@ }
 
 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 && error->status == BLE_HS_EDONE) {
+    NRF_LOG_INFO("CTS Characteristic discovery complete");
+    return 0;
+  }
 
-    if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
-        NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
-        currentTimeHandle = characteristic->val_handle;
-    }
-    return 0;
+  if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t *) &currentTimeCharacteristicUuid), &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::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 639ec83183c02ee199d56650f2ae187099fa4889..fcc124c2b006cf914b11a9f05ce6e0c94b244139 100644
--- a/src/components/ble/CurrentTimeClient.h
+++ b/src/components/ble/CurrentTimeClient.h
@@ -12,11 +12,13 @@         class CurrentTimeClient {
         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 @@                     .u { .type = BLE_UUID_TYPE_16 },
                     .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 022cc5100903032877e8eca8d77b0cd3d7df38ab..577c897a70095ea64bf392560bc86b49ec7d7b89 100644
--- a/src/components/ble/NimbleController.cpp
+++ b/src/components/ble/NimbleController.cpp
@@ -108,7 +108,7 @@   ASSERT(res == 0);
 }
 
 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 @@       NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT");
       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::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);
 }