From bfe68f4a91881719727ff9c20f26aa6dc327a05e Mon Sep 17 00:00:00 2001 From: h2zero <32826625+h2zero@users.noreply.github.com> Date: Fri, 26 Aug 2022 19:51:19 -0600 Subject: [PATCH] [Breaking] Add disconnect reason to client callback. (#398) (#81) Adds the reason code as a parameter to the client onDisconnect callback. * Update examples/docs. --- docs/Migration_guide.md | 8 ++++++++ .../Bluetooth_5/NimBLE_extended_client/main/main.cpp | 5 +++-- examples/basic/BLE_client/main/main.cpp | 4 +++- src/NimBLEClient.cpp | 12 ++---------- src/NimBLEClient.h | 5 +---- src/NimBLEDevice.cpp | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/Migration_guide.md b/docs/Migration_guide.md index 4283012..e50d09d 100644 --- a/docs/Migration_guide.md +++ b/docs/Migration_guide.md @@ -16,6 +16,7 @@ For more information on the improvements and additions please refer to the [clas * [Client](#client-api) * [Remote Services](#remote-services) * [Remote characteristics](#remote-characteristics) + * [Client Callbacks](#client-callbacks) * [Security](#client-security) * [Scanning](#scan-api) * [General Security](#security-api) @@ -316,6 +317,13 @@ the currently known database returned (false : default). Also now returns a pointer to `std::vector` instead of `std::map`.
+ +### Client callbacks + +> `BLEClientCallbacks::onDisconnect` (`NimBLEClientCallbacks::onDisconnect`) + +This now takes a second parameter `int reason` which provides the reason code for disconnection. + ### Client Security The client will automatically initiate security when the peripheral responds that it's required. diff --git a/examples/Bluetooth_5/NimBLE_extended_client/main/main.cpp b/examples/Bluetooth_5/NimBLE_extended_client/main/main.cpp index 4b37285..30baeb5 100644 --- a/examples/Bluetooth_5/NimBLE_extended_client/main/main.cpp +++ b/examples/Bluetooth_5/NimBLE_extended_client/main/main.cpp @@ -29,8 +29,9 @@ class ClientCallbacks : public NimBLEClientCallbacks { printf("Connected\n"); }; - void onDisconnect(NimBLEClient* pClient) { - printf("%s Disconnected - Starting scan\n", pClient->getPeerAddress().toString().c_str()); + void onDisconnect(NimBLEClient* pClient, int reason) { + printf("%s Disconnected, reason = %d - Starting scan\n", + pClient->getPeerAddress().toString().c_str(), reason); NimBLEDevice::getScan()->start(scanTime, scanEndedCB); }; }; diff --git a/examples/basic/BLE_client/main/main.cpp b/examples/basic/BLE_client/main/main.cpp index 26862c1..f092bca 100644 --- a/examples/basic/BLE_client/main/main.cpp +++ b/examples/basic/BLE_client/main/main.cpp @@ -43,7 +43,9 @@ class MyClientCallback : public BLEClientCallbacks { void onConnect(BLEClient* pclient) { } - void onDisconnect(BLEClient* pclient) { + /** onDisconnect now takes a reason parameter to indicate the reason for disconnection + void onDisconnect(BLEClient* pclient) { */ + void onDisconnect(BLEClient* pclient, int reason) { connected = false; printf("onDisconnect"); } diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 2899323..ecb5452 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -973,7 +973,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) { rc, NimBLEUtils::returnCodeToString(rc)); client->m_connEstablished = false; - client->m_pClientCallbacks->onDisconnect(client); + client->m_pClientCallbacks->onDisconnect(client, rc); break; } // BLE_GAP_EVENT_DISCONNECT @@ -1248,7 +1248,7 @@ void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) { NIMBLE_LOGD("NimBLEClientCallbacks", "onConnect: default"); } -void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient) { +void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient, int reason) { NIMBLE_LOGD("NimBLEClientCallbacks", "onDisconnect: default"); } @@ -1261,15 +1261,7 @@ uint32_t NimBLEClientCallbacks::onPassKeyRequest(){ NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyRequest: default: 123456"); return 123456; } -/* -void NimBLEClientCallbacks::onPassKeyNotify(uint32_t pass_key){ - NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyNotify: default: %d", pass_key); -} -bool NimBLEClientCallbacks::onSecurityRequest(){ - NIMBLE_LOGD("NimBLEClientCallbacks", "onSecurityRequest: default: true"); - return true; -}*/ void NimBLEClientCallbacks::onAuthenticationComplete(ble_gap_conn_desc* desc){ NIMBLE_LOGD("NimBLEClientCallbacks", "onAuthenticationComplete: default"); } diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index d4e349d..91766cb 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -131,7 +131,7 @@ public: * @brief Called when disconnected from the server. * @param [in] pClient A pointer to the calling client object. */ - virtual void onDisconnect(NimBLEClient* pClient); + virtual void onDisconnect(NimBLEClient* pClient, int reason); /** * @brief Called when server requests to update the connection parameters. @@ -147,9 +147,6 @@ public: */ virtual uint32_t onPassKeyRequest(); - /*virtual void onPassKeyNotify(uint32_t pass_key); - virtual bool onSecurityRequest();*/ - /** * @brief Called when the pairing procedure is complete. * @param [in] desc A pointer to the struct containing the connection information.\n diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 6563944..ed9eb6d 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -253,7 +253,7 @@ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) { } // Since we set the flag to false the app will not get a callback // in the disconnect event so we call it here for good measure. - pClient->m_pClientCallbacks->onDisconnect(pClient); + pClient->m_pClientCallbacks->onDisconnect(pClient, BLE_ERR_CONN_TERM_LOCAL); } else if(pClient->m_pTaskData != nullptr) { rc = ble_gap_conn_cancel();