Skip to content

Commit 6db0368

Browse files
finger563h2zero
authored andcommitted
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.
1 parent 9c3429d commit 6db0368

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/NimBLEClient.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,66 @@ uint16_t NimBLEClient::getConnId() {
551551
return m_conn_id;
552552
} // getConnId
553553

554+
/**
555+
* @brief Clear the connection information for this client.
556+
* @note This is designed to be used to reset the connection information after
557+
* calling setConnection(), and should not be used to disconnect from a
558+
* peer. To disconnect from a peer, use disconnect().
559+
*/
560+
void NimBLEClient::clearConnection() {
561+
m_conn_id = BLE_HS_CONN_HANDLE_NONE;
562+
m_connEstablished = false;
563+
m_peerAddress = NimBLEAddress();
564+
} // clearConnection
565+
566+
/**
567+
* @brief Set the connection information for this client.
568+
* @param [in] connInfo The connection information.
569+
* @return True on success.
570+
* @note Sets the connection established flag to true.
571+
* @note If the client is already connected to a peer, this will return false.
572+
* @note This is designed to be used when a connection is made outside of the
573+
* NimBLEClient class, such as when a connection is made by the
574+
* NimBLEServer class and the client is passed the connection id. This use
575+
* enables the GATT Server to read the name of the device that has
576+
* connected to it.
577+
*/
578+
bool NimBLEClient::setConnection(NimBLEConnInfo &connInfo) {
579+
if (isConnected() || m_connEstablished) {
580+
NIMBLE_LOGE(LOG_TAG, "Already connected");
581+
return false;
582+
}
583+
584+
m_peerAddress = connInfo.getAddress();
585+
m_conn_id = connInfo.getConnHandle();
586+
m_connEstablished = true;
587+
588+
return true;
589+
} // setConnection
590+
591+
/**
592+
* @brief Set the connection information for this client.
593+
* @param [in] conn_id The connection id.
594+
* @note Sets the connection established flag to true.
595+
* @note This is designed to be used when a connection is made outside of the
596+
* NimBLEClient class, such as when a connection is made by the
597+
* NimBLEServer class and the client is passed the connection id. This use
598+
* enables the GATT Server to read the name of the device that has
599+
* connected to it.
600+
* @note If the client is already connected to a peer, this will return false.
601+
* @note This will look up the peer address using the connection id.
602+
*/
603+
bool NimBLEClient::setConnection(uint16_t conn_id) {
604+
// we weren't provided the peer address, look it up using ble_gap_conn_find
605+
NimBLEConnInfo connInfo;
606+
int rc = ble_gap_conn_find(m_conn_id, &connInfo.m_desc);
607+
if (rc != 0) {
608+
NIMBLE_LOGE(LOG_TAG, "Connection info not found");
609+
return false;
610+
}
611+
612+
return setConnection(connInfo);
613+
} // setConnection
554614

555615
/**
556616
* @brief Retrieve the address of the peer.

src/NimBLEClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class NimBLEClient {
6161
bool deleteCallbacks = true);
6262
std::string toString();
6363
uint16_t getConnId();
64+
void clearConnection();
65+
bool setConnection(NimBLEConnInfo &conn_info);
66+
bool setConnection(uint16_t conn_id);
6467
uint16_t getMTU();
6568
bool secureConnection();
6669
void setConnectTimeout(uint32_t timeout);

0 commit comments

Comments
 (0)