Allow setting RTC clock backwards and fix elapsed-time underflow

Remove the "clock cannot go backwards" restriction from all set-time
paths (CLI `time`, `clock sync`, companion radio CMD_SET_DEVICE_TIME,
and simple_secure_chat). The ESP32-S3 RTC drifts 5-10% during deep
sleep, making backwards correction necessary after even a few days.

Add safeElapsedSecs() helper in ArduinoHelpers.h that clamps elapsed
time to 0 when a stored timestamp appears to be in the future after a
clock correction. Applied to:
- Neighbor "heard X ago" displays in simple_repeater
- UI time displays in companion_radio
- TimeSeriesData calculations in simple_sensor

Switch BaseChatMesh connection expiry from RTC timestamps to monotonic
millis(), making it immune to RTC adjustments from GPS, NTP, or manual
sync. Rename last_activity to last_activity_ms to reflect the change.
This commit is contained in:
Wessel Nieboer 2026-02-06 03:31:34 +01:00 committed by Wessel Nieboer
parent fb726e48c2
commit 5f36fde054
No known key found for this signature in database
GPG key ID: 27BB1C3D63DEEFFF
9 changed files with 39 additions and 46 deletions

View file

@ -1,4 +1,5 @@
#include "TimeSeriesData.h"
#include <helpers/ArduinoHelpers.h>
void TimeSeriesData::recordData(mesh::RTCClock* clock, float value) {
uint32_t now = clock->getCurrentTime();
@ -12,7 +13,7 @@ void TimeSeriesData::recordData(mesh::RTCClock* clock, float value) {
void TimeSeriesData::calcMinMaxAvg(mesh::RTCClock* clock, uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg* dest, uint8_t channel, uint8_t lpp_type) const {
int i = next, n = num_slots;
uint32_t ago = clock->getCurrentTime() - last_timestamp;
uint32_t ago = safeElapsedSecs(clock->getCurrentTime(), last_timestamp);
int num_values = 0;
float total = 0.0f;