[Bugfix][API-1022] fix get empty or ipv6 in lynq_get_connect_ap_ip
[Bugfix][API-1023] add status check in lynq_wifi_ap_start
[Feature]]T8TSK-210] add lynq_wifi_sta_connect_timeout function

Change-Id: I9808458dcfd8948686ef3a71cdc0ab6505d61cb4
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index 368ae4f..96c4579 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -1904,6 +1904,43 @@
     return 0;
 }
 
+static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
+{
+    char status[64];
+    curr_status_info curr_state;
+
+    CHECK_WPA_CTRL(CTRL_AP);
+
+    memset(status, 0, sizeof (status));
+
+    curr_state.ap = NULL;
+    curr_state.state = status;
+
+    printf("inner_check_ap_connected %d\n", retry_count);
+    usleep(500*1000);
+    if (0 == inner_get_status_info(idx, &curr_state))
+    {
+        if (strcmp(status, STATE_COMPLETED) == 0)
+        {
+            return 0;
+        }
+        else if (retry_count == 4) //not ok in 2s, do reconnect
+        {
+            DO_REQUEST("RECONNECT");
+            return inner_check_ap_connected(idx, retry_count+1);
+        }
+        else if (retry_count > 10)
+        {
+            printf("retry 10 time\n");
+            return -1;
+        }
+        else
+        {
+            return inner_check_ap_connected(idx, retry_count+1);
+        }
+    }
+    return -1;
+}
 
 int lynq_wifi_ap_start(lynq_wifi_index_e idx)
 {
@@ -1934,6 +1971,11 @@
         return -1;
     }
 
+    if (inner_check_ap_connected(idx, 0) != 0)
+    {
+        return -1;
+    }
+
     check_tether_and_notify();
 
     return 0;
@@ -2702,7 +2744,7 @@
     return inner_get_network_auth(CTRL_STA, net_no, auth);
 }
 
-int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
+int lynq_wifi_sta_connect_timeout(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
 {
     int count, net_no, index;
     int net_no_list[128];
@@ -2809,6 +2851,7 @@
 
 
     DO_OK_FAIL_REQUEST(cmd_disconnect);
+    system("echo \"\" > /tmp/dhcpcd.log");
     usleep(200*1000);
 
     ret = inner_sta_start_stop(net_no, 1, 1);
@@ -2818,13 +2861,18 @@
     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_sec = now.tv_sec + timeout;
     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_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
+{
+    return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
+}
+
 int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
 {
     ap_info_s ap;
@@ -2958,21 +3006,22 @@
 
     CHECK_WPA_CTRL(CTRL_AP);
 
-//    ap_info_s * tmp_ap;
-//    device_info_s * tmp_list;
+    //    ap_info_s * tmp_ap;
+    //    device_info_s * tmp_list;
     if (ap == NULL || list == NULL || len == NULL)
     {
         RLOGE("bad input param");
         return -1;
     }
 
-//    ap = &tmp_ap;
-//    list = &tmp_list;
+    //    ap = &tmp_ap;
+    //    list = &tmp_list;
     *ap = malloc(sizeof (ap_info_s));
+    memset(*ap, 0, sizeof (ap_info_s));
 
-    if (inner_get_status_info_ap (CTRL_AP, *ap) != 0)
+    if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
     {
-	RLOGE("inner_get_status_info_ap !=0\n");
+        RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
         return -1;
     }
 
@@ -2985,7 +3034,7 @@
     while (reply_len > 0)
     {
         if (memcmp(cmd_reply, "FAIL", 4) == 0)
-	{
+        {
             break;
         }
         line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
@@ -2997,7 +3046,7 @@
         cmd_reply[0] = '\0';
         ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
         if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
-	{
+        {
             RLOGD("run %s fail \n", lynq_next_sta_cmd);
             break;
         }
@@ -3619,6 +3668,8 @@
 
 int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
 {
+    int ret;
+    char *p;
     char bssid[1024] = {0};
 
     if (ip == NULL)
@@ -3634,7 +3685,33 @@
         return -1;
     }
 
-    return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
+    ip[0] = '\0';
+    ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
+    if (ret != 0)
+    {
+        RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
+        return -1;
+    }
+
+    if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
+    {
+        ip[0] = '\0';
+        ret = exec_cmd("grep \"offered\" /tmp/dhcpcd.log | awk -F \"from\" '{print $2}'| tail -1", ip, 32);
+        if (ret != 0)
+        {
+            ip[0] = '\0';
+            return 0;
+        }
+        else
+        {
+            p = strchr(ip, '\n');
+            if (p != NULL)
+            {
+                *p = '\0';
+            }
+        }
+    }
+    return 0;
 }
 
 int lynq_ap_connect_num(int sta_number)