From 33d0a732a6ac2a36e66d678939df491da500c43c Mon Sep 17 00:00:00 2001 From: h2zero Date: Thu, 30 Jul 2020 10:04:34 -0600 Subject: [PATCH] Set peer address of client when creating the instance. * Adds new client connect method that will connect to the address already set. Co-authored-by: wakwak-koba --- src/NimBLEClient.cpp | 22 +++++++++++++++++++++- src/NimBLEClient.h | 2 ++ src/NimBLEDevice.cpp | 5 ++++- src/NimBLEDevice.h | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 4266474..8ae7394 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -127,6 +127,10 @@ size_t NimBLEClient::deleteService(const NimBLEUUID &uuid) { } // deleteServices +bool NimBLEClient::connect(bool refreshServices) { + return connect(m_peerAddress, 0, refreshServices); +} + /** * @brief Connect to an advertising device. * @param [in] device The device to connect to. @@ -348,7 +352,23 @@ uint16_t NimBLEClient::getConnId() { */ NimBLEAddress NimBLEClient::getPeerAddress() { return m_peerAddress; -} // getAddress +} // getPeerAddress + + +/** + * @brief Set the peer address. + * @param [in] address The address of the peer that this client is + * connected or should connect to. + */ +void NimBLEClient::setPeerAddress(const NimBLEAddress &address) { + if(isConnected()) { + NIMBLE_LOGE(LOG_TAG, "Cannot set peer address while connected"); + return; + } + + m_peerAddress = address; + NIMBLE_LOGE(LOG_TAG, "Peer address set: %s", std::string(m_peerAddress).c_str()); +} // setPeerAddress /** diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 8f26048..600ac7c 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -41,8 +41,10 @@ public: bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true); bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); + bool connect(bool refreshServices = true); int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); NimBLEAddress getPeerAddress(); + void setPeerAddress(const NimBLEAddress &address); int getRssi(); std::vector* getServices(bool refresh = false); std::vector::iterator begin(); diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 68d4ec0..a5e242c 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -140,13 +140,16 @@ void NimBLEDevice::stopAdvertising() { * @return A reference to the new client object. */ #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) -/* STATIC */ NimBLEClient* NimBLEDevice::createClient() { +/* STATIC */ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) { if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) { NIMBLE_LOGW("Number of clients exceeds Max connections. Max=(%d)", NIMBLE_MAX_CONNECTIONS); } NimBLEClient* pClient = new NimBLEClient(); + if(peerAddress != NimBLEAddress("")) { + pClient->setPeerAddress(peerAddress); + } m_cList.push_back(pClient); return pClient; diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index 36fd2de..252c52a 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -130,7 +130,7 @@ public: #endif #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) - static NimBLEClient* createClient(); + static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress("")); static bool deleteClient(NimBLEClient* pClient); static NimBLEClient* getClientByID(uint16_t conn_id); static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);