forked from gebhardm/flmlocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flukso
executable file
·146 lines (127 loc) · 3.33 KB
/
flukso
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
#!/bin/sh /etc/rc.common
# Copyright (c) 2017 Bart Van Der Meerssche <bart@flukso.net>
START=99
SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1
set_version()
{
VERSION=$(opkg info flukso | grep -o -e '[0-9]*\.[0-9]*\.[0-9]*' | sed 's/\.//g')
uci set system.@system[0].version=$VERSION
uci commit
logger 'copying flukso package version into system uci file at firstboot'
}
load_config3()
{
MODEL=$(hexdump -v -e '"%.6s"' -s $((0x00)) -n 6 /dev/mtd4)
SERIAL=$(hexdump -v -e '"%.10s"' -s $((0x06)) -n 10 /dev/mtd4)
KEY=$(hexdump -v -e '1/1 "%.2x"' -s $((0x10)) -n 16 /dev/mtd4)
DEVICE=$(hexdump -v -e '1/1 "%.2x"' -s $((0x30)) -n 16 /dev/mtd4)
HOSTNAME=flukso-$(hexdump -v -e '1/1 "%.2x"' -s $((0x30)) -n 3 /dev/mtd4)
uci set system.@system[0].model=$MODEL
uci set system.@system[0].serial=$SERIAL
uci set system.@system[0].key=$KEY
uci set system.@system[0].device=$DEVICE
uci set system.@system[0].hostname=$HOSTNAME
uci set network.wan.hostname=$HOSTNAME
uci set flukso.main=settings
for i in `seq 128`;
do
ADDRESS=$((0x30 + i * 0x10))
ID=$(hexdump -v -e '1/1 "%.2x"' -s $ADDRESS -n 16 /dev/mtd4)
uci set flukso.$i=sensor
uci set flukso.$i.id=$ID
uci set flukso.main.max_provisioned_sensors=$i
done
if [ $(uci get kube.main.collect_group) == 0 ]
then
# GRP will be in the range [1..211]
GRP=$(($(hexdump -v -e '1/1 "%d"' -s $((0x30)) -n 1 /dev/mtd4) % 210 + 1))
uci set kube.main.collect_group=$GRP
fi
KUBE_KEY=$(hexdump -v -e '1/1 "%.2x"' -s $((0x20)) -n 16 /dev/mtd4)
uci set kube.main.key=$KUBE_KEY
uci commit
logger 'configuring flukso device and sensor ids at firstboot'
}
program_sensor_board()
{
DEV=/dev/ttyATH0
FIRMWARE=/usr/bin/flx/e/firmware.bin
OUT=/tmp/stm32flash.out
echo out > /sys/class/gpio/gpio20/direction
for i in `seq 10`;
do
stm32flash -b 38400 -i 24,-20,20:-24,-20,20 -R -v -w $FIRMWARE $DEV &> $OUT
[ $? = 0 ] && break
done
echo in > /sys/class/gpio/gpio20/direction
logger 'flashing the flx at firstboot'
}
start_fl()
{
FIRSTBOOT=$(uci get system.@system[0].firstboot)
if [ $FIRSTBOOT -eq '1' ]
then
program_sensor_board
fi
MODEL=$(uci get system.@system[0].model)
chown -R flukso:flukso /flukso
chmod 666 /sys/class/leds/globe/trigger
/usr/sbin/supd
service_start /usr/sbin/flxd
/usr/sbin/tmpod -u flukso
/usr/sbin/kubed -u flukso
# start the tmpo query daemon
/usr/sbin/queryd -u flukso
sleep 2
if [ $FIRSTBOOT -eq '1' ]
then
uci set system.@system[0].firstboot='0'
uci commit
fi
HEARTBEAT=/usr/bin/heartbeat
FCHECK=/usr/bin/fcheck
FTEST=/usr/bin/ftest
HOUR=$(date '+%H')
MINUTE=$(date '+%M')
CRON=/tmp/cron
# Tell the server we've had a reset
$HEARTBEAT 1 >&- 2>&- <&-
# Configure cron
echo "$MINUTE * * * * $HEARTBEAT 0" > $CRON
echo "*/15 * * * * $FCHECK" >> $CRON
echo "$((++MINUTE%60)) $HOUR * * * $FTEST" >> $CRON
crontab $CRON
# Making sure all committed uci changes are written to flash
sync
}
stop_fl()
{
crontab -r
/usr/sbin/kubed -k
rm -rf /var/run/kubed
/usr/sbin/tmpod -k
rm -rf /var/run/tmpod
service_stop /usr/sbin/flxd
/usr/sbin/supd -k
rm -rf /var/run/supd
#stop the query daemon
/usr/sbin/queryd -k
rm -rf /var/run/queryd
}
start()
{
FIRSTBOOT=$(uci get system.@system[0].firstboot)
if [ $FIRSTBOOT -eq '1' ]
then
set_version
load_config3
chown -R flukso:flukso /flukso
fi
echo "flm03" > /tmp/sysinfo/board_name
start_fl
}
stop()
{
stop_fl
}