refactor: Dedupe wifi scan in-place without std::map (#2262)
## Summary `std::map` heap-allocates each node. Avoid using it for wifi SSID dedupe, and instead just dedupe in-place with the existing `networks` vector. Removed dead `ipAddress` member in `WifiNetworkInfo` struct. --- ### AI Usage Did you use AI tools to help write this code? _**PARTIALLY**_
This commit is contained in:
@@ -6,8 +6,6 @@
|
|||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
#include "CrossPointSettings.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "WifiCredentialStore.h"
|
#include "WifiCredentialStore.h"
|
||||||
@@ -118,39 +116,35 @@ void WifiSelectionActivity::processWifiScanResults() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan complete, process results
|
// Scan complete, process results — deduplicate in-place, keeping strongest signal
|
||||||
// Use a map to deduplicate networks by SSID, keeping the strongest signal
|
networks.clear();
|
||||||
std::map<std::string, WifiNetworkInfo> uniqueNetworks;
|
networks.reserve(scanResult);
|
||||||
|
|
||||||
for (int i = 0; i < scanResult; i++) {
|
for (int i = 0; i < scanResult; i++) {
|
||||||
std::string ssid = WiFi.SSID(i).c_str();
|
char ssid[33];
|
||||||
|
strlcpy(ssid, WiFi.SSID(i).c_str(), sizeof(ssid));
|
||||||
const int32_t rssi = WiFi.RSSI(i);
|
const int32_t rssi = WiFi.RSSI(i);
|
||||||
|
|
||||||
// Skip hidden networks (empty SSID)
|
// Skip hidden networks (empty SSID)
|
||||||
if (ssid.empty()) {
|
if (ssid[0] == '\0') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we've already seen this SSID
|
auto it =
|
||||||
auto it = uniqueNetworks.find(ssid);
|
std::find_if(networks.begin(), networks.end(), [&ssid](const WifiNetworkInfo& n) { return n.ssid == ssid; });
|
||||||
if (it == uniqueNetworks.end() || rssi > it->second.rssi) {
|
if (it == networks.end()) {
|
||||||
// New network or stronger signal than existing entry
|
|
||||||
WifiNetworkInfo network;
|
WifiNetworkInfo network;
|
||||||
network.ssid = ssid;
|
network.ssid = ssid;
|
||||||
network.rssi = rssi;
|
network.rssi = rssi;
|
||||||
network.isEncrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
|
network.isEncrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
|
||||||
network.hasSavedPassword = WIFI_STORE.hasSavedCredential(network.ssid);
|
network.hasSavedPassword = WIFI_STORE.hasSavedCredential(network.ssid);
|
||||||
uniqueNetworks[ssid] = network;
|
networks.push_back(std::move(network));
|
||||||
|
} else if (rssi > it->rssi) {
|
||||||
|
it->rssi = rssi;
|
||||||
|
it->isEncrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert map to vector
|
|
||||||
networks.clear();
|
|
||||||
for (const auto& pair : uniqueNetworks) {
|
|
||||||
// cppcheck-suppress useStlAlgorithm
|
|
||||||
networks.push_back(pair.second);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort: saved-password networks first, then by signal strength (strongest first)
|
// Sort: saved-password networks first, then by signal strength (strongest first)
|
||||||
std::sort(networks.begin(), networks.end(), [](const WifiNetworkInfo& a, const WifiNetworkInfo& b) {
|
std::sort(networks.begin(), networks.end(), [](const WifiNetworkInfo& a, const WifiNetworkInfo& b) {
|
||||||
if (a.hasSavedPassword != b.hasSavedPassword) {
|
if (a.hasSavedPassword != b.hasSavedPassword) {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ struct WifiNetworkInfo {
|
|||||||
int32_t rssi;
|
int32_t rssi;
|
||||||
bool isEncrypted;
|
bool isEncrypted;
|
||||||
bool hasSavedPassword; // Whether we have saved credentials for this network
|
bool hasSavedPassword; // Whether we have saved credentials for this network
|
||||||
std::string ipAddress; // Populated after connection for display
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// WiFi selection states
|
// WiFi selection states
|
||||||
|
|||||||
Reference in New Issue
Block a user