[BREAKING] Use an array to manage created client instances.

* Replaces the use of std::list with a fixed array to track and manage created client instances.
* Removes: NimBLEDevice::getClientList
* Replaces: NimBLEDevice::getClientListSize with NimBLEDevice::getCreatedClientCount
This commit is contained in:
h2zero 2024-07-04 12:03:03 -06:00 committed by h2zero
parent a92149ac74
commit 21e1217e4c
3 changed files with 55 additions and 50 deletions

View file

@ -109,7 +109,7 @@ bool connectToServer() {
NimBLEClient* pClient = nullptr; NimBLEClient* pClient = nullptr;
/** Check if we have a client we should reuse first **/ /** Check if we have a client we should reuse first **/
if(NimBLEDevice::getClientListSize()) { if(NimBLEDevice::getCreatedClientCount()) {
/** Special case when we already know this device, we send false as the /** Special case when we already know this device, we send false as the
* second argument in connect() to prevent refreshing the service database. * second argument in connect() to prevent refreshing the service database.
* This saves considerable time and power. * This saves considerable time and power.
@ -132,7 +132,7 @@ bool connectToServer() {
/** No client to reuse? Create a new one. */ /** No client to reuse? Create a new one. */
if(!pClient) { if(!pClient) {
if(NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) { if(NimBLEDevice::getCreatedClientCount() >= NIMBLE_MAX_CONNECTIONS) {
printf("Max clients reached - no more connections available\n"); printf("Max clients reached - no more connections available\n");
return false; return false;
} }

View file

@ -78,12 +78,13 @@ NimBLEAdvertising* NimBLEDevice::m_bleAdvertising = nullptr;
# endif # endif
#endif #endif
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> NimBLEDevice::m_pClients{nullptr};
#endif
gap_event_handler NimBLEDevice::m_customGapHandler = nullptr; gap_event_handler NimBLEDevice::m_customGapHandler = nullptr;
ble_gap_event_listener NimBLEDevice::m_listener; ble_gap_event_listener NimBLEDevice::m_listener;
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) std::vector <NimBLEAddress> NimBLEDevice::m_ignoreList;
std::list <NimBLEClient*> NimBLEDevice::m_cList;
#endif
std::list <NimBLEAddress> NimBLEDevice::m_ignoreList;
std::vector<NimBLEAddress> NimBLEDevice::m_whiteList; std::vector<NimBLEAddress> NimBLEDevice::m_whiteList;
uint8_t NimBLEDevice::m_own_addr_type = BLE_OWN_ADDR_PUBLIC; uint8_t NimBLEDevice::m_own_addr_type = BLE_OWN_ADDR_PUBLIC;
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
@ -213,18 +214,23 @@ NimBLEScan* NimBLEDevice::getScan() {
* each client can connect to 1 peripheral device. * each client can connect to 1 peripheral device.
* @param [in] peerAddress An optional peer address that is copied to the new client * @param [in] peerAddress An optional peer address that is copied to the new client
* object, allows for calling NimBLEClient::connect(bool) without a device or address parameter. * object, allows for calling NimBLEClient::connect(bool) without a device or address parameter.
* @return A reference to the new client object. * @return A reference to the new client object, or nullptr on error.
*/ */
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
/* STATIC */ /* STATIC */
NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) { NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) {
if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) { if (getCreatedClientCount() == NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGW(LOG_TAG,"Number of clients exceeds Max connections. Cur=%d Max=%d", NIMBLE_LOGE(LOG_TAG,"Unable to create client; already at max: %d",NIMBLE_MAX_CONNECTIONS);
m_cList.size(), NIMBLE_MAX_CONNECTIONS); return nullptr;
} }
NimBLEClient* pClient = new NimBLEClient(peerAddress); NimBLEClient* pClient = new NimBLEClient(peerAddress);
m_cList.push_back(pClient); for (auto& clt : m_pClients) {
if (clt == nullptr) {
clt = pClient;
break;
}
}
return pClient; return pClient;
} // createClient } // createClient
@ -269,31 +275,32 @@ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) {
} }
} }
m_cList.remove(pClient); for (auto& clt : m_pClients) {
delete pClient; if (clt == pClient) {
delete pClient;
clt = nullptr;
}
}
return true; return true;
} // deleteClient } // deleteClient
/**
* @brief Get the list of created client objects.
* @return A pointer to the list of clients.
*/
/* STATIC */
std::list<NimBLEClient*>* NimBLEDevice::getClientList() {
return &m_cList;
} // getClientList
/** /**
* @brief Get the number of created client objects. * @brief Get the number of created client objects.
* @return Number of client objects created. * @return Number of client objects created.
*/ */
/* STATIC */ /* STATIC */
size_t NimBLEDevice::getClientListSize() { size_t NimBLEDevice::getCreatedClientCount() {
return m_cList.size(); auto count = 0;
} // getClientList for (auto clt : m_pClients) {
if (clt != nullptr) {
count++;
}
}
return count;
} // getCreatedClientCount
/** /**
@ -303,9 +310,9 @@ size_t NimBLEDevice::getClientListSize() {
*/ */
/* STATIC */ /* STATIC */
NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) { NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto clt : m_pClients) {
if((*it)->getConnId() == conn_id) { if(clt != nullptr && clt->getConnId() == conn_id) {
return (*it); return clt;
} }
} }
@ -316,30 +323,32 @@ NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) {
/** /**
* @brief Get a reference to a client by peer address. * @brief Get a reference to a client by peer address.
* @param [in] peer_addr The address of the peer to search for. * @param [in] peer_addr The address of the peer to search for.
* @return A pointer to the client object with the peer address. * @return A pointer to the client object with the peer address or nullptr.
*/ */
/* STATIC */ /* STATIC */
NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) { NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto clt : m_pClients) {
if((*it)->getPeerAddress().equals(peer_addr)) { if(clt != nullptr && clt->getPeerAddress() == peer_addr) {
return (*it); return clt;
} }
} }
return nullptr; return nullptr;
} // getClientPeerAddress } // getClientPeerAddress
/** /**
* @brief Finds the first disconnected client in the list. * @brief Finds the first disconnected client in the list.
* @return A pointer to the first client object that is not connected to a peer. * @return A pointer to the first client object that is not connected to a peer or nullptr.
*/ */
/* STATIC */ /* STATIC */
NimBLEClient* NimBLEDevice::getDisconnectedClient() { NimBLEClient* NimBLEDevice::getDisconnectedClient() {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto clt : m_pClients) {
if(!(*it)->isConnected()) { if(clt != nullptr && !clt->isConnected()) {
return (*it); return clt;
} }
} }
return nullptr; return nullptr;
} // getDisconnectedClient } // getDisconnectedClient
@ -983,12 +992,10 @@ void NimBLEDevice::deinit(bool clearAll) {
#endif #endif
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
for(auto &it : m_cList) { for(auto clt : m_pClients) {
deleteClient(it); deleteClient(clt);
m_cList.clear();
} }
#endif #endif
m_ignoreList.clear(); m_ignoreList.clear();
} }
} }

View file

@ -45,9 +45,7 @@
# include "esp_bt.h" # include "esp_bt.h"
#endif #endif
#include <map>
#include <string> #include <string>
#include <list>
#define BLEDevice NimBLEDevice #define BLEDevice NimBLEDevice
#define BLEClient NimBLEClient #define BLEClient NimBLEClient
@ -166,8 +164,7 @@ public:
static NimBLEClient* getClientByID(uint16_t conn_id); static NimBLEClient* getClientByID(uint16_t conn_id);
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr); static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
static NimBLEClient* getDisconnectedClient(); static NimBLEClient* getDisconnectedClient();
static size_t getClientListSize(); static size_t getCreatedClientCount();
static std::list<NimBLEClient*>* getClientList();
#endif #endif
#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
@ -221,21 +218,22 @@ private:
# endif # endif
#endif #endif
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) static std::vector<NimBLEAddress> m_ignoreList;
static std::list <NimBLEClient*> m_cList;
#endif
static std::list <NimBLEAddress> m_ignoreList;
static uint32_t m_passkey; static uint32_t m_passkey;
static ble_gap_event_listener m_listener; static ble_gap_event_listener m_listener;
static gap_event_handler m_customGapHandler; static gap_event_handler m_customGapHandler;
static uint8_t m_own_addr_type; static uint8_t m_own_addr_type;
static std::vector<NimBLEAddress> m_whiteList;
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL # ifdef CONFIG_BTDM_BLE_SCAN_DUPL
static uint16_t m_scanDuplicateSize; static uint16_t m_scanDuplicateSize;
static uint8_t m_scanFilterMode; static uint8_t m_scanFilterMode;
# endif # endif
#endif #endif
static std::vector<NimBLEAddress> m_whiteList;
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
static std::array<NimBLEClient*, NIMBLE_MAX_CONNECTIONS> m_pClients;
#endif
}; };