Developing lightweight computation at the DSG edge

qmp_wireless.sh 17.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
#    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#    The full GNU General Public License is included in this distribution in
#    the file called "COPYING".
20
21
#
# Contributors:
p4u's avatar
p4u committed
22
#   Pau Escrich <p4u@dabax.net>
23
24
#	Simó Albert i Beltran
#
25
26
27
28
29
30

##############################
# Global variables definition
##############################
QMP_PATH="/etc/qmp"
OWRT_WIRELESS_CONFIG="/etc/config/wireless"
p4u's avatar
p4u committed
31
32
TEMPLATE_BASE="$QMP_PATH/templates/wifi"
WIFI_DEFAULT_CONFIG="$QMP_PATH/templates/wifi/wireless.default.config"
33
34
35
36
37
38
39
40
41
TMP="/tmp"
QMPINFO="/etc/qmp/qmpinfo"

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

p4u's avatar
p4u committed
44
45
46
47
48
49
50
51
52
53
##############################
# Prepare wireless interface
#############################
# Prepare de WiFi interfaces
# 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
54
55
56
57
58
59
60
}

###################################
# Check channel for wifi interface
###################################
# First parameter: device
# Second parameter: channel
p4u's avatar
p4u committed
61
# Third parameter: mode (adhoc, ap, adhoc_ap)
62
63
64
# It returns the same channel if it is right, and the new one fixet if not

qmp_check_channel() {
p4u's avatar
p4u committed
65
66
67
68
69
70
		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"
71
72
73
		[ ! -z "$channel" ] && chaninfo="$($QMPINFO channels $1 | grep "^$channel ")"

		# Checking if some thing related with channel is wrong
p4u's avatar
p4u committed
74
		local wrong=0
75
		[ -z "$channel" ] || [ -z "$chaninfo" ] && wrong=1
p4u's avatar
p4u committed
76
		[ "$mode" == "adhoc" -o "$mode" == "adhoc_ap" ] && [ -z "$(echo $chaninfo | grep adhoc)" ] && wrong=1
77
78
		[ "$ht40" == "+" ] && [ -z "$(echo $chaninfo | grep +)" ] && wrong=1
		[ "$ht40" == "-" ] && [ -z "$(echo $chaninfo | grep -)" ] && wrong=1
p4u's avatar
p4u committed
79
		[ "$m11b" == "b" ] && [ $channel -gt 14 ] && wrong=1
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

		# 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
140
141
	local id=$1
	local device="$(qmp_uci_get @wireless[$id].device)"
142
143

	# checking if device is configured as "none"
p4u's avatar
p4u committed
144
145
	local mode="$(qmp_uci_get @wireless[$id].mode)"
	[ "$mode" == "none" ] && { echo "Interface $device is not managed by the qMp system"; return; }
146
147

	# spliting channel in channel number and ht40 mode
p4u's avatar
p4u committed
148
149
150
151
152
153
154
155
	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=""

156
	[ "$ht40" == "+" -o "$ht40" == "-" ] && {
p4u's avatar
p4u committed
157
158
159
		# Device is selected to use 40MHz channel
		htmode="HT40$ht40"

160
161
162
163
164
165
166
167
		[ $channel < 15 ] && {
		# If it is 2.4
			mode11="ng"
		} || {
		# If it is 5
			mode11="na"
		}

p4u's avatar
p4u committed
168
169
170
	} || { 
		m11b="$(echo $channel_raw | tr -d [0-9]+-)"
		m11n="$($QMPINFO modes $device | grep -c n)"
171
		[ "$m11b" == "b" ] && { 
Pau Escrich's avatar
Pau Escrich committed
172
			# Mode 11b is forced
p4u's avatar
p4u committed
173
174
			htmode=""
			mode11="b"
Pau Escrich's avatar
Pau Escrich committed
175
176
177
178
179
		} || [ $m11n -eq 0 ] && { 
			# Device is not 11n compatible
			htmode=""
			mode11="auto"
		} || {
p4u's avatar
p4u committed
180
			# Device is 11n compatible
181
182
183
184
185
186
187
188
189
			[ $channel < 15 ] && {
			# If it is 2.4
				htmode="HT20"
				mode11="ng"
			} || {
			# If it is 5
				htmode="HT20"
				mode11="na"
			}
p4u's avatar
p4u committed
190
191
192
193
194
195
196
197
198
199
200
201
		}
	}

	local mac="$(qmp_uci_get @wireless[$id].mac)"
	local name="$(qmp_uci_get @wireless[$id].name)"
	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)"
	local key="$(qmp_uci_get @wireless[$id].key)"	
202
	[ $(echo "$key" | wc -c) -lt 8 ] && encrypt="none" || encrypt="psk2"
p4u's avatar
p4u committed
203
204
205
206

	local dev_id="$(echo $device | tr -d [A-z])"
	dev_id=${dev_id:-$(date +%S)}
	local radio="radio$dev_id"
207
208

	echo "------------------------"
p4u's avatar
p4u committed
209
	echo "Device   $device"
210
211
212
213
214
215
216
	echo "Mac      $mac"
	echo "Mode     $mode"
	echo "Driver   $driver"
	echo "Channel  $channel"
	echo "Country  $country"
	echo "Network  $network"
	echo "Name     $name"
p4u's avatar
p4u committed
217
218
	echo "HTmode   $htmode"
	echo "11mode   $mode11"
219
	echo "Mrate    $mrate"
220
221
	echo "------------------------"

p4u's avatar
p4u committed
222
223
224
225
226
	local vap=0
	[ $mode == "adhoc_ap" ] && {
		mode="adhoc"
		vap=1
	}
227

p4u's avatar
p4u committed
228
229
230
	device_template="$TEMPLATE_BASE/device.$driver-$mode11"
	iface_template="$TEMPLATE_BASE/iface.$mode" 
	vap_template="$TEMPLATE_BASE/iface.ap"
231

p4u's avatar
p4u committed
232
	[ ! -f "$device_template" ] || [ ! -f "$iface_template" ]  && qmp_error "Template $template not found"
233

p4u's avatar
p4u committed
234
235
	cat $device_template | grep -v "^list " | sed \
	 -e s/"#QMP_RADIO"/"$radio"/ \
236
237
238
239
	 -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
240
	 -e s/"#QMP_MRATE"/"$mrate"/ \
241
	 -e s/"#QMP_HTMODE"/"$htmode"/ \
p4u's avatar
p4u committed
242
	 -e s/"#QMP_TXPOWER"/"$txpower"/ > $TMP/qmp_wifi_device
243

p4u's avatar
p4u committed
244
245
246
247
248
249
	qmp_prepare_wireless_iface $device

	cat $iface_template | sed \
	 -e s/"#QMP_RADIO"/"$radio"/ \
	 -e s/"#QMP_DEVICE"/"$device"/ \
	 -e s/"#QMP_IFNAME"/"$device"/ \
250
	 -e s/"#QMP_SSID"/"$(echo "${name:0:32}" | sed -e 's|/|\\/|g')"/ \
p4u's avatar
p4u committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
	 -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


	# If virtual AP interface has to be configured
	[ "$vap" == "1" ] && {
		qmp_prepare_wireless_iface ${device}ap
		cat $vap_template | sed \
	 	 -e s/"#QMP_RADIO"/"$radio"/ \
		 -e s/"#QMP_DEVICE"/"${device}ap"/ \
		 -e s/"#QMP_IFNAME"/"${device}ap"/ \
Pau Escrich's avatar
Pau Escrich committed
265
		 -e s/"#QMP_SSID"/"${name}-AP"/ \
p4u's avatar
p4u committed
266
267
268
269
270
271
272
		 -e s/"#QMP_NETWORK"/"lan"/ \
		 -e s/"#QMP_ENC"/"$encrypt"/ \
		 -e s/"#QMP_KEY"/"$key"/ \
		 -e s/"#QMP_MODE"/"ap"/ >> $TMP/qmp_wifi_iface
	}

	qmp_uci_import $TMP/qmp_wifi_iface
273
	qmp_uci_import $TMP/qmp_wifi_device
p4u's avatar
p4u committed
274

275
276
277
278
279
	# List arguments (needed for HT capab)
	cat $device_template | grep "^list " | sed s/"^list "//g | sed \
	 -e s/"#QMP_RADIO"/"$radio"/ | while read l; do
		qmp_uci_add_list_raw $l
	done
p4u's avatar
p4u committed
280

281
	uci reorder wireless.$radio=0
p4u's avatar
p4u committed
282
283
	#uci reorder wireless.@wifi-iface[$index]=16
	uci commit wireless
284
285
286
287
288
289
290
291
292
293
294
295
296
}

#############################
# 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
297
298
299
300
301
	local j=0
		
	while qmp_uci_test qmp.@wireless[$j]; do
		qmp_configure_wifi_device $j
		j=$(( $j + 1 ))
302
303
304
	done

	echo ""
p4u's avatar
p4u committed
305
	echo "Done: all WiFi devices configured"
306
307
308
309
310
311
312
313
314
315
316
}

####################
# 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
#  - third parameter: is configured mode, only needed by chanel

qmp_wifi_get_default() {
p4u's avatar
p4u committed
317
318
	local what="$1"
	local device="$2"
319
320
321
322
323
324
325
326
327

	# MODE
	# default mode depens on several things:
	#  if only 1 device = adhoc
	#  if only 1 bg device = ap
	#  else depending on index

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

p4u's avatar
p4u committed
328
329
		local devices=0
		local bg_devices=0
330
331
332
333
334
		for wd in $(qmp_get_wifi_devices); do
			devices=$(( $devices + 1 ))
			bg_devices=$(( $bg_devices + $($QMPINFO modes $wd | egrep "b|g" -c) ))
		done

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

p4u's avatar
p4u committed
337
		#If only one device, using AP+ADHOC
338
		if [ $devices -eq 1 ]; then
339
			[ $bg_devices -eq 0 ] && echo "adhoc" || echo "adhoc_ap"
340
341
		else

p4u's avatar
p4u committed
342
		#If only one B/G device (2.4GHz) available, using it as AP+ADHOC
343
344
		bg_this_device=$($QMPINFO modes $device | egrep "b|g" -c)
		if [ $bg_this_device -eq 1 -a $bg_devices -eq 1 ]; then
p4u's avatar
p4u committed
345
			echo "adhoc_ap"
346
347
		else

348
		#If only one B/G device of two devices, using the non B/G one as adhoc
349
350
351
		if [ $bg_devices -eq 1 -a $devices -eq 2 ]; then
			echo "adhoc"
		else
352
353
354
355
356
357
358
		
		#If more than one device BG, using first for ADHOC+AP and the others for ADHOC
		if [ $devices -gt 1 ]; then
			[ $index == 0 ] && echo "adhoc_ap" || echo "adhoc"
		else
		
		#This should never happend
p4u's avatar
p4u committed
359
			echo "adhoc_ap"
360
		fi;fi;fi;fi
361
362
363
364
365
366
367
368

	# CHANNEL
	# Default channel depends on the card and on configured mode
	#  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
369
		local mode="$3"
370
371

		# we are using index var to put devices in different channels
p4u's avatar
p4u committed
372
		local index=$(echo $device | tr -d [A-z])
373
374
		index=${index:-0}
		
375
376
		# QMPINFO returns a list of avaiable channels in this format: 130 ht40+ adhoc
		# this is the command line used to get available channels from a device
p4u's avatar
p4u committed
377
378
		local channels_cmd="$QMPINFO channels $device"
		local num_channels=$($channels_cmd | wc -l)
379

p4u's avatar
p4u committed
380
		# number of channels for AP is 11 or the number of channels available if less
p4u's avatar
p4u committed
381
		local num_channels_ap=$num_channels
p4u's avatar
p4u committed
382
		[ $num_channels_ap -gt 11 ] && num_channels_ap=11
383

p4u's avatar
p4u committed
384
385
386
		# use 40 Mhz of channel size (802.11n)
		local ht40="" # ht40+/ht40-

387
		# channel AdHoc is the last available (qmp_tac = inverse order) plus index*2+1 (1 3 5 ...)
p4u's avatar
p4u committed
388
389
390
391
392
393
394
395
		[ "$mode" == "adhoc" ] || [ -z "$mode" ] && {

			#this is global
			ADHOC_INDEX=${ADHOC_INDEX:-0}
			
			channel_info="$(qmp_tac $channels_cmd | grep adhoc | awk NR==${ADHOC_INDEX}+${ADHOC_INDEX}*2+1)"
			
			ADHOC_INDEX=$(($ADHOC_INDEX+1))
p4u's avatar
p4u committed
396
			# c is the channel number, checking if it is 802.11bg
p4u's avatar
p4u committed
397
398
399
400
401
402
403
404
405
406
			# 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"
				if [ $c -lt 5 ]; then c=1                                                                                                 
				else if [ $c -lt 9 ]; then c=6                                                                                            
				else c=11
				fi; fi
			ADHOC_BG_USED="$c"
			channel_info="$c $(echo $channel_info | cut -d' ' -f2-)"
p4u's avatar
p4u committed
407
408
409
410

			} || {
				# let's see if we can use ht40 mode
				ht40="$(echo $channel_info | cut -d' ' -f2)"
p4u's avatar
p4u committed
411
412
413
414
			}
						
		}
		
415
		# channel AP = ( node_id + index*3 ) % ( num_channels_ap) + 1
p4u's avatar
p4u committed
416
		# channel is 1, 6 or 11 for coexistence and performance
p4u's avatar
p4u committed
417
		[ "$mode" = "ap" -o "$mode" = "adhoc_ap" ] && {
418
419
420
421
422
423
424
425
426
427
428
429
430
431

			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
p4u's avatar
p4u committed
432
			
433
434
435
436
437
				#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
438
439
440
			channel_info="$($channels_cmd | awk NR==$c)"
		}
		
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		# if there is some problem, channel 6 is used
		if [ -z "$channel_info" ]; then
			qmp_log "Warning, not usable channels found in device $device "
			[ "$1" == "channel" ] && echo "6"
			return
		fi

		channel="$(echo $channel_info | cut -d' ' -f1)"
		[ "$ht40" == "ht40+" ] && channel="${channel}+"
		[ "$ht40" == "ht40-" ] && channel="${channel}-"

		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
462
463
464
465
466
467
468
469
470
471
472
qmp_reset_wifi() {
	#Generating default wifi configuration
	country="$(uci get qmp.wireless.country 2>/dev/null)"
	country="${country:-00}"

	mv /etc/config/wireless /tmp/wireless.old
	wifi detect | sed s/"disabled 1"/"country $country"/g > /etc/config/wireless

	wifi
}

473
474
475
476
477
478
479
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
480
	[ -z "$(qmp_uci_get wireless.mrate)" ] && qmp_uci_set wireless.mrate $(qmp_wifi_get_default mrate)
481
482
483
484

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

p4u's avatar
p4u committed
485
	macs="$(qmp_get_wifi_mac_devices | sort -u)"
486
487
488
489
490
491
492
493
494
495
496
497

	#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
498
				device="$(qmp_get_dev_from_wifi_mac $m)"
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
				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
531
		device=$(qmp_get_dev_from_wifi_mac $m)
532
533
534
535
536
537
538
539
540
541
		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
542
543
544
		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)"
545
546
547
548
549
		qmp_uci_set @wireless[$j].mac $m
		qmp_uci_set @wireless[$j].device $device
		id_configured="$id_configured $j"
	done
}