Skip to content

Commit ccddf0d

Browse files
committed
Add last error function to client. (#300)
1 parent 725079a commit ccddf0d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/NimBLEClient.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(pee
6363
m_deleteCallbacks = false;
6464
m_pTaskData = nullptr;
6565
m_connEstablished = false;
66+
m_lastErr = 0;
6667

6768
m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
6869
m_pConnParams.scan_window = 16; // Scan window in 0.625ms units (NimBLE Default)
@@ -251,6 +252,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
251252

252253
} while (rc == BLE_HS_EBUSY);
253254

255+
m_lastErr = rc;
256+
254257
if(rc != 0) {
255258
m_pTaskData = nullptr;
256259
return false;
@@ -273,6 +276,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
273276
return false;
274277

275278
} else if(taskData.rc != 0){
279+
m_lastErr = taskData.rc;
276280
NIMBLE_LOGE(LOG_TAG, "Connection failed; status=%d %s",
277281
taskData.rc,
278282
NimBLEUtils::returnCodeToString(taskData.rc));
@@ -314,6 +318,7 @@ bool NimBLEClient::secureConnection() {
314318

315319
int rc = NimBLEDevice::startSecurity(m_conn_id);
316320
if(rc != 0){
321+
m_lastErr = rc;
317322
m_pTaskData = nullptr;
318323
return false;
319324
}
@@ -322,6 +327,7 @@ bool NimBLEClient::secureConnection() {
322327
} while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
323328

324329
if(taskData.rc != 0){
330+
m_lastErr = taskData.rc;
325331
return false;
326332
}
327333

@@ -372,6 +378,7 @@ int NimBLEClient::disconnect(uint8_t reason) {
372378
}
373379

374380
NIMBLE_LOGD(LOG_TAG, "<< disconnect()");
381+
m_lastErr = rc;
375382
return rc;
376383
} // disconnect
377384

@@ -512,6 +519,7 @@ int NimBLEClient::getRssi() {
512519
if(rc != 0) {
513520
NIMBLE_LOGE(LOG_TAG, "Failed to read RSSI error code: %d, %s",
514521
rc, NimBLEUtils::returnCodeToString(rc));
522+
m_lastErr = rc;
515523
return 0;
516524
}
517525

@@ -650,11 +658,13 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
650658

651659
if (rc != 0) {
652660
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_svcs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
661+
m_lastErr = rc;
653662
return false;
654663
}
655664

656665
// wait until we have all the services
657666
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
667+
m_lastErr = taskData.rc;
658668

659669
if(taskData.rc == 0){
660670
NIMBLE_LOGD(LOG_TAG, "<< retrieveServices");
@@ -1136,6 +1146,15 @@ std::string NimBLEClient::toString() {
11361146
} // toString
11371147

11381148

1149+
/**
1150+
* @brief Get the last error code reported by the NimBLE host
1151+
* @return int, the NimBLE error code.
1152+
*/
1153+
int NimBLEClient::getLastError() {
1154+
return m_lastErr;
1155+
} // getLastError
1156+
1157+
11391158
void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) {
11401159
NIMBLE_LOGD("NimBLEClientCallbacks", "onConnect: default");
11411160
}

src/NimBLEClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class NimBLEClient {
7373
uint16_t latency, uint16_t timeout);
7474
void discoverAttributes();
7575
NimBLEConnInfo getConnInfo();
76+
int getLastError();
7677

7778
private:
7879
NimBLEClient(const NimBLEAddress &peerAddress);
@@ -90,6 +91,7 @@ class NimBLEClient {
9091
bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr);
9192

9293
NimBLEAddress m_peerAddress;
94+
int m_lastErr;
9395
uint16_t m_conn_id;
9496
bool m_connEstablished;
9597
bool m_deleteCallbacks;

0 commit comments

Comments
 (0)