From 0d9d648136dc23c94824a79ae515831ea39042ea Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Mon, 12 May 2025 15:35:53 +0100 Subject: [PATCH 01/13] fix: adjust error handling in suspend script --- skeleton/SYSTEM/tg5040/bin/suspend | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 7e78ce976..b91ef1940 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -1,5 +1,5 @@ #!/bin/sh -set -euo pipefail +set -eu exec 0<&- wpa_running= @@ -51,7 +51,7 @@ after() { if [ -n "$wpa_running" ]; then >&2 echo "Starting wpa_supplicant..." wpa_supplicant -B -iwlan0 -Dnl80211 -c/etc/wifi/wpa_supplicant.conf -I/etc/wifi/wpa_supplicant_overlay.conf -O/etc/wifi/sockets || true - (( udhcpc -i wlan0 &)&) + udhcpc -i wlan0 & fi if [ -n "$hciattach_running" ]; then @@ -68,7 +68,7 @@ after() { before >&2 echo "Suspending..." -echo mem >/sys/power/state +echo mem >/sys/power/state || true # Resume services in background to reduce UI latency -(( after &)&) +after & From 3dbac14bf8f199e372cc052d0c454da9c23ba29c Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 11:38:59 +0100 Subject: [PATCH 02/13] feat: use trap --- skeleton/SYSTEM/tg5040/bin/suspend | 67 +++++++++++++++--------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index b91ef1940..41039a732 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -8,7 +8,36 @@ bluetoothd_running= asound_state_dir=/tmp/asound-suspend -before() { +restore() { + >&2 echo "Resumed from suspend." + + >&2 echo "Restoring mixer state..." + alsactl --file "$asound_state_dir/asound.state.post" store || true + alsactl --file "$asound_state_dir/asound.state.pre" restore || true + + >&2 echo "Unblocking wireless..." + echo 1 >/sys/class/rfkill/rfkill0/state || true + + if [ -n "$wpa_running" ]; then + >&2 echo "Starting wpa_supplicant..." + wpa_supplicant -B -iwlan0 -Dnl80211 -c/etc/wifi/wpa_supplicant.conf -I/etc/wifi/wpa_supplicant_overlay.conf -O/etc/wifi/sockets || true + udhcpc -i wlan0 & + fi + + if [ -n "$hciattach_running" ]; then + >&2 echo "Starting hciattach..." + /etc/init.d/hciattach start || true + fi + if [ -n "$bluetoothd_running" ]; then + >&2 echo "Starting bluetoothd..." + /etc/bluetooth/bluetoothd start || true + /usr/bin/bluetoothctl power on || true + fi +} + +main() { + trap "restore &" EXIT INT TERM HUP QUIT + >&2 echo "Preparing for suspend..." >&2 echo "Saving mixer state..." @@ -36,39 +65,9 @@ before() { >&2 echo "Blocking wireless..." echo 0 >/sys/class/rfkill/rfkill0/state || true -} - -after() { - >&2 echo "Resumed from suspend." - - >&2 echo "Restoring mixer state..." - alsactl --file "$asound_state_dir/asound.state.post" store || true - alsactl --file "$asound_state_dir/asound.state.pre" restore || true - >&2 echo "Unblocking wireless..." - echo 1 >/sys/class/rfkill/rfkill0/state || true - - if [ -n "$wpa_running" ]; then - >&2 echo "Starting wpa_supplicant..." - wpa_supplicant -B -iwlan0 -Dnl80211 -c/etc/wifi/wpa_supplicant.conf -I/etc/wifi/wpa_supplicant_overlay.conf -O/etc/wifi/sockets || true - udhcpc -i wlan0 & - fi - - if [ -n "$hciattach_running" ]; then - >&2 echo "Starting hciattach..." - /etc/init.d/hciattach start || true - fi - if [ -n "$bluetoothd_running" ]; then - >&2 echo "Starting bluetoothd..." - /etc/bluetooth/bluetoothd start || true - /usr/bin/bluetoothctl power on || true - fi + >&2 echo "Suspending..." + echo mem >/sys/power/state || true } -before - ->&2 echo "Suspending..." -echo mem >/sys/power/state || true - -# Resume services in background to reduce UI latency -after & +main "$@" From c4bca67056cafee74da250ec2c5c7ad56afeb05a Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 11:44:04 +0100 Subject: [PATCH 03/13] fix: remove error ignore from suspend --- skeleton/SYSTEM/tg5040/bin/suspend | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 41039a732..638135d37 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -67,7 +67,7 @@ main() { echo 0 >/sys/class/rfkill/rfkill0/state || true >&2 echo "Suspending..." - echo mem >/sys/power/state || true + echo mem >/sys/power/state } main "$@" From 16703411e126b749c0844693427867675b47d044 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 12:01:07 +0100 Subject: [PATCH 04/13] fix: update trap command --- skeleton/SYSTEM/tg5040/bin/suspend | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 638135d37..5dafb48e2 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -8,8 +8,8 @@ bluetoothd_running= asound_state_dir=/tmp/asound-suspend -restore() { - >&2 echo "Resumed from suspend." +resume() { + >&2 echo "Resuming from suspend..." >&2 echo "Restoring mixer state..." alsactl --file "$asound_state_dir/asound.state.post" store || true @@ -36,7 +36,7 @@ restore() { } main() { - trap "restore &" EXIT INT TERM HUP QUIT + trap "resume" INT TERM HUP QUIT >&2 echo "Preparing for suspend..." @@ -68,6 +68,8 @@ main() { >&2 echo "Suspending..." echo mem >/sys/power/state + + resume & } main "$@" From 62afef754bb42b6506c36940a2e964f554bd2ba7 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 12:13:55 +0100 Subject: [PATCH 05/13] fix: remove unnecessary error handling --- skeleton/SYSTEM/tg5040/bin/suspend | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 5dafb48e2..f65206921 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -42,29 +42,29 @@ main() { >&2 echo "Saving mixer state..." mkdir -p "$asound_state_dir" - alsactl --file "$asound_state_dir/asound.state.pre" store || true + alsactl --file "$asound_state_dir/asound.state.pre" store if pgrep wpa_supplicant; then wpa_running=1 >&2 echo "Stopping wpa_supplicant..." - killall -9 wpa_supplicant || true + killall -9 wpa_supplicant fi - ifconfig wlan0 down || true + ifconfig wlan0 down if pgrep hciattach; then hciattach_running=1 >&2 echo "Stopping hciattach..." - /etc/init.d/hciattach stop || true + /etc/init.d/hciattach stop fi if pgrep bluetoothd; then bluetoothd_running=1 >&2 echo "Stopping bluetoothd..." - /etc/bluetooth/bluetoothd stop || true - killall -15 bluealsa || true + /etc/bluetooth/bluetoothd stop + killall -15 bluealsa fi >&2 echo "Blocking wireless..." - echo 0 >/sys/class/rfkill/rfkill0/state || true + echo 0 >/sys/class/rfkill/rfkill0/state >&2 echo "Suspending..." echo mem >/sys/power/state @@ -72,4 +72,4 @@ main() { resume & } -main "$@" +main From 6f13f0f3d7845114b04d15de052a2af941132840 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 12:59:01 +0100 Subject: [PATCH 06/13] fix: prevent multiple resume calls --- skeleton/SYSTEM/tg5040/bin/suspend | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index f65206921..423b90ccc 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -5,10 +5,15 @@ exec 0<&- wpa_running= hciattach_running= bluetoothd_running= +resume_ran= asound_state_dir=/tmp/asound-suspend resume() { + if [ -n "$resume_ran" ]; then + return + fi + resume_ran=1 >&2 echo "Resuming from suspend..." >&2 echo "Restoring mixer state..." @@ -70,6 +75,7 @@ main() { echo mem >/sys/power/state resume & + exit 0 } main From bd3702f66b1bce288fc2c947d6fb6b84dc0ef174 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 13:05:44 +0100 Subject: [PATCH 07/13] fix: add EXIT signal to resume trap --- skeleton/SYSTEM/tg5040/bin/suspend | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 423b90ccc..92a3ebf12 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -41,7 +41,7 @@ resume() { } main() { - trap "resume" INT TERM HUP QUIT + trap "resume" EXIT INT TERM HUP QUIT >&2 echo "Preparing for suspend..." From b60074eeb6225b8e4b0e1992ed24c70f1d3fb215 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 13:21:10 +0100 Subject: [PATCH 08/13] fix: initialize variables --- skeleton/SYSTEM/tg5040/bin/suspend | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 92a3ebf12..6595a7184 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -2,16 +2,16 @@ set -eu exec 0<&- -wpa_running= -hciattach_running= -bluetoothd_running= -resume_ran= +wpa_running=0 +hciattach_running=0 +bluetoothd_running=0 +resume_ran=0 asound_state_dir=/tmp/asound-suspend resume() { - if [ -n "$resume_ran" ]; then - return + if [ "$resume_ran" -eq 1 ]; then + return fi resume_ran=1 >&2 echo "Resuming from suspend..." @@ -23,17 +23,17 @@ resume() { >&2 echo "Unblocking wireless..." echo 1 >/sys/class/rfkill/rfkill0/state || true - if [ -n "$wpa_running" ]; then + if [ "$wpa_running" -eq 1 ]; then >&2 echo "Starting wpa_supplicant..." wpa_supplicant -B -iwlan0 -Dnl80211 -c/etc/wifi/wpa_supplicant.conf -I/etc/wifi/wpa_supplicant_overlay.conf -O/etc/wifi/sockets || true udhcpc -i wlan0 & fi - if [ -n "$hciattach_running" ]; then + if [ "$hciattach_running" -eq 1 ]; then >&2 echo "Starting hciattach..." /etc/init.d/hciattach start || true fi - if [ -n "$bluetoothd_running" ]; then + if [ "$bluetoothd_running" -eq 1 ]; then >&2 echo "Starting bluetoothd..." /etc/bluetooth/bluetoothd start || true /usr/bin/bluetoothctl power on || true @@ -72,7 +72,7 @@ main() { echo 0 >/sys/class/rfkill/rfkill0/state >&2 echo "Suspending..." - echo mem >/sys/power/state + echo mem >/sys/power/state || true resume & exit 0 From 2acebe2a6411809e709def049651c4cac0f4111d Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 13:26:26 +0100 Subject: [PATCH 09/13] fix: use lock file --- skeleton/SYSTEM/tg5040/bin/suspend | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 6595a7184..eda5cc677 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -5,15 +5,16 @@ exec 0<&- wpa_running=0 hciattach_running=0 bluetoothd_running=0 -resume_ran=0 + +lock_file=/tmp/resume.lock asound_state_dir=/tmp/asound-suspend resume() { - if [ "$resume_ran" -eq 1 ]; then - return + if [ ! -f "$lock_file" ]; then + return fi - resume_ran=1 + rm -f "$lock_file" >&2 echo "Resuming from suspend..." >&2 echo "Restoring mixer state..." @@ -43,6 +44,8 @@ resume() { main() { trap "resume" EXIT INT TERM HUP QUIT + touch "$lock_file" + >&2 echo "Preparing for suspend..." >&2 echo "Saving mixer state..." From 18bdd933057f2c9c3e7641b2547e24179518f819 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 13:37:01 +0100 Subject: [PATCH 10/13] fix: remove trap before exit --- skeleton/SYSTEM/tg5040/bin/suspend | 1 + 1 file changed, 1 insertion(+) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index eda5cc677..1f2b09639 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -78,6 +78,7 @@ main() { echo mem >/sys/power/state || true resume & + trap - EXIT INT TERM HUP QUIT exit 0 } From 1918f4bd18a8002a6d0513926c8a793aff92a82e Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 13:44:27 +0100 Subject: [PATCH 11/13] fix: whitespace --- skeleton/SYSTEM/tg5040/bin/suspend | 118 ++++++++++++++--------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 1f2b09639..b2307bc6b 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -15,71 +15,71 @@ resume() { return fi rm -f "$lock_file" - >&2 echo "Resuming from suspend..." - - >&2 echo "Restoring mixer state..." - alsactl --file "$asound_state_dir/asound.state.post" store || true - alsactl --file "$asound_state_dir/asound.state.pre" restore || true - - >&2 echo "Unblocking wireless..." - echo 1 >/sys/class/rfkill/rfkill0/state || true - - if [ "$wpa_running" -eq 1 ]; then - >&2 echo "Starting wpa_supplicant..." - wpa_supplicant -B -iwlan0 -Dnl80211 -c/etc/wifi/wpa_supplicant.conf -I/etc/wifi/wpa_supplicant_overlay.conf -O/etc/wifi/sockets || true - udhcpc -i wlan0 & - fi - - if [ "$hciattach_running" -eq 1 ]; then - >&2 echo "Starting hciattach..." - /etc/init.d/hciattach start || true - fi - if [ "$bluetoothd_running" -eq 1 ]; then - >&2 echo "Starting bluetoothd..." - /etc/bluetooth/bluetoothd start || true - /usr/bin/bluetoothctl power on || true - fi + >&2 echo "Resuming from suspend..." + + >&2 echo "Restoring mixer state..." + alsactl --file "$asound_state_dir/asound.state.post" store || true + alsactl --file "$asound_state_dir/asound.state.pre" restore || true + + >&2 echo "Unblocking wireless..." + echo 1 >/sys/class/rfkill/rfkill0/state || true + + if [ "$wpa_running" -eq 1 ]; then + >&2 echo "Starting wpa_supplicant..." + wpa_supplicant -B -iwlan0 -Dnl80211 -c/etc/wifi/wpa_supplicant.conf -I/etc/wifi/wpa_supplicant_overlay.conf -O/etc/wifi/sockets || true + udhcpc -i wlan0 & + fi + + if [ "$hciattach_running" -eq 1 ]; then + >&2 echo "Starting hciattach..." + /etc/init.d/hciattach start || true + fi + if [ "$bluetoothd_running" -eq 1 ]; then + >&2 echo "Starting bluetoothd..." + /etc/bluetooth/bluetoothd start || true + /usr/bin/bluetoothctl power on || true + fi } main() { - trap "resume" EXIT INT TERM HUP QUIT + trap "resume" EXIT INT TERM HUP QUIT touch "$lock_file" - >&2 echo "Preparing for suspend..." - - >&2 echo "Saving mixer state..." - mkdir -p "$asound_state_dir" - alsactl --file "$asound_state_dir/asound.state.pre" store - - if pgrep wpa_supplicant; then - wpa_running=1 - >&2 echo "Stopping wpa_supplicant..." - killall -9 wpa_supplicant - fi - ifconfig wlan0 down - - if pgrep hciattach; then - hciattach_running=1 - >&2 echo "Stopping hciattach..." - /etc/init.d/hciattach stop - fi - if pgrep bluetoothd; then - bluetoothd_running=1 - >&2 echo "Stopping bluetoothd..." - /etc/bluetooth/bluetoothd stop - killall -15 bluealsa - fi - - >&2 echo "Blocking wireless..." - echo 0 >/sys/class/rfkill/rfkill0/state - - >&2 echo "Suspending..." - echo mem >/sys/power/state || true - - resume & - trap - EXIT INT TERM HUP QUIT - exit 0 + >&2 echo "Preparing for suspend..." + + >&2 echo "Saving mixer state..." + mkdir -p "$asound_state_dir" + alsactl --file "$asound_state_dir/asound.state.pre" store + + if pgrep wpa_supplicant; then + wpa_running=1 + >&2 echo "Stopping wpa_supplicant..." + killall -9 wpa_supplicant + fi + ifconfig wlan0 down + + if pgrep hciattach; then + hciattach_running=1 + >&2 echo "Stopping hciattach..." + /etc/init.d/hciattach stop + fi + if pgrep bluetoothd; then + bluetoothd_running=1 + >&2 echo "Stopping bluetoothd..." + /etc/bluetooth/bluetoothd stop + killall -15 bluealsa + fi + + >&2 echo "Blocking wireless..." + echo 0 >/sys/class/rfkill/rfkill0/state + + >&2 echo "Suspending..." + echo mem >/sys/power/state || true + + resume & + trap - EXIT INT TERM HUP QUIT + exit 0 } main From 95e22b2bc4f65a3aef41335cac29a4caaf0ce519 Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Wed, 14 May 2025 15:52:21 +0100 Subject: [PATCH 12/13] fix: remove lock file --- skeleton/SYSTEM/tg5040/bin/suspend | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index b2307bc6b..0da057f29 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -6,15 +6,9 @@ wpa_running=0 hciattach_running=0 bluetoothd_running=0 -lock_file=/tmp/resume.lock - asound_state_dir=/tmp/asound-suspend resume() { - if [ ! -f "$lock_file" ]; then - return - fi - rm -f "$lock_file" >&2 echo "Resuming from suspend..." >&2 echo "Restoring mixer state..." @@ -44,8 +38,6 @@ resume() { main() { trap "resume" EXIT INT TERM HUP QUIT - touch "$lock_file" - >&2 echo "Preparing for suspend..." >&2 echo "Saving mixer state..." @@ -77,8 +69,8 @@ main() { >&2 echo "Suspending..." echo mem >/sys/power/state || true - resume & trap - EXIT INT TERM HUP QUIT + resume & # Resume services in background to reduce UI latency exit 0 } From 60c7076279dffc186d9a5fe11037bb83b849032c Mon Sep 17 00:00:00 2001 From: Ben Wadsworth Date: Thu, 15 May 2025 10:02:30 +0100 Subject: [PATCH 13/13] fix: remove audio state management & run resume in forground --- skeleton/SYSTEM/tg5040/bin/suspend | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/skeleton/SYSTEM/tg5040/bin/suspend b/skeleton/SYSTEM/tg5040/bin/suspend index 0da057f29..e1c348596 100755 --- a/skeleton/SYSTEM/tg5040/bin/suspend +++ b/skeleton/SYSTEM/tg5040/bin/suspend @@ -6,15 +6,10 @@ wpa_running=0 hciattach_running=0 bluetoothd_running=0 -asound_state_dir=/tmp/asound-suspend - +# shellcheck disable=SC2317 resume() { >&2 echo "Resuming from suspend..." - >&2 echo "Restoring mixer state..." - alsactl --file "$asound_state_dir/asound.state.post" store || true - alsactl --file "$asound_state_dir/asound.state.pre" restore || true - >&2 echo "Unblocking wireless..." echo 1 >/sys/class/rfkill/rfkill0/state || true @@ -40,10 +35,6 @@ main() { >&2 echo "Preparing for suspend..." - >&2 echo "Saving mixer state..." - mkdir -p "$asound_state_dir" - alsactl --file "$asound_state_dir/asound.state.pre" store - if pgrep wpa_supplicant; then wpa_running=1 >&2 echo "Stopping wpa_supplicant..." @@ -67,10 +58,8 @@ main() { echo 0 >/sys/class/rfkill/rfkill0/state >&2 echo "Suspending..." - echo mem >/sys/power/state || true + echo mem >/sys/power/state - trap - EXIT INT TERM HUP QUIT - resume & # Resume services in background to reduce UI latency exit 0 }