Rely on /sys/power/wake_lock

Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
Signed-off-by: Peter John Hartman <peterjohnhartman@gmail.com>
This commit is contained in:
Willow Barraco
2023-02-23 13:20:05 +01:00
committed by Peter John Hartman
parent b204be1d86
commit 89a77e6057
28 changed files with 272 additions and 370 deletions

View File

@@ -50,39 +50,53 @@ whichWake() {
echo "button"
}
sxmo_log "going to suspend to crust"
saveAllEventCounts
sxmo_hook_presuspend.sh
sxmo_led.sh blink red
if suspend_time="$(sxmo_hook_mnc.sh)"; then
sxmo_log "calling suspend with suspend_time <$suspend_time>"
start="$(date "+%s")"
sxmo_hook_suspend.sh "$suspend_time"
#We woke up again
time_spent="$(( $(date "+%s") - start ))"
if [ "$suspend_time" -gt 0 ] && [ "$((time_spent + 10))" -ge "$suspend_time" ]; then
UNSUSPENDREASON="rtc"
else
UNSUSPENDREASON="$(whichWake)"
finish() {
if [ -n "$INITIAL" ]; then
echo "$INITIAL" > /sys/power/autosleep
fi
else
sxmo_log "fake suspend (suspend_time ($suspend_time) less than zero)"
UNSUSPENDREASON=rtc # we fake the crust for those seconds
kill "$WAKEPID"
exit
}
autosuspend() {
YEARS8_TO_SEC=268435455
INITIAL="$(cat /sys/power/autosleep)"
trap 'finish' TERM INT EXIT
while : ; do
# necessary?
echo "$INITIAL" > /sys/power/autosleep
suspend_time=99999999 # far away
mnc="$(sxmo_hook_mnc.sh)"
if [ -n "$mnc" ] && [ "$mnc" -gt 0 ] && [ "$mnc" -lt "$YEARS8_TO_SEC" ]; then
if [ "$mnc" -le 15 ]; then # cronjob imminent
echo "waiting_cronjob" | doas tee -a /sys/power/wake_lock > /dev/null
suspend_time=$((mnc + 1)) # to arm the following one
else
suspend_time=$((mnc - 10))
fi
fi
sxmo_wakeafter "$suspend_time" "sxmo_autosuspend.sh wokeup" &
WAKEPID=$!
sleep 1 # wait for it to epoll pwait
echo mem > /sys/power/autosleep
wait
done
}
wokeup() {
# 10s basic hold
echo "woke_up 10000000000" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_hook_postwake.sh
}
if [ -z "$*" ]; then
set -- autosuspend
fi
echo "$UNSUSPENDREASON" > "$SXMO_UNSUSPENDREASONFILE"
sxmo_log "woke up from crust (reason=$UNSUSPENDREASON)"
if [ "$UNSUSPENDREASON" = "rtc" ]; then
sxmo_mutex.sh can_suspend lock "Waiting for cronjob"
fi
sxmo_hook_postwake.sh "$UNSUSPENDREASON"
"$@"

View File

@@ -6,12 +6,12 @@
. sxmo_common.sh
free_mutex() {
sxmo_mutex.sh can_suspend free "Playing with leds"
echo "playing_with_leds" | doas tee -a /sys/power/wake_unlock > /dev/null
exit
}
ensure_mutex() {
sxmo_mutex.sh can_suspend lock "Playing with leds"
echo "playing_with_leds" | doas tee -a /sys/power/wake_lock > /dev/null
trap 'free_mutex' TERM INT
}

View File

@@ -1,92 +0,0 @@
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
set -e
MUTEX_NAME="$1"
shift
ROOT_DIR="${XDG_RUNTIME_DIR:-/dev/shm/user/$(id -u)}"/sxmo_mutex
REASON_FILE="$ROOT_DIR/$MUTEX_NAME"
mkdir -p "$(dirname "$REASON_FILE")"
exec 3>"$REASON_FILE.lock"
touch "$REASON_FILE"
lock() {
flock 3
REASON="$1"
printf "%s\n" "$REASON" >> "$REASON_FILE"
sxmo_debug "lock \"$MUTEX_NAME\", total: $(wc -l < "$REASON_FILE"), \"$REASON\""
}
free() {
flock 3
REASON="$1"
grep -xnm1 "$REASON" "$REASON_FILE" | \
cut -d: -f1 | \
xargs -r -I{} sed -i '{}d' "$REASON_FILE"
sxmo_debug "free \"$MUTEX_NAME\", total: $(wc -l < "$REASON_FILE"), \"$REASON\""
}
lockedby() {
sxmo_debug "Lockedby: $1"
grep -qxm1 "$1" "$REASON_FILE"
}
freeall() {
printf "" > "$REASON_FILE"
sxmo_hook_statusbar.sh lockedby
sxmo_log "freeall($MUTEX_NAME)"
}
list() {
cat "$REASON_FILE"
}
hold() {
if ! [ -s "$REASON_FILE" ]; then
exit 0
fi
FIFO="$(mktemp -u)"
mkfifo "$FIFO"
inotifywait -mq -e "close_write" "$ROOT_DIR" >> "$FIFO" &
NOTIFYPID=$!
# shellcheck disable=SC2317
finish() {
kill "$NOTIFYPID" 2>/dev/null
rm "$FIFO"
exit 0
}
trap 'finish' TERM INT EXIT
# shellcheck disable=SC2034
while read -r _; do
if ! [ -s "$REASON_FILE" ]; then
exit 0
fi
done < "$FIFO"
}
holdexec() {
# shellcheck disable=SC2317
finish() {
kill "$HOLDPID" 2>/dev/null
exit
}
trap 'finish' TERM INT
hold &
HOLDPID=$!
wait "$HOLDPID"
"$@"
}
"$@"

View File

@@ -2,10 +2,4 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
sxmo_mutex.sh can_suspend list
tail -f "$XDG_STATE_HOME"/sxmo.log | stdbuf -oL grep 'sxmo_mutex.sh'
watch 'cat /sys/power/wake_lock | tr " " "\n"'

View File

@@ -19,7 +19,7 @@ finish() {
# (see static const int stk3310_ps_max[4])
printf 6553 > "$prox_path/events/in_proximity_thresh_rising_value"
sxmo_mutex.sh can_suspend free "Proximity lock is running"
echo "proximity_lock_running" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
exec sxmo_hook_"$INITIALSTATE".sh
@@ -50,8 +50,7 @@ trap 'finish' TERM INT
sxmo_daemons.sh stop idle_locker
sxmo_mutex.sh can_suspend lock "Proximity lock is running"
sxmo_hook_statusbar.sh lockedby
echo "proximity_lock_running" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
# find the device

View File

@@ -8,13 +8,11 @@
. sxmo_common.sh
# We can have multiple cronjobs at the same time
sxmo_mutex.sh can_suspend lock "Executing cronjob"
sxmo_mutex.sh can_suspend free "Waiting for cronjob"
sxmo_hook_statusbar.sh lockedby
echo "executing_cronjob" | doas tee -a /sys/power/wake_lock > /dev/null
echo "waiting_cronjob" | doas tee -a /sys/power/wake_unlock > /dev/null
finish() {
sxmo_mutex.sh can_suspend free "Executing cronjob"
sxmo_hook_statusbar.sh lockedby
echo "executing_cronjob" | doas tee -a /sys/power/wake_unlock > /dev/null
exit 0
}

View File

@@ -18,7 +18,7 @@ fi
# users can override this in sxmo_deviceprofile_mydevice.sh
files="${SXMO_SYS_FILES:-"/sys/power/state /sys/power/mem_sleep /dev/rtc0"}"
for file in $files; do
for file in $files /sys/power/autosleep; do
[ -e "$file" ] && chmod a+rw "$file"
done

View File

@@ -34,7 +34,7 @@ update_nixos() {
echo "Upgrade complete - reboot for all changes to take effect"
}
sxmo_mutex.sh can_suspend lock "Upgrading"
echo "upgrading" | doas tee -a /sys/power/wake_lock > /dev/null
case "$OS" in
alpine|postmarketos) update_apk;;
@@ -42,4 +42,4 @@ case "$OS" in
nixos) update_nixos;;
esac
sxmo_mutex.sh can_suspend free "Upgrading"
echo "upgrading" | doas tee -a /sys/power/wake_unlock > /dev/null