Skip to content

Commit 1845152

Browse files
committed
Add NTRIP_SERVER_DATA semaphore and mutex methods
1 parent 7772ba5 commit 1845152

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

Firmware/RTK_Everywhere/NtripServer.ino

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,7 @@ void ntripServerProcessRTCM(int serverIndex, uint8_t incoming)
447447
{
448448
if (ntripServer->networkClient->write(incoming) == 1) // Send this byte to socket
449449
{
450-
ntripServer->bytesSent = ntripServer->bytesSent + 1;
451-
ntripServer->rtcmBytesSent = ntripServer->rtcmBytesSent + 1;
452-
ntripServer->timer = millis();
450+
ntripServer->updateAfterWrite();
453451
netOutgoingRTCM = true;
454452
while (ntripServer->networkClient->available())
455453
ntripServer->networkClient->read(); // Absorb any unwanted incoming traffic
@@ -828,7 +826,7 @@ void ntripServerUpdate(int serverIndex)
828826
settings.ntripServer_CasterHost[serverIndex]);
829827
ntripServerRestart(serverIndex);
830828
}
831-
else if ((millis() - ntripServer->timer) > (10 * 1000))
829+
else if (ntripServer->millisSinceLastWrite() > (10 * 1000))
832830
{
833831
// GNSS stopped sending RTCM correction data
834832
systemPrintf("NTRIP Server %d breaking connection to %s due to lack of RTCM data!\r\n", serverIndex,
@@ -843,7 +841,7 @@ void ntripServerUpdate(int serverIndex)
843841
// connection. However increasing backoff delays should be
844842
// added when the NTRIP caster fails after a short connection
845843
// interval.
846-
if (((millis() - ntripServer->startTime) > NTRIP_SERVER_CONNECTION_TIME) &&
844+
if ((ntripServer->millisSinceStart() > NTRIP_SERVER_CONNECTION_TIME) &&
847845
(ntripServer->connectionAttempts || ntripServer->connectionAttemptTimeout))
848846
{
849847
// After a long connection period, reset the attempt counter

Firmware/RTK_Everywhere/settings.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,47 @@ typedef struct
413413
// Better debug printing by ntripServerProcessRTCM
414414
volatile uint32_t rtcmBytesSent;
415415
volatile uint32_t previousMilliseconds;
416+
417+
SemaphoreHandle_t serverSemaphore = NULL;
418+
419+
unsigned long millisSinceLastWrite()
420+
{
421+
unsigned long retVal = 0;
422+
if (serverSemaphore == NULL)
423+
serverSemaphore = xSemaphoreCreateMutex();
424+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
425+
{
426+
retVal = millis() - timer;
427+
xSemaphoreGive(serverSemaphore);
428+
}
429+
return retVal;
430+
}
431+
432+
unsigned long millisSinceStart()
433+
{
434+
unsigned long retVal = 0;
435+
if (serverSemaphore == NULL)
436+
serverSemaphore = xSemaphoreCreateMutex();
437+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
438+
{
439+
retVal = millis() - startTime;
440+
xSemaphoreGive(serverSemaphore);
441+
}
442+
return retVal;
443+
}
444+
445+
void updateAfterWrite()
446+
{
447+
if (serverSemaphore == NULL)
448+
serverSemaphore = xSemaphoreCreateMutex();
449+
if (xSemaphoreTake(serverSemaphore, 10 / portTICK_PERIOD_MS) == pdPASS)
450+
{
451+
bytesSent = bytesSent + 1;
452+
rtcmBytesSent = rtcmBytesSent + 1;
453+
timer = millis();
454+
xSemaphoreGive(serverSemaphore);
455+
}
456+
}
416457
} NTRIP_SERVER_DATA;
417458

418459
#endif // COMPILE_NETWORK

0 commit comments

Comments
 (0)