@@ -173,7 +173,7 @@ const RtkMode_t ntripServerMode = RTK_MODE_BASE_FIXED;
173173// ----------------------------------------
174174
175175// NTRIP Servers
176- volatile static NTRIP_SERVER_DATA ntripServerArray[NTRIP_SERVER_MAX];
176+ static NTRIP_SERVER_DATA ntripServerArray[NTRIP_SERVER_MAX];
177177
178178// ----------------------------------------
179179// NTRIP Server Routines
@@ -184,7 +184,7 @@ volatile static NTRIP_SERVER_DATA ntripServerArray[NTRIP_SERVER_MAX];
184184// ----------------------------------------
185185bool ntripServerConnectCaster (int serverIndex)
186186{
187- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
187+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
188188 const int SERVER_BUFFER_SIZE = 512 ;
189189 char serverBuffer[SERVER_BUFFER_SIZE];
190190
@@ -240,7 +240,7 @@ bool ntripServerConnectLimitReached(int serverIndex)
240240{
241241 bool limitReached;
242242 int minutes;
243- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
243+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
244244 int seconds;
245245
246246 // Retry the connection a few times
@@ -312,9 +312,9 @@ bool ntripServerEnabled(int serverIndex, const char ** line)
312312 {
313313 if (line)
314314 {
315- if (settings.ntripServer_CasterHost [0 ] == 0 )
315+ if (settings.ntripServer_CasterHost [serverIndex][ 0 ] == 0 )
316316 *line = " , Caster host not specified!" ;
317- else if (settings.ntripServer_CasterPort == 0 )
317+ else if (settings.ntripServer_CasterPort [serverIndex] == 0 )
318318 *line = " , Caster port not specified!" ;
319319 else
320320 *line = " , Mount point not specified!" ;
@@ -335,7 +335,7 @@ bool ntripServerEnabled(int serverIndex, const char ** line)
335335// ----------------------------------------
336336void ntripServerPrintStateSummary (int serverIndex)
337337{
338- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
338+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
339339
340340 switch (ntripServer->state )
341341 {
@@ -363,7 +363,7 @@ void ntripServerPrintStateSummary(int serverIndex)
363363// ----------------------------------------
364364void ntripServerPrintStatus (int serverIndex)
365365{
366- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
366+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
367367 uint64_t milliseconds;
368368 uint32_t days;
369369 byte hours;
@@ -380,7 +380,7 @@ void ntripServerPrintStatus(int serverIndex)
380380 if (ntripServer->state == NTRIP_SERVER_CASTING)
381381 // Use ntripServer->timer since it gets reset after each successful data
382382 // reception from the NTRIP caster
383- milliseconds = ntripServer->timer - ntripServer-> startTime ;
383+ milliseconds = ntripServer->getUptime () ;
384384 else
385385 {
386386 milliseconds = ntripServer->startTime ;
@@ -411,7 +411,7 @@ void ntripServerPrintStatus(int serverIndex)
411411// ----------------------------------------
412412void ntripServerProcessRTCM (int serverIndex, uint8_t incoming)
413413{
414- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
414+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
415415
416416 if (ntripServer->state == NTRIP_SERVER_CASTING)
417417 {
@@ -434,22 +434,27 @@ void ntripServerProcessRTCM(int serverIndex, uint8_t incoming)
434434 }
435435
436436 // If we have not gotten new RTCM bytes for a period of time, assume end of frame
437- if (((millis () - ntripServer->timer ) > 100 ) && (ntripServer->bytesSent > 0 ))
438- {
439- if ((!inMainMenu) && settings.debugNtripServerRtcm )
440- systemPrintf (" NTRIP Server %d transmitted %d RTCM bytes to Caster\r\n " , serverIndex,
441- ntripServer->bytesSent );
442-
443- ntripServer->bytesSent = 0 ;
444- }
437+ if (ntripServer->checkBytesSentAndReset (100 ) && (!inMainMenu) && settings.debugNtripServerRtcm )
438+ systemPrintf (" NTRIP Server %d transmitted %d RTCM bytes to Caster\r\n " , serverIndex,
439+ ntripServer->bytesSent );
445440
446441 if (ntripServer->networkClient && ntripServer->networkClient ->connected ())
447442 {
448- ntripServer->networkClient ->write (incoming); // Send this byte to socket
449- ntripServer->bytesSent = ntripServer->bytesSent + 1 ;
450- ntripServer->rtcmBytesSent = ntripServer->rtcmBytesSent + 1 ;
451- ntripServer->timer = millis ();
452- netOutgoingRTCM = true ;
443+ if (ntripServer->networkClient ->write (incoming) == 1 ) // Send this byte to socket
444+ {
445+ ntripServer->updateTimerAndBytesSent ();
446+ netOutgoingRTCM = true ;
447+ while (ntripServer->networkClient ->available ())
448+ ntripServer->networkClient ->read (); // Absorb any unwanted incoming traffic
449+ }
450+ // Failed to write the data
451+ else
452+ {
453+ // Done with this client connection
454+ if (settings.debugNtripServerRtcm && (!inMainMenu))
455+ systemPrintf (" NTRIP Server %d broken connection to %s\r\n " , serverIndex,
456+ settings.ntripServer_CasterHost [serverIndex]);
457+ }
453458 }
454459 }
455460
@@ -465,7 +470,7 @@ void ntripServerProcessRTCM(int serverIndex, uint8_t incoming)
465470// ----------------------------------------
466471void ntripServerResponse (int serverIndex, char *response, size_t maxLength)
467472{
468- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
473+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
469474 char *responseEnd;
470475
471476 // Make sure that we can zero terminate the response
@@ -484,11 +489,11 @@ void ntripServerResponse(int serverIndex, char *response, size_t maxLength)
484489// ----------------------------------------
485490void ntripServerRestart (int serverIndex)
486491{
487- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
492+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
488493
489494 // Save the previous uptime value
490495 if (ntripServer->state == NTRIP_SERVER_CASTING)
491- ntripServer->startTime = ntripServer->timer - ntripServer-> startTime ;
496+ ntripServer->startTime = ntripServer->getUptime () ;
492497 ntripServerConnectLimitReached (serverIndex);
493498}
494499
@@ -497,7 +502,7 @@ void ntripServerRestart(int serverIndex)
497502// ----------------------------------------
498503void ntripServerSetState (int serverIndex, uint8_t newState)
499504{
500- volatile NTRIP_SERVER_DATA * ntripServer;
505+ NTRIP_SERVER_DATA * ntripServer;
501506
502507 ntripServer = &ntripServerArray[serverIndex];
503508 if (settings.debugNtripServerState )
@@ -549,7 +554,7 @@ void ntripServerStop(int serverIndex, bool shutdown)
549554{
550555 bool enabled;
551556 int index;
552- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
557+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
553558
554559 if (ntripServer->networkClient )
555560 {
@@ -566,7 +571,7 @@ void ntripServerStop(int serverIndex, bool shutdown)
566571 // Increase timeouts if we started the network
567572 if (ntripServer->state > NTRIP_SERVER_OFF)
568573 // Mark the Server stop so that we don't immediately attempt re-connect to Caster
569- ntripServer->timer = millis ();
574+ ntripServer->setTimerToMillis ();
570575
571576 // Determine the next NTRIP server state
572577 online.ntripServer [serverIndex] = false ;
@@ -613,7 +618,7 @@ void ntripServerUpdate(int serverIndex)
613618 const char * line = " " ;
614619
615620 // Get the NTRIP data structure
616- volatile NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
621+ NTRIP_SERVER_DATA *ntripServer = &ntripServerArray[serverIndex];
617622
618623 // Shutdown the NTRIP server when the mode or setting changes
619624 DMW_if
@@ -678,8 +683,7 @@ void ntripServerUpdate(int serverIndex)
678683 // Failed to connect to to the network, attempt to restart the network
679684 ntripServerRestart (serverIndex);
680685
681- else if (settings.enableNtripServer &&
682- ((millis () - ntripServer->lastConnectionAttempt ) > ntripServer->connectionAttemptTimeout ))
686+ else if (settings.enableNtripServer )
683687 {
684688 // No RTCM correction data sent yet
685689 rtcmPacketsSent = 0 ;
@@ -697,7 +701,7 @@ void ntripServerUpdate(int serverIndex)
697701 // Initiate the connection to the NTRIP caster
698702 case NTRIP_SERVER_CONNECTING:
699703 // Delay before opening the NTRIP server connection
700- if (( millis () - ntripServer->timer ) >= ntripServer-> connectionAttemptTimeout )
704+ if (ntripServer->checkConnectionAttemptTimeout () )
701705 {
702706 // Attempt a connection to the NTRIP caster
703707 if (!ntripServerConnectCaster (serverIndex))
@@ -711,7 +715,7 @@ void ntripServerUpdate(int serverIndex)
711715 else
712716 {
713717 // Connection open to NTRIP caster, wait for the authorization response
714- ntripServer->timer = millis ();
718+ ntripServer->setTimerToMillis ();
715719 ntripServerSetState (serverIndex, NTRIP_SERVER_AUTHORIZATION);
716720 }
717721 }
@@ -724,7 +728,7 @@ void ntripServerUpdate(int serverIndex)
724728 strlen (" ICY 200 OK" )) // Wait until at least a few bytes have arrived
725729 {
726730 // Check for response timeout
727- if (( millis () - ntripServer->timer ) > 10000 )
731+ if (ntripServer->millisSinceTimer ( ) > 10000 )
728732 {
729733 if (ntripServerConnectLimitReached (serverIndex))
730734 systemPrintf (
@@ -771,7 +775,7 @@ void ntripServerUpdate(int serverIndex)
771775 settings.ntripServer_MountPoint [serverIndex]);
772776
773777 // Connection is now open, start the RTCM correction data timer
774- ntripServer->timer = millis ();
778+ ntripServer->setTimerToMillis ();
775779
776780 // We don't use a task because we use I2C hardware (and don't have a semaphore).
777781 online.ntripServer [serverIndex] = true ;
@@ -816,7 +820,7 @@ void ntripServerUpdate(int serverIndex)
816820 settings.ntripServer_CasterHost [serverIndex]);
817821 ntripServerRestart (serverIndex);
818822 }
819- else if (( millis () - ntripServer->timer ) > (10 * 1000 ))
823+ else if (ntripServer->millisSinceTimer ( ) > (10 * 1000 ))
820824 {
821825 // GNSS stopped sending RTCM correction data
822826 systemPrintf (" NTRIP Server %d breaking connection to %s due to lack of RTCM data!\r\n " , serverIndex,
@@ -831,7 +835,7 @@ void ntripServerUpdate(int serverIndex)
831835 // connection. However increasing backoff delays should be
832836 // added when the NTRIP caster fails after a short connection
833837 // interval.
834- if ((( millis () - ntripServer->startTime ) > NTRIP_SERVER_CONNECTION_TIME) &&
838+ if ((ntripServer->millisSinceStartTime ( ) > NTRIP_SERVER_CONNECTION_TIME) &&
835839 (ntripServer->connectionAttempts || ntripServer->connectionAttemptTimeout ))
836840 {
837841 // After a long connection period, reset the attempt counter
0 commit comments