[BugFix][API-1317]fix sta didn't auto-conenct,when conenct ap without disconenct_ap

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: Ied5cf1f0069f8baae0017184809b3d64c377125b
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index c731388..70d66cb 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -96,7 +96,7 @@
     pthread_mutex_t mutex;
 };
 
-int g_history_disconnect_valid_num = 0;
+volatile int g_history_disconnect_valid_num = 0;
 int g_history_disconnect_net[128];
 
 static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -143,6 +143,73 @@
 static int s_service_invoke_timeout_cnt=0;
 const int FAKE_MAX_INT_VALUE = 99999;
 
+static void print_disconnect_list()
+{
+    int i;
+    for( i = 0; i < g_history_disconnect_valid_num; i++ )
+    {
+        RLOGD(" list of g_history_disconnect_valid_num is  %d histroy_list[%d]:%d --------- %d",g_history_disconnect_valid_num,i,g_history_disconnect_net[i],__LINE__);
+    }
+
+    return;
+}
+
+// idex  ----> history_disconnect_list[x]  index
+static int removeElement(int idex)
+{
+    RLOGD("into removeElement");
+    if( index < 0 )
+    {
+        RLOGD("WIFI [removeElement] input idex < 0,idex is %d: ",idex);
+        return -1;
+    }
+    RLOGD("%s line: %d  g_history_disconnect_net[%d]: %d  end g_history_disconnect_net[%d]:%d",__func__,__LINE__,idex,g_history_disconnect_net[idex],g_history_disconnect_valid_num-1, g_history_disconnect_net[g_history_disconnect_valid_num-1]);
+    g_history_disconnect_net[idex] = g_history_disconnect_net[g_history_disconnect_valid_num-1];    //g_history_disconnect_vaild_num  -1 for get last index
+    g_history_disconnect_valid_num --;
+    RLOGD("end removeElement");
+    return 0;
+}
+static int check_history_disconenct_ap_list(int val)
+{
+    print_disconnect_list();
+    RLOGD("WIFI[check_history_disconenct_ap_list]into check_history_disconenct_ap_list && input val is %d g_history_disconnect_valid_num is %d line",val,g_history_disconnect_valid_num,__LINE__);
+    int i;
+    for( i = 0; i < g_history_disconnect_valid_num; i++)
+    {
+        if( val == g_history_disconnect_net[i] )
+        {
+             RLOGD("[wifi]-----input val is %d,g_history_disconnect_net[%d]:%d",val,i,g_history_disconnect_net[i]);
+             RLOGD("end check_history_disconenct_ap_list && return network index");
+             return i;
+        }
+    }
+    RLOGD("end check_history_disconenct_ap_list && return fail,didn't need remove networkid %d from list  g_history_disconnect_valid_num is %d line %d",val,g_history_disconnect_valid_num,__LINE__);
+    return -1;
+}
+
+
+static void lynq_sta_removeElement(int net_no)
+{
+    int ret;
+
+    ret = check_history_disconenct_ap_list(net_no);
+    if( ret == -1 )
+    {
+        RLOGD("curr_net_no not in history_disconenct_lsit,return 0 %s %d",__func__,__LINE__);
+        return;
+    }else
+    {
+        ret = removeElement(ret);
+        if( ret == 0 )
+        {
+            RLOGD("removeElement pass %s %d",__func__,__LINE__);
+            return;
+        }
+    }
+
+    return;
+}
+
 static void notify_service_invoke_fail(int error)
 {
     struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
@@ -837,19 +904,27 @@
 */
 static void lynq_two_arr_merge(int dis_net_list[],int valid_num,int out[],int * outlen)
 {
+    RLOGD("enter %s %d",__func__,__LINE__);
+    print_disconnect_list();
     int count,ncount,index;
     int flag = 0;
     int merge_index = 0;
     int net_no_list[128];
 
+    for(ncount = 0;ncount < valid_num; ncount++ )
+    {
+        RLOGD("input history disconenct_list[%d] %d %d",ncount,dis_net_list[ncount],__LINE__);
+    }
+
     index =lynq_get_network_number_list(0, 0, net_no_list,NULL);
     for( count = 0; count < index; count++)
     {
         for(ncount = 0; ncount < valid_num; ncount++)
         {
+            RLOGD(" %s dis_net_list[%d]->%d %d",__func__,ncount,dis_net_list[ncount],__LINE__);
             if(net_no_list[count] == dis_net_list[ncount])
             {
-                RLOGD("[wifi]this is history disconnect idx ----> ",net_no_list[count]);
+                RLOGD("[wifi]this is history disconnect idx ----> %d %d",net_no_list[count],__LINE__);
                 flag = 1;
                 break;
             }
@@ -857,6 +932,7 @@
         if( flag != 1 )
         {
             out[merge_index] = net_no_list[count];
+            RLOGD("out[%d]: %d    net_no_list[%d]: %d %d",merge_index,out[merge_index],count,net_no_list[count],__LINE__);
             merge_index ++;
         }
         flag = 0;
@@ -891,7 +967,7 @@
     if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
     {
         *state = LYNQ_WIFI_STA_STATUS_CONNECT;
-        RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
+        RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d",*state,*error);
         g_sta_conncet_status_flag = 0;
         return;
     }
@@ -1252,6 +1328,7 @@
                         system_call_v("%s %s", sta_status_change_script, "disconnect");
                     }
                 }
+
                 last_state = state;
             }
         }
@@ -3566,6 +3643,7 @@
 
                 pthread_mutex_lock(&s_global_check_mutex);
                 s_sta_status = INNER_STA_STATUS_CONNECTED;
+                lynq_sta_removeElement(net_no);
                 pthread_cond_signal(&s_global_check_cond);
                 pthread_mutex_unlock(&s_global_check_mutex);
                 return 0;
@@ -3620,6 +3698,10 @@
     system("echo \"\" > /tmp/wlan0_dhcpcd_router");
     usleep(200*1000);
 
+    pthread_mutex_lock(&s_global_check_mutex);
+    lynq_sta_removeElement(net_no);
+    pthread_mutex_unlock(&s_global_check_mutex);
+
     ret = inner_sta_start_stop(net_no, 1, 1);
 
     pthread_mutex_lock(&s_global_check_mutex);
@@ -3674,9 +3756,11 @@
 
 int lynq_wifi_sta_disconnect_ap(lynq_wifi_index_e idx, char *ssid)
 {
+    int i,check_history_idx_flag;
     ap_info_s ap;
     curr_status_info curr_state;
     ap.ap_ssid[0] = '\0';
+    check_history_idx_flag = 0;
 
     if (ssid == NULL || *ssid == '\0')
     {
@@ -3702,8 +3786,25 @@
 
     pthread_mutex_lock(&s_global_check_mutex);
     s_sta_status = INNER_STA_STATUS_DISCONNECTING;
-    g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
-    g_history_disconnect_valid_num++;
+    RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_valid_num is %d",g_history_disconnect_valid_num);
+    for( i = 0; i< g_history_disconnect_valid_num ; i++)
+    {
+        RLOGD("WIFI[lynq_wifi_sta_disconnect_ap]g_history_disconnect_net[%d] is %d,current disconnet network is %d",i,g_history_disconnect_net[i],curr_state.net_no);
+        if( g_history_disconnect_net[i] == curr_state.net_no)
+        {
+            RLOGD("current disconenct ap idx is %d && last aready into g_history_disconenct_net",curr_state.net_no);
+            check_history_idx_flag = 1;
+            break;
+        }
+    }
+    if ( check_history_idx_flag == 0)
+    {
+        RLOGD("current need add ap idx is %d ,g_history_disconnect_valid_num is %d line %d",curr_state.net_no,g_history_disconnect_valid_num,__LINE__);
+        g_history_disconnect_net[g_history_disconnect_valid_num] = curr_state.net_no;
+        g_history_disconnect_valid_num++;
+    }
+    RLOGD("%s %d",__func__,__LINE__);
+    print_disconnect_list();
     pthread_mutex_unlock(&s_global_check_mutex);
     return lynq_wifi_sta_stop_network(idx, curr_state.net_no);
 
@@ -3999,6 +4100,7 @@
     int count, net_no, index;
     int net_no_list[128];
     lynq_wifi_auth_s net_auth;
+
     char lynq_remove_cmd[MAX_CMD];
 
     if (ssid == NULL || *ssid == '\0')
@@ -4032,6 +4134,15 @@
     sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
 
     DO_OK_FAIL_REQUEST(lynq_remove_cmd);
+
+    RLOGD("WIFI[lynq_sta_forget_ap][check_history_disconenct_ap_list] input net_no is %d",net_no);
+
+    pthread_mutex_lock(&s_global_check_mutex);
+    lynq_sta_removeElement(net_no);
+    pthread_mutex_unlock(&s_global_check_mutex);
+
+    RLOGD("%s %d",__func__,__LINE__);
+    print_disconnect_list();
     DO_OK_FAIL_REQUEST(cmd_save_config);
 
     return 0;