From 32c213a8a3c1df9e1d64d0fc13533da74ca0476f Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 4 Jun 2024 11:37:24 -0500 Subject: [PATCH] feat(NimBLEClient): allow connection id / established flag to be set via public API. (#156) * Adds NimBLEClient::setConnection to allow servers to read the name of connected clients by passing their connection info to the Client class. * Adds NimBLEClient::clearConnection to be able to reuse the client without deleting and recreating. --- src/NimBLEClient.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/NimBLEClient.h | 3 +++ 2 files changed, 63 insertions(+) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index bd50e32..996b759 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -551,6 +551,66 @@ uint16_t NimBLEClient::getConnId() { return m_conn_id; } // getConnId +/** + * @brief Clear the connection information for this client. + * @note This is designed to be used to reset the connection information after + * calling setConnection(), and should not be used to disconnect from a + * peer. To disconnect from a peer, use disconnect(). + */ +void NimBLEClient::clearConnection() { + m_conn_id = BLE_HS_CONN_HANDLE_NONE; + m_connEstablished = false; + m_peerAddress = NimBLEAddress(); +} // clearConnection + +/** + * @brief Set the connection information for this client. + * @param [in] connInfo The connection information. + * @return True on success. + * @note Sets the connection established flag to true. + * @note If the client is already connected to a peer, this will return false. + * @note This is designed to be used when a connection is made outside of the + * NimBLEClient class, such as when a connection is made by the + * NimBLEServer class and the client is passed the connection id. This use + * enables the GATT Server to read the name of the device that has + * connected to it. + */ +bool NimBLEClient::setConnection(NimBLEConnInfo &connInfo) { + if (isConnected() || m_connEstablished) { + NIMBLE_LOGE(LOG_TAG, "Already connected"); + return false; + } + + m_peerAddress = connInfo.getAddress(); + m_conn_id = connInfo.getConnHandle(); + m_connEstablished = true; + + return true; +} // setConnection + +/** + * @brief Set the connection information for this client. + * @param [in] conn_id The connection id. + * @note Sets the connection established flag to true. + * @note This is designed to be used when a connection is made outside of the + * NimBLEClient class, such as when a connection is made by the + * NimBLEServer class and the client is passed the connection id. This use + * enables the GATT Server to read the name of the device that has + * connected to it. + * @note If the client is already connected to a peer, this will return false. + * @note This will look up the peer address using the connection id. + */ +bool NimBLEClient::setConnection(uint16_t conn_id) { + // we weren't provided the peer address, look it up using ble_gap_conn_find + NimBLEConnInfo connInfo; + int rc = ble_gap_conn_find(m_conn_id, &connInfo.m_desc); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Connection info not found"); + return false; + } + + return setConnection(connInfo); +} // setConnection /** * @brief Retrieve the address of the peer. diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 486b3d0..0306198 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -61,6 +61,9 @@ public: bool deleteCallbacks = true); std::string toString(); uint16_t getConnId(); + void clearConnection(); + bool setConnection(NimBLEConnInfo &conn_info); + bool setConnection(uint16_t conn_id); uint16_t getMTU(); bool secureConnection(); void setConnectTimeout(uint32_t timeout);