Skip to content

Commit ae201aa

Browse files
authored
Merge pull request #525 from LeeLeahy2/fix-network-failover-detection
Fix network failover detection
2 parents a145103 + c82ab45 commit ae201aa

File tree

12 files changed

+70
-38
lines changed

12 files changed

+70
-38
lines changed

Firmware/RTK_Everywhere/HTTP_Client.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static uint32_t httpClientConnectionAttemptTimeout = 5 * 1000L; // Wait 5s
5959
static int httpClientConnectionAttemptsTotal; // Count the number of connection attempts absolutely
6060

6161
static volatile uint32_t httpClientLastDataReceived; // Last time data was received via HTTP
62+
static NetPriority_t httpClientPriority = NETWORK_OFFLINE;
6263

6364
static NetworkClientSecure *httpSecureClient;
6465

@@ -275,6 +276,7 @@ void httpClientUpdate()
275276
case HTTP_CLIENT_ON: {
276277
if ((millis() - httpClientTimer) > httpClientConnectionAttemptTimeout)
277278
{
279+
httpClientPriority = NETWORK_OFFLINE;
278280
httpClientSetState(HTTP_CLIENT_NETWORK_STARTED);
279281
}
280282
break;
@@ -286,16 +288,16 @@ void httpClientUpdate()
286288
if (!httpClientModeNeeded)
287289
httpClientStop(true);
288290

289-
// Wait until the network is connected
290-
else if (networkHasInternet())
291+
// Wait until the network is connected to the media
292+
else if (networkIsConnected(&httpClientPriority))
291293
httpClientSetState(HTTP_CLIENT_CONNECTING_2_SERVER);
292294
break;
293295
}
294296

295297
// Connect to the HTTP server
296298
case HTTP_CLIENT_CONNECTING_2_SERVER: {
297299
// Determine if the network has failed
298-
if (networkHasInternet() == false)
300+
if (!networkIsConnected(&httpClientPriority))
299301
{
300302
// Failed to connect to the network, attempt to restart the network
301303
httpClientStop(true); // Was httpClientRestart(); - #StopVsRestart
@@ -352,7 +354,7 @@ void httpClientUpdate()
352354

353355
case HTTP_CLIENT_CONNECTED: {
354356
// Determine if the network has failed
355-
if (networkHasInternet() == false)
357+
if (!networkIsConnected(&httpClientPriority))
356358
{
357359
// Failed to connect to the network, attempt to restart the network
358360
httpClientStop(true); // Was httpClientRestart(); - #StopVsRestart
@@ -556,7 +558,7 @@ void httpClientUpdate()
556558
// Hang here until httpClientModeNeeded is set to false by updateProvisioning
557559
case HTTP_CLIENT_COMPLETE: {
558560
// Determine if the network has failed
559-
if (networkHasInternet() == false)
561+
if (!networkIsConnected(&httpClientPriority))
560562
// Failed to connect to the network, attempt to restart the network
561563
httpClientStop(true); // Was httpClientRestart(); - #StopVsRestart
562564
break;

Firmware/RTK_Everywhere/MQTT_Client.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static uint32_t mqttClientConnectionAttemptTimeout;
138138
static int mqttClientConnectionAttemptsTotal; // Count the number of connection attempts absolutely
139139

140140
static volatile uint32_t mqttClientLastDataReceived; // Last time data was received via MQTT
141+
static NetPriority_t mqttClientPriority = NETWORK_OFFLINE;
141142

142143
static NetworkClientSecure *mqttSecureClient;
143144

@@ -722,6 +723,7 @@ void mqttClientUpdate()
722723
case MQTT_CLIENT_ON: {
723724
if ((millis() - mqttClientTimer) > mqttClientConnectionAttemptTimeout)
724725
{
726+
mqttClientPriority = NETWORK_OFFLINE;
725727
mqttClientSetState(MQTT_CLIENT_WAIT_FOR_NETWORK);
726728
}
727729
break;
@@ -734,15 +736,15 @@ void mqttClientUpdate()
734736
mqttClientStop(true);
735737

736738
// Wait until the network is connected to the media
737-
else if (networkHasInternet())
739+
else if (networkIsConnected(&mqttClientPriority))
738740
mqttClientSetState(MQTT_CLIENT_CONNECTING_2_SERVER);
739741
break;
740742
}
741743

742744
// Connect to the MQTT server
743745
case MQTT_CLIENT_CONNECTING_2_SERVER: {
744746
// Determine if the network has failed
745-
if (networkHasInternet() == false)
747+
if (!networkIsConnected(&mqttClientPriority))
746748
{
747749
// Failed to connect to the network, attempt to restart the network
748750
mqttClientStop(true); // Was mqttClientRestart(); - #StopVsRestart
@@ -888,7 +890,7 @@ void mqttClientUpdate()
888890

889891
case MQTT_CLIENT_SERVICES_CONNECTED: {
890892
// Determine if the network has failed
891-
if (networkHasInternet() == false)
893+
if (!networkIsConnected(&mqttClientPriority))
892894
{
893895
// Failed to connect to the network, attempt to restart the network
894896
mqttClientStop(true); // Was mqttClientRestart(); - #StopVsRestart

Firmware/RTK_Everywhere/NTP.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const RtkMode_t ntpServerMode = RTK_MODE_NTP;
7575
// Locals
7676
//----------------------------------------
7777

78+
static NetPriority_t ntpServerPriority = NETWORK_OFFLINE;
7879
static NetworkUDP *ntpServer; // This will be instantiated when we know the NTP port
7980
static uint8_t ntpServerState;
8081
static uint32_t lastLoggedNTPRequest;
@@ -792,6 +793,7 @@ void ntpServerUpdate()
792793
// The NTP server only works over Ethernet
793794
if (networkInterfaceHasInternet(NETWORK_ETHERNET))
794795
{
796+
ntpServerPriority = NETWORK_OFFLINE;
795797
ntpServerSetState(NTP_STATE_NETWORK_CONNECTED);
796798
}
797799
}

Firmware/RTK_Everywhere/Network.ino

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,22 @@ const char *networkGetNameByPriority(NetPriority_t priority)
583583
return "None";
584584
}
585585

586+
//----------------------------------------
587+
// Determine if the network is available
588+
//----------------------------------------
589+
bool networkIsConnected(NetPriority_t *clientPriority)
590+
{
591+
// If the client is using the highest priority network and that
592+
// network is still available then continue as normal
593+
if (networkHasInternet_bm && (*clientPriority == networkPriority))
594+
return (networkHasInternet_bm & (1 << networkIndexTable[networkPriority]))
595+
? true : false;
596+
597+
// The network has changed, notify the client of the change
598+
*clientPriority = networkPriority;
599+
return false;
600+
}
601+
586602
//----------------------------------------
587603
// Determine if the network interface is online
588604
//----------------------------------------

Firmware/RTK_Everywhere/NtripClient.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static volatile uint8_t ntripClientState = NTRIP_CLIENT_OFF;
187187
static int ntripClientConnectionAttempts; // Count the number of connection attempts between restarts
188188
static uint32_t ntripClientConnectionAttemptTimeout;
189189
static int ntripClientConnectionAttemptsTotal; // Count the number of connection attempts absolutely
190+
static NetPriority_t ntripClientPriority = NETWORK_OFFLINE;
190191

191192
// NTRIP client timer usage:
192193
// * Reconnection delay
@@ -562,6 +563,7 @@ void ntripClientUpdate()
562563

563564
// Start the network
564565
case NTRIP_CLIENT_ON:
566+
ntripClientPriority = NETWORK_OFFLINE;
565567
ntripClientSetState(NTRIP_CLIENT_WAIT_FOR_NETWORK);
566568
break;
567569

@@ -571,8 +573,8 @@ void ntripClientUpdate()
571573
if (ntripClientForcedShutdown || NEQ_RTK_MODE(ntripClientMode) || !settings.enableNtripClient)
572574
ntripClientStop(true);
573575

574-
// Wait until the network is connected
575-
else if (networkHasInternet())
576+
// Wait until the network is connected to the media
577+
else if (networkIsConnected(&ntripClientPriority))
576578
{
577579
// Allocate the ntripClient structure
578580
ntripClient = new NetworkClient();
@@ -594,7 +596,7 @@ void ntripClientUpdate()
594596

595597
case NTRIP_CLIENT_NETWORK_CONNECTED:
596598
// Determine if the network has failed
597-
if (networkHasInternet() == false)
599+
if (!networkIsConnected(&ntripClientPriority))
598600
// Failed to connect to to the network, attempt to restart the network
599601
ntripClientStop(true); // Was ntripClientRestart(); - #StopVsRestart
600602

@@ -627,7 +629,7 @@ void ntripClientUpdate()
627629

628630
case NTRIP_CLIENT_WAIT_RESPONSE:
629631
// Determine if the network has failed
630-
if (networkHasInternet() == false)
632+
if (!networkIsConnected(&ntripClientPriority))
631633
// Failed to connect to to the network, attempt to restart the network
632634
ntripClientStop(true); // Was ntripClientRestart(); - #StopVsRestart
633635

@@ -748,7 +750,7 @@ void ntripClientUpdate()
748750

749751
case NTRIP_CLIENT_CONNECTED:
750752
// Determine if the network has failed
751-
if (networkHasInternet() == false)
753+
if (!networkIsConnected(&ntripClientPriority))
752754
// Failed to connect to to the network, attempt to restart the network
753755
ntripClientStop(true); // Was ntripClientRestart(); - #StopVsRestart
754756

Firmware/RTK_Everywhere/NtripServer.ino

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ const RtkMode_t ntripServerMode = RTK_MODE_BASE_FIXED;
175175

176176
// NTRIP Servers
177177
static NTRIP_SERVER_DATA ntripServerArray[NTRIP_SERVER_MAX];
178+
static NetPriority_t ntripServerPriority = NETWORK_OFFLINE;
178179

179180
//----------------------------------------
180181
// NTRIP Server Routines
@@ -607,6 +608,7 @@ void ntripServerUpdate(int serverIndex)
607608

608609
// Start the network
609610
case NTRIP_SERVER_ON:
611+
ntripServerPriority = NETWORK_OFFLINE;
610612
ntripServerSetState(ntripServer, NTRIP_SERVER_WAIT_FOR_NETWORK);
611613
break;
612614

@@ -622,7 +624,7 @@ void ntripServerUpdate(int serverIndex)
622624
}
623625

624626
// Wait until the network is connected
625-
else if (networkHasInternet())
627+
else if (networkIsConnected(&ntripServerPriority))
626628
{
627629
// Allocate the networkClient structure
628630
ntripServer->networkClient = new NetworkClient();
@@ -645,7 +647,7 @@ void ntripServerUpdate(int serverIndex)
645647
// Network available
646648
case NTRIP_SERVER_NETWORK_CONNECTED:
647649
// Determine if the network has failed
648-
if (networkHasInternet() == false)
650+
if (!networkIsConnected(&ntripServerPriority))
649651
// Failed to connect to to the network, attempt to restart the network
650652
ntripServerStop(serverIndex, true); // Was ntripServerRestart(serverIndex); - #StopVsRestart
651653

@@ -663,7 +665,7 @@ void ntripServerUpdate(int serverIndex)
663665
// Wait for GNSS correction data
664666
case NTRIP_SERVER_WAIT_GNSS_DATA:
665667
// Determine if the network has failed
666-
if (networkHasInternet() == false)
668+
if (!networkIsConnected(&ntripServerPriority))
667669
// Failed to connect to to the network, attempt to restart the network
668670
ntripServerStop(serverIndex, true); // Was ntripServerRestart(serverIndex); - #StopVsRestart
669671

@@ -673,7 +675,7 @@ void ntripServerUpdate(int serverIndex)
673675
// Initiate the connection to the NTRIP caster
674676
case NTRIP_SERVER_CONNECTING:
675677
// Determine if the network has failed
676-
if (networkHasInternet() == false)
678+
if (!networkIsConnected(&ntripServerPriority))
677679
// Failed to connect to to the network, attempt to restart the network
678680
ntripServerStop(serverIndex, true); // Was ntripServerRestart(serverIndex); - #StopVsRestart
679681

@@ -701,7 +703,7 @@ void ntripServerUpdate(int serverIndex)
701703
// Wait for authorization response
702704
case NTRIP_SERVER_AUTHORIZATION:
703705
// Determine if the network has failed
704-
if (networkHasInternet() == false)
706+
if (!networkIsConnected(&ntripServerPriority))
705707
// Failed to connect to to the network, attempt to restart the network
706708
ntripServerStop(serverIndex, true); // Was ntripServerRestart(serverIndex); - #StopVsRestart
707709

@@ -795,7 +797,7 @@ void ntripServerUpdate(int serverIndex)
795797
// NTRIP server authorized to send RTCM correction data to NTRIP caster
796798
case NTRIP_SERVER_CASTING:
797799
// Determine if the network has failed
798-
if (networkHasInternet() == false)
800+
if (!networkIsConnected(&ntripServerPriority))
799801
// Failed to connect to the network, attempt to restart the network
800802
ntripServerStop(serverIndex, true); // Was ntripServerRestart(serverIndex); - #StopVsRestart
801803

Firmware/RTK_Everywhere/TcpClient.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const RtkMode_t tcpClientMode = RTK_MODE_BASE_FIXED | RTK_MODE_BASE_SURVEY_IN |
148148

149149
static NetworkClient *tcpClient;
150150
static IPAddress tcpClientIpAddress;
151+
static NetPriority_t tcpClientPriority = NETWORK_OFFLINE;
151152
static uint8_t tcpClientState;
152153
static volatile RING_BUFFER_OFFSET tcpClientTail;
153154
static volatile bool tcpClientWriteError;
@@ -416,6 +417,7 @@ void tcpClientUpdate()
416417
if (EQ_RTK_MODE(tcpClientMode) && settings.enableTcpClient)
417418
{
418419
timer = 0;
420+
tcpClientPriority = NETWORK_OFFLINE;
419421
tcpClientSetState(TCP_CLIENT_STATE_WAIT_FOR_NETWORK);
420422
}
421423
break;
@@ -427,7 +429,7 @@ void tcpClientUpdate()
427429
tcpClientStop();
428430

429431
// Wait until the network is connected
430-
else if (networkHasInternet())
432+
else if (networkIsConnected(&tcpClientPriority))
431433
{
432434
#ifdef COMPILE_WIFI
433435
// Determine if WiFi is required
@@ -463,7 +465,7 @@ void tcpClientUpdate()
463465
// Attempt the connection ot the TCP server
464466
case TCP_CLIENT_STATE_CLIENT_STARTING:
465467
// Determine if the network has failed
466-
if (networkHasInternet() == false)
468+
if (!networkIsConnected(&tcpClientPriority))
467469
// Failed to connect to to the network, attempt to restart the network
468470
tcpClientStop();
469471

@@ -508,7 +510,7 @@ void tcpClientUpdate()
508510
// Wait for the TCP client to shutdown or a TCP client link failure
509511
case TCP_CLIENT_STATE_CONNECTED:
510512
// Determine if the network has failed
511-
if (networkHasInternet() == false)
513+
if (!networkIsConnected(&tcpClientPriority))
512514
// Failed to connect to to the network, attempt to restart the network
513515
tcpClientStop();
514516

Firmware/RTK_Everywhere/TcpServer.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static volatile uint8_t tcpServerClientWriteError;
7676
static NetworkClient *tcpServerClient[TCP_SERVER_MAX_CLIENTS];
7777
static IPAddress tcpServerClientIpAddress[TCP_SERVER_MAX_CLIENTS];
7878
static volatile RING_BUFFER_OFFSET tcpServerClientTails[TCP_SERVER_MAX_CLIENTS];
79+
static NetPriority_t tcpServerPriority = NETWORK_OFFLINE;
7980

8081
//----------------------------------------
8182
// TCP Server handleGnssDataTask Support Routines
@@ -380,6 +381,7 @@ void tcpServerUpdate()
380381
{
381382
if (settings.debugTcpServer && (!inMainMenu))
382383
systemPrintln("TCP server start");
384+
tcpServerPriority = NETWORK_OFFLINE;
383385
tcpServerSetState(TCP_SERVER_STATE_WAIT_FOR_NETWORK);
384386
}
385387
break;
@@ -391,7 +393,7 @@ void tcpServerUpdate()
391393
tcpServerStop();
392394

393395
// Wait until the network is connected to the media
394-
else if (networkHasInternet() || WIFI_SOFT_AP_RUNNING())
396+
else if (networkIsConnected(&tcpServerPriority))
395397
{
396398
// Delay before starting the TCP server
397399
if ((millis() - tcpServerTimer) >= (1 * 1000))
@@ -409,7 +411,7 @@ void tcpServerUpdate()
409411
// Handle client connections and link failures
410412
case TCP_SERVER_STATE_RUNNING:
411413
// Determine if the network has failed
412-
if ((networkHasInternet() == false && WIFI_SOFT_AP_RUNNING() == false) || (!settings.enableTcpServer && !settings.baseCasterOverride))
414+
if ((networkIsConnected(&tcpServerPriority) == false && WIFI_SOFT_AP_RUNNING() == false) || (!settings.enableTcpServer && !settings.baseCasterOverride))
413415
{
414416
if ((settings.debugTcpServer || PERIODIC_DISPLAY(PD_TCP_SERVER_DATA)) && (!inMainMenu))
415417
{

Firmware/RTK_Everywhere/UdpServer.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static NetworkUDP *udpServer = nullptr;
9494
static uint8_t udpServerState;
9595
static uint32_t udpServerTimer;
9696
static volatile RING_BUFFER_OFFSET udpServerTail;
97+
static NetPriority_t udpServerPriority;
9798

9899
//----------------------------------------
99100
// UDP Server handleGnssDataTask Support Routines
@@ -106,7 +107,7 @@ int32_t udpServerSendDataBroadcast(uint8_t *data, uint16_t length)
106107
return 0;
107108

108109
// Send the data as broadcast
109-
if (settings.enableUdpServer && online.udpServer && networkHasInternet())
110+
if (settings.enableUdpServer && online.udpServer && networkIsConnected(&udpServerPriority))
110111
{
111112
IPAddress broadcastAddress = networkGetBroadcastIpAddress();
112113
udpServer->beginPacket(broadcastAddress, settings.udpServerPort);
@@ -324,6 +325,7 @@ void udpServerUpdate()
324325
{
325326
if (settings.debugUdpServer && (!inMainMenu))
326327
systemPrintln("UDP server starting the network");
328+
udpServerPriority = NETWORK_OFFLINE;
327329
udpServerSetState(UDP_SERVER_STATE_WAIT_FOR_NETWORK);
328330
}
329331
break;
@@ -335,7 +337,7 @@ void udpServerUpdate()
335337
udpServerStop();
336338

337339
// Wait until the network is connected
338-
else if (networkHasInternet() || WIFI_SOFT_AP_RUNNING())
340+
else if (networkIsConnected(&udpServerPriority) || WIFI_SOFT_AP_RUNNING())
339341
{
340342
// Delay before starting the UDP server
341343
if ((millis() - udpServerTimer) >= (1 * 1000))
@@ -353,7 +355,7 @@ void udpServerUpdate()
353355
// Handle client connections and link failures
354356
case UDP_SERVER_STATE_RUNNING:
355357
// Determine if the network has failed
356-
if ((networkHasInternet() == false && WIFI_SOFT_AP_RUNNING() == false) || (!settings.enableUdpServer && !settings.baseCasterOverride))
358+
if ((networkIsConnected(&udpServerPriority) == false && WIFI_SOFT_AP_RUNNING() == false) || (!settings.enableUdpServer && !settings.baseCasterOverride))
357359
{
358360
if ((settings.debugUdpServer || PERIODIC_DISPLAY(PD_UDP_SERVER_DATA)) && (!inMainMenu))
359361
{

0 commit comments

Comments
 (0)