fix(mqtt): consult the RTC when libc cannot vouch for the fallback clock
The usable-clock fallback asked libc only, which does not answer for the case it was written to cover. On a cold boot ESP32RTCClock::begin() stamps libc with a 2024 placeholder on power-on; AutoDiscoverRTCClock::begin() probes the chip but never copies its time across, and getCurrentTime() reads the chip directly. So a Station G3 or T-Beam Supreme that knows exactly what time it is, on a network with UDP/123 blocked, still failed the plausibility test, left _ntp_synced false, and brought up no slots — precisely the deployment the fallback exists for. Ask the RTC when libc is below the floor. libc still wins when it is usable: a clock SNTP set recently outranks a chip that may have drifted. Accepting the RTC value then flows through the same block, so settimeofday() repairs libc and the epoch is written back to the chip. The choice is chooseFallbackClock() in MQTTConnectionPolicy, host-tested across the four states including the power-on placeholder and the exact floor. Also corrects the previous commit's claim that configTime() is called only when a server replied — the fallback necessarily points it at each server before knowing that; it is the post-acceptance call that is now conditional.
This commit is contained in:
1 parent
ee2f866b10
commit
436bb65ae6
3 files changed
+81
-7
No files matched your search
@@ -290,6 +290,48 @@ TEST(StaleToken, FailedMintNeverReconnects) {
|
||||
EXPECT_EQ(StaleTokenAction::Defer, Policy::classifyStaleToken(false, true, false));
|
||||
}
|
||||
|
||||
using Policy::ClockSource;
|
||||
|
||||
// 2026-01-01, the bridge's plausibility floor, and the 2024 placeholder
|
||||
// ESP32RTCClock::begin() stamps into libc on a power-on reset.
|
||||
static const uint32_t kFloor = 1767225600;
|
||||
static const uint32_t kPowerOnPlaceholder = 1715770351;
|
||||
static const uint32_t kPlausibleNow = 1786000000;
|
||||
|
||||
TEST(FallbackClock, PrefersTheSystemClockWhenItIsUsable) {
|
||||
// A clock SNTP set recently outranks an RTC that may have drifted.
|
||||
EXPECT_EQ(ClockSource::System,
|
||||
Policy::chooseFallbackClock(false, kPlausibleNow, kPlausibleNow - 900, kFloor));
|
||||
}
|
||||
|
||||
TEST(FallbackClock, FallsBackToTheRtcOnAColdBoot) {
|
||||
// The case the system-clock-only check missed: libc holds the power-on
|
||||
// placeholder while a detected chip holds real time.
|
||||
EXPECT_EQ(ClockSource::Rtc,
|
||||
Policy::chooseFallbackClock(false, kPowerOnPlaceholder, kPlausibleNow, kFloor));
|
||||
}
|
||||
|
||||
TEST(FallbackClock, NothingUsableStaysUnsynced) {
|
||||
EXPECT_EQ(ClockSource::None,
|
||||
Policy::chooseFallbackClock(false, kPowerOnPlaceholder, kPowerOnPlaceholder, kFloor));
|
||||
// rtc_time 0 is how a board with no clock to consult is passed in.
|
||||
EXPECT_EQ(ClockSource::None,
|
||||
Policy::chooseFallbackClock(false, kPowerOnPlaceholder, 0, kFloor));
|
||||
}
|
||||
|
||||
TEST(FallbackClock, ServerValidationNeverAcceptsALocalClock) {
|
||||
// `set mqtt.ntp` asks whether that host answers. No clock can answer for it,
|
||||
// however plausible — this is the path where a typo has to fail.
|
||||
EXPECT_EQ(ClockSource::None,
|
||||
Policy::chooseFallbackClock(true, kPlausibleNow, kPlausibleNow, kFloor));
|
||||
}
|
||||
|
||||
TEST(FallbackClock, TheFloorItselfIsAccepted) {
|
||||
EXPECT_EQ(ClockSource::System, Policy::chooseFallbackClock(false, kFloor, 0, kFloor));
|
||||
EXPECT_EQ(ClockSource::Rtc, Policy::chooseFallbackClock(false, kFloor - 1, kFloor, kFloor));
|
||||
EXPECT_EQ(ClockSource::None, Policy::chooseFallbackClock(false, kFloor - 1, kFloor - 1, kFloor));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
Reference in new issue
Block a user