-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerblock
81 lines (64 loc) · 1.73 KB
/
powerblock
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
#!/system/bin/sh
### BEGIN INIT INFO
# Exec: ready
# Process: new
### END INIT INFO
VERSION_NUMBER="1.2.0"
# Common path for all GPIO access
BASE_GPIO_PATH=/sys/class/gpio
# Assign names to GPIO pin numbers
STATUS_PIN=17
SHUTDOWN_PIN=18
# Assign names to states
STATE_ON="1"
STATE_OFF="0"
doRun=$STATE_ON
# Exports a pin if not already exported
# Parameter $1: pin number
exportPin() {
if [ ! -e "$BASE_GPIO_PATH/gpio$1" ]; then
echo "$1" > "$BASE_GPIO_PATH/export"
fi
}
# Set a pin as an output
# Parameter $1: pin number to be set as output pin
setDirectionOutput() {
echo "out" > "$BASE_GPIO_PATH/gpio$1/direction"
}
# Set a pin as an output
# Parameter $1: pin number to be set as output pin
setDirectionInput() {
echo "in" > "$BASE_GPIO_PATH/gpio$1/direction"
}
# Set value of a pin
# Parameter $1: pin number
# Parameter $2: output value. Can be 0 or 1
setPinValue() {
echo "$2" > "$BASE_GPIO_PATH/gpio$1/value"
}
# Returns the current pin value
# Parameter $1: pin number to be read from
getPinValue() {
cat "$BASE_GPIO_PATH/gpio$1/value"
}
doShutdown() {
reboot -p # Android call for shutting down the system
doRun=$STATE_OFF
}
exportPin "$STATUS_PIN"
exportPin "$SHUTDOWN_PIN"
setDirectionOutput "$STATUS_PIN"
setDirectionInput "$SHUTDOWN_PIN"
setPinValue "$STATUS_PIN" "$STATE_ON"
echo "[PowerBlock] Service started, version $VERSION_NUMBER"
log -p i -t "PowerBlock" "Service started, version $VERSION_NUMBER"
# Poll shutdown signal once per second
while [ "$doRun" -eq "$STATE_ON" ]; do
sleep 1
currentValue=$(getPinValue "$SHUTDOWN_PIN")
if [ "$currentValue" -eq "$STATE_ON" ]; then
echo "[PowerBlock] Shutdown signal detected."
log -p i -t "PowerBlock" "Shutdown signal detected."
doShutdown
fi
done