Skip to content

Commit e1a3525

Browse files
LiveSparkslucasssvazP-R-O-C-H-Y
authored
fix(esp32): Added a timeout option to the BLEClient's connect function (#9005)
* fix(esp32): Added timeout to BLEClient.connect fn * Update libraries/BLE/src/BLEClient.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --------- Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
1 parent 7d59554 commit e1a3525

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

libraries/BLE/src/BLEClient.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,23 @@ bool BLEClient::connect(BLEAdvertisedDevice* device) {
9292
return connect(address, type);
9393
}
9494

95+
/**
96+
* Add overloaded function to ease connect to peer device with not public address
97+
*/
98+
bool BLEClient::connectTimeout(BLEAdvertisedDevice* device, uint32_t timeoutMs) {
99+
BLEAddress address = device->getAddress();
100+
esp_ble_addr_type_t type = device->getAddressType();
101+
return connect(address, type, timeoutMs);
102+
}
103+
95104
/**
96105
* @brief Connect to the partner (BLE Server).
97106
* @param [in] address The address of the partner.
107+
* @param [in] type The type of the address.
108+
* @param [in] timeoutMs The number of milliseconds to wait for the connection to complete.
98109
* @return True on success.
99110
*/
100-
bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
111+
bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type, uint32_t timeoutMs) {
101112
log_v(">> connect(%s)", address.toString().c_str());
102113

103114
// 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) {
142153
return false;
143154
}
144155

145-
rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
156+
bool got_sem = m_semaphoreOpenEvt.timedWait("connect", timeoutMs); // Wait for the connection to complete.
157+
rc = m_semaphoreOpenEvt.value();
146158
// check the status of the connection and cleanup in case of failure
147-
if (rc != ESP_GATT_OK) {
159+
if (!got_sem || rc != ESP_GATT_OK) {
148160
BLEDevice::removePeerDevice(m_appId, true);
149161
esp_ble_gattc_app_unregister(m_gattc_if);
150162
m_gattc_if = ESP_GATT_IF_NONE;

libraries/BLE/src/BLEClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class BLEClient {
3737
~BLEClient();
3838

3939
bool connect(BLEAdvertisedDevice* device);
40-
bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC); // Connect to the remote BLE Server
40+
bool connectTimeout(BLEAdvertisedDevice* device, uint32_t timeoutMS = portMAX_DELAY);
41+
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
4142
void disconnect(); // Disconnect from the remote BLE Server
4243
BLEAddress getPeerAddress(); // Get the address of the remote BLE Server
4344
int getRssi(); // Get the RSSI of the remote BLE Server

0 commit comments

Comments
 (0)