[Bugfix][API-934][API-939][API-867][API-872] delete bss_flush function;fix bugs since add wpa3;add wpa service state check;add LYNQ_WIFI_STA_STATUS_CONNECT_FAIL for each connect request

Change-Id: I07ea80e7f518657ebfe47c8ff6112ecfe3316997
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index d2df291..e6c394b 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -22,6 +22,8 @@
 #include <netdb.h>
 #include <ifaddrs.h>
 #include "log/log.h"
+#include <sys/time.h>
+#include <asm/errno.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -73,6 +75,7 @@
     pthread_mutex_t mutex;
 };
 
+
 static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
@@ -91,6 +94,75 @@
     int net_no;
 }curr_status_info;
 
+typedef enum {
+    INNER_STA_STATUS_INIT = 0,
+    INNER_STA_STATUS_CONNECTING,
+    INNER_STA_STATUS_ASSOCIATING,
+    INNER_STA_STATUS_ASSOCIATED,
+    INNER_STA_STATUS_CONNECTED,
+    INNER_STA_STATUS_DISCONNECTING,
+    INNER_STA_STATUS_DISCONNECTED,
+    INNER_STA_STATUS_CANCEL,
+}inner_sta_status_s;
+
+static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
+static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
+static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
+static error_number_s s_sta_error_number = -1;
+static char s_sta_current_connecting_ssid[64] = {0};
+static struct timespec s_sta_connect_timeout;
+const int MAX_CONNNECT_TIME = 15; // second
+pthread_t g_global_watcher_pid = 0;
+static int s_service_invoke_timeout_cnt=0;
+const int FAKE_MAX_INT_VALUE = 99999;
+
+static void notify_service_invoke_fail(int error)
+{
+    struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
+    pthread_mutex_lock(&s_global_check_mutex);
+    if (error == -2) //timeout
+    {
+        s_service_invoke_timeout_cnt++;
+        if (s_service_invoke_timeout_cnt > 10)
+        {
+            pthread_cond_signal(&s_global_check_cond);
+        }
+    }
+    else if (error == -1)
+    {
+        // check if can connect wpa service
+        lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
+        if (lynq_wpa_ctrl == NULL)
+        {
+            s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
+            pthread_cond_signal(&s_global_check_cond);
+        }
+        wpa_ctrl_close(lynq_wpa_ctrl);
+        lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
+        if (lynq_wpa_ctrl == NULL)
+        {
+            s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
+            pthread_cond_signal(&s_global_check_cond);
+        }
+        wpa_ctrl_close(lynq_wpa_ctrl);
+    }
+
+    pthread_mutex_unlock(&s_global_check_mutex);
+}
+
+static void check_tether_and_notify()
+{
+    RLOGD("check_tether_and_notify called");
+    if (0 == system("ifconfig | grep tether"))
+    {
+        return;
+    }
+    pthread_mutex_lock(&s_global_check_mutex);
+    s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
+    pthread_cond_signal(&s_global_check_cond);
+    pthread_mutex_unlock(&s_global_check_mutex);
+}
+
 static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
                             char *reply, size_t *reply_len,
                                   void (*msg_cb)(char *msg, size_t len))
@@ -103,6 +175,10 @@
     pthread_mutex_lock(&ctrl->mutex);
     ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
     pthread_mutex_unlock(&ctrl->mutex);
+    if (ret != 0)
+    {
+        notify_service_invoke_fail(ret);
+    }
     return ret;
 }
 
@@ -197,6 +273,10 @@
     if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
     {
         RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
+        if (ret != 0)
+        {
+            notify_service_invoke_fail(ret);
+        }
         return -1;
     }
 
@@ -228,9 +308,11 @@
         }
         else
         {
+            RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
             wpa_ctrl_close(*pp_lynq_wpa_ctrl);
             *pp_lynq_wpa_ctrl = NULL;
             *idle_count = 0;
+            notify_service_invoke_fail(-2);
             usleep(SLEEP_TIME_ON_IDLE);
             return -1;
         }
@@ -262,6 +344,7 @@
         wpa_ctrl_close(*pp_lynq_wpa_ctrl);
         *pp_lynq_wpa_ctrl = NULL;
         *idle_count = 0;
+        notify_service_invoke_fail(ret);
         return -1;
     }
 
@@ -281,6 +364,11 @@
     {
         if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
         {
+            if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
+            {
+                check_tether_and_notify();
+            }
+
             continue;
         }
 
@@ -327,6 +415,47 @@
     }
 }
 
+static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
+{
+    char * p;
+    const char * try_associat_flag = "Trying to associate";
+    const char * associated_flag = "Associated with ";
+
+    pthread_mutex_lock(&s_global_check_mutex);
+    if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
+    {
+        pthread_mutex_unlock(&s_global_check_mutex);
+        return;
+    }
+
+    if (state == LYNQ_WIFI_STATUS_EGNORE)
+    {
+        if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
+        {
+            s_sta_status = INNER_STA_STATUS_ASSOCIATING;
+        }
+        else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
+        {
+            s_sta_status = INNER_STA_STATUS_ASSOCIATED;
+        }
+    }
+    else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
+    {
+        s_sta_error_number = error_num;
+        if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
+        {
+            s_sta_status = INNER_STA_STATUS_DISCONNECTED;
+            pthread_cond_signal(&s_global_check_cond);
+        }
+    }
+    else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
+    {
+        s_sta_status = INNER_STA_STATUS_CONNECTED;
+        pthread_cond_signal(&s_global_check_cond);
+    }
+    pthread_mutex_unlock(&s_global_check_mutex);
+}
+
 void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
 {
     char *pReason;
@@ -445,6 +574,16 @@
 
 }
 
+static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
+{
+    if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
+    {
+        RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
+        g_sta_callback_func(g_sta_callback_priv, state, error);
+        RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
+    }
+}
+
 static void STAWatcherThreadProc() {
     size_t len = MAX_RET;
     char msg_notify[MAX_RET];
@@ -455,6 +594,7 @@
     struct wpa_ctrl *lynq_wpa_ctrl = NULL;
     g_sta_watcher_stop_flag = 0;
 
+    RLOGD("STAWatcherThreadProc thread started ------");
     while (g_sta_watcher_stop_flag == 0)
     {
         if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
@@ -478,11 +618,11 @@
                 continue;
             }
             get_state_error(msg_notify,&state,&error);
-            if (state != LYNQ_WIFI_STATUS_EGNORE)
+            notify_connect_status(state, error);
+
+            if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
             {
-                RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
-                g_sta_callback_func(g_sta_callback_priv, state, error);
-                RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
+                inner_check_connect_error(msg_notify, state, error);
             }
         }
     }
@@ -493,6 +633,128 @@
     }
 }
 
+// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
+static void GlobalWatcherThreadProc()
+{
+    int ret, connect_timeout, service_abnormal;
+    error_number_s error_num = -1;
+    inner_sta_status_s sta_status;
+    scan_info_s *scan_list = NULL;
+    int i, scan_len=0;
+    char connecting_ssid[64];
+    struct timeval now;
+
+    RLOGD("GlobalWatcherThreadProc start to run");
+
+    while (1)
+    {
+        pthread_mutex_lock(&s_global_check_mutex);
+        pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
+        if (s_sta_status == INNER_STA_STATUS_CONNECTED)
+        {
+            pthread_mutex_unlock(&s_global_check_mutex);
+            usleep(50*1000);
+            notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
+            continue;
+        }
+
+        connect_timeout = 0;
+        service_abnormal = 0;
+        if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
+        {
+            while (1)
+            {
+                ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
+                if (ret == ETIME)
+                {
+                    connect_timeout = 1;
+                }
+                else if (ret != 0)
+                {
+                    gettimeofday(&now,NULL);
+                    if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
+                    {
+                        usleep(SLEEP_TIME_ON_IDLE);
+                        continue;
+                    }
+                    connect_timeout = 1;
+                }
+                sta_status = s_sta_status;
+                error_num = s_sta_error_number;
+                s_sta_status = INNER_STA_STATUS_INIT;
+                strcpy(connecting_ssid, s_sta_current_connecting_ssid);
+                memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
+                memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
+                break;
+            }
+        }
+        if (s_service_invoke_timeout_cnt > 10)
+        {
+            service_abnormal = 1;
+            s_service_invoke_timeout_cnt = 0;
+        }
+        pthread_mutex_unlock(&s_global_check_mutex);
+
+        if (service_abnormal == 1)
+        {
+            sleep(1);
+            RLOGE("wpa service is abnormal info app to exit");
+            notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
+            if (g_ap_callback_func != NULL)
+            {
+                g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_SERVICE_ABNORMAL);
+            }
+            sleep(FAKE_MAX_INT_VALUE); // wait process to exit
+        }
+
+        if (sta_status == INNER_STA_STATUS_CANCEL)
+        {
+            continue;
+        }
+        else if (sta_status == INNER_STA_STATUS_CONNECTED)
+        {
+            notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
+        }
+        else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
+        {
+            if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
+            {
+                for(i=0; i < scan_len;i++)
+                {
+                    if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
+                    {
+                        error_num = LYNQ_AUTH_ERROR;
+                        break;
+                    }
+                }
+                free(scan_list);
+            }
+            notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
+        }
+        else if (connect_timeout == 0)
+        {
+            notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
+        }
+        else // wait timeout
+        {
+            if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
+            {
+                ; // wpa service abnormal
+            }
+            else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
+            {
+                RLOGD("GlobalWatcherThreadProc notify connected");
+                notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
+            }
+            else
+            {
+                RLOGD("GlobalWatcherThreadProc notify timeout");
+                notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
+            }
+        }
+    } // while (1)
+}
+
 int lynq_wifi_enable(void)
 {
     int ret = 0;
@@ -569,7 +831,7 @@
     {
         ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
         if(ret<0)
-	{
+        {
             RLOGE("[wifi error]creat APWatcherThreadProc fail");
             ret = -1;
             goto out_enable;
@@ -580,19 +842,30 @@
     if (g_sta_watcher_pid == 0 ) {
         ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
         if(ret<0)
-	{
+        {
             RLOGE("[wifi error]creat STAWatcherThreadProc fail");
             ret = -1;
             goto out_enable;
         }
     }
 
+    if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
+    {
+        ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
+        if(ret<0)
+        {
+            RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
+            ret = -1;
+            goto out_enable;
+        }
+    }
+
     RLOGD("creat STAWatcherTheradProc susccs");
     for (i=0; i<10; i++)
     {
         usleep(300*1000);
         if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1)
-	{
+        {
             break;
         }
     }
@@ -1042,6 +1315,7 @@
     return -1;
 }
 
+static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
 static int inner_get_status_info(int interface, curr_status_info *curr_state) {
     int i, count;
     char *p;
@@ -1072,24 +1346,24 @@
     for(i=0; i < count; i++)
     {
         if (curr_state->ap != NULL)
-	{
+        {
             p = strstr(split_lines[i], FLAG_SBSID);
             if (p != NULL)
-	    {
+            {
                 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
                 ret = 0;
                 continue;
             }
             p = strstr(split_lines[i], FLAG_SSID);
             if (p != NULL)
-	    {
+            {
                 inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid));
                 ret = 0;
                 continue;
             }
             p = strstr(split_lines[i], FLAG_KEY_MGMT);
             if (p != NULL)
-	    {
+            {
                 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
                 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
                 ret = 0;
@@ -1097,24 +1371,24 @@
             }
             p = strstr(split_lines[i], FLAG_FREQ);
             if (p != NULL)
-	    {
+            {
                 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
                 ret = 0;
                 continue;
             }
             p = strstr(split_lines[i], FLAG_IPADDR);
             if (p != NULL)
-	    {
+            {
                 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
                 ret = 0;
                 continue;
             }
         } // end  if (ap != NULL)
         if (curr_state->state != NULL)
-	{
+        {
             p = strstr(split_lines[i], FLAG_STATE);
             if (p != NULL)
-	    {
+            {
                 strcpy(curr_state->state, p + strlen(FLAG_STATE));
                 ret = 0;
                 continue;
@@ -1122,17 +1396,21 @@
 
         } //end else if (state != NULL)
         if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
-	{
+        {
             ret = 0;
             curr_state->net_no = atoi(p + strlen(FLAG_ID));
             RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
         }
     }
 
+    if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
+    {
+        inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
+    }
+
     return ret;
 }
 
-
 int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
 {
     RLOGD("enter lynq_wifi_ap_ssid_set");
@@ -1668,6 +1946,8 @@
     sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
     DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
 
+    check_tether_and_notify();
+
     return 0;
 }
 
@@ -1904,24 +2184,33 @@
     if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
     {
         if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
-	{
+        {
             if (strcmp(lynq_proto_str, "RSN") == 0)
-	    {
+            {
                 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
                 return 0;
             }
+            else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
+            {
+                return 0;
+            }
         }
     }
+    else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
+    {
+        return 0;
+    }
+
     if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
     {
-	RLOGE("check ieee80211w error\n");
+        RLOGE("check ieee80211w error\n");
         return -1;
     }
     if ( strncmp(lynq_auth_str,"1",1) == 0 )
     {
 
-       *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
-       return 0;
+        *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
+        return 0;
     }else if( strncmp(lynq_auth_str,"2",1) == 0 )
     {
 
@@ -1929,6 +2218,7 @@
         return 0;
     }else
     {
+        RLOGE("check ieee80211w error, not 1 or 2\n");
         *auth = -1;
         return -1;
     }
@@ -2423,6 +2713,9 @@
     int count, net_no, index;
     int net_no_list[128];
     lynq_wifi_auth_s net_auth;
+    curr_status_info curr_state;
+    ap_info_s ap_info;
+    char status[64];
 
     if (ssid == NULL || *ssid == '\0')
     {
@@ -2433,39 +2726,77 @@
     if (LYNQ_WIFI_AUTH_OPEN != auth)
     {
         if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
-	{
+        {
             RLOGE("bad password\n");
             return -1;
         }
     }
 
+
+    pthread_mutex_lock(&s_global_check_mutex);
+    if (s_sta_status != INNER_STA_STATUS_INIT)
+    {
+        s_sta_status = INNER_STA_STATUS_CANCEL;
+        pthread_cond_signal(&s_global_check_cond);
+    }
+    pthread_mutex_unlock(&s_global_check_mutex);
+
     CHECK_IDX(idx, CTRL_STA);
+    CHECK_WPA_CTRL(CTRL_STA);
 
     net_no = -1;
-    count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
+    memset(&ap_info, 0, sizeof (ap_info));
+    memset(status, 0, sizeof (status));
 
-    for (index=0; index < count; index++)
+    curr_state.ap = &ap_info;
+    curr_state.state = status;
+
+    if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
     {
-        net_auth = -1;
-        if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
-	{
-            net_no = net_no_list[index];
-            break;
+        if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
+        {
+            net_no = curr_state.net_no;
+            if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
+                    && strcmp(ap_info.psw, psw) == 0)
+            {
+                RLOGD("already connected\n");
+
+                pthread_mutex_lock(&s_global_check_mutex);
+                s_sta_status = INNER_STA_STATUS_CONNECTED;
+                pthread_cond_signal(&s_global_check_cond);
+                pthread_mutex_unlock(&s_global_check_mutex);
+                return 0;
+            }
         }
     }
 
-    if (net_no < 0)
+    if (net_no == -1)
     {
-        net_no = lynq_add_network(CTRL_STA);
-        if (net_no == -1)
-	{
-            return -1;
+        count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
+
+        for (index=0; index < count; index++)
+        {
+            net_auth = -1;
+            if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
+            {
+                net_no = net_no_list[index];
+                break;
+            }
         }
 
-        RLOGD("net no is %d\n", net_no);
-        if (0 != inner_set_sta_ssid(net_no, ssid))
-	{
-            return -1;
+        if (net_no < 0)
+        {
+            net_no = lynq_add_network(CTRL_STA);
+            if (net_no == -1)
+            {
+                return -1;
+            }
+
+            RLOGD("net no is %d\n", net_no);
+            if (0 != inner_set_sta_ssid(net_no, ssid))
+            {
+                return -1;
+            }
         }
     }
 
@@ -2474,7 +2805,22 @@
         return -1;
     }
 
-    return inner_sta_start_stop(net_no, 1, 1);
+
+    DO_OK_FAIL_REQUEST(cmd_disconnect);
+    usleep(200*1000);
+
+    ret = inner_sta_start_stop(net_no, 1, 1);
+
+    pthread_mutex_lock(&s_global_check_mutex);
+    s_sta_status = INNER_STA_STATUS_CONNECTING;
+    strcpy(s_sta_current_connecting_ssid, ssid);
+    struct timeval now;
+    gettimeofday(&now,NULL);
+    s_sta_connect_timeout.tv_sec = now.tv_sec + MAX_CONNNECT_TIME;
+    s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
+    pthread_cond_signal(&s_global_check_cond);
+    pthread_mutex_unlock(&s_global_check_mutex);
+    return ret;
 }
 
 int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
@@ -2504,6 +2850,9 @@
         return 0;
     }
 
+    pthread_mutex_lock(&s_global_check_mutex);
+    s_sta_status = INNER_STA_STATUS_DISCONNECTING;
+    pthread_mutex_unlock(&s_global_check_mutex);
     return inner_sta_start_stop(curr_state.net_no, 0, 0);
 }
 
@@ -2835,7 +3184,7 @@
 
     CHECK_WPA_CTRL(CTRL_STA);
 
-    system(clean_last_re);
+//    system(clean_last_re); // youchen @ 2023-04-14 temp delete ,next time to fix the orginal bug
     g_sta_scan_finish_flag = 0;
     DO_REQUEST(lynq_scan_cmd);
     if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )