[Bugfix][API-779][API-714][API-703][API-702][API-701][API-605][API-791][API-787][API-792] fix ssid encode releated problems & multi thread call problems

Change-Id: I2cd2d3942320a1e40b15b43dd3a33661ceebcb17
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index cabc0a2..d0c3c32 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -60,7 +60,14 @@
 const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
 const char * STATE_COMPLETED = "COMPLETED";
 
-static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
+struct local_wpa_ctrl{
+    struct wpa_ctrl *ctrl;
+    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};
 
 //you.chen add for tv-box start
 volatile int g_gbw_enabled = 0;
@@ -76,6 +83,50 @@
     int net_no;
 }curr_status_info;
 
+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))
+{
+    int ret;
+    if (ctrl->ctrl == NULL) {
+        printf("local_wpa_ctrl_request ctrl is null\n");
+        return -1;
+    }
+    pthread_mutex_lock(&ctrl->mutex);
+    ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
+    pthread_mutex_unlock(&ctrl->mutex);
+    return ret;
+}
+
+static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
+    int repeat_cnt;
+    struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
+    pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
+    printf("inner_get_wpa_ctrl\n");
+    for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
+        pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
+//        printf("wait enable finish\n");
+        usleep(500 * 1000);
+        pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
+    }
+    if (NULL == g_lynq_wpa_ctrl[index]) {
+        printf("NULL == g_lynq_wpa_ctrl[index]\n");
+        goto out_addr;
+    }
+    if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
+        g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
+        if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
+            printf("wpa_ctrl_open fail\n");
+            goto out_addr;
+        }
+        pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
+    }
+    lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
+out_addr:
+    pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
+    return lynq_wpa_ctrl;
+}
+
 #define PRINT_AND_RETURN_VALUE(str,value) \
 {\
     perror((str));\
@@ -93,23 +144,17 @@
 #define CHECK_WPA_CTRL(index) int ret = 0;\
     size_t reply_len = MAX_RET; \
     char cmd_reply[MAX_RET]={0}; \
-    struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
+    struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
     do{ \
-        if (NULL == g_lynq_wpa_ctrl[index]) { \
-            g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
-            if (NULL == g_lynq_wpa_ctrl[index]) { \
-                printf("wpa_ctrl_open fail\n"); \
-                return -1; \
-            } \
-        } \
-        lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
+        lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
+        if (NULL == lynq_wpa_ctrl) return -1; \
     }while(0)
 
 #define DO_REQUEST(cmd_str) do { \
         reply_len = MAX_RET;\
         cmd_reply[0] = '\0'; \
         printf("to call [%s]\n", cmd_str);  \
-        ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
+        ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
         if (ret != 0) { \
             printf("call "#cmd_str" fail %d\n", ret); \
             return ret; \
@@ -130,6 +175,7 @@
     }while (0)
 
 
+static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
 
 static void APWatcherThreadProc() {
     size_t len = MAX_RET;
@@ -264,6 +310,13 @@
 {
     int ret = 0;
     int i;
+    pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
+
+    if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
+        goto out_enable;
+    }
+
+    printf("lynq_wifi_enable1\n");
     const char * cmd_check_service =
             "state=`systemctl is-active wg870_drv_insmod.service`\n"
             "[ \"\"$state == \"active\" ] && exit 0\n"
@@ -275,7 +328,8 @@
     ret = system(cmd_check_service);
     if (ret != 0) {
         printf("service state %d\n", ret);
-        return -1;
+        ret = -1;
+        goto out_enable;
     }
 
     for (i=0; i<10; i++) {
@@ -286,7 +340,8 @@
     }
 
     if (i >= 10) {
-        return -1;
+        ret = -1;
+        goto out_enable;
     }
 
     //@todo delete add temp check for socket avilable start (20220606)
@@ -300,7 +355,8 @@
 
     if (i >= 60)
     {
-        return -1;
+        ret  = -1;
+        goto out_enable;
     }
     //@todo delete add temp check for socket avilable end (20220606)
 
@@ -318,14 +374,16 @@
     if (g_ap_watcher_pid == 0 ) {
         ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
         if(ret<0){
-            return -1;
+            ret = -1;
+            goto out_enable;
         }
     }
 
     if (g_sta_watcher_pid == 0 ) {
         ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
         if(ret<0){
-            return -1;
+            ret = -1;
+            goto out_enable;
         }
     }
 
@@ -336,11 +394,18 @@
         }
     }
 
+    g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
+    g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
+    memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
+    memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
+out_enable:
+    pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
     return ret;
 }
 
 int lynq_wifi_disable(void)
 {
+    pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
     g_ap_watcher_stop_flag = 1;
     g_sta_watcher_stop_flag = 1;
     if (g_ap_watcher_pid != 0)
@@ -356,11 +421,95 @@
     g_lynq_wpa_ctrl[0] = NULL;
     g_lynq_wpa_ctrl[1] = NULL;
     system("systemctl stop wg870_drv_insmod.service");
-	return 0;
+    pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
+    return 0;
+}
+
+static inline char inner_convert_char(char in)
+{
+    if (in >= '0' && in <= '9')
+    {
+        return in - '0';
+    }
+    else if (in >= 'a' && in <= 'f')
+    {
+        return in - 'a' + 10;
+    }
+    else if (in >= 'A' && in <= 'F')
+    {
+        return in - 'A' + 10;
+    }
+    else
+    {
+        return '\xff';
+    }
+}
+
+static inline void inner_copy_ssid(char * out_ssid, const char * ssid,  size_t out_ssid_len)
+{
+    char *p;
+    size_t pos = 0;
+    if (NULL == out_ssid)
+        return;
+    //printf("input ssid=[%s]\n", ssid);
+    memset(out_ssid, 0, out_ssid_len);
+    if (NULL == ssid)
+        return;
+    p = strchr(ssid, '\\');
+    if (NULL == p)
+    {
+        strncpy(out_ssid, ssid, out_ssid_len);
+        //printf(" first %s\n", out_ssid);
+    }
+    else
+    {
+        pos = p - ssid;
+        memcpy(out_ssid, ssid, pos);
+        //printf("pos %lu -- %s\n", pos, out_ssid);
+        for(; pos < out_ssid_len; pos ++)
+        {
+            if (p[0] == '\0')
+            {
+                //printf(" out %s\n", out_ssid);
+                return;
+            }
+            else if (p[0] != '\\')
+            {
+                out_ssid[pos] = p[0];
+                p += 1;
+            }
+            else if (p[1] == 'x' || p[1] == 'X')
+            {
+                out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
+                p += 4;
+            }
+            else if (p[1] == '\\')
+            {
+                out_ssid[pos] = '\\';
+                p += 2;
+            }
+            else if (p[1] == 't')
+            {
+                out_ssid[pos] = '\t';
+                p += 2;
+            }
+            else if (p[1] == 'r')
+            {
+                out_ssid[pos] = '\r';
+                p += 2;
+            }
+            else if (p[1] == 'n')
+            {
+                out_ssid[pos] = '\n';
+                p += 2;
+            }//todo find a better way to convert?
+        }
+    }
+    //printf(" out %s\n", out_ssid);
 }
 
 static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
-
+    int i, ssid_len;
     char lynq_cmd_get[128]={0};
 
     if (out_put == NULL) {
@@ -387,7 +536,33 @@
     }
 
 //    printf("reply len %d, %08x\n", reply_len, (int)out_put);
-    memcpy(out_put, cmd_reply, reply_len + 1);
+    if (strcmp(param_name, "ssid") == 0)
+    {
+        if (cmd_reply[0] == '\"') {
+            ssid_len = reply_len - 1;
+            memcpy(out_put, cmd_reply + 1, ssid_len);
+            if (out_put[ssid_len-1] == '\"')
+            {
+                out_put[ssid_len-1] = '\0';
+            }
+            else
+            {
+                out_put[ssid_len] = '\0';
+            }
+        }
+        else{
+            ssid_len = reply_len / 2;
+            for(i=0; i<ssid_len; i++)
+            {
+                out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
+            }
+            out_put[ssid_len] = '\0';
+        }
+    }
+    else
+    {
+        memcpy(out_put, cmd_reply, reply_len + 1);
+    }
     return 0;
 }
 
@@ -410,123 +585,26 @@
     return ret;
 }
 
-static void trim_space(char * p, int count) {
-    char * begin = p;
-    p += count;
-    printf("%C-%C||\n", *begin, *p);
-    while (p >= begin ) {
-        if (*p == ' ') {
-            *p-- = '\0';
-        }
-        else {
-            break;
-        }
-    }
-}
-
-static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
-    FILE * fp;
-    int len, ret;
-    int count, count_words, index;
-    int mac_start, mac_end;
-    int ip_start, ip_end;
-    char *split_lines[128] = {0};
-    char *buff;
-    const char * ip_header = "IP address";
-    const char * mac_header = "HW address";
-    const char * zero_mac = "00:00:00:00:00:00";
-
-    fp = fopen("/proc/net/arp", "rb");
-    if (NULL == fp) {
-        printf("open file fail\n");
-        return  -1;
-    }
-
-    buff = alloca(MAX_RET);
-    fseek(fp, 0, SEEK_SET);
-    len = fread(buff, 1, MAX_RET, fp);
-    fclose(fp);
-    if (len <= 0) {
-        printf("read file fail\n");
+static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
+{
+    char * p;
+    int ret = 0;
+    char cmd[256]={0};
+    if (NULL == mac || NULL == ip)
         return -1;
+    memset(ip, 0, ip_len);
+    sprintf(cmd, "ip neigh | grep \"lladdr\" | grep \"tether\" | grep \"%s\" | head -1 | awk '{print $1}'", mac);
+    ret = exec_cmd(cmd, ip, ip_len);
+    p = strchr(ip, '\n');
+    if (NULL != p)
+    {
+        *p = '\0';
     }
-    printf("file : %s\n", buff);
-
-    count = lynq_split(buff, len, '\n', split_lines);
-    printf("----- %s\n", split_lines[0]);
-
-    mac_end = 0;
-    count_words = strlen(split_lines[0]);
-    if (strstr(split_lines[0], mac_header) != NULL) {
-        mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
-        mac_end = mac_start + strlen(mac_header) + 1;
-        while (mac_end < count_words) {
-            if (split_lines[0][mac_end] != ' ') {
-                break;
-            }
-            mac_end++;
-        }
-    }
-
-    ip_end = 0;
-    if (strstr(split_lines[0], ip_header) != NULL) {
-        ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
-        ip_end = ip_start + strlen(ip_header) + 1;
-        while (ip_end < count_words) {
-            if (split_lines[0][ip_end] != ' ') {
-                break;
-            }
-            ip_end++;
-        }
-    }
-
-    if (mac_end == 0 || ip_end == 0) {
-        return 0;
-    }
-
-    ret = 0;
-    for(index = 1;index < count; index++) {
-        if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
-            continue;
-        }
-        mac_list[ret] = malloc(mac_end - mac_start + 1);
-        ip_list[ret] = malloc(ip_end - ip_start + 1);
-        memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
-        memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
-        trim_space(mac_list[ret],  mac_end - mac_start - 1);
-        trim_space(ip_list[ret],  ip_end - ip_start - 1);
-        ret++;
-    }
-
+    printf("inner_get_ip_by_mac %s\n", ip);
     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) {
+static int inner_get_hostname_by_ip(char *ip, char *hostname) {
     struct in_addr addr ={0};
     struct hostent *ht;
 
@@ -534,6 +612,7 @@
         return -1;
     }
 
+    *hostname = '\0';
     if (inet_aton(ip, &addr) == 0) {
         printf("---inet_aton fail\n");
         return -1;
@@ -557,6 +636,7 @@
     int count, index, words_count;
     char * split_lines[128]= {0};
     char * split_words[128] = {0};
+    char local_ssid[128] = {0};
     const char *lynq_wifi_list_networks = "LIST_NETWORKS";
 
     CHECK_WPA_CTRL(ap_sta);
@@ -571,7 +651,8 @@
     for(index=1; index < count; index++) {
         words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
         if (words_count > 2) {
-            if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
+            inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
+            if (ssid == NULL || strcmp(local_ssid, ssid) == 0) {
                 net_no_list[ret++] = atoi(split_words[0]);
             }
         }
@@ -677,6 +758,9 @@
         else if (strstr( flag, "NONE") != NULL) {
             return LYNQ_WIFI_AUTH_OPEN;
         }
+        else if (strcmp( flag, "[ESS]") == 0) {
+            return LYNQ_WIFI_AUTH_OPEN;
+        }
     }
 
     return -1;
@@ -713,6 +797,7 @@
     const char * FLAG_FREQ = "freq=";
     const char * FLAG_STATE = "wpa_state=";
     const char * FLAG_ID = "id=";
+    const char * FLAG_IPADDR = "ip_address=";
     char *split_lines[128] = {0};
 
     CHECK_WPA_CTRL(interface);
@@ -731,13 +816,13 @@
         if (curr_state->ap != NULL) {
             p = strstr(split_lines[i], FLAG_SBSID);
             if (p != NULL) {
-                strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
+                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) {
-                strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
+                inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid));
                 ret = 0;
                 continue;
             }
@@ -754,6 +839,12 @@
                 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);
@@ -806,19 +897,8 @@
 
 int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
 {
-    int len;
     CHECK_IDX(idx, CTRL_AP);
-    if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
-        return -1;
-    len = strlen(ap_ssid);
-    if (ap_ssid[0] == '\"') {
-        memmove(ap_ssid, ap_ssid + 1, len - 1);
-        len -= 1;
-    }
-    if (len > 0 && ap_ssid[len-1] == '\"') {
-        ap_ssid[len-1] = '\0';
-    }
-    return 0;
+    return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
 }
 
 /*****
@@ -1431,10 +1511,11 @@
 int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
 
     FILE * fp;
-    int len, ret, network_len;
+    int len, ret, network_len, i, ssid_len;
     int count, index;
     char *split_lines[128] = {0};
-    char *buff, *p;
+    char *buff, *p, *ssid, *ssid_end_flag;
+    char tmp_ssid[128]={0};
 
     network_len = 0;
     p = NULL;
@@ -1474,9 +1555,28 @@
              break;
         }
 
-        if (strstr(p, ap->ap_ssid) != NULL) {
-            break;
+        ssid = strstr(p, "ssid=");
+        if (ssid != NULL) {
+            ssid += strlen("ssid=");
+            if (ssid[0] == '\"') {
+                if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
+                    break;
+            }
+            else{
+                ssid_end_flag = strstr(ssid, "\n");
+                if (ssid_end_flag != NULL)
+                {
+                    ssid_len = (ssid_end_flag - ssid) / 2;
+                    for(i=0; i<ssid_len; i++)
+                    {
+                        tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
+                    }
+                    if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
+                        break;
+                }
+            }
         }
+
     }
 
     if (index >= len || NULL == p || network_len <= 0) {
@@ -1583,7 +1683,7 @@
     curr_state.state = NULL;
 
     if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
-        strcpy(sta_ssid, ap_info.ap_ssid);
+        strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
         return 0;
     }
 
@@ -1668,6 +1768,7 @@
                     best_scan_index = j;
                     best_rssi = scan_list[j].rssi;
                 }
+                strncpy(save_list[i].base_info.ap_mac, scan_list[j].mac, sizeof (save_list[i].base_info.ap_mac));
                 break;
             }
         }
@@ -1675,6 +1776,7 @@
 
     if (best_index >= 0) {
         memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
+        inner_get_ip_by_mac( info->base_info.ap_mac, info->base_info.ap_ip, sizeof (info->base_info.ap_ip));
         info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
         info->rssi = best_rssi;
         ret = 0;
@@ -1938,12 +2040,11 @@
 
 int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
 {
-    int ip_count, index, i, line_count;
+    int index, line_count;
+    device_info_s *dev_info;
     const char *lynq_first_sta_cmd = "STA-FIRST";
     char lynq_next_sta_cmd[MAX_CMD];
     char *bssid[1024] = {0};
-    char *mac_list[128] = {0};
-    char *ip_list[128] = {0};
     char *split_lines[128] = {0};
 
     CHECK_IDX(idx, CTRL_AP);
@@ -1968,9 +2069,6 @@
     lynq_get_interface_ip(idx, (*ap)->ap_ip);
     lynq_ap_password_get(idx, (*ap)->psw);
 
-    ip_count = get_ip_mac_list(mac_list, ip_list);
-    printf("get count %d\n", ip_count);
-
     DO_REQUEST(lynq_first_sta_cmd);
 
     index = 0;
@@ -1985,7 +2083,7 @@
         sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
         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);
+        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) {
             printf("run %s fail \n", lynq_next_sta_cmd);
             break;
@@ -1996,20 +2094,15 @@
 
     *list = malloc(sizeof(device_info_s) * (*len));
     for (index=0; index < *len; index++) {
-        strcpy((*list)[index].sta_mac, bssid[index]);
-        for(i=0;i < ip_count; i++ ) {
-            if (strcmp(bssid[index], mac_list[i]) == 0) {
-                strcpy((*list)[index].sta_ip, ip_list[i]);
-                break;
-            }
-        }
-        get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
-        (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
+        dev_info = &(*list)[index];
+        memset(dev_info, 0, sizeof(device_info_s));
+        strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
+        inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
+        inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
+        dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
         free(bssid[index]);
     }
 
-    free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
-
     return 0;
 }
 
@@ -2057,7 +2150,7 @@
         p->band = convert_band_from_freq(atoi(split_words[1]));
         p->rssi = -1 * atoi( split_words[2]);
         p->auth = convert_max_auth_from_flag(split_words[3]);
-        strcpy(p->ssid, split_words[4]);
+        inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
     }
 
     return 0;
@@ -2104,10 +2197,9 @@
 
 int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
 {    
-    int count, index, ssid_len;
+    int count, index;
     int net_no_list[128];
     char freq[16];
-    char *ssid_ptr;
 
     if (list == NULL || len == NULL) {
         printf("bad param\n");
@@ -2128,17 +2220,6 @@
     for (index=0; index < count; index++) {
         printf("to get ssid %d\n", index);
         inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
-
-        ssid_ptr = (*list)[index].base_info.ap_ssid;
-        ssid_len = strlen(ssid_ptr);
-        if (ssid_ptr[0] == '\"') {
-            memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
-            ssid_len -= 1;
-        }
-        if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
-            ssid_ptr[ssid_len - 1] = '\0';
-        }
-
         inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
         inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
         if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
@@ -2276,7 +2357,7 @@
 //        reply_len = MAX_RET;
 //        cmd_reply[0] = '\0';
 //        printf("to call [%s]\n", cmd_str);
-//        ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
+//        ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
 //        if (ret != 0) {
 //            printf("call ##cmd_str fail %d\n", ret);
 //            return ret;
@@ -2518,11 +2599,7 @@
 
 int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
 {
-    int index;
-    int ip_count = 0;
     char bssid[1024] = {0};
-    char *mac_list[128] = {0};
-    char *ip_list[128] = {0};
 
     if (ip == NULL)
     {
@@ -2537,24 +2614,7 @@
         return -1;
     }
 
-    ip_count = get_ip_mac_list(mac_list, ip_list);
-
-    for (index=0; index < ip_count; index++)
-    {
-        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;
+    return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
 }
 
 int lynq_ap_connect_num(int sta_number)