From a9c8564524530668ec8348799042f451353d3139 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Tue, 30 Mar 2021 23:11:33 +0800 Subject: [PATCH] fix millisecond unsigned rollover errors found via portduino --- src/OSTimer.cpp | 2 +- src/gps/RTC.cpp | 2 +- src/mesh/PhoneAPI.cpp | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/OSTimer.cpp b/src/OSTimer.cpp index 0978163ce..9be4bd3db 100644 --- a/src/OSTimer.cpp +++ b/src/OSTimer.cpp @@ -40,7 +40,7 @@ bool scheduleHWCallback(PendableFunction callback, void *param1, uint32_t param2 tParam1 = param1; tParam2 = param2; - timerAlarmWrite(timer, delayMsec * 1000L, false); // Do not reload, we want it to be a single shot timer + timerAlarmWrite(timer, delayMsec * 1000UL, false); // Do not reload, we want it to be a single shot timer timerRestart(timer); timerAlarmEnable(timer); return true; diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index 87b73ce89..5f6a54c5c 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -39,7 +39,7 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv) currentQuality = q; shouldSet = true; DEBUG_MSG("Upgrading time to RTC %ld secs (quality %d)\n", tv->tv_sec, q); - } else if(q == RTCQualityGPS && (now - lastSetMsec) > (12 * 60 * 60 * 1000L)) { + } else if(q == RTCQualityGPS && (now - lastSetMsec) > (12 * 60 * 60 * 1000UL)) { // Every 12 hrs we will slam in a new GPS time, to correct for local RTC clock drift shouldSet = true; DEBUG_MSG("Reapplying external time to correct clock drift %ld secs\n", tv->tv_sec); diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index db039afb2..018ab6d3c 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -15,7 +15,10 @@ #error ToRadio is too big #endif -PhoneAPI::PhoneAPI() {} +PhoneAPI::PhoneAPI() +{ + lastContactMsec = millis(); +} PhoneAPI::~PhoneAPI() { @@ -53,9 +56,12 @@ void PhoneAPI::close() void PhoneAPI::checkConnectionTimeout() { if (isConnected()) { - bool newConnected = (millis() - lastContactMsec < getPref_phone_timeout_secs() * 1000L); - if (!newConnected) + uint32_t now = millis(); + bool newContact = (now - lastContactMsec) < getPref_phone_timeout_secs() * 1000UL; + if (!newContact) { + DEBUG_MSG("Timed out on phone contact, dropping phone connection\n"); close(); + } } }