|
11 | 11 | //****************************************
|
12 | 12 |
|
13 | 13 | #define WIFI_DEFAULT_CHANNEL 1
|
14 |
| -#define WIFI_IP_ADDRESS_TIMEOUT_MSEC (15 * 1000) |
| 14 | +#define WIFI_IP_ADDRESS_TIMEOUT_MSEC (15 * MILLISECONDS_IN_A_SECOND) |
| 15 | +#define WIFI_CONNECTION_STABLE_MSEC (15 * MILLISECONDS_IN_A_MINUTE) |
15 | 16 |
|
16 | 17 | static const char * wifiAuthorizationName[] =
|
17 | 18 | {
|
@@ -1031,6 +1032,182 @@ void wifiStop(NetIndex_t index, uintptr_t parameter, bool debug)
|
1031 | 1032 | networkSequenceNextEntry(NETWORK_WIFI_STATION, settings.debugNetworkLayer);
|
1032 | 1033 | }
|
1033 | 1034 |
|
| 1035 | +//********************************************************************* |
| 1036 | +// Update the WiFi station state |
| 1037 | +void wifiStationUpdate() |
| 1038 | +{ |
| 1039 | + static int connectionAttempts; |
| 1040 | + bool enabled; |
| 1041 | + bool online; |
| 1042 | + const char * reason; |
| 1043 | + static uint32_t startTimeout; |
| 1044 | + static uint32_t timer; |
| 1045 | + int users; |
| 1046 | + |
| 1047 | + // Determine if WiFi station should stop |
| 1048 | + enabled = wifiStationEnabled(&reason); |
| 1049 | + online = wifiStationOnline; |
| 1050 | + if ((enabled == false) && (wifiStationState >= WIFI_STATION_STATE_STARTING)) |
| 1051 | + { |
| 1052 | + // Display the reason why WiFi is disabled |
| 1053 | + if (settings.debugWifiState) |
| 1054 | + systemPrintf("WiFi Station %s\r\n", reason); |
| 1055 | + |
| 1056 | + // Notify the consumers that WiFi is shutting down |
| 1057 | + if (online) |
| 1058 | + { |
| 1059 | + // Notify the consumers that the network connection is broken |
| 1060 | + networkConsumerReconnect(NETWORK_WIFI_STATION); |
| 1061 | + |
| 1062 | + // Tell the network layer that the network is offline |
| 1063 | + // This prevents network consumers from reconnecting to this network |
| 1064 | + networkInterfaceInternetConnectionLost(NETWORK_WIFI_STATION); |
| 1065 | + |
| 1066 | + // WiFi station is no longer online |
| 1067 | + wifi.clearStarted(WIFI_STA_ONLINE); |
| 1068 | + wifiStationOnline = false; |
| 1069 | + } |
| 1070 | + wifiStationSetState(WIFI_STATION_STATE_WAIT_NO_USERS); |
| 1071 | + } |
| 1072 | + |
| 1073 | + // Update the WiFi station state |
| 1074 | + switch (wifiStationState) |
| 1075 | + { |
| 1076 | + // There are no WiFi station consumers |
| 1077 | + case WIFI_STATION_STATE_OFF: |
| 1078 | + if (enabled) |
| 1079 | + { |
| 1080 | + connectionAttempts = 0; |
| 1081 | + timer = millis(); |
| 1082 | + startTimeout = 0; |
| 1083 | + |
| 1084 | + // Display the major state transition |
| 1085 | + if (settings.debugWifiState) |
| 1086 | + systemPrintf("--------------- %s Starting ---------------\r\n", |
| 1087 | + networkInterfaceTable[NETWORK_WIFI_STATION].name); |
| 1088 | + |
| 1089 | + // Start WiFi station |
| 1090 | + wifiStationSetState(WIFI_STATION_STATE_STARTING); |
| 1091 | + } |
| 1092 | + break; |
| 1093 | + |
| 1094 | + // Wait for WiFi station users to release resources before shutting |
| 1095 | + // down WiFi station |
| 1096 | + case WIFI_STATION_STATE_WAIT_NO_USERS: |
| 1097 | + users = networkUserCount(NETWORK_WIFI_STATION); |
| 1098 | + if (users) |
| 1099 | + { |
| 1100 | + static uint32_t lastMsec; |
| 1101 | + |
| 1102 | + // Display the network users |
| 1103 | + uint32_t currentMsec = millis(); |
| 1104 | + if (settings.debugWifiState && ((currentMsec - lastMsec) > (2 * 1000))) |
| 1105 | + { |
| 1106 | + lastMsec = currentMsec; |
| 1107 | + systemPrintf("%s: Waiting for WiFi users to shutdown\r\n", |
| 1108 | + networkInterfaceTable[NETWORK_WIFI_STATION].name); |
| 1109 | + networkUserDisplay(NETWORK_WIFI_STATION); |
| 1110 | + } |
| 1111 | + } |
| 1112 | + |
| 1113 | + // No more network users |
| 1114 | + else |
| 1115 | + { |
| 1116 | + // Stop WiFi station if necessary |
| 1117 | + if (enabled == false) |
| 1118 | + { |
| 1119 | + // Display the major state transition |
| 1120 | + if (wifiStationRunning) |
| 1121 | + { |
| 1122 | + if (settings.debugWifiState) |
| 1123 | + systemPrintf("--------------- %s Stopping ---------------\r\n", |
| 1124 | + networkInterfaceTable[NETWORK_WIFI_STATION].name); |
| 1125 | + wifiStationOff(__FILE__, __LINE__); |
| 1126 | + } |
| 1127 | + wifiStationSetState(WIFI_STATION_STATE_OFF); |
| 1128 | + } |
| 1129 | + |
| 1130 | + // Restart WiFi after delay |
| 1131 | + else |
| 1132 | + { |
| 1133 | + // Clear the bits to perform the restart operation |
| 1134 | + wifi.clearStarted(WIFI_STA_RECONNECT); |
| 1135 | + wifiStationSetState(WIFI_STATION_STATE_RESTART); |
| 1136 | + } |
| 1137 | + } |
| 1138 | + break; |
| 1139 | + |
| 1140 | + // Display the restart delay and then start WiFi station |
| 1141 | + case WIFI_STATION_STATE_RESTART: |
| 1142 | + if (startTimeout && settings.debugWifiState) |
| 1143 | + { |
| 1144 | + // Display the delay |
| 1145 | + uint32_t seconds = startTimeout / MILLISECONDS_IN_A_SECOND; |
| 1146 | + uint32_t minutes = seconds / SECONDS_IN_A_MINUTE; |
| 1147 | + seconds -= minutes * SECONDS_IN_A_MINUTE; |
| 1148 | + systemPrintf("WiFi: Delaying %2d:%02d before restarting WiFi\r\n", minutes, seconds); |
| 1149 | + } |
| 1150 | + timer = millis(); |
| 1151 | + wifiStationSetState(WIFI_STATION_STATE_STARTING); |
| 1152 | + break; |
| 1153 | + |
| 1154 | + // At least one consumer is requesting a network |
| 1155 | + case WIFI_STATION_STATE_STARTING: |
| 1156 | + // Delay before starting WiFi |
| 1157 | + if ((millis() - timer) >= startTimeout) |
| 1158 | + { |
| 1159 | + timer = millis(); |
| 1160 | + |
| 1161 | + // Increase the timeout |
| 1162 | + startTimeout <<= 1; |
| 1163 | + if (!startTimeout) |
| 1164 | + startTimeout = WIFI_MIN_TIMEOUT; |
| 1165 | + else if (startTimeout > WIFI_MAX_TIMEOUT) |
| 1166 | + startTimeout = WIFI_MAX_TIMEOUT; |
| 1167 | + |
| 1168 | + // Account for this connection attempt |
| 1169 | + connectionAttempts++; |
| 1170 | + |
| 1171 | + // Attempt to start WiFi station |
| 1172 | + if (wifiStationOn(__FILE__, __LINE__)) |
| 1173 | + { |
| 1174 | + // Successfully connected to a remote AP |
| 1175 | + if (settings.debugWifiState) |
| 1176 | + systemPrintf("WiFi: WiFi station successfully started\r\n"); |
| 1177 | + |
| 1178 | + // WiFi station is now available |
| 1179 | + wifiStationSetState(WIFI_STATION_STATE_ONLINE); |
| 1180 | + } |
| 1181 | + else |
| 1182 | + { |
| 1183 | + // Failed to connect to a remote AP |
| 1184 | + if (settings.debugWifiState) |
| 1185 | + systemPrintf("WiFi: WiFi station failed to start!\r\n"); |
| 1186 | + |
| 1187 | + // Start the next network interface if necessary |
| 1188 | + if (connectionAttempts >= 2) |
| 1189 | + networkStartNextInterface(NETWORK_WIFI_STATION); |
| 1190 | + } |
| 1191 | + } |
| 1192 | + break; |
| 1193 | + |
| 1194 | + // WiFi station consumers have internet access |
| 1195 | + case WIFI_STATION_STATE_ONLINE: |
| 1196 | + // Wait until the WiFi link is stable |
| 1197 | + if ((millis() - timer) >= WIFI_CONNECTION_STABLE_MSEC) |
| 1198 | + { |
| 1199 | + connectionAttempts = 0; |
| 1200 | + startTimeout = 0; |
| 1201 | + wifiStationSetState(WIFI_STATION_STATE_STABLE); |
| 1202 | + } |
| 1203 | + break; |
| 1204 | + |
| 1205 | + // WiFi station consumers have internet access |
| 1206 | + case WIFI_STATION_STATE_STABLE: |
| 1207 | + break; |
| 1208 | + } |
| 1209 | +} |
| 1210 | + |
1034 | 1211 | //*********************************************************************
|
1035 | 1212 | // Stop WiFi and release all resources
|
1036 | 1213 | void wifiStopAll()
|
|
0 commit comments