Skip to content

Commit f2fface

Browse files
committed
WiFi: Rename WIFI_STATION_STATE_RESTART to WIFI_STATION_STATE_RESTART_DELAY
Move startup delay from WIFI_STATION_STATE_STARTING into WIFI_STATION_STATE_RESTART_DELAY
1 parent ad547c2 commit f2fface

File tree

1 file changed

+84
-70
lines changed

1 file changed

+84
-70
lines changed

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 84 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ enum WIFI_STATION_STATES
4242
{
4343
WIFI_STATION_STATE_OFF,
4444
WIFI_STATION_STATE_WAIT_NO_USERS,
45-
WIFI_STATION_STATE_RESTART,
45+
WIFI_STATION_STATE_RESTART_DELAY,
4646
WIFI_STATION_STATE_STARTING,
4747
WIFI_STATION_STATE_ONLINE,
4848
WIFI_STATION_STATE_STABLE,
@@ -51,9 +51,14 @@ enum WIFI_STATION_STATES
5151
};
5252
uint8_t wifiStationState;
5353

54-
const char *wifiStationStateName[] = {
55-
"WIFI_STATION_STATE_OFF", "WIFI_STATION_STATE_WAIT_NO_USERS", "WIFI_STATION_STATE_RESTART",
56-
"WIFI_STATION_STATE_STARTING", "WIFI_STATION_STATE_ONLINE", "WIFI_STATION_STATE_STABLE",
54+
const char * wifiStationStateName[] =
55+
{
56+
"WIFI_STATION_STATE_OFF",
57+
"WIFI_STATION_STATE_WAIT_NO_USERS",
58+
"WIFI_STATION_STATE_RESTART_DELAY",
59+
"WIFI_STATION_STATE_STARTING",
60+
"WIFI_STATION_STATE_ONLINE",
61+
"WIFI_STATION_STATE_STABLE",
5762
};
5863
const int wifiStationStateNameEntries = sizeof(wifiStationStateName) / sizeof(wifiStationStateName[0]);
5964

@@ -935,12 +940,12 @@ void wifiStationUpdate()
935940
| enabled | |
936941
| | |
937942
V !enabled | |
938-
WIFI_STATION_STATE_RESTART_DELAY ----------' |
939-
| |
940-
| Timeout |
941-
| Complete |
942-
V !enabled |
943-
.----> WIFI_STATION_STATE_STARTING -------------. |
943+
.--> WIFI_STATION_STATE_RESTART_DELAY ----------' |
944+
| | |
945+
| | Timeout |
946+
| | Complete |
947+
| V !enabled |
948+
| WIFI_STATION_STATE_STARTING -------------. |
944949
| | | |
945950
| | WiFi connected | |
946951
| V !enabled | |
@@ -952,18 +957,14 @@ void wifiStationUpdate()
952957
| | | |
953958
| | !enabled | |
954959
| V | |
955-
| +<--------------------------' |
956-
| | |
960+
| Display +<--------------------------' |
961+
| delay | |
957962
| V |
958963
| WIFI_STATION_STATE_WAIT_NO_USERS |
959964
| | |
960965
| | No Users |
961-
| V !enabled |
962-
| +--------------------------------'
963-
| Display |
964-
| delay | enabled
965-
| V
966-
'------ WIFI_STATION_STATE_RESTART
966+
| enabled V !enabled |
967+
'-------------------+--------------------------------'
967968
968969
Network Loss Handling:
969970
@@ -1032,13 +1033,8 @@ void wifiStationUpdate()
10321033
timer = millis();
10331034
startTimeout = 0;
10341035

1035-
// Display the major state transition
1036-
if (settings.debugWifiState)
1037-
systemPrintf("--------------- %s Starting ---------------\r\n",
1038-
networkInterfaceTable[NETWORK_WIFI_STATION].name);
1039-
10401036
// Start WiFi station
1041-
wifiStationSetState(WIFI_STATION_STATE_STARTING);
1037+
wifiStationSetState(WIFI_STATION_STATE_RESTART_DELAY);
10421038
}
10431039
break;
10441040

@@ -1075,6 +1071,8 @@ void wifiStationUpdate()
10751071
networkInterfaceTable[NETWORK_WIFI_STATION].name);
10761072
wifiStationOff(__FILE__, __LINE__);
10771073
}
1074+
1075+
// Reset the start timeout
10781076
wifiStationSetState(WIFI_STATION_STATE_OFF);
10791077
}
10801078

@@ -1083,67 +1081,83 @@ void wifiStationUpdate()
10831081
{
10841082
// Clear the bits to perform the restart operation
10851083
wifi.clearStarted(WIFI_STA_RECONNECT);
1086-
wifiStationSetState(WIFI_STATION_STATE_RESTART);
1084+
1085+
// Display the restart delay and then start WiFi station
1086+
if (startTimeout && settings.debugWifiState)
1087+
{
1088+
// Display the delay
1089+
uint32_t seconds = startTimeout / MILLISECONDS_IN_A_SECOND;
1090+
uint32_t minutes = seconds / SECONDS_IN_A_MINUTE;
1091+
seconds -= minutes * SECONDS_IN_A_MINUTE;
1092+
systemPrintf("WiFi: Delaying %2d:%02d before restarting WiFi\r\n", minutes, seconds);
1093+
}
1094+
timer = millis();
1095+
wifiStationSetState(WIFI_STATION_STATE_RESTART_DELAY);
10871096
}
10881097
}
10891098
break;
10901099

1091-
// Display the restart delay and then start WiFi station
1092-
case WIFI_STATION_STATE_RESTART:
1093-
if (startTimeout && settings.debugWifiState)
1100+
// Perform the restart delay
1101+
case WIFI_STATION_STATE_RESTART_DELAY:
1102+
// Stop WiFi station if necessary
1103+
if (enabled == false)
10941104
{
1095-
// Display the delay
1096-
uint32_t seconds = startTimeout / MILLISECONDS_IN_A_SECOND;
1097-
uint32_t minutes = seconds / SECONDS_IN_A_MINUTE;
1098-
seconds -= minutes * SECONDS_IN_A_MINUTE;
1099-
systemPrintf("WiFi: Delaying %2d:%02d before restarting WiFi\r\n", minutes, seconds);
1105+
wifiStationSetState(WIFI_STATION_STATE_OFF);
1106+
break;
11001107
}
1101-
timer = millis();
1108+
1109+
// Delay before starting WiFi
1110+
if ((millis() - timer) < startTimeout)
1111+
break;
1112+
1113+
// Display the major state transition
1114+
if (settings.debugWifiState)
1115+
systemPrintf("--------------- %s Starting ---------------\r\n",
1116+
networkInterfaceTable[NETWORK_WIFI_STATION].name);
1117+
1118+
// Timeout complete
11021119
wifiStationSetState(WIFI_STATION_STATE_STARTING);
1103-
break;
1120+
1121+
// |
1122+
// | Fall through
1123+
// V
11041124

11051125
// At least one consumer is requesting a network
11061126
case WIFI_STATION_STATE_STARTING:
1107-
// Delay before starting WiFi
1108-
if ((millis() - timer) >= startTimeout)
1109-
{
1110-
timer = millis();
1111-
1112-
// Increase the timeout
1113-
startTimeout <<= 1;
1114-
if (!startTimeout)
1115-
startTimeout = WIFI_MIN_TIMEOUT;
1116-
else if (startTimeout > WIFI_MAX_TIMEOUT)
1117-
startTimeout = WIFI_MAX_TIMEOUT;
1127+
// Increase the timeout
1128+
startTimeout <<= 1;
1129+
if (!startTimeout)
1130+
startTimeout = WIFI_MIN_TIMEOUT;
1131+
else if (startTimeout > WIFI_MAX_TIMEOUT)
1132+
startTimeout = WIFI_MAX_TIMEOUT;
11181133

1119-
// Account for this connection attempt
1120-
connectionAttempts++;
1134+
// Account for this connection attempt
1135+
connectionAttempts++;
11211136

1122-
// Attempt to start WiFi station
1123-
if (wifiStationOn(__FILE__, __LINE__))
1124-
{
1125-
// Successfully connected to a remote AP
1126-
if (settings.debugWifiState)
1127-
systemPrintf("WiFi: WiFi station successfully started\r\n");
1137+
// Attempt to start WiFi station
1138+
if (wifiStationOn(__FILE__, __LINE__) == false)
1139+
{
1140+
// Failed to connect to a remote AP
1141+
if (settings.debugWifiState)
1142+
systemPrintf("WiFi: WiFi station failed to start!\r\n");
11281143

1129-
// WiFi station is now available
1130-
wifiStationSetState(WIFI_STATION_STATE_ONLINE);
1131-
}
1132-
else
1133-
{
1134-
// Failed to connect to a remote AP
1135-
if (settings.debugWifiState)
1136-
systemPrintf("WiFi: WiFi station failed to start!\r\n");
1144+
// Start the next network interface if necessary
1145+
if (connectionAttempts >= 2)
1146+
networkStartNextInterface(NETWORK_WIFI_STATION);
11371147

1138-
// Restart WiFi after delay
1139-
// Clear the bits to perform the restart operation
1140-
wifi.clearStarted(WIFI_STA_RECONNECT);
1141-
wifiStationSetState(WIFI_STATION_STATE_RESTART);
1148+
// Perform the restart delay
1149+
timer = millis();
1150+
wifiStationSetState(WIFI_STATION_STATE_RESTART_DELAY);
1151+
}
1152+
else
1153+
{
1154+
// Successfully connected to a remote AP
1155+
if (settings.debugWifiState)
1156+
systemPrintf("WiFi: WiFi station successfully started\r\n");
11421157

1143-
// Start the next network interface if necessary
1144-
if (connectionAttempts >= 2)
1145-
networkStartNextInterface(NETWORK_WIFI_STATION);
1146-
}
1158+
// WiFi station is now available
1159+
timer = millis();
1160+
wifiStationSetState(WIFI_STATION_STATE_ONLINE);
11471161
}
11481162
break;
11491163

0 commit comments

Comments
 (0)