Browse Source

Merge 0ec2475bcf into adc631f315

pull/3003/merge
Alexander Hoff ❍ 14 hours ago
committed by GitHub
parent
commit
ce4124d3fe
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      src/helpers/AutoDiscoverRTCClock.cpp
  2. 14
      src/helpers/RTCValidity.h
  3. 28
      test/test_rtc_validity/test_rtc_validity.cpp

9
src/helpers/AutoDiscoverRTCClock.cpp

@ -1,4 +1,5 @@
#include "AutoDiscoverRTCClock.h"
#include "RTCValidity.h"
#include "RTClib.h"
#include <Melopero_RV3028.h>
#include "RTC_RX8130CE.h"
@ -30,6 +31,10 @@ void AutoDiscoverRTCClock::begin(TwoWire& wire) {
#if !defined(DISABLE_DS3231_PROBE)
if (i2c_probe(wire, DS3231_ADDRESS)) {
ds3231_success = rtc_3231.begin(&wire);
if (ds3231_success && mesh::seedRTCIfLostPower(rtc_3231.lostPower(), _fallback->getCurrentTime(),
[](uint32_t time) { rtc_3231.adjust(DateTime(time)); })) {
MESH_DEBUG_PRINTLN("DS3231: Lost power, initialized from fallback clock");
}
}
#endif
@ -44,6 +49,10 @@ void AutoDiscoverRTCClock::begin(TwoWire& wire) {
if (i2c_probe(wire, PCF8563_ADDRESS)) {
MESH_DEBUG_PRINTLN("PCF8563: Found");
rtc_8563_success = rtc_8563.begin(&wire);
if (rtc_8563_success && mesh::seedRTCIfLostPower(rtc_8563.lostPower(), _fallback->getCurrentTime(),
[](uint32_t time) { rtc_8563.adjust(DateTime(time)); })) {
MESH_DEBUG_PRINTLN("PCF8563: Lost power, initialized from fallback clock");
}
}
if (i2c_probe(wire, RX8130CE_ADDRESS)) {

14
src/helpers/RTCValidity.h

@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
namespace mesh {
template <typename AdjustClock>
bool seedRTCIfLostPower(bool lost_power, uint32_t fallback_time, AdjustClock adjust_clock) {
if (!lost_power) return false;
adjust_clock(fallback_time);
return true;
}
} // namespace mesh

28
test/test_rtc_validity/test_rtc_validity.cpp

@ -0,0 +1,28 @@
#include <gtest/gtest.h>
#include <helpers/RTCValidity.h>
TEST(RTCValidity, SeedsLostPowerClockFromFallback) {
uint32_t adjusted_to = 0;
bool seeded = mesh::seedRTCIfLostPower(true, 1715770351,
[&](uint32_t time) { adjusted_to = time; });
EXPECT_TRUE(seeded);
EXPECT_EQ(1715770351u, adjusted_to);
}
TEST(RTCValidity, PreservesValidBatteryBackedClock) {
bool adjusted = false;
bool seeded = mesh::seedRTCIfLostPower(false, 1715770351,
[&](uint32_t) { adjusted = true; });
EXPECT_FALSE(seeded);
EXPECT_FALSE(adjusted);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading…
Cancel
Save