Skip to content

Commit 5a5c744

Browse files
authored
Merge pull request #657 from LeeLeahy2/wip-ntrip-server
Fix NTRIP server, now supports multiple NTRIP server streams
2 parents 0c545ac + 4af8056 commit 5a5c744

File tree

3 files changed

+32
-50
lines changed

3 files changed

+32
-50
lines changed

Firmware/RTK_Everywhere/NtripServer.ino

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ NtripServer.ino
8282
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8383
NTRIP Server States:
8484
NTRIP_SERVER_OFF: Network off or using NTRIP Client
85-
NTRIP_SERVER_ON: WIFI_STATE_START state
8685
NTRIP_SERVER_WAIT_FOR_NETWORK: Connecting to the network
8786
NTRIP_SERVER_NETWORK_CONNECTED: Connected to the network
8887
NTRIP_SERVER_WAIT_GNSS_DATA: Waiting for correction data from GNSS
@@ -94,12 +93,8 @@ NtripServer.ino
9493
| ^
9594
ntripServerStart | | ntripServerShutdown()
9695
v |
97-
.---------> NTRIP_SERVER_ON <-------------------.
96+
.--> NTRIP_SERVER_WAIT_FOR_NETWORK <------------.
9897
| | |
99-
| | | ntripServerRestart()
100-
| v Fail |
101-
| NTRIP_SERVER_WAIT_FOR_NETWORK ------------->+
102-
| | ^
10398
| | |
10499
| v Fail |
105100
| NTRIP_SERVER_NETWORK_CONNECTED ----------->+
@@ -145,7 +140,6 @@ static const int NTRIP_SERVER_CONNECTION_TIME = 5 * 60 * 1000;
145140
enum NTRIPServerState
146141
{
147142
NTRIP_SERVER_OFF = 0, // Using Bluetooth or NTRIP client
148-
NTRIP_SERVER_ON, // WIFI_STATE_START state
149143
NTRIP_SERVER_WAIT_FOR_NETWORK, // Connecting to WiFi access point
150144
NTRIP_SERVER_NETWORK_CONNECTED, // WiFi connected to an access point
151145
NTRIP_SERVER_WAIT_GNSS_DATA, // Waiting for correction data from GNSS
@@ -157,7 +151,6 @@ enum NTRIPServerState
157151
};
158152

159153
const char *const ntripServerStateName[] = {"NTRIP_SERVER_OFF",
160-
"NTRIP_SERVER_ON",
161154
"NTRIP_SERVER_WAIT_FOR_NETWORK",
162155
"NTRIP_SERVER_NETWORK_CONNECTED",
163156
"NTRIP_SERVER_WAIT_GNSS_DATA",
@@ -169,6 +162,12 @@ const int ntripServerStateNameEntries = sizeof(ntripServerStateName) / sizeof(nt
169162

170163
const RtkMode_t ntripServerMode = RTK_MODE_BASE_FIXED;
171164

165+
//----------------------------------------
166+
// Macros
167+
//----------------------------------------
168+
169+
#define NETWORK_CONSUMER(x) (NETCONSUMER_NTRIP_SERVER + x)
170+
172171
//----------------------------------------
173172
// Locals
174173
//----------------------------------------
@@ -345,7 +344,6 @@ void ntripServerPrintStateSummary(int serverIndex)
345344
case NTRIP_SERVER_OFF:
346345
systemPrint("Disconnected");
347346
break;
348-
case NTRIP_SERVER_ON:
349347
case NTRIP_SERVER_WAIT_FOR_NETWORK:
350348
case NTRIP_SERVER_NETWORK_CONNECTED:
351349
case NTRIP_SERVER_WAIT_GNSS_DATA:
@@ -464,7 +462,7 @@ void ntripServerProcessRTCM(int serverIndex, uint8_t incoming)
464462
// Indicate that the GNSS is providing correction data
465463
else if (ntripServer->state == NTRIP_SERVER_WAIT_GNSS_DATA)
466464
{
467-
ntripServerSetState(ntripServer, NTRIP_SERVER_CONNECTING);
465+
ntripServerSetState(serverIndex, NTRIP_SERVER_CONNECTING);
468466
}
469467
}
470468

@@ -503,20 +501,11 @@ void ntripServerRestart(int serverIndex)
503501
//----------------------------------------
504502
// Update the state of the NTRIP server state machine
505503
//----------------------------------------
506-
void ntripServerSetState(NTRIP_SERVER_DATA *ntripServer, uint8_t newState)
504+
void ntripServerSetState(int serverIndex, uint8_t newState)
507505
{
508-
int serverIndex;
509-
for (serverIndex = 0; serverIndex < NTRIP_SERVER_MAX; serverIndex++)
510-
{
511-
if (ntripServer == &ntripServerArray[serverIndex])
512-
serverIndex = serverIndex;
513-
}
514-
if (serverIndex >= NTRIP_SERVER_MAX)
515-
{
516-
systemPrintf("NTRIP Server: %p unknown NTRIP Server structure!\r\n", (void *)ntripServer);
517-
return;
518-
}
506+
NTRIP_SERVER_DATA * ntripServer;
519507

508+
ntripServer = &ntripServerArray[serverIndex];
520509
if (settings.debugNtripServerState)
521510
{
522511
if (ntripServer->state == newState)
@@ -556,8 +545,7 @@ void ntripServerStart(int serverIndex)
556545
// Start the NTRIP server
557546
systemPrintf("NTRIP Server %d start\r\n", serverIndex);
558547
ntripServerStop(serverIndex, false);
559-
if (ntripServerEnabled(serverIndex, nullptr))
560-
networkConsumerAdd(NETCONSUMER_NTRIP_SERVER_0 + serverIndex, NETWORK_ANY, __FILE__, __LINE__);
548+
networkConsumerAdd(NETWORK_CONSUMER(serverIndex), NETWORK_ANY, __FILE__, __LINE__);
561549
}
562550

563551
//----------------------------------------
@@ -582,13 +570,13 @@ void ntripServerStop(int serverIndex, bool shutdown)
582570
}
583571

584572
// Increase timeouts if we started the network
585-
if (ntripServer->state > NTRIP_SERVER_ON)
573+
if (ntripServer->state > NTRIP_SERVER_OFF)
586574
// Mark the Server stop so that we don't immediately attempt re-connect to Caster
587575
ntripServer->timer = millis();
588576

589577
// Determine the next NTRIP server state
590578
online.ntripServer[serverIndex] = false;
591-
networkConsumerOffline(NETCONSUMER_NTRIP_SERVER_0 + serverIndex);
579+
networkConsumerOffline(NETWORK_CONSUMER(serverIndex));
592580
if (shutdown)
593581
{
594582
if (settings.debugNtripServerState)
@@ -599,8 +587,8 @@ void ntripServerStop(int serverIndex, bool shutdown)
599587
systemPrintf("NTRIP Server %d caster port not configured!\r\n", serverIndex);
600588
if (settings.debugNtripServerState && (!settings.ntripServer_MountPoint[serverIndex][0]))
601589
systemPrintf("NTRIP Server %d mount point not configured!\r\n", serverIndex);
602-
networkConsumerRemove(NETCONSUMER_NTRIP_SERVER_0 + serverIndex, NETWORK_ANY, __FILE__, __LINE__);
603-
ntripServerSetState(ntripServer, NTRIP_SERVER_OFF);
590+
networkConsumerRemove(NETWORK_CONSUMER(serverIndex), NETWORK_ANY, __FILE__, __LINE__);
591+
ntripServerSetState(serverIndex, NTRIP_SERVER_OFF);
604592
ntripServer->connectionAttempts = 0;
605593
ntripServer->connectionAttemptTimeout = 0;
606594

@@ -617,7 +605,7 @@ void ntripServerStop(int serverIndex, bool shutdown)
617605
{
618606
if (settings.debugNtripServerState)
619607
systemPrintf("ntripServerStop server %d start requested\r\n", serverIndex);
620-
ntripServerSetState(ntripServer, NTRIP_SERVER_ON);
608+
ntripServerSetState(serverIndex, NTRIP_SERVER_WAIT_FOR_NETWORK);
621609
}
622610
}
623611

@@ -634,8 +622,9 @@ void ntripServerUpdate(int serverIndex)
634622
NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
635623

636624
// Shutdown the NTRIP server when the mode or setting changes
637-
DMW_ds(ntripServerSetState, ntripServer);
638-
connected = networkConsumerIsConnected(NETCONSUMER_NTRIP_SERVER_0 + serverIndex);
625+
DMW_if
626+
ntripServerSetState(serverIndex, ntripServer->state);
627+
connected = networkConsumerIsConnected(NETWORK_CONSUMER(serverIndex));
639628
enabled = ntripServerEnabled(serverIndex, &line);
640629
if (!enabled && (ntripServer->state > NTRIP_SERVER_OFF))
641630
ntripServerShutdown(serverIndex);
@@ -650,12 +639,11 @@ void ntripServerUpdate(int serverIndex)
650639
{
651640
case NTRIP_SERVER_OFF:
652641
if (enabled)
642+
{
643+
// Start the network
653644
ntripServerStart(serverIndex);
654-
break;
655-
656-
// Start the network
657-
case NTRIP_SERVER_ON:
658-
ntripServerSetState(ntripServer, NTRIP_SERVER_WAIT_FOR_NETWORK);
645+
ntripServerSetState(serverIndex, NTRIP_SERVER_WAIT_FOR_NETWORK);
646+
}
659647
break;
660648

661649
// Wait for a network media connection
@@ -676,15 +664,15 @@ void ntripServerUpdate(int serverIndex)
676664
reportHeapNow(settings.debugNtripServerState);
677665

678666
// Reset the timeout when the network changes
679-
if (networkChanged(NETCONSUMER_NTRIP_SERVER_0 + serverIndex))
667+
if (networkChanged(NETWORK_CONSUMER(serverIndex)))
680668
{
681669
ntripServer->connectionAttempts = 0;
682670
ntripServer->connectionAttemptTimeout = 0;
683671
}
684672

685673
// The network is available for the NTRIP server
686-
networkUserAdd(NETCONSUMER_NTRIP_SERVER_0 + serverIndex, __FILE__, __LINE__);
687-
ntripServerSetState(ntripServer, NTRIP_SERVER_NETWORK_CONNECTED);
674+
networkUserAdd(NETWORK_CONSUMER(serverIndex), __FILE__, __LINE__);
675+
ntripServerSetState(serverIndex, NTRIP_SERVER_NETWORK_CONNECTED);
688676
}
689677
}
690678
break;
@@ -703,7 +691,7 @@ void ntripServerUpdate(int serverIndex)
703691
rtcmPacketsSent = 0;
704692

705693
// Open socket to NTRIP caster
706-
ntripServerSetState(ntripServer, NTRIP_SERVER_WAIT_GNSS_DATA);
694+
ntripServerSetState(serverIndex, NTRIP_SERVER_WAIT_GNSS_DATA);
707695
}
708696
break;
709697

@@ -730,7 +718,7 @@ void ntripServerUpdate(int serverIndex)
730718
{
731719
// Connection open to NTRIP caster, wait for the authorization response
732720
ntripServer->timer = millis();
733-
ntripServerSetState(ntripServer, NTRIP_SERVER_AUTHORIZATION);
721+
ntripServerSetState(serverIndex, NTRIP_SERVER_AUTHORIZATION);
734722
}
735723
}
736724
break;
@@ -794,7 +782,7 @@ void ntripServerUpdate(int serverIndex)
794782
// We don't use a task because we use I2C hardware (and don't have a semaphore).
795783
online.ntripServer[serverIndex] = true;
796784
ntripServer->startTime = millis();
797-
ntripServerSetState(ntripServer, NTRIP_SERVER_CASTING);
785+
ntripServerSetState(serverIndex, NTRIP_SERVER_CASTING);
798786
}
799787

800788
// Look for '401 Unauthorized'
@@ -887,8 +875,6 @@ void ntripServerUpdate()
887875
//----------------------------------------
888876
void ntripServerValidateTables()
889877
{
890-
if (NETCONSUMER_OTA_CLIENT != (NETCONSUMER_NTRIP_SERVER_0 + NTRIP_SERVER_MAX))
891-
reportFatalError("Adjust NETCONSUMER_NTRIP_SERVER_* entries to match NTRIP_SERVER_MAX");
892878
if (ntripServerStateNameEntries != NTRIP_SERVER_STATE_MAX)
893879
reportFatalError("Fix ntripServerStateNameEntries to match NTRIPServerState");
894880
}

Firmware/RTK_Everywhere/RTK_Everywhere.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,6 @@ volatile bool deadManWalking;
912912
DMW_if systemPrintf("%s called\r\n", string); \
913913
}
914914
#define DMW_c(string) DMW_if systemPrintf("%s called\r\n", string);
915-
#define DMW_ds(routine, dataStructure) DMW_if routine(dataStructure, dataStructure->state);
916915
#define DMW_m(string) DMW_if systemPrintln(string);
917916
#define DMW_r(string) DMW_if systemPrintf("%s returning\r\n", string);
918917
#define DMW_rs(string, status) DMW_if systemPrintf("%s returning %d\r\n", string, (int32_t)status);

Firmware/RTK_Everywhere/settings.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,8 @@ enum
581581
NETCONSUMER_NTP_SERVER,
582582
NETCONSUMER_NTRIP_CLIENT,
583583
NETCONSUMER_NTRIP_SERVER,
584-
NETCONSUMER_NTRIP_SERVER_0 = NETCONSUMER_NTRIP_SERVER,
585-
NETCONSUMER_NTRIP_SERVER_1,
586-
NETCONSUMER_NTRIP_SERVER_2,
587-
NETCONSUMER_NTRIP_SERVER_3,
588-
NETCONSUMER_OTA_CLIENT,
584+
NETCONSUMER_NTRIP_SERVER_MAX = NETCONSUMER_NTRIP_SERVER + NTRIP_SERVER_MAX,
585+
NETCONSUMER_OTA_CLIENT = NETCONSUMER_NTRIP_SERVER_MAX,
589586
NETCONSUMER_PPL_KEY_UPDATE,
590587
NETCONSUMER_PPL_MQTT_CLIENT,
591588
NETCONSUMER_TCP_CLIENT,

0 commit comments

Comments
 (0)