Skip to content

Commit 30c5fbe

Browse files
committed
More NTRIP_SERVER_DATA mutex methods
1 parent 1845152 commit 30c5fbe

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

Firmware/RTK_Everywhere/NtripServer.ino

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -434,20 +434,15 @@ 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
{
448443
if (ntripServer->networkClient->write(incoming) == 1) // Send this byte to socket
449444
{
450-
ntripServer->updateAfterWrite();
445+
ntripServer->updateTimerAndBytesSent();
451446
netOutgoingRTCM = true;
452447
while (ntripServer->networkClient->available())
453448
ntripServer->networkClient->read(); // Absorb any unwanted incoming traffic
@@ -498,7 +493,7 @@ void ntripServerRestart(int serverIndex)
498493

499494
// Save the previous uptime value
500495
if (ntripServer->state == NTRIP_SERVER_CASTING)
501-
ntripServer->startTime = ntripServer->timer - ntripServer->startTime;
496+
ntripServer->startTime = ntripServer->getUptime();
502497
ntripServerConnectLimitReached(serverIndex);
503498
}
504499

@@ -576,7 +571,7 @@ void ntripServerStop(int serverIndex, bool shutdown)
576571
// Increase timeouts if we started the network
577572
if (ntripServer->state > NTRIP_SERVER_OFF)
578573
// Mark the Server stop so that we don't immediately attempt re-connect to Caster
579-
ntripServer->timer = millis();
574+
ntripServer->setTimerToMillis();
580575

581576
// Determine the next NTRIP server state
582577
online.ntripServer[serverIndex] = false;
@@ -707,7 +702,7 @@ void ntripServerUpdate(int serverIndex)
707702
// Initiate the connection to the NTRIP caster
708703
case NTRIP_SERVER_CONNECTING:
709704
// Delay before opening the NTRIP server connection
710-
if ((millis() - ntripServer->timer) >= ntripServer->connectionAttemptTimeout)
705+
if (ntripServer->checkConnectionAttemptTimeout())
711706
{
712707
// Attempt a connection to the NTRIP caster
713708
if (!ntripServerConnectCaster(serverIndex))
@@ -721,7 +716,7 @@ void ntripServerUpdate(int serverIndex)
721716
else
722717
{
723718
// Connection open to NTRIP caster, wait for the authorization response
724-
ntripServer->timer = millis();
719+
ntripServer->setTimerToMillis();
725720
ntripServerSetState(serverIndex, NTRIP_SERVER_AUTHORIZATION);
726721
}
727722
}
@@ -734,7 +729,7 @@ void ntripServerUpdate(int serverIndex)
734729
strlen("ICY 200 OK")) // Wait until at least a few bytes have arrived
735730
{
736731
// Check for response timeout
737-
if ((millis() - ntripServer->timer) > 10000)
732+
if (ntripServer->millisSinceTimer() > 10000)
738733
{
739734
if (ntripServerConnectLimitReached(serverIndex))
740735
systemPrintf(
@@ -781,7 +776,7 @@ void ntripServerUpdate(int serverIndex)
781776
settings.ntripServer_MountPoint[serverIndex]);
782777

783778
// Connection is now open, start the RTCM correction data timer
784-
ntripServer->timer = millis();
779+
ntripServer->setTimerToMillis();
785780

786781
// We don't use a task because we use I2C hardware (and don't have a semaphore).
787782
online.ntripServer[serverIndex] = true;
@@ -826,7 +821,7 @@ void ntripServerUpdate(int serverIndex)
826821
settings.ntripServer_CasterHost[serverIndex]);
827822
ntripServerRestart(serverIndex);
828823
}
829-
else if (ntripServer->millisSinceLastWrite() > (10 * 1000))
824+
else if (ntripServer->millisSinceTimer() > (10 * 1000))
830825
{
831826
// GNSS stopped sending RTCM correction data
832827
systemPrintf("NTRIP Server %d breaking connection to %s due to lack of RTCM data!\r\n", serverIndex,
@@ -841,7 +836,7 @@ void ntripServerUpdate(int serverIndex)
841836
// connection. However increasing backoff delays should be
842837
// added when the NTRIP caster fails after a short connection
843838
// interval.
844-
if ((ntripServer->millisSinceStart() > NTRIP_SERVER_CONNECTION_TIME) &&
839+
if ((ntripServer->millisSinceStartTime() > NTRIP_SERVER_CONNECTION_TIME) &&
845840
(ntripServer->connectionAttempts || ntripServer->connectionAttemptTimeout))
846841
{
847842
// After a long connection period, reset the attempt counter

Firmware/RTK_Everywhere/settings.h

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,11 @@ typedef struct
414414
volatile uint32_t rtcmBytesSent;
415415
volatile uint32_t previousMilliseconds;
416416

417+
418+
// Protect all methods that manipulate timer with a mutex - to avoid race conditions
417419
SemaphoreHandle_t serverSemaphore = NULL;
418420

419-
unsigned long millisSinceLastWrite()
421+
unsigned long millisSinceTimer()
420422
{
421423
unsigned long retVal = 0;
422424
if (serverSemaphore == NULL)
@@ -429,7 +431,7 @@ typedef struct
429431
return retVal;
430432
}
431433

432-
unsigned long millisSinceStart()
434+
unsigned long millisSinceStartTime()
433435
{
434436
unsigned long retVal = 0;
435437
if (serverSemaphore == NULL)
@@ -442,7 +444,7 @@ typedef struct
442444
return retVal;
443445
}
444446

445-
void updateAfterWrite()
447+
void updateTimerAndBytesSent()
446448
{
447449
if (serverSemaphore == NULL)
448450
serverSemaphore = xSemaphoreCreateMutex();
@@ -454,6 +456,63 @@ typedef struct
454456
xSemaphoreGive(serverSemaphore);
455457
}
456458
}
459+
460+
bool checkBytesSentAndReset(uint32_t timerLimit)
461+
{
462+
bool retVal = false;
463+
if (serverSemaphore == NULL)
464+
serverSemaphore = xSemaphoreCreateMutex();
465+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
466+
{
467+
if (((millis() - timer) > timerLimit) && (bytesSent > 0))
468+
{
469+
retVal = true;
470+
bytesSent = 0;
471+
}
472+
xSemaphoreGive(serverSemaphore);
473+
}
474+
return retVal;
475+
}
476+
477+
unsigned long getUptime()
478+
{
479+
unsigned long retVal = 0;
480+
if (serverSemaphore == NULL)
481+
serverSemaphore = xSemaphoreCreateMutex();
482+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
483+
{
484+
retVal = timer - startTime;
485+
xSemaphoreGive(serverSemaphore);
486+
}
487+
return retVal;
488+
}
489+
490+
void setTimerToMillis()
491+
{
492+
if (serverSemaphore == NULL)
493+
serverSemaphore = xSemaphoreCreateMutex();
494+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
495+
{
496+
timer = millis();
497+
xSemaphoreGive(serverSemaphore);
498+
}
499+
}
500+
501+
bool checkConnectionAttemptTimeout()
502+
{
503+
bool retVal = false;
504+
if (serverSemaphore == NULL)
505+
serverSemaphore = xSemaphoreCreateMutex();
506+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
507+
{
508+
if ((millis() - timer) >= connectionAttemptTimeout)
509+
{
510+
retVal = true;
511+
}
512+
xSemaphoreGive(serverSemaphore);
513+
}
514+
return retVal;
515+
}
457516
} NTRIP_SERVER_DATA;
458517

459518
#endif // COMPILE_NETWORK

0 commit comments

Comments
 (0)