xref: /OK3568_Linux_fs/external/rk_pcba_test/pcba_minui/wlan_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <utime.h>
27 #include <fcntl.h>
28 #include <cutils/log.h>
29 #include "common.h"
30 //#include "extra-functions.h"
31 #include "wlan_test.h"
32 #include "test_case.h"
33 #include "language.h"
34 
35 #define TAG	"[PCBA,WIFI]: "
36 #define LOG(x...)	printf(TAG x)
37 
38 #define MAX_SCAN_COUNTS	(64)
39 #define SCAN_RESULT_LENGTH	(128 * MAX_SCAN_COUNTS)
40 #define SCAN_RESULT_FILE	"/data/scan_result.txt"
41 #define SCAN_RESULT_FILE2	"/data/scan_result2.txt"
42 
43 static char ssids[MAX_SCAN_COUNTS][128];
44 static char rssis[MAX_SCAN_COUNTS][128];
45 
46 static char wifi_type[64] = {0};
47 extern int check_wifi_chip_type_string(char *type);
48 static const char RECOGNIZE_WIFI_CHIP[] = "/data/misc/wifi/wifi_chip";
49 
50 #if 0
51 /*
52  * RSSI Levels as used by notification icon
53  *
54  * Level 4  -55 <= RSSI
55  * Level 3  -66 <= RSSI < -55
56  * Level 2  -77 <= RSSI < -67
57  * Level 1  -88 <= RSSI < -78
58  * Level 0         RSSI < -88
59  */
60 static int calc_rssi_lvl(int rssi)
61 {
62 	rssi *= -1;
63 
64 	if (rssi >= -55)
65 		return 4;
66 	else if (rssi >= -66)
67 		return 3;
68 	else if (rssi >= -77)
69 		return 2;
70 	else if (rssi >= -88)
71 		return 1;
72 	else
73 		return 0;
74 }
75 
76 static void process_ssid(char *dst, char *src, char *src2)
77 {
78 	char *p, *p2, *tmp, *tmp2;
79 	int i, j, dbm, dbm2 = 99, index = 0, rssi;
80 
81 	for (i = 0; i < MAX_SCAN_COUNTS; i++) {
82 		/* ESSID:"PocketAP_Home" */
83 		tmp = &ssids[i][0];
84 		p = strstr(src, "ESSID:");
85 		if (p == NULL)
86 			break;
87 		/* skip "ESSID:" */
88 		p += strlen("ESSID:");
89 		while ((*p != '\0') && (*p != '\n'))
90 			*tmp++ = *p++;
91 		*tmp++ = '\0';
92 		src = p;
93 		/* LOG("src = %s\n", src); */
94 
95 		/* Quality:4/5  Signal level:-59 dBm  Noise level:-96 dBm */
96 		tmp2 = &rssis[i][0];
97 		p2 = strstr(src2, "Signal level");
98 		if (p2 == NULL)
99 			break;
100 		/* skip "level=" */
101 		p2 += strlen("Signal level") + 1;
102 		/* like "-90 dBm", total 3 chars */
103 		*tmp2++ = *p2++;	/* '-' */
104 		*tmp2++ = *p2++;	/* '9' */
105 		*tmp2++ = *p2++;	/* '0' */
106 		*tmp2++ = *p2++;	/* ' ' */
107 		*tmp2++ = *p2++;	/* 'd' */
108 		*tmp2++ = *p2++;	/* 'B' */
109 		*tmp2++ = *p2++;	/* 'm' */
110 		*tmp2++ = '\0';
111 		src2 = p2;
112 		/* LOG("src2 = %s\n", src2); */
113 		LOG("i = %d, %s, %s\n", i, &ssids[i][0], &rssis[i][0]);
114 	}
115 
116 	LOG("total = %d\n", i);
117 	if (i == 0)
118 		return;
119 
120 	for (j = 0; j < i; j++) {
121 		dbm = atoi(&rssis[j][1]);	/* skip '-' */
122 		if (dbm == 0)
123 			continue;
124 		if (dbm < dbm2) {		/* get max rssi */
125 			dbm2 = dbm;
126 			index = j;
127 		}
128 	}
129 
130 	LOG("index = %d, dbm = %d\n", index, dbm2);
131 	LOG("select ap: %s, %s\n", &ssids[index][0], &rssis[index][0]);
132 
133 	rssi = calc_rssi_lvl(atoi(&rssis[index][1]));
134 
135 	sprintf(dst, "{ %s \"%d\" }", &ssids[index][0], rssi);
136 }
137 
138 int save_wifi_chip_type(char *type)
139 {
140 	int ret;
141 	int fd;
142 	int len;
143 	char buf[64];
144 	ret = access(RECOGNIZE_WIFI_CHIP, R_OK|W_OK);
145 	if ((ret == 0) || (errno == EACCES)) {
146 		if ((ret != 0) && (chmod(RECOGNIZE_WIFI_CHIP, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
147 			LOG("Cannot set RW to %s : %s\n", RECOGNIZE_WIFI_CHIP,strerror(errno));
148 			return -1;
149 		}
150 		LOG("%s is exit\n", RECOGNIZE_WIFI_CHIP);
151 		return 0;
152 	}
153 
154 	fd = TEMP_FAILURE_RETRY(open(RECOGNIZE_WIFI_CHIP, O_CREAT|O_RDWR, 0664));
155 	if (fd < 0) {
156 		LOG("Cannot create %s : %s\n", RECOGNIZE_WIFI_CHIP,strerror(errno));
157 		return -1;
158 	}
159 	LOG("is not exit,save wifi chip %s\n", RECOGNIZE_WIFI_CHIP);
160 	strcpy(buf, type);
161 	LOG("recognized wifi chip = %s , save to %s\n",buf, RECOGNIZE_WIFI_CHIP);
162 	len = strlen(buf)+1;
163 	if (TEMP_FAILURE_RETRY(write(fd, buf, len)) != len) {
164 		LOG("Error writing %s : %s\n", RECOGNIZE_WIFI_CHIP, strerror(errno));
165 		close(fd);
166 		return -1;
167 	}
168 	close(fd);
169 	if (chmod(RECOGNIZE_WIFI_CHIP, 0664) < 0) {
170 		LOG("Error changing permissions of %s to 0664: %s\n", RECOGNIZE_WIFI_CHIP, strerror(errno));
171 		unlink(RECOGNIZE_WIFI_CHIP);
172 		return -1;
173 	}
174 	return 1;
175 }
176 #endif
177 
wlan_test(void * argv)178 void *wlan_test(void *argv)
179 {
180 	int ret = 0, y;
181 	FILE *fp = NULL;
182 	FILE *fp2 = NULL;
183 	char *results = NULL;
184 	char *results2 = NULL;
185 	char ssid[100];
186 	struct testcase_info *tc_info = (struct testcase_info *)argv;
187 	char wifi_pcba_node = 1;
188 
189 	/* remind ddr test */
190 	if (tc_info->y <= 0)
191 		tc_info->y = get_cur_print_y();
192 
193 	y = tc_info->y;
194 	ui_print_xy_rgba(0, y, 255, 255, 0, 255, "%s:[%s..]\n", PCBA_WIFI,
195 			 PCBA_TESTING);
196 
197 #if 0
198 	if (wifi_type[0] == 0) {
199 		check_wifi_chip_type_string(wifi_type);
200 		save_wifi_chip_type(wifi_type);
201 	}
202 	ret = __system("busybox chmod 777 /res/wifi.sh");
203 	if (ret)
204 		LOG("chmod wifi.sh failed :%d\n", ret);
205 
206 	ret = __system("/res/wifi.sh");
207 	if (ret <= 0)
208 		goto error_exit;
209 
210 	results = malloc(SCAN_RESULT_LENGTH);
211 	results2 = malloc(SCAN_RESULT_LENGTH);
212 	if (results == NULL || results2 == NULL)
213 		goto error_exit;
214 
215 	fp = fopen(SCAN_RESULT_FILE, "r");
216 	fp2 = fopen(SCAN_RESULT_FILE2, "r");
217 	if (fp == NULL || fp2 == NULL)
218 		goto error_exit;
219 
220 	memset(results, 0, SCAN_RESULT_LENGTH);
221 	fread(results, SCAN_RESULT_LENGTH, 1, fp);
222 	results[SCAN_RESULT_LENGTH - 1] = '\0';
223 
224 	memset(results2, 0, SCAN_RESULT_LENGTH);
225 	fread(results2, SCAN_RESULT_LENGTH, 1, fp2);
226 	results2[SCAN_RESULT_LENGTH - 1] = '\0';
227 
228 	memset(ssid, 0, 100);
229 
230 	process_ssid(ssid, results, results2);
231 
232 
233 	ui_print_xy_rgba(0, y, 0, 255, 0, 255, "%s:[%s] %s\n", PCBA_WIFI,
234 			 PCBA_SECCESS, ssid);
235 	tc_info->result = 0;
236 
237 	fclose(fp);
238 	fclose(fp2);
239 	free(results);
240 	free(results2);
241 
242 	LOG("wlan_test success.\n");
243 
244 	return 0;
245 
246 error_exit:
247 	if (fp)
248 		fclose(fp);
249 	if (fp2)
250 		fclose(fp2);
251 	if (results)
252 		free(results);
253 	if (results2)
254 		free(results2);
255 
256 	ui_print_xy_rgba(0, y, 225, 0, 0, 255, "%s:[%s] %s\n", PCBA_WIFI,
257 			 PCBA_FAILED, ssid);
258 	tc_info->result = -1;
259 
260 	LOG("wlan_test failed.\n");
261 #else   //Linux pcba test
262 
263     int rst;
264     char ssid_buf[128] = {0};
265     char result_filename[100] = {0};
266 
267     printf("======================start wifi test=============================\n");
268     rst = run_test_item_cmd("echo_auto_test echo_wlan_test");
269 
270     if(rst == 0) {
271         snprintf(result_filename, sizeof(result_filename),
272 		        "%s/echo_wlan_test_result", "/tmp");
273         ret = parse_test_result(result_filename, "wlan_test", ssid_buf);
274 LOG("000 %s\n", ssid_buf);
275 
276     }else  {
277         return NULL;
278     }
279 
280     if(ret == 0) {
281         if(strstr(ssid_buf, "SSID") != NULL) {
282             char ch;
283             char *result = NULL, *p;
284             char delims[] = " ";
285 
286             memset(ssid, 0, sizeof(ssid));
287 
288             ch = *ssid_buf;
289             p = ssid_buf;
290             while (ch != '\0')
291             {
292                 if (ch == '<' || ch == '>')
293                     *(p-1) = ' ';
294                 ch = *p++;
295             }
296             LOG("111 %s\n", ssid_buf);
297 
298             result = strtok(ssid_buf, delims);
299             while( result != NULL ) {
300                 printf("result is \"%s\"\n", result);
301 
302                 if(strstr(result,"SSID")!= NULL){
303                     strcat(ssid, result);
304                     strcat(ssid, " ");
305                 }
306                 if(strstr(result, "Signal")!= NULL){
307                     strcat(ssid, result);
308                     strcat(ssid, " ");
309                 }
310                 if(strstr(result, "Level")!= NULL){
311                     strcat(ssid, result);
312                     strcat(ssid, " ");
313                 }
314                 if(strstr(result, "RSSI")!= NULL){
315                     strcat(ssid, result);
316                 }
317 
318                 result = strtok( NULL, delims );
319             }
320         }
321         ui_print_xy_rgba(0, y, 0, 255, 0, 255, "%s:[%s] { %s }\n", PCBA_WIFI,
322                  PCBA_SECCESS, ssid);
323         tc_info->result = 0;
324         LOG("wlan_test success.\n");
325     } else {
326         if(ssid[0] == 0)
327             strcpy(ssid, "Not found specfied ssid.");
328 
329         ui_print_xy_rgba(0, y, 225, 0, 0, 255, "%s:[%s] { %s }\n", PCBA_WIFI,
330                  PCBA_FAILED, ssid);
331         tc_info->result = -1;
332         LOG("wlan_test failed.\n");
333     }
334 
335 #endif
336 
337 	return argv;
338 }
339