From 44f16e6ee2561a50a9418a5fd911fbc842b7dc17 Mon Sep 17 00:00:00 2001 From: h2zero Date: Sat, 30 May 2020 09:01:42 -0600 Subject: [PATCH] Correct client connection status signalling. * NimBLEClient::disconnect() was setting the connected flag to false before the disconnection occured. * NimBLEDevice::deleteClient() was not waiting for disconnection if it was already in progress. * Client gap event handler was releasing the connection event semaphore when it should not. --- src/NimBLEClient.cpp | 8 +++----- src/NimBLEDevice.cpp | 13 +++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index e23ed69..153b440 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -242,15 +242,11 @@ int NimBLEClient::disconnect(uint8_t reason) { NIMBLE_LOGD(LOG_TAG, ">> disconnect()"); int rc = 0; if(m_isConnected){ - m_isConnected = false; // flag the disconnect now so no calls are performed after rc = ble_gap_terminate(m_conn_id, reason); if(rc != 0){ NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); } - // Sometimes a disconnect event is not sent so we need to make sure - // the device can be found again. - NimBLEDevice::removeIgnored(m_peerAddress); } NIMBLE_LOGD(LOG_TAG, "<< disconnect()"); @@ -711,8 +707,10 @@ uint16_t NimBLEClient::getMTU() { NIMBLE_LOGE(LOG_TAG, "Error: Connection failed; status=%d %s", event->connect.status, NimBLEUtils::returnCodeToString(event->connect.status)); + + client->m_isConnected = false; + client->m_semaphoreOpenEvt.give(event->connect.status); } - client->m_semaphoreOpenEvt.give(event->connect.status); return 0; } // BLE_GAP_EVENT_CONNECT diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 1695a51..b3d882b 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -152,21 +152,26 @@ void NimBLEDevice::stopAdvertising() { return false; } + int rc =0; + if(pClient->m_isConnected) { - if (pClient->disconnect() != 0) { + rc = pClient->disconnect(); + if (rc != 0 && rc != BLE_HS_EALREADY && rc != BLE_HS_ENOTCONN) { return false; } + while(pClient->m_isConnected) { - vTaskDelay(1); + vTaskDelay(10); } } if(pClient->m_waitingToConnect) { - if(ble_gap_conn_cancel() != 0){ + rc = ble_gap_conn_cancel(); + if (rc != 0 && rc != BLE_HS_EALREADY) { return false; } while(pClient->m_waitingToConnect) { - vTaskDelay(1); + vTaskDelay(10); } }