From 1ed6ba38f2b7f9161164d7fadf4245972e150c68 Mon Sep 17 00:00:00 2001 From: Jane Rachinger Date: Tue, 13 Jun 2023 22:14:16 +0200 Subject: [PATCH 3/3] use relative times for broken RTCs --- src/main.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f781fff..f448e88 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,11 +34,15 @@ std::list alarmList; sdbus::UnixFd suspendDelayLockFd; -void writeToRTC(std::uint64_t data) { +void writeToRTC(std::uint64_t data, bool relative) { std::cout << "Writing to RTC: " << data << std::endl; std::ofstream rtc("/sys/class/rtc/rtc0/wakealarm"); if (rtc.is_open()) { - rtc << data << std::endl; + if (relative) { + rtc << "+" << data << std::endl; + } else { + rtc << data << std::endl; + } } else { std::cout << "ERROR: Couldn't open RTC to write" << std::endl; } @@ -69,8 +73,8 @@ void rescedule() if (alarmList.size()) { uint64_t localReadFromRTC = readFromRTC(); if ((localReadFromRTC > alarmList.front().getTime()) || (!localReadFromRTC)) { - writeToRTC(0); - writeToRTC(alarmList.front().getTime()); + writeToRTC(0, false); + writeToRTC(alarmList.front().getTime() - now, true); } } } @@ -95,7 +99,7 @@ std::string removeAlarm(const std::string& id) if ((alarmList.size()) && (alarmList.front().getId() == id) && (alarmList.front().getTime() == readFromRTC())) { - writeToRTC(0); + writeToRTC(0, false); } alarmList.remove_if([id](Alarm &a){return id == a.getId();}); @@ -123,7 +127,7 @@ void handleSuspend(const bool active) { std::time_t now = std::time(nullptr); if ((alarmList.size()) && (alarmList.front().getTime() < now + 10UL)) { std::cout << "Next alarm too close. Wake up in 10 Seconds ..." << std::endl; - writeToRTC(now + 10UL); + writeToRTC(10UL, true); } suspendDelayLockFd.reset(); } else { -- 2.41.0