Skip to content

Commit 32a8ef8

Browse files
committed
Revert #697277f and replace with stack checks.
Replaces `NimBLEDevice::setConnectionInProgress` and `NimBLEDevice::isConnectionInProgress()` with lower level checks to avoid potential incorrect state reporting. `NimBLEClient::connect` will instead call `NimBLEScan::stop` if it stopped the scan to release any resources waiting, the call the callback if set.
1 parent fd31ceb commit 32a8ef8

File tree

5 files changed

+20
-38
lines changed

5 files changed

+20
-38
lines changed

src/NimBLEClient.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
176176
return false;
177177
}
178178

179-
if (NimBLEDevice::isConnectionInProgress()) {
179+
if (ble_gap_conn_active()) {
180180
NIMBLE_LOGE(LOG_TAG, "Connection already in progress");
181181
return false;
182182
}
@@ -198,13 +198,11 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
198198
deleteServices();
199199
}
200200

201-
int rc = 0;
201+
int rc = 0;
202+
bool scanStopped = false;
202203
m_config.asyncConnect = asyncConnect;
203204
m_config.exchangeMTU = exchangeMTU;
204205

205-
// Set the connection in progress flag to prevent a scan from starting while connecting.
206-
NimBLEDevice::setConnectionInProgress(true);
207-
208206
do {
209207
# if CONFIG_BT_NIMBLE_EXT_ADV
210208
rc = ble_gap_ext_connect(NimBLEDevice::m_ownAddrType,
@@ -230,9 +228,11 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
230228
break;
231229

232230
case BLE_HS_EBUSY:
233-
// Scan was active, stop it through the NimBLEScan API to release any tasks and call the callback.
234-
if (!NimBLEDevice::getScan()->stop()) {
235-
rc = BLE_HS_EUNKNOWN;
231+
// Scan was active, stop it directly to prevent the callback from being called in case it's restarted there.
232+
rc = ble_gap_disc_cancel();
233+
if (rc == 0 || rc == BLE_HS_EALREADY) {
234+
rc = BLE_HS_EBUSY; // try again
235+
scanStopped = true;
236236
}
237237
break;
238238

@@ -256,9 +256,18 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
256256

257257
} while (rc == BLE_HS_EBUSY);
258258

259+
if (scanStopped) {
260+
// If we stopped the scan directly call the CPP API now to cleanup
261+
NimBLEScan* pScan = NimBLEDevice::getScan();
262+
pScan->stop();
263+
// Call the callback now since it won't be called in the stop call as the scan was already stopped
264+
if (pScan->m_pScanCallbacks != nullptr) {
265+
pScan->m_pScanCallbacks->onScanEnd(pScan->m_scanResults);
266+
}
267+
}
268+
259269
if (rc != 0) {
260270
m_lastErr = rc;
261-
NimBLEDevice::setConnectionInProgress(false);
262271
return false;
263272
}
264273

@@ -985,7 +994,6 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
985994
return 0;
986995
}
987996

988-
NimBLEDevice::setConnectionInProgress(false);
989997
rc = event->connect.status;
990998
if (rc == 0) {
991999
pClient->m_connHandle = event->connect.conn_handle;

src/NimBLEDevice.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ std::vector<NimBLEAddress> NimBLEDevice::m_ignoreList{};
101101
std::vector<NimBLEAddress> NimBLEDevice::m_whiteList{};
102102
uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC};
103103

104-
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
105-
bool NimBLEDevice::m_connectionInProgress{false};
106-
# endif
107-
108104
# ifdef ESP_PLATFORM
109105
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
110106
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE};
@@ -1239,23 +1235,6 @@ bool NimBLEDevice::setCustomGapHandler(gap_event_handler handler) {
12391235
return rc == 0;
12401236
} // setCustomGapHandler
12411237

1242-
/**
1243-
* @brief Set the connection in progress flag.
1244-
* @param [in] inProgress The connection in progress flag.
1245-
* @details This is used to prevent a scan from starting while a connection is in progress.
1246-
*/
1247-
void NimBLEDevice::setConnectionInProgress(bool inProgress) {
1248-
m_connectionInProgress = inProgress;
1249-
} // setConnectionInProgress
1250-
1251-
/**
1252-
* @brief Check if a connection is in progress.
1253-
* @return True if a connection is in progress.
1254-
*/
1255-
bool NimBLEDevice::isConnectionInProgress() {
1256-
return m_connectionInProgress;
1257-
} // isConnectionInProgress
1258-
12591238
/**
12601239
* @brief Return a string representation of the address of this device.
12611240
* @return A string representation of this device address.

src/NimBLEDevice.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ class NimBLEDevice {
184184
static bool isBonded(const NimBLEAddress& address);
185185
static bool deleteAllBonds();
186186
static NimBLEAddress getBondedAddress(int index);
187-
static void setConnectionInProgress(bool inProgress);
188-
static bool isConnectionInProgress();
189187
# endif
190188

191189
private:
@@ -197,10 +195,6 @@ class NimBLEDevice {
197195
static uint8_t m_ownAddrType;
198196
static std::vector<NimBLEAddress> m_whiteList;
199197

200-
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
201-
static bool m_connectionInProgress;
202-
# endif
203-
204198
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
205199
static NimBLEScan* m_pScan;
206200
# endif

src/NimBLEScan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ bool NimBLEScan::isScanning() {
291291
bool NimBLEScan::start(uint32_t duration, bool is_continue) {
292292
NIMBLE_LOGD(LOG_TAG, ">> start: duration=%" PRIu32, duration);
293293

294-
if (NimBLEDevice::isConnectionInProgress()) {
294+
if (ble_gap_conn_active()) {
295295
NIMBLE_LOGE(LOG_TAG, "Connection in progress, cannot start scan");
296296
return false;
297297
}

src/NimBLEScan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class NimBLEScan {
8282

8383
private:
8484
friend class NimBLEDevice;
85+
friend class NimBLEClient;
8586

8687
NimBLEScan();
8788
~NimBLEScan();

0 commit comments

Comments
 (0)