[Bugfix][API-1158]tmp war for fix sta device connect 870ap fail

Only Configure:NO

Affected branch:MR3.0-xx && GSW3.0

Affected module:wifi

Is it addected on both ZXIC and MTK: only MTK

Self-test: Yes

Doc Update: NO

Change-Id: I704a847f0554cc11038981d0c372ad8088e81f32
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index f56cc2d..3f0ad4d 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -41,11 +41,15 @@
 #define CTRL_AP 1
 #define AP_NETWORK_0 0
 #define STA_MAX_SAVED_AP_NUM 50
+#define MAC_LEN 17
 
 pthread_t g_ap_watcher_pid = 0;
 volatile int g_ap_watcher_stop_flag = 0;
 volatile int g_ap_watcher_started_flag = 0;
 
+pthread_t g_ap_tmp_watcher_pid = 0;
+volatile int g_ap_tmp_watcher_stop_flag = 0;
+
 pthread_t g_sta_watcher_pid = 0;
 volatile int g_sta_watcher_stop_flag = 0;
 volatile int g_sta_scan_finish_flag = 1;
@@ -531,6 +535,157 @@
     pthread_mutex_unlock(&s_global_check_mutex);
 }
 
+/*
+just tmp add for fix sta connect ap fail check ap connect info
+return 0 --->Current no sta device connect this AP
+*/
+static int lynq_connected_ap_sta_status() {
+
+    FILE *fp;
+    size_t i = 0;
+    int ret;
+    char lynq_cmd_ret[MAX_RET]={0};
+
+    if((fp=popen("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 list_sta","r"))==NULL)
+    {
+        perror("popen error!");
+        return -1;
+    }
+    if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
+    {
+        perror("fread fail!");
+        ret=pclose(fp);
+        if(ret == -1)
+            perror("close file faild");
+        return -1;
+    }
+    if( strlen(lynq_cmd_ret) < MAC_LEN)
+    {
+        RLOGD("---Current no sta device connect this AP %s  %d\n", lynq_cmd_ret,strlen(lynq_cmd_ret));
+        ret=pclose(fp);
+        if(ret==-1)
+        {
+            perror("close file faild");
+        }
+        return 0;
+    }else{
+        ret=pclose(fp);
+        if(ret==-1)
+        {
+            perror("close file faild");
+        }
+        RLOGD("---Current has sta device connect this AP--- %s\n", lynq_cmd_ret);
+        return 1;
+     }
+}
+
+/*
+    just tmp add for fix sta connect ap fail; check fw status
+    return 1 ----> fw status error; need wl down/up
+*/
+static int check_current_fw_status() {
+
+    FILE *fp;
+    FILE *fp1;
+    size_t i = 0;
+    int ret;
+    char lynq_cmd_ret_2g[MAX_RET]={0};
+    char lynq_cmd_ret_5g[MAX_RET]={0};
+
+    const char * fw_status = "0x0096";    //0x0096 is normal fw status
+
+    if((fp=popen("wl shmem  0x15ee a","r"))==NULL)
+    {
+        perror("popen error!");
+        return -1;
+    }
+    if((fread(lynq_cmd_ret_5g,sizeof(lynq_cmd_ret_2g),1,fp))<0)
+    {
+        perror("fread fail!");
+        if(pclose(fp) == -1)
+            perror("close fp file faild");
+        return -1;
+    }
+
+    if((fp1=popen("wl shmem  0x15ee b","r"))==NULL)
+    {
+        perror("popen error!");
+        if(pclose(fp) == -1)
+            perror("clsoe fp file faild");
+        return -1;
+    }
+    if((fread(lynq_cmd_ret_2g,sizeof(lynq_cmd_ret_5g),1,fp1))<0)
+    {
+        perror("fread fail!");
+        if(pclose(fp1) == -1)
+            perror("clsoe fp1 file faild");
+        if(pclose(fp) == -1)
+            perror("clsoe fp file faild");
+        return -1;
+    }
+
+    if ( strncmp(fw_status,lynq_cmd_ret_2g,6) == 0 || strncmp(fw_status,lynq_cmd_ret_5g,6) == 0 )
+    {
+        ret=pclose(fp);
+        if(ret==-1)
+        {
+            perror("close fp file faild");
+        }
+        ret=pclose(fp1);
+        if(ret==-1)
+        {
+            perror("close fp1 file faild");
+        }
+        return 0;
+    }else
+    {
+        ret=pclose(fp);
+        if(ret==-1)
+        {
+           perror("close file faild");
+        }
+        if(pclose(fp1) == -1)
+        {
+            perror("clsoe file fp1 faild");
+        }
+        RLOGD("current fw status --error--");
+        return 1;
+    }
+}
+
+static void APTmpWatcherThreadProc() {
+
+    int i = 0;
+    int delytime = 300;
+    g_ap_tmp_watcher_stop_flag = 0;
+
+    RLOGD("APTmpWatcherThreadProc ----> ThreadProc start");
+    while(1)
+    {
+        sleep(1);
+        i++;
+        if ( i == delytime )
+        {
+            if( lynq_connected_ap_sta_status() == 0 )      //o --->no sta device connect this ap
+            {
+                if(check_current_fw_status() == 1)        //1 --->current fw status not 0x0096
+                {
+                    system("wl down");
+                    system("wl up");
+                }
+
+            }
+            i = 0;
+        }
+        if( g_ap_tmp_watcher_stop_flag == 1 )       //quit proc
+        {
+           RLOGD("APTmpWatcherThreadProc ----- > ap closed or wifi disabled");
+           return;
+        }
+    }
+
+}
+
 static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
 {
     char LYNQ_DISABLE_CMD[128]={0};
@@ -790,7 +945,7 @@
             RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
             *error = LYNQ_UNSPECIFIED_REASON;
             *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
-            RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d£¬networkid:%d\n",*state,*error,*networkid);
+            RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
             return;
         }
     }
@@ -1126,6 +1281,7 @@
     g_ap_watcher_stop_flag = 1;
     g_sta_watcher_stop_flag = 1;
     g_sta_auto_watcher_stop_flag = 1;
+    g_ap_tmp_watcher_stop_flag = 1;
     if (g_ap_watcher_pid != 0)
         pthread_join(g_ap_watcher_pid, NULL);
     if (g_sta_watcher_pid != 0)
@@ -1136,6 +1292,8 @@
         wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
     if (g_lynq_wpa_ctrl[1] != NULL)
         wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
+    if (g_ap_tmp_watcher_pid != 0)
+        pthread_join(g_ap_tmp_watcher_pid, NULL);
     g_ap_watcher_pid = 0;
     g_sta_watcher_pid = 0;
     g_sta_auto_watcher_pid = 0;
@@ -2280,6 +2438,16 @@
     }
 
     check_tether_and_notify();
+    if (g_ap_tmp_watcher_pid == 0)
+    {
+        if(pthread_create(&g_ap_tmp_watcher_pid,NULL,APTmpWatcherThreadProc,NULL) < 0)
+        {
+            g_ap_tmp_watcher_pid = 0;
+            RLOGE("[wifi error]create APTmpWatcherThreadProc fail");
+            return -1;
+        }
+        RLOGD("[lynq_wifi_ap_start] creat APTmpWatcherThreadProc ok");
+    }
 
     return 0;
 }
@@ -2309,6 +2477,10 @@
         RLOGE("lynq_wifi_ap_start excute script fail");
         return -1;
     }
+    g_ap_tmp_watcher_stop_flag = 1;
+    if (g_ap_tmp_watcher_pid != 0)
+        pthread_join(g_ap_tmp_watcher_pid, NULL);
+    g_ap_tmp_watcher_pid = 0;
 
     return 0;
 }
@@ -3919,6 +4091,7 @@
     return 0;
 }
 
+
 static int check_and_init_uci_config(char * country_code)
 {
     FILE * fp;
@@ -3956,7 +4129,7 @@
 
     if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
     {
-        RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
+	RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
         is_different = 1;
     }