mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
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:
parent
fb726e48c2
commit
5f36fde054
9 changed files with 39 additions and 46 deletions
|
|
@ -1193,13 +1193,8 @@ void MyMesh::handleCmdFrame(size_t len) {
|
|||
} else if (cmd_frame[0] == CMD_SET_DEVICE_TIME && len >= 5) {
|
||||
uint32_t secs;
|
||||
memcpy(&secs, &cmd_frame[1], 4);
|
||||
uint32_t curr = getRTCClock()->getCurrentTime();
|
||||
if (secs >= curr) {
|
||||
getRTCClock()->setCurrentTime(secs);
|
||||
writeOKFrame();
|
||||
} else {
|
||||
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
|
||||
}
|
||||
getRTCClock()->setCurrentTime(secs);
|
||||
writeOKFrame();
|
||||
} else if (cmd_frame[0] == CMD_SEND_SELF_ADVERT) {
|
||||
mesh::Packet* pkt;
|
||||
if (_prefs.advert_loc_policy == ADVERT_LOC_NONE) {
|
||||
|
|
|
|||
|
|
@ -233,7 +233,7 @@ public:
|
|||
for (int i = 0; i < UI_RECENT_LIST_SIZE; i++, y += 11) {
|
||||
auto a = &recent[i];
|
||||
if (a->name[0] == 0) continue; // empty slot
|
||||
int secs = _rtc->getCurrentTime() - a->recv_timestamp;
|
||||
uint32_t secs = safeElapsedSecs(_rtc->getCurrentTime(), a->recv_timestamp);
|
||||
if (secs < 60) {
|
||||
sprintf(tmp, "%ds", secs);
|
||||
} else if (secs < 60*60) {
|
||||
|
|
@ -496,7 +496,7 @@ public:
|
|||
|
||||
auto p = &unread[head];
|
||||
|
||||
int secs = _rtc->getCurrentTime() - p->timestamp;
|
||||
uint32_t secs = safeElapsedSecs(_rtc->getCurrentTime(), p->timestamp);
|
||||
if (secs < 60) {
|
||||
sprintf(tmp, "%ds", secs);
|
||||
} else if (secs < 60*60) {
|
||||
|
|
|
|||
|
|
@ -355,7 +355,7 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
|
|||
#if MAX_NEIGHBOURS
|
||||
// add next neighbour to results
|
||||
auto neighbour = sorted_neighbours[index + offset];
|
||||
uint32_t heard_seconds_ago = getRTCClock()->getCurrentTime() - neighbour->heard_timestamp;
|
||||
uint32_t heard_seconds_ago = safeElapsedSecs(getRTCClock()->getCurrentTime(), neighbour->heard_timestamp);
|
||||
memcpy(&results_buffer[results_offset], neighbour->id.pub_key, pubkey_prefix_length); results_offset += pubkey_prefix_length;
|
||||
memcpy(&results_buffer[results_offset], &heard_seconds_ago, 4); results_offset += 4;
|
||||
memcpy(&results_buffer[results_offset], &neighbour->snr, 1); results_offset += 1;
|
||||
|
|
@ -1042,7 +1042,7 @@ void MyMesh::formatNeighborsReply(char *reply) {
|
|||
mesh::Utils::toHex(hex, neighbour->id.pub_key, 4);
|
||||
|
||||
// add next neighbour
|
||||
uint32_t secs_ago = getRTCClock()->getCurrentTime() - neighbour->heard_timestamp;
|
||||
uint32_t secs_ago = safeElapsedSecs(getRTCClock()->getCurrentTime(), neighbour->heard_timestamp);
|
||||
sprintf(dp, "%s:%d:%d", hex, secs_ago, neighbour->snr);
|
||||
while (*dp)
|
||||
dp++; // find end of string
|
||||
|
|
|
|||
|
|
@ -158,13 +158,8 @@ class MyMesh : public BaseChatMesh, ContactVisitor {
|
|||
}
|
||||
|
||||
void setClock(uint32_t timestamp) {
|
||||
uint32_t curr = getRTCClock()->getCurrentTime();
|
||||
if (timestamp > curr) {
|
||||
getRTCClock()->setCurrentTime(timestamp);
|
||||
Serial.println(" (OK - clock set!)");
|
||||
} else {
|
||||
Serial.println(" (ERR: clock cannot go backwards)");
|
||||
}
|
||||
getRTCClock()->setCurrentTime(timestamp);
|
||||
Serial.println(" (OK - clock set!)");
|
||||
}
|
||||
|
||||
void importCard(const char* command) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue