Skip to content

Commit ee9b0ba

Browse files
committed
Swap dynamic memory for static in wifi rssi sorting
1 parent a6b68cd commit ee9b0ba

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/Wippersnapper.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
// Cpp STD
2222
#include <vector>
23+
#include <algorithm>
2324

2425
// Nanopb dependencies
2526
#include <nanopb/pb_common.h>
@@ -215,6 +216,7 @@ typedef enum {
215216
} fsm_net_t;
216217

217218
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
219+
#define WS_MAX_SORTED_NETWORKS 15 ///< Maximum number of networks to sort
218220
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks
219221
/* MQTT Configuration */
220222
#define WS_KEEPALIVE_INTERVAL_MS \

src/network_interfaces/Wippersnapper_AIRLIFT.h

+13-16
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
109109
*/
110110
/****************************************************************/
111111
struct WiFiNetwork {
112-
char ssid[33]; /*!< SSID (Max 32 characters + null terminator */
113-
int rssi; /*!< Received Signal Strength Indicator */
112+
String ssid[32]; /*!< SSID (Max 32 characters + null terminator */
113+
int32_t rssi = -99; /*!< Received Signal Strength Indicator */
114114
};
115115

116116
/*******************************************************************/
@@ -145,38 +145,35 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
145145
return false;
146146
}
147147

148-
// Dynamically allocate memory for the network list
149-
std::vector<WiFiNetwork> networks(n);
148+
WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
150149

151-
// Store the scanned networks in the vector
152-
for (int i = 0; i < n; ++i) {
153-
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
154-
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
155-
'\0'; // Ensure null termination
150+
// Store the scanned networks in the array
151+
for (int i = 0; i < n && i < WS_MAX_SORTED_NETWORKS; ++i) {
152+
strncpy(networks[i].ssid->c_str(), WiFi.SSID(i), sizeof(networks[i].ssid));
156153
networks[i].rssi = WiFi.RSSI(i);
157154
}
158155

159156
// Sort the networks by RSSI in descending order
160-
std::sort(networks.begin(), networks.end(), compareByRSSI);
157+
std::sort(networks, networks + std::min(n, WS_MAX_SORTED_NETWORKS), compareByRSSI);
161158

162159
// Was the network within secrets.json found?
163-
for (const auto &network : networks) {
164-
if (strcmp(_ssid, network.ssid) == 0) {
160+
for (int i = 0; i < n; ++i) {
161+
if (strcmp(_ssid, WiFi.SSID(i)) == 0) {
165162
WS_DEBUG_PRINT("SSID (");
166163
WS_DEBUG_PRINT(_ssid);
167164
WS_DEBUG_PRINT(") found! RSSI: ");
168-
WS_DEBUG_PRINTLN(network.rssi);
165+
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
169166
return true;
170167
}
171168
}
172169

173170
// User-set network not found, print scan results to serial console
174171
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
175172
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
176-
for (const auto &network : networks) {
177-
WS_DEBUG_PRINT(network.ssid);
173+
for (int i = 0; i < n; ++i) {
174+
WS_DEBUG_PRINT(WiFi.SSID(i));
178175
WS_DEBUG_PRINT(" ");
179-
WS_DEBUG_PRINT(network.rssi);
176+
WS_DEBUG_PRINT(WiFi.RSSI(i));
180177
WS_DEBUG_PRINTLN("dB");
181178
}
182179

0 commit comments

Comments
 (0)