Skip to content

Rework WiFi states and better handle restart #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: release_candidate
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion Firmware/RTK_Everywhere/Developer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void wifiDisplayNetworkData() {}
void wifiDisplaySoftApStatus() {}
bool wifiEspNowOff(const char * fileName, uint32_t lineNumber) {return true;}
bool wifiEspNowOn(const char * fileName, uint32_t lineNumber) {return false;}
void wifiEspNowSetChannel(WIFI_CHANNEL_t channel) {}
void wifiEspNowChannelSet(WIFI_CHANNEL_t channel) {}
int wifiNetworkCount() {return 0;}
void wifiResetTimeout() {}
IPAddress wifiSoftApGetIpAddress() {return IPAddress((uint32_t)0);}
Expand Down
12 changes: 12 additions & 0 deletions Firmware/RTK_Everywhere/HTTP_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ void httpClientUpdate()
// Connect to the HTTP server
case HTTP_CLIENT_CONNECTING_2_SERVER: {
// Allocate the httpSecureClient structure
networkUseDefaultInterface();
httpSecureClient = new NetworkClientSecure();
if (!httpSecureClient)
{
Expand All @@ -371,6 +372,17 @@ void httpClientUpdate()
if (!httpSecureClient->connect(THINGSTREAM_SERVER, HTTPS_PORT))
{
// Failed to connect to the server
int length = 1024;
char * errMessage = (char *)rtkMalloc(length, "HTTP error message");
if (errMessage)
{
memset(errMessage, 0, length);
httpSecureClient->lastError(errMessage, length - 1);
systemPrintf("Get %s failed, %s\r\n", THINGSTREAM_SERVER, errMessage);
rtkFree(errMessage, "HTTP error message");
}
else
systemPrintf("Get %s failed!\r\n", THINGSTREAM_SERVER);
systemPrintln("ERROR: Failed to connect to the Thingstream server!");
httpClientRestart(); // I _think_ we want to restart here - i.e. retry after the timeout?
break;
Expand Down
11 changes: 11 additions & 0 deletions Firmware/RTK_Everywhere/MQTT_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ void mqttClientUpdate()
// Connect to the MQTT broker
case MQTT_CLIENT_CONNECTING_2_BROKER: {
// Allocate the mqttSecureClient structure
networkUseDefaultInterface();
mqttSecureClient = new NetworkClientSecure();
if (!mqttSecureClient)
{
Expand Down Expand Up @@ -951,6 +952,16 @@ void mqttClientUpdate()
// Attempt connection to the MQTT broker
if (!mqttClient->connect(settings.pointPerfectBrokerHost, 8883))
{
// Failed to connect to the server
int length = 1024;
char * errMessage = (char *)rtkMalloc(length, "HTTP error message");
if (errMessage)
{
memset(errMessage, 0, length);
mqttSecureClient->lastError(errMessage, length - 1);
systemPrintf("MQTT Error: %s\r\n", errMessage);
rtkFree(errMessage, "HTTP error message");
}
systemPrintf("Failed to connect to MQTT broker %s\r\n", settings.pointPerfectBrokerHost);
mqttClientRestart();
break;
Expand Down
79 changes: 79 additions & 0 deletions Firmware/RTK_Everywhere/Network.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,37 @@ void networkInterfaceInternetConnectionAvailable(NetIndex_t index)
networkMulticastDNSStart(previousIndex);
}

/*
Network Loss Handling:

Arduino IP lost event
|
|
V
networkInterfaceEventInternetLost
|
| Set internet lost event flag
V
networkUpdate
|
| Clear internet lost event flag
V
+<------- Fake connection loss
|
V
networkInterfaceInternetConnectionLost
|
| Notify Interface of connection loss
V
.----------------+----------------.
| |
| |
V V
networkInterfaceRunning Interface stop sequence
called by xxxUpdate
or xxxEnabled
*/

//----------------------------------------
// Mark network interface as having NO access to the internet
//----------------------------------------
Expand Down Expand Up @@ -1377,6 +1408,35 @@ void networkInterfaceInternetConnectionLost(NetIndex_t index)
}
}

//----------------------------------------
// Get the interface priority
//----------------------------------------
NetPriority_t networkInterfacePriority(NetIndex_t index)
{
NetPriority_t priority;

// Validate the index
networkValidateIndex(index);

// Get the interface priority
priority = networkIndexTable[index];
return priority;
}

//----------------------------------------
// Determine if the interface should be running
//----------------------------------------
NetPriority_t networkInterfaceRunning(NetIndex_t index)
{
NetPriority_t priority;

// Get the interface priority
priority = networkInterfacePriority(index);

// Return the running status
return (networkPriority >= priority);
}

//----------------------------------------
// Determine if the specified network interface is higher priority than
// the current network interface
Expand Down Expand Up @@ -2434,6 +2494,25 @@ void networkUpdate()
}
}

//----------------------------------------
// Set the default network interface
//----------------------------------------
void networkUseDefaultInterface()
{
NetIndex_t index;
bool isDefault;

// Get the network index
index = networkGetCurrentInterfaceIndex();
if (index < NETWORK_OFFLINE)
{
// Get the default network interface
isDefault = networkInterfaceTable[index].netif->isDefault();
if (!isDefault)
networkInterfaceTable[index].netif->setDefault();
}
}

//----------------------------------------
// Add a network user
//----------------------------------------
Expand Down
1 change: 1 addition & 0 deletions Firmware/RTK_Everywhere/NtripClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ void ntripClientUpdate()
if (connected)
{
// Allocate the ntripClient structure
networkUseDefaultInterface();
ntripClient = new NetworkClient();
if (!ntripClient)
{
Expand Down
1 change: 1 addition & 0 deletions Firmware/RTK_Everywhere/TcpClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ bool tcpClientStart()
NetworkClient *client;

// Allocate the TCP client
networkUseDefaultInterface();
client = new NetworkClient();
if (client)
{
Expand Down
Loading