From 5f2730de023ddba98edf4b3e06d9c4c8ad61dde5 Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 17 Nov 2024 16:40:28 -0700 Subject: [PATCH] Revert #724e1a7 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. --- src/NimBLEClient.cpp | 10 ---------- src/NimBLEDevice.cpp | 21 --------------------- src/NimBLEDevice.h | 6 ------ src/NimBLEScan.cpp | 2 +- 4 files changed, 1 insertion(+), 38 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 6289cda..decdb45 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -176,11 +176,6 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes, return false; } - if (NimBLEDevice::isConnectionInProgress()) { - NIMBLE_LOGE(LOG_TAG, "Connection already in progress"); - return false; - } - const ble_addr_t* peerAddr = address.getBase(); if (ble_gap_conn_find_by_addr(peerAddr, NULL) == 0) { NIMBLE_LOGE(LOG_TAG, "A connection to %s already exists", address.toString().c_str()); @@ -202,9 +197,6 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes, m_config.asyncConnect = asyncConnect; m_config.exchangeMTU = exchangeMTU; - // 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, @@ -258,7 +250,6 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes, if (rc != 0) { m_lastErr = rc; - NimBLEDevice::setConnectionInProgress(false); return false; } @@ -985,7 +976,6 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) { return 0; } - NimBLEDevice::setConnectionInProgress(false); rc = event->connect.status; if (rc == 0) { pClient->m_connHandle = event->connect.conn_handle; diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index b12ecfd..a858151 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -101,10 +101,6 @@ std::vector NimBLEDevice::m_ignoreList{}; std::vector NimBLEDevice::m_whiteList{}; uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC}; -# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) -bool NimBLEDevice::m_connectionInProgress{false}; -# endif - # ifdef ESP_PLATFORM # ifdef CONFIG_BTDM_BLE_SCAN_DUPL uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE}; @@ -1238,23 +1234,6 @@ bool NimBLEDevice::setCustomGapHandler(gap_event_handler handler) { return rc == 0; } // setCustomGapHandler -/** - * @brief Set the connection in progress flag. - * @param [in] inProgress The connection in progress flag. - * @details This is used to prevent a scan from starting while a connection is in progress. - */ -void NimBLEDevice::setConnectionInProgress(bool inProgress) { - m_connectionInProgress = inProgress; -} // setConnectionInProgress - -/** - * @brief Check if a connection is in progress. - * @return True if a connection is in progress. - */ -bool NimBLEDevice::isConnectionInProgress() { - return m_connectionInProgress; -} // isConnectionInProgress - /** * @brief Return a string representation of the address of this device. * @return A string representation of this device address. diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index 9a722b7..1530377 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -184,8 +184,6 @@ class NimBLEDevice { static bool isBonded(const NimBLEAddress& address); static bool deleteAllBonds(); static NimBLEAddress getBondedAddress(int index); - static void setConnectionInProgress(bool inProgress); - static bool isConnectionInProgress(); # endif private: @@ -197,10 +195,6 @@ class NimBLEDevice { static uint8_t m_ownAddrType; static std::vector m_whiteList; -# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) - static bool m_connectionInProgress; -# endif - # if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) static NimBLEScan* m_pScan; # endif diff --git a/src/NimBLEScan.cpp b/src/NimBLEScan.cpp index d3fc1c6..58b4988 100644 --- a/src/NimBLEScan.cpp +++ b/src/NimBLEScan.cpp @@ -291,7 +291,7 @@ bool NimBLEScan::isScanning() { bool NimBLEScan::start(uint32_t duration, bool is_continue) { NIMBLE_LOGD(LOG_TAG, ">> start: duration=%" PRIu32, duration); - if (NimBLEDevice::isConnectionInProgress()) { + if (ble_gap_conn_active()) { NIMBLE_LOGE(LOG_TAG, "Connection in progress, cannot start scan"); return false; }