Skip to content

Commit

Permalink
Swap dynamic memory for static in wifi rssi sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Nov 21, 2024
1 parent a6b68cd commit ee9b0ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

// Cpp STD
#include <vector>
#include <algorithm>

// Nanopb dependencies
#include <nanopb/pb_common.h>
Expand Down Expand Up @@ -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 \
Expand Down
29 changes: 13 additions & 16 deletions src/network_interfaces/Wippersnapper_AIRLIFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};

/*******************************************************************/
Expand Down Expand Up @@ -145,38 +145,35 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
return false;
}

// Dynamically allocate memory for the network list
std::vector<WiFiNetwork> 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->c_str(), 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;
}
}

// 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");
}

Expand Down

0 comments on commit ee9b0ba

Please sign in to comment.