ad28a0b0d4ad810a8c6e994e158e552850587699
[feed/packages.git] / net / dcwapd / files / dcwapd.init
1 #!/bin/sh /etc/rc.common
2
3 USE_PROCD=1
4
5 START=99
6 STOP=01
7
8 CONFIGURATION=dcwapd
9 VERBOSE=1
10 # NOTE: all functions write the result to the $result variable
11 result=
12
13 get_channelsets()
14 {
15 # default to empty
16 result=
17 channelsets=$(uci show $CONFIGURATION | grep "=channel-set$")
18 for channelset in $channelsets; do
19 channelset=$(echo "$channelset" | sed -rn "s/$CONFIGURATION\.(.*)=.*/\1/p")
20 result="$result $channelset"
21 done
22 if [ $VERBOSE -eq 1 ]; then
23 echo "Channel Sets: $result" 2>&1 | logger
24 fi
25 }
26
27 # $1 : the channel set name
28 get_channelset_enabled()
29 {
30 # default to disabled
31 result=0
32 if [ -n "$1" ]; then
33 result=$(uci get $CONFIGURATION."$1".enabled)
34 fi
35 if [ $VERBOSE -eq 1 ]; then
36 echo "Channel Set \"$1\" Enabled: $result" 2>&1 | logger
37 fi
38 }
39
40 # $1 : the channel set name
41 get_primary_bridge()
42 {
43 result=
44 if [ -n "$1" ]; then
45 result=$(uci get $CONFIGURATION."$1".bridge)
46 fi
47 if [ $VERBOSE -eq 1 ]; then
48 echo "Channel Set \"$1\" Primary Bridge: $result" 2>&1 | logger
49 fi
50 }
51
52 # $1 : the channel set name
53 get_datachannels()
54 {
55 # default to empty
56 result=
57 if [ -n "$1" ]; then
58 result=$(uci get $CONFIGURATION."$1".data_channels)
59 fi
60 if [ $VERBOSE -eq 1 ]; then
61 echo "Channel Set \"$1\" Data Channels: $result" 2>&1 | logger
62 fi
63 }
64
65 # $1 : the wlan interface name
66 get_wifi_iface_num()
67 {
68 result=
69 if [ -n "$1" ];then
70 #result=$(echo "$1" | sed -n "s/wlan//p")
71 result=$(echo "$1" | sed -rn "s/wlan([0-9]*).*/\1/p")
72 fi
73 }
74
75 # $1 : the bridge name
76 get_bridge_network_name()
77 {
78 result=
79 if [ -n "$1" ];then
80 result=$(echo "$1" | sed -n "s/br-//p")
81 fi
82 }
83
84 # $1 : the wlan interface name
85 set_iface_init_state()
86 {
87 result=
88 if [ -n "$1" ]; then
89 iface=$1
90 # need to extract the "X" from wlanX
91 get_wifi_iface_num "$iface"
92 iface_num=$result
93 if [ -n "$iface_num" ]; then
94 # get the iface network
95 init_net=$(uci get wireless.@wifi-iface[$iface_num].network)
96 if [ -n "$init_net" ]; then
97 # if the iface network is a bridge, but doesn't start with "br-"
98 # I think we need to prepend it?
99 net_type=$(uci get network."$init_net".type)
100 if [ -n "$net_type" ] && [ "$net_type" = "bridge" ]; then
101 prefix_ok=$(echo "$init_net" | grep "^br-")
102 if [ -z "$prefix_ok" ]; then
103 init_net="br-$init_net"
104 fi
105 fi
106 fi
107
108 # make sure that the init_net section exists
109 init_net_section=$(uci get dcwapd.init_net)
110 if [ "$init_net_section" != "init_net" ]; then
111 # the section did not exist
112 uci set dcwapd.init_net=init_net
113 fi
114
115 # save the initial network
116 if [ $VERBOSE -eq 1 ]; then
117 echo "Saving '$iface' initial network '$init_net'" 2>&1 | logger
118 fi
119 uci set $CONFIGURATION.init_net."$iface"="$init_net"
120 uci commit
121
122 # save the initial network in the result variable
123 result=$init_net
124 fi
125 fi
126 }
127
128 # $1 : the wlan interface name
129 get_iface_init_state()
130 {
131 result=
132 if [ -n "$1" ];then
133 init_net=$(uci get $CONFIGURATION.init_net."$iface")
134
135 # if the response starts with "uci: ", it was an error not the real result
136 err=$(echo "$init_net" | grep "^uci: ")
137 if [ -z "$err" ]; then
138 # no error, set the result
139 result=$init_net
140
141 if [ $VERBOSE -eq 1 ]; then
142 echo "Got '$iface' initial network '$init_net'" 2>&1 | logger
143 fi
144 fi
145 fi
146 }
147
148 # $1 : the name of the data channel name to bring up
149 datachannel_up()
150 {
151 if [ -n "$1" ]; then
152 bridge=$(uci get $CONFIGURATION."$1".bridge)
153 interfaces=$(uci get $CONFIGURATION."$1".interfaces)
154 if [ $VERBOSE -eq 1 ]; then
155 echo "Creating Data Channel Bridge: $bridge" 2>&1 | logger
156 fi
157
158 get_bridge_network_name "$bridge"
159 netname=$result
160 if [ -n "$netname" ]; then
161 uci set network."$netname"=interface
162 uci set network."$netname".type=bridge
163 uci set network."$netname".proto=static
164 uci set network."$netname".bridge_empty='1'
165 fi
166
167 # create the bridge
168 uci commit
169 /etc/init.d/network reload
170
171 for iface in $interfaces; do
172 # if iface is in a bridge, the bridge name should be stored in result
173 set_iface_init_state "$iface"
174 init_bridge=$result
175
176 # update uci with the new bridge info
177 get_wifi_iface_num "$iface"
178 iface_num=$result
179 if [ -n "$iface_num" ]; then
180 uci set wireless.@wifi-iface[$iface_num].network="$netname"
181 fi
182
183 # manually put the interface into the data bridge
184 # if iface is in a bridge, remove it before adding it to the data bridge
185 if [ -n "$init_bridge" ]; then
186 brctl delif "$init_bridge" "$iface" 2>&1 | logger
187 fi
188 brctl addif "$bridge" "$iface" 2>&1 | logger
189 done
190
191 # commit uci changes and reload the network
192 uci commit
193 /etc/init.d/network reload
194 #/etc/init.d/network restart
195 # while [ 1 ]; do
196 # ifconfig "$bridge" > /dev/null 2>&1
197 # if [ $? == 0 ]; then
198 # break;
199 # fi
200 # sleep 1
201 # done
202 fi
203 }
204
205 # $1 : the name of the data channel to bring down
206 datachannel_down()
207 {
208 if [ -n "$1" ]; then
209 bridge=$(uci get $CONFIGURATION."$1".bridge)
210 interfaces=$(uci get $CONFIGURATION."$1".interfaces)
211 for iface in $interfaces; do
212 if [ $VERBOSE -eq 1 ]; then
213 echo "Deconfiguring Data Channel Interface: $iface" 2>&1 | logger
214 fi
215
216 # manually remove the interface from the data bridge
217 brctl delif "$bridge" "$iface" 2>&1 | logger
218
219 get_iface_init_state "$iface"
220 init_bridge=$result
221 if [ -n "$init_bridge" ]; then
222 # manually move the interface back to the original bridge
223 brctl addif "$init_bridge" "$iface" 2>&1 | logger
224
225 # update uci with the new bridge and interface configuration
226 get_wifi_iface_num "$iface"
227 iface_num=$result
228 get_bridge_network_name "$init_bridge"
229 netname=$result
230 if [ -n "$iface_num" ] && [ -n "$netname" ]; then
231 uci set wireless.@wifi-iface[$iface_num].network="$netname"
232 fi
233 fi
234 done
235 if [ $VERBOSE -eq 1 ]; then
236 echo "Deconfiguring Data Channel Bridge: $bridge" 2>&1 | logger
237 fi
238
239 # delete the bridge from uci
240 get_bridge_network_name "$bridge"
241 netname=$result
242 if [ -n "$netname" ]; then
243 uci delete network."$netname"
244 fi
245
246 # commit uci changes and reload the network
247 uci commit
248 /etc/init.d/network reload
249 #`/etc/init.d/network restart`
250 fi
251 }
252
253 start_service() {
254 config_load "$CONFIGURATION"
255 local enabled
256
257 config_get enabled general enabled
258 if [ "$enabled" != "1" ]; then
259 echo "dcwapd is disabled in UCI"
260 return 1
261 fi
262
263 get_channelsets
264 # get the list of channel sets
265 channelsets=$result
266
267 for channelset in $channelsets; do
268 if [ -n "$channelset" ]; then
269 get_channelset_enabled "$channelset"
270 enabled=$result
271 if [ "$enabled" = "1" ]; then
272 # the channel set is enabled
273
274 # get the list of data channels used by the channel set
275 get_datachannels "$channelset"
276 datachannels=$result
277 for datachannel in $datachannels; do
278 datachannel_up "$datachannel"
279 done
280 fi
281 fi
282 done
283
284 procd_open_instance
285 procd_set_param file /etc/config/dcwapd
286 procd_set_param command dcwapd
287 procd_set_param stdout 1
288 procd_set_param stderr 1
289 procd_close_instance
290 }
291
292 stop_service() {
293 get_channelsets
294 # get the list of channel sets
295 channelsets=$result
296
297 for channelset in $channelsets; do
298 if [ -n "$channelset" ]; then
299 # we don't care if it is enabled, tear it down
300 # get_channelset_enabled $channelset
301 # enabled=$result
302 # if [ $enabled = "1" ]; then
303 # # the channel set is enabled
304
305 # get the list of data channels used by the channel set
306 get_datachannels "$channelset"
307 datachannels=$result
308 for datachannel in $datachannels; do
309 datachannel_down "$datachannel"
310 done
311 # fi
312 fi
313 done
314
315 sleep 1
316 }
317
318 service_triggers()
319 {
320 procd_add_reload_trigger dcwapd
321 }
322
323 reload_service() {
324 stop
325 start
326 }