Skip to content

Commit acff0c4

Browse files
authored
Merge pull request #248 from LeeLeahy2/wifi-change
Restart WiFi when WiFi is running and menu changes occur
2 parents b04b4e4 + dea350a commit acff0c4

File tree

2 files changed

+110
-11
lines changed

2 files changed

+110
-11
lines changed

Firmware/RTK_Everywhere/Network.ino

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,25 @@ void networkDisplayIpAddress(uint8_t networkType)
399399
}
400400
}
401401

402+
//----------------------------------------
403+
// Display the network users
404+
//----------------------------------------
405+
void networkDisplayUsers(NETWORK_USER users)
406+
{
407+
uint8_t userNumber = 0;
408+
uint32_t mask;
409+
410+
while (users)
411+
{
412+
mask = 1 << userNumber;
413+
if (users & mask)
414+
{
415+
users &= ~mask;
416+
systemPrintf(" 0x%08x: %s\r\n", mask, networkUserToString(userNumber));
417+
}
418+
}
419+
}
420+
402421
//----------------------------------------
403422
// Get the network type
404423
//----------------------------------------
@@ -713,7 +732,7 @@ void networkSetState(NETWORK_DATA *network, byte newState)
713732
// Display the network state
714733
systemPrint("Network State: ");
715734
if (newState != network->state)
716-
systemPrintf("%s --> ", networkState[network->state]);
735+
systemPrintf("%s --> ", networkStateToString(network->state));
717736
else
718737
systemPrint("*");
719738

@@ -724,7 +743,7 @@ void networkSetState(NETWORK_DATA *network, byte newState)
724743
reportFatalError("Unknown network layer state");
725744
}
726745
else
727-
systemPrintf("%s\r\n", networkState[newState]);
746+
systemPrintf("%s\r\n", networkStateToString(newState));
728747
}
729748

730749
// Validate the network state
@@ -788,6 +807,42 @@ void networkStart(uint8_t networkType)
788807
}
789808
}
790809

810+
//----------------------------------------
811+
// Translate the network state into a string
812+
//----------------------------------------
813+
const char * networkStateToString(uint8_t state)
814+
{
815+
if (state >= networkStateEntries)
816+
return "Unknown";
817+
return networkState[state];
818+
}
819+
820+
//----------------------------------------
821+
// Display the network status
822+
//----------------------------------------
823+
void networkStatus(uint8_t networkType)
824+
{
825+
NETWORK_DATA *network;
826+
827+
// Get the network
828+
network = &networkData;
829+
830+
// Display the network status
831+
systemPrintf("requestedNetwork: %d (%s)\r\n", network->requestedNetwork,
832+
networkTypeToString(network->requestedNetwork));
833+
systemPrintf("type: %d (%s)\r\n", network->type, networkTypeToString(network->type));
834+
systemPrintf("activeUsers: 0x%08x\r\n", network->activeUsers);
835+
networkDisplayUsers(network->activeUsers);
836+
systemPrintf("userOpens: 0x%08x\r\n", network->userOpens);
837+
networkDisplayUsers(network->userOpens);
838+
systemPrintf("connectionAttempt: %d\r\n", network->connectionAttempt);
839+
systemPrintf("restart: %s\r\n", network->restart ? "true" : "false");
840+
systemPrintf("shutdown: %s\r\n", network->shutdown ? "true" : "false");
841+
systemPrintf("state: %d (%s)\r\n", network->state, networkStateToString(network->state));
842+
systemPrintf("timeout: %d\r\n", network->timeout);
843+
systemPrintf("timerStart: %d\r\n", network->timerStart);
844+
}
845+
791846
//----------------------------------------
792847
// Stop the network
793848
//----------------------------------------
@@ -967,6 +1022,16 @@ uint8_t networkTranslateNetworkType(uint8_t networkType, bool translateActive)
9671022
return newNetworkType;
9681023
}
9691024

1025+
//----------------------------------------
1026+
// Translate type into a string
1027+
//----------------------------------------
1028+
const char * networkTypeToString(uint8_t type)
1029+
{
1030+
if (type >= networkNameEntries)
1031+
return "Unknown";
1032+
return networkName[type];
1033+
}
1034+
9701035
//----------------------------------------
9711036
// Update the network device state
9721037
//----------------------------------------
@@ -1016,17 +1081,39 @@ void networkTypeUpdate(uint8_t networkType)
10161081
// Delay before starting the network
10171082
else if ((millis() - network->timerStart) >= network->timeout)
10181083
{
1019-
// Start the network
1020-
network->type = networkTranslateNetworkType(network->requestedNetwork, true);
1084+
// Determine the network type
1085+
uint8_t type = networkTranslateNetworkType(network->requestedNetwork, true);
1086+
1087+
// Verify that WiFi is configured properly
1088+
if (network->type == NETWORK_TYPE_WIFI)
1089+
{
1090+
// Verify that at least one SSID is available
1091+
if (!wifiNetworkCount())
1092+
{
1093+
// Display the SSID error message
1094+
systemPrintln("ERROR: Please enter at least one SSID before using WiFi");
1095+
displayNoSSIDs(2000);
1096+
1097+
// Restart the delay and try again
1098+
network->timerStart = millis();
1099+
network->timeout = NETWORK_CONNECTION_TIMEOUT;
1100+
break;
1101+
}
1102+
}
1103+
1104+
// Display the network type change
1105+
network->type = type;
10211106
if (settings.debugNetworkLayer && (network->type != network->requestedNetwork))
1022-
systemPrintf("networkTypeUpdate, network->type: %s --> %s\r\n", networkName[network->requestedNetwork],
1107+
systemPrintf("networkTypeUpdate, network->type: %s --> %s\r\n", networkTypeToString(network->requestedNetwork),
10231108
networkName[network->type]);
10241109
if (settings.debugNetworkLayer)
10251110
systemPrintf("networkTypeUpdate, network->requestedNetwork: %s --> %s\r\n",
1026-
networkName[network->requestedNetwork], networkName[network->type]);
1111+
networkName[network->requestedNetwork], networkTypeToString(network->type));
10271112
network->requestedNetwork = NETWORK_TYPE_ACTIVE;
10281113
if (settings.debugNetworkLayer)
1029-
systemPrintf("Network starting %s\r\n", networkName[network->type]);
1114+
systemPrintf("Network starting %s\r\n", networkTypeToString(network->type));
1115+
1116+
// Start the network
10301117
if (network->type == NETWORK_TYPE_WIFI)
10311118
wifiStart();
10321119
network->timerStart = millis();
@@ -1267,7 +1354,19 @@ bool networkUserOpen(uint8_t user, uint8_t networkType)
12671354
return false;
12681355
}
12691356

1357+
//----------------------------------------
1358+
// Translate user into a string
1359+
//----------------------------------------
1360+
const char * networkUserToString(uint8_t userNumber)
1361+
{
1362+
if (userNumber >= networkUserEntries)
1363+
return "Unknown";
1364+
return networkUser[userNumber];
1365+
}
1366+
1367+
//----------------------------------------
12701368
// Verify the network layer tables
1369+
//----------------------------------------
12711370
void networkVerifyTables()
12721371
{
12731372
// Verify the table lengths

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ void menuWiFi()
154154
else
155155
{
156156
// Restart WiFi if we are not in AP config mode
157-
if (wifiIsConnected())
157+
NETWORK_DATA *network = networkGet(NETWORK_TYPE_WIFI, false);
158+
if (network)
158159
{
159160
if (settings.debugWifiState == true)
160161
systemPrintln("Menu caused restarting of WiFi");
161-
WIFI_STOP();
162-
wifiStart();
163-
wifiConnectionAttempts = 0; // Reset the timeout
162+
networkRestartNetwork(network);
163+
networkStop(NETWORK_TYPE_WIFI);
164164
}
165165
}
166166
}

0 commit comments

Comments
 (0)