xref: /OK3568_Linux_fs/external/rk_pcba_test/echo_wlan_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  wlan_test.c  --  wlan test application
3  *
4  *  Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5  *  Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * 	 http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 //WLAN test program
20 #include <stdio.h>
21 
22 //open()���ͷ�ļ�
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 
27 //error���ͷ�ļ�
28 #include <errno.h>
29 #include <string.h>
30 #include "wlan_test.h"
31 
32 #define LOG_TAG "wlan_test"
33 #include "common.h"
34 #define WLAN_PROC_ERR -45
35 
36 #define WLAN_START_UP_COMMAND "ifconfig wlan0 up"
37 #define WLAN_MANAGE_START "wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf &"
38 #define WLAN_SCAN_COMMAND "wpa_cli -i wlan0 scan 'wlan0'"
39 #define WLAN_SCAN_RESULT  "wpa_cli -i wlan0 scan_r | busybox awk 'NR>=3{print $5,$3}'"
40 #define SCAN_RESULT_FILE "/tmp/wlan_scan_result.txt"
41 
42 #define AP_SUPPORT_CMD "iw list | busybox grep AP > %s"
43 #define AP_SUPPORT_FILE "/tmp/wlan_ap_result.txt"
44 #define SOFTAP_MSG "dmesg | grep SSID | tail -n 6 > %s"
45 #define MSG_FILE "/tmp/softap_start_msg.txt"
46 
47 #ifdef PCBA_PX3SE
48 #define CONNECT_AP_CMD "wpa_supplicant -i wlan0 -c /data/wpa_conf &"
49 #define CONNECT_AP_STATUS "wpa_cli status > %s"
50 #define CONNECT_AP_RESULT_FILE "/tmp/wlan_connect_result.txt"
51 
52 #define REPORT_SSID     "Rockchip" //ָ��ɨ��ĵ�wifi ssid����
53 #endif
54 
55 //* 1���رպ�̨wpa_supplicant����
close_wpa_supplicant(void)56 static int close_wpa_supplicant(void)
57 {
58     int test_flag = -1;
59     char pid_buf[64];
60     char cmd[64];
61     FILE* pp;
62     printf("====================function : %s start =================\n",__func__);
63     pp = popen("ps |grep wpa_supplicant|awk 'NR==1 {print $1}'","r");
64     //pp = popen("ps | grep wpa_supplicant | grep -v grep","r");
65     //����ļ���ʧ�ܣ������������Ϣ
66     if (!pp)
67     {
68         printf("%s popen err%s\n",__func__,strerror(errno));
69         return -1;
70     }
71     memset(pid_buf,0,sizeof(pid_buf));
72     fgets(pid_buf,sizeof(pid_buf),pp);
73     pclose(pp);
74     printf("Get pid_buf is: \t %s\n",pid_buf);
75     sprintf(cmd,"kill -9 %d",atoi(pid_buf));
76     printf("cmd is: %s\n",cmd);
77     system(cmd);
78     printf("====================function : %s finish =================\n",__func__);
79     return 0;
80 }
81 
82 /*
83  * RSSI Levels as used by notification icon
84  *
85  * Level 4  -55 <= RSSI
86  * Level 3  -66 <= RSSI < -55
87  * Level 2  -77 <= RSSI < -67
88  * Level 1  -88 <= RSSI < -78
89  * Level 0         RSSI < -88
90  */
calc_rssi_lvl(int rssi)91 static int calc_rssi_lvl(int rssi)
92 {
93 	if (rssi >= -55)
94 		return 4;
95 	else if (rssi >= -66)
96 		return 3;
97 	else if (rssi >= -77)
98 		return 2;
99 	else if (rssi >= -88)
100 		return 1;
101 	else
102 		return 0;
103 }
104 
105 
106 /* ��ɨ�赽��WiFi��Ϣ�����path��Ӧ�ļ�
107  * ����ʾ��һ��WiFi���ֺ��ź�ǿ��
108 */
wlan_exec(const char * cmd,char * path)109 int wlan_exec(const char *cmd, char *path)
110 {
111     char result_buf[128];
112     char ssid[128];
113     int signal_level;
114     int ch=0;
115     int fd;
116 
117      printf("=================== function :%s start======================\n\n",__func__);
118 
119     memset(result_buf, 0, sizeof(result_buf));
120     memset(ssid, 0, sizeof(ssid));
121     //��ʾ��һ��WiFi��Ϣ
122     FILE *pp = popen(cmd, "r");
123     //����ļ���ʧ�ܣ������������Ϣ
124     if (!pp)
125     {
126         printf("%s popen err%s\n",__func__,strerror(errno));
127         return -1;
128     }
129 
130 #ifdef PCBA_PX3SE
131     /*chad.ma add below*/
132     if (path != NULL) {
133         fd = open(path, O_CREAT | O_WRONLY	| O_TRUNC);
134         if (fd <0) {
135             log_err("open %s fail, errno = %d\n", path, errno);
136             return errno;
137         }
138     }
139     /*chad.ma add up*/
140 #endif
141 
142 	while(!feof(pp))
143 	{
144 	    fscanf(pp,"%s",ssid);
145 	    fscanf(pp,"%d",&signal_level);
146 	    //�����ź�ǿ���ж�WiFi�Ƿ���������
147 	    if(signal_level< -200||signal_level >=0)
148         {
149             printf("wlan scan result is null\n");
150             return -1;
151         }
152 #ifdef PCBA_PX3SE
153         snprintf(result_buf,sizeof(result_buf),
154                     "SSID:%s Signal Level:%d RSSI:%d\n",
155                     ssid, signal_level, calc_rssi_lvl(signal_level));
156         int w_len = write(fd, result_buf, strlen(result_buf));
157 		if (w_len <= 0) {
158 			log_err("Write %s fail, errno = %d\n", path, errno);
159 			return errno;
160 		}
161 #endif
162 
163 	    printf("SSID is: %s,signal level is: %d\n",ssid,signal_level);
164 	    printf("SSID is: %s,rssi is: %d\n",ssid,calc_rssi_lvl(signal_level));
165 	}
166 	pclose(pp);
167 
168 #ifdef PCBA_PX3SE
169     close(fd);
170 #endif
171 	printf("\n=================== function :%s finish======================\n",__func__);
172 	return 0;
173 }
174 
wlan_test(void * argv)175 void *wlan_test(void *argv)
176 {
177     int test_flag=-1;
178     char cmd[128];
179     printf("===================Wlan test start======================\n");
180     //sprintf(cmd,"aplay %s/wlan_test_start.wav",AUDIO_PATH);
181     //system(cmd);
182     //system("aplay /data/cfg/test/wlan_test_start.wav");
183 
184     //1���ȹرպ�̨wpa_supplicant���̣�Ȼ����ж��WLAN�����̼���Ȼ�����¼��������̼�
185     test_flag = close_wpa_supplicant();
186     system("echo 1 > /sys/class/rkwifi/driver");
187 
188     //2������wlan0�˿�
189     int status = system(WLAN_START_UP_COMMAND);
190 
191     /* add by chad ma 20180629 below judge*/
192     if(status == -1) {
193         printf("system run error ....\n");
194         goto fail;
195     } else {
196         log_info("exit status value = [0x%x]\n", status);
197         if (WIFEXITED(status))
198         {
199             if (0 == WEXITSTATUS(status))
200             {
201                 log_info("run WLAN_START_UP_COMMAND successfully.\n");
202                 //ret = 0;
203             }
204             else
205             {
206                 system("rfkill unblock wifi");
207             }
208         }
209         else
210         {
211             log_info("exit status = [%d]\n", WEXITSTATUS(status));
212 
213             //ret = -1;
214         }
215     }
216     sleep(2);
217 
218     //3���������������������wpa_supplicant
219     system(WLAN_MANAGE_START);
220     sleep(2);
221 
222     //4��ɨ����������
223     system(WLAN_SCAN_COMMAND);
224     sleep(3);
225 
226     //5����ʾ��WiFi��Ϣ
227     test_flag = wlan_exec(WLAN_SCAN_RESULT,SCAN_RESULT_FILE);
228     if(test_flag<0)
229     {
230         //��һ��scanʧ�ܣ���ִ��һ��;
231         system(WLAN_SCAN_COMMAND);
232         sleep(2);
233         test_flag = wlan_exec(WLAN_SCAN_RESULT,SCAN_RESULT_FILE);
234         if(test_flag<0)
235         goto fail;
236         //return -1;
237     }
238 
239     printf("===================Wlan test success ======================\n");
240     system("ifconfig wlan0 down");
241     system("busybox killall wpa_supplicant");      //�ر����߹������wpa_supplicant
242     return (void*)test_flag;
243 fail:
244     printf("===================Wlan test failed======================\n");
245     system("ifconfig wlan0 down");
246     system("busybox killall wpa_supplicant");      //�ر����߹������wpa_supplicant
247     return (void*)test_flag;
248 }
249 
main(int argc,char * argv[])250 int main(int argc, char *argv[])
251 {
252     int test_flag = 0,err_code = 0;
253     char read_buf[128];
254     int find_flag = 0;
255     FILE *fp;
256     char buf[COMMAND_VALUESIZE] = "wlan_test";
257     char result[COMMAND_VALUESIZE] = RESULT_PASS;
258     test_flag = (int)wlan_test(argv[0]);
259     if(test_flag < 0)
260     {
261         strcpy(result,RESULT_FAIL);
262         err_code = WLAN_PROC_ERR;
263     }
264 
265 #ifdef PCBA_PX3SE
266     //��ɨ���������ļ��У�ɨ��ָ��ssid��signal
267     fp = fopen(SCAN_RESULT_FILE, "r");
268     memset(read_buf, 0 ,sizeof(read_buf));
269      //����ļ���ʧ�ܣ������������Ϣ
270     if (!fp)
271     {
272         printf("%s fopen err:%s\n",__func__,strerror(errno));
273         return -1;
274     }
275 
276     while(!feof(fp))
277     {
278         fgets(read_buf,sizeof(read_buf),fp);
279         if(strstr(read_buf,REPORT_SSID)!= NULL)
280         {
281             find_flag = 1;
282             //ע�⣺read_buf�Ὣһ�еĻس���Ҳ����ȥ������Ҫ��read_buf���и���
283             int len = strlen(read_buf);
284             if ( len < 128) {
285                 //assert(len < 128);
286                 read_buf[len - 1] = '\0';
287                 printf("\n ###Found!!! %s ###\n",read_buf);
288                 break;
289             } else {
290                 printf("%s ERROR!!! read to much from one line\n",__func__);
291             }
292         }
293     }
294     fclose(fp);
295     //�����������Ƶ�buf��
296     if (find_flag) {
297         strcat(buf, ": ");
298         strcat(buf, read_buf);
299     }
300 
301     //ɾ��ɨ���ļ�
302     //remove(SCAN_RESULT_FILE);
303     printf("\n $$ %s :not found %s $$\n",buf , REPORT_SSID);
304 #endif
305     send_msg_to_server(buf, result, err_code);
306 }
307