blob: a235195cbc70e8955fb0df3fa7ef5dab3f57fcd8 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * wlan-station.c -- wifi station mode Deal module
3 *
4 * Copyright (c) ZTE Corporation All Rights Reserved.
5 *
6 * $Id: wlan-station.c, v 0.1 2015-03-09 $
7 * Authors : XUJIAN - xu.jian5@zte.com.cn>
8 */
9#include "wifi_util.h"
10#include "wlan-station.h"
11#include "Vector.h"
12#include "wifi_sta_ctrl.h"
13#include "soft_timer.h"
14#include "rtk_arch.h"
15#include <semaphore.h>
16#include <limits.h>
17#define SUCCESS "1"
18#define FAILED "0"
19#define REPLY_BUF_SIZE 4096 // wpa_supplicant's maximum size.
20
21#define UEVENT_BUFFER_SIZE 1024*2
22
23#define WIFI_STATION_CMD_LEN 256
24
25//¶¨Ê±Æ÷Ïà¹Ø
26#define WIFI_STATION_TIMER_CONNECT 109
27#define WIFI_STATION_TIMER_GET_STATUS 110
28#define WIFI_STATION_TIMER_SCAN 111
29//add sim card init status query timer
30#define WIFI_STATION_TIMER_SIM_CARD_STATUS 112
31#define WIFI_STATION_TIMER_GET_SCAN_RESULTS 113
32
33#define WIFI_STATION_TIMER_CONNECT_INTERVAL 1000
34#define WIFI_STATION_TIMER_GET_STATUS_INTERVAL 2000
35#define WIFI_STATION_TIMER_SCAN_INTERVAL 60000 /// 10 ----> 15s nxl
36
37#define WIFI_STATION_TIMER_GET_SCAN_RESULTS_INTERVAL 8000 /// 10 ----> 15s nxl
38
39
40#define WIFI_STATION_TIMER_SIM_CARD_STATUS_INTERVAL 2000
41
42#define WIFI_STATION_TIMER_WAIT_CONNECT_COUNT 20
43#define WIFI_STATION_TIMER_CONNECT_TIME_OUT 116
44
45//ɨÃèÏà¹Ø
46#define WIFI_STATION_SCAN_DURATION 8
47#define WIFI_STATION_SCRIPT_RSP_LINE_LEN 1024
48#define WIFI_STATION_SCAN_RESULT_STR_LEN 1024
49#define WIFI_STATION_SCAN_ONE_RESULT_STR_LEN 256
50#define WIFI_STATION_SCAN_RESULT_LIST_NUM 60//Ò»°ãʵ¼Ê¹Ì¼þÄÜɨ³öÀ´30¼¸¸ö£¬ÏµÍ³ÄÜ´æ´¢µÄɨÃè½á¹û×î¶à²»³¬¹ý2048¸ö×Ö½Ú
51
52
53
54
55
56#define WIFI_STATION_SIGNAL_LEVEL1 -80
57#define WIFI_STATION_SIGNAL_LEVEL2 -78
58#define WIFI_STATION_SIGNAL_LEVEL3 -75
59#define WIFI_STATION_SIGNAL_LEVEL4 -72
60
61#define WIFI_STATION_CHANNEL1 2412
62#define WIFI_STATION_CHANNEL2 2417
63#define WIFI_STATION_CHANNEL3 2422
64#define WIFI_STATION_CHANNEL4 2427
65#define WIFI_STATION_CHANNEL5 2432
66#define WIFI_STATION_CHANNEL6 2437
67#define WIFI_STATION_CHANNEL7 2442
68#define WIFI_STATION_CHANNEL8 2447
69#define WIFI_STATION_CHANNEL9 2452
70#define WIFI_STATION_CHANNEL10 2457
71#define WIFI_STATION_CHANNEL11 2462
72#define WIFI_STATION_CHANNEL12 2467
73#define WIFI_STATION_CHANNEL13 2472
74#define WIFI_STATION_FREQ_LEN 16
75
76#define WIFI_STATION_MSG_SOURCE_STR_LEN 15
77#define WIFI_STATION_IP_STATUS_LEN 16
78#define WIFI_CONNECTION_LEN 4
79#define WIFI_ZPBIC_STATUS_LEN 16
80#define WIFI_STATION_WPA_SUPPLICANT_STATUS_LEN 16
81
82#define WIFI_STATION_SCAN_RESULT_PARSE_IGNORE "bssid / frequency /"
83
84#define WIFI_STATION_AUTH_WPA_PSK "WPA-PSK"
85#define WIFI_STATION_AUTH_WPA2_PSK "WPA2-PSK"
86#define WIFI_STATION_AUTH_WPA_EAP "WPA-EAP"
87#define WIFI_STATION_AUTH_WPA2_EAP "WPA2-EAP"
88#define WIFI_STATION_AUTH_SHARED "SHARED"
89#define WIFI_STATION_AUTH_WPA3 "SAE"
90
91#define WIFI_STATION_ENCRYPT_CCMP "CCMP"
92#define WIFI_STATION_ENCRYPT_TKIP "TKIP"
93#define WIFI_STATION_ENCRYPT_WEP "WEP"
94
95#define WIFI_STATION_PROFILE_AUTH_WPA_PSK "WPAPSK"
96#define WIFI_STATION_PROFILE_AUTH_WPA2_PSK "WPA2PSK"
97//#define WIFI_STATION_PROFILE_AUTH_WPA_EAP "WPAEAP"
98//#define WIFI_STATION_PROFILE_AUTH_WPA2_EAP "WPA2EAP"
99#define WIFI_STATION_PROFILE_AUTH_WPA_EAP "EAP-SIM/AKA"
100#define WIFI_STATION_PROFILE_AUTH_SHARED "SHARED"
101#define WIFI_STATION_PROFILE_AUTH_WPA3 "WPA3Personal"
102#define WIFI_STATION_PROFILE_AUTH_WPA2_WPA3 "WPA2WPA3"
103
104#define WIFI_STATION_PROFILE_ENCRYPT_WEP "WEP"
105#define WIFI_STATION_PROFILE_ENCRYPT_CCMP "CCMP"
106#define WIFI_STATION_PROFILE_ENCRYPT_TKIP "TKIP"
107
108
109
110#define WIFI_STATION_STATUS_RESULT_PARSE_BSSID "bssid"
111#define WIFI_STATION_STATUS_RESULT_PARSE_SSID "ssid"
112#define WIFI_STATION_STATUS_RESULT_PARSE_WPA_STATUS "wpa_state"
113#define WIFI_STATION_STATUS_RESULT_PARSE_SUPPLICANT_PAE_STATUS "Supplicant PAE state"
114#define WIFI_STATION_STATUS_RESULT_PARSE_SUPPPORT_STATUS "suppPortStatus"
115#define WIFI_STATION_STATUS_RESULT_PARSE_EAP_STATUS "EAP state"
116
117
118
119
120#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_OK "COMPLETED"
121
122#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_INACTIVE "INACTIVE"
123#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_SCANNING "SCANNING"
124#define WIFI_STATION_TIMER_SCANNING_STATE_COUNT 10
125
126#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_AUTHENTICATING "AUTHENTICATING"
127#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_ASSOCIATING "ASSOCIATING"
128#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_ASSOCIATED "ASSOCIATED"
129#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_4WAY_HANDSHAKE "4WAY_HANDSHAKE"
130#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_4WAY_GROUP_HANDSHAKE "GROUP_HANDSHAKE"
131#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_4WAY_GROUP_UNKNOWN "UNKNOWN"
132#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_4WAY_GROUP_DISCONNECTED "DISCONNECTED"
133#define WIFI_STATION_STATUS_RESULT_WPA_STATUS_4WAY_GROUP_INTERFACE_DISABLED "INTERFACE_DISABLED"
134
135
136#define WIFI_STATION_STATUS_RESULT_EAP_STATUS_FAIL "FAILURE"
137#define WIFI_STATION_STATUS_RESULT_EAP_STATUS_SUCCESS "SUCCESS"
138
139
140
141#define WIFI_STATION_IP_STATUS_CONNECT "connect"
142#define WIFI_STATION_IP_STATUS_DISCONNECT "disconnect"
143#define WIFI_STATION_IP_STATUS_CONNECTING "connecting"
144#define WIFI_STATION_IP_STATUS_DHCPING "dhcping"
145
146#define INVALID_NETWORK_ID -1
147
148#define safe_free(x) do { if(x) {free(x); x=NULL;} } while(0)
149
150#define WPA_CLI "wpa_cli -p"ROOT_DIR"/wifi/wpa_supplicant -iwlan0-vxd"
151
152#define WIFI_CARD_MODE_LEN 3
153
154#define WIFI_PIN_STATUS_LEN 3
155
156#define SIMCARD_INIT_SUCCESS "success"
157#define SIMCARD_INIT_UNDEFINED "undefined"
158#define SIMCARD_INIT_NO_CARD "no_card"
159//#define SIMCARD_INIT_LOCKED "locked"
160//#define SIMCARD_INIT_FAIL "fail"
161//#define SIMCARD_INIT_PARTIAL_SUCCESS "partial_success"
162
163
164extern struct wlan_sta_manager *sta_manager;
165extern int need_channel_follow;
166
167//static int g_timer_sim_init_undefined_status_count = 0;
168//add by zhouti ÔÚeap ÈÏÖ¤¹ý³Ìǰ·¢Ë͵ÄÒì²½ÏûÏ¢Ôڵȴýʱºò£¬Èç¹ûÓû§disconnect£¬ÄÇôÐèÒªÅжϻØÀ´µÄrespÏûÏ¢ÊÇ·ñΪÕâ´ÎµÄ»ØÓ¦£¬Èç¹ûÊǾÍÐèÒª¶ªÆú²»´¦Àí
169static int g_send_count = 0;
170static int g_pre_send_count = 0;
171
172//gebin: Õ⼸¸öµÄÄ¿µÄ¾ÍÊÇΪÁËÔÚÏ̼߳䴫µÝ²ÎÊý°É??? Èç¹ûÕâÑù¾Í²»ÒªÓÃÈ«¾Ö±äÁ¿ÁË£¬ipc¿ÉÒÔ´«²ÎÊý
173char g_linked_ap_mac[WIFI_STATION_MAC_LEN] = {0};
174int g_linked_network_id = -1;
175int g_linked_network_id_last = -1;
176/*±£´æµ±Ç°ÀúÊ·ÁбíAPÄÚÈÝ*/
177static spot_t *g_spot_list_p[WIFI_STATION_SPOT_LIST_NUM] = {NULL};//ÈȵãÁбíÖ¸ÕëÊý×é
178
179static scan_result_t *g_scan_result_list_p[WIFI_STATION_SCAN_RESULT_LIST_NUM] = {NULL};//ɨÃèÈȵãÁбíÖ¸ÕëÊý×é
180static int g_cardmode = 0; //È«¾Ö¼Ç¼sim¿¨ÀàÐÍ
181
182static int g_blc_send_close_apsta = 0; // È«¾Ö¼Ç¼BLC·¢Ë͹رÕÏûÏ¢Ôò²»ÔÙÖ÷¶¯Á¬½Ówifi,Ö±µ½BLC·¢ËÍ´ò¿ª
183int g_wpa_supplicant;
184extern sem_t g_wpa_supplicant_id;
185
186int has_exec_disconnect = 0; //ÊÇ·ñÖ´Ðйý¶Ï¿ª²Ù×÷
187int has_exec_select_network = 0; //ÊÇ·ñÖ´ÐйýÑ¡ÔñÁ¬½Óijһ¸öÈȵã
188int webui_select_network = 0; //¼Ç¼webuiÏ·¢µÄÑ¡Ôñij¸öÈȵãÁ¬½ÓÃüÁî
189int g_disable_other_network = 0; //ÊÇ·ñ½ûÓÃÆäËûµÄÈȵã
190
191int g_connecting_time_out_timer = 0;//È«¾Ö¼Ç¼ÊÇ·ñÒÑ´´½¨Á¬½Ó³¬Ê±¶¨Ê±Æ÷
192static int wifi_station_connect (int from_where);
193static void wifi_station_scan (void);
194static void *wifi_station_query_sim_card_status(void *arg);
195static void sta_connect (int network_id);
196extern void basic_deal_all (int cmd);
197static int find_linked_ap_index(void);
198static void delete_quote (char * str);
199static void wlan_station_deinit(void);
200static void process_tmp_disabled (char *buf);
201
202extern int check_alive(struct wlan_socket *skt);
203
204
205inline char * sta_docmd(const char *cmd)
206{
207 return docmd (&sta_manager->sock, cmd);
208}
209
210
211/*
212 0 : not exist
213 1: alive
214*/
215int check_supplicant_alive()
216{
217 return check_alive(&sta_manager->sock);
218
219}
220
221
222void *send_message_to_handle_connect_timeout(void *arg)
223{
224 ipc_send_message (MODULE_ID_WLAN_SERVER, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_CONNECT_TIMEOUT, 0, NULL, 0);
225 return NULL;
226}
227void Create_connect_timeout_timer()
228{
229 wf_log ("[111]g_connecting_time_out_timer=%d", g_connecting_time_out_timer);
230 if (g_connecting_time_out_timer) {
231 wf_log ("[xxx]station connecting timeout softtimer already exists");
232 return;
233 }
234 wf_log ("[222]create station connecting timeout softtimer");
235 sc_timer_create (WIFI_STATION_TIMER_CONNECT_TIME_OUT, TIMER_FLAG_ONCE, 60 * 1000, send_message_to_handle_connect_timeout, NULL);
236 g_connecting_time_out_timer = 1;
237 wf_log ("[333]g_connecting_time_out_timer=%d", g_connecting_time_out_timer);
238 return;
239}
240void Delete_connect_timeout_timer (void)
241{
242 wf_log ("[555]g_connecting_time_out_timer=%d", g_connecting_time_out_timer);
243 if (g_connecting_time_out_timer == 0) {
244 wf_log ("[xxx]station connecting timeout softtimer was already deleted");
245 return;
246 }
247 wf_log ("[666]delete station connecting timeout softtimer");
248 sc_timer_delete (WIFI_STATION_TIMER_CONNECT_TIME_OUT);
249 g_connecting_time_out_timer = 0;
250 wf_log ("[777]g_connecting_time_out_timer=%d", g_connecting_time_out_timer);
251 return;
252}
253
254//Ö´ÐÐÒ»¸öshellÃüÁÊä³ö½á¹ûÖðÐд洢ÔÚresvecÖУ¬²¢·µ»ØÐÐÊý
255static int32_t convert2vector (const char *str, vector_t* resvec)
256{
257 char *tmp = NULL; //ÉèÖÃÒ»¸öºÏÊʵij¤¶È£¬ÒԴ洢ÿһÐÐÊä³ö
258 char *ret = NULL;
259 FILE *fp = NULL;
260 int tmp_len = 0;
261
262 if (str == NULL || resvec == NULL) {
263 return -1;
264 }
265
266 fp = fopen ("/tmp/scan_results", "w+"); //½¨Á¢¹ÜµÀ
267 if (!fp) {
268 wf_log ("fopen errno: %d\n", errno);
269 return -1;
270 }
271 fprintf (fp, "%s", str);
272 fseek (fp, 0, SEEK_SET);
273 tmp = safe_malloc (WIFI_STATION_SCRIPT_RSP_LINE_LEN, TRUE);
274
275again:
276 errno = 0;
277 while ((ret=fgets (tmp, WIFI_STATION_SCRIPT_RSP_LINE_LEN, fp)) != NULL) {
278 tmp_len = strlen (tmp);
279 if (tmp_len > 0) {
280 if (tmp[tmp_len - 1] == '\n') {
281 tmp[tmp_len - 1] = '\0'; //È¥³ý»»Ðзû
282 }
283 vector_push_back (resvec, var_string (tmp));
284 }
285 }
286
287 if (EINTR ==errno) { //kw 3
288 wf_log("fgets recv EINTR!");
289 goto again;
290 }
291
292 fclose (fp);
293 safe_free (tmp);
294
295 return vector_size (resvec);
296}
297
298
299//TODO ÍøÂç×éÍø¿ÚDOWN UP·â×°
300static void network_down_up (BOOL is_up)
301{
302 if (is_up == TRUE) {
303 sc_cfg_set ("WAN_MODE", "WIFI"); //usb,ppp,wifi
xf.libdd93d52023-05-12 07:10:14 -0700304 /*printf("[wlan-station]calling ipv4.sh linkup\n");
lh9ed821d2023-04-07 01:36:19 -0700305 system("zte_ipv4.sh \"linkup\"");
306 printf("[wlan-station]calling internet.sh\n");
307 system("internet.sh");
308 printf("[wlan-station]calling nat.sh\n");
309 system("nat.sh");*/
310 netdev_connect_internet (WIFI_WAN_DEV, MODULE_ID_WIFI);
311
312 wf_log ("[wlan-station]netdev_connect_internet");
313 } else {
314 netdev_disconnect_internet (WIFI_WAN_DEV, MODULE_ID_WIFI);
315 wf_log ("[wlan-station]netdev_disconnect_internet");
316 //system("zte_ipv4.sh \"linkdown\"");
317 sc_cfg_set ("WAN_MODE", "PPP"); //usb,ppp,wifi
318 }
319}
320
321
322static void update_EX_APLIST_connect_status (BOOL is_connected)
323{
324 char *scan_result_str = NULL;
325 char *scan_result_str_tmp = NULL;
326 int i = 0;
327 int found_ap_index = -1;
328 char *splited_string = NULL;
329 char *splited_string1 = NULL;
330 char *save = NULL;
331 char *save1 = NULL;
332 char *delimiter = ";";
333 int found_str_index = 0;
334 BOOL is_found = FALSE;
335 BOOL is_find_EX_APLIST1 = FALSE;
336
337 int index = 0;
338// ²»¹ÜÊÇÁ´½Ó³É¹¦»¹ÊǶϿª£¬¶¼¿ÉÒÔÕÒµ½¶ÔÓ¦µÄÈȵ㣬²»»áΪÎÞЧֵ
339 index = find_linked_ap_index();
340
341 for (i = 0; i < WIFI_STATION_SCAN_RESULT_LIST_NUM; i++) {
342 if (g_scan_result_list_p[i] != NULL && index != -1) {
343 if (strcmp (g_scan_result_list_p[i]->mac, g_spot_list_p[index]->mac) == 0) {
344 found_ap_index = i;
345
346 if (is_connected == TRUE) {
347 g_scan_result_list_p[i]->connect_status = 1;
348 } else {
349 g_scan_result_list_p[i]->connect_status = 0;
350 }
351
352 break;
353 }
354 }
355
356 }
357
358 //ûÕÒµ½APÔòÖ±½ÓÍ˳ö
359 if (found_ap_index == -1) {
360 return;
361 }
362
363 scan_result_str = safe_malloc (WIFI_STATION_SCAN_RESULT_STR_LEN, FALSE);
364 scan_result_str_tmp = safe_malloc (WIFI_STATION_SCAN_RESULT_STR_LEN, FALSE);
365 if (scan_result_str == NULL || scan_result_str_tmp == NULL) {
366 safe_free (scan_result_str);
367 safe_free (scan_result_str_tmp);
368 return;
369 }
370
371 //²éÕÒEX_APLIST
372 sc_cfg_get ("EX_APLIST", scan_result_str, WIFI_STATION_SCAN_RESULT_STR_LEN);
373
374 safe_strcpy (scan_result_str_tmp, scan_result_str, WIFI_STATION_SCAN_RESULT_STR_LEN);
375 //ÒԷֺŷָî×Ö·û´®
376 i = 0;
377 splited_string = strtok_r (scan_result_str_tmp, delimiter, &save);
378 while (splited_string) {
379 if (i == found_ap_index) {
380 //ÕÒµ½connect_statusÔÚ×Ö·û´®ÖеÄË÷Òý
381 found_str_index += 2;
382
383 is_found = TRUE;
384 break;
385 }
386 found_str_index += strlen (splited_string) + 1; //ÒªÔÙ¼ÓÉϷֺŵÄ1¸ö×Ö·û³¤¶È
387
388 i++;
389 splited_string = strtok_r (NULL, delimiter, &save);
390 }
391
392 //²éÕÒEX_APLIST1
393 if (is_found == FALSE) {
394 is_find_EX_APLIST1 = TRUE;
395 found_str_index = 0;
396 memset (scan_result_str, 0, WIFI_STATION_SCAN_RESULT_STR_LEN);
397 memset (scan_result_str_tmp, 0, WIFI_STATION_SCAN_RESULT_STR_LEN);
398 sc_cfg_get ("EX_APLIST1", scan_result_str, WIFI_STATION_SCAN_RESULT_STR_LEN);
399
400 safe_strcpy (scan_result_str_tmp, scan_result_str, WIFI_STATION_SCAN_RESULT_STR_LEN);
401
402 splited_string1 = strtok_r (scan_result_str_tmp, delimiter, &save1);
403 while (splited_string1) {
404 if (i == found_ap_index) {
405 //ÕÒµ½connect_statusÔÚ×Ö·û´®ÖеÄË÷Òý
406 found_str_index += 2;
407
408 is_found = TRUE;
409 break;
410 }
411 found_str_index += strlen (splited_string1) + 1; //ÒªÔÙ¼ÓÉϷֺŵÄ1¸ö×Ö·û³¤¶È
412
413 i++;
414 splited_string1 = strtok_r (NULL, delimiter, &save1);
415 }
416 }
417
418 if (is_found == TRUE
419 && found_str_index > 0 && found_str_index < strlen (scan_result_str)) {
420 if (is_connected == TRUE) {
421 scan_result_str[found_str_index] = '1';
422 } else {
423 scan_result_str[found_str_index] = '0';
424 }
425
426 if (is_find_EX_APLIST1 == TRUE) {
427 sc_cfg_set ("EX_APLIST1", scan_result_str);
428 } else {
429 sc_cfg_set ("EX_APLIST", scan_result_str);
430 }
431 }
432
433 safe_free (scan_result_str);
434 safe_free (scan_result_str_tmp);
435}
436
437static spot_t* parse_spot_string (char *spot_string)
438{
439 spot_t *spot = NULL;
440 char *splited_string = NULL;
441 char *save = NULL;
442 const char *delimiter = ",";
443 wifi_profile_content_t wifi_profile_content = WIFI_STATION_CONTENT_UNDEFINED;
444 char spot_string_tmp[WIFI_STATION_SPOT_LEN] = {0};
445
446 if (spot_string == NULL) {
447 return NULL;
448 }
449 //klocwork
450 strncpy(spot_string_tmp, spot_string, sizeof(spot_string_tmp)-1);
451
452 spot = safe_malloc (sizeof (spot_t), FALSE);
453 if (spot == NULL) {
454 return NULL;
455 }
456
457 spot->is_tried_connect = FALSE;
458
459 //·Ö¸î×Ö·û´®£¬´æ´¢µ½½á¹¹Ìå
460 splited_string = strtok_r (spot_string_tmp, delimiter, &save);
461 while (splited_string) {
462 wifi_profile_content++;
463 switch (wifi_profile_content) {
464 case WIFI_STATION_PROFILE_NAME:
465 //safe_strcpy (spot->profile_name, splited_string, WIFI_STATION_SPOT_PROFILE_NAME_LEN);
466 strncpy (spot->profile_name, splited_string, sizeof(spot->profile_name)-1);
467 break;
468 case WIFI_STATION_FROM_PROVIDER:
469 spot->from_provider = atoi (splited_string);
470 break;
471 case WIFI_STATION_CONNECT_STATUS:
472 spot->connect_status = atoi (splited_string);
473 break;
474 case WIFI_STATION_SIGNAL:
475 spot->signal = atoi (splited_string);
476 break;
477 case WIFI_STATION_SSID:
478 //safe_strcpy (spot->ssid, splited_string, WIFI_STATION_SSID_LEN);
479 strncpy (spot->ssid, splited_string, sizeof(spot->ssid)-1);
480 break;
481 case WIFI_STATION_AUTH_MODE:
482 //safe_strcpy (spot->auth_mode, splited_string, WIFI_STATION_AUTH_MODE_LEN);
483 strncpy (spot->auth_mode, splited_string, sizeof(spot->auth_mode)-1);
484 break;
485 case WIFI_STATION_ENCRYPT_TYPE:
486 //safe_strcpy (spot->encrypt_type, splited_string, WIFI_STATION_ENCRYPT_TYPE_LEN);
487 strncpy (spot->encrypt_type, splited_string, sizeof(spot->encrypt_type)-1);
488 break;
489 case WIFI_STATION_PASSWORD:
490 //safe_strcpy (spot->password, splited_string, WIFI_STATION_SPOT_PASSWORD_LEN);
491 strncpy (spot->password, splited_string, sizeof(spot->password)-1);
492 break;
493 case WIFI_STATION_KEY_ID:
494 spot->keyID = atoi (splited_string);
495 break;
496 case WIFI_STATION_AP_MAC:
497 //safe_strcpy (spot->mac, splited_string, WIFI_STATION_MAC_LEN);
498 strncpy (spot->mac, splited_string, sizeof(spot->mac)-1);
499 break;
500 default:
501 break;
502 }
503
504 splited_string = strtok_r (NULL, delimiter, &save);
505 }
506
507 spot->network_id = -1; // from nv profile , there is no network id property, so ,default set it -1
508
509 if (wifi_profile_content != WIFI_STATION_AP_MAC) {
510 safe_free (spot);
511 return NULL;
512 }
513
514
515 return spot;
516
517}
518
519
520static void save_config (spot_t *spot)
521{
522
523 char cmd[WIFI_STATION_CMD_LEN] = {0};
524 char *ptr = NULL;
525 char *ptr2 = NULL;
526 char *ptr3 = NULL;
527 char *ptr_w3 = NULL;
528 char *ptr_w23 = NULL;
529 char *ptr5 = NULL;
530 char *ptr6 = NULL;
531 int key_len = 0;
532 int id;
533
534 if (spot == NULL) {
535 return;
536 }
537
538 if (spot->network_id == -1) {
539
540 //id = atoi (sta_docmd ("ADD_NETWORK"));
541 //klocwork
542 char *reply = NULL;
543 reply = sta_docmd("ADD_NETWORK");
544 if (reply == NULL) {
545 return;
546 }
547 id = atoi(reply);
548
549 spot->network_id = id;
550 wf_log ("[sav_config] new spot get new networkid=%d", id);
551
552 } else {
553 id = spot->network_id ;
554 wf_log ("[sav_config] old spot have networkid=%d", id);
555
556 }
557
558
559 sprintf (cmd, "SET_NETWORK %d ssid \"%s\"", id , spot->ssid);
560 sta_docmd (cmd);
561
562 memset (cmd, 0, WIFI_STATION_CMD_LEN);
563 ptr = strstr (spot->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA_PSK);
564 ptr2 = strstr (spot->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA2_PSK);
565 ptr3 = strstr (spot->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA_EAP);
566 ptr_w3 = strstr(spot->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA3);
567 ptr_w23 = strstr(spot->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA2_WPA3);
568
569 ptr5 = strstr (spot->encrypt_type, WIFI_STATION_PROFILE_ENCRYPT_CCMP);
570 ptr6 = strstr (spot->encrypt_type, WIFI_STATION_PROFILE_ENCRYPT_TKIP);
571
572 sprintf (cmd, "SET_NETWORK %d priority 1", id);
573 sta_docmd (cmd);
574
575
576 if (ptr || ptr2) {
577 sprintf (cmd, "SET_NETWORK %d key_mgmt WPA-PSK", id);
578 sta_docmd (cmd);
579
580 if (ptr && !ptr2) {
581 sprintf (cmd, "SET_NETWORK %d proto WPA", id);
582 sta_docmd (cmd);
583 } else if (!ptr && ptr2) {
584 sprintf (cmd, "SET_NETWORK %d proto WPA2", id);
585 sta_docmd (cmd);
586 } else{
587 sprintf (cmd, "SET_NETWORK %d proto WPA RSN", id);
588 sta_docmd (cmd);
589 }
590
591 if (ptr5 && !ptr6) {
592 sprintf (cmd, "SET_NETWORK %d pairwise CCMP", id);
593 sta_docmd (cmd);
594 } else if (!ptr5 && ptr6) {
595 sprintf (cmd, "SET_NETWORK %d pairwise TKIP", id);
596 sta_docmd (cmd);
597 } else {
598 wf_log ("[save_config]can support both of CCMP TKIP", ptr5, ptr6);
599 sprintf (cmd, "SET_NETWORK %d pairwise CCMP TKIP", id);
600 sta_docmd (cmd);
601 }
602
603 sprintf (cmd, "SET_NETWORK %d psk \"%s\" ", id , spot->password);
604 sta_docmd (cmd);
605 } else if (ptr3) { //|| ptr4)
606 sprintf (cmd, "SET_NETWORK %d key_mgmt WPA-EAP", id);
607 sta_docmd (cmd);
608
609 if (g_cardmode == CARD_MODE_USIM) {
610 sprintf (cmd, "SET_NETWORK %d eap AKA", id);
611 sta_docmd (cmd);
612 } else {
613 sprintf (cmd, "SET_NETWORK %d eap SIM", id);
614 sta_docmd (cmd);
615 }
616 sprintf (cmd, "SET_NETWORK %d pcsc '\" \"'", id);
617 sta_docmd (cmd);
618
619//ÖØÐ¸²¸ÇÉèÖÃÓÅÏȼ¶Îª×î¸ß
620 sprintf (cmd, "SET_NETWORK %d priority 4", id);
621 sta_docmd (cmd);
622
623 } else if (ptr_w3) {
624 sprintf (cmd, "SET_NETWORK %d key_mgmt SAE", id);
625 sta_docmd (cmd);
626
627 //if set ,other need ieee80211w 0?
628 //sprintf (cmd, "SET_NETWORK %d proto RSN", id);
629 //sta_docmd (cmd);
630
631 //sprintf (cmd, "SET_NETWORK %d pairwise CCMP", id);
632 //sta_docmd (cmd);
633
634 //sprintf (cmd, "SET_NETWORK %d ieee80211w 2", id);
635 //sta_docmd (cmd);
636
637
638 sprintf (cmd, "SET_NETWORK %d psk \"%s\"", id , spot->password);
639 sta_docmd (cmd);
640 } else if (ptr_w23) {
641 sprintf (cmd, "SET_NETWORK %d key_mgmt SAE WPA-PSK", id);
642 sta_docmd (cmd);
643
644 //sprintf (cmd, "SET_NETWORK %d proto RSN", id);
645 //sta_docmd (cmd);
646
647 //sprintf (cmd, "SET_NETWORK %d pairwise CCMP", id);
648 //sta_docmd (cmd);
649
650 //sprintf (cmd, "SET_NETWORK %d ieee80211w 1", id);
651 //sta_docmd (cmd);
652
653
654 sprintf (cmd, "SET_NETWORK %d psk \"%s\"", id , spot->password);
655 sta_docmd (cmd);
656 } else {
657 sprintf (cmd, "SET_NETWORK %d key_mgmt NONE", id);
658 sta_docmd (cmd);
659 }
660
661 ptr = NULL;
662 ptr = strstr (spot->encrypt_type, WIFI_STATION_PROFILE_ENCRYPT_WEP);
663 if (ptr) {
664 sprintf (cmd, "SET_NETWORK %d wep_tx_keyidx %d", id , spot->keyID);
665 sta_docmd (cmd);
666
667 key_len = strlen (spot->password);
668 wf_log ("key_len=%d,password_wep=%s", key_len, spot->password);
669 memset (cmd, 0, WIFI_STATION_CMD_LEN);
670 if (key_len == 5 || key_len == 13) { //ascii
671 sprintf (cmd, "SET_NETWORK %d wep_key%d \"%s\"", id , spot->keyID, spot->password);
672 } else if (key_len == 10 || key_len == 26) { //hex
673 sprintf (cmd, "SET_NETWORK %d wep_key%d %s", id, spot->keyID, spot->password);
674 } else {
675 sprintf (cmd, "SET_NETWORK %d wep_key%d \"%s\"", id, spot->keyID, spot->password);
676 }
677 sta_docmd (cmd);
678 }
679
680 ptr = NULL;
681 ptr = strstr (spot->auth_mode, WIFI_STATION_PROFILE_AUTH_SHARED);
682 if (ptr) {
683 sprintf (cmd, "SET_NETWORK %d auth_alg SHARED", id);
684 sta_docmd (cmd);
685 }
686
687 sprintf (cmd, "SET_NETWORK %d scan_ssid 1", id);
688 sta_docmd (cmd);
689
690
691 sta_docmd ("SAVE_CONFIG");
692 sta_docmd ("LIST_NETWORKS");
693}
694
695
696
697static void dump_spot(void)
698{
699 int i = 0;
700 int spot_num = 0;
701 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
702 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
703 spot_num = atoi (wifi_profile_num);
704 if (spot_num > WIFI_STATION_SPOT_LIST_NUM || spot_num < 0)
705 return;
706
707//´ËʱµÄ¸öÊýÒѾ­ÊǼõ¹ýµÄÁË£¬ËùÒÔ¸öÊýÒª¼Ó1
708 for (i = 0; i < spot_num; i++) {
709 if (NULL != g_spot_list_p[i]) {
710 wf_log ("spot[%d]%s,%d,%d,%d,%s,%s,%s,%s,%d,%s,%d", i , g_spot_list_p[i]->profile_name,
711 g_spot_list_p[i]->from_provider, g_spot_list_p[i]->connect_status, g_spot_list_p[i]->signal,
712 g_spot_list_p[i]->ssid, g_spot_list_p[i]->auth_mode, g_spot_list_p[i]->encrypt_type,
713 g_spot_list_p[i]->password, g_spot_list_p[i]->keyID, g_spot_list_p[i]->mac, g_spot_list_p[i]->network_id);
714 } else {
715 wf_log ("ERROR, g_spot_list_p[%d] should not be NULL spot_num = %d\n ", i, spot_num);
716 }
717 }
718}
719
720static void del_spot (int id)
721{
722 int i = 0;
723 int spot_num = 0;
724 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
725 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
726 spot_num = atoi (wifi_profile_num);
727 if (spot_num >= WIFI_STATION_SPOT_LIST_NUM || spot_num < 0)//klocwork
728 return;
729
730 safe_free (g_spot_list_p[id]);
731
732//´ËʱµÄ¸öÊýÒѾ­ÊǼõ¹ýµÄÁË£¬ËùÒÔ¸öÊýÒª¼Ó1
733 for (i = id + 1; i <= spot_num; i++) {
734
735 if (g_spot_list_p[i] != NULL) {
736 g_spot_list_p[i - 1] = g_spot_list_p[i];
737 g_spot_list_p[i] = NULL;
738 }
739 }
740
741
742 //dump_spot();
743}
744
745/*webui ÒѾ­½«ÐÂÔöµÄÈÈµã´æÈënv
746 ´Ëº¯ÊýÐèÒª½«nvÖÐÐÂÈȵ㠸üе½ÎÒÃÇÓ¦ÓõÄÈ«¾Ö±äÁ¿ÖÐ
747 ²¢½«ÐÂÈÈµã´æÈëÅäÖÃÎļþ£¬²¢½«»ñÈ¡µÄnetworkid ´æÈëÈ«¾Ö±äÁ¿
748
749*/
750static void webui_update_spot_list (updateinfo_t *info)
751{
752 int i = 0;
753 spot_t *spot = NULL;
754 int spot_num = 0;
755 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
756 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
757 int flag = 0;
758
759 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
760 spot_num = atoi (wifi_profile_num);
761
762 wf_log ("action=%s profile=%s", info->action, info->spot);
763
764//Ìí¼ÓµÄÈȵ㲻¿ÉÄÜÊÇÔËÓªÉÌÈȵ㣬²»¿ÉÄÜÊÇÕýÁ´½ÓµÄÈȵ㣬ËùÒÔ¶¼ÊÇÆÕͨÈȵ㣬ֱ½Ó°´Ë³Ðò¼ÓÔÚºóÃæ
765 if (strcmp (info->action, "add") == 0) {
766
767 wf_log ("%s processing", info->action);
768 if (spot_num < 1 || spot_num > WIFI_STATION_SPOT_LIST_NUM) {//klocwork
769 return;
770 }
771
772 spot = parse_spot_string (info->spot);
773 if (spot == NULL) {//klocwork
774 return;
775 }
776 spot->network_id = -1;
777 save_config (spot);
778 g_spot_list_p[spot_num - 1] = spot;
779
780//Èç¹ûÊǶϿª×´Ì¬£¬Ö±½ÓÁ¬½Ó£¬Èç¹ûÊÇÁ¬½Ó״̬£¬Ö»ÐèÒªenable¾Í¿ÉÒÔÁË
781 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
782
783 if (strcmp (sta_ip_status, "disconnect") == 0) {
784 sta_connect (spot->network_id);
785 }
786
787 } else if (strcmp (info->action, "modify") == 0) {
788 wf_log ("%s processing", info->action);
789 spot = parse_spot_string (info->spot);
790 if (spot == NULL) {//klocwork
791 return;
792 }
793 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
794 if (g_spot_list_p[i] != NULL && !strcmp (g_spot_list_p[i]->profile_name, spot->profile_name)) {
795 spot->network_id = g_spot_list_p[i]->network_id;
796 safe_free (g_spot_list_p[i]);
797 g_spot_list_p[i] = spot;
798 save_config (spot);
799 flag = 1;
800 break;
801 }
802 }
803 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
804
805 if (strcmp (sta_ip_status, "disconnect") == 0) {
806 sta_connect (spot->network_id);
807 }
808 if (flag == 0) {//for klocwork,¼Ç¼ȫ¾ÖµÄ²»ÄÜɾ
809 safe_free (spot);
810 }
811 } else if (strcmp (info->action, "delete") == 0) {
812 int network_id = -1;;
813 int del_id = -1;
814 char cmd[WIFI_STATION_CMD_LEN] = {0};
815 wf_log ("%s processing", info->action);
816
817 dump_spot();
818 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
819 if (g_spot_list_p[i] != NULL && !strncmp (g_spot_list_p[i]->profile_name, info->spot, strlen(g_spot_list_p[i]->profile_name))) {
820 network_id = g_spot_list_p[i]->network_id;
821 del_id = i;
822 break;
823 }
824 }
825
826 if (network_id != -1) {
827 sprintf (cmd, "REMOVE_NETWORK %d", network_id);
828 sta_docmd (cmd);
829 sta_docmd ("SAVE_CONFIG");
830
831 if (del_id != -1)
832 del_spot (del_id);
833 } else {
834 wf_log ("spot:%s not found", info->spot);
835 }
836
837 }
838 dump_spot();
839}
840
841
842
843/*
844 ÔÚɾ³ýÈȵã¹ý³ÌÖÐͻȻ¶Ïµç£¬ÎÞ·¨±£´æµ½conf Îļþ£¬ÐèÒªÖØÐÂͬ²½Ò»ÏÂ
845
846*/
847
848static int get_spot_info_from_conf (spot_ssid_id_t *info, int num)
849{
850
851 int i = 0;
852 char cmd[WIFI_STATION_CMD_LEN] = {0};
853 char * reply = NULL;
854
855 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
856
857 info->id = -1;
858 memset (info->ssid, 0 , sizeof (info->ssid));
859
860 sprintf (cmd, "GET_NETWORK %d ssid", i);
861 reply = sta_docmd (cmd);
862 if (reply != NULL) {
863 wf_log ("[update networkid from conf]reply SSID=%s", reply);
864 strncpy (info->ssid, reply, sizeof (info->ssid)-1);
865 } else {
866 wf_log ("[update networkid from conf]conf read over!");
867 break;
868 }
869
870 if (strlen (info->ssid) > 0) {
871 wf_log ("[update networkid from conf] delete quote");
872
873 delete_quote (info->ssid);
874 }
875
876 info->id = i;
877 wf_log ("[update networkid from conf]SSID=%s, network_id=%d", info->ssid, i);
878 info += 1;
879 }
880
881 return i;
882}
883
884
885/**/
886static void delete_quote (char * str)
887{
888 int len = 0;
889 printf("str=%s\n", str);
890 if (*str == '\"') {
891 //strcpy (str, str + 1); //cov m
892 len = strlen(str + 1);
893 memmove(str, str + 1, len + 1);
894 *(str + len) = '\0';
895 }
896 printf("str=%s\n", str);
897
898 if (*(str + strlen(str) - 1) == '\"')
899 *(str + strlen(str) - 1) = '\0';
900
901 printf("str=%s\n", str);
902}
903
904
905//1. ¿ªÆô¹Ø±Õapsta
906/*
907
908 ¿ªÆô: conf fileÖØÐ±»wpa_supplicant¶ÁÈ¡£¬ÖØÆô·ÖÅänetwork id£¬ÐèÒªÖØÐÂͬ²½µ½spot list
909
910
911 ¹Ø±Õ: ²»Çå¿Õconf file£¬ ΪÁ˺͹ػú±£³ÖÒ»Ö£¬Ò²ÐèÒª½«spot ÄÚÈÝÇå¿Õ
912*/
913
914
915//2. ¿ª»ú¹Ø»ú
916/*
917
918 ¹Ø»ú: Õý³£¹Ø»úºÍÒì³£¶Ïµç£¬¶¼²»»áÓÐÈκβÙ×÷Á÷³Ì£¬confÎļþ²»»áÐÞ¸Ä
919 ¿ª»ú: Ö±½ÓÖØÐ·ÖÅäspot Äڴ棬´Ónvͬ²½spotÊý¾Ý£¬È»ºó´ÓconfÎļþÖÐͬ²½networkid£¬Èç¹ûûÓжÔÓ¦µÄnetworid
920
921 ÔòÐèÒªÖØÐÂÌí¼Ó¸ÃÈȵ㵽confÎļþÖУ¬±ÈÈçÊÇÖ±½ÓÔ¤ÖõÄÔËÓªÉÌÈȵã
922
923*/
924void sync_nv_spot_list_conf_file()
925{
926 spot_ssid_id_t g_spot_ssid_id_p[WIFI_STATION_SPOT_LIST_NUM] = {{0}};
927
928 int i = 0, j = 0;
929 int spot_num = 0;
930 int conf_spot_num = 0;
931
932 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
933 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
934 spot_num = atoi (wifi_profile_num);
935 if (spot_num > WIFI_STATION_SPOT_LIST_NUM || spot_num < 0)
936 return;
937
938 memset (g_spot_ssid_id_p, 0, WIFI_STATION_SPOT_LIST_NUM * sizeof (struct spot_ssid_id));
939 conf_spot_num = get_spot_info_from_conf (g_spot_ssid_id_p, spot_num);
940 wf_log ("spot_num=%d,conf_spot_num=%d", spot_num, conf_spot_num);
941
942 for (i = 0; i < spot_num; i++) {
943 wf_log ("[sync spot before]SSID:%s, networkid=%d", g_spot_list_p[i]->ssid, g_spot_list_p[i]->network_id);
944 for (j = 0; j < WIFI_STATION_SPOT_LIST_NUM; j ++) {
945//[sync before]spot SSID:CPE_E3DE4E, conf ssid="CPE_E3DE4E", dubble quote !!!!!!
946 wf_log ("[compare to conf]spot SSID:%s, conf ssid=%s", g_spot_list_p[i]->ssid, g_spot_ssid_id_p[j].ssid);
947 if (strcmp (g_spot_ssid_id_p[j].ssid, g_spot_list_p[i]->ssid) == 0) {
948 g_spot_list_p[i]->network_id = g_spot_ssid_id_p[j].id;
949 break;
950 }
951 }
952
953 if (-1 == g_spot_list_p[i]->network_id) {
954 save_config (g_spot_list_p[i]); //Èç¹ûconfÖÐûÓУ¬ÔòÐèÒª¼ÓÈ룬update spot networkid
955 }
956 wf_log ("[sync spot after]SSID:%s, networkid=%d", g_spot_list_p[i]->ssid, g_spot_list_p[i]->network_id);
957 }
958
959/*ÌÞ³ý confÎļþÖÐûÓÐÕý³£É¾³ýµÄspot*/
960 for (i = 0; i < conf_spot_num; i++) {
961 int conf_spot_in_nv = 0;
962
963 wf_log ("[sync conf before]SSID:%s, networkid=%d", g_spot_ssid_id_p[i].ssid, g_spot_ssid_id_p[i].id);
964 if (strlen (g_spot_ssid_id_p[i].ssid) > 0) {
965 for (j = 0; j < spot_num; j ++) {
966 wf_log ("[compare to spot]g_spot_list_p[j]->ssid=%s", g_spot_list_p[j]->ssid);
967 if (strcmp (g_spot_ssid_id_p[i].ssid, g_spot_list_p[j]->ssid) == 0) {
968 conf_spot_in_nv = 1;
969 break;
970 }
971 }
972
973 // Èç¹û²»´æÔÚnvÖУ¬ÐèҪɾ³ý
974 if (0 == conf_spot_in_nv) {
975 char cmd[WIFI_STATION_CMD_LEN] = {0};
976 sprintf (cmd, "REMOVE_NETWORK %d", g_spot_ssid_id_p[i].id);
977 sta_docmd (cmd);
978 }
979 }
980
981 wf_log ("[sync conf after]SSID:%s, networkid=%d", g_spot_ssid_id_p[i].ssid, g_spot_ssid_id_p[i].id);
982 }
983
984
985
986}
987
988//»ñÈ¡ÈȵãÁÐ±í£¬·µ»ØÒ»¸öÖ¸ÕëÊý×飬·ÅÈëÈ«¾Ö
989static void get_spot_list (void)
990{
991 int i = 0;
992 int j = 0;
993 char spot_string[WIFI_STATION_SPOT_LEN] = {0};
994 char wifi_profile_nv_name[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
995 spot_t *spot = NULL;
996 int spot_num = 0;
997 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
998 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
999 spot_num = atoi (wifi_profile_num);
1000
1001 wf_log ("free [g_spot_list_p] all spot");
1002
1003 //ÏÈÊÍ·Å֮ǰ´æ´¢µÄÈȵãÁбíÄÚ´æ
1004 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1005 safe_free (g_spot_list_p[i]);
1006 //g_spot_list_p[i] = NULL;
1007 }
1008
1009 if (spot_num > 0 && spot_num <= WIFI_STATION_SPOT_LIST_NUM) {
1010 for (i = 0; i < spot_num; i++) {
1011 if (i == 0) {
1012 sc_cfg_get ("wifi_profile", spot_string, WIFI_STATION_SPOT_LEN);
1013 } else {
1014 memset (wifi_profile_nv_name, 0, WIFI_STATION_PROFILE_NV_NAME_LEN);
1015 snprintf (wifi_profile_nv_name, WIFI_STATION_PROFILE_NV_NAME_LEN, "wifi_profile%d", i);
1016 sc_cfg_get (wifi_profile_nv_name, spot_string, WIFI_STATION_SPOT_LEN);
1017 }
1018
1019 wf_log ("get_spot_list spot_string[%d]= %s", i, spot_string);
1020 spot = parse_spot_string (spot_string);
1021 if (spot != NULL) {
1022 g_spot_list_p[j] = spot;
1023 j++;
1024 }
1025 }
1026 }
1027
1028}
1029
1030static void save_spot_list (void)
1031{
1032 int i = 0;
1033 char spot_string[WIFI_STATION_SPOT_LEN] = {0};
1034 char wifi_profile_nv_name[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1035
1036 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1037 if (g_spot_list_p[i] != NULL) {
1038 snprintf (spot_string, WIFI_STATION_SPOT_LEN, "%s,%d,%d,%d,%s,%s,%s,%s,%d,%s", g_spot_list_p[i]->profile_name,
1039 g_spot_list_p[i]->from_provider, g_spot_list_p[i]->connect_status, g_spot_list_p[i]->signal,
1040 g_spot_list_p[i]->ssid, g_spot_list_p[i]->auth_mode, g_spot_list_p[i]->encrypt_type,
1041 g_spot_list_p[i]->password, g_spot_list_p[i]->keyID, g_spot_list_p[i]->mac);
1042
1043 wf_log (" g_spot_list_p[%d]=%s, [%d]", i, spot_string, g_spot_list_p[i]->network_id);
1044
1045 if (i == 0) {
1046 sc_cfg_set ("wifi_profile", spot_string);
1047 } else {
1048 snprintf (wifi_profile_nv_name, WIFI_STATION_PROFILE_NV_NAME_LEN, "wifi_profile%d", i);
1049 sc_cfg_set (wifi_profile_nv_name, spot_string);
1050 }
1051 }
1052 }
1053}
1054
1055
1056/*
1057½«Á´½Ó³É¹¦µÄÈȵ㣬Åŵ½µÚһλ
1058*/
1059static void resort_spot_list (void)
1060{
1061
1062 int i = 0, j = 0;
1063 int index = 0;
1064 spot_t * spot = NULL;
1065 // char profile[WIFI_STATION_SPOT_PROFILE_NAME_LEN] = {0};
1066
1067// sc_cfg_get("EX_wifi_profile", profile, WIFI_STATION_SPOT_PROFILE_NAME_LEN);
1068
1069
1070 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1071 if (g_spot_list_p[i] != NULL) {
1072 if (g_spot_list_p[i]->connect_status == 1) {
1073 index = i;
1074 spot = g_spot_list_p[i];
1075 break;
1076 }
1077 }
1078
1079 }
1080
1081 if (index == 0) return;
1082
1083 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1084 if (1 == g_spot_list_p[i]->from_provider) {
1085 continue;
1086 } else {
1087 //move other spot
1088 for (j = index; j > i; j--) { // ¼Ù¶¨×îºóÒ»¸ö¿Ï¶¨Êǿյ쬲»¿ÉÄÜ10¸öÂúÁË£¬»¹ÔÚÌí¼ÓÈȵ㣬֮ǰÒѾ­ÅжϴíÎó·µ»ØÁË
1089 if (g_spot_list_p[j] == NULL) continue;
1090 else {
1091 g_spot_list_p[j] = g_spot_list_p[j - 1]; //j must greater than 0
1092 }
1093 }
1094 if (i != index) {
1095 g_spot_list_p[i] = spot;
1096 wf_log ("[%s]SSID=[%s] from %d move to %d", __FILE__, spot->ssid, index, i);
1097 } else {
1098 wf_log ("[%s]SSID=[%s] do not move", __FILE__, spot->ssid);
1099 }
1100 break;
1101 }
1102 }
1103
1104 //¸ü¸Ä˳Ðòºó£¬ÐèÒªÖØÐ±¸·Ý signal
1105 //reback_up_signal();
1106
1107 save_spot_list();
1108
1109}
1110
1111
1112static void update_spot_list_signal_connect_status (short signal, short connect_status)
1113{
1114 int i = 0;
1115
1116 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1117 if (g_spot_list_p[i] != NULL) {
1118 if (signal >= 0) {
1119 g_spot_list_p[i]->signal = signal;
1120 }
1121
1122 if (connect_status >= 0) {
1123 g_spot_list_p[i]->connect_status = connect_status;
1124 }
1125 }
1126 }
1127
1128}
1129
1130static void update_spot_connect_status_by_status_result (BOOL is_connected)
1131{
1132 int i = 0;
1133 char spot_string[WIFI_STATION_SPOT_LEN] = {0};
1134 char wifi_profile_nv_name[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1135
1136 wf_log ("is_connected=%d, g_linked_network_id= %d", is_connected, g_linked_network_id);
1137
1138 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1139 if (g_spot_list_p[i] != NULL && g_spot_list_p[i] ->network_id == g_linked_network_id) {
1140 if (is_connected == TRUE) {
1141 g_spot_list_p[i]->connect_status = 1;
1142 } else {
1143 g_spot_list_p[i]->connect_status = 0;
1144 }
1145
1146 if (strcmp (g_spot_list_p[i]->mac, "0F:00:00:00:00:00") == 0) {
1147 strcpy (g_spot_list_p[i]->mac, g_linked_ap_mac);
1148 }
1149 wf_log ("g_spot_list_p[%d]->mac=%s", i, g_spot_list_p[i]->mac);
1150 wf_log ("profile[%d]=%d", i, g_spot_list_p[i]->connect_status);
1151
1152 snprintf (spot_string, WIFI_STATION_SPOT_LEN, "%s,%d,%d,%d,%s,%s,%s,%s,%d,%s", g_spot_list_p[i]->profile_name,
1153 g_spot_list_p[i]->from_provider, g_spot_list_p[i]->connect_status, g_spot_list_p[i]->signal,
1154 g_spot_list_p[i]->ssid, g_spot_list_p[i]->auth_mode, g_spot_list_p[i]->encrypt_type,
1155 g_spot_list_p[i]->password, g_spot_list_p[i]->keyID, g_spot_list_p[i]->mac);
1156
1157 if (i == 0) {
1158 sc_cfg_set ("wifi_profile", spot_string);
1159 } else {
1160 snprintf (wifi_profile_nv_name, WIFI_STATION_PROFILE_NV_NAME_LEN, "wifi_profile%d", i);
1161 sc_cfg_set (wifi_profile_nv_name, spot_string);
1162 }
1163
1164 break;
1165 }
1166 }
1167}
1168
1169static void update_spot_signal_by_scan_result (scan_result_t *scan_result)
1170{
1171 int i = 0;
1172
1173 if (scan_result == NULL) {
1174 return;
1175 }
1176
1177 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
1178 if (g_spot_list_p[i] != NULL) {
1179 if (strcmp (g_spot_list_p[i]->mac, scan_result->mac) == 0 &&
1180
1181 strcmp (g_spot_list_p[i]->ssid, scan_result->ssid) == 0
1182 // && (strcmp(g_spot_list_p[i]->auth_mode, scan_result->auth_mode) == 0 ||
1183 // (strcmp(g_spot_list_p[i]->auth_mode,WIFI_STATION_PROFILE_AUTH_WPA_EAP)==0 && strcmp(scan_result->auth_mode,"802.1X")==0))
1184 /*&& strstr(g_spot_list_p[i]->encrypt_type, scan_result->encrypt_type) != NULL*/)
1185// Õë¶ÔÒþ²ØÈȵ㣬ÊÖ¶¯Ìí¼ÓµÄcipherΪauto£¬µ¼ÖÂÎÞ·¨Ê¶±ð¸üÐÂÈȵãÐźÅ
1186//±¾À´ÐèҪʹÓÃmac½øÐÐÆ¥Å䣬Î÷°²²»ÅäºÏ£¬Ö»ÓÐÈ¡ÕâÑùµÄ·±Ëö°ì·¨
1187//webui×Ô¶¯Ê¶±ðtkip aesΪauto£¬±£´æÔÚnv profileÀïÃæÎªTKIPCCMP,µ¼Ö´˴¦ÎÞ·¨Æ¥Å䣬ÎÞ·¨¸üÐÂÈȵãÐźÅ
1188//EAP-SIM/AKA, ÓëÔ­±¾Ð´µÄ802.1xÒ²²»Æ¥Å䣬µ¼ÖÂÎÞ·¨¸üÐÂÈȵãÐźÅ
1189//ÐÞ¸ÄΪɨÃèÈȵãµÄ¼ÓÃÜÀàÐÍΪTKIP CCMP,Ö»ÒªÄÜÔÚ±£´æÈȵãÖÐÕÒµ½´Ë×Ö·û£¬ÔòÈÏΪÊÇͬһ¸öÈȵ㣬µ«ÊÇÎÞ·¨½â¾öÕæµÄÓÐÁ½¸öÏàͬµÄÈȵ㣬Õâ¸öÎÞ·¨Ê¶±ð
1190 {
1191 g_spot_list_p[i]->signal = scan_result->signal;
1192 break;
1193 }
1194 }
1195 }
1196}
1197
1198
1199static void sort_scan_result_by_signal (scan_result_t **scan_result, int size)
1200{
1201 int i, j = 0;
1202 scan_result_t *insertnode = NULL;
1203
1204 if (scan_result == NULL) {
1205 return;
1206 }
1207
1208 for (i = 0; i < size; i++) {
1209 if (scan_result[i] == NULL) {
1210 return;
1211 }
1212 }
1213
1214 //Ö±½Ó²åÈëÅÅÐòËã·¨
1215 for (i = 1; i < size; i++) { //ÒÀ´Î²åÈëÊýÖµ
1216 insertnode = scan_result[i]; //ÉèÖÃÓû²åÈëµÄÊýÖµ,ÉÚ±ø
1217 j = i - 1; //Óû²åÈëÊý×éµÄ¿ªÊ¼Î»ÖÃ
1218 while (j >= 0 && insertnode->signal > scan_result[j]->signal) { //ÕÒÊʵ±µÄ²åÈëλÖÃ
1219 scan_result[j + 1] = scan_result[j];
1220 j--;
1221 }
1222 scan_result[j + 1] = insertnode;
1223 }
1224
1225}
1226
1227/*
1228 inputStr: if there is a \, then
1229 outputStr: add \\ before, to be \\\
1230
1231
1232*/
1233
1234static char* process_backlash (char* inputStr, char* outputStr)
1235{
1236 int len = 0;
1237 int i, j = 0;
1238 if ( (NULL == inputStr) || (NULL == outputStr)) {
1239 return NULL;
1240 }
1241
1242 len = strlen (inputStr);
1243 for (i = 0; i < len; i++) { //linux תÒå
1244 if ( (inputStr[i] == 0x5c && inputStr[i + 1] == 'x')) {
1245 outputStr[j++] = 0x5c;
1246 }
1247 outputStr[j++] = inputStr[i];
1248 }
1249 outputStr[j] = '\0';
1250 //printf("[process_backlash]inputStr=%s,outputStr=%s",inputStr,outputStr);
1251
1252 return outputStr;
1253}
1254
1255static void save_scan_result (scan_result_t* scan_result, char *scan_result_str, char *scan_result_str1)
1256{
1257 char *scan_result_str_temp = NULL;
1258 char one_scan_result_str[WIFI_STATION_SCAN_ONE_RESULT_STR_LEN] = {0};
1259 int one_scan_result_str_len = 0;
1260
1261 char ssid[128] = {0};
1262 if (scan_result == NULL
1263 || scan_result_str == NULL
1264 || scan_result_str1 == NULL) {
1265 return;
1266 }
1267
1268 if (strlen (scan_result->ssid) <= 0) {
1269 return;
1270 }
1271
1272 scan_result_str_temp = safe_malloc (WIFI_STATION_SCAN_RESULT_STR_LEN, TRUE);
1273
1274
1275 process_backlash (scan_result->ssid, ssid);
1276
1277 snprintf (one_scan_result_str, WIFI_STATION_SCAN_ONE_RESULT_STR_LEN, "%d,%d,%s,%d,%d,%s,%s,%s", scan_result->from_provider, scan_result->connect_status, ssid,
1278 scan_result->signal, scan_result->channel, scan_result->auth_mode, scan_result->encrypt_type, scan_result->mac);
1279
1280 one_scan_result_str_len = strlen (one_scan_result_str);
1281
1282 //cfg½Ó¿ÚÊÇÒÔ"nv name=nv value\0"·½Ê½À´´æ´¢£¬ÇÒÒ»´Î×Ϊ1024£¬Òò´ËÕâ±ßÊÇ+2
1283 if (one_scan_result_str_len + strlen (scan_result_str) + strlen ("EX_APLIST") + 2 > WIFI_STATION_SCAN_RESULT_STR_LEN) {
1284 if (one_scan_result_str_len + strlen (scan_result_str1) + strlen ("EX_APLIST1") + 2 > WIFI_STATION_SCAN_RESULT_STR_LEN) {
1285 //³¬³ö2048×Ö½ÚÁË£¬Í˳ö
1286 safe_free (scan_result_str_temp);
1287 return;
1288 } else {
1289 safe_strcpy (scan_result_str_temp, scan_result_str1, strlen(scan_result_str1) + 1);
1290
1291 strncat (scan_result_str_temp, one_scan_result_str, one_scan_result_str_len);
1292 strncat (scan_result_str_temp, ";", 1);
1293
1294 //¸³Öµ½á¹û
1295 safe_strcpy (scan_result_str1, scan_result_str_temp, WIFI_STATION_SCAN_RESULT_STR_LEN);
1296 }
1297 } else {
1298 safe_strcpy (scan_result_str_temp, scan_result_str, strlen(scan_result_str) + 1);//wlocwork
1299
1300 strncat (scan_result_str_temp, one_scan_result_str, one_scan_result_str_len);
1301 strncat (scan_result_str_temp, ";", 1);
1302
1303 //¸³Öµ½á¹û
1304 safe_strcpy (scan_result_str, scan_result_str_temp, WIFI_STATION_SCAN_RESULT_STR_LEN);
1305 }
1306
1307 safe_free (scan_result_str_temp);
1308
1309}
1310
1311
1312static scan_result_t* parse_scan_result (Var *var)
1313{
1314 char *ptr = NULL;
1315 char *ptr1 = NULL;
1316 char *ptr2 = NULL;
1317 char *ptr3 = NULL;
1318 char *ptr4 = NULL;
1319 char *ptr5 = NULL;
1320 scan_result_t *scan_result = NULL;
1321 int frequency = 0;
1322 int signal = 0;
1323 char flags[WIFI_STATION_FLAGS_LEN] = {0};
1324 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
1325
1326 char operater_ap[32] = {0};
1327
1328 if (var == NULL) {
1329 return NULL;
1330 }
1331
1332 //²éÕÒÊÇ·ñÊǿɺöÂÔµÄÒ»ÐÐ
1333 ptr = strstr (var->val.str.sval, WIFI_STATION_SCAN_RESULT_PARSE_IGNORE);
1334 if (ptr) {
1335 return NULL;
1336 } else {
1337 //·ÖÅäɨÃè½á¹ûÄÚ´æ
1338 scan_result = safe_malloc (sizeof (scan_result_t), FALSE);
1339 if (scan_result == NULL) {
1340 return NULL;
1341 }
1342
1343 //Æ¥ÅäɨÃè½á¹ûÐÅÏ¢
1344 sscanf (var->val.str.sval, "%19[0-9,a-f,A-F,:] %d %d %127s %127[^\n]", scan_result->mac, &frequency, &signal, flags, scan_result->ssid);
1345
1346 if (strncmp (scan_result->ssid, "\\x00", 4) == 0) {
1347 wf_log ("scan_result->mac=[%s] scan_result->ssid=[%s]is a hidden AP",
1348 scan_result->mac, scan_result->ssid);
1349 safe_free (scan_result);
1350 return NULL;
1351 }
1352
1353
1354 sc_cfg_get ("operater_ap", operater_ap, sizeof (operater_ap));
1355 if (strcmp (scan_result->ssid, operater_ap) == 0) {
1356 scan_result->from_provider = 1;//fromProvider ÊÇ·ñÀ´×ÔÔËÓªÉÌ
1357 } else {
1358 scan_result->from_provider = 0;//fromProvider ÊÇ·ñÀ´×ÔÔËÓªÉÌ
1359 }
1360
1361 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
1362 //connectStatus Á¬½Ó״̬
1363 if (g_linked_network_id != -1 && strcmp (sta_ip_status, "connect") == 0) {
1364 int i = 0;
1365 i = find_linked_ap_index();
1366 if (i != -1) {
1367 wf_log ("g_spot_list_p[i]->ssid=[%s],scan_result->ssid=[%s]",
1368 g_spot_list_p[i]->ssid, scan_result->ssid);
1369
1370 if (strcmp (g_spot_list_p[i]->mac, scan_result->mac) == 0
1371 && strcmp (g_spot_list_p[i]->ssid, scan_result->ssid) == 0) {
1372 scan_result->connect_status = 1;
1373 } else {
1374 scan_result->connect_status = 0;
1375
1376 }
1377 wf_log ("connect_status = [%d]", scan_result->connect_status);
1378 } else {
1379 scan_result->connect_status = 0;
1380 }
1381
1382 } else {
1383 scan_result->connect_status = 0;
1384 }
1385
1386 //signalÐźÅÇ¿¶È
1387 if (signal > WIFI_STATION_SIGNAL_LEVEL4) {
1388 scan_result->signal = 4;
1389 } else if (signal > WIFI_STATION_SIGNAL_LEVEL3) {//kw 3
1390 scan_result->signal = 3;
1391 } else if (signal > WIFI_STATION_SIGNAL_LEVEL2) {
1392 scan_result->signal = 2;
1393 } else if (signal > WIFI_STATION_SIGNAL_LEVEL1) {
1394 scan_result->signal = 1;
1395 } else {
1396 scan_result->signal = 0;
1397 }
1398
1399 //channel
1400 switch (frequency) {
1401 case WIFI_STATION_CHANNEL1:
1402 scan_result->channel = 1;
1403 break;
1404 case WIFI_STATION_CHANNEL2:
1405 scan_result->channel = 2;
1406 break;
1407 case WIFI_STATION_CHANNEL3:
1408 scan_result->channel = 3;
1409 break;
1410 case WIFI_STATION_CHANNEL4:
1411 scan_result->channel = 4;
1412 break;
1413 case WIFI_STATION_CHANNEL5:
1414 scan_result->channel = 5;
1415 break;
1416 case WIFI_STATION_CHANNEL6:
1417 scan_result->channel = 6;
1418 break;
1419 case WIFI_STATION_CHANNEL7:
1420 scan_result->channel = 7;
1421 break;
1422 case WIFI_STATION_CHANNEL8:
1423 scan_result->channel = 8;
1424 break;
1425 case WIFI_STATION_CHANNEL9:
1426 scan_result->channel = 9;
1427 break;
1428 case WIFI_STATION_CHANNEL10:
1429 scan_result->channel = 10;
1430 break;
1431 case WIFI_STATION_CHANNEL11:
1432 scan_result->channel = 11;
1433 break;
1434 case WIFI_STATION_CHANNEL12:
1435 scan_result->channel = 12;
1436 break;
1437 case WIFI_STATION_CHANNEL13:
1438 scan_result->channel = 13;
1439 break;
1440 default:
1441 scan_result->channel = 0;
1442 break;
1443 }
1444
1445 //authMode¼øÈ¨Ä£Ê½
1446 ptr = NULL;
1447 ptr2 = NULL;
1448 ptr3 = NULL;
1449 ptr = strstr (flags, WIFI_STATION_AUTH_WPA_PSK);
1450 ptr1 = strstr (flags, WIFI_STATION_AUTH_WPA3); //wpa2/wpa3: [WPA2-PSK+SAE-CCMP] wpa3: [WPA2-SAE-CCMP][ESS]
1451 ptr2 = strstr (flags, WIFI_STATION_AUTH_WPA2_PSK);
1452 ptr3 = strstr (flags, WIFI_STATION_AUTH_SHARED);
1453 ptr4 = strstr (flags, WIFI_STATION_AUTH_WPA_EAP);
1454 ptr5 = strstr (flags, WIFI_STATION_AUTH_WPA2_EAP);
1455 if (ptr && ptr2) {
1456 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "WPAPSKWPA2PSK");
1457 } else if (ptr1 && ptr2) {
1458 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "WPA2WPA3");
1459 } else if (ptr) {
1460 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "WPAPSK");
1461 } else if (ptr2) {
1462 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "WPA2PSK");
1463 } else if (ptr1) {
1464 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "WPA3Personal");
1465 } else if (ptr3) {
1466 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "SHARED");
1467 } else if (ptr4 && ptr5) {
1468
1469 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "802.1X");
1470 //TODO scan_result->from_provider = 1;//fromProvider ÊÇ·ñÀ´×ÔÔËÓªÉÌ
1471 } else if (ptr4) {
1472 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "802.1X");
1473
1474 //TODO scan_result->from_provider = 1;//fromProvider ÊÇ·ñÀ´×ÔÔËÓªÉÌ
1475 } else if (ptr5) {
1476 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "802.1X");
1477
1478 //TODO scan_result->from_provider = 1;//fromProvider ÊÇ·ñÀ´×ÔÔËÓªÉÌ
1479 } else {
1480 snprintf (scan_result->auth_mode, WIFI_STATION_AUTH_MODE_LEN, "OPEN");
1481 }
1482
1483 //encryptType¼ÓÃÜÀàÐÍ
1484 ptr = NULL;
1485 ptr2 = NULL;
1486 ptr3 = NULL;
1487 ptr = strstr (flags, WIFI_STATION_ENCRYPT_CCMP);
1488 ptr2 = strstr (flags, WIFI_STATION_ENCRYPT_TKIP);
1489 ptr3 = strstr (flags, WIFI_STATION_ENCRYPT_WEP);
1490 if (ptr && ptr2) {
1491 snprintf (scan_result->encrypt_type, WIFI_STATION_ENCRYPT_TYPE_LEN, "TKIPCCMP");
1492 } else if (ptr) {
1493 snprintf (scan_result->encrypt_type, WIFI_STATION_ENCRYPT_TYPE_LEN, "CCMP");
1494 } else if (ptr2) {
1495 snprintf (scan_result->encrypt_type, WIFI_STATION_ENCRYPT_TYPE_LEN, "TKIP");
1496 } else if (ptr3) {
1497 snprintf (scan_result->encrypt_type, WIFI_STATION_ENCRYPT_TYPE_LEN, "WEP");
1498 } else {
1499 snprintf (scan_result->encrypt_type, WIFI_STATION_ENCRYPT_TYPE_LEN, "NONE");
1500 }
1501 }
1502
1503 return scan_result;
1504
1505}
1506
1507
1508static void handle_scan_result (vector_t* v)
1509{
1510 int i = 0;
1511 int j = 0;
1512 Var *var = NULL;
1513 char *scan_result_str = NULL;
1514 char *scan_result_str1 = NULL;
1515 int scan_result_str_len = 0;
1516 int scan_result_str_len1 = 0;
1517 scan_result_t *scan_result = NULL;
1518
1519 if (v == NULL) {
1520 return;
1521 }
1522
1523 scan_result_str = safe_malloc (WIFI_STATION_SCAN_RESULT_STR_LEN, FALSE);
1524 scan_result_str1 = safe_malloc (WIFI_STATION_SCAN_RESULT_STR_LEN, FALSE);
1525
1526 if (scan_result_str == NULL || scan_result_str1 == NULL) {
1527 safe_free (scan_result_str);
1528 safe_free (scan_result_str1);
1529 return;
1530 }
1531
1532 //ÏÈÊÍ·Å֮ǰ´æ´¢µÄɨÃèÈȵãÁбíÄÚ´æ
1533 for (i = 0; i < WIFI_STATION_SCAN_RESULT_LIST_NUM; i++) {
1534 safe_free (g_scan_result_list_p[i]);
1535 //g_scan_result_list_p[i] = NULL;
1536 //printf("xujian safe_free g_scan_result_list_p[%d] = %d \n", i, g_scan_result_list_p[i]);
1537 }
1538
1539// Ö»ÄÜÊÇÔÚwebui¸üÐÂÁбí²Å»áÈ¥»ñÈ¡ÁÐ±í£¬ÆäËûÇé¿ö²»ÐèÒª
1540// get_spot_list();
1541 //Ïȳõʼ»¯ÈȵãÁбíµÄÐźÅΪ0
1542 update_spot_list_signal_connect_status (0, -1);
1543
1544 for (i = 0; i < vector_size (v); i++) {
1545 if (j >= WIFI_STATION_SCAN_RESULT_LIST_NUM) {
1546 break;
1547 }
1548
1549 var = vector_get (v, i);
1550 if (var->type == IS_STRING) {
1551 scan_result = parse_scan_result (var); // ÀïÃæÓÐ ¸üРɨÃèÁбíµÄ ÈȵãÁ¬½ÓÇé¿ö£¬¸ù¾ÝmacµØÖ·½øÐÐÆ¥Åä
1552 if (scan_result != NULL) {
1553 g_scan_result_list_p[j] = scan_result;
1554
1555 //²éÕÒ¸üжÔÓ¦ÈȵãµÄÐźÅÇ¿¶È
1556 update_spot_signal_by_scan_result (g_scan_result_list_p[j]);
1557
1558 j++;
1559 }
1560 }
1561 }
1562
1563 save_spot_list();
1564
1565 sort_scan_result_by_signal (g_scan_result_list_p, j);
1566
1567 for (i = 0; i < j; i++) {
1568 save_scan_result (g_scan_result_list_p[i], scan_result_str, scan_result_str1);
1569 }
1570
1571 scan_result_str_len = strlen (scan_result_str);
1572 scan_result_str_len1 = strlen (scan_result_str1);
1573
1574 wf_log ("scan_result_str_len = %d scan_result_str_len1 = %d j = %d", scan_result_str_len, scan_result_str_len1, j);
1575
1576 //ÏÈÇå¿ÕɨÃèÁбíNV£¬ÔÙд
1577 sc_cfg_set ("EX_APLIST", "");
1578 sc_cfg_set ("EX_APLIST1", "");
1579
1580 if (scan_result_str_len > 0 && scan_result_str_len < WIFI_STATION_SCAN_RESULT_STR_LEN) {
1581 scan_result_str[scan_result_str_len - 1] = '\0'; //È¥µô×îºóÒ»¸ö·ÖºÅ
1582 sc_cfg_set ("EX_APLIST", scan_result_str);
1583 }//klocwork
1584 if (scan_result_str_len1 > 0 && scan_result_str_len1 < WIFI_STATION_SCAN_RESULT_STR_LEN) {
1585 scan_result_str1[scan_result_str_len1 - 1] = '\0'; //È¥µô×îºóÒ»¸ö·ÖºÅ
1586 sc_cfg_set ("EX_APLIST1", scan_result_str1);
1587 }
1588
1589 safe_free (scan_result_str);
1590 safe_free (scan_result_str1);
1591}
1592
1593
1594static void ipfail_disconnect_ap()
1595{
1596 char cmd[WIFI_STATION_CMD_LEN] = {0};
1597 int spot_num = 0;
1598 int i = 0;
1599
1600 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1601 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
1602 spot_num = atoi (wifi_profile_num);
1603
1604 for (i = 0; i < spot_num; i++) {
1605 if (g_spot_list_p[i]->network_id == g_linked_network_id) {
1606 wf_log ("[SSID= %s] ip dhcp failed ,will be disabled, and set priority 0", g_spot_list_p[i]->ssid);
1607 sprintf (cmd, "DISABLE_NETWORK %d", g_linked_network_id);
1608 sta_docmd (cmd);
1609
1610 sprintf (cmd, "SET_NETWORK %d priority 0", g_linked_network_id);
1611 sta_docmd (cmd);
1612 break;
1613 }
1614 }
1615 wifi_station_connect (MODULE_ID_WIFI);
1616// disable cmd will disconnect the ap , and sta_disconnected will deal the process
1617 // i guess wpa_supplicant will connect other enabled spot
1618
1619}
1620
1621
1622static void handle_sim_init_status_timer()
1623{
1624 char sim_zpbic_status[WIFI_ZPBIC_STATUS_LEN] = {0};
1625
1626 if (g_send_count != g_pre_send_count) {
1627 wf_log ("g_send_count is %d -----g_pre_send_count is %d", g_send_count, g_pre_send_count);
1628 return;
1629 }
1630 sc_cfg_get ("sim_zpbic_status", sim_zpbic_status, sizeof (sim_zpbic_status));
1631 if (strcmp (sim_zpbic_status, SIMCARD_INIT_NO_CARD) == 0) {
1632 //ɾ³ýsim init ״̬¶¨Ê±Æ÷
1633 wf_log ("sim_zpbic_status is fail");
1634 // g_timer_sim_init_undefined_status_count = 0;
1635 //sc_timer_delete(WIFI_STATION_TIMER_SIM_CARD_STATUS);
1636 wifi_station_connect (MODULE_ID_WIFI);
1637
1638 return;
1639 } else if (strcmp (sim_zpbic_status, SIMCARD_INIT_UNDEFINED) == 0) {
1640// printf(" handle_sim_init_status_timer in g_timer_sim_init_undefined_status_count is %d", //g_timer_sim_init_undefined_status_count);
1641#if 0
1642 if (g_timer_sim_init_undefined_status_count >= 4) {
1643 //ɾ³ýsim init ״̬¶¨Ê±Æ÷
1644 //printf(" handle_sim_init_status_timer g_timer_sim_init_undefined_status_count is %d", //g_timer_sim_init_undefined_status_count);
1645 //g_timer_sim_init_undefined_status_count = 0;
1646 sc_timer_delete (WIFI_STATION_TIMER_SIM_CARD_STATUS);
1647 wlan_station_msg_handle_ex();
1648 return;
1649 }
1650 //g_timer_sim_init_undefined_status_count++;
1651#endif
1652 sc_timer_create (WIFI_STATION_TIMER_SIM_CARD_STATUS,
1653 TIMER_FLAG_ONCE, WIFI_STATION_TIMER_SIM_CARD_STATUS_INTERVAL,
1654 wifi_station_query_sim_card_status, NULL);
1655 } else if (strcmp (sim_zpbic_status, SIMCARD_INIT_SUCCESS) == 0) {
1656 wf_log (" success");
1657
1658 //wlan_station_msg_handle_connect();
1659 wifi_station_connect (MODULE_ID_WIFI);
1660 } else {
1661 wf_log ("unknow");
1662
1663 wifi_station_connect (MODULE_ID_WIFI);
1664
1665 }
1666}
1667
1668
1669static void wpa_supplicant_scan (void)
1670{
1671 sta_docmd ("SCAN");
1672}
1673
1674static void wpa_supplicant_get_scan_result (vector_t* v)
1675{
1676 char *reply = NULL;
1677
1678 if (v == NULL) {
1679 return;
1680 }
1681
1682 //»ñȡɨÃè½á¹û
1683 wf_log ("wlan-station get_scan_result");
1684// exec_script(WPA_CLI" scan_results", v);
1685 reply = sta_docmd ("SCAN_RESULTS");
1686
1687// wf_log("[scan] scan results:\n %s" , reply);
1688 convert2vector (reply, v);
1689}
1690
1691
1692static void *wifi_station_query_sim_card_status(void* arg)
1693{
1694 //wlan_station_send_message(MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_SIM_INIT_TIMER_PROCESS, 0, NULL);
1695 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_SIM_INIT_TIMER_PROCESS, 0, NULL, 0);
1696 return NULL;
1697}
1698
1699static void wifi_station_scan (void)
1700{
1701 char wifi_sta_connection[WIFI_CONNECTION_LEN] = {0};
1702 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
1703
1704 sc_cfg_get ("wifi_sta_connection", wifi_sta_connection, sizeof (wifi_sta_connection));
1705
1706
1707 wf_log ("wifi_station_scan");
1708 //¹¦ÄܹرÕÔòÖ±½ÓÍ˳ö
1709 if (strcmp (wifi_sta_connection, "1") != 0) {
1710 return;
1711 }
1712
1713 //ÕýÔÚÁ¬½Ó״̬ÔòÍ˳ö
1714 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
1715 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_CONNECTING) == 0 ||
1716 strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_DHCPING) == 0) {
1717 wf_log ("sta_ip_status=connecting,set scan_finish=2========");
1718 sc_cfg_set ("scan_finish", "2"); //Ä¿µÄÊǸæËßwebui£¬´Ëʱµ×²ãÔÚconnecting
1719 return;
1720 }
1721
1722 //wpa_supplicant½ø³ÌûÆô¶¯ÔòÆô¶¯£¬ÊôÓÚÒì³£±£»¤
1723 if (check_supplicant_alive() ==0) {
1724 wf_log ("wpa supplicant is not running!xx");
1725 sc_cfg_set ("scan_finish", "2"); //Ä¿µÄÊǸæËßwebui£¬´Ëʱµ×²ã³öÎÊÌâÁË
1726
1727 return;
1728 }
1729// sc_cfg_set("scan_finish", "0");
1730
1731 //ɨÃè
1732 wpa_supplicant_scan();
1733
1734}
1735
1736static void wifi_station_get_scan_results (void)
1737{
1738 vector_t *v = NULL;
1739 char wifi_sta_connection[WIFI_CONNECTION_LEN] = {0};
1740 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
1741 sc_cfg_get ("wifi_sta_connection", wifi_sta_connection, sizeof (wifi_sta_connection));
1742
1743 wf_log ("wifi_station_get_scan_results");
1744
1745 //¹¦ÄܹرÕÔòÖ±½ÓÍ˳ö
1746 if (strcmp (wifi_sta_connection, "1") != 0) {
1747 wf_log ("wifi_sta_connection isn't 1,abormal!!!========");
1748 return;
1749 }
1750
1751 //ÕýÔÚÁ¬½Ó״̬ÔòÍ˳ö
1752 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
1753 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_CONNECTING) == 0 ||
1754 strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_DHCPING) == 0) {
1755 wf_log ("sta_ip_status=connecting,set scan_finish=2========");
1756 sc_cfg_set ("scan_finish", "2"); //Ä¿µÄÊǸæËßwebui£¬´Ëʱµ×²ãÔÚconnecting
1757 return;
1758 }
1759
1760 //wpa_supplicant½ø³ÌûÆô¶¯ÔòÆô¶¯£¬ÊôÓÚÒì³£±£»¤
1761 if (check_supplicant_alive() ==0) {
1762 wf_log ("detect wpa_supplicant isnot running========");
1763 sc_cfg_set ("scan_finish", "2"); //Ä¿µÄÊǸæËßwebui£¬´Ëʱµ×²ã³öÎÊÌâÁË
1764 return;
1765 }
1766
1767 v = vector_init();
1768 wf_log ("starting scan result");
1769
1770 //»ñȡɨÃè½á¹û
1771 wpa_supplicant_get_scan_result (v);
1772
1773 //´¦ÀíɨÃè½á¹û
1774 handle_scan_result (v);
1775
1776 sc_cfg_set ("scan_finish", "1");
1777
1778 wf_log ("set scan finish 1");
1779
1780 vector_free (v);
1781}
1782
1783static void wifi_station_open_apsta(void)
1784{
1785 wf_log ("restart apsta");
1786 basic_deal_all (WIFI_CFG_RESTART_APSTATION); //ÐèÒª½«µ¥¶ÀAPģʽÇл»Îªapsta ģʽ
1787}
1788
1789static void sta_enable_all_networks (void)
1790{
1791 wf_log (" webui_select_network= %d, g_disable_other_network=%d ",
1792 webui_select_network, g_disable_other_network);
1793
1794#if 0
1795 // µ¥¶À´¦Àíwebui ÔÚÁ´½Ó״̬£¬Ñ¡ÔñÆäËûÈÈµã½øÐÐÁ´½ÓÇé¿ö
1796 if (webui_select_network == 1) {
1797 webui_select_network = 0; // ½ö½öΪÁ˱ÜÃâselectÃüÁîÒýÆðµÄÉϸöÈȵãµÄ¶Ï¿ªÊ¼þ
1798 return;
1799 }
1800#endif
1801
1802 if (g_disable_other_network == 1) {
1803 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_CONNECTING);
1804 sta_docmd ("ENABLE_NETWORK all");
1805 // reconnect , willl connect last one ???
1806 sta_docmd ("RECONNECT");
1807 //has_exec_select_network =0;
1808 g_disable_other_network = 0; // select will disable others networks, so enable all , then reset it
1809 }
1810}
1811
1812static void sta_disconnected (void)
1813{
1814 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
1815
1816 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
1817
1818 if (strcmp (sta_ip_status, "disconnect") != 0) { // ÓпÉÄÜÊǹرÕapsta£¬ÒѾ­×öÁËdeinit£¬ËùÒÔÏÂÃæÁ÷³Ì¾Í²»ÐèÒªÁË
1819 write_status (STASTATUS, "F"); //F ±íʾ ¶Ï¿ª×´Ì¬£¬0~4 ±íʾÐźÅÇ¿¶È 5¸ö¼¶±ð
1820 //¸üÐÂEX_APLISTÖеÄÁ¬½Ó״̬Ϊ0
1821 update_EX_APLIST_connect_status (FALSE);
1822 //¸üÐÂÈȵãÁбíÖжÔÓ¦ÈȵãµÄÁ¬½Ó״̬Ϊfalse
1823 update_spot_connect_status_by_status_result (FALSE);
1824
1825 network_down_up (FALSE);
1826
1827 g_linked_network_id_last = g_linked_network_id;
1828
1829 if (0 ==webui_select_network) {
1830 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_DISCONNECT);
1831 }else if(1 == webui_select_network){
1832 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_CONNECTING);
1833 webui_select_network =0;
1834 }
1835
1836 }
1837}
1838
1839static void sta_disconnect (void)
1840{
1841 int i = 0;
1842 int network_id = -1;
1843 char cmd[WIFI_STATION_CMD_LEN] = {0};
1844 int spot_num = 0;
1845 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1846
1847 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
1848 spot_num = atoi (wifi_profile_num);
1849
1850 dump_spot();
1851 for (i = 0; i < spot_num; i++) {
1852 if (NULL != g_spot_list_p[i] && g_spot_list_p[i]->connect_status == 1) {
1853 network_id = g_spot_list_p[i]->network_id;
1854 break;
1855 }
1856 }
1857
1858 if (network_id != -1) {
1859 sprintf (cmd, "DISCONNECT");
1860 sta_docmd (cmd);
1861 has_exec_disconnect = 1;
1862 // sprintf(cmd, "DISABLE_NETWORK %d", network_id);
1863 // sta_docmd(cmd);
1864 } else {
1865 wf_log ("All SSID AP already disconnect, do not disconnect again");
1866 }
1867}
1868
1869static void sta_connect (int network_id)
1870{
1871 int i = 0;
1872// int network_id = -1;
1873 char cmd[WIFI_STATION_CMD_LEN] = {0};
1874 int spot_num = 0;
1875 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1876 char ssid[WIFI_STATION_SSID_LEN] = {0};
1877 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
1878 char manual_d_wifi[8] = {0};
1879 char blc_wan_mode[32] = {0};
1880
1881 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
1882 spot_num = atoi (wifi_profile_num);
1883
1884 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
1885 sc_cfg_get ("manual_d_wifi", manual_d_wifi, sizeof (manual_d_wifi));
1886 sc_cfg_get ("blc_wan_mode", blc_wan_mode, sizeof (blc_wan_mode));
1887
1888 if ((strcmp (manual_d_wifi, "1") == 0) || (strcmp ("PPPOE", blc_wan_mode) == 0)) {
1889 wf_log ("connect failed manual_d_wifi=%s,blc_wan_mode=%s",
1890 manual_d_wifi, blc_wan_mode);
1891 return;
1892 }
1893
1894 if (network_id == INVALID_NETWORK_ID) {
1895 // set sta_ip_status, for webui
1896 sc_cfg_get ("EX_SSID1", ssid, WIFI_STATION_SSID_LEN);
1897 // sc_cfg_get("EX_mac", mac, WIFI_STATION_MAC_LEN);
1898
1899 for (i = 0; i < spot_num; i++) {
1900 wf_log ("g_spot_list_p[%d] = 0x%p", i , g_spot_list_p[i]);//cov m
1901 if (g_spot_list_p[i] != NULL && !strcmp (g_spot_list_p[i]->ssid, ssid)) {
1902
1903//protect not to connect twice
1904 if(1 == g_spot_list_p[i]->connect_status ){
1905 wf_log ("[%s] is connect ,do not need connect again,[sta_ip_status=%s]", ssid, sta_ip_status);
1906 return;
1907 }
1908 network_id = g_spot_list_p[i]->network_id;
1909//select¶¯×÷»áÒýÆðÁ¬×ŵÄÈȵã¶Ï¿ª£¬¶Ï¿ªÊ±ºòÒª¼Ç¼´Ë¶¯×÷£¬²»ÐèÒªÔÙÈ¥enable all
1910 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_CONNECT) == 0 )
1911 webui_select_network = 1;
1912 break;
1913 }
1914 }
1915 }
1916
1917 if (network_id == -1) {
1918 wf_log ("got the wrong network id");
1919 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_DISCONNECT);
1920 return;
1921 } else {
1922// if(strcmp(sta_ip_status, WIFI_STATION_IP_STATUS_DISCONNECT) != 0)
1923 {
1924 sprintf (cmd, "SELECT_NETWORK %d", network_id);
1925 sta_docmd (cmd);
1926 g_disable_other_network = 1;
1927 }
1928 sprintf (cmd, "ENABLE_NETWORK %d", network_id);
1929 sta_docmd (cmd);
1930
1931 sta_docmd ("SAVE_CONFIG");
1932
1933#if 0
1934 if (has_exec_disconnect == 1) {
1935 sta_docmd ("RECONNECT");
1936 has_exec_disconnect = 0; // reconnect will reset has_exec_disconnect
1937 }
1938#endif
1939 }
1940
1941// Create_connect_timeout_timer(); //´´½¨Á¬½Ó³¬Ê±¶¨Ê±Æ÷
1942}
1943
1944static void do_dhcp (void)
1945{
1946 //Á¬½Ó³É¹¦£¬µ÷ÍøÂç×é½Ó¿Ú½øÐÐDHCPÁ÷³Ì·ÖÅäIPµØÖ·
1947 network_down_up (TRUE);
1948}
1949
1950/*
1951
1952µ±¶Ï¿ªÊ±ºò£¬¸üÐÂÈȵãÁ´½Ó״̬£¬g_linked_network_id »¹ÊǼǼµÄÁ´½ÓµÄÈȵã
1953
1954sta_ip_status ¼Ç¼µÄ״̬»¹ÊÇconnect ״̬
1955*/
1956static int find_linked_ap_index(void)
1957{
1958 int spot_num = 0;
1959 int i = 0;
1960
1961 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
1962
1963 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1964 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
1965 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
1966
1967 spot_num = atoi (wifi_profile_num);
1968
1969 if (-1 == g_linked_network_id) return -1;
1970
1971 for (i = 0; i < spot_num; i++) {
1972 if (g_spot_list_p[i]->network_id == g_linked_network_id) {
1973 wf_log ("find_linked_ap_index = %d", i); // Ò»°ã ¶¼ÊÇ0£¬µ±ÓÐÔËÓªÉÌÈȵãʱºò¿ÉÄܲ»ÊÇ0
1974 return i;
1975 }
1976 }
1977
1978 return -1;
1979
1980}
1981
1982static void disable_network (char * ssid)
1983{
1984 char cmd[WIFI_STATION_CMD_LEN] = {0};
1985 int spot_num = 0;
1986 int i = 0;
1987
1988 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
1989 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
1990
1991 spot_num = atoi (wifi_profile_num);
1992
1993 for (i = 0; i < spot_num; i++) {
1994 if (strcmp (g_spot_list_p[i]->ssid, ssid) == 0) {
1995 if (-1 != g_spot_list_p[i]->network_id) {
1996 sprintf (cmd, "DISABLE_NETWORK %d", g_spot_list_p[i]->network_id);
1997 sta_docmd (cmd);
1998 sta_docmd ("SAVE_CONFIG");
1999 break;
2000 } else {
2001 wf_log ("Error !! ssid =%s, networkid = %d\n ", g_spot_list_p[i]->ssid, g_spot_list_p[i]->network_id);
2002 }
2003 }
2004 }
2005}
2006
2007static void update_current_EX (char * ssid, char * mac)
2008{
2009 char buf[WIFI_STATION_SPOT_KEY_ID_LEN] = {0};
2010 int spot_num = 0;
2011 int i = 0;
2012
2013 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
2014 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
2015
2016 spot_num = atoi (wifi_profile_num);
2017
2018 for (i = 0; i < spot_num; i++) {
2019 if (strcmp (g_spot_list_p[i]->ssid, ssid) == 0) {
2020 sc_cfg_set ("EX_wifi_profile", g_spot_list_p[i]->profile_name);
2021 sc_cfg_set ("EX_SSID1", ssid);
2022 sc_cfg_set ("EX_AuthMode", g_spot_list_p[i]->auth_mode);
2023 sc_cfg_set ("EX_EncrypType", g_spot_list_p[i]->encrypt_type);
2024
2025 sprintf (buf, "%d", g_spot_list_p[i]->keyID);
2026 sc_cfg_set ("EX_DefaultKeyID", buf);
2027
2028 sc_cfg_set ("EX_WPAPSK1", g_spot_list_p[i]->password);
2029 sc_cfg_set ("EX_WEPKEY", g_spot_list_p[i]->password);
2030 sc_cfg_set ("EX_mac", mac);
2031 break;
2032 }
2033 }
2034}
2035
2036static void disable_other_network (int network_id)
2037{
2038
2039 char cmd[WIFI_STATION_CMD_LEN] = {0};
2040 int spot_num = 0;
2041 int i = 0;
2042
2043 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
2044 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
2045
2046 spot_num = atoi (wifi_profile_num);
2047 if (spot_num > WIFI_STATION_SPOT_LIST_NUM || spot_num < 0)
2048 return;
2049
2050 for (i = 0; i < spot_num; i++) {
2051 if (g_spot_list_p[i]->network_id == network_id) {
2052 continue;
2053 }
2054
2055 sprintf (cmd, "DISABLE_NETWORK %d", g_spot_list_p[i]->network_id);
2056 sta_docmd (cmd);
2057 }
2058
2059 g_disable_other_network = 1;
2060
2061}
2062
2063static void sta_connected()
2064{
2065 char cmd[WIFI_STATION_CMD_LEN] = {0};
2066
2067 write_status (STASTATUS, "4"); //F ±íʾ ¶Ï¿ª×´Ì¬£¬0~4 ±íʾÐźÅÇ¿¶È 5¸ö¼¶±ð ÔÝʱÐźÅдÂú¸ñ
2068
2069 //¸üÐÂÈȵãÁбíÖжÔÓ¦ÈȵãµÄÁ¬½Ó״̬Ϊtrue
2070 update_spot_connect_status_by_status_result (TRUE);
2071
2072 //¸üÐÂËùÓÐÈȵãÁбíÖеÄis_tried_connectΪfalse
2073 //update_spot_list_is_tried_connect(FALSE);
2074
2075 resort_spot_list();
2076
2077 //¸üÐÂEX_APLISTÖеÄÁ¬½Ó״̬Ϊ1
2078 update_EX_APLIST_connect_status (TRUE);
2079
2080 // set the success AP to EX_SSID1
2081 // update_current_EX();
2082
2083 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_CONNECT);
2084// Delete_connect_timeout_timer();
2085
2086 wf_log ("g_linked_network_id_last = %d, g_linked_network_id= %d", g_linked_network_id_last, g_linked_network_id);
2087
2088// ²»ÊÇÎÞЧid£¬ÇÒÉÏÒ»¸öÁ´½ÓµÄºÍÕâ´ÎÁ´½ÓµÄ²»ÊÇͬһ¸öÈȵã
2089 if (g_linked_network_id_last != -1 && g_linked_network_id_last != g_linked_network_id) { // thr first time, there's no the last id
2090 sprintf (cmd, "SET_NETWORK %d priority 2", g_linked_network_id_last);
2091 sta_docmd (cmd);
2092 }
2093 sprintf (cmd, "SET_NETWORK %d priority 3", g_linked_network_id);
2094 sta_docmd (cmd);
2095
2096 wf_log ("g_disable_other_network= %d", g_disable_other_network);
2097
2098//Èç¹û²»ÊÇselect ÃüÁîÖ®ºóÍê³ÉµÄÁ´½Ó£¬¾Í²»»ádisableÆäËûÈȵ㣬Ӧ¸ÃÊÇwpa_supplicant ÔÚenable allºó×ÔÐÐÑ¡ÔñÈÈµã½øÐÐÁ´½Ó
2099 if (g_disable_other_network == 0) {
2100 disable_other_network (g_linked_network_id);
2101 // g_disable_other_network =1;
2102 }
2103
2104 sta_docmd ("SAVE_CONFIG");
2105
2106}
2107
2108static void update_eapsim_spot(void)
2109{
2110 int i = 0;
2111 int spot_num = 0;
2112 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
2113 char cmd[WIFI_STATION_CMD_LEN] = {0};
2114
2115 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
2116 spot_num = atoi (wifi_profile_num);
2117 if (spot_num > WIFI_STATION_SPOT_LIST_NUM || spot_num < 0)
2118 return;
2119
2120 for (i = 0; i < spot_num; i++) {
2121 if (strstr (g_spot_list_p[i]->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA_EAP)) { //|| strstr(auth_mode, WIFI_STATION_PROFILE_AUTH_WPA2_EAP))
2122 if (g_cardmode == CARD_MODE_USIM) {
2123 sprintf (cmd, "SET_NETWORK %d eap AKA", g_spot_list_p[i]->network_id);
2124 sta_docmd (cmd);
2125 } else {
2126 sprintf (cmd, " SET_NETWORK %d eap SIM", g_spot_list_p[i]->network_id);
2127 sta_docmd (cmd);
2128 }
2129 }
2130 }
2131}
2132
2133/*
2134 * just enable all networks let supplicant to connect automaticly
2135 */
2136static int wifi_station_connect (int from_where)
2137{
2138 char wifi_profile_num[8] = {0};
2139 char manual_d_wifi[8] = {0};
2140 char wifi_cur_state[8] = {0};
2141 char blc_wan_mode[16] = {0};
2142
2143 wf_log ("enter from %d blc_close_apsta= %d", from_where, g_blc_send_close_apsta);
2144
2145 if (g_blc_send_close_apsta == 1) {
2146 wf_log ("blc send close wifi,so only blc can open wifi again.");
2147 return -1;
2148 }
2149
2150 //Èç¹ûûÓпÉÒÔÓõÄÈȵ㣬ֱ½Ó·µ»ØÊ§°Ü
2151 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
2152 sc_cfg_get ("manual_d_wifi", manual_d_wifi, sizeof (manual_d_wifi));
2153 sc_cfg_get ("blc_wan_mode", blc_wan_mode, sizeof (blc_wan_mode));
2154 sc_cfg_get ("wifi_cur_state", wifi_cur_state, sizeof (wifi_cur_state));
2155
2156 //ÊÖ¶¯¶Ï¿ªwifi£¬¼´Ê¹Ö÷¿ØÔÙÈÃÁ¬½Ó£¬Ò²²»»áÔÙÁ¬½ÓÁË
2157 if ( (strcmp (wifi_cur_state, WIFI_CLOSED) == 0) || (strcmp (wifi_profile_num, "0") == 0)
2158 || (strcmp (manual_d_wifi, "1") == 0) || (strcmp ("PPPOE", blc_wan_mode) == 0)) {
2159 wf_log ("connect failed wifi_cur_state=%s, wifi_profile_num=%s,manual_d_wifi=%s,blc_wan_mode=%s",
2160 wifi_cur_state, wifi_profile_num, manual_d_wifi, blc_wan_mode);
2161 return -1;
2162 }
2163
2164 sta_enable_all_networks();
2165// Create_connect_timeout_timer(); //´´½¨Á¬½Ó³¬Ê±¶¨Ê±Æ÷
2166 return 0;
2167}
2168
2169static void wifi_station_nv_init (void)
2170{
2171 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_DISCONNECT);
2172 sc_cfg_set ("scan_finish", "0");
2173 //sc_cfg_set("EX_APLIST", "");
2174 //sc_cfg_set("EX_APLIST1", "");
2175 sc_cfg_set ("manual_d_wifi", "0");
2176
2177 memset (g_linked_ap_mac, 0, WIFI_STATION_MAC_LEN);
2178 g_linked_network_id = -1;
2179 g_linked_network_id_last = -1;
2180
2181 has_exec_disconnect = 0;
2182 webui_select_network = 0;
2183 g_disable_other_network = 1;
2184
2185 get_spot_list();
2186 sync_nv_spot_list_conf_file();
2187 update_spot_list_signal_connect_status (0, 0);
2188 save_spot_list();
2189}
2190
2191static int exist_eap_simaka_spot()
2192{
2193 int i = 0;
2194 int spot_num = 0;
2195 char wifi_profile_num[WIFI_STATION_PROFILE_NV_NAME_LEN] = {0};
2196
2197 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
2198 spot_num = atoi (wifi_profile_num);
2199
2200 for (i = 0; i < spot_num; i++) {
2201 if (strstr (g_spot_list_p[i]->auth_mode, WIFI_STATION_PROFILE_AUTH_WPA_EAP)) //|| strstr(auth_mode, WIFI_STATION_PROFILE_AUTH_WPA2_EAP))
2202 return g_spot_list_p[i]->network_id;
2203 }
2204 return -1;
2205}
2206
2207
2208void wlan_station_init (void)
2209{
2210 if(1 == g_wpa_supplicant)return ;
2211
2212 wf_log ("time=%d", time(NULL));
2213 if (sta_manager->start_supplicant(sta_manager) == 0) {
2214 wf_log ("time=%d", time(NULL));
2215 wf_log (" wpa_supplicant run success");
2216
2217 if (0 == wifi_connect_to_supplicant(&sta_manager->sock)) {
2218 wf_log (" socket connect success");
2219 sem_post (&g_wpa_supplicant_id);
2220 g_wpa_supplicant = 1;
2221 } else {
2222 wlan_station_deinit();
2223 return;
2224 }
2225 }else {
2226 wf_log ("wpa_supplicant start failed");
2227 wlan_station_deinit();
2228 return;
2229 }
2230
2231
2232 wifi_station_nv_init();
2233 sta_docmd ("SCAN_INTERVAL 15");
2234
2235 //Èç¹ûÓÐEAP-SIMÈȵ㣬ÔòÐèÒªÏÈ×߸òéѯSIM¿¨ÐÅÏ¢Á÷³Ì
2236
2237 if (-1 != exist_eap_simaka_spot()) { // ²»ÊÇ-1£¬ Ôò´æÔÚsimaka spot
2238 //eap send msg for card type
2239 /*if((0 == strcmp(ssid, "CMCC-SIM-test1")) || (0 == strcmp(ssid, "CMCC-SIM-test2")))
2240 {
2241 strcpy(auth_mode, WIFI_STATION_PROFILE_AUTH_WPA_EAP);
2242 }*/
2243
2244 //if(0 == wlan_station_send_message(ZUFI_MODULE_ID_AT_MAIN, MODULE_ID_CARD_MODE_REQ, 0, NULL))
2245 if (0 == ipc_send_message (MODULE_ID_WIFI, MODULE_ID_AT_CTL, MSG_CMD_CARD_MODE_REQ, 0, NULL, 0)) {
2246 //·¢ËÍ ²éѯ¿¨Ä£Ê½ÏûÏ¢£¬µÈ´ýÒì²½·µ»Ø
2247 g_pre_send_count = g_send_count;
2248 return;
2249 } else {
2250 wifi_station_connect (MODULE_ID_WIFI);
2251 }
2252 } else {
2253 wifi_station_connect (MODULE_ID_WIFI);
2254 }
2255}
2256
2257static void clear_conf_file(void)
2258{
2259 sta_docmd ("REMOVE_NETWORK all");
2260 sta_docmd ("SAVE_CONFIG");
2261}
2262
2263static void clear_spot_list(void)
2264{
2265 int i = 0;
2266 wf_log ("[clear_spot_list]");
2267
2268 for (i = 0; i < WIFI_STATION_SPOT_LIST_NUM; i++) {
2269 if (g_spot_list_p[i] != NULL) {
2270 safe_free (g_spot_list_p[i]);
2271 }
2272
2273 }
2274
2275}
2276static void wlan_station_deinit(void)
2277{
2278 int count = 50;
2279
2280 wf_log ("enter");
2281
2282 sta_docmd ("DISABLE_NETWORK all");
2283 sta_docmd ("SAVE_CONFIG");
2284#if !defined(__REALTEK_8192_CHIP__)
2285 sta_docmd ("TERMINATE");
2286 usleep (150000);
2287
2288 while (count-- > 0) {
2289 if (check_alive(&sta_manager->sock) == 0) {
2290 wf_log ("TERMINATE over\n");
2291 break;
2292 }
2293 usleep (100000);
2294 }
2295#endif
2296
2297 g_disable_other_network = 1;
2298 sta_manager->close_connection(sta_manager);
2299
2300 g_wpa_supplicant = 0;
2301
2302 clear_spot_list();
2303 memset (g_linked_ap_mac, 0, WIFI_STATION_MAC_LEN);
2304 g_linked_network_id = -1;
2305 g_linked_network_id_last = -1;
2306 has_exec_disconnect = 0;
2307 webui_select_network = 0;
2308 sc_cfg_set ("scan_finish", "0");
2309 sc_cfg_set ("EX_APLIST", "");
2310 sc_cfg_set ("EX_APLIST1", "");
2311
2312 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_DISCONNECT);
2313
2314}
2315
2316
2317void wifi_station_close()
2318{
2319 sta_disconnected();
2320 wlan_station_deinit();
2321}
2322
2323
2324BOOL wlan_station_is_station_msg (unsigned short msg)
2325{
2326 if (msg >= MSG_CMD_WIFI_STATION_OPEN && msg <= MSG_CMD_WIFI_STATION_END) {
2327 return TRUE;
2328 } else if(msg == MSG_CMD_NET_WAN_DIAL_FAIL ||
2329 msg == MSG_CMD_CARD_MODE_RSP ||
2330 msg == MSG_CMD_PIN_STATUS_RSP){
2331 return TRUE;
2332 }
2333 else{
2334 return FALSE;
2335 }
2336}
2337
2338static void wlan_station_msg_handle_card_mode_for_eap (MSG_BUF *pMsg)
2339{
2340#if 0
2341 spot_t *p = NULL;
2342 char profile_name[WIFI_STATION_SPOT_PROFILE_NAME_LEN] = {0};
2343 char *ssid = NULL;
2344 char auth_mode[WIFI_STATION_AUTH_MODE_LEN] = {0};
2345 char encrypt_type[WIFI_STATION_ENCRYPT_TYPE_LEN] = {0};
2346 char *password_wpa = NULL;
2347 char *password_wep = NULL;
2348#endif
2349 char cardmodestr[WIFI_CARD_MODE_LEN+1] = {0};
2350 int cardmode = 0;
2351
2352 if (g_send_count != g_pre_send_count) {
2353 wf_log ("------------wlan_station_msg_handle_card_mode-----g_send_count is %d -----g_pre_send_count is %d", g_send_count, g_pre_send_count);
2354 return;
2355 }
2356 wf_log ("------------wlan_station_msg_handle_card_mode------------++++++++++++++++++++++++++++++");
2357 if (pMsg != NULL) {
2358 //kw 3
2359 memcpy (cardmodestr, pMsg->aucDataBuf, WIFI_CARD_MODE_LEN);
2360
2361 wf_log ("------------wlan_station_msg_handle_card_mode--------333----++++++++++++++++++++++++++++++");
2362 cardmode = atoi (cardmodestr);
2363 wf_log ("------------wlan_station_msg_handle_card_mode--------cardmode is %d----++++++++++++++++++++++++++++++", cardmode);
2364 if (cardmode < 0 || cardmode > INT_MAX-1) //kw 3, cardmode 0-2
2365 cardmode = 0;
2366 }
2367
2368 g_cardmode = cardmode;
2369
2370 if (cardmode != CARD_MODE_USIM && cardmode != CARD_MODE_SIM) {
2371 wifi_station_connect (MODULE_ID_WIFI);
2372 } else {
2373 //eap send msg for pin status
2374 if (0 == ipc_send_message (MODULE_ID_WIFI, MODULE_ID_AT_CTL, MSG_CMD_PIN_STATUS_REQ, 0, NULL, 0)) {
2375 wf_log ("------------ipc_send_message-------MSG_CMD_PIN_STATUSREQ-----++++++++++++++++++++++++++++++");
2376 g_pre_send_count = g_send_count;
2377 return;
2378 } else {
2379 wf_log ("------------ipc_send_message-------MODULE_ID_PIN_STATUS_REQ--ex---++++++++++++++++++++++++++++++");
2380 wifi_station_connect (MODULE_ID_WIFI);
2381
2382 }
2383
2384 //wlan_station_msg_handle_connect();
2385 }
2386}
2387
2388static void wlan_station_msg_handle_pin_status_for_eap (MSG_BUF *pMsg)
2389{
2390 char pinstatusstr[WIFI_PIN_STATUS_LEN+1] = {0};
2391 int pinstatus = 0;
2392 char sim_zpbic_status[WIFI_ZPBIC_STATUS_LEN] = {0};
2393
2394 if (g_send_count != g_pre_send_count) {
2395 wf_log ("------------wlan_station_msg_handle_pin_status_for_eap-----g_send_count is %d -----g_pre_send_count is %d", g_send_count, g_pre_send_count);
2396 return;
2397 }
2398 wf_log ("------------wlan_station_msg_handle_pin_status_for_eap------------++++++++++++++++++++++++++++++");
2399 if (pMsg != NULL) {
2400 //kw 3
2401 memcpy (pinstatusstr, pMsg->aucDataBuf, WIFI_PIN_STATUS_LEN);
2402
2403 pinstatus = atoi (pinstatusstr);
2404
2405 if (PIN_STATUS_NO_PIN != pinstatus) {
2406 wifi_station_connect (MODULE_ID_WIFI);
2407
2408 } else {
2409 //Ôö¼Ó¶Ô¿¨³õʼ»¯ÊÇ·ñÍê³ÉµÄÅжÏ
2410 sc_cfg_get ("sim_zpbic_status", sim_zpbic_status, sizeof (sim_zpbic_status));
2411 if (0 == strcmp (sim_zpbic_status, SIMCARD_INIT_SUCCESS)) {
2412 wf_log ("--------------------sim_zpbic_status-----SIMCARD_INIT_SUCCESS ++++++++++++++-------------");
2413 update_eapsim_spot();
2414 // wlan_station_msg_handle_connect();
2415 wifi_station_connect (MODULE_ID_WIFI);
2416 } else if (0 == strcmp (sim_zpbic_status, SIMCARD_INIT_UNDEFINED)) {
2417 wf_log ("--------------------sim_zpbic_status-----SIMCARD_INIT_UNDEFINED ++++++++++++++-------------");
2418 //g_timer_sim_init_undefined_status_count = 0;
2419 g_pre_send_count = g_send_count;
2420 //δÍê³É¿¨ÎļþµÄ³õʼ»¯ÎÞ·¨½øÐÐeapÈÏÖ¤£¬ÐèÒª´´½¨¶¨Ê±Æ÷ÂÖѯsim¿¨³õʼ»¯×´Ì¬
2421 sc_timer_create (WIFI_STATION_TIMER_SIM_CARD_STATUS,
2422 TIMER_FLAG_ONCE, WIFI_STATION_TIMER_SIM_CARD_STATUS_INTERVAL,
2423 wifi_station_query_sim_card_status, NULL);
2424 } else {
2425 wf_log ("--------------------sim_zpbic_status-----wlan_station_msg_handle_ex ++++++++++++++-------------");
2426 wifi_station_connect (MODULE_ID_WIFI);
2427 }
2428 }
2429 }
2430}
2431
2432//wifi station´¦Àíº¯Êý
2433int wlan_sta_parse_msg (MSG_BUF *pMsg)
2434{
2435
2436 int ret = 0;
2437 switch (pMsg->usMsgCmd) {
2438 case MSG_CMD_WIFI_STATION_OPEN: {
2439#if defined(__REALTEK_8192_CHIP__) || defined(__AIC_8800DW_CHIP__)
2440 wifi_station_open_apsta();
2441 //wifi_station_connect(MODULE_ID_WEB_CGI);//´ò¿ªµÄʱºòÒѾ­Á¬ÁËÒ»´Î£¬Ã»ÓбØÒªÔÙ´ÎÁ¬½Ó
2442#else
2443 wf_log("-MSG_CMD_WIFI_STATION_OPEN-");
2444 wlan_station_init();
2445#endif
2446 break;
2447 }
2448 case MSG_CMD_WIFI_STATION_CLOSE: {
2449 sta_disconnected();
2450 wlan_station_deinit();
2451#if defined(__REALTEK_8192_CHIP__)|| defined(__AIC_8800DW_CHIP__)
2452 basic_deal_all (WIFI_CFG_RESTART_AP);
2453#endif
2454 break;
2455 }
2456 case MSG_CMD_WIFI_STATION_CLOSE_OPEN_MSSID: {
2457 sta_disconnected();
2458 wlan_station_deinit();
2459 basic_deal_all (WIFI_CFG_CLOSESTA_OPENMSSID);
2460 break;
2461 }
2462 case MSG_CMD_WIFI_STATION_SPOT_UPDATE: {
2463 webui_update_spot_list ( (updateinfo_t*) (pMsg->aucDataBuf));
2464
2465 //ÿ´ÎÓû§¸üÐÂÈȵãºó£¬²»ÐèÒª¸üб£´æsignalµÄÈ«¾Ö±äÁ¿£¬¶øÊÇÔÚɨÃèµÄʱºò£¬À´¼ì²âÊÇ·ñÓб仯
2466 //ËùÒÔÔÚÈȵã±ä»¯ºó£¬ÐèҪͨ¹ýscanÀ´¾¡¿ì¸üÐÂÈȵã״̬
2467 break;
2468 }
2469 case MSG_CMD_WIFI_STATION_SCAN: {
2470 wifi_station_scan();
2471 break;
2472 }
2473 case MSG_CMD_WIFI_STATION_CONNECT: {
2474 sta_connect (INVALID_NETWORK_ID);
2475 break;
2476 }
2477 case MSG_CMD_WIFI_STATION_DISCONNECT: {
2478 sta_disconnect();
2479 break;
2480 }
2481 case MSG_CMD_WIFI_STATION_GET_SCAN_RESULTS: {
2482 wifi_station_get_scan_results();
2483 break;
2484 }
2485 case MSG_CMD_WIFI_STATION_SIM_INIT_TIMER_PROCESS: {
2486 handle_sim_init_status_timer();
2487 break;
2488 }
2489 case MSG_CMD_WIFI_STATION_EVENT_CONNECTED: {
2490 wf_log ("[wifi event connected , goto dhcp]");
2491 sc_cfg_set ("sta_ip_status", "dhcping");
2492 do_dhcp();
2493 break;
2494 }
2495 case MSG_CMD_WIFI_STATION_EVENT_DISCONNECTED: {
2496 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2497 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2498
2499 if (strcmp (sta_ip_status, "connect") == 0 || strcmp (sta_ip_status, "dhcping") == 0) {
2500 sta_disconnected();
2501 }
2502 else{
2503 wf_log ("The Spot connect failed, just set sta_ip_status disconnect");
2504 sc_cfg_set ("sta_ip_status", "disconnect");
2505 }
2506 break;
2507 }
2508 case MSG_CMD_WIFI_STATION_EVENT_CONNECTING: {
2509 sc_cfg_set("sta_ip_status", WIFI_STATION_IP_STATUS_CONNECTING);
2510 break;
2511 }
2512
2513 case MSG_CMD_BLC_OPEN_WIFISTATION_ACCESS: { // you can connect
2514 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2515
2516 g_blc_send_close_apsta = 0;
2517 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2518
2519 wf_log ("sta_ip_status=%s", sta_ip_status);
2520
2521 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_DISCONNECT) == 0) {
2522 wifi_station_connect (MODULE_ID_MAIN_CTRL);
2523 } else {
2524 wf_log ("do not deal connect cmd, sta_ip_status=%s", sta_ip_status);
2525 }
2526 break;
2527 }
2528 case MSG_CMD_BLC_CLOSE_WIFISTATION_ACCESS: {
2529 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2530
2531 g_blc_send_close_apsta = 1;
2532 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2533 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_DISCONNECT) != 0) {
2534 sta_disconnect();// if wifi not connect ok , then if there is disconnect event??
2535 } else {
2536 wf_log ("do not deal disconnect cmd, sta_ip_status=%s", sta_ip_status);
2537
2538 }
2539 break;
2540 }
2541
2542 case MSG_CMD_WIFI_STATION_BLC_CONNECTED: {
2543 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2544
2545 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2546 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_DHCPING) == 0) {
2547 wf_log ("[wlan-station] wlan0-vxd get ip success");
2548 sta_connected();
2549 }
2550 else{
2551 wf_log (" DO not deal , sta_ip_status=%s", sta_ip_status);
2552 }
2553 break;
2554 }
2555
2556 case MSG_CMD_NET_WAN_DIAL_FAIL: {
2557 wf_log ("wlan0-vxd dhcp ip failed");
2558 ipfail_disconnect_ap();
2559 break;
2560 }
2561 case MSG_CMD_CARD_MODE_RSP: {
2562 wlan_station_msg_handle_card_mode_for_eap (pMsg);
2563 break;
2564 }
2565 case MSG_CMD_PIN_STATUS_RSP: {
2566 wlan_station_msg_handle_pin_status_for_eap (pMsg);
2567 break;
2568 }
2569 case MSG_CMD_WIFI_STATION_CONNECT_TIMEOUT: {
2570 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2571 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2572 wf_log ("handle connect timeout msg,sta_ip_status=%s", sta_ip_status);
2573 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_CONNECTING) == 0) {
2574 sta_disconnect();
2575 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_DISCONNECT);
2576 g_connecting_time_out_timer = 0;
2577 }
2578 break;
2579 }
2580
2581 case MSG_CMD_WIFI_STATION_NO_AVAILABLE_AP:{
2582 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2583 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2584 wf_log ("handle no available ap, sta_ip_status=%s", sta_ip_status);
2585 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_CONNECTING) == 0) {
2586 sc_cfg_set ("sta_ip_status", "disconnect");
2587 if(1 == g_disable_other_network){
2588 sta_docmd ("ENABLE_NETWORK all");// the connecting status will be set by assocating event, if no assoc , just be disconnect
2589 g_disable_other_network = 0;
2590 }
2591 }
2592 break;
2593 }
2594 case MSG_CMD_WIFI_STATION_TEMP_DISABLED: {
2595 if (g_wpa_supplicant == 1) {
2596 //wf_log ("MSG_CMD_WIFI_STATION_TEMP_DISABLED=%s", (char *)(pMsg->aucDataBuf));
2597
2598 sc_cfg_set ("sta_ip_status", WIFI_STATION_IP_STATUS_DISCONNECT);
2599 process_tmp_disabled((char *)(pMsg->aucDataBuf));
2600 }
2601 break;
2602 }
2603 case MSG_CMD_WIFI_STATION_FAILED_SET: {
2604 char sta_ip_status[WIFI_STATION_IP_STATUS_LEN] = {0};
2605 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2606 wf_log ("handle Failed to set, sta_ip_status=%s", sta_ip_status);
2607 if (strcmp (sta_ip_status, WIFI_STATION_IP_STATUS_CONNECTING) == 0) {
2608 sc_cfg_set ("sta_ip_status", "disconnect");
2609 //if(1 == g_disable_other_network){
2610 // sta_docmd ("ENABLE_NETWORK all");// the connecting status will be set by assocating event, if no assoc , just be disconnect
2611 // g_disable_other_network = 0;
2612 //}
2613 }
2614 break;
2615 }
2616
2617 default: {
2618 ret = -1;
2619 break;
2620 }
2621 }
2622
2623 return ret;
2624}
2625
2626
2627int get_int_from_event (char * buf, char * tag)
2628{
2629 int ret = -1;
2630 char * pstr = NULL;
2631
2632 if(NULL == buf || NULL == tag)
2633 return -1;
2634
2635 pstr = strstr (buf, tag);
2636 if (pstr != NULL) {
2637 pstr += strlen (tag);
2638 ret = atoi (pstr);
2639 }
2640
2641 return ret;
2642}
2643
2644/*
2645ÃÀÀö¶³È˵ÄÁù³¯¹Å¶¼ÄÏhh, ÖÐÎÄssid»á±È½Ï³¤
2646\xe7\xbe\x8e\xe4\xb8\xbd\xe5\x86\xbb\xe4\xba\xba\xe7\x9a\x84\xe5\x85\xad\xe6\x9c\x9d\xe5\x8f\xa4\xe9\x83\xbd\xe5\x8d\x97hh
2647*/
2648void process_trying_to_assoc (char * buf)
2649{
2650 char * start = NULL;
2651 char * end = NULL;
2652 char ssid[WIFI_STATION_SSID_LEN] = {0};
2653 char mac[WIFI_STATION_MAC_LEN] = {0};
2654 unsigned int len = 0;//klocwork
2655
2656 start = strstr (buf, "SSID=\'") + strlen("SSID=\'");
2657 if (start != NULL) {
2658 end = strstr (start, "\' ");//klocwork
2659 }
2660
2661 strncpy (mac, buf + strlen ("Trying to associate with "), 17);
2662 wf_log ("Trying to associate with mac=%s", mac);
2663 if (end != NULL && start != NULL) {
2664 len = end - start;
2665 len = MIN(len, WIFI_STATION_SSID_LEN-1);
2666 //strncpy (ssid, start, len);
2667 //ssid[WIFI_STATION_SSID_LEN-1] = '\0';//klocwork
2668 snprintf(ssid,len+1,"%s",start);
2669 wf_log ("Trying to associate with ssid= %s", ssid);
2670 }
2671
2672 update_current_EX (ssid, mac);
2673
2674}
2675
2676//Trying to associate with ca:c2:f1:74:2f:f4 (SSID='D_mi_D' freq=2412 MHz)
2677//SME: Trying to authenticate with 90:1d:27:78:2c:1e (SSID='Ufi_782111' freq=2412 MHz)
2678static void channel_follow(char * buf)
2679{
2680 char * start = NULL;
2681 char * end = NULL;
2682 char freq[WIFI_STATION_FREQ_LEN] = {0};
2683 unsigned int len = 0;//klocwork
2684 int frequency = 0;
2685
2686 start = strstr(buf, "freq=") + strlen("freq=");
2687 if (start != NULL) {
2688 end = strstr (start, " MHz");//klocwork
2689 }
2690
2691 if (end != NULL && start != NULL) {
2692 len = end - start;
2693 len = MIN(len, WIFI_STATION_FREQ_LEN-1);
2694 snprintf(freq, len+1, "%s", start);
2695 frequency = atoi(freq);
2696 wf_log("Trying to associate with freq= %s[%d]", freq, frequency);
2697
2698 switch (frequency) {
2699 case WIFI_STATION_CHANNEL1:
2700 sc_cfg_set("flower_channel", "1");
2701 break;
2702 case WIFI_STATION_CHANNEL2:
2703 sc_cfg_set("flower_channel", "2");
2704 break;
2705 case WIFI_STATION_CHANNEL3:
2706 sc_cfg_set("flower_channel", "3");
2707 break;
2708 case WIFI_STATION_CHANNEL4:
2709 sc_cfg_set("flower_channel", "4");
2710 break;
2711 case WIFI_STATION_CHANNEL5:
2712 sc_cfg_set("flower_channel", "5");
2713 break;
2714 case WIFI_STATION_CHANNEL6:
2715 sc_cfg_set("flower_channel", "6");
2716 break;
2717 case WIFI_STATION_CHANNEL7:
2718 sc_cfg_set("flower_channel", "7");
2719 break;
2720 case WIFI_STATION_CHANNEL8:
2721 sc_cfg_set("flower_channel", "8");
2722 break;
2723 case WIFI_STATION_CHANNEL9:
2724 sc_cfg_set("flower_channel", "9");
2725 break;
2726 case WIFI_STATION_CHANNEL10:
2727 sc_cfg_set("flower_channel", "10");
2728 break;
2729 case WIFI_STATION_CHANNEL11:
2730 sc_cfg_set("flower_channel", "11");
2731 break;
2732 case WIFI_STATION_CHANNEL12:
2733 sc_cfg_set("flower_channel", "12");
2734 break;
2735 case WIFI_STATION_CHANNEL13:
2736 sc_cfg_set("flower_channel", "13");
2737 break;
2738 default:
2739 sc_cfg_set("flower_channel", "0");
2740 wf_log("channel_follow with unknow freq = [%d]", frequency);
2741 break;
2742 }
2743 }
2744
2745 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_CHANNEL_FOLLOW, 0, NULL, 0);
2746
2747}
2748
2749/*
2750STA: CTRL-EVENT-SSID-TEMP-DISABLED id=7 ssid="ydh" auth_failures=5 duration=60 reason=CONN_FAILED
2751
2752*/
2753
2754static void process_tmp_disabled (char *buf)
2755{
2756 char *p_WRONG_KEY = NULL;
2757// char *p_AUTH_FAILED = NULL;
2758// char *p_CONN_FAILED = NULL;
2759 char wifi_profile_num[8] = {0};
2760 char cmd[WIFI_STATION_CMD_LEN] = {0};
2761 int network_id = -1;
2762 int auth_failures = 0;
2763
2764 sc_cfg_get ("wifi_profile_num", wifi_profile_num, sizeof (wifi_profile_num));
2765 p_WRONG_KEY = strstr (buf, "WRONG_KEY");
2766// p_AUTH_FAILED = strstr(buf, "AUTH_FAILED");
2767// p_CONN_FAILED = strstr(buf, "CONN_FAILED");
2768
2769 /*
2770 ¿ÉÒÔÓиüÓÅ»¯µÄËã·¨£¬Ìá¸ßÁ´½ÓЧÂÊ
2771 */
2772 auth_failures = get_int_from_event (buf, "auth_failures=");
2773
2774 if (atoi (wifi_profile_num) > 1 && (p_WRONG_KEY || auth_failures > 1)) {
2775 network_id = get_int_from_event (buf, "id=");
2776
2777 wf_log ("network_id = %d wrong key or tmp_disabled_times = %d", network_id, auth_failures);
2778
2779 sprintf (cmd, "SET_NETWORK %d priority 0", network_id);
2780 sta_docmd (cmd);
2781 sta_docmd ("SAVE_CONFIG");
2782
2783 wifi_station_connect (MODULE_ID_WIFI);
2784
2785 if (p_WRONG_KEY) {
2786 wf_log ("network_id = %d wrong key to disable it", network_id);
2787 sprintf (cmd , "DISABLE_NETWORK %d", network_id);
2788 sta_docmd (cmd);
2789 sta_docmd ("SAVE_CONFIG");
2790 }
2791 }
2792}
2793
2794
2795void *station_loop (void *param)
2796{
2797 char buf[UEVENT_BUFFER_SIZE];
2798 char name[32] = {0};
2799 static int scan_times = 0;
2800 int loop_run = 0;
2801
2802 strncpy (name, (char*) param, sizeof(name)-1);
2803 prctl (PR_SET_NAME, name, 0, 0, 0);
2804
2805 while (1) { //¼àÌýcharging״̬µÄ²å°Î
2806 //if wpa_supplicant is stop ,then wait
2807 //if recv the sem, g_wpa_supplicant ==1
2808 // wf_log("station_loop: g_wpa_supplicant= %d", g_wpa_supplicant);
2809 if (g_wpa_supplicant == 0 || 0 == loop_run) {
2810 sem_wait (&g_wpa_supplicant_id);
2811 }
2812 loop_run =1;
2813 // ʼþ²Î¿¼wpa_supplantÖÐwpa_ctrl.hÖж¨Òå
2814 int nread = wifi_wait_for_event (&sta_manager->sock, buf, sizeof (buf));
2815
2816 //wake_sta_lock();
2817 if (nread > 0) {
2818 wf_log ("STA: %s", buf);
2819
2820 if (strstr (buf, "CTRL-EVENT-SCAN-RESULTS")) {
2821 char sta_ip_status[16] = {0};
2822 sc_cfg_get ("sta_ip_status", sta_ip_status, sizeof (sta_ip_status));
2823
2824 if (strcmp (sta_ip_status, "connecting") != 0) {//disconnect connect dhcping
2825 wf_log ("snd msg to get scan results");
2826 scan_times = 0; // IF status change to not connecting, then set 0
2827 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_GET_SCAN_RESULTS, 0, NULL, 0);
2828 } else {
2829 // 17S ccan interval, 17*12 =204S about, to disconnect, just set the nv value, the wpa_supplicant still scan to connect
2830 // the final status ,is enable all network, and scan to connect ,but sta_ip_status is disconnect
2831 scan_times ++;
2832 if (4 == scan_times) {//12-->4 , decrease the waiting time
2833 wf_log ("can not find available spot, just set sta_ip_status disconnect", buf);
2834 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_NO_AVAILABLE_AP, 0, NULL, 0);
2835 scan_times = 0;
2836 }
2837 }
2838 }
2839 //STA: CTRL-EVENT-CONNECTED - Connection to a6:44:d1:86:c5:d9 completed [id=0 id_str=]
2840
2841 else if (strstr (buf, "CTRL-EVENT-CONNECTED")) {
2842 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_CONNECTED_ENABLE_AP_CMD, 0, NULL, 0);
2843
2844 char *p = strstr (buf, "Connection to ");
2845
2846 scan_times = 0; // IF status change to not connecting, then set 0
2847
2848 if (p != NULL) {//
2849 p += strlen ("Connection to ");
2850 strncpy (g_linked_ap_mac, p, 17);
2851 }
2852 g_linked_network_id = get_int_from_event (buf, "[id=");
2853 wf_log ("g_linked_network_id_last = %d, g_linked_network_id= %d", g_linked_network_id_last, g_linked_network_id);
2854 //wf_send_message( MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_EVENT_CONNECTED, 0, NULL);
2855 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_EVENT_CONNECTED, 0, NULL, 0);
2856 } else if (strstr (buf, "CTRL-EVENT-DISCONNECTED")) {
2857 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_EVENT_DISCONNECTED, 0, NULL, 0);
2858 }
2859
2860 //STA: CTRL-EVENT-SSID-REENABLED id=0 ssid="ZONG MBB-MF920W+-000012"
2861 else if (strstr (buf, "CTRL-EVENT-SSID-REENABLED")) {
2862 scan_times = 0; // when reenable , it indicate there is available AP, so set it 0
2863 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_EVENT_CONNECTING, 0, NULL, 0);
2864 }
2865 //STA: Trying to associate with 00:90:49:58:00:73 (SSID='Wo4G-0073' freq=2412 MHz)
2866 else if (strstr (buf, "Trying to associate with")) {
2867#if defined(__AIC_8800DW_CHIP__)
2868 channel_follow(buf);
2869#endif
2870 scan_times = 0; // when begin to assoc , it indicate there is available AP, so set it 0
2871 // CTRL-EVENT-SSID-REENABLED maybe lost
2872 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_EVENT_CONNECTING, 0, NULL, 0);
2873 process_trying_to_assoc (buf);
2874 }
2875 //SME: Trying to authenticate with 90:1d:27:78:2c:1e (SSID='Ufi_782111' freq=2412 MHz)
2876 else if (strstr (buf, "Trying to authenticate with")) {
2877#if defined(__SSV_6X5X_CHIP__)
2878 channel_follow(buf);
2879#endif
2880 }
2881 else if (strstr (buf , "CTRL-EVENT-SIGNAL-CHANGE")) {
2882 wf_log ("snd msg to update signal level");
2883 }
2884 // CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="CPE_E3DE4E" auth_failures=2 duration=20 reason=WRONG_KEY ¡¢AUTH_FAILED¡¢CONN_FAILED
2885 else if (strstr (buf, "CTRL-EVENT-SSID-TEMP-DISABLED")) {
2886 //time sequence sc_cfg_set sta_ip_status, process in another thread with other event
2887 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_TEMP_DISABLED, nread, buf, 0);
2888 }
2889 //WPA: Failed to select authenticated key management type --- 1
2890 //WPA: Failed to set WPA key management and encryption suites --- 2
2891 else if (strstr (buf, "WPA: Failed to set")) {
2892 //sta auth failed
2893 ipc_send_message (MODULE_ID_WIFI, MODULE_ID_WIFI, MSG_CMD_WIFI_STATION_FAILED_SET, 0, NULL, 0);
2894 }
2895 else if (strstr (buf, "CTRL-EVENT-TERMINATING")) {
2896
2897 loop_run = 0;
2898 }
2899 } else {
2900 wf_log ("Could not read pending message.");
2901 }
2902 }
2903
2904 return NULL;
2905}
2906
2907
2908void wlan_station_open()
2909{
2910
2911}
2912
2913void wlan_statemachine_init()
2914{
2915
2916}
2917
2918