Skip to content

Commit

Permalink
Revert #697277f and replace with stack checks.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
h2zero committed Nov 18, 2024
1 parent 78cce34 commit b719a39
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 38 deletions.
10 changes: 0 additions & 10 deletions src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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,
Expand Down Expand Up @@ -258,7 +250,6 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes,

if (rc != 0) {
m_lastErr = rc;
NimBLEDevice::setConnectionInProgress(false);
return false;
}

Expand Down Expand Up @@ -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;
Expand Down
21 changes: 0 additions & 21 deletions src/NimBLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ std::vector<NimBLEAddress> NimBLEDevice::m_ignoreList{};
std::vector<NimBLEAddress> 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};
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions src/NimBLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -197,10 +195,6 @@ class NimBLEDevice {
static uint8_t m_ownAddrType;
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)
static NimBLEScan* m_pScan;
# endif
Expand Down
2 changes: 1 addition & 1 deletion src/NimBLEScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit b719a39

Please sign in to comment.