[Breaking] Add disconnect reason to client callback. (#398) (#81)

Adds the reason code as a parameter to the client onDisconnect callback.

* Update examples/docs.
This commit is contained in:
h2zero 2022-08-26 19:51:19 -06:00 committed by GitHub
parent bb3dd5f114
commit bfe68f4a91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 18 deletions

View file

@ -16,6 +16,7 @@ For more information on the improvements and additions please refer to the [clas
* [Client](#client-api) * [Client](#client-api)
* [Remote Services](#remote-services) * [Remote Services](#remote-services)
* [Remote characteristics](#remote-characteristics) * [Remote characteristics](#remote-characteristics)
* [Client Callbacks](#client-callbacks)
* [Security](#client-security) * [Security](#client-security)
* [Scanning](#scan-api) * [Scanning](#scan-api)
* [General Security](#security-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`. Also now returns a pointer to `std::vector` instead of `std::map`.
<br/> <br/>
<a name="client-callbacks"></a>
### Client callbacks
> `BLEClientCallbacks::onDisconnect` (`NimBLEClientCallbacks::onDisconnect`)
This now takes a second parameter `int reason` which provides the reason code for disconnection.
<a name="client-security"></a> <a name="client-security"></a>
### Client Security ### Client Security
The client will automatically initiate security when the peripheral responds that it's required. The client will automatically initiate security when the peripheral responds that it's required.

View file

@ -29,8 +29,9 @@ class ClientCallbacks : public NimBLEClientCallbacks {
printf("Connected\n"); printf("Connected\n");
}; };
void onDisconnect(NimBLEClient* pClient) { void onDisconnect(NimBLEClient* pClient, int reason) {
printf("%s Disconnected - Starting scan\n", pClient->getPeerAddress().toString().c_str()); printf("%s Disconnected, reason = %d - Starting scan\n",
pClient->getPeerAddress().toString().c_str(), reason);
NimBLEDevice::getScan()->start(scanTime, scanEndedCB); NimBLEDevice::getScan()->start(scanTime, scanEndedCB);
}; };
}; };

View file

@ -43,7 +43,9 @@ class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) { 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; connected = false;
printf("onDisconnect"); printf("onDisconnect");
} }

View file

@ -973,7 +973,7 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) {
rc, NimBLEUtils::returnCodeToString(rc)); rc, NimBLEUtils::returnCodeToString(rc));
client->m_connEstablished = false; client->m_connEstablished = false;
client->m_pClientCallbacks->onDisconnect(client); client->m_pClientCallbacks->onDisconnect(client, rc);
break; break;
} // BLE_GAP_EVENT_DISCONNECT } // BLE_GAP_EVENT_DISCONNECT
@ -1248,7 +1248,7 @@ void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) {
NIMBLE_LOGD("NimBLEClientCallbacks", "onConnect: default"); NIMBLE_LOGD("NimBLEClientCallbacks", "onConnect: default");
} }
void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient) { void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient, int reason) {
NIMBLE_LOGD("NimBLEClientCallbacks", "onDisconnect: default"); NIMBLE_LOGD("NimBLEClientCallbacks", "onDisconnect: default");
} }
@ -1261,15 +1261,7 @@ uint32_t NimBLEClientCallbacks::onPassKeyRequest(){
NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyRequest: default: 123456"); NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyRequest: default: 123456");
return 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){ void NimBLEClientCallbacks::onAuthenticationComplete(ble_gap_conn_desc* desc){
NIMBLE_LOGD("NimBLEClientCallbacks", "onAuthenticationComplete: default"); NIMBLE_LOGD("NimBLEClientCallbacks", "onAuthenticationComplete: default");
} }

View file

@ -131,7 +131,7 @@ public:
* @brief Called when disconnected from the server. * @brief Called when disconnected from the server.
* @param [in] pClient A pointer to the calling client object. * @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. * @brief Called when server requests to update the connection parameters.
@ -147,9 +147,6 @@ public:
*/ */
virtual uint32_t onPassKeyRequest(); virtual uint32_t onPassKeyRequest();
/*virtual void onPassKeyNotify(uint32_t pass_key);
virtual bool onSecurityRequest();*/
/** /**
* @brief Called when the pairing procedure is complete. * @brief Called when the pairing procedure is complete.
* @param [in] desc A pointer to the struct containing the connection information.\n * @param [in] desc A pointer to the struct containing the connection information.\n

View file

@ -253,7 +253,7 @@ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) {
} }
// Since we set the flag to false the app will not get a callback // 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. // 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) { } else if(pClient->m_pTaskData != nullptr) {
rc = ble_gap_conn_cancel(); rc = ble_gap_conn_cancel();