Fix endless loop when calling scan start from scan end callback.

When attempting to connect and the scanner is running `NimBLEScan::stop` is called to stop it
which then calls the `onScanEnded` callback. If the app then starts the scan again in this
callback an endless loop will be created.

This change prevents the endless loop by setting a flag in `NimBLEDevice` that is checked before
starting a scan while a client is trying to connect.

* Adds `NimBLEDevice::setConnectionInProgress` and `NimBLEDevice::isConnectionInProgress` functions.
This commit is contained in:
h2zero
2024-11-10 08:28:14 -07:00
committed by h2zero
parent 7d0636bc91
commit 724e1a7083
4 changed files with 37 additions and 2 deletions

View File

@@ -182,6 +182,9 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes)
m_pTaskData = &taskData;
int rc = 0;
// Set the connection in progress flag to prevent a scan from starting while connecting.
NimBLEDevice::setConnectionInProgress(true);
do {
# if CONFIG_BT_NIMBLE_EXT_ADV
rc = ble_gap_ext_connect(NimBLEDevice::m_ownAddrType,
@@ -207,7 +210,7 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes)
break;
case BLE_HS_EBUSY:
// Scan was still running, stop it and try again
// Scan was active, stop it through the NimBLEScan API to release any tasks and call the callback.
if (!NimBLEDevice::getScan()->stop()) {
rc = BLE_HS_EUNKNOWN;
}
@@ -236,8 +239,8 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes)
} while (rc == BLE_HS_EBUSY);
NimBLEDevice::setConnectionInProgress(false);
m_lastErr = rc;
if (rc != 0) {
m_pTaskData = nullptr;
return false;