From a1ea262c83f14ad9b20f17483cefd575134a289b Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 21 Nov 2024 19:44:46 +0000 Subject: [PATCH] Swap dynamic memory for static in wifi rssi sorting --- src/Wippersnapper.h | 2 ++ .../Wippersnapper_AIRLIFT.h | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index ede52322..49f844d0 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -20,6 +20,7 @@ // Cpp STD #include +#include // Nanopb dependencies #include @@ -215,6 +216,7 @@ typedef enum { } fsm_net_t; #define WS_WDT_TIMEOUT 60000 ///< WDT timeout +#define WS_MAX_SORTED_NETWORKS 15 ///< Maximum number of networks to sort #define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks /* MQTT Configuration */ #define WS_KEEPALIVE_INTERVAL_MS \ diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index b08fde4f..f2e65eef 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -109,8 +109,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { */ /****************************************************************/ struct WiFiNetwork { - char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ - int rssi; /*!< Received Signal Strength Indicator */ + String ssid[32]; /*!< SSID (Max 32 characters + null terminator */ + int32_t rssi = -99; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ @@ -145,27 +145,24 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { return false; } - // Dynamically allocate memory for the network list - std::vector networks(n); + WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; - // Store the scanned networks in the vector - for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = - '\0'; // Ensure null termination + // Store the scanned networks in the array + for (int i = 0; i < n && i < WS_MAX_SORTED_NETWORKS; ++i) { + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); networks[i].rssi = WiFi.RSSI(i); } // Sort the networks by RSSI in descending order - std::sort(networks.begin(), networks.end(), compareByRSSI); + std::sort(networks, networks + std::min(n, WS_MAX_SORTED_NETWORKS), compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + for (int i = 0; i < n; ++i) { + if (strcmp(_ssid, WiFi.SSID(i)) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(WiFi.RSSI(i)); return true; } } @@ -173,10 +170,10 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { // User-set network not found, print scan results to serial console WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); - for (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); + for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINT(WiFi.SSID(i)); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINT(WiFi.RSSI(i)); WS_DEBUG_PRINTLN("dB"); }