From a3cac65b25900a62eb558c2066583f0a2537d902 Mon Sep 17 00:00:00 2001 From: Ravi Kharb Date: Fri, 15 Dec 2023 15:20:02 +0530 Subject: [PATCH 1/2] fix(esp32): Added timeout to BLEClient.connect fn --- libraries/BLE/src/BLEClient.cpp | 20 ++++++++++++++++---- libraries/BLE/src/BLEClient.h | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/libraries/BLE/src/BLEClient.cpp b/libraries/BLE/src/BLEClient.cpp index ae82c2417a1..ecfc7fa485c 100644 --- a/libraries/BLE/src/BLEClient.cpp +++ b/libraries/BLE/src/BLEClient.cpp @@ -92,12 +92,23 @@ bool BLEClient::connect(BLEAdvertisedDevice* device) { return connect(address, type); } +/** + * Add overloaded function to ease connect to peer device with not public address + */ +bool BLEClient::connectTimeout(BLEAdvertisedDevice* device, uint32_t timeoutMs) { + BLEAddress address = device->getAddress(); + esp_ble_addr_type_t type = device->getAddressType(); + return connect(address, type, timeoutMs); +} + /** * @brief Connect to the partner (BLE Server). * @param [in] address The address of the partner. + * @param [in] type The type of the address. + * @param [in] timeoutMs The number of milliseconds to wait for the connection to complete. * @return True on success. */ -bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) { +bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type, uint32_t timeoutMs) { log_v(">> connect(%s)", address.toString().c_str()); // We need the connection handle that we get from registering the application. We register the app @@ -142,9 +153,10 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) { return false; } - rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete. - // check the status of the connection and cleanup in case of failure - if (rc != ESP_GATT_OK) { + bool got_sem = m_semaphoreOpenEvt.timedWait("connect", timeoutMs); // Wait for the connection to complete. + rc = m_semaphoreOpenEvt.value(); + // check the status of the connection and cleanup in case of failure + if (!got_sem || rc != ESP_GATT_OK) { BLEDevice::removePeerDevice(m_appId, true); esp_ble_gattc_app_unregister(m_gattc_if); m_gattc_if = ESP_GATT_IF_NONE; diff --git a/libraries/BLE/src/BLEClient.h b/libraries/BLE/src/BLEClient.h index 0207a214b66..2c8bc2c4ab9 100644 --- a/libraries/BLE/src/BLEClient.h +++ b/libraries/BLE/src/BLEClient.h @@ -37,7 +37,8 @@ class BLEClient { ~BLEClient(); bool connect(BLEAdvertisedDevice* device); - bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC); // Connect to the remote BLE Server + bool connectTimeout(BLEAdvertisedDevice* device, uint32_t timeoutMS = portMAX_DELAY); + bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC, uint32_t timeoutMS = portMAX_DELAY); // Connect to the remote BLE Server void disconnect(); // Disconnect from the remote BLE Server BLEAddress getPeerAddress(); // Get the address of the remote BLE Server int getRssi(); // Get the RSSI of the remote BLE Server From 1bd04ba5910a8f262126d4aa488c54072aadce92 Mon Sep 17 00:00:00 2001 From: LiveSparks Date: Tue, 30 Jan 2024 16:50:54 +0530 Subject: [PATCH 2/2] Update libraries/BLE/src/BLEClient.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/BLE/src/BLEClient.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/BLE/src/BLEClient.cpp b/libraries/BLE/src/BLEClient.cpp index ecfc7fa485c..60ee53c0fa0 100644 --- a/libraries/BLE/src/BLEClient.cpp +++ b/libraries/BLE/src/BLEClient.cpp @@ -154,9 +154,9 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type, uint32_t t } bool got_sem = m_semaphoreOpenEvt.timedWait("connect", timeoutMs); // Wait for the connection to complete. - rc = m_semaphoreOpenEvt.value(); - // check the status of the connection and cleanup in case of failure - if (!got_sem || rc != ESP_GATT_OK) { + rc = m_semaphoreOpenEvt.value(); + // check the status of the connection and cleanup in case of failure + if (!got_sem || rc != ESP_GATT_OK) { BLEDevice::removePeerDevice(m_appId, true); esp_ble_gattc_app_unregister(m_gattc_if); m_gattc_if = ESP_GATT_IF_NONE;