-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscript.sh
196 lines (166 loc) · 6.28 KB
/
script.sh
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
#!/bin/sh
# load config
if [ -e "config.sh" ]; then
source ./config.sh
else
logger "Could not find config.sh in $(pwd)"
echo "Could not find config.sh in $(pwd)"
exit
fi
# load utils
if [ -e "utils.sh" ]; then
source ./utils.sh
else
logger "Could not find utils.sh in $(pwd)"
echo "Could not find utils.sh in $(pwd)"
exit
fi
kill_kindle
customize_kindle
GLOBAL_ERROR_COUNT=0
while true; do
echo "Starting new loop"
logger "START NEW LOOP"
logger "Set CPU scaling governer to powersave"
echo powersave >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
logger "Set prevent screen saver to true"
lipc-set-prop com.lab126.powerd preventScreenSaver 1
echo "Check battery level"
CHARGING_FILE=`kdb get system/driver/charger/SYS_CHARGING_FILE`
IS_CHARGING=$(cat $CHARGING_FILE)
CHECKBATTERY=$(gasgauge-info -s | sed 's/.$//')
CHECKCHARGECURRENT=$(gasgauge-info -l | sed 's/mA//g')
logger "Battery: isCharging=${IS_CHARGING} percentage=${CHECKBATTERY}% current=${CHECKCHARGECURRENT}mA"
if [ ${IS_CHARGING} -eq 1 ] && [ ${CHECKBATTERY} -le ${RESTART_POWERD_THRESHOLD} ] && [ ${CHECKCHARGECURRENT} -le 0 ]; then
logger "Restarting powerd"
/etc/init.d/powerd restart
fi
if [ ${CHECKBATTERY} -le ${BATTERYLOW} ]; then
logger "Battery below ${BATTERYLOW}"
eips -f -g "${LIMGBATT}"
./rtcwake -d rtc$RTC -s $BATTERYSLEEP -m mem
sleep 30 # waiting time when charging until battery level is higher than "BATTERYLOW" otherwise it will fall into sleep again
else
logger "Remaining battery ${CHECKBATTERY}"
fi
### activate wifi
logger "Enabling and checking wifi"
lipc-set-prop com.lab126.wifid enable 1
echo "Check wifi connection"
WLANNOTCONNECTED=0
WLANCOUNTER=0
PINGNOTWORKING=0
PINGCOUNTER=0
ERROR_SUSPEND=0
### wait for wifi
while wait_wlan; do
if [ ${WLANCOUNTER} -gt 5 ]; then
logger "Trying Wifi reconnect"
/usr/bin/wpa_cli -i $NET reconnect
fi
if [ ${WLANCOUNTER} -gt 30 ]; then
logger "No known wifi found"
logger "DEBUG ifconfig $(ifconfig ${NET})"
logger "DEBUG cmState $(lipc-get-prop com.lab126.wifid cmState)"
logger "DEBUG signalStrength $(lipc-get-prop com.lab126.wifid signalStrength)"
eips -f -g "${LIMGERRWIFI}"
WLANNOTCONNECTED=1
ERROR_SUSPEND=1 #short sleeptime will be activated
break 1
fi
let WLANCOUNTER=WLANCOUNTER+1
logger "Waiting for wifi ${WLANCOUNTER}"
sleep $WLANCOUNTER
done
if [ ${WLANNOTCONNECTED} -eq 0 ]; then
logger "Connected to wifi"
### lost standard gateway if wifi is not available
GATEWAY=$(ip route | grep default | grep ${NET} | awk '{print $3}')
logger "Found default gateway ${GATEWAY}"
if [ -z "${GATEWAY}" ]; then
route add default gw ${ROUTERIP}
logger "Default gateway lost after sleep"
logger "Setting default gateway to ${ROUTERIP}"
fi
echo "ping"
### wait for working ping
while wait_ping; do
if [ ${PINGCOUNTER} -gt 5 ]; then
logger "Trying Wifi reconnect"
/usr/bin/wpa_cli -i $NET reconnect
fi
if [ ${PINGCOUNTER} -gt 10 ]; then
logger "Ping not working"
logger "DEBUG ifconfig $(ifconfig ${NET})"
CMSTATE=$(lipc-get-prop com.lab126.wifid cmState)
logger "DEBUG cmState ${CMSTATE}"
logger "DEBUG signalStrength $(lipc-get-prop com.lab126.wifid signalStrength)"
eips -f -g "${LIMGERRWIFI}"
PINGNOTWORKING=1
ERROR_SUSPEND=1 #short sleeptime will be activated
break 1
fi
let PINGCOUNTER=PINGCOUNTER+1
logger "Waiting for working ping ${PINGCOUNTER}"
logger "Trying to set route gateway to ${ROUTERIP}"
route add default gw ${ROUTERIP}
sleep $PINGCOUNTER
done
if [ ${PINGNOTWORKING} -eq 0 ]; then
logger "Ping worked successfully"
echo "Downloading and drawing image"
DOWNLOADRESULT=$(wget -q "$IMAGE_URI" -O $TMPFILE)
logger "Download result ${DOWNLOADRESULT}"
echo $DOWNLOADRESULT
if $DOWNLOADRESULT; then
mv $TMPFILE $SCREENSAVERFILE
logger "Screen saver image file updated"
if [ ${CLEAR_SCREEN_BEFORE_RENDER} -eq 1 ]; then
eips -c
sleep 1
fi
eips -f -g ${SCREENSAVERFILE}
else
logger "Error updating screensaver"
if [ ${CLEAR_SCREEN_BEFORE_RENDER} -eq 1 ]; then
eips -c
sleep 1
fi
eips -f -g ${LIMGERR} #show error picture
ERROR_SUSPEND=1 #short sleep time will be activated
fi
rm ${TMPFILE} -f
logger "Removed temporary files"
if [ ${CHECKBATTERY} -le ${BATTERYALERT} ]; then
eips 2 2 -h " Battery at ${CHECKBATTERY}%, please charge "
fi
fi
fi
sleep $DELAY_BEFORE_SUSPEND
echo "Calculate next timer and going to sleep"
if [ ${ERROR_SUSPEND} -eq 1 ]; then
let GLOBAL_ERROR_COUNT=GLOBAL_ERROR_COUNT+1
TODAY=$(date +%s)
WAKEUPTIME=$((${TODAY} + ${INTERVAL_ON_ERROR} - ${DELAY_BEFORE_SUSPEND}))
logger "An error has occurred, will try again on ${WAKEUPTIME}"
if [ ${GLOBAL_ERROR_COUNT} -ge 10 ]; then
logger "REBOOT BECAUSE OF 10 ERRORS"
reboot
fi
if [ ${USE_RTC} -eq 1 ]; then
./rtcwake -d rtc$RTC -s $INTERVAL_ON_ERROR -m mem
else
sleep $INTERVAL_ON_ERROR
fi
else
GLOBAL_ERROR_COUNT=0
TODAY=$(date +%s)
WAKEUPTIME=$((${TODAY} + ${INTERVAL} - ${DELAY_BEFORE_SUSPEND}))
logger "SUCCESS, will update again on ${WAKEUPTIME}"
if [ ${USE_RTC} -eq 1 ]; then
./rtcwake -d rtc$RTC -s $INTERVAL -m mem
else
sleep $INTERVAL
fi
fi
done