diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index ddeb1de..a171b8c 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -436,6 +436,24 @@ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval, } // updateConnParams +/** + * @brief Get detailed information about the current peer connection. + */ +NimBLEConnInfo NimBLEClient::getConnInfo() { + NimBLEConnInfo connInfo; + if (!isConnected()) { + NIMBLE_LOGE(LOG_TAG, "Not connected"); + } else { + int rc = ble_gap_conn_find(m_conn_id, &connInfo.m_desc); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Connection info not found"); + } + } + + return connInfo; +} // getConnInfo + + /** * @brief Set the timeout to wait for connection attempt to complete. * @param [in] time The number of seconds before timeout. diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index a4b8470..d90086c 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -23,6 +23,7 @@ #include "NimBLEAddress.h" #include "NimBLEUUID.h" #include "NimBLEUtils.h" +#include "NimBLEConnInfo.h" #include "NimBLEAdvertisedDevice.h" #include "NimBLERemoteService.h" @@ -71,6 +72,7 @@ public: void updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout); void discoverAttributes(); + NimBLEConnInfo getConnInfo(); private: NimBLEClient(const NimBLEAddress &peerAddress); diff --git a/src/NimBLEConnInfo.h b/src/NimBLEConnInfo.h new file mode 100644 index 0000000..e357d8c --- /dev/null +++ b/src/NimBLEConnInfo.h @@ -0,0 +1,55 @@ +#ifndef NIMBLECONNINFO_H_ +#define NIMBLECONNINFO_H_ + +#include "NimBLEAddress.h" + +/** + * @brief Connection information. + */ +class NimBLEConnInfo { +friend class NimBLEServer; +friend class NimBLEClient; + ble_gap_conn_desc m_desc; + NimBLEConnInfo() { m_desc = {}; } + NimBLEConnInfo(ble_gap_conn_desc desc) { m_desc = desc; } +public: + /** @brief Gets the over-the-air address of the connected peer */ + NimBLEAddress getAddress() { return NimBLEAddress(m_desc.peer_ota_addr); } + + /** @brief Gets the ID address of the connected peer */ + NimBLEAddress getIdAddress() { return NimBLEAddress(m_desc.peer_id_addr); } + + /** @brief Gets the connection handle of the connected peer */ + uint16_t getConnHandle() { return m_desc.conn_handle; } + + /** @brief Gets the connection interval for this connection (in 1.25ms units) */ + uint16_t getConnInterval() { return m_desc.conn_itvl; } + + /** @brief Gets the supervision timeout for this connection (in 10ms units) */ + uint16_t getConnTimeout() { return m_desc.supervision_timeout; } + + /** @brief Gets the allowable latency for this connection (unit = number of intervals) */ + uint16_t getConnLatency() { return m_desc.conn_latency; } + + /** @brief Gets the maximum transmission unit size for this connection (in bytes) */ + uint16_t getMTU() { return ble_att_mtu(m_desc.conn_handle); } + + /** @brief Check if we are in the master role in this connection */ + bool isMaster() { return (m_desc.role == BLE_GAP_ROLE_MASTER); } + + /** @brief Check if we are in the slave role in this connection */ + bool isSlave() { return (m_desc.role == BLE_GAP_ROLE_SLAVE); } + + /** @brief Check if we are connected to a bonded peer */ + bool isBonded() { return (m_desc.sec_state.bonded == 1); } + + /** @brief Check if the connection in encrypted */ + bool isEncrypted() { return (m_desc.sec_state.encrypted == 1); } + + /** @brief Check if the the connection has been authenticated */ + bool isAuthenticated() { return (m_desc.sec_state.authenticated == 1); } + + /** @brief Gets the key size used to encrypt the connection */ + uint8_t getSecKeySize() { return m_desc.sec_state.key_size; } +}; +#endif diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index 423e879..f0a8ea0 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -253,6 +253,63 @@ size_t NimBLEServer::getConnectedCount() { } // getConnectedCount +/** + * @brief Get the vector of the connected client ID's. + */ +std::vector NimBLEServer::getPeerDevices() { + return m_connectedPeersVec; +} // getPeerDevices + + +/** + * @brief Get the connection information of a connected peer by vector index. + * @param [in] index The vector index of the peer. + */ +NimBLEConnInfo NimBLEServer::getPeerInfo(size_t index) { + if (index >= m_connectedPeersVec.size()) { + NIMBLE_LOGE(LOG_TAG, "No peer at index %u", index); + return NimBLEConnInfo(); + } + + return getPeerIDInfo(m_connectedPeersVec[index]); +} // getPeerInfo + + +/** + * @brief Get the connection information of a connected peer by address. + * @param [in] address The address of the peer. + */ +NimBLEConnInfo NimBLEServer::getPeerInfo(const NimBLEAddress& address) { + ble_addr_t peerAddr; + memcpy(&peerAddr.val, address.getNative(),6); + peerAddr.type = address.getType(); + + NimBLEConnInfo peerInfo; + int rc = ble_gap_conn_find_by_addr(&peerAddr, &peerInfo.m_desc); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Peer info not found"); + } + + return peerInfo; +} // getPeerInfo + + +/** + * @brief Get the connection information of a connected peer by connection ID. + * @param [in] id The connection id of the peer. + */ +NimBLEConnInfo NimBLEServer::getPeerIDInfo(uint16_t id) { + NimBLEConnInfo peerInfo; + + int rc = ble_gap_conn_find(id, &peerInfo.m_desc); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Peer info not found"); + } + + return peerInfo; +} // getPeerIDInfo + + /** * @brief Handle a GATT Server Event. * diff --git a/src/NimBLEServer.h b/src/NimBLEServer.h index aaaf287..47309e1 100644 --- a/src/NimBLEServer.h +++ b/src/NimBLEServer.h @@ -25,6 +25,7 @@ #include "NimBLEAdvertising.h" #include "NimBLEService.h" #include "NimBLESecurity.h" +#include "NimBLEConnInfo.h" class NimBLEService; @@ -58,7 +59,10 @@ public: uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout); uint16_t getPeerMTU(uint16_t conn_id); -// std::vector getPeerDevices(); + std::vector getPeerDevices(); + NimBLEConnInfo getPeerInfo(size_t index); + NimBLEConnInfo getPeerInfo(const NimBLEAddress& address); + NimBLEConnInfo getPeerIDInfo(uint16_t id); void advertiseOnDisconnect(bool); private: