[Feature][task-view-306]merge P56U08(patch6) version

Only Configure: No
Affected branch: master
Affected module: unknow
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I8e809511ac30c97228dd110c304b4a08f4af36d7
diff --git a/ap/app/zte_comm/wlan/inc/wifi_ap_ctrl.h b/ap/app/zte_comm/wlan/inc/wifi_ap_ctrl.h
index 078563f..0925ac3 100755
--- a/ap/app/zte_comm/wlan/inc/wifi_ap_ctrl.h
+++ b/ap/app/zte_comm/wlan/inc/wifi_ap_ctrl.h
@@ -24,8 +24,8 @@
 #include <rtc_timer.h>
 
 #define SOFTAP_MAX_BUFFER_SIZE	4096
-#define AP_BSS_START_DELAY	200000
-#define AP_BSS_STOP_DELAY	500000
+#define AP_BSS_START_DELAY	100000
+#define AP_BSS_STOP_DELAY	200000
 #define AP_SET_CFG_DELAY	500000
 #define AP_DRIVER_START_DELAY	800000
 #define AP_CHANNEL_DEFAULT	6
diff --git a/ap/app/zte_comm/wlan/src/wifi_ap_ctrl.c b/ap/app/zte_comm/wlan/src/wifi_ap_ctrl.c
index 7cd5c1d..b773329 100755
--- a/ap/app/zte_comm/wlan/src/wifi_ap_ctrl.c
+++ b/ap/app/zte_comm/wlan/src/wifi_ap_ctrl.c
@@ -376,7 +376,7 @@
 		ap_svr->sock_m.pid = pid;
 #endif		
         wf_log("SoftAP started successfully pid=%d pid=%d", ap_svr->sock.pid,ap_svr->sock_m.pid);
-        //usleep(AP_BSS_START_DELAY);
+        usleep(AP_BSS_START_DELAY);
     }
     return 0;
 }
@@ -1545,8 +1545,36 @@
 	return ret;
 }
 
+/* Started by AICoder, pid:6a44d66d5ewb7ef142b8088b70a260601ed87c9b */
+static int reinit_drv(struct wlan_ap_server *ap_svr)
+{
+	int ret = -1;
+	int ifindex = 0;
+	
+	ret = ap_svr->drv_proxy.drv_init(&ap_svr->drv_proxy);
+	if (ret != 0) {
+		wf_log("drv_init failed.");
+		return -1;
+	}
+	
+	ifindex = if_nametoindex(ap_svr->drv_proxy.iface_name);
+	if (ifindex == 0) {
+		wf_log("iface not exist.");
+		return -1;
+	}
+	
+	return 0;
+}
+
 static void restart_due_cap(struct wlan_ap_server *ap_svr)
 {
+	int ret = -1;
+
+	if (g_cap_wifi_enable == 0) {
+		wf_log("wifi not opened.");
+		return;
+	}
+	
 	//stop
 	if (g_cap_sta_enable == 1) {
 		wifi_station_close();
@@ -1563,21 +1591,29 @@
 	
 	//start
 	if (g_cap_wifi_enable == 1) {
-		ap_svr->drv_proxy.drv_init(&ap_svr->drv_proxy);
-		send_wlan_status_to_cap(IN_WIFI_STATUS_ENABLED);
+		ret = reinit_drv(ap_svr);
+		if (ret != 0) {
+			g_cap_wifi_enable = 0;
+			send_wlan_status_to_cap(IN_WIFI_STATUS_FIRMWARE_CRASH);
+			return;
+		}
 	}
 
 	if (g_cap_ap_enable == 1) {
-		ap_svr->startap(ap_svr);
+		ret = ap_svr->startap(ap_svr);
+		if ((g_cap_sta_enable == 1) && (ret == 0)) {
+			wait_ap_enabled();
+		}
+		wf_log("startap:%d.", ret);
 	}
 
 	if (g_cap_sta_enable == 1) {
-		wait_ap_enabled();
 		wlan_station_init();
 		sta_num_apsta_open();
 	}
 
 }
+/* Ended by AICoder, pid:6a44d66d5ewb7ef142b8088b70a260601ed87c9b */
 #endif
 
 static void open_wifi_nv_set()
@@ -1660,24 +1696,24 @@
 
 /* Started by AICoder, pid:a4e1a16bf7036c114fae096b506fec24b3125d96 */
 static int get_wlan_mac_address(const char *interface, char *mac_addr) {
-    // ¹¹½¨°üº¬½Ó¿ÚÐÅÏ¢µÄÎļþ·¾¶
+    // ¹¹½¨°üº¬½Ó¿ÚÐÅÏ¢µÄÎļþ·¾¶
     char file_path[100];
     snprintf(file_path, sizeof(file_path), "/sys/class/net/%s/address", interface);
     
-    // ´ò¿ª°üº¬½Ó¿ÚÐÅÏ¢µÄÎļþ
+    // ´ò¿ª°üº¬½Ó¿ÚÐÅÏ¢µÄÎļþ
     FILE *fp = fopen(file_path, "r");
     if (fp == NULL) {
         perror("cat not open wlan address file");
         return -1;
     }
 
-    // ´ÓÎļþÖжÁÈ¡MACµØÖ·
-    if (fgets(mac_addr, 18, fp) == NULL) { // MACµØÖ·Í¨³£ÊÇ17¸ö×Ö·û¼ÓÉÏÖÕÖ¹·û
+    // ´ÓÎļþÖжÁÈ¡MACµØÖ·
+    if (fgets(mac_addr, 18, fp) == NULL) { // MACµØÖ·Í¨³£ÊÇ17¸ö×Ö·û¼ÓÉÏÖÕÖ¹·û
         fclose(fp);
         return -1;
     }
 
-    // ¹Ø±ÕÎļþ²¢·µ»Ø³É¹¦
+    // ¹Ø±ÕÎļþ²¢·µ»Ø³É¹¦
     fclose(fp);
     return 0;
 }
@@ -1689,7 +1725,7 @@
 	sc_cfg_get ("ssid_write_flag", ssid_write_flag, sizeof (ssid_write_flag));
 	wf_log ("ssid_write_flag = %s", ssid_write_flag);
 
-	/*ÓëNVÖÐÊÇ·ñÒÑдÈëSSID µÄ±ê־λ±È½Ï£¬Èç¹ûΪ1±íʾÒÑдÈ룬²»ÓÃÔÙд£»0±íʾ»¹Î´Ð´Èë*/
+	/*ÓëNVÖÐÊÇ·ñÒÑдÈëSSID µÄ±ê־λ±È½Ï£¬Èç¹ûΪ1±íʾÒÑдÈ룬²»ÓÃÔÙд£»0±íʾ»¹Î´Ð´Èë*/
 	if (!strcmp (ssid_write_flag, "0")) {
 		char  wifi_mac[20]={0};
 		get_wlan_mac_address(ap_server->drv_proxy.iface_name, wifi_mac);
@@ -1818,8 +1854,9 @@
 		return -1;
 	}
 
+	ret = -1;
 	wait_bridge();
-	usleep (200000);
+	//usleep (200000);
 	if(0 == startSoftap (ap_svr)){
 		//usleep (200000);
 		if(0 == wifi_connect_to_hostapd(&ap_svr->sock)){
@@ -1856,6 +1893,9 @@
 		else{
 			wf_log (" socket connect or attach failed.");
 			wlan_set_state(WLAN_OFF,WLAN_OFF);
+		#ifdef USE_CAP_SUPPORT
+			send_ap_status_to_cap(IN_WIFI_AP_INDEX_MAX, IN_WIFI_AP_STATUS_ERROR);
+		#endif
 		}
 
 	}else{
@@ -2138,7 +2178,15 @@
 	int i = 1;
 	int find_wlan = -1;
 	char *reply = NULL;
-	ap_svr->drv_proxy.drv_init(&ap_svr->drv_proxy);
+	/* Started by AICoder, pid:ld7cfg0cc0s40df149b9083090b6f01330e3ae49 */
+	int ret = 0;
+	
+	ret = ap_svr->drv_proxy.drv_init(&ap_svr->drv_proxy);
+	if(-1 == ret) {
+		wf_log ("drv_init fail!");
+		return -1;
+	}
+	/* Ended by AICoder, pid:ld7cfg0cc0s40df149b9083090b6f01330e3ae49 */
 	for (i = 1; i <= 10; i++) {
 		wf_log ("ifconfig wlan i=%d", i);
 		find_wlan = wfsystem ("ifconfig wlan0");
@@ -2147,7 +2195,7 @@
 				wf_log ("drv_init failed!!");
 				return -1;
 			} else {
-				usleep (200000 * i);
+				usleep (200000);
 				continue;
 			}
 		}
@@ -2169,21 +2217,22 @@
 	return 0;
 }
 
-//wait max 10s for acs scan finish
+//wait max 8s for acs scan finish
+/* Started by AICoder, pid:he98fob7146b9731425108fc209e1a1b66425f2c */
 static void wait_ap_enabled(void)
 {
 	int i = 0;
-	for (i = 1; i <= 50; i++) {
+	for (i = 1; i <= 40; i++) {
 		if (g_ap_enabled == 0) {
 			usleep(200000);
 		} else {
-			usleep(100000);
+			//usleep(100000);
 			break;
 		}
 	}
 
 }
-
+/* Ended by AICoder, pid:he98fob7146b9731425108fc209e1a1b66425f2c */
 
  void *hostap_loop (void *param)
 {	
diff --git a/ap/app/zte_comm/wlan/src/wifi_socket.c b/ap/app/zte_comm/wlan/src/wifi_socket.c
index d697676..f5fa2a8 100755
--- a/ap/app/zte_comm/wlan/src/wifi_socket.c
+++ b/ap/app/zte_comm/wlan/src/wifi_socket.c
@@ -65,7 +65,7 @@
 		if (skt->ctrl_conn == NULL) {
 			if(i<50){
 				wf_log ("wpa_ctrl_open ctrl_conn times= %d", i);
-				usleep(500000);
+				usleep(100000);
 				continue;
 			}
 			wf_log ("Unable to open connection to hostapd on \"%s\": %s \n",
diff --git a/ap/app/zte_comm/wlan/src/wifi_sta_ctrl.c b/ap/app/zte_comm/wlan/src/wifi_sta_ctrl.c
index 8603cb0..028b9df 100755
--- a/ap/app/zte_comm/wlan/src/wifi_sta_ctrl.c
+++ b/ap/app/zte_comm/wlan/src/wifi_sta_ctrl.c
@@ -112,7 +112,7 @@
 int wifi_start_supplicant (struct  wlan_sta_manager *sta_ctrl)

 {

 

-	int count = 200; /* wait at most 20 seconds for completion */

+	int count = 50; /* wait at most 5 seconds for completion */

 	char 	wifi_root_dir[32] = {0};

 	char *buf = NULL;

 	int ret = -1;

@@ -161,7 +161,7 @@
 

 	while (count-- > 0) {

 		if (access(WPA_PID_FILE, F_OK) == 0){

-			usleep (100000);

+			//usleep (100000);

 			sta_ctrl->sock.pid = read_wpa_pid();

 			if(sta_ctrl->sock.pid == 0) {

 				usleep (100000);

diff --git a/ap/app/zte_comm/wlan/src/wlan-station.c b/ap/app/zte_comm/wlan/src/wlan-station.c
index 54070e3..a12db51 100755
--- a/ap/app/zte_comm/wlan/src/wlan-station.c
+++ b/ap/app/zte_comm/wlan/src/wlan-station.c
@@ -2720,6 +2720,32 @@
 

 }

 

+/* Started by AICoder, pid:17c05ab2631ba551484608a0f093f11de3c5165e */

+void cap_close_station(void)

+{

+	g_cap_sta_enable = 0;

+	send_sta_status_to_cap(IN_WIFI_STA_STATUS_NONE, IN_WIFI_REASON_CODE_NONE);

+	//sc_cfg_set("wifi_sta_connection", "0");

+

+	if (0 == g_wpa_supplicant) {

+		wf_log("station has closed");

+		return;

+	}

+	//MSG_CMD_WIFI_STATION_CLOSE;

+	sta_disconnected();

+	wlan_station_deinit();

+	if (g_work_mode == IN_WIFI_WORK_MODE_AP0 || g_work_mode == IN_WIFI_WORK_MODE_AP1) {

+		//no mssid, not need

+		//basic_deal_all (WIFI_CFG_RESTART_AP);

+	}

+	else {

+		sc_cfg_set ("wifi_cur_state", WIFI_CLOSED);

+	}

+

+}

+/* Ended by AICoder, pid:17c05ab2631ba551484608a0f093f11de3c5165e */

+

+/* Started by AICoder, pid:ife7a60485s530a149930907308c8b1b9099759f */

 static void cap_station_enable(wlan_sta_enable_t *sta_ena)

 {

 	if (sta_ena->enable == 1) {

@@ -2735,28 +2761,17 @@
 		}

 		else {

 			wf_log("-MSG_CMD_WIFI_STATION_OPEN-");

+			sc_cfg_set ("wifi_cur_state", WIFI_OPENED);

 			wlan_station_init();

 		}

 	}

 	else {

-		g_cap_sta_enable = 0;

-		send_sta_status_to_cap(IN_WIFI_STA_STATUS_NONE, IN_WIFI_REASON_CODE_NONE);

-		//sc_cfg_set("wifi_sta_connection", "0");

-

-		if (0 == g_wpa_supplicant) {

-			wf_log("station has closed");

-			return;

-		}

-		//MSG_CMD_WIFI_STATION_CLOSE;

-		sta_disconnected();

-		wlan_station_deinit();

-		if (g_work_mode == IN_WIFI_WORK_MODE_AP0 || g_work_mode == IN_WIFI_WORK_MODE_AP1) {

-			//no mssid, not need

-			//basic_deal_all (WIFI_CFG_RESTART_AP);

-		}

+		cap_close_station();

 	}

 

 }

+/* Ended by AICoder, pid:ife7a60485s530a149930907308c8b1b9099759f */

+

 

 static int get_cur_ap_rssi(void)

 {

@@ -3167,7 +3182,7 @@
 		if (strcmp (sta_ip_status, "connect") == 0 || strcmp (sta_ip_status, "dhcping") == 0) {

 			sta_disconnected();

 		#ifdef USE_CAP_SUPPORT

-			send_sta_status_to_cap(IN_WIFI_STA_STATUS_IDLE, IN_WIFI_REASON_CODE_NONE);

+			send_sta_status_to_cap(IN_WIFI_STA_STATUS_DISCONNECTED, IN_WIFI_REASON_CODE_NONE);

 		#endif

 		}

 		else{

diff --git a/ap/app/zte_comm/wlan/src/wlan_main.c b/ap/app/zte_comm/wlan/src/wlan_main.c
index 424451c..1cfc5e1 100755
--- a/ap/app/zte_comm/wlan/src/wlan_main.c
+++ b/ap/app/zte_comm/wlan/src/wlan_main.c
@@ -49,8 +49,10 @@
 extern int send_sta_status_to_cap(int status, int reason);

 extern int wlan_drv_test_init(void);

 extern int wlan_drv_test_deinit(void);

+extern void cap_close_station(void);

 extern int wifi_get_ap_mac(void);

 

+

 int send_ap_status_to_cap(int idx, int status);

 int send_sta_change_to_cap(int idx, int status, char *mac);

 #endif

@@ -868,9 +870,9 @@
 

 	//if (va0) station_mac0

 	//old²»ÔÚnewÖÐ ¶Ï¿ª

-	chekc_sta_change(IN_WIFI_AP_INDEX_AP0, sta_mac_old, sta_mac_new, 0);

+	chekc_sta_change(IN_WIFI_AP_INDEX_MAX, sta_mac_old, sta_mac_new, 0);

 	//new²»ÔÚoldÖÐ ½ÓÈë

-	chekc_sta_change(IN_WIFI_AP_INDEX_AP0, sta_mac_new, sta_mac_old, 1);

+	chekc_sta_change(IN_WIFI_AP_INDEX_MAX, sta_mac_new, sta_mac_old, 1);

 

 	//if (va1) station_mac1

 

@@ -962,7 +964,16 @@
 {		

 	wlan_sta_connect_status_t w_stat = {0};

 	int ret = 0;

-

+	/* Started by AICoder, pid:abb41t0ebcg3e4c14bb50bb640828d1bed15f44f */

+	if (idx == IN_WIFI_AP_INDEX_MAX) {

+		if (g_work_mode == IN_WIFI_WORK_MODE_AP1) {

+			idx = IN_WIFI_AP_INDEX_AP1;

+		}

+		else {

+			idx = IN_WIFI_AP_INDEX_AP0;

+		}

+	}

+	/* Ended by AICoder, pid:abb41t0ebcg3e4c14bb50bb640828d1bed15f44f */

 	if (idx <= IN_WIFI_AP_INDEX_MIN || idx >= IN_WIFI_AP_INDEX_MAX) {

 		wf_log("idx:%d not support\n", idx);

 		return -1;

@@ -1010,7 +1021,8 @@
 		wf_log ("at cmd need rsp!");

 		return 0;

 	}

-/* Ended by AICoder, pid:g0fb0rb932z35711469a0b6320f5110490e688a6 */

+

+

 #if !(defined(CONFIG_WIFI_EFUSE_MAC))

 	sc_cfg_get("wifi_mac", wifi_mac, sizeof(wifi_mac));

 	if(strlen(wifi_mac) == 0){

@@ -1019,6 +1031,8 @@
 		return -1;

 	}

 #endif

+/* Ended by AICoder, pid:g0fb0rb932z35711469a0b6320f5110490e688a6 */

+

 	return 0;

 }

 

@@ -1083,7 +1097,7 @@
 	}

 

 	if (strcmp (wifi_sta_connection, "1") == 0) {

-		send_sta_status_to_cap(IN_WIFI_STA_STATUS_NONE, IN_WIFI_REASON_CODE_NONE);

+		cap_close_station();

 		sc_cfg_set("wifi_sta_connection", "0");

 	}

 

@@ -1644,7 +1658,7 @@
 		break;

 	case MSG_CMD_WIFI_FORCE_RESTART:// broadcom ,realtek need, form netlink msg

 #ifdef USE_CAP_SUPPORT

-		send_wlan_status_to_cap(IN_WIFI_STATUS_FIRMWARE_CRASH);

+		//send_wlan_status_to_cap(IN_WIFI_STATUS_FIRMWARE_CRASH);

 #endif

 		//ÔÚ wifi firmware crashºóµÄÖØÆôÁ÷³Ì£¬²»ÔÊÐíwebui·¢ËÍÏûÏ¢ÏÂÀ´£¬ËùÒÔ½«RadioOffÖÃ0

 		//sc_cfg_set("wifi_cur_state", WIFI_CLOSED);

@@ -1684,7 +1698,7 @@
 		add_sta_mac(pMsg->aucDataBuf);

 #ifdef USE_CAP_SUPPORT

 		//Èç¹û¸±ssidÒ²Òª,¸±ssidµÄMSG_CMD_AP_STA_CONNECTED»»Ò»¸öÏûÏ¢Ãû

-		send_sta_change_to_cap(IN_WIFI_AP_INDEX_AP0, 1, pMsg->aucDataBuf);

+		send_sta_change_to_cap(IN_WIFI_AP_INDEX_MAX, 1, pMsg->aucDataBuf);

 #endif

 		break;

 	case MSG_CMD_AP_STA_DISCONNECTED:

@@ -1692,7 +1706,7 @@
 		//wlan_prepare_sleep();

 #ifdef USE_CAP_SUPPORT

 		//Èç¹û¸±ssidÒ²Òª,¸±ssidµÄMSG_CMD_AP_STA_CONNECTED»»Ò»¸öÏûÏ¢Ãû

-		send_sta_change_to_cap(IN_WIFI_AP_INDEX_AP0, 0, pMsg->aucDataBuf);

+		send_sta_change_to_cap(IN_WIFI_AP_INDEX_MAX, 0, pMsg->aucDataBuf);

 #endif

 		break;