Developing lightweight computation at the DSG edge

qmp_wireless.sh 22 KB
Newer Older
1
2
3
4
5
6
#!/bin/sh
##############################
# Global variables definition
##############################
QMP_PATH="/etc/qmp"
OWRT_WIRELESS_CONFIG="/etc/config/wireless"
p4u's avatar
p4u committed
7
8
TEMPLATE_BASE="$QMP_PATH/templates/wifi"
WIFI_DEFAULT_CONFIG="$QMP_PATH/templates/wifi/wireless.default.config"
9
10
11
12
13
14
15
16
17
TMP="/tmp"
QMPINFO="/etc/qmp/qmpinfo"

#######################
# Importing files
######################
SOURCE_WIRELESS=1
. $QMP_PATH/qmp_common.sh
[ -z "$SOURCE_NET" ] && . $QMP_PATH/qmp_network.sh
18
[ -z "$SOURCE_FUNCTIONS" ] && . $QMP_PATH/qmp_functions.sh
19

p4u's avatar
p4u committed
20
21
22
##############################
# Prepare wireless interface
#############################
23
# Delete the wifi-iface interface in /etc/config/wireless and create an empty one
p4u's avatar
p4u committed
24
25
26
27
28
# First parameter: device
qmp_prepare_wireless_iface() {
	local device=$1
	qmp_uci_test wireless.$device && qmp_uci_del_raw wireless.$device
	qmp_uci_set_raw wireless.$device=wifi-iface
29
30
31
32
33
34
35
}

###################################
# Check channel for wifi interface
###################################
# First parameter: device
# Second parameter: channel
36
# Third parameter: mode (adhoc, ap, adhoc_ap, aplan, client, clientwan, 80211s, 80211s_aplan, none)
37
38
39
# It returns the same channel if it is right, and the new one fixet if not

qmp_check_channel() {
p4u's avatar
p4u committed
40
41
42
43
44
45
		local dev="$1"
		local right_channel="$2"
		local channel="$(echo $2 | tr -d b+-)"
		local ht40="$(echo $2 | tr -d b[0-9])"
		local m11b="$(echo $2 | tr -d [0-9]+-)"
		local mode="$3"
46
47
48
		[ ! -z "$channel" ] && chaninfo="$($QMPINFO channels $1 | grep "^$channel ")"

		# Checking if some thing related with channel is wrong
p4u's avatar
p4u committed
49
		local wrong=0
50
		[ -z "$channel" ] || [ -z "$chaninfo" ] && wrong=1
51
52
		[ "$mode" == "80211s" -o "$mode" == "80211s_aplan" ] && [ -z "$(echo $chaninfo | grep 80211s)" ] && wrong=1
		[ "$mode" == "adhoc" -o "$mode" == "adhoc_ap" ] && [ -z "$(echo $chaninfo | grep adhoc)" ] && wrong=1
53
54
		[ "$ht40" == "+" ] && [ -z "$(echo $chaninfo | grep +)" ] && wrong=1
		[ "$ht40" == "-" ] && [ -z "$(echo $chaninfo | grep -)" ] && wrong=1
p4u's avatar
p4u committed
55
		[ "$m11b" == "b" ] && [ $channel -gt 14 ] && wrong=1
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

		# If something wrong, asking for default parameter
		[ $wrong -ne 0 ] && right_channel="$(qmp_wifi_get_default channel $dev $mode)"

		echo "$right_channel"
}

#############################
# Configure driver from wifi
#############################
# This function reload modules from madwifi and mac80211
# Also depending on which driver is configured in config file, modifies the files from /etc/modules.d

qmp_configure_wifi_driver() {
	mac80211_modules="mac80211 ath ath5k ath9k_hw ath9k_common ath9k"
	madwifi_modules="ath_hal ath_ahb ath_pci"

	#Removing all modules
	echo "Removing wifi modules..."
	for m in $(qmp_reverse_order $mac80211_modules); do
		rmmod -f $m 2>/dev/null
	done
	for m in $(qmp_reverse_order $madwifi_modules); do
		rmmod -f $m 2>/dev/null
	done

	rmmod -a

	#Loading driver modules
	echo "Loading wifi modules..."
	driver="$(qmp_uci_get wireless.driver)"
	case $driver in
	"madwifi")
		mv /etc/modules.d/50-madwifi /etc/modules.d/22-madwifi 2>/dev/null
		for m in $madwifi_modules; do
			insmod $m
		done
		;;
	"mac80211")
		mv /etc/modules.d/22-madwifi /etc/modules.d/50-madwifi 2>/dev/null
		for m in $mac80211_modules; do
			insmod $m
		done
		;;
	*)
		qmp_error "Driver $driver not found"
		;;
	esac
}

########################
# Configure wifi device
########################
# Configure a wifi device according qmp config file
# Parameters are: 1-> qmp config id, 2-> device name

qmp_configure_wifi_device() {
	echo ""
	echo "Configuring device $2"

p4u's avatar
p4u committed
116
117
	local id=$1
	local device="$(qmp_uci_get @wireless[$id].device)"
118

119
120
121
122
123
124
125
126
127
128
129
130


	# Remove the wireless device in $device from the previous lan/wan/mesh groups
	# and put it in the appropriate ones according to the selected mode
	#
	# The possible modes are:
	# adhoc_ap ==> Ad hoc (mesh) + access point (LAN)
	# adhoc =====> Ad hoc (mesh)
	# ap ========> Access point (mesh)
	# aplan =====> Access point (LAN)
	# client ====> Client (mesh)
	# clientwan => Client (WAN)
131
132
	# 80211s ====> 802.11s (mesh)
	# 80211s_aplan ====> 802.11s (mesh) + access poin (LAN)
133
	# 80211s_adhoc ====> 802.11s (mesh) + adhoc (mesh)
134

p4u's avatar
p4u committed
135
	local mode="$(qmp_uci_get @wireless[$id].mode)"
136
  local dev_disabled="0"
137
138
139

	# Remove $device and also unneeded white spaces
	local allmeshdevs="$(qmp_uci_get interfaces.mesh_devices)"
140
	local meshdevs="$(echo $allmeshdevs | sed -e s/$device[a-z]*[A-Z]*//g  -e 's/^[ \t]*//' -e 's/ \+/ /g' -e 's/[ \t]*$//')"
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	local allwandevs="$(qmp_uci_get interfaces.wan_devices)"
	local wandevs="$(echo $allwandevs | sed -e s/$device//g  -e 's/^[ \t]*//' -e 's/ \+/ /g' -e 's/[ \t]*$//')"
	local alllandevs="$(qmp_uci_get interfaces.lan_devices)"
	local landevs="$(echo $alllandevs | sed -e s/$device//g  -e 's/^[ \t]*//' -e 's/ \+/ /g' -e 's/[ \t]*$//')"

	case $mode in
		adhoc_ap)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device"
			qmp_uci_set interfaces.lan_devices  "$landevs $device"
			qmp_uci_set interfaces.wan_devices  "$wandevs"
			;;
		adhoc)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device"
			qmp_uci_set interfaces.lan_devices "$landevs"
			qmp_uci_set interfaces.wan_devices "$wandevs"
			;;
157
158
159
160
161
162
163
164
165
166
		80211s)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device"
			qmp_uci_set interfaces.lan_devices "$landevs"
			qmp_uci_set interfaces.wan_devices "$wandevs"
			;;
		80211s_aplan)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device"
			qmp_uci_set interfaces.lan_devices  "$landevs $device"
			qmp_uci_set interfaces.wan_devices  "$wandevs"
			;;
167
168
169
170
171
		80211s_adhoc)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device ${device}a"
			qmp_uci_set interfaces.lan_devices  "$landevs"
			qmp_uci_set interfaces.wan_devices  "$wandevs"
			;;
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
		ap)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device"
			qmp_uci_set interfaces.lan_devices "$landevs"
			qmp_uci_set interfaces.wan_devices "$wandevs"
			;;
		aplan)
			qmp_uci_set interfaces.lan_devices "$landevs $device"
			qmp_uci_set interfaces.mesh_devices "$meshdevs"
			qmp_uci_set interfaces.wan_devices "$wandevs"
			;;
		client)
			qmp_uci_set interfaces.mesh_devices "$meshdevs $device"
			qmp_uci_set interfaces.lan_devices "$landevs"
			qmp_uci_set interfaces.wan_devices "$wandevs"
			;;
		clientwan)
			qmp_uci_set interfaces.mesh_devices "$meshdevs"
			qmp_uci_set interfaces.lan_devices "$landevs"
			qmp_uci_set interfaces.wan_devices "$wandevs $device"
			;;
		none)
			qmp_uci_set interfaces.mesh_devices "$meshdevs"
			qmp_uci_set interfaces.lan_devices "$landevs"
			qmp_uci_set interfaces.wan_devices "$wandevs"
			echo "Interface $device is not managed by the qMp system"
197
      dev_disabled="1"
198
199
			;;
	esac
200
201

	# spliting channel in channel number and ht40 mode
p4u's avatar
p4u committed
202
203
204
205
206
207
208
209
	local channel_raw="$(qmp_uci_get @wireless[$id].channel)"
	local channel="$(echo $channel_raw | tr -d b+-)"

	# htmode and mode selection
	local ht40="$(echo $channel_raw | tr -d [0-9][A-z])"
	local mode11=""
	local htmode=""

210
	[ "$ht40" == "+" -o "$ht40" == "-" ] && {
p4u's avatar
p4u committed
211
212
213
		# Device is selected to use 40MHz channel
		htmode="HT40$ht40"

214
		[ $channel -lt 15 ] && {
215
216
217
218
219
220
221
		# If it is 2.4
			mode11="ng"
		} || {
		# If it is 5
			mode11="na"
		}

222
	} || {
p4u's avatar
p4u committed
223
224
		m11b="$(echo $channel_raw | tr -d [0-9]+-)"
		m11n="$($QMPINFO modes $device | grep -c n)"
225
		[ "$m11b" == "b" ] && {
Pau Escrich's avatar
Pau Escrich committed
226
			# Mode 11b is forced
p4u's avatar
p4u committed
227
228
			htmode=""
			mode11="b"
229
		} || [ $m11n -eq 0 ] && {
Pau Escrich's avatar
Pau Escrich committed
230
231
232
233
			# Device is not 11n compatible
			htmode=""
			mode11="auto"
		} || {
p4u's avatar
p4u committed
234
			# Device is 11n compatible
235
			[ $channel -lt 15 ] && {
236
237
238
239
240
241
242
243
			# If it is 2.4
				htmode="HT20"
				mode11="ng"
			} || {
			# If it is 5
				htmode="HT20"
				mode11="na"
			}
p4u's avatar
p4u committed
244
245
246
247
248
		}
	}

	local mac="$(qmp_uci_get @wireless[$id].mac)"
	local name="$(qmp_uci_get @wireless[$id].name)"
249
250
	local essidap="$(qmp_uci_get @wireless[$id].essidap)"
	local mesh80211s="$(qmp_uci_get @wireless[$id].mesh80211s)"
p4u's avatar
p4u committed
251
252
253
254
255
256
	local driver="$(qmp_uci_get wireless.driver)"
	local country="$(qmp_uci_get wireless.country)"
	local mrate="$(qmp_uci_get wireless.mrate)"
	local bssid="$(qmp_uci_get wireless.bssid)"
	local txpower="$(qmp_uci_get @wireless[$id].txpower)"
	local network="$(qmp_get_virtual_iface $device)"
257
	local key="$(qmp_uci_get @wireless[$id].key)"
258
	[ $(echo "$key" | wc -c) -lt 8 ] && encrypt="none" || encrypt="psk2"
259

p4u's avatar
p4u committed
260
261
262
	local dev_id="$(echo $device | tr -d [A-z])"
	dev_id=${dev_id:-$(date +%S)}
	local radio="radio$dev_id"
263
264

	echo "------------------------"
265
266
267
268
269
270
271
272
273
274
275
276
277
	echo "Device        $device"
	echo "Mac           $mac"
	echo "Mode          $mode"
	echo "Driver        $driver"
	echo "Channel       $channel"
	echo "Country       $country"
	echo "Network       $network"
	echo "AdHoc ESSID   $name"
	echo "AP ESSID      $essidap"
	echo "Mesh network" $mesh80211s
	echo "HTmode        $htmode"
	echo "11mode        $mode11"
	echo "Mrate         $mrate"
278
  echo "Disabled     $dev_disabled"
279
280
	echo "------------------------"

281
282
283
	[ -z $essidap ] && essidap=$(echo ${name:0:29})"-AP"
	[ -z $mesh80211s ] && mesh80211s="qMp"

p4u's avatar
p4u committed
284
	local vap=0
285
	local ahl=0
p4u's avatar
p4u committed
286
287
288
289
	[ $mode == "adhoc_ap" ] && {
		mode="adhoc"
		vap=1
	}
290

291
292
293
294
	[ $mode == "80211s_aplan" ] && {
		mode="80211s"
		vap=1
	}
295

296
297
298
299
300
	[ $mode == "80211s_adhoc" ] && {
		mode="80211s"
		ahl=1
	}

p4u's avatar
p4u committed
301
	device_template="$TEMPLATE_BASE/device.$driver-$mode11"
302
	[ ! -f "$device_template" ] && qmp_error "Device template $device_template not found"
303

304
	cat $device_template | grep -v ^# | grep -v "^list " | sed \
p4u's avatar
p4u committed
305
	 -e s/"#QMP_RADIO"/"$radio"/ \
306
307
308
309
	 -e s/"#QMP_TYPE"/"$driver"/ \
	 -e s/"#QMP_MAC"/"$mac"/ \
	 -e s/"#QMP_CHANNEL"/"$channel"/ \
	 -e s/"#QMP_COUNTRY"/"$country"/ \
Pau Escrich's avatar
Pau Escrich committed
310
	 -e s/"#QMP_MRATE"/"$mrate"/ \
311
	 -e s/"#QMP_HTMODE"/"$htmode"/ \
p4u's avatar
p4u committed
312
	 -e s/"#QMP_TXPOWER"/"$txpower"/ > $TMP/qmp_wifi_device
313
  echo "wireless.$radio.disabled="$dev_disabled >> $TMP/qmp_wifi_device
314

315
316
317
318
319
  [ $mode != "none" ] && {
    echo $device
  iface_template="$TEMPLATE_BASE/iface.$mode"
  [ ! -f "$iface_template" ] && qmp_error "Interface template $iface_template not found"
  echo $device
p4u's avatar
p4u committed
320
321
	qmp_prepare_wireless_iface $device

322
323
324
    echo "Mode is $mode"
    echo "Network is $network"

325
	cat $iface_template | grep -v ^# | sed \
p4u's avatar
p4u committed
326
327
328
	 -e s/"#QMP_RADIO"/"$radio"/ \
	 -e s/"#QMP_DEVICE"/"$device"/ \
	 -e s/"#QMP_IFNAME"/"$device"/ \
329
	 -e s/"#QMP_SSID"/"$(echo "${name:0:32}" | sed -e 's|/|\\/|g')"/ \
330
331
	 -e s/"#QMP_APSSID"/"$(echo "${essidap:0:32}" | sed -e 's|/|\\/|g')"/ \
	 -e s/"#QMP_MSSID"/"$(echo "${mesh80211s:0:32}" | sed -e 's|/|\\/|g')"/ \
p4u's avatar
p4u committed
332
333
334
335
336
337
	 -e s/"#QMP_BSSID"/"$bssid"/ \
	 -e s/"#QMP_NETWORK"/"$network"/ \
	 -e s/"#QMP_ENC"/"$encrypt"/ \
	 -e s/"#QMP_KEY"/"$key"/ \
	 -e s/"#QMP_MODE"/"$mode"/ > $TMP/qmp_wifi_iface

338
}
p4u's avatar
p4u committed
339
340

	# If virtual AP interface has to be configured
341
342
  vap_template="$TEMPLATE_BASE/iface.ap"
  [ ! -f "$vap_template" ] && qmp_error "Virtual AP template $vap_template not found"
p4u's avatar
p4u committed
343
344
	[ "$vap" == "1" ] && {
		qmp_prepare_wireless_iface ${device}ap
345
		cat $vap_template | grep -v ^# | sed \
p4u's avatar
p4u committed
346
347
348
	 	 -e s/"#QMP_RADIO"/"$radio"/ \
		 -e s/"#QMP_DEVICE"/"${device}ap"/ \
		 -e s/"#QMP_IFNAME"/"${device}ap"/ \
349
	 	 -e s/"#QMP_APSSID"/"$(echo "${essidap:0:32}" | sed -e 's|/|\\/|g')"/ \
p4u's avatar
p4u committed
350
351
352
353
354
		 -e s/"#QMP_NETWORK"/"lan"/ \
		 -e s/"#QMP_ENC"/"$encrypt"/ \
		 -e s/"#QMP_KEY"/"$key"/ \
		 -e s/"#QMP_MODE"/"ap"/ >> $TMP/qmp_wifi_iface
	}
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

	# If legacy Ad hoc interface has to be configured
  ahl_template="$TEMPLATE_BASE/iface.adhoclegacy"
  [ ! -f "$ahl_template" ] && qmp_error "Legacy Ad hoc mesh template $ahl_template not found"
	[ "$ahl" == "1" ] && {
		qmp_prepare_wireless_iface ${device}a
		cat $ahl_template | grep -v ^# | sed \
		 -e s/"#QMP_RADIO"/"$radio"/ \
		 -e s/"#QMP_DEVICE"/"${device}a"/ \
		 -e s/"#QMP_IFNAME"/"${device}a"/ \
		 -e s/"#QMP_SSID"/"$(echo "${name:0:32}" | sed -e 's|/|\\/|g')"/ \
		 -e s/"#QMP_BSSID"/"$bssid"/ \
		 -e s/"#QMP_NETWORK"/"${network}a"/ \
		 -e s/"#QMP_MODE"/"adhoc"/ >> $TMP/qmp_wifi_iface
	}

p4u's avatar
p4u committed
371
	qmp_uci_import $TMP/qmp_wifi_iface
372
373
	qmp_uci_import $TMP/qmp_wifi_device
	# List arguments (needed for HT capab)
374
	cat $device_template | grep -v ^# | grep "^list " | sed s/"^list "//g | sed \
375
376
377
378
	 -e s/"#QMP_RADIO"/"$radio"/ | while read l; do
		qmp_uci_add_list_raw $l
	done
	uci reorder wireless.$radio=0
p4u's avatar
p4u committed
379
380
	#uci reorder wireless.@wifi-iface[$index]=16
	uci commit wireless
381
382
383
384
385
386
387
388
389
390
391
392
393
}

#############################
# Configure all wifi devices
#############################
#This function search for all wifi devices and leave them configured according qmp config file

qmp_configure_wifi() {

	echo "Backuping wireless config file to: $OWRT_WIRELESS_CONFIG.qmp_backup"
	cp $OWRT_WIRELESS_CONFIG $OWRT_WIRELESS_CONFIG.qmp_backup 2>/dev/null
	echo "" > $OWRT_WIRELESS_CONFIG

p4u's avatar
p4u committed
394
	local j=0
395

p4u's avatar
p4u committed
396
397
398
	while qmp_uci_test qmp.@wireless[$j]; do
		qmp_configure_wifi_device $j
		j=$(( $j + 1 ))
399
400
401
	done

	echo ""
p4u's avatar
p4u committed
402
	echo "Done: all WiFi devices configured"
403
404
405
406
407
408
409
410
}

####################
# Get default values
####################
# This function returns the default values
#  - first parameter: is always what are you asking for (mode, channel, name,...)
#  - second parameter: is device name, only needed by mode and channel
411
#  - third parameter: is configured mode, only needed by channel
412
413

qmp_wifi_get_default() {
p4u's avatar
p4u committed
414
415
	local what="$1"
	local device="$2"
416

417
418
419
420
421
422
423
424
425
	# WIRELESS MODE
	#
	# The default wireless mode depends on the following criteria:
	#  - Only 1 device:
	#		 · If 5 GHz-capable: 802.11s (mesh) on the 5 GHz band
	#		 · Otherwise: 802.11s (mesh) + AP (LAN) on the 2.4 GHz band
	#  - Two or more devices:
	#    · Depending on the device's capabilities and on the other devices'

426
427
428
429
430
	#  if only 1 bg device = ap
	#  else depending on index

	if [ "$what" == "mode" ]; then

431
432
		# Count the total number of devices, single-band 802.11a/an devices,
		# single-band 802.11b/bg/bgn and dual-band 802.11ab/abg/abgn devices.
p4u's avatar
p4u committed
433
		local devices=0
434
		local a_devices=0
p4u's avatar
p4u committed
435
		local bg_devices=0
436
		local abg_devices=0
437
438
		for wd in $(qmp_get_wifi_devices); do
			devices=$(( $devices + 1 ))
439
440
441
			a_devices=$(( $a_devices + $($QMPINFO modes $wd | egrep "a" | egrep -v "b|g" -c) ))
			bg_devices=$(( $bg_devices + $($QMPINFO modes $wd | egrep -v "a" | egrep "b|g" -c) ))
			abg_devices=$(( $abg_devices + $($QMPINFO modes $wd | egrep "a" | egrep "b|g" -c) ))
442
443
		done

p4u's avatar
p4u committed
444
		local index=$(echo $device | tr -d [A-z])
445

446
447

		# Only one device: if 5 GHz-capable, then 80211s; otherwise 80211s_aplan
448
		if [ $devices -eq 1 ]; then
449
			[ $bg_devices -eq 0 ] && echo "80211s" || echo "80211s_aplan"
450
451
		else

452
453
454
455
456
			# If current device is 5 GHz-capable only, then 80211s
			a_this_device=$($QMPINFO modes $device | egrep "a" | egrep -v "b|g" -c)
			if [ $a_this_device -eq 1 ]; then
				echo "80211s"
			else
457

458
459
460
461
462
				# If current device is 2.4 GHz-capable only => 80211s_aplan
				bg_this_device=$($QMPINFO modes $device | egrep -v "a" | egrep "b|g" -c)
				if [ $bg_this_device -eq 1 ]; then
					echo "80211s_aplan"
				else
463

464
					# If current device is dual-band capable only => depends on the other devices
465

466
467
468
469
					# If there are no other only 5 GHz-capable devices => 80211s
					if [ $a_devices -eq 0 ] && [ $abg_devices -eq 1 ]; then
						echo "80211s"
					else
470

471
472
473
474
						# If there are no other only 2.4 GHz-capable devices => 80211s_aplan
						if [ $bg_devices -eq 0 ] && [ $abg_devices -eq 1 ]; then
							echo "80211s_aplan"
						else
475

476
477
478
479
							# If all the devices are dual-band capable => first device 80211s, the rest 80211s_aplan
							if [ $abg_devices -eq $devices ]; then
								[ $index == 0 ] && echo "80211s" || echo "80211s_aplan"
							else
480

481
482
								# This should never happen
								echo "80211s_aplan"
483
		fi;fi;fi;fi;fi;fi
484

485
486
487
488
	# WIRELESS CHANNEL
	#
	# The default wireless channel depends on the card and on the configured mode:
	#
489
490
491
492
493
	#  Highest channel -> adhoc or not-configured
	#  Lower channel -> ap

	elif [ "$what" == "channel" ]; then
		[ -z "$device" ] && qmp_error "Device not found?"
p4u's avatar
p4u committed
494
		local mode="$3"
495
496

		# we are using index var to put devices in different channels
p4u's avatar
p4u committed
497
		local index=$(echo $device | tr -d [A-z])
498
		index=${index:-0}
499

500
		# QMPINFO returns a list of avaiable channels in this format: 120 ht40+ adhoc 80211s
501
		# this is the command line used to get available channels from a device
p4u's avatar
p4u committed
502
503
		local channels_cmd="$QMPINFO channels $device"
		local num_channels=$($channels_cmd | wc -l)
504

p4u's avatar
p4u committed
505
		# number of channels for AP is 11 or the number of channels available if less
p4u's avatar
p4u committed
506
		local num_channels_ap=$num_channels
p4u's avatar
p4u committed
507
		[ $num_channels_ap -gt 11 ] && num_channels_ap=11
508

p4u's avatar
p4u committed
509
510
511
		# use 40 Mhz of channel size (802.11n)
		local ht40="" # ht40+/ht40-

512
		# channel AdHoc is the last available (qmp_tac = inverse order) plus index*2+1 (1 3 5 ...)
513
		[ "$mode" == "adhoc" ] || [ "$mode" == "80211s" ] || [ -z "$mode" ] && {
p4u's avatar
p4u committed
514
515
516

			#this is global
			ADHOC_INDEX=${ADHOC_INDEX:-0}
517

p4u's avatar
p4u committed
518
			channel_info="$(qmp_tac $channels_cmd | grep adhoc | awk NR==${ADHOC_INDEX}+${ADHOC_INDEX}*2+1)"
519

p4u's avatar
p4u committed
520
			ADHOC_INDEX=$(($ADHOC_INDEX+1))
p4u's avatar
p4u committed
521
			# c is the channel number, checking if it is 802.11bg
p4u's avatar
p4u committed
522
523
524
525
			# in such case it will be 1, 6 and 11 for performance and coexistence with other networks
			c="$(echo $channel_info | cut -d' ' -f1)"
			[ $c -lt 14 ] && {
				qmp_log "Using adhoc device in 802.11bg mode"
526
527
				if [ $c -lt 5 ]; then c=1
				else if [ $c -lt 9 ]; then c=6
p4u's avatar
p4u committed
528
529
530
531
				else c=11
				fi; fi
			ADHOC_BG_USED="$c"
			channel_info="$c $(echo $channel_info | cut -d' ' -f2-)"
p4u's avatar
p4u committed
532
533
534
535

			} || {
				# let's see if we can use ht40 mode
				ht40="$(echo $channel_info | cut -d' ' -f2)"
p4u's avatar
p4u committed
536
			}
537

p4u's avatar
p4u committed
538
		}
539

540
		# channel AP = ( node_id + index*3 ) % ( num_channels_ap) + 1
p4u's avatar
p4u committed
541
		# channel is 1, 6 or 11 for coexistence and performance
542
		[ "$mode" = "ap" -o "$mode" = "adhoc_ap" -o "$mode" = "80211s_aplan" ] && {
543
544
545
546
547
548
549
550
551
552
553
554
555
556

			AP_INDEX=${AP_INDEX:-0}

			# if there is only one wifi device, configure a static channel (it will be used as adhoc_ap)
			if [ $($QMPINFO devices wifi | wc -l) -eq 1 ]; then
				c=1
			else
				c=$(((($(qmp_get_dec_node_id)+$AP_INDEX*3) % $num_channels_ap) +1))
				AP_INDEX=$(($AP_INDEX+1))

				if [ $c -lt 5 ]; then c=1
				else if [ $c -lt 9 ]; then c=6
				else c=11
				fi; fi
557

558
559
560
561
562
				#if the resulting channel is used by adhoc, selecting another one
				[ -n "$ADHOC_BG_USED" ] && [ $ADHOC_BG_USED -eq $c ] && \
				( [ $c -lt 7 ] && c=$(($c+5)) || c=$(($c-5)) )
			fi

p4u's avatar
p4u committed
563
564
			channel_info="$($channels_cmd | awk NR==$c)"
		}
565

566
567
		# if there is some problem, channel 6 is used
		if [ -z "$channel_info" ]; then
568
			qmp_log "Warning, no usable channels found in device $device "
569
570
571
572
573
			[ "$1" == "channel" ] && echo "6"
			return
		fi

		channel="$(echo $channel_info | cut -d' ' -f1)"
574
575
576
577
578
		# Temporary fix for #336
		#[ "$ht40" == "ht40+" ] && channel="${channel}+"
		#[ "$ht40" == "ht40-" ] && channel="${channel}-"
		[ "$ht40" == "ht40+" ] && channel="${channel}"
		[ "$ht40" == "ht40-" ] && channel="${channel}"
579
580
581
582
583
584
585
586
587
588
589

		echo "$channel"

	# REST OF DEFAULT VAULES
	# The rest of default values are taken from the template
	else
		[ ! -f "$WIFI_DEFAULT_CONFIG" ] && qmp_error "Template not found $WIFI_DEFAULT_CONFIG"
		cat $WIFI_DEFAULT_CONFIG | grep $what | cut -d' ' -f2
	fi
}

p4u's avatar
p4u committed
590
591
592
qmp_reset_wifi() {
	#Generating default wifi configuration
	country="$(uci get qmp.wireless.country 2>/dev/null)"
593
	country="${country:-US}"
594

p4u's avatar
p4u committed
595
	mv /etc/config/wireless /tmp/wireless.old
596
597
	wifi config
	sed -i s/"disabled '1'"/"country $country"/g /etc/config/wireless
Roger Pueyo Centelles's avatar
Roger Pueyo Centelles committed
598
	wifi
599
	sleep 5
p4u's avatar
p4u committed
600
601
}

602
603
604
605
606
607
608
qmp_configure_wifi_initial() {

	#First we are going to configure default parameters if they are not present
	[ -z "$(qmp_uci_get wireless)" ] && qmp_uci_set wireless qmp
	[ -z "$(qmp_uci_get wireless.driver)" ] && qmp_uci_set wireless.driver $(qmp_wifi_get_default driver)
	[ -z "$(qmp_uci_get wireless.country)" ] && qmp_uci_set wireless.country $(qmp_wifi_get_default country)
	[ -z "$(qmp_uci_get wireless.bssid)" ] && qmp_uci_set wireless.bssid $(qmp_wifi_get_default bssid)
p4u's avatar
p4u committed
609
	[ -z "$(qmp_uci_get wireless.mrate)" ] && qmp_uci_set wireless.mrate $(qmp_wifi_get_default mrate)
610
611
612
613

	#Changing to configured countrycode
	iw reg set $(qmp_uci_get wireless.country)

614
	macs="$(qmp_get_wifi_mac_devices | uniq)"
615
616
617
618
619
620
621
622
623
624
625
626

	#Looking for configured devices
	id_configured=""
	to_configure=""
	for m in $macs; do
		found=0
		j=0
		while [ ! -z "$(qmp_uci_get @wireless[$j])" ]; do
			configured_mac="$(qmp_uci_get @wireless[$j].mac | tr [A-Z] [a-z])"
			if [ "$configured_mac" == "$m" ]; then
				#If we found configured device, we are going to check all needed parameters
				found=1
627
				device="$(qmp_get_dev_from_wifi_mac $m)"
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
				id_configured="$id_configured $j"
				echo "Found configured device: $m"
			        [ -z "$(qmp_uci_get @wireless[$j].mode)" ] && qmp_uci_set @wireless[$j].mode $(qmp_wifi_get_default mode $device)
        			[ -z "$(qmp_uci_get @wireless[$j].name)" ] && qmp_uci_set @wireless[$j].name $(qmp_wifi_get_default name)
				[ -z "$(qmp_uci_get @wireless[$j].txpower)" ] && qmp_uci_set @wireless[$j].txpower $(qmp_wifi_get_default txpower)

				# If channel is configured, we are going to check it
				# if not, using default one
				sleep 1 && mode="$(qmp_uci_get @wireless[$j].mode)"
				channel="$(qmp_uci_get @wireless[$j].channel)"
				if [ -z "$channel" ]; then
					 qmp_uci_set @wireless[$j].channel $(qmp_wifi_get_default channel $device $mode)

				else
					newchan="$(qmp_check_channel $device $channel $mode)"
					if [ "$newchan" != "$channel" ]; then
						qmp_log Warning: "Channel $channel for device $device in mode $mode is not right, using default one"
						qmp_uci_set @wireless[$j].channel $newchan
					fi
				fi

				qmp_uci_set @wireless[$j].device $device
				break
			fi
			j=$(( $j + 1 ))
		done

		[ $found -eq 0 ] && to_configure="$to_configure $m"
	done

	#Configuring devices not found before
	for m in $to_configure; do
660
		device=$(qmp_get_dev_from_wifi_mac $m)
661
662
663
664
665
666
667
668
669
670
		echo "Configuring device: $device | $m"
		#Looking for a free slot to put new configuration
		j=0
		while [ ! -z "$(echo $id_configured | grep $j)" ]; do j=$(( $j +1 )); done
		#Now we have a free slot, let's go to configure device
		[ -z "$(qmp_uci_get @wireless[$j])" ] && qmp_uci_add wireless
		[ -z "$(qmp_uci_get @wireless[$j].mode)" ] && qmp_uci_set @wireless[$j].mode $(qmp_wifi_get_default mode $device)
		[ -z "$(qmp_uci_get @wireless[$j].name)" ] && qmp_uci_set @wireless[$j].name $(qmp_wifi_get_default name)
		[ -z "$(qmp_uci_get @wireless[$j].txpower)" ] && qmp_uci_set @wireless[$j].txpower $(qmp_wifi_get_default txpower)
		sleep 1 && mode="$(qmp_uci_get @wireless[$j].mode)"
p4u's avatar
p4u committed
671
672
673
		channel="$(qmp_uci_get @wireless[$j].channel)"
		[ -z "$channel" ] && channel=$(qmp_wifi_get_default channel $device $mode)
		qmp_uci_set @wireless[$j].channel "$(qmp_check_channel $device $channel $mode)"
674
675
676
677
678
		qmp_uci_set @wireless[$j].mac $m
		qmp_uci_set @wireless[$j].device $device
		id_configured="$id_configured $j"
	done
}