[Bugfix][API-464][API-465][wifi] fix lynq_wifi_get_sta_available_ap & lynq_get_ap_device_list

Change-Id: Id5c32e5df6d4556a2dea158c7113a52bdc7bdbdb
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index 1424065..03fc67a 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -42,7 +42,7 @@
 
 pthread_t g_sta_watcher_pid = 0;
 volatile int g_sta_watcher_stop_flag = 0;
-volatile int g_sta_scan_finish_flag = 0;
+volatile int g_sta_scan_finish_flag = 1;
 volatile int g_sta_watcher_started_flag = 0;
 
 void * g_ap_callback_priv = NULL;
@@ -58,6 +58,7 @@
 const char * cmd_disconnect = "DISCONNECT";
 const char * cmd_remove_all = "REMOVE_NETWORK all";
 const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
+const char * STATE_COMPLETED = "COMPLETED";
 
 static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
 
@@ -480,6 +481,31 @@
     return ret;
 }
 
+static void free_ip_mac_list_mem(char ** mac_list, int mac_cnt, char ** ip_list, int ip_cnt)
+{
+    int i;
+    if (mac_list != NULL && mac_cnt > 0) {
+        for(i = 0; i< mac_cnt; i++)
+        {
+            if (NULL != mac_list[i])
+            {
+                free(mac_list[i]);
+                mac_list[i] = NULL;
+            }
+        }
+    }
+    if (ip_list != NULL && ip_cnt > 0) {
+        for(i = 0; i< mac_cnt; i++)
+        {
+            if (NULL != ip_list[i])
+            {
+                free(ip_list[i]);
+                ip_list[i] = NULL;
+            }
+        }
+    }
+}
+
 static int get_hostname_by_ip(char *ip, char *hostname) {
     struct in_addr addr ={0};
     struct hostent *ht;
@@ -1512,14 +1538,16 @@
 
 int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
 {
-    scan_info_s *scan_list;
-    saved_ap_info_s *save_list;
+    scan_info_s *scan_list = NULL;
+    saved_ap_info_s *save_list = NULL;
     int scan_len=0;
     int save_len=0;
     int best_index = -1;
     int best_scan_index = -1;
     int best_rssi = 0;
-    int i, j;
+    int i, j, ret;
+
+    ret = -1;
 
     CHECK_IDX(idx, CTRL_STA);
     if (info == NULL) {
@@ -1528,21 +1556,48 @@
 
     curr_status_info curr_state;
     ap_info_s ap_info;
-    curr_state.ap = &ap_info;
-    curr_state.state = NULL;
+    char status[64];
 
-    if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
+    memset(&ap_info, 0, sizeof (ap_info));
+    memset(status, 0, sizeof (status));
+
+    curr_state.ap = &ap_info;
+    curr_state.state = status;
+
+    if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
         memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
-        info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
+        if (strcmp(status, STATE_COMPLETED) == 0)
+        {
+            info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
+        }
+        else
+        {
+            info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
+        }
         lynq_get_connect_ap_rssi(idx, &info->rssi);
+        lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
         return 0;
     }
 
+    lynq_wifi_sta_start_scan(idx);
+
     if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
+        if (NULL != scan_list)
+        {
+            free(scan_list);
+        }
         return -1;
     }
 
     if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
+        if (NULL != scan_list)
+        {
+            free(scan_list);
+        }
+        if (NULL != save_list)
+        {
+            free(save_list);
+        }
         return -1;
     }
 
@@ -1551,6 +1606,7 @@
             if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
                     && save_list[i].base_info.auth == scan_list[j].auth) {
                 if (best_rssi == 0) {
+                    best_index = i;
                     best_rssi = scan_list[j].rssi;
                 }
                 else if (best_rssi > scan_list[j].rssi) {
@@ -1567,10 +1623,19 @@
         memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
         info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
         info->rssi = best_rssi;
-        return 0;
+        ret = 0;
     }
 
-    return -1;
+    if (NULL != scan_list)
+    {
+        free(scan_list);
+    }
+    if (NULL != save_list)
+    {
+        free(save_list);
+    }
+
+    return ret;
 }
 
 static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
@@ -1859,7 +1924,8 @@
         reply_len = MAX_RET;
         cmd_reply[0] = '\0';
         ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
-        if (ret != 0 || memcpy(cmd_reply, "FAIL", 4) == 0) {
+        if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
+            printf("run %s fail \n", lynq_next_sta_cmd);
             break;
         }
     }
@@ -1880,6 +1946,8 @@
         free(bssid[index]);
     }
 
+    free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
+
     return 0;
 }
 
@@ -1895,6 +1963,11 @@
         return -1;
     }
 
+    for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
+    {
+        usleep(100 * 1000);
+    }
+
     CHECK_IDX(idx, CTRL_STA);
 
     CHECK_WPA_CTRL(CTRL_STA);
@@ -2028,6 +2101,7 @@
     CHECK_WPA_CTRL(CTRL_STA);
 
     DO_OK_FAIL_REQUEST(lynq_scan_cmd);
+    g_sta_scan_finish_flag = 0;
 
     return 0;
 }
@@ -2083,7 +2157,6 @@
 int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
 {
     char state[MAX_CMD];
-    const char * STATE_COMPLETED = "COMPLETED";
     CHECK_IDX(idx, CTRL_AP);
 
     if (inner_get_status_info_state(CTRL_AP, state) != 0) {
@@ -2103,7 +2176,6 @@
 
 int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
     char state[MAX_CMD];
-    const char * STATE_COMPLETED = "COMPLETED";
     CHECK_IDX(idx, CTRL_STA);
 
     if (inner_get_status_info_state(CTRL_STA, state) != 0) {
@@ -2229,54 +2301,48 @@
 
 int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
 {
-    struct ifaddrs *ifaddr, *ifa;
-   int family, s;
-   char host[NI_MAXHOST];
-   const char * ifaName = "wlan0";
-   if (ip == NULL)
-   {
+    struct ifaddrs *ifaddr_header, *ifaddr;
+    struct in_addr * ifa;
+    const char * ifaName = "wlan0";
+    if (ip == NULL)
+    {
        return -1;
-   }
+    }
 
-   if (idx == 1) {
+    if (idx == 1) {
        ifaName = "tether";
-   }
-   else if (idx != 0) {
+    }
+    else if (idx != 0) {
        return -1;
-   }
+    }
 
-   if (getifaddrs(&ifaddr) == -1)
-   {
+    if (getifaddrs(&ifaddr_header) == -1)
+    {
        perror("getifaddrs");
        return -1;
        //exit(EXIT_FAILURE);
-   }
+    }
 
 
-   for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
-   {
-       if (ifa->ifa_addr == NULL)
+    for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
+    {
+        if (ifaddr->ifa_addr == NULL)
            continue;
-       host[0] = '\0';
-       s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
-
-       printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
-       if((strcmp(ifa->ifa_name,ifaName)==0))
-       {
-           if (s != 0)
-           {
-               //  printf("getnameinfo() failed: %s", gai_strerror(s));
-               //exit(EXIT_FAILURE);
-           }
-           freeifaddrs(ifaddr);
-           strcpy(ip, host);
-           printf("ip %s\n", ip);
-           return 0;
-           //printf("	Interface : <%s>",ifa->ifa_name );
-           //printf("	  Address : <%s>", host);
-       }
-   }
-   return -1;
+        if((strcmp(ifaddr->ifa_name,ifaName)==0))
+        {
+            if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
+            {
+               // is a valid IP4 Address
+               ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
+               inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
+               printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
+               freeifaddrs(ifaddr_header);
+               printf("ip %s\n", ip);
+               return 0;
+            }
+        }
+    }
+    return -1;
 }
 
 int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
@@ -2406,11 +2472,16 @@
         if (strcmp(bssid, mac_list[index]) == 0)
         {
             strcpy(ip, ip_list[index]);
+
+            free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
+
             return 0;
         }
     }
 
     printf("not found\n");
+    free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
+
     return -1;
 }