Skip to content

Commit e302e0c

Browse files
committed
Move wifi settings check to WiFi.ino
1 parent c66a4f9 commit e302e0c

File tree

5 files changed

+50
-44
lines changed

5 files changed

+50
-44
lines changed

Firmware/RTK_Everywhere/Developer.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const uint8_t * networkGetMacAddress()
4949
}
5050
NetPriority_t networkGetPriority() {return 0;}
5151
bool networkHasInternet() {return false;}
52-
bool networkHasInternet(NetIndex_t index) {return false;}
5352
bool networkInterfaceHasInternet(NetIndex_t index) {return false;}
5453
bool networkIsInterfaceStarted(NetIndex_t index) {return false;}
5554
void networkMarkOffline(NetIndex_t index) {}

Firmware/RTK_Everywhere/States.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ void stateUpdate()
457457

458458
//Create temporary copy of Settings, so that we can check if they change while parsing
459459
//Useful for detecting when we need to change WiFi station settings
460-
webServerSettingsClone();
460+
wifiSettingsClone();
461461

462462
parseIncomingSettings();
463463

Firmware/RTK_Everywhere/WebServer.ino

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static const char *const webServerStateNames[] = {
5252
static const int webServerStateEntries = sizeof(webServerStateNames) / sizeof(webServerStateNames[0]);
5353

5454
static uint8_t webServerState;
55-
static struct Settings * webServerPreviousSettings;
5655

5756
// Once connected to the access point for WiFi Config, the ESP32 sends current setting values in one long string to
5857
// websocket After user clicks 'save', data is validated via main.js and a long string of values is returned.
@@ -1125,35 +1124,6 @@ void webServerSetState(uint8_t newState)
11251124
reportFatalError("Web Server: Invalid web config state");
11261125
}
11271126

1128-
//----------------------------------------
1129-
// Check the web server settings and free the settings structure
1130-
//----------------------------------------
1131-
bool webServerSettingsCheckAndFree()
1132-
{
1133-
bool changed;
1134-
1135-
changed = false;
1136-
if (webServerPreviousSettings)
1137-
{
1138-
changed = wifiCheckSettings(webServerPreviousSettings);
1139-
rtkFree(webServerPreviousSettings, "WebServer previous settings");
1140-
webServerPreviousSettings = nullptr;
1141-
}
1142-
return changed;
1143-
}
1144-
1145-
//----------------------------------------
1146-
// Allocate the settings structure and clone the settings
1147-
//----------------------------------------
1148-
void webServerSettingsClone()
1149-
{
1150-
webServerPreviousSettings = (struct Settings *) rtkMalloc(sizeof(settings), "WebServer previous settings");
1151-
if (webServerPreviousSettings == nullptr)
1152-
systemPrintln("ERROR: Web Server failed to allocate previous settings!\r\n");
1153-
else
1154-
memcpy(webServerPreviousSettings, &settings, sizeof(settings));
1155-
}
1156-
11571127
//----------------------------------------
11581128
// Start the Web Server state machine
11591129
//----------------------------------------
@@ -1173,7 +1143,7 @@ void webServerStart()
11731143
systemPrintln("Web Server: Starting");
11741144

11751145
// Start the network
1176-
if ((settings.wifiConfigOverAP == false) || networkHasInternet())
1146+
if ((settings.wifiConfigOverAP == false) || networkInterfaceHasInternet(NETWORK_WIFI_STATION))
11771147
networkConsumerAdd(NETCONSUMER_WEB_CONFIG, NETWORK_ANY, __FILE__, __LINE__);
11781148
if (settings.wifiConfigOverAP)
11791149
networkSoftApConsumerAdd(NETCONSUMER_WEB_CONFIG, __FILE__, __LINE__);

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,15 @@ bool wifiSoftApSsidSet; // Set when the WiFi soft AP SSID string exists
341341
bool wifiStationRestart; // Restart Wifi station
342342
bool 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
347349
void 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
447475
void 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;

Firmware/RTK_Everywhere/menuCommands.ino

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,9 +1305,6 @@ SettingValueResponse updateSettingWithValue(bool inCommands, const char *setting
13051305

13061306
knownSetting = true;
13071307

1308-
// Check to see if WiFi settings have been changed
1309-
//bool changed = webServerSettingsCheckAndFree();
1310-
13111308
// Inform the OTA state machine that it is needed
13121309
otaRequestFirmwareVersionCheck = true;
13131310
}

0 commit comments

Comments
 (0)