-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.sh
executable file
·206 lines (138 loc) · 4.46 KB
/
daemon.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
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
#
#-------------------------------------------------------------------
# file: daemon.sh
# This script should be auto started, to support WittyPi hardware
#------------------------------------------------------------------
#-----------------------
# check if sudo is used
#-----------------------
if [ "$(id -u)" != 0 ]; then
echo 'ERROR: script must be run as root or with sudo'
exit 1
fi
#-----------------------
# get current directory
#-----------------------
cur_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
#----------------
# load utilities
#----------------
. "$cur_dir/utilities.sh"
log 'Witty Pi daemon (v2.56a) is started.'
#-----------------------------
# halt by GPIO-4 (BCM naming)
#-----------------------------
halt_pin=4
#-------------------------------------------------------
# make sure the halt pin is input with internal pull up
#-------------------------------------------------------
gpio -g mode $halt_pin up
gpio -g mode $halt_pin in
#-----------------------------
# LED on GPIO-17 (BCM naming)
#-----------------------------
led_pin=17
#--------------------
# wait for RTC ready
#--------------------
sleep 2
#--------------------------
# is RTC board connected?
#--------------------------
is_rtc_connected
#------------------------------
has_rtc=$? # should be 0 if RTC connected
#------------------------------
if [ $has_rtc == 0 ] ; then
#---------------------------------------
# disable square wave and enable alarms
#---------------------------------------
i2c_write 0x01 0x68 0x0E 0x07
byte_F=$(i2c_read 0x01 0x68 0x0F)
#--------------------------------------------------------
# if woke up by alarm B (shutdown), turn off immediately
#--------------------------------------------------------
if [ $((($byte_F&0x1) == 0)) == '1' ] && [ $((($byte_F&0x2) != 0)) == '1' ] ; then
log 'Unexpectedly woken up by shutdown alarm, going back to sleep'
do_shutdown $halt_pin $led_pin $has_rtc
fi
#-------------------
# clear alarm flags
#-------------------
clear_alarm_flags $byte_F
else
log 'ERROR: Witty Pi is not connected, skipping I2C communications'
fi
#------------------
# synchronize time
#------------------
if [ $has_rtc == 0 ] ; then
"$cur_dir/syncTime.sh" &
else
log 'ERROR: Witty Pi is not connected, skip synchronizing time...'
fi
#-----------------------------
# wait for system time update
#-----------------------------
sleep 3
#---------------------
# run schedule script
#---------------------
if [ $has_rtc == 0 ] ; then
"$cur_dir/runScript.sh" 0 revise >> "$cur_dir/schedule.log" &
else
log 'Witty Pi is not connected, skipping schedule script'
fi
#--------------------------------------
# delay until GPIO pin state is stable
#--------------------------------------
counter=0
#-------------------------------------------
while [ $counter -lt 5 ]; do # increase this value if it needs more time
#-------------------------------------------
if [ $(gpio -g read $halt_pin) == '1' ] ; then
counter=$(($counter+1))
else
counter=0
fi
sleep 1
done
#--------------------------------------
# run extra tasks script in background
#--------------------------------------
"$cur_dir/extraTasks.sh" >> "$cur_dir/wittyPi.log" 2>&1 &
#-------------------------------------------------------------
# wait for GPIO-4 (BCM naming) falling, or alarm B (shutdown)
#-------------------------------------------------------------
log 'Waiting for incoming shutdown command'
while true; do
gpio -g wfi $halt_pin falling
#------------------------------------
# Check exit status of GPIO command
# if there is a problen abort daemon
#------------------------------------
if [ "$?" != 0 ] ; then
log 'ERROR: problem with gpio (have you compiled kernel with CONFIG_GPIO_SYSFS=y?)'
exit 255
fi
if [ $has_rtc == 0 ] ; then
byte_F=$(i2c_read 0x01 0x68 0x0F)
if [ $((($byte_F&0x1) != 0)) == '1' ] && [ $((($byte_F&0x2) == 0)) == '1' ] ; then
#--------------------------------------------------
# alarm A (startup) occurs, clear flags and ignore
#--------------------------------------------------
log 'ERROR: startup in ON state (ignored)'
clear_alarm_flags
else
break;
fi
else
#-----------------------------------------
# power switch can still work without RTC
#-----------------------------------------
break;
fi
done
log 'Shutdown command received'
do_shutdown $halt_pin $led_pin $has_rtc