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.
This commit is contained in:
h2zero 2024-11-17 16:40:28 -07:00 committed by h2zero
parent fbbcfadc0c
commit 5f2730de02
4 changed files with 1 additions and 38 deletions

View file

@ -176,11 +176,6 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
return false; return false;
} }
if (NimBLEDevice::isConnectionInProgress()) {
NIMBLE_LOGE(LOG_TAG, "Connection already in progress");
return false;
}
const ble_addr_t* peerAddr = address.getBase(); const ble_addr_t* peerAddr = address.getBase();
if (ble_gap_conn_find_by_addr(peerAddr, NULL) == 0) { if (ble_gap_conn_find_by_addr(peerAddr, NULL) == 0) {
NIMBLE_LOGE(LOG_TAG, "A connection to %s already exists", address.toString().c_str()); 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.asyncConnect = asyncConnect;
m_config.exchangeMTU = exchangeMTU; m_config.exchangeMTU = exchangeMTU;
// Set the connection in progress flag to prevent a scan from starting while connecting.
NimBLEDevice::setConnectionInProgress(true);
do { do {
# if CONFIG_BT_NIMBLE_EXT_ADV # if CONFIG_BT_NIMBLE_EXT_ADV
rc = ble_gap_ext_connect(NimBLEDevice::m_ownAddrType, rc = ble_gap_ext_connect(NimBLEDevice::m_ownAddrType,
@ -258,7 +250,6 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,
if (rc != 0) { if (rc != 0) {
m_lastErr = rc; m_lastErr = rc;
NimBLEDevice::setConnectionInProgress(false);
return false; return false;
} }
@ -985,7 +976,6 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
return 0; return 0;
} }
NimBLEDevice::setConnectionInProgress(false);
rc = event->connect.status; rc = event->connect.status;
if (rc == 0) { if (rc == 0) {
pClient->m_connHandle = event->connect.conn_handle; pClient->m_connHandle = event->connect.conn_handle;

View file

@ -101,10 +101,6 @@ std::vector<NimBLEAddress> NimBLEDevice::m_ignoreList{};
std::vector<NimBLEAddress> NimBLEDevice::m_whiteList{}; std::vector<NimBLEAddress> NimBLEDevice::m_whiteList{};
uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC}; 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 ESP_PLATFORM
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL # ifdef CONFIG_BTDM_BLE_SCAN_DUPL
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE}; 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; return rc == 0;
} // setCustomGapHandler } // 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. * @brief Return a string representation of the address of this device.
* @return A string representation of this device address. * @return A string representation of this device address.

View file

@ -184,8 +184,6 @@ class NimBLEDevice {
static bool isBonded(const NimBLEAddress& address); static bool isBonded(const NimBLEAddress& address);
static bool deleteAllBonds(); static bool deleteAllBonds();
static NimBLEAddress getBondedAddress(int index); static NimBLEAddress getBondedAddress(int index);
static void setConnectionInProgress(bool inProgress);
static bool isConnectionInProgress();
# endif # endif
private: private:
@ -197,10 +195,6 @@ class NimBLEDevice {
static uint8_t m_ownAddrType; static uint8_t m_ownAddrType;
static std::vector<NimBLEAddress> m_whiteList; static std::vector<NimBLEAddress> 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) # if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
static NimBLEScan* m_pScan; static NimBLEScan* m_pScan;
# endif # endif

View file

@ -291,7 +291,7 @@ bool NimBLEScan::isScanning() {
bool NimBLEScan::start(uint32_t duration, bool is_continue) { bool NimBLEScan::start(uint32_t duration, bool is_continue) {
NIMBLE_LOGD(LOG_TAG, ">> start: duration=%" PRIu32, duration); 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"); NIMBLE_LOGE(LOG_TAG, "Connection in progress, cannot start scan");
return false; return false;
} }