From 0f4595a8691e72443a55fb8eac8b94a212d9af33 Mon Sep 17 00:00:00 2001
From: jane400 <alpine@j4ne.de>
Date: Wed, 14 Jun 2023 13:55:34 +0200
Subject: [PATCH] community/waked: fix qualcomm's rtc errata

The RTC on Qualcomm platforms are apparently read-only and don't reflect
the current time, this commit adds a patch where we use relative instead
of absolute time when writing an alarm to the RTC, as this will work on
sdm845 and similiar. Thank you @caleb, who basically told me the problem
and suggested to just use relative times in waked.
---
 .../waked/0001-cmake-add-install-target.patch |  6 +-
 ...pedef-for-uint64_t-fixes-compilation.patch | 24 +++++++
 ...3-use-relative-times-for-broken-RTCs.patch | 63 +++++++++++++++++++
 community/waked/APKBUILD                      |  8 ++-
 4 files changed, 96 insertions(+), 5 deletions(-)
 create mode 100644 community/waked/0002-Include-typedef-for-uint64_t-fixes-compilation.patch
 create mode 100644 community/waked/0003-use-relative-times-for-broken-RTCs.patch

diff --git a/community/waked/0001-cmake-add-install-target.patch b/community/waked/0001-cmake-add-install-target.patch
index e21fe87ac532..9fecdb8c8943 100644
--- a/community/waked/0001-cmake-add-install-target.patch
+++ b/community/waked/0001-cmake-add-install-target.patch
@@ -1,7 +1,7 @@
-From 27a0919d0d5f37ea512bc216e9c0262e1d9a3aeb Mon Sep 17 00:00:00 2001
+From 6e906c1de7bc5442ca2232d851e20bfc2c02ed98 Mon Sep 17 00:00:00 2001
 From: Clayton Craft <clayton@craftyguy.net>
 Date: Fri, 19 Nov 2021 18:52:03 -0800
-Subject: [PATCH] cmake: add install target
+Subject: [PATCH 1/3] cmake: add install target
 
 This adds a simple install target to install the compiled binary + dbus
 conf file.
@@ -51,5 +51,5 @@ index a24e527..a89508d 100644
 +
 +install(TARGETS waked DESTINATION "${CMAKE_INSTALL_BINDIR}")
 -- 
-2.34.0
+2.41.0
 
diff --git a/community/waked/0002-Include-typedef-for-uint64_t-fixes-compilation.patch b/community/waked/0002-Include-typedef-for-uint64_t-fixes-compilation.patch
new file mode 100644
index 000000000000..32fa514ca44c
--- /dev/null
+++ b/community/waked/0002-Include-typedef-for-uint64_t-fixes-compilation.patch
@@ -0,0 +1,24 @@
+From 6facb897036eb2fe4dd5b066b51f3120e52f7e03 Mon Sep 17 00:00:00 2001
+From: Jane Rachinger <jane400@bingo-ev.de>
+Date: Tue, 13 Jun 2023 22:13:58 +0200
+Subject: [PATCH 2/3] Include typedef for uint64_t, fixes compilation
+
+---
+ src/alarm.h | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/src/alarm.h b/src/alarm.h
+index ad32676..c7107e2 100644
+--- a/src/alarm.h
++++ b/src/alarm.h
+@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
+ #define ALARM_H
+ 
+ #include <string>
++#include <cstdint>
+ 
+ class Alarm
+ {
+-- 
+2.41.0
+
diff --git a/community/waked/0003-use-relative-times-for-broken-RTCs.patch b/community/waked/0003-use-relative-times-for-broken-RTCs.patch
new file mode 100644
index 000000000000..a2b3699f47e1
--- /dev/null
+++ b/community/waked/0003-use-relative-times-for-broken-RTCs.patch
@@ -0,0 +1,63 @@
+From 1ed6ba38f2b7f9161164d7fadf4245972e150c68 Mon Sep 17 00:00:00 2001
+From: Jane Rachinger <jane400@bingo-ev.de>
+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<Alarm> 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
+
diff --git a/community/waked/APKBUILD b/community/waked/APKBUILD
index e2264f8ef7a9..da02a92ad17b 100644
--- a/community/waked/APKBUILD
+++ b/community/waked/APKBUILD
@@ -3,7 +3,7 @@
 # Maintainer: Clayton Craft <clayton@craftyguy.net>
 pkgname=waked
 pkgver=0.1.1
-pkgrel=3
+pkgrel=4
 license="GPL-2.0-or-later"
 pkgdesc="Waked is a daemon which lets Apps wake the system from suspend at requested times"
 arch="all"
@@ -12,6 +12,8 @@ makedepends="cmake samurai sdbus-cpp-dev"
 source="https://gitlab.com/seath1/waked/-/archive/v$pkgver/waked-v$pkgver.tar.bz2
 	waked.initd
 	0001-cmake-add-install-target.patch
+	0002-Include-typedef-for-uint64_t-fixes-compilation.patch
+	0003-use-relative-times-for-broken-RTCs.patch
 	"
 subpackages="$pkgname-openrc"
 options="!check" # No test suite
@@ -41,5 +43,7 @@ package() {
 sha512sums="
 8d0c3d659e7e8a8f4d8c0462871a234931d0e915935cdd119f79a8059ac74baba22ce9d12c1a376cad232a7ef79a8bdd35392f04c435ad8653ec8947ed170c37  waked-v0.1.1.tar.bz2
 04364b519d7266859b151a12178c68e5837c8b6310f6b1ca7920d4163970dfd6310a7c4816ec81d0bfbd8ab9c9e93168ad5ef473b8ebc69304da45c0ca196f11  waked.initd
-323ba3948a2d9e7313f745bb56333c5cad21dc7366fdf5983324bb4b0ab84b1bbe29874d328bb3c46e1c5b550ea499ee084119da9ce575d6f1c1401452f51b09  0001-cmake-add-install-target.patch
+2de6b05dc8dcb5c526c2d419c022a728029dac390926413078eaf9347d7e018e73aa285256882bbbf35ecbdc2f0f277c3eff17ad5238e97e6e38d9bdaf310539  0001-cmake-add-install-target.patch
+a932f341cafce2ef14112f1dd53888fea941ad2adfa5ff420ab8ef7ec10c08fd7afadbe0d2f0a8aba1cab963dc0a25d55daffb331f539879420d551106fce143  0002-Include-typedef-for-uint64_t-fixes-compilation.patch
+38d14cabea4cde437f7825d86a628040ae0c0e37b380cf48da2364c0607a7fcf9746884299468b9f86f1c34dd80fadf6a3d37b98ff1fb18bbf9e31f7dc2733ea  0003-use-relative-times-for-broken-RTCs.patch
 "
-- 
GitLab