Skip to content

Commit 9be784f

Browse files
authoredFeb 16, 2021
Improve cleanup in BLEClient (espressif#4742)
- Remove client from the list of devices in case registration fails - Filter other events not related to registration during registration phase - Cleanup if connect fails - Reset if after disconnect - Disconnect callback *after* cleanup is done so object can be deleted This fixes some of the issues I had like: - `BLEClient::connect` hangs up and never recovered because registration failed - `BLEClient` could not be deleted after disconnect or deletion creating ghost events espressif#4047 - `BLEClient` could not be properly reused after a connection was attempted (successful or not) * Cleanup in case of registration and connect failure. Cleanup before calling disconnect callback for safe delete. Reject other events during registration. Adresses espressif#4047, espressif#4055 * Clear if after unregister espressif#4047
1 parent 7cdfb8b commit 9be784f

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed
 

‎libraries/BLE/src/BLEClient.cpp

+30-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,17 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
110110
return false;
111111
}
112112

113-
m_semaphoreRegEvt.wait("connect");
113+
uint32_t rc = m_semaphoreRegEvt.wait("connect");
114+
115+
if (rc != ESP_GATT_OK) {
116+
// fixes ESP_GATT_NO_RESOURCES error mostly
117+
log_e("esp_ble_gattc_app_register_error: rc=%d", rc);
118+
BLEDevice::removePeerDevice(m_appId, true);
119+
// not sure if this is needed here
120+
// esp_ble_gattc_app_unregister(m_gattc_if);
121+
// m_gattc_if = ESP_GATT_IF_NONE;
122+
return false;
123+
}
114124

115125
m_peerAddress = address;
116126

@@ -128,7 +138,13 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
128138
return false;
129139
}
130140

131-
uint32_t rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
141+
rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
142+
// check the status of the connection and cleanup in case of failure
143+
if (rc != ESP_GATT_OK) {
144+
BLEDevice::removePeerDevice(m_appId, true);
145+
esp_ble_gattc_app_unregister(m_gattc_if);
146+
m_gattc_if = ESP_GATT_IF_NONE;
147+
}
132148
log_v("<< connect(), rc=%d", rc==ESP_GATT_OK);
133149
return rc == ESP_GATT_OK;
134150
} // connect
@@ -160,6 +176,11 @@ void BLEClient::gattClientEventHandler(
160176
log_d("gattClientEventHandler [esp_gatt_if: %d] ... %s",
161177
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
162178

179+
// it is possible to receive events from other connections while waiting for registration
180+
if (m_gattc_if == ESP_GATT_IF_NONE && event != ESP_GATTC_REG_EVT) {
181+
return;
182+
}
183+
163184
// Execute handler code based on the type of event received.
164185
switch(event) {
165186

@@ -184,15 +205,17 @@ void BLEClient::gattClientEventHandler(
184205
if (evtParam->disconnect.conn_id != getConnId()) break;
185206
// If we receive a disconnect event, set the class flag that indicates that we are
186207
// no longer connected.
187-
if (m_isConnected && m_pClientCallbacks != nullptr) {
188-
m_pClientCallbacks->onDisconnect(this);
189-
}
208+
bool m_wasConnected = m_isConnected;
190209
m_isConnected = false;
191210
esp_ble_gattc_app_unregister(m_gattc_if);
211+
m_gattc_if = ESP_GATT_IF_NONE;
192212
m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE);
193213
m_semaphoreRssiCmplEvt.give();
194214
m_semaphoreSearchCmplEvt.give(1);
195215
BLEDevice::removePeerDevice(m_appId, true);
216+
if (m_wasConnected && m_pClientCallbacks != nullptr) {
217+
m_pClientCallbacks->onDisconnect(this);
218+
}
196219
break;
197220
} // ESP_GATTC_DISCONNECT_EVT
198221

@@ -228,7 +251,8 @@ void BLEClient::gattClientEventHandler(
228251
//
229252
case ESP_GATTC_REG_EVT: {
230253
m_gattc_if = gattc_if;
231-
m_semaphoreRegEvt.give();
254+
// pass on the registration status result, in case of failure
255+
m_semaphoreRegEvt.give(evtParam->reg.status);
232256
break;
233257
} // ESP_GATTC_REG_EVT
234258

0 commit comments

Comments
 (0)
Please sign in to comment.