@@ -341,11 +341,15 @@ bool wifiSoftApSsidSet; // Set when the WiFi soft AP SSID string exists
341341bool wifiStationRestart; // Restart Wifi station
342342bool wifiStationSsidSet; // Set when one or more SSID strings exist
343343
344+ static struct Settings *wifiPreviousSettings;
345+
344346// *********************************************************************
345347// Set WiFi credentials
346348// Enable TCP connections
347349void menuWiFi ()
348350{
351+ wifiSettingsClone (); // Create a copy of settings to detect if WiFi settings change
352+
349353 while (1 )
350354 {
351355 networkDisplayInterface (NETWORK_WIFI_STATION);
@@ -418,9 +422,26 @@ void menuWiFi()
418422 clearBuffer (); // Empty buffer of any newline chars
419423}
420424
425+ // ----------------------------------------
426+ // Check the settings and free the settings structure
427+ // ----------------------------------------
428+ bool wifiSettingsChangedAndFree ()
429+ {
430+ bool changed;
431+
432+ changed = false ;
433+ if (wifiPreviousSettings)
434+ {
435+ changed = wifiSettingsChanged (wifiPreviousSettings);
436+ rtkFree (wifiPreviousSettings, " WiFi previous settings" );
437+ wifiPreviousSettings = nullptr ;
438+ }
439+ return changed;
440+ }
441+
421442// *********************************************************************
422443// Check for setting differences
423- bool wifiCheckSettings (struct Settings *newSettings)
444+ bool wifiSettingsChanged (struct Settings *newSettings)
424445{
425446 bool changed;
426447
@@ -434,14 +455,21 @@ bool wifiCheckSettings(struct Settings *newSettings)
434455 (memcmp (newSettings->wifiNetworks [2 ].password , settings.wifiNetworks [2 ].password , PASSWORD_LENGTH) != 0 ) ||
435456 (memcmp (newSettings->wifiNetworks [3 ].ssid , settings.wifiNetworks [3 ].ssid , SSID_LENGTH) != 0 ) ||
436457 (memcmp (newSettings->wifiNetworks [3 ].password , settings.wifiNetworks [3 ].password , PASSWORD_LENGTH) != 0 );
437- if (changed)
438- {
439- // Update the WiFi settings
440- wifiUpdateSettings ();
441- }
442458 return changed;
443459}
444460
461+ // ----------------------------------------
462+ // Allocate the settings structure and clone the settings
463+ // ----------------------------------------
464+ void wifiSettingsClone ()
465+ {
466+ wifiPreviousSettings = (struct Settings *)rtkMalloc (sizeof (settings), " WiFi previous settings" );
467+ if (wifiPreviousSettings == nullptr )
468+ systemPrintln (" ERROR: WiFi failed to allocate previous settings!\r\n " );
469+ else
470+ memcpy (wifiPreviousSettings, &settings, sizeof (settings));
471+ }
472+
445473// *********************************************************************
446474// Display the soft AP details
447475void wifiDisplayNetworkData ()
@@ -1070,9 +1098,21 @@ void wifiUpdateSettings()
10701098 // Remember the change in SSID values
10711099 wifiStationSsidSet = ssidSet;
10721100
1073- // If there are consumers, and WiFi currently has connectivity, don't restart it
1074- if ((networkConsumerCount (NETWORK_WIFI_STATION) > 0 ) && (networkHasInternet () == false ))
1101+ // If there are consumers, and WiFi currently is not connected, then allow restart
1102+ if ((networkConsumerCount (NETWORK_WIFI_STATION) > 0 ) &&
1103+ (networkInterfaceHasInternet (NETWORK_WIFI_STATION) == false ))
1104+ {
1105+ if (settings.debugWifiState )
1106+ systemPrintln (" Restarting WiFi because settings have changed" );
1107+ wifiStationRestart = ssidSet;
1108+ }
1109+
1110+ // If we are connected over WiFi, and settings change, restart connection
1111+ bool wifiChanged =
1112+ wifiSettingsChangedAndFree (); // Check to see if WiFi settings have been changed, then clear memory
1113+ if (wifiChanged == true && networkInterfaceHasInternet (NETWORK_WIFI_STATION) == true )
10751114 {
1115+ // Restart network
10761116 if (settings.debugWifiState )
10771117 systemPrintln (" Restarting WiFi because settings have changed" );
10781118 wifiStationRestart = ssidSet;
0 commit comments