aboutsummaryrefslogtreecommitdiff
path: root/sleepwalk
blob: cdb93cd1e4bac888a9575f3921c770e6f4bbf4e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash

SLEEP_SECS_INIT=30
SLEEP_SECS=$SLEEP_SECS_INIT
SLEEP_STEP=180
SLEEP_MAX_SECS=600
WAKE_SECS=35
LOCK_DIR="/tmp/sleepwalk"
CPU_STATE=1
THRESHOLD="15.00"

set_cpu_offline() {
        if [ $CPU_STATE -eq 1 ]; then
                echo "Powering off all CPU cores except cpu0"
                for i in $(seq 2); do
                        echo 0 > /sys/devices/system/cpu/cpu$i/online
                done
                CPU_STATE=0
        fi
}

set_cpu_online() {
        SLEEP_SECS=$SLEEP_SECS_INIT
        if [ $CPU_STATE -eq 0 ]; then
                echo "Powering on all CPU cores except cpu0"
                for i in $(seq 2); do
                        echo 1 > /sys/devices/system/cpu/cpu$i/online
                done
                CPU_STATE=1
        fi
}

start_deep_sleep() {
        led_sleep

        if [ $((SLEEP_SECS + $SLEEP_STEP)) -lt $SLEEP_MAX_SECS ]; then
                SLEEP_SECS=$((SLEEP_SECS + SLEEP_STEP))
        else
                SLEEP_SECS=$SLEEP_MAX_SECS
        fi

        sleep 5 # Give some extra time for pending output data

        if [ can_suspend ]; then
                echo mem > /sys/power/state 2>/dev/null
        fi

        return $?
}

wait_for_notifications() {
        secs=$WAKE_SECS
        is_led_on=0
        while [ $secs -ge 0 ]; do
                sleep 1
                if [ $is_led_on == 0 ]; then
                        led_wake
                        is_led_on=1
                else
                        led_disable
                        is_led_on=0
                fi

                if is_screen_on; then
			if is_charging; then
				cpufreq-set -g conservative
			else
				cpufreq-set -g powersave
			fi
                        set_cpu_online
                        break
                fi
                set_cpu_offline
                secs=$(($secs-1))
        done
        led_disable
}

schedule_wake_time() {
        echo "Sleeping for ${SLEEP_SECS}s"
        rtcwake -m no --date "+${SLEEP_SECS}s" 2>&1 >/dev/null
}

reset_wake_time() {
        led_wake
        > /sys/class/rtc/rtc0/wakealarm
}

is_charging() {
        [ "$(cat /sys/class/power_supply/axp20x-usb/online)" = "1" ]
}

is_inhibitor_active() {
        # INHIBITORS=$(sudo -u "$(cat /proc/$(pidof -s gnome-session-ctl)/environ | grep -z USER | sed 's,\x0,,;s,USER=,,')" DBUS_SESSION_BUS_ADDRESS=$(cat /proc/$(pidof -s gnome-session-ctl)/environ | grep -z DBUS_SESSION_BUS_ADDRESS | sed "s,\x0,,;s,DBUS_SESSION_BUS_ADDRESS=,,") gnome-session-inhibit --list)
        # printf "%s\n" "$INHIBITORS"
        INHIBITORS=$(systemd-inhibit --list --what=sleep --mode=block --no-legend \
                | grep -e 'sleep')

        [ -n "$INHIBITORS" ]
        # if [ "$INHIBITORS" = "No inhibitors" ]; then
        #       return 1
        # fi

        # return 0
}

is_screen_on() {
        return "$(cat /sys/class/backlight/backlight/bl_power)"
}

network_activity() {
	for n in $(seq 3)
	do
		for i in wwan0 wlan0
		do
			A=$(ifstat -i $i 1 1 | tail -n1)
			RX=$(echo $A | awk '{print $1}')
			TX=$(echo $A | awk '{print $2}')

			if [ "$RX" = "n/a" ] || [ "$TX" = "n/a" ]
			then
				return 1
			fi

			for v in $RX $TX
			do
				C=$(echo $v $THRESHOLD |
					awk '{if ($1 > $2) print "y"; else print "n"}')

				if [ "$C" = "y" ]
				then
					return 0
				fi
			done
		done
	done

	return 1
}

can_suspend() {
        ! ( is_inhibitor_active || is_screen_on || is_charging || network_activity )
}

led_sleep() {
        :
        # echo 1 > /sys/class/leds/green\:indicator/brightness
        # echo 0 > /sys/class/leds/red\:indicator/brightness
        # echo 0 > /sys/class/leds/blue\:indicator/brightness
}

led_wake() {
        :
        # echo 0 > /sys/class/leds/green\:indicator/brightness
        # echo 1 > /sys/class/leds/red\:indicator/brightness
        # echo 0 > /sys/class/leds/blue\:indicator/brightness
}

led_disable() {
        :
        # echo 0 > /sys/class/leds/green\:indicator/brightness
        # echo 0 > /sys/class/leds/red\:indicator/brightness
        # echo 0 > /sys/class/leds/blue\:indicator/brightness
}

if [ $EUID -ne 0 ]; then
        echo "Needs root" >&2
        exit 1
fi

if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
        cat <<-EOF >&2
                Usage: sleepwalk [start/stop]
                Commands:
                         start          Start sleep cycle
                         stop           Stop sleep cycle
EOF
        exit
fi

if [ "$1" == "start" ]; then
        if [ -d "$LOCK_DIR" ] || ! mkdir "$LOCK_DIR"; then
                echo "sleepwalk is already running - run 'sleepwalk stop' to stop it" >&2
                exit 1
        fi
        trap "$0 stop" EXIT

        while [ -d "$LOCK_DIR" ]; do
                while ! can_suspend; do
                        set_cpu_online

			if is_charging && is_screen_on; then
				cpufreq-set -g conservative
			else
				cpufreq-set -g powersave
			fi

                        sleep 10
                done

                reset_wake_time
                schedule_wake_time

                while ! start_deep_sleep; do
                        reset_wake_time
                        echo "Failed going to sleep, try again in $WAKE_SECS seconds ..." >&2
                        wait_for_notifications
                        schedule_wake_time
                        if ! can_suspend; then
                                break
                        fi
                done
                wait_for_notifications
        done
elif [ "$1" == "stop" ]; then
        reset_wake_time
        led_disable
        rmdir "$LOCK_DIR" 2>/dev/null
else
        echo "Unknown command: $1" >&2
        exit 1
fi