@@ -138,37 +138,47 @@ class ws_networking_pico : public Wippersnapper {
138138 return false ;
139139 }
140140
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 ;
143143
144144 // Store the scanned networks in the vector
145145 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+ }
150160 }
151161
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);
154164
155165 // 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 ) {
158168 WS_DEBUG_PRINT (" SSID (" );
159169 WS_DEBUG_PRINT (_ssid);
160170 WS_DEBUG_PRINT (" ) found! RSSI: " );
161- WS_DEBUG_PRINTLN (network .rssi );
171+ WS_DEBUG_PRINTLN (networks[i] .rssi );
162172 return true ;
163173 }
164174 if (WS._isWiFiMulti ) {
165175 // multi network mode
166176 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 ) {
168178 WS_DEBUG_PRINT (" SSID (" );
169179 WS_DEBUG_PRINT (WS._multiNetworks [j].ssid );
170180 WS_DEBUG_PRINT (" ) found! RSSI: " );
171- WS_DEBUG_PRINTLN (network .rssi );
181+ WS_DEBUG_PRINTLN (networks[i] .rssi );
172182 return true ;
173183 }
174184 }
@@ -177,11 +187,18 @@ class ws_networking_pico : public Wippersnapper {
177187
178188 // User-set network not found, print scan results to serial console
179189 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 );
183200 WS_DEBUG_PRINT (" " );
184- WS_DEBUG_PRINT (network .rssi );
201+ WS_DEBUG_PRINT (networks[i] .rssi );
185202 WS_DEBUG_PRINTLN (" dB" );
186203 }
187204
0 commit comments