Skip to content

Commit 301f71f

Browse files
committed
Update logging and logic
1 parent bf2f1bb commit 301f71f

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

src/network_interfaces/Wippersnapper_AIRLIFT.h

Lines changed: 22 additions & 10 deletions
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-
int32_t rssi = -99; /*!< Received Signal Strength Indicator */
112+
char ssid[33]; /*!< SSID (Max 32 characters + null terminator */
113+
int32_t rssi = -99; /*!< Received Signal Strength Indicator */
114114
};
115115

116116
/*******************************************************************/
@@ -146,42 +146,54 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
146146
}
147147

148148
WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
149+
uint8_t numSavedNetworks = 0;
149150

150151
// Store the scanned networks in the array
151152
for (int i = 0; i < n; ++i) {
152-
if (i < WS_MAX_SORTED_NETWORKS){
153+
if (i < WS_MAX_SORTED_NETWORKS) {
153154
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid));
154155
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0';
155156
networks[i].rssi = WiFi.RSSI(i);
157+
numSavedNetworks++;
156158
} else {
157159
WS_DEBUG_PRINT("ERROR: Too many networks found! (>");
158160
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
159161
WS_DEBUG_PRINT(") Ignoring ");
160162
WS_DEBUG_PRINT(WiFi.SSID(i));
163+
WS_DEBUG_PRINT("(");
164+
WS_DEBUG_PRINT(WiFi.RSSI(i));
165+
WS_DEBUG_PRINTLN(")");
161166
}
162167
}
163168

164169
// Sort the networks by RSSI in descending order
165-
std::sort(networks, networks + std::min(n, WS_MAX_SORTED_NETWORKS), compareByRSSI);
170+
std::sort(networks, networks + numSavedNetworks, compareByRSSI);
166171

167172
// Was the network within secrets.json found?
168-
for (int i = 0; i < n; ++i) {
169-
if (strcmp(_ssid, WiFi.SSID(i)) == 0) {
173+
for (int i = 0; i < numSavedNetworks; ++i) {
174+
if (strcmp(_ssid, networks[i].ssid) == 0) {
170175
WS_DEBUG_PRINT("SSID (");
171176
WS_DEBUG_PRINT(_ssid);
172177
WS_DEBUG_PRINT(") found! RSSI: ");
173-
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
178+
WS_DEBUG_PRINTLN(networks[i].rssi);
174179
return true;
175180
}
176181
}
177182

178183
// User-set network not found, print scan results to serial console
179184
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
180-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
185+
WS_DEBUG_PRINT("WipperSnapper found these WiFi networks");
186+
if (n > WS_MAX_SORTED_NETWORKS) {
187+
WS_DEBUG_PRINT(" (only first ");
188+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
189+
WS_DEBUG_PRINTLN(" used):");
190+
} else {
191+
WS_DEBUG_PRINTLN(":");
192+
}
181193
for (int i = 0; i < n; ++i) {
182-
WS_DEBUG_PRINT(WiFi.SSID(i));
194+
WS_DEBUG_PRINT(networks[i].ssid);
183195
WS_DEBUG_PRINT(" ");
184-
WS_DEBUG_PRINT(WiFi.RSSI(i));
196+
WS_DEBUG_PRINT(networks[i].rssi);
185197
WS_DEBUG_PRINTLN("dB");
186198
}
187199

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Wippersnapper_ESP32 : public Wippersnapper {
9999
/****************************************************************/
100100
struct WiFiNetwork {
101101
char ssid[33]; /*!< SSID (Max 32 characters + null terminator */
102-
int rssi; /*!< Received Signal Strength Indicator */
102+
int32_t rssi; /*!< Received Signal Strength Indicator */
103103
};
104104

105105
/*******************************************************************/
@@ -146,38 +146,47 @@ class Wippersnapper_ESP32 : public Wippersnapper {
146146
return false;
147147
}
148148

149-
// Dynamically allocate memory for the network list
150-
std::vector<WiFiNetwork> networks(n);
151-
149+
WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
150+
uint8_t numSavedNetworks = 0;
152151
// Store the scanned networks in the vector
153152
for (int i = 0; i < n; ++i) {
154-
strncpy(networks[i].ssid, WiFi.SSID(i).c_str(),
155-
sizeof(networks[i].ssid) - 1);
156-
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
157-
'\0'; // Ensure null termination
158-
networks[i].rssi = WiFi.RSSI(i);
153+
if (i < WS_MAX_SORTED_NETWORKS) {
154+
strncpy(networks[i].ssid, WiFi.SSID(i).c_str(),
155+
sizeof(networks[i].ssid));
156+
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0';
157+
networks[i].rssi = WiFi.RSSI(i);
158+
numSavedNetworks++;
159+
} else {
160+
WS_DEBUG_PRINT("ERROR: Too many networks found! (>");
161+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
162+
WS_DEBUG_PRINT(") Ignoring ");
163+
WS_DEBUG_PRINT(WiFi.SSID(i));
164+
WS_DEBUG_PRINT("(");
165+
WS_DEBUG_PRINT(WiFi.RSSI(i));
166+
WS_DEBUG_PRINTLN(")");
167+
}
159168
}
160169

161170
// Sort the networks by RSSI in descending order
162-
std::sort(networks.begin(), networks.end(), compareByRSSI);
171+
std::sort(networks, networks + numSavedNetworks, compareByRSSI);
163172

164173
// Was the network within secrets.json found?
165-
for (const auto &network : networks) {
166-
if (strcmp(_ssid, network.ssid) == 0) {
174+
for (int i = 0; i < numSavedNetworks; ++i) {
175+
if (strcmp(_ssid, networks[i].ssid) == 0) {
167176
WS_DEBUG_PRINT("SSID (");
168177
WS_DEBUG_PRINT(_ssid);
169178
WS_DEBUG_PRINT(") found! RSSI: ");
170-
WS_DEBUG_PRINTLN(network.rssi);
179+
WS_DEBUG_PRINTLN(networks[i].rssi);
171180
return true;
172181
}
173182
if (WS._isWiFiMulti) {
174183
// multi network mode
175184
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
176-
if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) {
185+
if (strcmp(WS._multiNetworks[j].ssid, networks[i].ssid) == 0) {
177186
WS_DEBUG_PRINT("SSID (");
178187
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
179188
WS_DEBUG_PRINT(") found! RSSI: ");
180-
WS_DEBUG_PRINTLN(network.rssi);
189+
WS_DEBUG_PRINTLN(networks[i].rssi);
181190
return true;
182191
}
183192
}
@@ -186,11 +195,18 @@ class Wippersnapper_ESP32 : public Wippersnapper {
186195

187196
// User-set network not found, print scan results to serial console
188197
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
189-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
190-
for (const auto &network : networks) {
191-
WS_DEBUG_PRINT(network.ssid);
198+
WS_DEBUG_PRINT("WipperSnapper found these WiFi networks");
199+
if (n > WS_MAX_SORTED_NETWORKS) {
200+
WS_DEBUG_PRINT(" (only first ");
201+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
202+
WS_DEBUG_PRINTLN(" used):");
203+
} else {
204+
WS_DEBUG_PRINTLN(":");
205+
}
206+
for (int i = 0; i < n; ++i) {
207+
WS_DEBUG_PRINT(networks[i].ssid);
192208
WS_DEBUG_PRINT(" ");
193-
WS_DEBUG_PRINT(network.rssi);
209+
WS_DEBUG_PRINT(networks[i].rssi);
194210
WS_DEBUG_PRINTLN("dB");
195211
}
196212

0 commit comments

Comments
 (0)