@@ -138,37 +138,47 @@ class ws_networking_pico : public Wippersnapper {
138
138
return false ;
139
139
}
140
140
141
- // Dynamically allocate memory for the network list
142
- std::vector<WiFiNetwork> networks (n) ;
141
+ WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
142
+ uint8_t numSavedNetworks = 0 ;
143
143
144
144
// Store the scanned networks in the vector
145
145
for (int i = 0 ; i < n; ++i) {
146
- strncpy (networks[i].ssid , WiFi.SSID (i), sizeof (networks[i].ssid ) - 1 );
147
- networks[i].ssid [sizeof (networks[i].ssid ) - 1 ] =
148
- ' \0 ' ; // Ensure null termination
149
- networks[i].rssi = WiFi.RSSI (i);
146
+ if (i < WS_MAX_SORTED_NETWORKS) {
147
+ strncpy (networks[i].ssid , WiFi.SSID (i), sizeof (networks[i].ssid ));
148
+ networks[i].ssid [sizeof (networks[i].ssid ) - 1 ] = ' \0 ' ;
149
+ networks[i].rssi = WiFi.RSSI (i);
150
+ numSavedNetworks++;
151
+ } else {
152
+ WS_DEBUG_PRINT (" ERROR: Too many networks found! (>" );
153
+ WS_DEBUG_PRINT (WS_MAX_SORTED_NETWORKS);
154
+ WS_DEBUG_PRINT (" ) Ignoring " );
155
+ WS_DEBUG_PRINT (WiFi.SSID (i));
156
+ WS_DEBUG_PRINT (" (" );
157
+ WS_DEBUG_PRINT (WiFi.RSSI (i));
158
+ WS_DEBUG_PRINTLN (" )" );
159
+ }
150
160
}
151
161
152
- // Sort the networks by RSSI in descending order
153
- std::sort (networks. begin () , networks. end () , compareByRSSI);
162
+ // / Sort the networks by RSSI in descending order
163
+ std::sort (networks, networks + numSavedNetworks , compareByRSSI);
154
164
155
165
// Was the network within secrets.json found?
156
- for (const auto &network : networks ) {
157
- if (strcmp (_ssid, network .ssid ) == 0 ) {
166
+ for (int i = 0 ; i < numSavedNetworks; ++i ) {
167
+ if (strcmp (_ssid, networks[i] .ssid ) == 0 ) {
158
168
WS_DEBUG_PRINT (" SSID (" );
159
169
WS_DEBUG_PRINT (_ssid);
160
170
WS_DEBUG_PRINT (" ) found! RSSI: " );
161
- WS_DEBUG_PRINTLN (network .rssi );
171
+ WS_DEBUG_PRINTLN (networks[i] .rssi );
162
172
return true ;
163
173
}
164
174
if (WS._isWiFiMulti ) {
165
175
// multi network mode
166
176
for (int j = 0 ; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
167
- if (strcmp (WS._multiNetworks [j].ssid , network .ssid ) == 0 ) {
177
+ if (strcmp (WS._multiNetworks [j].ssid , networks[i] .ssid ) == 0 ) {
168
178
WS_DEBUG_PRINT (" SSID (" );
169
179
WS_DEBUG_PRINT (WS._multiNetworks [j].ssid );
170
180
WS_DEBUG_PRINT (" ) found! RSSI: " );
171
- WS_DEBUG_PRINTLN (network .rssi );
181
+ WS_DEBUG_PRINTLN (networks[i] .rssi );
172
182
return true ;
173
183
}
174
184
}
@@ -177,11 +187,18 @@ class ws_networking_pico : public Wippersnapper {
177
187
178
188
// User-set network not found, print scan results to serial console
179
189
WS_DEBUG_PRINTLN (" ERROR: Your requested WiFi network was not found!" );
180
- WS_DEBUG_PRINTLN (" WipperSnapper found these WiFi networks: " );
181
- for (const auto &network : networks) {
182
- WS_DEBUG_PRINT (network.ssid );
190
+ WS_DEBUG_PRINT (" WipperSnapper found these WiFi networks" );
191
+ if (n > WS_MAX_SORTED_NETWORKS) {
192
+ WS_DEBUG_PRINT (" (only first " );
193
+ WS_DEBUG_PRINT (WS_MAX_SORTED_NETWORKS);
194
+ WS_DEBUG_PRINTLN (" used):" );
195
+ } else {
196
+ WS_DEBUG_PRINTLN (" :" );
197
+ }
198
+ for (int i = 0 ; i < n; ++i) {
199
+ WS_DEBUG_PRINT (networks[i].ssid );
183
200
WS_DEBUG_PRINT (" " );
184
- WS_DEBUG_PRINT (network .rssi );
201
+ WS_DEBUG_PRINT (networks[i] .rssi );
185
202
WS_DEBUG_PRINTLN (" dB" );
186
203
}
187
204
0 commit comments