mirror of https://github.com/meshcore-dev/MeshCore
3 changed files with 51 additions and 0 deletions
@ -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
|
||||
@ -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…
Reference in new issue