From 7a82067177604690ba58adb16ccde936f13c3855 Mon Sep 17 00:00:00 2001 From: h2zero Date: Wed, 29 Dec 2021 08:12:38 -0700 Subject: [PATCH] Add last error function to client. --- src/NimBLEClient.cpp | 19 +++++++++++++++++++ src/NimBLEClient.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index b1e4634..9f4e08f 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -63,6 +63,7 @@ NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(pee m_deleteCallbacks = false; m_pTaskData = nullptr; m_connEstablished = false; + m_lastErr = 0; m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default) m_pConnParams.scan_window = 16; // Scan window in 0.625ms units (NimBLE Default) @@ -252,6 +253,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { } while (rc == BLE_HS_EBUSY); + m_lastErr = rc; + if(rc != 0) { m_pTaskData = nullptr; return false; @@ -274,6 +277,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { return false; } else if(taskData.rc != 0){ + m_lastErr = taskData.rc; NIMBLE_LOGE(LOG_TAG, "Connection failed; status=%d %s", taskData.rc, NimBLEUtils::returnCodeToString(taskData.rc)); @@ -315,6 +319,7 @@ bool NimBLEClient::secureConnection() { int rc = NimBLEDevice::startSecurity(m_conn_id); if(rc != 0){ + m_lastErr = rc; m_pTaskData = nullptr; return false; } @@ -323,6 +328,7 @@ bool NimBLEClient::secureConnection() { } while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--); if(taskData.rc != 0){ + m_lastErr = taskData.rc; return false; } @@ -373,6 +379,7 @@ int NimBLEClient::disconnect(uint8_t reason) { } NIMBLE_LOGD(LOG_TAG, "<< disconnect()"); + m_lastErr = rc; return rc; } // disconnect @@ -535,6 +542,7 @@ int NimBLEClient::getRssi() { if(rc != 0) { NIMBLE_LOGE(LOG_TAG, "Failed to read RSSI error code: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); + m_lastErr = rc; return 0; } @@ -673,11 +681,13 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) { if (rc != 0) { NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_svcs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); + m_lastErr = rc; return false; } // wait until we have all the services ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + m_lastErr = taskData.rc; if(taskData.rc == 0){ NIMBLE_LOGD(LOG_TAG, "<< retrieveServices"); @@ -1159,6 +1169,15 @@ std::string NimBLEClient::toString() { } // toString +/** + * @brief Get the last error code reported by the NimBLE host + * @return int, the NimBLE error code. + */ +int NimBLEClient::getLastError() { + return m_lastErr; +} // getLastError + + void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) { NIMBLE_LOGD("NimBLEClientCallbacks", "onConnect: default"); } diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 3333f3c..37700b7 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -71,6 +71,7 @@ public: void setDataLen(uint16_t tx_octets); void discoverAttributes(); NimBLEConnInfo getConnInfo(); + int getLastError(); private: NimBLEClient(const NimBLEAddress &peerAddress); @@ -88,6 +89,7 @@ private: bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr); NimBLEAddress m_peerAddress; + int m_lastErr; uint16_t m_conn_id; bool m_connEstablished; bool m_deleteCallbacks;