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.
This commit is contained in:
William Emfinger 2024-06-04 11:37:24 -05:00 committed by GitHub
parent 51bf1f3c7c
commit 32c213a8a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 63 additions and 0 deletions

View file

@ -551,6 +551,66 @@ uint16_t NimBLEClient::getConnId() {
return m_conn_id; return m_conn_id;
} // getConnId } // 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. * @brief Retrieve the address of the peer.

View file

@ -61,6 +61,9 @@ public:
bool deleteCallbacks = true); bool deleteCallbacks = true);
std::string toString(); std::string toString();
uint16_t getConnId(); uint16_t getConnId();
void clearConnection();
bool setConnection(NimBLEConnInfo &conn_info);
bool setConnection(uint16_t conn_id);
uint16_t getMTU(); uint16_t getMTU();
bool secureConnection(); bool secureConnection();
void setConnectTimeout(uint32_t timeout); void setConnectTimeout(uint32_t timeout);