xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/ssv6xxx/script/scan_conn.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/bin/bash
2
3wpa_ret ( )
4{
5	if [ $1 != "OK" ]
6		then
7		if [ $3 == 1 ]
8			then
9			wpa_cli remove_network $2>/dev/null
10			echo "wpa_cli command failed on $2"
11		fi
12		exit 1
13	fi
14}
15
16connect_ap ( )
17{
18	#if [ $1 -eq 1 ]
19		#then
20		ssid=\""$2"\"
21		pw=\""$3"\"
22		declare -i scantimes=1
23		nw_no=$(wpa_cli add_network | sed '1d')
24		ret=$(wpa_cli set_network $nw_no ssid $ssid | sed '1d')
25		echo "Set ssid for nw $nw_no: $ret"
26		wpa_ret $ret $nw_no 1
27		if [ $5 == "WPA" -o $5 == "WPA2" ]
28			then
29			ret=$(wpa_cli set_network $nw_no psk $pw | sed '1d')
30                       	echo "Set PSK: $ret"
31                        wpa_ret $ret $nw_no 1
32		elif [ $5 == "WEP40" -o $5 == "WEP128" -o $5 == "OPEN" ]
33			then
34			ret=$(wpa_cli set_network $nw_no key_mgmt NONE | sed '1d')
35                       	echo "Set Key-mgmt: $ret"
36                        wpa_ret $ret $nw_no 1
37			ret=$(wpa_cli set_network $nw_no auth_alg OPEN | sed '1d')
38                        echo "Set Aith-alg: $ret"
39                        wpa_ret $ret $nw_no 1
40			if [ $5 != "OPEN" ]
41				then
42				ret=$(wpa_cli set_network $nw_no wep_key0 $pw | sed '1d')
43                        	echo "Set wep key: $ret"
44                        	wpa_ret $ret $nw_no 1
45			fi
46		fi
47		#echo "Set proto"
48		#ret=$(wpa_cli set_network $nw_no proto WPA | sed '1d')
49		#wpa_cli status #echo $ret
50		#wpa_ret $ret $nw_no 1
51		#echo "Set key_mgmt"
52		#ret=$(wpa_cli set_network $nw_no key_mgmt WPA-PSK | sed '1d')
53		#wpa_cli status #echo $ret
54		#wpa_ret $ret $nw_no 1
55		#echo "Set pairwise"
56		#ret=$(wpa_cli set_network $nw_no pairwise CCMP | sed '1d')
57		#wpa_cli status #echo $ret
58		#wpa_ret $ret $nw_no 1
59		#echo "Set group"
60		#ret=$(wpa_cli set_network $nw_no group CCMP | sed '1d')
61		#wpa_cli status #echo $ret
62		#wpa_ret $ret $nw_no 1
63		ret=$(wpa_cli enable_network $nw_no | sed '1d')
64		echo "Enable nw $nw_no: $ret"
65		wpa_ret $ret $nw_no 1
66		#echo "Connect"
67		#ret=$(wpa_cli reconnect | sed '1d')
68		#wpa_cli status #echo $ret
69		#wpa_ret $ret $nw_no 1
70		state=$(wpa_cli status -i $1 | grep wpa_state | sed 's/wpa_state=//g')
71		form_st="INACTIVE"
72		while [ $state != "COMPLETED" ]
73		do
74			if [ $form_st != $state ]
75				then
76				echo "Status: $state"
77			fi
78			if [ $state == "SCANNING" ]
79				then
80				sleep 1
81			elif [ $state == "DISCONNECTED" ]
82				then
83				if [ $scantimes <= $4]
84					then
85					echo "Connect"
86                			ret=$(wpa_cli reconnect | sed '1d')
87					wpa_ret $ret $nw_no 1
88					$scantimes=$scantimes + 1
89				else
90					echo "exceed scan times=$scantimes"
91				fi
92			fi
93			form_st=$state
94			state=$(wpa_cli status -i $1 | grep wpa_state | sed 's/wpa_state=//g')
95		done
96		if [ $state == "COMPLETED" ]
97			then
98			echo "Connected and request for IP address"
99			dhclient -4 $1 >/dev/null
100		fi
101	#fi
102}
103
104if [ $# -eq 5 ]
105	then
106	echo "Scanning"
107	declare -i total_scantime=$4
108        for((i=1; i<=$4; i=i+1))
109	do
110		echo "scan loop:$i"
111		scan_st=$(wpa_cli scan -i $1)
112		total_scantime=total_scantime-1
113		sleep 2
114		result=$(wpa_cli scan_results -i $1 | grep -c $2)
115		if [ $result -eq 1 ]
116			then
117			echo "Target AP $2 is found."
118			break;
119		fi
120	done
121        if [ $result -eq 1 ]
122		then
123		connect_ap $1 $2 $3 $total_scantime $5
124	else
125		echo "Target ap is not found"
126	fi
127
128
129elif [ $3 == "off" ]
130	then
131	echo "Disconnect AP"
132	dhclient -r $1 >/dev/null
133	dhclient -x >/dev/null
134	nw_no=$(wpa_cli list_network | grep CURRENT | awk '{print $1}')
135	echo "Disable network"
136	ret=$(wpa_cli disable_network $nw_no | sed '1d')
137	wpa_ret $ret $nw_no 1
138	echo "Remove network"
139	ret=$(wpa_cli remove_network $nw_no | sed '1d')
140	wpa_ret $ret $nw_no 0
141else
142	echo "====================================================================================="
143	echo "manual scan & connect script"
144	echo "./scan_conn.sh <interface> <BSSID> <PW> <scan_times>"
145	echo "./scan_conn.sh <interface> <BSSID> off"
146	echo "====================================================================================="
147fi
148
149