@@ -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,13 +378,20 @@ bool NimBLEClient::secureConnection(bool async) const {
375378 */
376379bool 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+ case BLE_HS_ENOTCONN:
384+ case BLE_HS_EALREADY:
385+ break ;
386+ case BLE_HS_HCI_ERR (BLE_ERR_UNK_CONN_ID): // should not happen but just in case
387+ return true ;
388+ default :
389+ NIMBLE_LOGE (LOG_TAG, " ble_gap_terminate failed: rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
390+ m_lastErr = rc;
391+ return false ;
382392 }
383393
384- m_connStatus = ( rc == BLE_HS_ENOTCONN) ? DISCONNECTED : DISCONNECTING;
394+ m_connStatus = rc == BLE_HS_ENOTCONN ? DISCONNECTED : DISCONNECTING;
385395 return true ;
386396} // disconnect
387397
@@ -1051,7 +1061,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
10511061 // set this incase the client instance was changed due to incorrect event arg bug above
10521062 pTaskData = pClient->m_pTaskData ;
10531063
1054- const int connEstablishFailReason = BLE_HS_ERR_HCI_BASE + BLE_ERR_CONN_ESTABLISHMENT;
1064+ const int connEstablishFailReason = BLE_HS_HCI_ERR ( BLE_ERR_CONN_ESTABLISHMENT) ;
10551065 if (rc == connEstablishFailReason && pClient->m_connectFailRetryCount < pClient->m_config .connectFailRetries ) {
10561066 pClient->m_connHandle = BLE_HS_CONN_HANDLE_NONE;
10571067 ++pClient->m_connectFailRetryCount ;
@@ -1263,16 +1273,15 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
12631273 pTaskData = nullptr ;
12641274 }
12651275
1266- if (event->enc_change .status == 0 ||
1267- event->enc_change .status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) {
1276+ if (event->enc_change .status == 0 || event->enc_change .status == BLE_HS_HCI_ERR (BLE_ERR_PINKEY_MISSING)) {
12681277 NimBLEConnInfo peerInfo;
12691278 rc = ble_gap_conn_find (event->enc_change .conn_handle , &peerInfo.m_desc );
12701279 if (rc != 0 ) {
12711280 rc = 0 ;
12721281 break ;
12731282 }
12741283
1275- if (event->enc_change .status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) {
1284+ if (event->enc_change .status == BLE_HS_HCI_ERR ( BLE_ERR_PINKEY_MISSING)) {
12761285 // Key is missing, try deleting.
12771286 ble_store_util_delete_peer (&peerInfo.m_desc .peer_id_addr );
12781287 // Attempt a retry if async secure failed.
0 commit comments