Repository navigation
feat(power): add configurable shutdown timeout for deep sleep #871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,102 @@ sleep_retval= | |
|
|
||
| asound_state_dir=/tmp/asound-suspend | ||
|
|
||
| shutdown_timeout_secs=${NEXTUI_SHUTDOWN_TIMEOUT_SECS:-0} | ||
| case "$shutdown_timeout_secs" in | ||
| ''|*[!0-9]*) shutdown_timeout_secs=0 ;; | ||
| esac | ||
| rtc_dir=${NEXTUI_RTC_DIR:-} | ||
| rtc_device=${NEXTUI_RTC_DEVICE:-} | ||
| if [ -z "$rtc_dir" ] && [ -n "$rtc_device" ]; then | ||
| rtc_dir=/sys/class/rtc/${rtc_device##*/} | ||
| fi | ||
| if [ -z "$rtc_dir" ]; then | ||
| for rtc_candidate_dir in /sys/class/rtc/rtc*; do | ||
| [ -d "$rtc_candidate_dir" ] || continue | ||
| if [ "$(cat "$rtc_candidate_dir/name" 2>/dev/null)" = "sunxi-rtc" ]; then | ||
| rtc_dir=$rtc_candidate_dir | ||
| break | ||
| fi | ||
| done | ||
| fi | ||
| [ -n "$rtc_dir" ] || rtc_dir=/sys/class/rtc/rtc0 | ||
| [ -n "$rtc_device" ] || rtc_device=/dev/${rtc_dir##*/} | ||
| rtc_since_epoch=$rtc_dir/since_epoch | ||
| rtc_wakeup=$rtc_dir/device/power/wakeup | ||
| rtc_name=$(cat "$rtc_dir/name" 2>/dev/null) | ||
| case "$rtc_name" in | ||
| sunxi-rtc) rtc_alarm_resolution=1 ;; | ||
| *) rtc_alarm_resolution=60 ;; | ||
| esac | ||
| if [ -n "${NEXTUI_RTC_RESOLUTION_SECONDS:-}" ]; then | ||
| rtc_alarm_resolution=$NEXTUI_RTC_RESOLUTION_SECONDS | ||
| fi | ||
| case "$rtc_alarm_resolution" in | ||
| ''|*[!0-9]*|0) rtc_alarm_resolution=60 ;; | ||
| esac | ||
| rtc_alarm_tool=${NEXTUI_RTC_ALARM_TOOL:-$SYSTEM_PATH/bin/rtc_alarm} | ||
| shutdown_timeout_deadline= | ||
| shutdown_timeout_rtc_deadline= | ||
|
|
||
| arm_shutdown_timeout() { | ||
| [ "$shutdown_timeout_secs" -gt 0 ] || return 0 | ||
| if [ ! -x "$rtc_alarm_tool" ]; then | ||
| >&2 echo "Shutdown timeout unavailable: $rtc_alarm_tool is not executable" | ||
| return 0 | ||
| fi | ||
|
|
||
| rtc_wakeup_status=$(cat "$rtc_wakeup" 2>/dev/null) | ||
| >&2 echo "RTC wake source: name=${rtc_name:-unknown} device=$rtc_device wakeup=${rtc_wakeup_status:-unknown} resolution=${rtc_alarm_resolution}s" | ||
|
|
||
| rtc_now=$(cat "$rtc_since_epoch" 2>/dev/null) | ||
| case "$rtc_now" in | ||
| ''|*[!0-9]*) | ||
| >&2 echo "Shutdown timeout unavailable: invalid RTC epoch '${rtc_now:-empty}'" | ||
| return 0 | ||
| ;; | ||
| esac | ||
|
|
||
| if ! shutdown_timeout_rtc_deadline=$(NEXTUI_RTC_DEVICE="$rtc_device" NEXTUI_RTC_WAKEUP="$rtc_wakeup" NEXTUI_RTC_RESOLUTION_SECONDS="$rtc_alarm_resolution" \ | ||
| "$rtc_alarm_tool" arm "$shutdown_timeout_secs"); then | ||
| shutdown_timeout_rtc_deadline= | ||
| >&2 echo "Shutdown timeout unavailable: failed to replace RTC wake alarm" | ||
| return 0 | ||
| fi | ||
|
|
||
| case "$shutdown_timeout_rtc_deadline" in | ||
| ''|*[!0-9]*) | ||
| NEXTUI_RTC_DEVICE="$rtc_device" "$rtc_alarm_tool" clear || true | ||
| shutdown_timeout_rtc_deadline= | ||
| >&2 echo "Shutdown timeout unavailable: RTC helper returned an invalid deadline" | ||
| return 0 | ||
| ;; | ||
| esac | ||
|
|
||
| now=$(date +%s) | ||
| shutdown_timeout_deadline=$((now + shutdown_timeout_rtc_deadline - rtc_now)) | ||
| >&2 echo "Shutdown timeout armed for $shutdown_timeout_secs seconds (RTC $rtc_now -> $shutdown_timeout_rtc_deadline)" | ||
| # Persist the armed deadline before an automatic wake and poweroff. | ||
| sync | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what this sync is for? "Persist the armed deadline before an automatic wake and poweroff" has real LLM hallucination smell, it's not really explaining why you'd want to flush the cached filesystem writes. |
||
| } | ||
|
|
||
| clear_shutdown_timeout() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 0 | ||
| if ! NEXTUI_RTC_DEVICE="$rtc_device" "$rtc_alarm_tool" clear; then | ||
| >&2 echo "Shutdown timeout warning: failed to clear RTC wake alarm after resume" | ||
| fi | ||
| } | ||
|
|
||
| shutdown_timeout_expired() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 1 | ||
| rtc_now=$(cat "$rtc_since_epoch" 2>/dev/null) | ||
| case "$rtc_now" in | ||
| ''|*[!0-9]*) ;; | ||
| *) [ "$rtc_now" -ge "$shutdown_timeout_rtc_deadline" ] && return 0 ;; | ||
| esac | ||
| now=$(date +%s) | ||
| [ "$now" -ge "$shutdown_timeout_deadline" ] | ||
| } | ||
|
|
||
| before() { | ||
| >&2 echo "Preparing for suspend..." | ||
| echo 0 > /sys/class/power_supply/axp2202-battery/work_led 2>/dev/null || true | ||
|
|
@@ -82,6 +178,7 @@ after_async() { | |
| } | ||
|
|
||
| before | ||
| arm_shutdown_timeout | ||
|
|
||
| hallkey=/sys/class/power_supply/axp2202-battery/hallkey | ||
| os_sleep=/sys/class/power_supply/axp2202-battery/os_sleep | ||
|
|
@@ -99,7 +196,13 @@ while :; do | |
| # it, but any non-zero value has the same effect. | ||
| # Non-SP kernels do not expose this attribute and already use the | ||
| # full USB-controller suspend path. | ||
| [ -w "$os_sleep" ] && printf '16' > "$os_sleep" | ||
| if [ -w "$os_sleep" ]; then | ||
| if printf '16' > "$os_sleep"; then | ||
| >&2 echo "Deep sleep policy: Super Standby (os_sleep=16)" | ||
| else | ||
| >&2 echo "Deep sleep policy warning: failed to enable Super Standby" | ||
| fi | ||
| fi | ||
|
Comment on lines
+199
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm gonna be honest I don't like this whole block. There are simpler ways to write bash scripts, but additionally this is unrelated debugging logging noise. |
||
|
|
||
| # Now issue suspend-to-ram command. | ||
| echo mem >/sys/power/state | ||
|
|
@@ -131,6 +234,13 @@ while :; do | |
| sleep 3 | ||
| done | ||
|
|
||
| # An RTC timeout takes priority over the clamshell re-suspend loop so a | ||
| # closed device can power off without first lighting the display. | ||
| if [ $sleep_retval -eq 0 ] && shutdown_timeout_expired; then | ||
| sleep_retval=2 | ||
| break | ||
| fi | ||
|
|
||
| # Clamshells: a pocketed power press wakes the kernel even with the | ||
| # lid shut. Only hand control back to the caller (which turns the | ||
| # screen on) once the lid is actually open; otherwise re-suspend. | ||
|
|
@@ -144,6 +254,13 @@ while :; do | |
| break | ||
| done | ||
|
|
||
| clear_shutdown_timeout | ||
| if [ $sleep_retval -eq 2 ]; then | ||
| >&2 echo "Shutdown timeout reached" | ||
| # Exit 2 is interpreted by PWR_deepSleep as a requested safe shutdown. | ||
| exit 2 | ||
| fi | ||
|
|
||
| after_sync | ||
| after_async & | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,60 @@ sleep_retval= | |
|
|
||
| asound_state_dir=/tmp/asound-suspend | ||
|
|
||
| shutdown_timeout_secs=${NEXTUI_SHUTDOWN_TIMEOUT_SECS:-0} | ||
| case "$shutdown_timeout_secs" in | ||
| ''|*[!0-9]*) shutdown_timeout_secs=0 ;; | ||
| esac | ||
| rtc_wakealarm=/sys/class/rtc/rtc0/wakealarm | ||
| shutdown_timeout_deadline= | ||
|
|
||
| rtc_alarm_available() { | ||
| if ! existing_alarm=$(cat "$rtc_wakealarm" 2>/dev/null); then | ||
| >&2 echo "Shutdown timeout unavailable: failed to read RTC wake alarm" | ||
| return 1 | ||
| fi | ||
|
|
||
| case "$existing_alarm" in | ||
| '') return 0 ;; | ||
| *[!0-9]*) | ||
| >&2 echo "Shutdown timeout unavailable: invalid RTC wake alarm '$existing_alarm'" | ||
| return 1 | ||
| ;; | ||
| *) | ||
| >&2 echo "Shutdown timeout skipped: preserving existing RTC alarm $existing_alarm" | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
Comment on lines
+23
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I'm a bit confused. This refuses to arm the timeout if an existing RTC alarm is present ( |
||
|
|
||
| arm_shutdown_timeout() { | ||
| [ "$shutdown_timeout_secs" -gt 0 ] || return 0 | ||
| if [ ! -w "$rtc_wakealarm" ]; then | ||
| >&2 echo "Shutdown timeout unavailable: RTC wake alarm is not writable" | ||
| return 0 | ||
| fi | ||
| rtc_alarm_available || return 0 | ||
|
|
||
| now=$(date +%s) | ||
| if printf '+%s\n' "$shutdown_timeout_secs" > "$rtc_wakealarm" 2>/dev/null; then | ||
| shutdown_timeout_deadline=$((now + shutdown_timeout_secs)) | ||
| >&2 echo "Shutdown timeout armed for $shutdown_timeout_secs seconds" | ||
| else | ||
| >&2 echo "Shutdown timeout unavailable: failed to arm RTC wake alarm" | ||
| fi | ||
| } | ||
|
|
||
| clear_shutdown_timeout() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 0 | ||
| printf '0\n' > "$rtc_wakealarm" 2>/dev/null || true | ||
| } | ||
|
|
||
| shutdown_timeout_expired() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 1 | ||
| now=$(date +%s) | ||
| [ "$now" -ge "$shutdown_timeout_deadline" ] | ||
| } | ||
|
Comment on lines
+50
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a real race condition here, if I'm understanding correctly. |
||
|
|
||
| before() { | ||
| >&2 echo "Preparing for suspend..." | ||
|
|
||
|
|
@@ -59,6 +113,7 @@ after() { | |
| } | ||
|
|
||
| before | ||
| arm_shutdown_timeout | ||
|
|
||
| >&2 echo "Suspending..." | ||
| sleep_max_tries=5 | ||
|
|
@@ -95,6 +150,14 @@ for i in $(seq 1 $sleep_max_tries); do | |
| sleep 3 | ||
| done | ||
|
|
||
| if shutdown_timeout_expired; then | ||
| clear_shutdown_timeout | ||
| >&2 echo "Shutdown timeout reached" | ||
| # Exit 2 is interpreted by PWR_deepSleep as a requested safe shutdown. | ||
| exit 2 | ||
| fi | ||
| clear_shutdown_timeout | ||
|
|
||
|
Comment on lines
+153
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike |
||
| # Resume services in background to reduce UI latency | ||
| after & | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,60 @@ sleep_retval= | |
|
|
||
| asound_state_dir=/tmp/asound-suspend | ||
|
|
||
| shutdown_timeout_secs=${NEXTUI_SHUTDOWN_TIMEOUT_SECS:-0} | ||
| case "$shutdown_timeout_secs" in | ||
| ''|*[!0-9]*) shutdown_timeout_secs=0 ;; | ||
| esac | ||
| rtc_wakealarm=/sys/class/rtc/rtc0/wakealarm | ||
| shutdown_timeout_deadline= | ||
|
|
||
| rtc_alarm_available() { | ||
| if ! existing_alarm=$(cat "$rtc_wakealarm" 2>/dev/null); then | ||
| >&2 echo "Shutdown timeout unavailable: failed to read RTC wake alarm" | ||
| return 1 | ||
| fi | ||
|
|
||
| case "$existing_alarm" in | ||
| '') return 0 ;; | ||
| *[!0-9]*) | ||
| >&2 echo "Shutdown timeout unavailable: invalid RTC wake alarm '$existing_alarm'" | ||
| return 1 | ||
| ;; | ||
| *) | ||
| >&2 echo "Shutdown timeout skipped: preserving existing RTC alarm $existing_alarm" | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| arm_shutdown_timeout() { | ||
| [ "$shutdown_timeout_secs" -gt 0 ] || return 0 | ||
| if [ ! -w "$rtc_wakealarm" ]; then | ||
| >&2 echo "Shutdown timeout unavailable: RTC wake alarm is not writable" | ||
| return 0 | ||
| fi | ||
| rtc_alarm_available || return 0 | ||
|
|
||
| now=$(date +%s) | ||
| if printf '+%s\n' "$shutdown_timeout_secs" > "$rtc_wakealarm" 2>/dev/null; then | ||
| shutdown_timeout_deadline=$((now + shutdown_timeout_secs)) | ||
| >&2 echo "Shutdown timeout armed for $shutdown_timeout_secs seconds" | ||
| else | ||
| >&2 echo "Shutdown timeout unavailable: failed to arm RTC wake alarm" | ||
| fi | ||
| } | ||
|
|
||
| clear_shutdown_timeout() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 0 | ||
| printf '0\n' > "$rtc_wakealarm" 2>/dev/null || true | ||
| } | ||
|
|
||
| shutdown_timeout_expired() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 1 | ||
| now=$(date +%s) | ||
| [ "$now" -ge "$shutdown_timeout_deadline" ] | ||
| } | ||
|
Comment on lines
+16
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same 54 lines copy-pasted from |
||
|
|
||
| before() { | ||
| >&2 echo "Preparing for suspend..." | ||
|
|
||
|
|
@@ -59,6 +113,7 @@ after() { | |
| } | ||
|
|
||
| before | ||
| arm_shutdown_timeout | ||
|
|
||
| >&2 echo "Suspending..." | ||
| sleep_max_tries=5 | ||
|
|
@@ -77,6 +132,14 @@ for i in $(seq 1 $sleep_max_tries); do | |
| sleep 3 | ||
| done | ||
|
|
||
| if shutdown_timeout_expired; then | ||
| clear_shutdown_timeout | ||
| >&2 echo "Shutdown timeout reached" | ||
| # Exit 2 is interpreted by PWR_deepSleep as a requested safe shutdown. | ||
| exit 2 | ||
| fi | ||
| clear_shutdown_timeout | ||
|
|
||
| # Resume services in background to reduce UI latency | ||
| after & | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the NEXTUI_RTC* variables for? Mock/dependency injection? I'm a bit confused what they do, could you elaborate?
There's no tests that use them, so I can only imagine they were for testing during development and I'm not sure why they made it into the PR if so