Skip to content

Enhance RTC handling with unit test support for system time fallback#10642

Merged
thebentern merged 3 commits into
masterfrom
fix-rtc-time
Jun 6, 2026
Merged

Enhance RTC handling with unit test support for system time fallback#10642
thebentern merged 3 commits into
masterfrom
fix-rtc-time

Conversation

@thebentern

@thebentern thebentern commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

This pull request adds robust unit testing support and fixes for RTC (real-time clock) fallback logic, especially for platforms without a hardware RTC (like RP2040). The main focus is to ensure that the system time fallback does not overwrite higher-quality time sources (e.g., from GPS or network) and to allow deterministic testing of time-related logic. It introduces test seams, new helper functions for tests, and regression tests for a previously reported issue.

Closes #9828

Key changes:

RTC fallback improvements and test seams:

  • Added test-only variables and logic to RTC.cpp to allow injection and control of system time and fallback behavior during unit tests, enabling deterministic and isolated testing of RTC logic.
  • Refactored readFromRTC() and related code to use a new readFromSystemTimeFallback() helper, ensuring system time is only adopted when no better source exists and simplifying the fallback handling.
  • Extended platform support for setting system time to include RP2040 as well as ESP32 in perhapsSetRTC().

Unit test support functions:

  • Added several new functions in RTC.cpp/RTC.h for unit tests: resetRTCStateForTests(), setRTCSystemTimeForTests(), clearRTCSystemTimeForTests(), and setReadFromRTCUseSystemTimeForTests(), enabling tests to control and reset RTC state and behavior. [1] [2]

Regression and behavior tests:

@thebentern thebentern requested a review from Copilot June 6, 2026 13:58
@github-actions github-actions Bot added the ai-generated Possible AI-generated low-quality content label Jun 6, 2026
@thebentern thebentern added enhancement New feature or request bugfix Pull request that fixes bugs and removed ai-generated Possible AI-generated low-quality content labels Jun 6, 2026
@thebentern thebentern mentioned this pull request Jun 6, 2026
7 tasks

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens RTC/system-time fallback behavior so that a later readFromRTC() (on platforms where gettimeofday() may only reflect uptime) won’t overwrite higher-quality time already established via network/GPS, while still allowing the fallback to seed time when nothing better exists. It also adds a native unit test suite with test seams to force the fallback path and inject deterministic system time.

Changes:

  • Refactors the no-hardware-RTC gettimeofday() path into a dedicated fallback that only seeds internal timekeeping when current quality is RTCQualityNone.
  • Adds unit-test-only hooks to inject/mock system time and force the fallback path even when a hardware-RTC branch is compiled.
  • Introduces a new test_rtc unit test suite covering preservation of higher-quality time and fallback seeding behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test/test_rtc/test_main.cpp Adds regression tests validating fallback seeding and preservation of higher-quality network time.
src/gps/RTC.h Exposes new PIO_UNIT_TESTING test seam APIs for RTC/system-time behavior.
src/gps/RTC.cpp Implements system-time fallback helper, test seams for injected time/forced fallback, and adjusts readFromRTC()/perhapsSetRTC() behavior accordingly.

Comment thread src/gps/RTC.cpp
Comment thread src/gps/RTC.cpp
thebentern and others added 2 commits June 6, 2026 09:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@thebentern thebentern merged commit c7f17a8 into master Jun 6, 2026
84 checks passed
@thebentern thebentern deleted the fix-rtc-time branch June 6, 2026 19:15
Evil8it pushed a commit to Evil8it/ME4TACTNK that referenced this pull request Jun 10, 2026
…eshtastic#10642)

* Enhance RTC handling with unit test support for system time fallback

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
NomDeTom pushed a commit to NomDeTom/MeshtasticFirmware that referenced this pull request Jun 25, 2026
…eshtastic#10642)

* Enhance RTC handling with unit test support for system time fallback

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
(cherry picked from commit c7f17a8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants