Skip to content

Commit 437e651

Browse files
committed
Add NimBLEConnInfo::toString method.
1 parent ea7075a commit 437e651

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

examples/Bluetooth_5/NimBLE_extended_server/main/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M;
3636
/** Handler class for server events */
3737
class ServerCallbacks : public NimBLEServerCallbacks {
3838
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override {
39-
printf("Client connected:: %s\n", connInfo.getAddress().toString().c_str());
39+
printf("Client connected:\n%s", connInfo.toString().c_str());
4040
}
4141

4242
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override {

examples/Bluetooth_5/NimBLE_multi_advertiser/main/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M;
3636
/** Handler class for server events */
3737
class ServerCallbacks : public NimBLEServerCallbacks {
3838
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override {
39-
printf("Client connected: %s\n", connInfo.getAddress().toString().c_str());
39+
printf("Client connected:\n%s", connInfo.toString().c_str());
4040
}
4141

4242
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override {

examples/NimBLE_Server/main/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static NimBLEServer* pServer;
1616
** Remove as you see fit for your needs */
1717
class ServerCallbacks : public NimBLEServerCallbacks {
1818
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) override {
19-
printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
19+
printf("Client connected:\n%s", connInfo.toString().c_str());
2020

2121
/**
2222
* We can use the connection handle here to ask for different connection parameters.

src/NimBLEConnInfo.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626

2727
#include "NimBLEAddress.h"
28+
#include <cstdio>
2829

2930
/**
3031
* @brief Connection information.
@@ -70,6 +71,41 @@ class NimBLEConnInfo {
7071
/** @brief Gets the key size used to encrypt the connection */
7172
uint8_t getSecKeySize() const { return m_desc.sec_state.key_size; }
7273

74+
/** @brief Get a string representation of the connection info, useful for debugging */
75+
std::string toString() const {
76+
std::string str;
77+
// 294 chars max expected from all labels + worst-case values, round up to 300.
78+
str.resize(300);
79+
80+
snprintf(&str[0],
81+
str.size(),
82+
" Address: %s\n"
83+
" ID Address: %s\n"
84+
" Connection Handle: %u\n"
85+
" Connection Interval: %.1f ms\n"
86+
" Connection Timeout: %u ms\n"
87+
" Connection Latency: %u\n"
88+
" MTU: %u bytes\n"
89+
" Role: %s\n"
90+
" Bonded: %s\n"
91+
" Encrypted: %s\n"
92+
" Authenticated: %s\n"
93+
" Security Key Size: %u\n",
94+
getAddress().toString().c_str(),
95+
getIdAddress().toString().c_str(),
96+
getConnHandle(),
97+
getConnInterval() * 1.25f,
98+
getConnTimeout() * 10,
99+
getConnLatency(),
100+
getMTU(),
101+
isMaster() ? "Master" : "Slave",
102+
isBonded() ? "Yes" : "No",
103+
isEncrypted() ? "Yes" : "No",
104+
isAuthenticated() ? "Yes" : "No",
105+
getSecKeySize());
106+
return str;
107+
}
108+
73109
private:
74110
friend class NimBLEServer;
75111
friend class NimBLEClient;

0 commit comments

Comments
 (0)