aboutsummaryrefslogtreecommitdiff
path: root/net/dcwapd/files/dcwapd.init
blob: ad28a0b0d4ad810a8c6e994e158e552850587699 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/sh /etc/rc.common

USE_PROCD=1

START=99
STOP=01

CONFIGURATION=dcwapd
VERBOSE=1
# NOTE: all functions write the result to the $result variable
result=

get_channelsets()
{
	# default to empty
	result=
	channelsets=$(uci show $CONFIGURATION | grep "=channel-set$")
	for channelset in $channelsets; do
		channelset=$(echo "$channelset" | sed -rn "s/$CONFIGURATION\.(.*)=.*/\1/p")
		result="$result $channelset"
	done
	if [ $VERBOSE -eq 1 ]; then
		echo "Channel Sets: $result" 2>&1 | logger
	fi
}

# $1 : the channel set name
get_channelset_enabled()
{
	# default to disabled
	result=0
	if [ -n "$1" ]; then
		result=$(uci get $CONFIGURATION."$1".enabled)
	fi
	if [ $VERBOSE -eq 1 ]; then
		echo "Channel Set \"$1\" Enabled: $result" 2>&1 | logger
	fi
}

# $1 : the channel set name
get_primary_bridge()
{
	result=
	if [ -n "$1" ]; then
		result=$(uci get $CONFIGURATION."$1".bridge)
	fi
	if [ $VERBOSE -eq 1 ]; then
		echo "Channel Set \"$1\" Primary Bridge: $result" 2>&1 | logger
	fi
}

# $1 : the channel set name
get_datachannels()
{
	# default to empty
	result=
	if [ -n "$1" ]; then
		result=$(uci get $CONFIGURATION."$1".data_channels)
	fi
	if [ $VERBOSE -eq 1 ]; then
		echo "Channel Set \"$1\" Data Channels: $result" 2>&1 | logger
	fi
}

# $1 : the wlan interface name
get_wifi_iface_num()
{
	result=
	if [ -n "$1" ];then
		#result=$(echo "$1" | sed -n "s/wlan//p")
		result=$(echo "$1" | sed -rn "s/wlan([0-9]*).*/\1/p")
	fi
}

# $1 : the bridge name
get_bridge_network_name()
{
	result=
	if [ -n "$1" ];then
		result=$(echo "$1" | sed -n "s/br-//p")
	fi
}

# $1 : the wlan interface name
set_iface_init_state()
{
	result=
	if [ -n "$1" ]; then
		iface=$1
		# need to extract the "X" from wlanX
		get_wifi_iface_num "$iface"
		iface_num=$result
		if [ -n "$iface_num" ]; then
			# get the iface network
			init_net=$(uci get wireless.@wifi-iface[$iface_num].network)
			if [ -n "$init_net" ]; then
				# if the iface network is a bridge, but doesn't start with "br-"
				#  I think we need to prepend it?
				net_type=$(uci get network."$init_net".type)
				if [ -n "$net_type" ] && [ "$net_type" = "bridge" ]; then
					prefix_ok=$(echo "$init_net" | grep "^br-")
					if [ -z "$prefix_ok" ]; then
						init_net="br-$init_net"
					fi
				fi
			fi

			# make sure that the init_net section exists
			init_net_section=$(uci get dcwapd.init_net)
			if [ "$init_net_section" != "init_net" ]; then
				# the section did not exist
				uci set dcwapd.init_net=init_net
			fi

			# save the initial network
			if [ $VERBOSE -eq 1 ]; then
				echo "Saving '$iface' initial network '$init_net'" 2>&1 | logger
			fi
			uci set $CONFIGURATION.init_net."$iface"="$init_net"
			uci commit

			# save the initial network in the result variable
			result=$init_net
		fi
	fi
}

# $1 : the wlan interface name
get_iface_init_state()
{
	result=
	if [ -n "$1" ];then
		init_net=$(uci get $CONFIGURATION.init_net."$iface")

		# if the response starts with "uci: ", it was an error not the real result
		err=$(echo "$init_net" | grep "^uci: ")
		if [ -z "$err" ]; then
			# no error, set the result
			result=$init_net

			if [ $VERBOSE -eq 1 ]; then
				echo "Got '$iface' initial network '$init_net'" 2>&1 | logger
			fi
		fi
	fi
}

# $1 : the name of the data channel name to bring up
datachannel_up()
{
	if [ -n "$1" ]; then
		bridge=$(uci get $CONFIGURATION."$1".bridge)
		interfaces=$(uci get $CONFIGURATION."$1".interfaces)
		if [ $VERBOSE -eq 1 ]; then
			echo "Creating Data Channel Bridge: $bridge" 2>&1 | logger
		fi

		get_bridge_network_name "$bridge"
		netname=$result
		if [ -n "$netname" ]; then
			uci set network."$netname"=interface
			uci set network."$netname".type=bridge
			uci set network."$netname".proto=static
			uci set network."$netname".bridge_empty='1'
		fi

		# create the bridge
		uci commit
		/etc/init.d/network reload

		for iface in $interfaces; do
			# if iface is in a bridge, the bridge name should be stored in result
			set_iface_init_state "$iface"
			init_bridge=$result

			# update uci with the new bridge info
			get_wifi_iface_num "$iface"
			iface_num=$result
			if [ -n "$iface_num" ]; then
				uci set wireless.@wifi-iface[$iface_num].network="$netname"
			fi

			# manually put the interface into the data bridge
			# if iface is in a bridge, remove it before adding it to the data bridge
			if [ -n "$init_bridge" ]; then
				brctl delif "$init_bridge" "$iface" 2>&1 | logger
			fi
			brctl addif "$bridge" "$iface" 2>&1 | logger
		done

		# commit uci changes and reload the network
		uci commit
		/etc/init.d/network reload
		#/etc/init.d/network restart
		# while [ 1 ]; do
		# 	ifconfig "$bridge" > /dev/null 2>&1
		# 	if [ $? == 0 ]; then
		# 		break;
		# 	fi
		# 	sleep 1
		# done
	fi
}

# $1 : the name of the data channel to bring down
datachannel_down()
{
	if [ -n "$1" ]; then
		bridge=$(uci get $CONFIGURATION."$1".bridge)
		interfaces=$(uci get $CONFIGURATION."$1".interfaces)
		for iface in $interfaces; do
			if [ $VERBOSE -eq 1 ]; then
				echo "Deconfiguring Data Channel Interface: $iface" 2>&1 | logger
			fi

			# manually remove the interface from the data bridge
			brctl delif "$bridge" "$iface" 2>&1 | logger

			get_iface_init_state "$iface"
			init_bridge=$result
			if [ -n "$init_bridge" ]; then
				# manually move the interface back to the original bridge
				brctl addif "$init_bridge" "$iface" 2>&1 | logger

				# update uci with the new bridge and interface configuration
				get_wifi_iface_num "$iface"
				iface_num=$result
				get_bridge_network_name "$init_bridge"
				netname=$result
				if [ -n "$iface_num" ] && [ -n "$netname" ]; then
					uci set wireless.@wifi-iface[$iface_num].network="$netname"
				fi
			fi
		done
		if [ $VERBOSE -eq 1 ]; then
			echo "Deconfiguring Data Channel Bridge: $bridge" 2>&1 | logger
		fi

		# delete the bridge from uci
		get_bridge_network_name "$bridge"
		netname=$result
		if [ -n "$netname" ]; then
			uci delete network."$netname"
		fi

		# commit uci changes and reload the network
		uci commit
		/etc/init.d/network reload
		#`/etc/init.d/network restart`
	fi
}

start_service() {
	config_load "$CONFIGURATION"
	local enabled

	config_get enabled general enabled
	if [ "$enabled" != "1" ]; then
		echo "dcwapd is disabled in UCI"
		return 1
	fi

	get_channelsets
	# get the list of channel sets
	channelsets=$result

	for channelset in $channelsets; do
	if [ -n "$channelset" ]; then
		get_channelset_enabled "$channelset"
		enabled=$result
		if [ "$enabled" = "1" ]; then
			# the channel set is enabled

			# get the list of data channels used by the channel set
			get_datachannels "$channelset"
			datachannels=$result
			for datachannel in $datachannels; do
				datachannel_up "$datachannel"
			done
		fi
	fi
	done

	procd_open_instance
	procd_set_param file /etc/config/dcwapd
	procd_set_param command dcwapd
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

stop_service() {
	get_channelsets
	# get the list of channel sets
	channelsets=$result

	for channelset in $channelsets; do
	if [ -n "$channelset" ]; then
# we don't care if it is enabled, tear it down
#		get_channelset_enabled $channelset
#		enabled=$result
#		if [ $enabled = "1" ]; then
#			# the channel set is enabled

			# get the list of data channels used by the channel set
			get_datachannels "$channelset"
			datachannels=$result
			for datachannel in $datachannels; do
				datachannel_down "$datachannel"
			done
#		fi
	fi
	done

	sleep 1
}

service_triggers()
{
	procd_add_reload_trigger dcwapd
}

reload_service() {
	stop
	start
}