NimBLE-Arduino 2.3.9
Loading...
Searching...
No Matches
NimBLEConnInfo.h
1/*
2 * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_CONNINFO_H_
19#define NIMBLE_CPP_CONNINFO_H_
20
21#if defined(CONFIG_NIMBLE_CPP_IDF)
22# include "host/ble_gap.h"
23#else
24# include "nimble/nimble/host/include/host/ble_gap.h"
25#endif
26
27#include "NimBLEAddress.h"
28#include <cstdio>
29
34 public:
36 NimBLEAddress getAddress() const { return NimBLEAddress(m_desc.peer_ota_addr); }
37
39 NimBLEAddress getIdAddress() const { return NimBLEAddress(m_desc.peer_id_addr); }
40
42 uint16_t getConnHandle() const { return m_desc.conn_handle; }
43
45 uint16_t getConnInterval() const { return m_desc.conn_itvl; }
46
48 uint16_t getConnTimeout() const { return m_desc.supervision_timeout; }
49
51 uint16_t getConnLatency() const { return m_desc.conn_latency; }
52
54 uint16_t getMTU() const { return ble_att_mtu(m_desc.conn_handle); }
55
57 bool isMaster() const { return (m_desc.role == BLE_GAP_ROLE_MASTER); }
58
60 bool isSlave() const { return (m_desc.role == BLE_GAP_ROLE_SLAVE); }
61
63 bool isBonded() const { return (m_desc.sec_state.bonded == 1); }
64
66 bool isEncrypted() const { return (m_desc.sec_state.encrypted == 1); }
67
69 bool isAuthenticated() const { return (m_desc.sec_state.authenticated == 1); }
70
72 uint8_t getSecKeySize() const { return m_desc.sec_state.key_size; }
73
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(),
97 getConnInterval() * 1.25f,
98 getConnTimeout() * 10,
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
109 private:
110 friend class NimBLEServer;
111 friend class NimBLEClient;
112 friend class NimBLECharacteristic;
113 friend class NimBLEDescriptor;
114
115 ble_gap_conn_desc m_desc{};
116 NimBLEConnInfo() {};
117 NimBLEConnInfo(ble_gap_conn_desc desc) { m_desc = desc; }
118};
119
120#endif // NIMBLE_CPP_CONNINFO_H_
A BLE device address.
Definition NimBLEAddress.h:42
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:41
A model of a BLE client.
Definition NimBLEClient.h:49
Connection information.
Definition NimBLEConnInfo.h:33
uint16_t getConnLatency() const
Gets the allowable latency for this connection (unit = number of intervals)
Definition NimBLEConnInfo.h:51
NimBLEAddress getAddress() const
Gets the over-the-air address of the connected peer.
Definition NimBLEConnInfo.h:36
uint16_t getMTU() const
Gets the maximum transmission unit size for this connection (in bytes)
Definition NimBLEConnInfo.h:54
NimBLEAddress getIdAddress() const
Gets the ID address of the connected peer.
Definition NimBLEConnInfo.h:39
bool isEncrypted() const
Check if the connection in encrypted.
Definition NimBLEConnInfo.h:66
uint8_t getSecKeySize() const
Gets the key size used to encrypt the connection.
Definition NimBLEConnInfo.h:72
bool isMaster() const
Check if we are in the master role in this connection.
Definition NimBLEConnInfo.h:57
std::string toString() const
Get a string representation of the connection info, useful for debugging.
Definition NimBLEConnInfo.h:75
bool isAuthenticated() const
Check if the the connection has been authenticated.
Definition NimBLEConnInfo.h:69
uint16_t getConnHandle() const
Gets the connection handle (also known as the connection id) of the connected peer.
Definition NimBLEConnInfo.h:42
uint16_t getConnTimeout() const
Gets the supervision timeout for this connection (in 10ms units)
Definition NimBLEConnInfo.h:48
uint16_t getConnInterval() const
Gets the connection interval for this connection (in 1.25ms units)
Definition NimBLEConnInfo.h:45
bool isSlave() const
Check if we are in the slave role in this connection.
Definition NimBLEConnInfo.h:60
bool isBonded() const
Check if we are connected to a bonded peer.
Definition NimBLEConnInfo.h:63
A model of a BLE descriptor.
Definition NimBLEDescriptor.h:33
The model of a BLE server.
Definition NimBLEServer.h:62