forked from ioDiken/pionic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpionic.sh
More file actions
executable file
·133 lines (108 loc) · 3.95 KB
/
pionic.sh
File metadata and controls
executable file
·133 lines (108 loc) · 3.95 KB
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
#!/bin/bash -eu
# This starts the test station operation. It runs in background at boot.
# Report errors before exit, this tries to look like the 'unbound variable' error
trap 'echo $0: line $LINENO: exit status $? >&2' ERR
# abort with a message
die() { echo "$0: $*" >&2; exit 1; }
grep -q Raspberry /etc/rpi-issue &>/dev/null || die "Requires a Raspberry Pi"
((UID==0)) || die "Must be run as root"
here=$(realpath ${0%/*})
# Return ip address for interface $1 and true, or false if no IP
ipaddr() { local s=$(ip -4 -o a show dev $1 2>/dev/null | awk '{print $4}'); [[ $s ]] && echo $s; }
# Where to put temp files, cgi/factory also needs to know this
tmp=/tmp/pionic
# curl timeout after 4 seconds, -q=disable curl.config, -s=silent (no status), -S=show error, -f=fail with exit status 22
curl="curl --connect-timeout 2 -qsSf"
# turn console on and off
console()
{
case "$1" in
off)
setterm --cursor off > /dev/tty1
echo 0 > /sys/class/vtconsole/vtcon1/bind
dmesg -n 1
;;
*)
echo 1 > /sys/class/vtconsole/vtcon1/bind
setterm --cursor on > /dev/tty1
;;
esac
true
}
case "${1:-}" in
start)
(
# store the subshell pid
mkdir -p $tmp
echo $BASHPID > $tmp/.pid
# after this point, kill shell children on exit and reinstate console
trap 'exs=$?;
kill $(jobs -p) &>/dev/null && wait $(jobs -p) || true;
console on || true;
exit $exs' EXIT
# wait for network to be up and start daemons
while true; do
wait=0
if ! (($(cat /sys/class/net/eth0/carrier))); then
echo "Ethernet is not attached"
wait=1
elif ! station_ip=$(ipaddr eth0); then
echo "Waiting for station ID, MAC=$(cat /sys/class/net/eth0/address)"
wait=1
else
station=${station_ip##*.}
station=${station%/*}
fi
if ! [ -d /sys/class/net/eth1 ]; then
echo "USB ethernet is not attached"
wait=1
fi
if ! ipaddr br0 &>/dev/null; then
echo "Waiting for br0"
wait=1
fi
((wait)) || break
sleep 1
done
# start daemons
pgrep -f cgiserver &>/dev/null || $here/cgiserver -p 80 -d ~pi/pionic/cgi &
! [ -d $here/beacon ] || pgrep -f beacon &>/dev/null || $here/beacon/beacon send br0 &
# Try to fetch the fixture driver name, note local port 61080 redirects to server port 80
# If we don't get a response then use the default
echo "Requesting fixture from server"
fixture=$($curl "http://localhost:61080/cgi-bin/factory?service=fixture")
fixture=${fixture,,}
[[ $fixture && $fixture != none ]] || fixture=default
echo "Using fixture '$fixture'"
if [ -x $here/fixtures/$fixture ]; then
# use built-in fixture driver
console off
$here/fixtures/$fixture $here $station
else
# otherwise try to download it
rm -rf $tmp/fixtures
mkdir $tmp/fixtures
fixtures="http://localhost:61080/fixtures.tar.gz"
echo "Fetching $fixtures..."
$curl $fixtures | tar -C $tmp/fixtures -xz || die "Fetch failed"
[[ -e $tmp/fixtures/$fixture ]] || die "Fixture driver '$fixture' not found"
console off
$tmp/fixtures/$fixture $here $station
fi
die "Fixture driver '$fixture' exit status $?"
) &
;;
stop)
if [ -e $tmp/.pid ]; then
kill $(cat $tmp/.pid) &>/dev/null || true
rm -f $tmp/.pid
fi
;;
res*)
$0 stop
exec $0 start
;;
*) die "Usage: $0 stop | start | restart"
;;
esac
true