Skip to content

Commit 91251ea

Browse files
committed
[Bugfix] HCI response to disconnect of unknown ID should return success.
* Update macro use to use conversion macros.
1 parent afe1221 commit 91251ea

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

src/NimBLEClient.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ bool NimBLEClient::secureConnection(bool async) const {
354354
if (NimBLEDevice::startSecurity(m_connHandle)) {
355355
NimBLEUtils::taskWait(taskData, BLE_NPL_TIME_FOREVER);
356356
}
357-
} while (taskData.m_flags == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
357+
} while (taskData.m_flags == BLE_HS_HCI_ERR(BLE_ERR_PINKEY_MISSING) && retryCount--);
358358

359359
m_pTaskData = nullptr;
360360

@@ -364,7 +364,10 @@ bool NimBLEClient::secureConnection(bool async) const {
364364
}
365365

366366
m_lastErr = taskData.m_flags;
367-
NIMBLE_LOGE(LOG_TAG, "secureConnection: failed rc=%d", taskData.m_flags);
367+
NIMBLE_LOGE(LOG_TAG,
368+
"secureConnection: failed rc=%d %s",
369+
taskData.m_flags,
370+
NimBLEUtils::returnCodeToString(taskData.m_flags));
368371
return false;
369372

370373
} // secureConnection
@@ -375,14 +378,19 @@ bool NimBLEClient::secureConnection(bool async) const {
375378
*/
376379
bool NimBLEClient::disconnect(uint8_t reason) {
377380
int rc = ble_gap_terminate(m_connHandle, reason);
378-
if (rc != 0 && rc != BLE_HS_ENOTCONN && rc != BLE_HS_EALREADY) {
379-
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
380-
m_lastErr = rc;
381-
return false;
381+
switch (rc) {
382+
case 0:
383+
m_connStatus = DISCONNECTING;
384+
return true;
385+
case BLE_HS_ENOTCONN:
386+
case BLE_HS_EALREADY:
387+
case BLE_HS_HCI_ERR(BLE_ERR_UNK_CONN_ID): // should not happen but just in case
388+
return true;
382389
}
383390

384-
m_connStatus = (rc == BLE_HS_ENOTCONN) ? DISCONNECTED : DISCONNECTING;
385-
return true;
391+
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
392+
m_lastErr = rc;
393+
return false;
386394
} // disconnect
387395

388396
/**
@@ -1051,7 +1059,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
10511059
// set this incase the client instance was changed due to incorrect event arg bug above
10521060
pTaskData = pClient->m_pTaskData;
10531061

1054-
const int connEstablishFailReason = BLE_HS_ERR_HCI_BASE + BLE_ERR_CONN_ESTABLISHMENT;
1062+
const int connEstablishFailReason = BLE_HS_HCI_ERR(BLE_ERR_CONN_ESTABLISHMENT);
10551063
if (rc == connEstablishFailReason && pClient->m_connectFailRetryCount < pClient->m_config.connectFailRetries) {
10561064
pClient->m_connHandle = BLE_HS_CONN_HANDLE_NONE;
10571065
++pClient->m_connectFailRetryCount;
@@ -1263,16 +1271,15 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
12631271
pTaskData = nullptr;
12641272
}
12651273

1266-
if (event->enc_change.status == 0 ||
1267-
event->enc_change.status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) {
1274+
if (event->enc_change.status == 0 || event->enc_change.status == BLE_HS_HCI_ERR(BLE_ERR_PINKEY_MISSING)) {
12681275
NimBLEConnInfo peerInfo;
12691276
rc = ble_gap_conn_find(event->enc_change.conn_handle, &peerInfo.m_desc);
12701277
if (rc != 0) {
12711278
rc = 0;
12721279
break;
12731280
}
12741281

1275-
if (event->enc_change.status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) {
1282+
if (event->enc_change.status == BLE_HS_HCI_ERR(BLE_ERR_PINKEY_MISSING)) {
12761283
// Key is missing, try deleting.
12771284
ble_store_util_delete_peer(&peerInfo.m_desc.peer_id_addr);
12781285
// Attempt a retry if async secure failed.

src/NimBLEServer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,15 @@ bool NimBLEServer::start() {
335335
*/
336336
bool NimBLEServer::disconnect(uint16_t connHandle, uint8_t reason) const {
337337
int rc = ble_gap_terminate(connHandle, reason);
338-
if (rc != 0 && rc != BLE_HS_ENOTCONN && rc != BLE_HS_EALREADY) {
339-
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
340-
return false;
338+
switch (rc) {
339+
case 0:
340+
case BLE_HS_ENOTCONN:
341+
case BLE_HS_EALREADY:
342+
case BLE_HS_HCI_ERR(BLE_ERR_UNK_CONN_ID):
343+
return true;
341344
}
342-
343-
return true;
345+
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
346+
return false;
344347
} // disconnect
345348

346349
/**

0 commit comments

Comments
 (0)