| /**@File lib_wifi6.c | 
 | *  @Brief :about function test | 
 | *  @details : | 
 | *  @Author : you.chen | 
 | *  @Date : 2022-4-6 | 
 | *  @Version : V1.0 | 
 | *  @copy ritght : Copyright (c) MobileTek | 
 | */ | 
 | #include <log/log.h> | 
 | #include <stdio.h> | 
 | #include <sys/types.h> | 
 | #include <stdlib.h> | 
 | #include <string.h> | 
 | #include "libwifi6.h" | 
 | #include <wpa_ctrl.h> | 
 | #include <string.h> | 
 | #include <time.h> | 
 | #include <pthread.h> | 
 | #include <sys/socket.h> | 
 | #include <netinet/in.h> | 
 | #include <arpa/inet.h> | 
 | #include <netdb.h> | 
 | #include <ifaddrs.h> | 
 |  | 
 | #ifdef __cplusplus | 
 | extern "C" { | 
 | #endif | 
 | #ifdef __cplusplus | 
 | } | 
 | #endif | 
 |  | 
 | #define MAX_CMD 128 | 
 | #define MAX_RET 4096 | 
 | #define MODE_LEN 10 | 
 | #define CTRL_STA 0 | 
 | #define CTRL_AP 1 | 
 | #define AP_NETWORK_0 0 | 
 |  | 
 | pthread_t g_ap_watcher_pid = 0; | 
 | volatile int g_ap_watcher_stop_flag = 0; | 
 | volatile int g_ap_watcher_started_flag = 0; | 
 |  | 
 | pthread_t g_sta_watcher_pid = 0; | 
 | volatile int g_sta_watcher_stop_flag = 0; | 
 | volatile int g_sta_scan_finish_flag = 1; | 
 | volatile int g_sta_watcher_started_flag = 0; | 
 |  | 
 | void * g_ap_callback_priv = NULL; | 
 | AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL; | 
 | void * g_sta_callback_priv = NULL; | 
 | STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL; | 
 |  | 
 | //const char * CTRL_PATH="/var/run/wpa_supplicant"; | 
 | const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"}; | 
 | //const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"}; | 
 | const char * cmd_list_networks = "LIST_NETWORKS"; | 
 | const char * cmd_save_config = "SAVE_CONFIG"; | 
 | 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}; | 
 |  | 
 |  | 
 | typedef struct __curr_status_info { | 
 |     ap_info_s *ap; | 
 |     char * state; | 
 |     int net_no; | 
 | }curr_status_info; | 
 |  | 
 | #define PRINT_AND_RETURN_VALUE(str,value) \ | 
 | {\ | 
 |     perror((str));\ | 
 |     return (value);\ | 
 | } | 
 |  | 
 | #define CHECK_IDX(idx, type) do { \ | 
 |         if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \ | 
 |                 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \ | 
 |             printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \ | 
 |             return -1; \ | 
 |         } \ | 
 |     }while (0) | 
 |  | 
 | #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; \ | 
 |     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]; \ | 
 |     }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); \ | 
 |         if (ret != 0) { \ | 
 |             printf("call "#cmd_str" fail %d\n", ret); \ | 
 |             return ret; \ | 
 |         } \ | 
 |         cmd_reply[reply_len+1] = '\0'; \ | 
 |         printf("cmd replay [ %s ]\n", cmd_reply); \ | 
 |     }while(0) | 
 |  | 
 | #define DO_OK_FAIL_REQUEST(cmd_str) do { \ | 
 |         DO_REQUEST(cmd_str); \ | 
 |         if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\ | 
 |             printf("cmd "#cmd_str" return FAIL\n"); \ | 
 |             return -1; \ | 
 |         } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \ | 
 |             printf("cmd "#cmd_str" return not OK|FAIL\n"); \ | 
 |             return -1; \ | 
 |         } \ | 
 |     }while (0) | 
 |  | 
 |  | 
 |  | 
 | static void APWatcherThreadProc() { | 
 |     size_t len = MAX_RET; | 
 |     char msg_notify[MAX_RET]; | 
 |  | 
 |     struct wpa_ctrl *lynq_wpa_ctrl = NULL; | 
 |     g_ap_watcher_stop_flag = 0; | 
 |  | 
 |     while (g_ap_watcher_stop_flag == 0) { | 
 |         if (lynq_wpa_ctrl == NULL) { | 
 |             lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change | 
 |             if (lynq_wpa_ctrl == NULL) { | 
 |                 usleep(100*1000); | 
 |                 continue; | 
 |             } | 
 |  | 
 |             wpa_ctrl_attach(lynq_wpa_ctrl); | 
 |             g_ap_watcher_started_flag = 1; | 
 |         } | 
 |  | 
 |         if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { | 
 |             usleep(100*1000); | 
 |             continue; | 
 |         } | 
 |         memset(msg_notify, 0, MAX_RET); | 
 |         len = MAX_RET; | 
 |         if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { | 
 |             msg_notify[len+1] = '\0'; | 
 |             printf("ap------> %s\n", msg_notify); | 
 |             if (g_ap_callback_func == NULL) { | 
 |                 continue; | 
 |             } | 
 |             if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) { | 
 |                 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT); | 
 |             } | 
 |             else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) { | 
 |                 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT); | 
 |             } | 
 |         } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) | 
 |     } // end while (g_ap_watcher_stop_flag == 0) | 
 |     if (lynq_wpa_ctrl != NULL) { | 
 |         wpa_ctrl_detach(lynq_wpa_ctrl); | 
 |         wpa_ctrl_close(lynq_wpa_ctrl); | 
 |     } | 
 | } | 
 |  | 
 | static void STAWatcherThreadProc() { | 
 |     size_t len = MAX_RET; | 
 |     char msg_notify[MAX_RET]; | 
 |     char *pReason; | 
 |     error_number_s error; | 
 |  | 
 |     struct wpa_ctrl *lynq_wpa_ctrl = NULL; | 
 |     g_sta_watcher_stop_flag = 0; | 
 |  | 
 |     while (g_sta_watcher_stop_flag == 0) { | 
 |         if (lynq_wpa_ctrl == NULL) { | 
 |             lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]); | 
 |             if (lynq_wpa_ctrl == NULL) { | 
 |                 usleep(100*1000); | 
 |                 continue; | 
 |             } | 
 |  | 
 |             wpa_ctrl_attach(lynq_wpa_ctrl); | 
 |             g_sta_watcher_started_flag = 1; | 
 |         } | 
 |  | 
 |         if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { | 
 |             usleep(100*1000); | 
 |             continue; | 
 |         } | 
 |         memset(msg_notify, 0, MAX_RET); | 
 |         len = MAX_RET; | 
 |         if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { | 
 |             msg_notify[len+1] = '\0'; | 
 |             printf("sta ------> %s\n", msg_notify); | 
 |             if (strstr(msg_notify, state_scan_result) != NULL) { | 
 |                 g_sta_scan_finish_flag = 1; | 
 |             } | 
 |  | 
 |             if (g_sta_callback_func == NULL) { | 
 |                 continue; | 
 |             } | 
 |             error = -1; | 
 |             if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) { | 
 |                 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error); | 
 |             } | 
 |             else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) { | 
 |                 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error); | 
 |             } | 
 |             else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) { | 
 |                 pReason = strstr(msg_notify, "reason="); | 
 |                 if (pReason != NULL) { | 
 |                     pReason += strlen("reason="); | 
 |                     if (memcmp(pReason, "CONN_FAILED", 11) == 0) { | 
 |                         error = LYNQ_TIME_OUT; | 
 |                     } | 
 |                     else if (memcmp(pReason, "WRONG_KEY", 9) == 0) { | 
 |                         error = LYNQ_PSW_ERROR; | 
 |                     } | 
 |                     g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error); | 
 |                 } | 
 |             } | 
 |             else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) { | 
 |                 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error); | 
 |             } | 
 |             else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) { | 
 |                 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP); | 
 |             } | 
 |         } | 
 |     } | 
 |     if (lynq_wpa_ctrl != NULL) { | 
 |         wpa_ctrl_detach(lynq_wpa_ctrl); | 
 |         wpa_ctrl_close(lynq_wpa_ctrl); | 
 |     } | 
 | } | 
 |  | 
 | int lynq_wifi_enable(void) | 
 | { | 
 |     int ret = 0; | 
 |     int i; | 
 |     const char * cmd_check_service = | 
 |             "state=`systemctl is-active wg870_drv_insmod.service`\n" | 
 |             "[ \"\"$state == \"active\" ] && exit 0\n" | 
 |             "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n"; | 
 | //    if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) { | 
 | //        return 0; | 
 | //    } | 
 |  | 
 |     ret = system(cmd_check_service); | 
 |     if (ret != 0) { | 
 |         printf("service state %d\n", ret); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     for (i=0; i<10; i++) { | 
 |         if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) { | 
 |             break; | 
 |         } | 
 |         usleep(300*1000); | 
 |     } | 
 |  | 
 |     if (i >= 10) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     //@todo delete add temp check for socket avilable start (20220606) | 
 |     for (i=0; i<60; i++) | 
 |     { | 
 |         if (system("netstat -an | grep -q DGRAM") == 0) { | 
 |             break; | 
 |         } | 
 |         sleep(1); | 
 |     } | 
 |  | 
 |     if (i >= 60) | 
 |     { | 
 |         return -1; | 
 |     } | 
 |     //@todo delete add temp check for socket avilable end (20220606) | 
 |  | 
 |     if (0 != system("ifconfig | grep -q ap0")) { | 
 |         system("connmanctl enable wifi"); | 
 |         usleep(300*1000); | 
 |         system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect"); | 
 |         system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0"); | 
 |         usleep(300*1000); | 
 |         system("connmanctl tether wifi on lynq 1qaz@WSX#$%^"); | 
 |         usleep(300*1000); | 
 |         system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect"); | 
 |     } | 
 |  | 
 |     if (g_ap_watcher_pid == 0 ) { | 
 |         ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL); | 
 |         if(ret<0){ | 
 |             return -1; | 
 |         } | 
 |     } | 
 |  | 
 |     if (g_sta_watcher_pid == 0 ) { | 
 |         ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL); | 
 |         if(ret<0){ | 
 |             return -1; | 
 |         } | 
 |     } | 
 |  | 
 |     for (i=0; i<10; i++) { | 
 |         usleep(300*1000); | 
 |         if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) { | 
 |             break; | 
 |         } | 
 |     } | 
 |  | 
 |     return ret; | 
 | } | 
 |  | 
 | int lynq_wifi_disable(void) | 
 | { | 
 |     g_ap_watcher_stop_flag = 1; | 
 |     g_sta_watcher_stop_flag = 1; | 
 |     if (g_ap_watcher_pid != 0) | 
 |         pthread_join(g_ap_watcher_pid, NULL); | 
 |     if (g_sta_watcher_pid != 0) | 
 |         pthread_join(g_sta_watcher_pid, NULL); | 
 |     if (g_lynq_wpa_ctrl[0] != NULL) | 
 |         wpa_ctrl_close(g_lynq_wpa_ctrl[0]); | 
 |     if (g_lynq_wpa_ctrl[1] != NULL) | 
 |         wpa_ctrl_close(g_lynq_wpa_ctrl[1]); | 
 |     g_ap_watcher_pid = 0; | 
 |     g_sta_watcher_pid = 0; | 
 |     g_lynq_wpa_ctrl[0] = NULL; | 
 |     g_lynq_wpa_ctrl[1] = NULL; | 
 |     system("systemctl stop wg870_drv_insmod.service"); | 
 | 	return 0; | 
 | } | 
 |  | 
 | static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) { | 
 |  | 
 |     char lynq_cmd_get[128]={0}; | 
 |  | 
 |     if (out_put == NULL) { | 
 |         printf("output ptr is null\n"); | 
 |         return -1; | 
 |     } | 
 |     if (param_name == NULL) { | 
 |         printf("param ptr is null"); | 
 |         return -1; | 
 |     } | 
 |     if (param_name[0] == '\0') { | 
 |         printf("param is empty"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name); | 
 |  | 
 |     CHECK_WPA_CTRL(interface); | 
 |  | 
 |     DO_REQUEST(lynq_cmd_get); | 
 |  | 
 |     if (memcmp(cmd_reply, "FAIL", 4) == 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 | //    printf("reply len %d, %08x\n", reply_len, (int)out_put); | 
 |     memcpy(out_put, cmd_reply, reply_len + 1); | 
 |     return 0; | 
 | } | 
 |  | 
 | static int lynq_split(char * str, int len, char delimiter, char * results[]) { | 
 |     int ret = 0; | 
 |     char * end = str + len - 1; | 
 |     results[ret++] = str; | 
 |     while(str < end) { | 
 |         if (*str == delimiter) { | 
 |             *str++ = '\0'; | 
 |             results[ret++] = str; | 
 |             continue; | 
 |         } | 
 |         str++; | 
 |     } | 
 |     if (*str == delimiter) { | 
 |         *str = '\0'; | 
 |     } | 
 |  | 
 |     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"); | 
 |         return -1; | 
 |     } | 
 |     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++; | 
 |     } | 
 |  | 
 |     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; | 
 |  | 
 |     if (ip == NULL || *ip == '\0' || hostname == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     if (inet_aton(ip, &addr) == 0) { | 
 |         printf("---inet_aton fail\n"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET); | 
 |  | 
 |     if (ht == NULL) { | 
 |         printf("---gethostbyaddr fail\n"); | 
 |         herror(NULL); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     strcpy(hostname, ht->h_name); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid) | 
 | { | 
 |     int count, index, words_count; | 
 |     char * split_lines[128]= {0}; | 
 |     char * split_words[128] = {0}; | 
 |     const char *lynq_wifi_list_networks = "LIST_NETWORKS"; | 
 |  | 
 |     CHECK_WPA_CTRL(ap_sta); | 
 |  | 
 |     DO_REQUEST(lynq_wifi_list_networks); | 
 |  | 
 |     count = lynq_split(cmd_reply, reply_len, '\n', split_lines); | 
 |  | 
 |     //@todo check ssid field to compatible | 
 |  | 
 |     ret = 0; | 
 |     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) { | 
 |                 net_no_list[ret++] = atoi(split_words[0]); | 
 |             } | 
 |         } | 
 |     } | 
 |  | 
 |     return ret; | 
 | } | 
 |  | 
 | static int lynq_add_network(int ap_sta) { | 
 |     size_t i=0; | 
 |     CHECK_WPA_CTRL(ap_sta); | 
 |     const char *lynq_wifi_add_network = "ADD_NETWORK"; | 
 |  | 
 |     DO_REQUEST(lynq_wifi_add_network); | 
 |     if (memcmp(cmd_reply, "FAIL", 4) == 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     for(i=0;i<reply_len;i++) { | 
 |         if(cmd_reply[i] == '\n') { | 
 |             cmd_reply[i] = '\0'; | 
 |             break; | 
 |         } | 
 |     } | 
 |     return atoi(cmd_reply); | 
 | } | 
 |  | 
 | static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no) | 
 | { | 
 |     int count, index; | 
 |     int net_no_list[128]; | 
 |  | 
 |  | 
 |     count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL); | 
 |     for (index=0; index < count; index++) { | 
 |         if (net_no_list[index] == net_no) { | 
 |             return 0; | 
 |         } | 
 |     } | 
 |  | 
 |     if (count >= 1) | 
 |         index = net_no_list[count - 1]; | 
 |     else | 
 |         index = -1; | 
 |  | 
 |     while (index < net_no ) { | 
 |         index = lynq_add_network(ap_sta); | 
 |         if (index >= net_no) { // required network no created | 
 |             return 0; | 
 |         } | 
 |         else if( index < 0) { | 
 |             printf("add network fail\n"); | 
 |             return -1; | 
 |         } | 
 |     } | 
 |  | 
 |     if (index < 0) | 
 |         return -1; | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo | 
 |     if (freq > 5000 && freq < 6000) { | 
 |         return LYNQ_WIFI_5G_band; | 
 |     } | 
 |     else if (freq > 2000 && freq <  3000) { | 
 |         return LYNQ_WIFI_2G_band; | 
 |     } | 
 |     return LYNQ_WIFI_2_and_5G_band; | 
 | } | 
 |  | 
 | static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) { | 
 |     if (key_mgmt != NULL) { | 
 |         if (memcmp( key_mgmt, "NONE", 4) == 0) { | 
 |             return LYNQ_WIFI_AUTH_OPEN; | 
 |         } | 
 |         else if (memcmp( key_mgmt, "WEP", 3) == 0){ | 
 |             return LYNQ_WIFI_AUTH_WEP; | 
 |         } | 
 |         else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){ | 
 |             return LYNQ_WIFI_AUTH_WPA_PSK; | 
 |         } | 
 |         else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){ | 
 |             return LYNQ_WIFI_AUTH_WPA2_PSK; | 
 |         } | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 | static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) { | 
 |     if (flag != NULL) { | 
 |         if (strstr( flag, "WPA2-PSK") != NULL){ | 
 |             return LYNQ_WIFI_AUTH_WPA2_PSK; | 
 |         } | 
 |         else if (strstr( flag, "WPA-PSK") != NULL){ | 
 |             return LYNQ_WIFI_AUTH_WPA_PSK; | 
 |         } | 
 |         else if (strstr( flag, "WEP") != NULL){ | 
 |             return LYNQ_WIFI_AUTH_WEP; | 
 |         } | 
 |         else if (strstr( flag, "NONE") != NULL) { | 
 |             return LYNQ_WIFI_AUTH_OPEN; | 
 |         } | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 | static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) { | 
 |     switch (bw) { | 
 |     case 10: | 
 |         return LYNQ_WIFI_BANDWIDTH_HT10; | 
 |         break; | 
 |     case 20: | 
 |         return LYNQ_WIFI_BANDWIDTH_HT20; | 
 |         break; | 
 |     case 40: | 
 |         return LYNQ_WIFI_BANDWIDTH_HT40; | 
 |         break; | 
 |     case 80: | 
 |         return LYNQ_WIFI_BANDWIDTH_HT80; | 
 |         break; | 
 |     default: | 
 |         break; | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 | static int inner_get_status_info(int interface, curr_status_info *curr_state) { | 
 |     int i, count; | 
 |     char *p; | 
 |     const char *lynq_status_cmd = "STATUS"; | 
 |     const char * FLAG_SSID = "ssid="; | 
 |     const char * FLAG_SBSID = "bssid="; | 
 |     const char * FLAG_KEY_MGMT = "key_mgmt="; | 
 |     const char * FLAG_FREQ = "freq="; | 
 |     const char * FLAG_STATE = "wpa_state="; | 
 |     const char * FLAG_ID = "id="; | 
 |     char *split_lines[128] = {0}; | 
 |  | 
 |     CHECK_WPA_CTRL(interface); | 
 |  | 
 |     if (curr_state == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     DO_REQUEST(lynq_status_cmd); | 
 |  | 
 |     count = lynq_split(cmd_reply, reply_len, '\n', split_lines); | 
 |  | 
 |     curr_state->net_no = -1; | 
 |     ret = -1; | 
 |     for(i=0; i < count; i++) { | 
 |         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)); | 
 |                 ret = 0; | 
 |                 continue; | 
 |             } | 
 |             p = strstr(split_lines[i], FLAG_SSID); | 
 |             if (p != NULL) { | 
 |                 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_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)); | 
 |                 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p); | 
 |                 ret = 0; | 
 |                 continue; | 
 |             } | 
 |             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; | 
 |             } | 
 |         } // 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; | 
 |             } | 
 |  | 
 |         } //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)); | 
 |             printf("net_no %d, -- %s\n", curr_state->net_no, p); | 
 |         } | 
 |     } | 
 |  | 
 |     return ret; | 
 | } | 
 |  | 
 |  | 
 | int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid) | 
 | { | 
 |     char lynq_wifi_ssid_cmd[80]={0}; | 
 |  | 
 |     if (ap_ssid == NULL) { | 
 |         printf("ap_ssid is null\n"); | 
 |         return -1; | 
 |     } | 
 |     else { | 
 |         printf("idx:%d ap_ssid : %s\n", idx, ap_ssid); | 
 |     } | 
 |  | 
 |     if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd); | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 | 	return 0; | 
 |  | 
 | } | 
 |  | 
 | 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; | 
 | } | 
 |  | 
 | /***** | 
 |  *frequency  <------>channel | 
 |  * | 
 |  *frequency   1    2    3    4    5    6    7    8    9   10   11   12   13   36   40   44   48   149  153  157  161  165 | 
 |  * | 
 |  * | 
 |  *channel     2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825 | 
 |  * | 
 |  * | 
 |  * */  | 
 | static int lynq_check_set_frequency(int input_frequency){ | 
 |     static int legitimate_frequency[21]={2412,2417,2422,2427,2432,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825}; | 
 |     int i = 0; | 
 |     int flag_check = 0; | 
 |     for(i=0 ;i<= 21; i++){ | 
 |         if(input_frequency == legitimate_frequency[i]){ | 
 |             flag_check = 1; | 
 |             break; | 
 |         } | 
 |     } | 
 |     if(flag_check == 1){ | 
 |         printf("input frequency in range\n"); | 
 |     }else{ | 
 |         printf("input frequency is eero--->%d,please check it\n",input_frequency); | 
 |         return -1; | 
 |     } | 
 |     return 0; | 
 | } | 
 | int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency) | 
 | { | 
 |     char lynq_wifi_frequency_cmd[128]={0}; | 
 |     char lynq_cmd_mode[128]={0}; | 
 |     char lynq_cmd_slect[128]={0}; | 
 |  | 
 |     //@do check input frequency | 
 |     if((lynq_check_set_frequency(lynq_wifi_frequency)) != 0){ | 
 |         return -1; | 
 |     } | 
 |     if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency); | 
 |     sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); | 
 |     sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(cmd_disconnect); | 
 |     DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_cmd_mode); | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency) | 
 | { | 
 |     char lynq_frequency_str[MAX_RET] = {0}; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) { | 
 |         return -1; | 
 |     } | 
 |     *lynq_wifi_frequency = atoi(lynq_frequency_str); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth) | 
 | { | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |     switch(bandwidth){ | 
 | 		case LYNQ_WIFI_BANDWIDTH_HT10: | 
 | 		{ | 
 |             printf("bandwith  [%d] not support now\n", bandwidth); | 
 | 			return -1; | 
 | 		} | 
 | 		case LYNQ_WIFI_BANDWIDTH_HT20: | 
 |         { | 
 |             char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6"; | 
 |             system("wl down"); | 
 |             if (system(lynq_cmd_bandwith) != 0 ){ | 
 |                 return -1; | 
 |             } | 
 |             system("wl up"); | 
 |             break; | 
 |         } | 
 |         case LYNQ_WIFI_BANDWIDTH_HT40: | 
 | 		{ | 
 |             char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u"; | 
 |             sprintf(lynq_cmd_bandwith, "wl chanspec "); | 
 |             system("wl down"); | 
 |             if (system(lynq_cmd_bandwith) != 0 ){ | 
 |                 return -1; | 
 |             } | 
 |             system("wl up"); | 
 |             break; | 
 | 		} | 
 |         case LYNQ_WIFI_BANDWIDTH_HT80: | 
 | 		{ | 
 |             char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80"; | 
 |             system("wl down"); | 
 |             if (system(lynq_cmd_bandwith) != 0 ){ | 
 |                 return -1; | 
 |             } | 
 |             system("wl up"); | 
 |             break; | 
 | 		}				 | 
 | 		default: | 
 |         { | 
 |             printf("auth type [%d] not support now\n", bandwidth); | 
 | 			return -1; | 
 |         } | 
 |     } | 
 |  | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth) | 
 | { | 
 |     int count = 0; | 
 |     int index = 0; | 
 |     char *split_words[128] = {0}; | 
 |     const char *lynq_chanspec_cmd = "DRIVER chanspec\n"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     DO_REQUEST(lynq_chanspec_cmd); | 
 |  | 
 |     count = lynq_split(cmd_reply, reply_len, ' ', split_words); | 
 |     for(;index < count; index++) { | 
 |         if (strncmp(split_words[index], "bw", 2) != 0) { | 
 |             continue; | 
 |         } | 
 |  | 
 |         index++; | 
 |         if (index >= count) { | 
 |             return -1; | 
 |         } | 
 |  | 
 |         printf("bw %s\n", split_words[index]); | 
 |         *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index])); | 
 |         return 0; | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel) | 
 | { | 
 |     char lynq_cmd_channel[MAX_CMD]={0}; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     sprintf(lynq_cmd_channel, "wl channel %d", channel); | 
 |  | 
 |     if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     system("wl down"); | 
 |     if (system(lynq_cmd_channel) != 0 ){ | 
 |         return -1; | 
 |     } | 
 |     system("wl up"); | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel) | 
 | { | 
 |     int count = 0; | 
 |     int index = 0; | 
 |     char *split_words[128] = {0}; | 
 |     char lynq_chanspec_cmd[]="DRIVER chanspec\n"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     DO_REQUEST(lynq_chanspec_cmd); | 
 |  | 
 |     count = lynq_split(cmd_reply, reply_len, ' ', split_words); | 
 |     for(;index < count; index++) { | 
 |         printf("---- %s\n",split_words[index]); | 
 |         if (strncmp(split_words[index], "channel", 2) != 0) { | 
 |             continue; | 
 |         } | 
 |  | 
 |         index++; | 
 |         if (index >= count) { | 
 |             return -1; | 
 |         } | 
 |  | 
 |         *channel = atoi(split_words[index]); | 
 |         return 0; | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 |  | 
 | int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth) | 
 | { | 
 |     char ssid[MAX_CMD] = {0}; | 
 |     int freq = 0; | 
 |     char lynq_auth_cmd[64]={0}; | 
 |     char lynq_auth_alg_cmd[64]={0}; | 
 |     char lynq_psk_cmd[64]={0}; | 
 |     char lynq_pairwise_cmd[64]={0}; | 
 |     lynq_wifi_auth_s org_auth; | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) { | 
 |         if (org_auth == auth) { | 
 |             return 0; | 
 |         } | 
 |         else { | 
 |             if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) { | 
 |                 ssid[0] = '\0'; | 
 |             } | 
 |             lynq_wifi_ap_frequency_get(idx, &freq); | 
 |  | 
 |             DO_OK_FAIL_REQUEST(cmd_disconnect); | 
 |             DO_OK_FAIL_REQUEST(cmd_remove_all); | 
 |             if (ssid[0] != '\0') { | 
 |                 lynq_wifi_ap_ssid_set(idx, ssid); | 
 |             } | 
 |             if (freq != 0) { | 
 |                 lynq_wifi_ap_frequency_set(idx, freq); | 
 |             } | 
 |         } | 
 |     } | 
 |  | 
 | 	switch(auth){ | 
 | 		case LYNQ_WIFI_AUTH_OPEN: | 
 |         { | 
 |             sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); | 
 |             sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); | 
 |  | 
 |             DO_OK_FAIL_REQUEST(lynq_auth_cmd); | 
 | 			break; | 
 | 		} | 
 |         case LYNQ_WIFI_AUTH_WEP: | 
 |         { | 
 |             sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); | 
 |             sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); | 
 |             sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg  SHARED", AP_NETWORK_0); | 
 |  | 
 |             DO_OK_FAIL_REQUEST(lynq_auth_cmd); | 
 |             DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd); | 
 |             break; | 
 |         } | 
 | 		case LYNQ_WIFI_AUTH_WPA_PSK: | 
 |         case LYNQ_WIFI_AUTH_WPA2_PSK: | 
 | 		{ | 
 |             if (auth == LYNQ_WIFI_AUTH_WPA_PSK) { | 
 |                 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0); | 
 |                 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); | 
 |             } | 
 |             else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) { | 
 |                 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0); | 
 |                 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); | 
 |             } | 
 | //            sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0); | 
 | //            sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); | 
 |             sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0); | 
 |  | 
 |             DO_OK_FAIL_REQUEST(lynq_auth_cmd); | 
 |             DO_OK_FAIL_REQUEST(lynq_psk_cmd); | 
 |             DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); | 
 | 			break; | 
 | 		}		 | 
 | 		default: | 
 |         { | 
 |             printf("auth type [%d] not support now\n", auth); | 
 | 			return -1; | 
 |         } | 
 |     } | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) | 
 | { | 
 |     char lynq_auth_str[MAX_RET] = {0}; | 
 |     char lynq_auth_alg_str[MAX_RET] = {0}; | 
 |     char lynq_proto_str[MAX_RET] = {0}; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     if (memcmp( lynq_auth_str, "NONE", 4) == 0) { | 
 |         if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) { | 
 |             *auth = LYNQ_WIFI_AUTH_OPEN; | 
 |         } | 
 |         else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){ | 
 |             *auth = LYNQ_WIFI_AUTH_WEP; | 
 |         } | 
 |         else { | 
 |             *auth = LYNQ_WIFI_AUTH_OPEN; | 
 |         } | 
 |     } | 
 |     else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) { | 
 |         if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) { | 
 |             *auth = -1; | 
 |         } | 
 |         else if (memcmp(lynq_proto_str, "RSN", 3) == 0) { | 
 |             *auth = LYNQ_WIFI_AUTH_WPA2_PSK; | 
 |         } | 
 |         else { | 
 |             *auth = LYNQ_WIFI_AUTH_WPA_PSK; | 
 |         } | 
 |     } | 
 |     else { | 
 |         *auth = -1; | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 |  | 
 | int lynq_wifi_ap_start(lynq_wifi_index_e idx) | 
 | { | 
 |     char LYNQ_WIFI_CMD[128]={0}; | 
 |     //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all"; | 
 |     //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 | //    system("connmanctl enable wifi"); | 
 | //    system("connmanctl tether wifi on cy-test 12345678"); | 
 | //    system("ifconfig wlan0 down"); | 
 | //    system("ifconfig wlan0 up"); | 
 | //    system("ifconfig wlan0 up"); | 
 |  | 
 |     //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd); | 
 |     //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd); | 
 |  | 
 |     sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0); | 
 |     DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_restart(lynq_wifi_index_e idx) | 
 | { | 
 |     return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx)  : -1; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_stop(lynq_wifi_index_e idx) | 
 | { | 
 |     char LYNQ_WIFI_CMD[128]={0}; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); | 
 |  | 
 | //    system("connmanctl tether wifi off"); | 
 | 	 | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx) | 
 | { | 
 |     char lynq_disable_cmd[128] = {0}; | 
 |     char lynq_select_cmd[128] = {0}; | 
 |     const char *lynq_hide_cmd = "SET HIDE_SSID 1"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 | 	 | 
 |     sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0); | 
 |     sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_disable_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_hide_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_select_cmd); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx) | 
 | { | 
 |     char lynq_disable_cmd[128] = {0}; | 
 |     char lynq_select_cmd[128] = {0}; | 
 |     const char *lynq_unhide_cmd = "SET HIDE_SSID 0"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0); | 
 |     sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_disable_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_unhide_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_select_cmd); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_ap_password_set(lynq_wifi_index_e idx,char *password) | 
 | { | 
 | 	int pass_len; | 
 |     char lynq_tmp_cmd[MAX_CMD] = {0}; | 
 |     char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0}; | 
 |     if( password == NULL ){ | 
 |         return -1; | 
 |     } | 
 |     pass_len=strlen(password); | 
 |     lynq_wifi_auth_s auth = -1; | 
 |     if(pass_len < 8 || pass_len >= 64){ | 
 | 		return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     if (0 != lynq_wifi_ap_auth_get(idx, &auth)) { | 
 |         return -1; | 
 |     } | 
 |     else if (auth == LYNQ_WIFI_AUTH_OPEN) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     if (auth == LYNQ_WIFI_AUTH_WEP) { | 
 |         sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password); | 
 |         sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0); | 
 |         DO_OK_FAIL_REQUEST(lynq_tmp_cmd); | 
 |         DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd); | 
 |     } | 
 |     else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) { | 
 |         sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password); | 
 |         DO_OK_FAIL_REQUEST(lynq_tmp_cmd); | 
 |     } | 
 |     else { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | int lynq_ap_password_get(lynq_wifi_index_e idx, char *password) | 
 | { | 
 |     FILE * fp; | 
 |     int len, ret; | 
 |     int count, index; | 
 |     char *split_lines[128] = {0}; | 
 |     char *buff, *p; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb"); | 
 | //    fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "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); | 
 |  | 
 |     for(index=0; index < len; index ++) { | 
 |         if (memcmp(buff + index, "network={", 9) != 0) { | 
 |             continue; | 
 |         } | 
 |          p = buff + index + 9; | 
 |          for (; index < len; index ++ ) { | 
 |              if (buff[index] != '}') { | 
 |                  continue; | 
 |              } | 
 |              buff[index] = '\0'; | 
 |              break; | 
 |          } | 
 |          len = buff + index - p; | 
 |     } | 
 |  | 
 |     count = lynq_split(p, len, '\n', split_lines); | 
 |  | 
 |     ret = -1; | 
 |     for(index=0; index < count; index++) { | 
 |         p = strstr(split_lines[index], "psk="); | 
 |         if (p != NULL) { | 
 |             p += 4; | 
 |             if (*p == '\"') { | 
 |                 p++; | 
 |             } | 
 |         } | 
 |         else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) { | 
 |             p += 9; | 
 |             if (*p == '\"') { | 
 |                 p++; | 
 |             } | 
 |         } | 
 |         else { | 
 |             continue; | 
 |         } | 
 |  | 
 |         strcpy(password, p); | 
 |  | 
 |         while(*password != '\0') { | 
 |             if (*password == '\"') { | 
 |                 *password = '\0'; | 
 |                 break; | 
 |             } | 
 |             password++; | 
 |         } | 
 |         ret = 0; | 
 |         break; | 
 |     } //end for(index=0; index < count; index++) | 
 |  | 
 |     return ret; | 
 | } | 
 |  | 
 | static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) { | 
 |     char lynq_auth_str[MAX_RET] = {0}; | 
 |     char lynq_proto_str[MAX_RET] = {0}; | 
 |  | 
 |     if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     *auth = convert_auth_from_key_mgmt(lynq_auth_str); | 
 |  | 
 |     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; | 
 | } | 
 |  | 
 | int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password) | 
 | { | 
 |     int pass_len, net_no, count, index; | 
 |     char lynq_tmp_cmd[300]={0}; | 
 |     int net_no_list[128]; | 
 |     lynq_wifi_auth_s net_auth; | 
 |     pass_len=strlen(password); | 
 |     if(pass_len < 8 || pass_len >= 64){ | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     net_no = -1; | 
 |     count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_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 == ap->auth) { | 
 |             net_no = net_no_list[index]; | 
 |             break; | 
 |         } | 
 |     } | 
 |  | 
 |     if (net_no < 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_tmp_cmd); | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | 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 count, index; | 
 |     char *split_lines[128] = {0}; | 
 |     char *buff, *p; | 
 |  | 
 |     network_len = 0; | 
 |     p = NULL; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     if (NULL == password) { | 
 |         printf("bad param\n"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "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); | 
 |  | 
 |     for(index=0; index < len; index ++) { | 
 |         for(; index < len; index ++) { | 
 |             if (memcmp(buff + index, "network={", 9) != 0) { | 
 |                 continue; | 
 |             } | 
 |              p = buff + index + 9; | 
 |              for (; index < len; index ++ ) { | 
 |                  if (buff[index] != '}') { | 
 |                      continue; | 
 |                  } | 
 |                  buff[index] = '\0'; | 
 |                  break; | 
 |              } | 
 |              network_len = buff + index - p; | 
 |              break; | 
 |         } | 
 |  | 
 |         if (strstr(p, ap->ap_ssid) != NULL) { | 
 |             break; | 
 |         } | 
 |     } | 
 |  | 
 |     if (index >= len || NULL == p || network_len <= 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     count = lynq_split(p, network_len, '\n', split_lines); | 
 |  | 
 |     ret = -1; | 
 |     for(index=0; index < count; index++) { | 
 |         p = strstr(split_lines[index], "psk="); | 
 |         if (p != NULL) { | 
 |             p += 4; | 
 |             if (*p == '\"') { | 
 |                 p++; | 
 |             } | 
 |         } | 
 |         else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) { | 
 |             p += 9; | 
 |             if (*p == '\"') { | 
 |                 p++; | 
 |             } | 
 |         } | 
 |         else { | 
 |             continue; | 
 |         } | 
 |  | 
 |         strcpy(password, p); | 
 |  | 
 |         while(*password != '\0') { | 
 |             if (*password == '\"') { | 
 |                 *password = '\0'; | 
 |                 break; | 
 |             } | 
 |             password++; | 
 |         } | 
 |         ret = 0; | 
 |         break; | 
 |     } //end for(index=0; index < count; index++) | 
 |  | 
 |     return ret; | 
 | } | 
 |  | 
 | static int inner_set_sta_ssid(int net_no, char *sta_ssid) | 
 | { | 
 | 	char lynq_wifi_ssid_cmd[80]={0}; | 
 |  | 
 | 	if (sta_ssid == NULL) { | 
 | 		printf("sta_ssid is null\n"); | 
 | 		return -1; | 
 |     } | 
 |  | 
 | 	CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd); | 
 | //    DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 | 	return 0; | 
 |  | 
 | } | 
 |  | 
 | static int inner_sta_start_stop(int net_no, int start_flag, int save) | 
 | { | 
 |     char lynq_disable_cmd[128]={0}; | 
 |     char lynq_select_cmd[128]={0}; | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     if (save != 0) { | 
 |         if (start_flag != 0) | 
 |         { | 
 |             sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no); | 
 |             DO_OK_FAIL_REQUEST(lynq_select_cmd); | 
 |         } | 
 |         else | 
 |         { | 
 |             sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no); | 
 |             DO_OK_FAIL_REQUEST(lynq_select_cmd); | 
 |         } | 
 |         DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |     } | 
 |  | 
 |     if (start_flag == 0) { | 
 |         sprintf(lynq_disable_cmd,"DISCONNECT"); | 
 |         DO_OK_FAIL_REQUEST(lynq_disable_cmd); | 
 |     } | 
 |     else { | 
 |         sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no); | 
 |         DO_OK_FAIL_REQUEST(lynq_select_cmd); | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid) | 
 | { | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     curr_status_info curr_state; | 
 |     ap_info_s ap_info; | 
 |     curr_state.ap = &ap_info; | 
 |     curr_state.state = NULL; | 
 |  | 
 |     if (0 == inner_get_status_info(CTRL_STA, &curr_state)) { | 
 |         strcpy(sta_ssid, ap_info.ap_ssid); | 
 |         return 0; | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 | int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info) | 
 | { | 
 |     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, ret; | 
 |  | 
 |     ret = -1; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |     if (info == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     curr_status_info curr_state; | 
 |     ap_info_s ap_info; | 
 |     char status[64]; | 
 |  | 
 |     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)); | 
 |         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; | 
 |     } | 
 |  | 
 |     for (i=0; i < save_len; i++) { | 
 |         for (j=0; j < scan_len; j++) { | 
 |             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) { | 
 |                     best_index = i; | 
 |                     best_scan_index = j; | 
 |                     best_rssi = scan_list[j].rssi; | 
 |                 } | 
 |                 break; | 
 |             } | 
 |         } | 
 |     } | 
 |  | 
 |     if (best_index >= 0) { | 
 |         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; | 
 |         ret = 0; | 
 |     } | 
 |  | 
 |     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) | 
 | { | 
 |     char lynq_auth_cmd[64]={0}; | 
 |     char lynq_ket_mgmt_cmd[64]={0}; | 
 |     char lynq_pairwise_cmd[64]={0}; | 
 |     char lynq_psk_cmd[64]={0}; | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 | 	switch(auth){ | 
 | 		case LYNQ_WIFI_AUTH_OPEN: | 
 |         { | 
 |             sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no); | 
 |  | 
 |             DO_OK_FAIL_REQUEST(lynq_auth_cmd); | 
 | //            DO_OK_FAIL_REQUEST(cmd_save_config); | 
 | 			break; | 
 | 		} | 
 | 		case LYNQ_WIFI_AUTH_WPA_PSK: | 
 |         case LYNQ_WIFI_AUTH_WPA2_PSK: | 
 | 		{ | 
 |             if (auth == LYNQ_WIFI_AUTH_WPA_PSK) { | 
 |                 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no); | 
 |             } | 
 |             else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) { | 
 |                 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no); | 
 |             } | 
 |             sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no); | 
 |             sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no); | 
 |  | 
 |             DO_OK_FAIL_REQUEST(lynq_auth_cmd); | 
 |             DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd); | 
 |             DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); | 
 |  | 
 |             if (password != NULL) { | 
 |                 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password); | 
 |                 DO_OK_FAIL_REQUEST(lynq_psk_cmd); | 
 |             } | 
 |  | 
 | //            DO_OK_FAIL_REQUEST(cmd_save_config); | 
 | 			break; | 
 | 		}		 | 
 | 		default: | 
 | 			return -1; | 
 |     } | 
 |  | 
 | 	return 0; | 
 | } | 
 |  | 
 | static int inner_get_curr_net_no(int interface) { | 
 |     curr_status_info curr_state; | 
 |     curr_state.ap = NULL; | 
 |     curr_state.state = NULL; | 
 |  | 
 |     if (0 != inner_get_status_info(interface, &curr_state)) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     return curr_state.net_no; | 
 | } | 
 |  | 
 | int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) | 
 | { | 
 |     int net_no; | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     net_no = inner_get_curr_net_no(CTRL_STA); | 
 |  | 
 |     if (net_no < 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     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 count, net_no, index; | 
 |     int net_no_list[128]; | 
 |     lynq_wifi_auth_s net_auth; | 
 |  | 
 |     if (ssid == NULL || *ssid == '\0') { | 
 |         printf("bad ssid\n"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     if (LYNQ_WIFI_AUTH_OPEN != auth) { | 
 |         if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) { | 
 |             printf("bad password\n"); | 
 |             return -1; | 
 |         } | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     net_no = -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; | 
 |         } | 
 |     } | 
 |  | 
 |     if (net_no < 0) { | 
 |         net_no = lynq_add_network(CTRL_STA); | 
 |         if (net_no == -1) { | 
 |             return -1; | 
 |         } | 
 |  | 
 |         printf("net no is %d\n", net_no); | 
 |         if (0 != inner_set_sta_ssid(net_no, ssid)) { | 
 |             return -1; | 
 |         } | 
 |     } | 
 |  | 
 |     if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     return inner_sta_start_stop(net_no, 1, 1); | 
 | } | 
 |  | 
 | int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid) | 
 | { | 
 |     ap_info_s ap; | 
 |     curr_status_info curr_state; | 
 |     ap.ap_ssid[0] = '\0'; | 
 |  | 
 |     if (ssid == NULL || *ssid == '\0') { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     curr_state.ap = ≈ | 
 |     curr_state.state = NULL; | 
 |  | 
 |     if (inner_get_status_info(CTRL_STA, &curr_state) != 0) { | 
 |         return 0; | 
 |     } | 
 |  | 
 |     if (strcmp(ap.ap_ssid, ssid) != 0) { | 
 |         return 0; | 
 |     } | 
 |  | 
 |     return inner_sta_start_stop(curr_state.net_no, 0, 0); | 
 | } | 
 |  | 
 | int lynq_wifi_sta_start(lynq_wifi_index_e idx) | 
 | { | 
 |     const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; | 
 |     const char *lynq_reconnect_cmd = "RECONNECT"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |     CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     system("connmanctl enable wifi"); | 
 |  | 
 |     if (system("ifconfig | grep -q wlan0") != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     DO_OK_FAIL_REQUEST(cmd_remove_all); | 
 |     system(lynq_reconfigure_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_reconnect_cmd); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_sta_stop(lynq_wifi_index_e idx) | 
 | { | 
 |     char lynq_disable_network_cmd[MAX_CMD]; | 
 |     curr_status_info curr_state; | 
 |     ap_info_s ap_info; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |     CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     curr_state.ap = &ap_info; | 
 |     curr_state.state = NULL; | 
 |  | 
 |     if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) { | 
 |         return 0; | 
 |     } | 
 |  | 
 |     sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no); | 
 |     DO_OK_FAIL_REQUEST(lynq_disable_network_cmd); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 |     return 0; | 
 | //    return system("connmanctl disable wifi"); | 
 | } | 
 |  | 
 | //static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) { | 
 | //    int i, count; | 
 | //    char *p; | 
 | //    const char * FLAG_SSID = "ssid="; | 
 | //    const char * FLAG_SBSID = "bssid="; | 
 | //    const char * FLAG_KEY_MGMT = "key_mgmt="; | 
 | //    const char * FLAG_FREQ = "freq="; | 
 | //    char lynq_sta_cmd[MAX_CMD]; | 
 | //    char *split_lines[128] = {0}; | 
 |  | 
 | //    CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 | //    sprintf(lynq_sta_cmd, "STA %s", bssid); | 
 |  | 
 | //    DO_REQUEST(lynq_sta_cmd); | 
 |  | 
 | //    count = lynq_split(cmd_reply, reply_len, '\n', split_lines); | 
 |  | 
 | //    for(i=0; i < count; i++) { | 
 | //        p = strstr(split_lines[i], FLAG_SSID); | 
 | //        if (p != NULL) { | 
 | //            strcpy(ap->ap_ssid, p + strlen(FLAG_SSID)); | 
 | //            continue; | 
 | //        } | 
 | //    } | 
 |  | 
 | //    lynq_get_interface_ip(idx, ap->ap_ip); | 
 | //    lynq_ap_password_set(idx, ap->psw); | 
 |  | 
 | //    return 0; | 
 | //} | 
 |  | 
 | static int inner_get_status_info_ap (int interface, ap_info_s *ap) { | 
 |     curr_status_info curr_state; | 
 |     curr_state.ap = ap; | 
 |     curr_state.state = NULL; | 
 |     return inner_get_status_info(interface, &curr_state); | 
 | } | 
 |  | 
 | 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; | 
 |     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); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 | //    ap_info_s * tmp_ap; | 
 | //    device_info_s * tmp_list; | 
 |     if (ap == NULL || list == NULL || len == NULL) { | 
 |         printf("bad input param"); | 
 |         return -1; | 
 |     } | 
 |  | 
 | //    ap = &tmp_ap; | 
 | //    list = &tmp_list; | 
 |     *ap = malloc(sizeof (ap_info_s)); | 
 |  | 
 |     if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     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; | 
 |     while (reply_len > 0) { | 
 |         if (memcmp(cmd_reply, "FAIL", 4) == 0) { | 
 |             break; | 
 |         } | 
 |         line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines); | 
 |         bssid[index] = malloc(strlen(split_lines[0]) + 1); | 
 |         strcpy(bssid[index], split_lines[0]); | 
 |         index++; | 
 |         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); | 
 |         if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) { | 
 |             printf("run %s fail \n", lynq_next_sta_cmd); | 
 |             break; | 
 |         } | 
 |     } | 
 |  | 
 |     *len = index; | 
 |  | 
 |     *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; | 
 |         free(bssid[index]); | 
 |     } | 
 |  | 
 |     free_ip_mac_list_mem(mac_list, 128, ip_list, 128); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len) | 
 | { | 
 |     int i, count, index, count_words; | 
 |     const char *lynq_scan_result_cmd = "SCAN_RESULTS"; | 
 |     char *split_lines[128] = {0}; | 
 |     char *split_words[128] = {0}; | 
 |     scan_info_s * p; | 
 |  | 
 |     if (list == NULL || len == NULL) { | 
 |         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); | 
 |  | 
 |     DO_REQUEST(lynq_scan_result_cmd); | 
 |  | 
 |     count = lynq_split(cmd_reply, reply_len, '\n', split_lines); | 
 |     *len = count - 1; | 
 |     *list = malloc(sizeof (scan_info_s) * *len); | 
 |  | 
 |     count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header | 
 |     for (index=0; index <count_words; index++) { | 
 |         printf("----header: %s\n", split_words[index]); | 
 |     } | 
 |  | 
 |     for(index = 1;index < count; index++) { | 
 |         printf("---- %s\n",split_lines[index]); | 
 |         count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words); | 
 |         if (count_words < 4) | 
 |             continue; | 
 |         printf("count: %d, %s\n", count_words, split_words[0]); | 
 |         //bssid / frequency / signal level / flags / ssid | 
 |         p = (*list) + index - 1; | 
 |         strcpy(p->mac, split_words[0]); | 
 |         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]); | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth) | 
 | { | 
 |     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') { | 
 |         printf("bad ssid\n"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     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); | 
 |  | 
 |     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; | 
 |         } | 
 |     } | 
 |  | 
 |     if (net_no < 0) { | 
 |         return 0; | 
 |     } | 
 |  | 
 |     sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_remove_cmd); | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len) | 
 | {     | 
 |     int count, index, ssid_len; | 
 |     int net_no_list[128]; | 
 |     char freq[16]; | 
 |     char *ssid_ptr; | 
 |  | 
 |     if (list == NULL || len == NULL) { | 
 |         printf("bad param\n"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 | //    CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL); | 
 |     printf("count is %d\n", count); | 
 |  | 
 |     *list = malloc(sizeof (saved_ap_info_s) * count); | 
 |     memset(*list, 0, sizeof (saved_ap_info_s) * count); | 
 |     *len = count; | 
 |  | 
 |     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) { | 
 |             (*list)[index].base_info.band = convert_band_from_freq(atoi(freq)); | 
 |         } | 
 |         else { | 
 |             (*list)[index].base_info.band = -1; | 
 |         } | 
 |  | 
 |         lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw); | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx) | 
 | { | 
 |     const char *lynq_scan_cmd = "SCAN"; | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(lynq_scan_cmd); | 
 |     g_sta_scan_finish_flag = 0; | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) { | 
 |     if (cb == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     g_ap_callback_priv = priv; | 
 |     g_ap_callback_func = cb; | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_unreg_ap_event_callback(void * priv) { | 
 |     if (g_ap_callback_priv == priv) { | 
 |         g_ap_callback_func = NULL; | 
 |         g_ap_callback_priv = NULL; | 
 |         return 0; | 
 |     } | 
 |     return -1; | 
 | } | 
 |  | 
 | int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){ | 
 |     if (cb == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     g_sta_callback_priv = priv; | 
 |     g_sta_callback_func = cb; | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_unreg_sta_event_callback(void * priv) { | 
 |     if (g_sta_callback_priv == priv) { | 
 |         g_sta_callback_func = NULL; | 
 |         g_sta_callback_priv = NULL; | 
 |         return 0; | 
 |     } | 
 |     return -1; | 
 | } | 
 |  | 
 |  | 
 | static int inner_get_status_info_state (int interface, char *state) { | 
 |     curr_status_info curr_state; | 
 |     curr_state.ap = NULL; | 
 |     curr_state.state = state; | 
 |     return inner_get_status_info(interface, &curr_state); | 
 | } | 
 |  | 
 | int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status) | 
 | { | 
 |     char state[MAX_CMD]; | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     if (inner_get_status_info_state(CTRL_AP, state) != 0) { | 
 |         *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; | 
 |         return 0; | 
 |     } | 
 |  | 
 |     if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { | 
 |         *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE; | 
 |     } | 
 |     else { | 
 |         *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) { | 
 |     char state[MAX_CMD]; | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     if (inner_get_status_info_state(CTRL_STA, state) != 0) { | 
 |         *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; | 
 |         return 0; | 
 |     } | 
 |  | 
 |     if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { | 
 |         *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE; | 
 |     } | 
 |     else { | 
 |         *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) { | 
 | //    CHECK_IDX(idx, CTRL_AP); | 
 | //    int ret = 0; | 
 | //    size_t reply_len = MAX_RET; | 
 | //    char cmd_reply[MAX_RET]={0}; | 
 | //    const char * cmd_str = "GET country"; | 
 | //    struct wpa_ctrl *s_lynq_wpa_ctrl = NULL; | 
 | //    do{ | 
 | //        if (NULL == s_lynq_wpa_ctrl) { | 
 | //            s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd"); | 
 | //            if (NULL == s_lynq_wpa_ctrl ) { | 
 | //                printf("wpa_ctrl_open fail\n"); | 
 | //                return -1; | 
 | //            } | 
 | //        } | 
 | //    }while(0); | 
 |  | 
 | //    do { | 
 | //        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); | 
 | //        if (ret != 0) { | 
 | //            printf("call ##cmd_str fail %d\n", ret); | 
 | //            return ret; | 
 | //        } | 
 | //        cmd_reply[reply_len+1] = '\0'; | 
 | //        printf("cmd replay [ %s ]\n", cmd_reply); | 
 | //    }while(0); | 
 |  | 
 |     FILE *fp; | 
 |     size_t i = 0; | 
 |     char lynq_cmd_ret[MAX_RET]={0}; | 
 |  | 
 | //    CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     if((fp=popen("wl country","r"))==NULL) | 
 |         { | 
 |             perror("popen error!"); | 
 |             return -1; | 
 |         } | 
 |     if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) | 
 |     { | 
 |         perror("fread fail!"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     for(i=0; i < strlen(lynq_cmd_ret); i++) { | 
 |         if (lynq_cmd_ret[i] == ' ') { | 
 |             lynq_cmd_ret[i] = '\0'; | 
 |             break; | 
 |         } | 
 |     } | 
 |  | 
 |     strcpy(country_code,lynq_cmd_ret); | 
 |     printf("---country code %s\n", country_code); | 
 |  | 
 |     int ret=pclose(fp); | 
 |     if(ret==-1) | 
 |     { | 
 |         perror("close file faild"); | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) { | 
 | //    const char * cmd_str = "GET country"; | 
 | //    CHECK_IDX(idx, CTRL_AP); | 
 | //    CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 | //    DO_REQUEST(cmd_str); | 
 | //    printf("result %s\n", cmd_reply); | 
 |  | 
 |     if (country_code == NULL || *country_code == '\0') { | 
 |         printf("bad country code\n"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     char lynq_country_cmd[MAX_CMD]; | 
 |     sprintf(lynq_country_cmd, "wl country %s", country_code); | 
 |     if (system(lynq_country_cmd) == 0) { | 
 |         return 0; | 
 |     } | 
 |  | 
 |     return -1; | 
 | } | 
 |  | 
 | int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac) | 
 | { | 
 |     if (mac == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |     ap_info_s ap; | 
 |     ap.ap_mac[0] = '\0'; | 
 |  | 
 |     if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { | 
 |         return -1; | 
 |     } | 
 |     strcpy(mac, ap.ap_mac); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip) | 
 | { | 
 |     struct ifaddrs *ifaddr_header, *ifaddr; | 
 |     struct in_addr * ifa; | 
 |     const char * ifaName = "wlan0"; | 
 |     if (ip == NULL) | 
 |     { | 
 |        return -1; | 
 |     } | 
 |  | 
 |     if (idx == 1) { | 
 |        ifaName = "tether"; | 
 |     } | 
 |     else if (idx != 0) { | 
 |        return -1; | 
 |     } | 
 |  | 
 |     if (getifaddrs(&ifaddr_header) == -1) | 
 |     { | 
 |        perror("getifaddrs"); | 
 |        return -1; | 
 |        //exit(EXIT_FAILURE); | 
 |     } | 
 |  | 
 |  | 
 |     for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next) | 
 |     { | 
 |         if (ifaddr->ifa_addr == NULL) | 
 |            continue; | 
 |         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) | 
 | { | 
 |     int count; | 
 |     size_t i; | 
 |     char *split_words[128] = {0}; | 
 |     const char *lynq_get_mac_cmd = "DRIVER MACADDR"; | 
 |  | 
 |     CHECK_WPA_CTRL(idx); | 
 |  | 
 |     DO_REQUEST(lynq_get_mac_cmd); | 
 |  | 
 |     if (memcmp(cmd_reply, "FAIL", 4) == 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     count = lynq_split(cmd_reply, reply_len, '=', split_words); | 
 |  | 
 |     if (count < 2) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     for (i=0; i < strlen(split_words[1]); i++ ) { | 
 |         if (split_words[1][i] != ' ') { | 
 |             break; | 
 |         } | 
 |     } | 
 |  | 
 |     strcpy(mac, split_words[1] + i); | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi) | 
 | { | 
 | //    int count; | 
 | //    char *split_words[128] = {0}; | 
 | //    const char *lynq_get_rssi_cmd = "DRIVER RSSI"; | 
 |  | 
 | //    if (rssi == NULL) { | 
 | //        return -1; | 
 | //    } | 
 |  | 
 | //    CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 | //    CHECK_WPA_CTRL(CTRL_STA); | 
 |  | 
 | //    DO_REQUEST(lynq_get_rssi_cmd); | 
 |  | 
 | //    if (memcmp(cmd_reply, "FAIL", 4) == 0) { | 
 | //        return -1; | 
 | //    } | 
 |  | 
 | //    count = lynq_split(cmd_reply, reply_len, ' ', split_words); | 
 |  | 
 | //    if (count < 2) { | 
 | //        return -1; | 
 | //    } | 
 |  | 
 | //    *rssi = atoi(split_words[1]) * -1; | 
 |  | 
 |     FILE *fp; | 
 |     size_t i = 0; | 
 |     char lynq_cmd_ret[MAX_RET]={0}; | 
 |  | 
 | //    CHECK_IDX(idx, CTRL_AP); | 
 | /*******change other cmd to get rssi******* | 
 |  * | 
 |  *wl rssi ---> wl -i wlan0 rssi | 
 |  * | 
 |  ***** change by qs.xiong 20221011*******/ | 
 |     if((fp=popen("wl -i wlan0 rssi","r"))==NULL) | 
 |         { | 
 |             perror("popen error!"); | 
 |             return -1; | 
 |         } | 
 |     if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) | 
 |     { | 
 |         perror("fread fail!"); | 
 |         return -1; | 
 |     } | 
 |     *rssi = atoi(lynq_cmd_ret) * -1; | 
 | /****** if got rssi is 0,means sta didn't connected any device****/ | 
 |     if(*rssi == 0) | 
 |     { | 
 |         printf("sta didn't connected any ap device,please check connection\n"); | 
 |     } | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band) | 
 | { | 
 |     if (band == NULL) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |     ap_info_s ap; | 
 |     ap.band = -1; | 
 |  | 
 |     if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { | 
 |         return -1; | 
 |     } | 
 |     *band = ap.band; | 
 |  | 
 |     return 0; | 
 | } | 
 |  | 
 | 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) | 
 |     { | 
 |         printf("invalid param"); | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_STA); | 
 |  | 
 |     if (lynq_get_connect_ap_mac(idx, bssid) != 0) | 
 |     { | 
 |         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; | 
 | } | 
 |  | 
 | int lynq_ap_connect_num(int sta_number) | 
 | { | 
 |     char lynq_limit_cmd[32]={0}; | 
 |     int ret; | 
 |     if((sta_number < 1 ) && (sta_number > 15)){ | 
 |         printf("sta_number: not in range\n",sta_number); | 
 |         return -1; | 
 |     } | 
 |     sprintf(lynq_limit_cmd,"wl maxassoc  %d", sta_number); | 
 |     ret = system(lynq_limit_cmd); | 
 |     if(ret != 0){ | 
 |         printf("cmd of limit ap devices number error\n"); | 
 |     } | 
 |     return 0; | 
 | } | 
 |  | 
 | int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode) | 
 | { | 
 |  | 
 |     char lynq_wifi_acs_cmd[128]={0}; | 
 |     char lynq_cmd_mode[128]={0}; | 
 |     char lynq_cmd_slect[128]={0}; | 
 |  | 
 |     if((acs_mode != 2) && (acs_mode != 5)){ | 
 |         PRINT_AND_RETURN_VALUE("set acs_mode is error",-1); | 
 |     } | 
 |  | 
 |     if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { | 
 |         return -1; | 
 |     } | 
 |  | 
 |     CHECK_IDX(idx, CTRL_AP); | 
 |  | 
 |     CHECK_WPA_CTRL(CTRL_AP); | 
 |  | 
 |     sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode); | 
 |     sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); | 
 |     sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); | 
 |  | 
 |     DO_OK_FAIL_REQUEST(cmd_disconnect); | 
 |     DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd); | 
 |     DO_OK_FAIL_REQUEST(lynq_cmd_mode); | 
 |     DO_OK_FAIL_REQUEST(cmd_save_config); | 
 |     DO_OK_FAIL_REQUEST(lynq_cmd_slect); | 
 |  | 
 |     return 0; | 
 | } |