Skip to content

Commit ebe96f9

Browse files
committed
Make millis timeouts wrapsafe - resolves #742
1 parent db12e22 commit ebe96f9

21 files changed

+75
-62
lines changed

Firmware/RTK_Everywhere/Begin.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,10 +1955,10 @@ void tpISR()
19551955
{
19561956
if (timTpUpdated) // Only sync if timTpUpdated is true - set by storeTIMTPdata on ZED platforms only
19571957
{
1958-
if (millisNow - lastRTCSync >
1958+
if ((millisNow - lastRTCSync) >
19591959
syncRTCInterval) // Only sync if it is more than syncRTCInterval since the last sync
19601960
{
1961-
if (millisNow < (timTpArrivalMillis + 999)) // Only sync if the GNSS time is not stale
1961+
if ((millisNow - timTpArrivalMillis) < 999) // Only sync if the GNSS time is not stale
19621962
{
19631963
if (gnss->isFullyResolved()) // Only sync if GNSS time is fully resolved
19641964
{

Firmware/RTK_Everywhere/Bluetooth.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void bluetoothUpdate()
5959
{
6060
#ifdef COMPILE_BT
6161
static uint32_t lastCheck = millis(); // Check if connected every 100ms
62-
if (millis() > (lastCheck + 100))
62+
if ((millis() - lastCheck) > 100)
6363
{
6464
lastCheck = millis();
6565

Firmware/RTK_Everywhere/Display.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void displayUpdate()
212212
if (online.display == true)
213213
{
214214
static unsigned long lastDisplayUpdate = 0;
215-
if (millis() - lastDisplayUpdate > 500 || forceDisplayUpdate == true) // Update display at 2Hz
215+
if (((millis() - lastDisplayUpdate) > 500) || (forceDisplayUpdate == true)) // Update display at 2Hz
216216
{
217217
lastDisplayUpdate = millis();
218218
forceDisplayUpdate = false;
@@ -2493,7 +2493,7 @@ void paintSystemTest()
24932493
if (online.display == true)
24942494
{
24952495
// Toggle between two displays
2496-
if (millis() - systemTestDisplayTime > 3000)
2496+
if ((millis() - systemTestDisplayTime) > 3000)
24972497
{
24982498
systemTestDisplayTime = millis();
24992499
systemTestDisplayNumber++;
@@ -3206,7 +3206,7 @@ void displayWebConfig(std::vector<iconPropertyBlinking> &iconPropertyList)
32063206

32073207
// Toggle display back and forth for long SSIDs and IPs
32083208
// Run the timer no matter what, but load firstHalf/lastHalf with the same thing if strlen < maxWidth
3209-
if (millis() - ssidDisplayTimer > 2000)
3209+
if ((millis() - ssidDisplayTimer) > 2000)
32103210
{
32113211
ssidDisplayTimer = millis();
32123212
ssidDisplayFirstHalf = !ssidDisplayFirstHalf;

Firmware/RTK_Everywhere/ESPNOW.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ void espNowUpdate()
638638

639639
// If it's been longer than 50ms since we last added a byte to the buffer
640640
// then we've reached the end of the RTCM stream. Send partial buffer.
641-
if (espNowOutgoingSpot > 0 && (millis() - espNowLastAdd) > 50)
641+
if ((espNowOutgoingSpot > 0) && ((millis() - espNowLastAdd) > 50))
642642
{
643643
if (espNowState == ESPNOW_PAIRED)
644644
esp_now_send(0, (uint8_t *)&espNowOutgoing, espNowOutgoingSpot); // Send partial packet to all peers
@@ -654,7 +654,7 @@ void espNowUpdate()
654654

655655
// If we don't receive an ESP NOW packet after some time, set RSSI to very negative
656656
// This removes the ESPNOW icon from the display when the link goes down
657-
if (millis() - espNowLastRssiUpdate > 5000 && espNowRSSI > -255)
657+
if (((millis() - espNowLastRssiUpdate) > 5000) && (espNowRSSI > -255))
658658
espNowRSSI = -255;
659659

660660
// The display menu, serial menu, or CLI may request pairing be started
@@ -682,7 +682,7 @@ void espNowUpdate()
682682
randomSeed(millis());
683683
static unsigned long lastMacSend = millis();
684684
static int timeout = 1000 + random(0, 100); // Pick a random number between 1000 to 1100ms
685-
if (millis() - lastMacSend > timeout)
685+
if ((millis() - lastMacSend) > timeout)
686686
{
687687
lastMacSend = millis();
688688
espNowSendPairMessage(

Firmware/RTK_Everywhere/GNSS.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static void pushGPGGA(char *ggaData)
106106
// Provide the caster with our current position as needed
107107
if (ntripClient->connected() && settings.ntripClient_TransmitGGA == true)
108108
{
109-
if (millis() - lastGGAPush > NTRIPCLIENT_MS_BETWEEN_GGA)
109+
if ((millis() - lastGGAPush) > NTRIPCLIENT_MS_BETWEEN_GGA)
110110
{
111111
lastGGAPush = millis();
112112

@@ -359,7 +359,7 @@ void gnssFirmwareBeginUpdate()
359359
// Button task will gnssFirmwareRemoveUpdate and restart
360360

361361
// Temporary fix for buttonless Flex. TODO - remove
362-
if ((productVariant == RTK_FLEX) && (millis() > (lastSerial + 30000)))
362+
if ((productVariant == RTK_FLEX) && ((millis() - lastSerial) > 30000))
363363
{
364364
// Beep to indicate exit
365365
beepOn();

Firmware/RTK_Everywhere/GNSS_LG290P.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ bool GNSS_LG290P::enterConfigMode(unsigned long waitForSemaphoreTimeout_millis)
598598
do
599599
{ // Wait for up to waitForSemaphoreTimeout for library to stop blocking
600600
isBlocking = _lg290p->isBlocking();
601-
} while (isBlocking && (millis() < (start + waitForSemaphoreTimeout_millis)));
601+
} while (isBlocking && ((millis() - start) < waitForSemaphoreTimeout_millis));
602602

603603
// This will fail if the library is still blocking, but it is worth a punt...
604604
return (_lg290p->sendOkCommand("$PQTMCFGPROT",

Firmware/RTK_Everywhere/GNSS_Mosaic.ino

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ bool GNSS_MOSAIC::sendAndWaitForIdle(HardwareSerial *serialPort, const char *mes
21212121
unsigned long startTime = millis();
21222122
size_t replySeen = 0;
21232123

2124-
while ((millis() < (startTime + timeout)) && (replySeen < strlen(reply))) // While not timed out and reply not seen
2124+
while (((millis() - startTime) < timeout) && (replySeen < strlen(reply))) // While not timed out and reply not seen
21252125
{
21262126
if (serialPort->available()) // If a char is available
21272127
{
@@ -2145,7 +2145,7 @@ bool GNSS_MOSAIC::sendAndWaitForIdle(HardwareSerial *serialPort, const char *mes
21452145
if (replySeen == strlen(reply)) // If the reply was seen
21462146
{
21472147
startTime = millis();
2148-
while (millis() < (startTime + idle))
2148+
while ((millis() - startTime) < idle)
21492149
{
21502150
if (serialPort->available())
21512151
{
@@ -2256,18 +2256,18 @@ bool GNSS_MOSAIC::sendWithResponse(HardwareSerial *serialPort, const char *messa
22562256
}
22572257

22582258
// If the reply has started to arrive at the timeout, allow extra time
2259-
if (millis() > (startTime + timeout)) // Have we timed out?
2259+
if ((millis() - startTime) > timeout) // Have we timed out?
22602260
if (replySeen == 0) // If replySeen is zero, don't keepGoing
22612261
keepGoing = false;
22622262

2263-
if (millis() > (startTime + timeout + wait)) // Have we really timed out?
2263+
if ((millis() - startTime) > (timeout + wait)) // Have we really timed out?
22642264
keepGoing = false; // Don't keepGoing
22652265
}
22662266

22672267
if (replySeen == strlen(reply)) // If the reply was seen
22682268
{
22692269
startTime = millis();
2270-
while (millis() < (startTime + wait))
2270+
while ((millis() - startTime) < wait)
22712271
{
22722272
if (serialPort->available())
22732273
{
@@ -2795,7 +2795,7 @@ void GNSS_MOSAIC::update()
27952795
const unsigned long sdCardSizeCheckInterval = 5000; // Matches the interval in logUpdate
27962796
static unsigned long sdCardLastFreeChange = millis(); // X5 is slow to update free. Seems to be about every ~20s?
27972797
static uint64_t previousFreeSpace = 0;
2798-
if (millis() > (sdCardSizeLastCheck + sdCardSizeCheckInterval))
2798+
if ((millis() - sdCardSizeLastCheck) > sdCardSizeCheckInterval)
27992799
{
28002800
updateSD(); // Check if the card has been removed / inserted
28012801

@@ -2817,7 +2817,7 @@ void GNSS_MOSAIC::update()
28172817
// The free space has not changed
28182818
// X5 is slow to update free. Seems to be about every ~20s?
28192819
// So only set logIncreasing to false after 30s
2820-
if (millis() > (sdCardLastFreeChange + 30000))
2820+
if ((millis() - sdCardLastFreeChange) > 30000)
28212821
logIncreasing = false;
28222822
}
28232823
else // if (sdFreeSpace > previousFreeSpace)
@@ -2839,7 +2839,7 @@ void GNSS_MOSAIC::update()
28392839

28402840
// Update spartnCorrectionsReceived
28412841
// Does this need if(online.lband_gnss) ? Not sure... TODO
2842-
if (millis() > (lastSpartnReception + (settings.correctionsSourcesLifetime_s * 1000))) // Timeout
2842+
if ((millis() - lastSpartnReception) > (settings.correctionsSourcesLifetime_s * 1000)) // Timeout
28432843
{
28442844
if (spartnCorrectionsReceived) // If corrections were being received
28452845
{
@@ -2920,7 +2920,7 @@ void GNSS_MOSAIC::waitSBFReceiverSetup(HardwareSerial *serialPort, unsigned long
29202920
reportFatalError("Failed to initialize the SBF parser");
29212921

29222922
unsigned long startTime = millis();
2923-
while ((millis() < (startTime + timeout)) && (_receiverSetupSeen == false))
2923+
while (((millis() - startTime) < timeout) && (_receiverSetupSeen == false))
29242924
{
29252925
if (serialPort->available())
29262926
{
@@ -3223,7 +3223,7 @@ void mosaicX5flushRX(unsigned long timeout)
32233223
if (timeout > 0)
32243224
{
32253225
unsigned long startTime = millis();
3226-
while (millis() < (startTime + timeout))
3226+
while ((millis() - startTime) < timeout)
32273227
{
32283228
if (serial2GNSS->available())
32293229
{
@@ -3247,7 +3247,7 @@ void mosaicX5flushRX(unsigned long timeout)
32473247
bool mosaicX5waitCR(unsigned long timeout)
32483248
{
32493249
unsigned long startTime = millis();
3250-
while (millis() < (startTime + timeout))
3250+
while ((millis() - startTime) < timeout)
32513251
{
32523252
if (serial2GNSS->available())
32533253
{

Firmware/RTK_Everywhere/GNSS_ZED.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ float GNSS_ZED::getSurveyInMeanAccuracy()
13751375

13761376
// Use a local static so we don't have to request these values multiple times (ZED takes many ms to respond
13771377
// to this command)
1378-
if (millis() - lastCheck > 1000)
1378+
if ((millis() - lastCheck) > 1000)
13791379
{
13801380
lastCheck = millis();
13811381
svinMeanAccuracy = _zed->getSurveyInMeanAccuracy(50);
@@ -1396,7 +1396,7 @@ int GNSS_ZED::getSurveyInObservationTime()
13961396

13971397
// Use a local static so we don't have to request these values multiple times (ZED takes many ms to respond
13981398
// to this command)
1399-
if (millis() - lastCheck > 1000)
1399+
if ((millis() - lastCheck) > 1000)
14001400
{
14011401
lastCheck = millis();
14021402
svinObservationTime = _zed->getSurveyInObservationTime(50);
@@ -2668,7 +2668,7 @@ bool GNSS_ZED::surveyInReset()
26682668
while (_zed->getSurveyInActive(100) || _zed->getSurveyInValid(100))
26692669
{
26702670
delay(100);
2671-
if (millis() - startTime > maxTime)
2671+
if ((millis() - startTime) > maxTime)
26722672
return (false); // Reset of survey failed
26732673
}
26742674

@@ -2732,7 +2732,7 @@ bool GNSS_ZED::surveyInStart()
27322732
while (_zed->getSurveyInActive(100) == false)
27332733
{
27342734
delay(100);
2735-
if (millis() - startTime > maxTime)
2735+
if ((millis() - startTime) > maxTime)
27362736
return (false); // Reset of survey failed
27372737
}
27382738

Firmware/RTK_Everywhere/LoRa.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void updateLora()
160160
if (settings.debugLora == true)
161161
{
162162
static unsigned long lastReport = 0;
163-
if (millis() - lastReport > 3000)
163+
if ((millis() - lastReport) > 3000)
164164
{
165165
lastReport = millis();
166166
systemPrintf("LoRa transmitted %d RTCM bytes\r\n", loraBytesSent);
@@ -233,7 +233,7 @@ void updateLora()
233233
break;
234234

235235
case (LORA_RX_SHARED):
236-
if ((millis() - loraLastIncomingSerial) / 1000 > settings.loraSerialInteractionTimeout_s)
236+
if (((millis() - loraLastIncomingSerial) / 1000) > settings.loraSerialInteractionTimeout_s)
237237
{
238238
systemPrintln("LoRa shared port timeout expired. Moving to dedicated LoRa receive with no USB output.");
239239
systemFlush(); // Complete prints
@@ -570,7 +570,7 @@ void beginLoraFirmwareUpdate()
570570
// Button task will removeUpdateLoraFirmware and restart
571571

572572
// Temporary fix for buttonless Flex. TODO - remove
573-
if ((productVariant == RTK_FLEX) && (millis() > (lastSerial + 30000)))
573+
if ((productVariant == RTK_FLEX) && ((millis() - lastSerial) > 30000))
574574
{
575575
// Beep to indicate exit
576576
beepOn();

Firmware/RTK_Everywhere/MQTT_Client.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ void mqttClientUpdate()
10251025
mqttClient->poll();
10261026

10271027
// Determine if a data timeout has occurred
1028-
if (millis() - mqttClientLastDataReceived >= MQTT_CLIENT_DATA_TIMEOUT)
1028+
if ((millis() - mqttClientLastDataReceived) >= MQTT_CLIENT_DATA_TIMEOUT)
10291029
{
10301030
systemPrintln("MQTT client data timeout. Disconnecting...");
10311031
mqttClientRestart();

0 commit comments

Comments
 (0)