Fix BLE connection handling (#4137)

Remove device from Peer list if connection fails.

Only call onConnect callback if connection was successful.

Only call onDisconnect callback if the connection was previously connected (ESP_GATTC_DISCONNECT_EVT is fired on a unsuccessful connection attempt also).

Resolves a number of issues with phantom events and callbacks being fired.
This commit is contained in:
buxtronix 2020-11-03 05:20:40 +11:00 committed by GitHub
parent 9f7ff009c6
commit 76cd2e2375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -105,6 +105,7 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId); esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId);
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
log_e("esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); log_e("esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
BLEDevice::removePeerDevice(m_appId, true);
return false; return false;
} }
@ -122,6 +123,7 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
); );
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
log_e("esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); log_e("esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
BLEDevice::removePeerDevice(m_appId, true);
return false; return false;
} }
@ -181,10 +183,10 @@ void BLEClient::gattClientEventHandler(
if (evtParam->disconnect.conn_id != getConnId()) break; if (evtParam->disconnect.conn_id != getConnId()) break;
// If we receive a disconnect event, set the class flag that indicates that we are // If we receive a disconnect event, set the class flag that indicates that we are
// no longer connected. // no longer connected.
m_isConnected = false; if (m_isConnected && m_pClientCallbacks != nullptr) {
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onDisconnect(this); m_pClientCallbacks->onDisconnect(this);
} }
m_isConnected = false;
esp_ble_gattc_app_unregister(m_gattc_if); esp_ble_gattc_app_unregister(m_gattc_if);
m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE); m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE);
m_semaphoreRssiCmplEvt.give(); m_semaphoreRssiCmplEvt.give();
@ -203,11 +205,13 @@ void BLEClient::gattClientEventHandler(
// //
case ESP_GATTC_OPEN_EVT: { case ESP_GATTC_OPEN_EVT: {
m_conn_id = evtParam->open.conn_id; m_conn_id = evtParam->open.conn_id;
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onConnect(this);
}
if (evtParam->open.status == ESP_GATT_OK) { if (evtParam->open.status == ESP_GATT_OK) {
m_isConnected = true; // Flag us as connected. m_isConnected = true; // Flag us as connected.
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onConnect(this);
}
} else {
log_e("Failed to connect, status=%s", GeneralUtils::errorToString(evtParam->open.status));
} }
m_semaphoreOpenEvt.give(evtParam->open.status); m_semaphoreOpenEvt.give(evtParam->open.status);
break; break;