[Feature] use WPA_CLIENT_SO & add ap+sta mode

Change-Id: I6e8246f1a978d82694489f84d94b4767f1667f23
diff --git a/src/lynq/lib/liblynq-wifi6/include/libwifi6.h b/src/lynq/lib/liblynq-wifi6/include/libwifi6.h
index 8afadb4..f5d9f01 100755
--- a/src/lynq/lib/liblynq-wifi6/include/libwifi6.h
+++ b/src/lynq/lib/liblynq-wifi6/include/libwifi6.h
@@ -10,72 +10,212 @@
 #define __LIBWIFI6_H__
 
 typedef enum {
-	LYNQ_WIFI_WORK_MODE_STA=0,		/*WiFi is in STA mode */
-	LYNQ_WIFI_WORK_MODE_AP,			/*WiFi is in AP mode */
-}lynq_wifi_work_mode_m;
-
-typedef enum {
-	LYNQ_WIFI_BANDWIDTH_HT20 = 0,	
-	LYNQ_WIFI_BANDWIDTH_HT80,		
+    LYNQ_WIFI_BANDWIDTH_HT10 = 0, // not support in ap mode
+    LYNQ_WIFI_BANDWIDTH_HT20,
+    LYNQ_WIFI_BANDWIDTH_HT40,    // not support in ap mode
+    LYNQ_WIFI_BANDWIDTH_HT80,
 }lynq_wifi_bandwidth_type_m;
 
 typedef enum {
-	LYNQ_WIFI_AUTH_OPEN = 0,
-	LYNQ_WIFI_AUTH_WPA_PSK,
-}lynq_wifi_ap_auth_s;
+    LYNQ_WIFI_AUTH_OPEN = 0,
+    LYNQ_WIFI_AUTH_WEP,
+    LYNQ_WIFI_AUTH_WPA_PSK,
+    LYNQ_WIFI_AUTH_WPA2_PSK,
+}lynq_wifi_auth_s;
 
 typedef enum {
-	LYNQ_WIFI_STA_AUTH_OPEN = 0,
-	LYNQ_WIFI_STA_AUTH_WPA_PSK,
-}lynq_wifi_sta_auth_s;
+    LYNQ_WIFI_2G_band = 1,
+    LYNQ_WIFI_5G_band,
+    LYNQ_WIFI_2_and_5G_band,  //not support
+}lynq_wifi_band_m;
 
 typedef enum {
-	LYNQ_NETWORK_0 =0,
-	LYNQ_NETWORK_1,
-	LYNQ_NETWORK_2,
-	LYNQ_NETWORK_3,
-	LYNQ_NETWORK_4,
-	LYNQ_NETWORK_5,
-	LYNQ_NETWORK_6,
-	LYNQ_NETWORK_7,
-	LYNQ_NETWORK_8,
-	LYNQ_NETWORK_9,
+    LYNQ_WIFI_AP_STATUS_DISABLE = 0,   
+    LYNQ_WIFI_AP_STATUS_ENABLE,        //ap is running status
+}lynq_wifi_ap_run_status_s;
+
+typedef enum {
+    LYNQ_WIFI_STA_STATUS_DISABLE = 0,
+    LYNQ_WIFI_STA_STATUS_ENABLE,       //sta is running status
+}lynq_wifi_sta_run_status_s;
+
+typedef enum {
+    LYNQ_WIFI_INTERFACE_0 = 0,      //sta
+    LYNQ_WIFI_INTERFACE_1,         //ap
 }lynq_wifi_index_e;
 
+typedef enum {
+    LYNQ_WIFI_STATUS_DISCONNECT = 0,
+    LYNQ_WIFI_STATUS_CONNECT,
+}lynq_wifi_ap_status_s;
+
+typedef struct ap_info
+{
+    char ap_ip[32];
+    char ap_mac[32];
+    char ap_ssid[32];
+    char psw[64];                      //password 
+    lynq_wifi_auth_s auth;
+    lynq_wifi_band_m band;
+}ap_info_s;
+
+typedef struct device_info
+{
+    char sta_ip[32];
+    char sta_mac[32];
+    char hostname[32];
+    lynq_wifi_ap_status_s status;
+}device_info_s;                
+
+typedef struct scan_info
+{
+    char mac[32];
+    char ssid[32];
+    lynq_wifi_band_m band;
+    lynq_wifi_auth_s auth;
+    int rssi;
+}scan_info_s;
+
+typedef struct ap_detail_info
+{
+    struct ap_info base_info;            // base_info : base info of strcut ap_info 
+    lynq_wifi_ap_run_status_s status;     // status:is not running 1:is running
+    int rssi;                         //[0~199]
+}ap_detail_info_s;
+
+typedef struct saved_ap_info
+{
+    struct ap_info base_info;
+}saved_ap_info_s;
+
+
+// insmod drive and start service of wifi
 int lynq_wifi_enable(void);
+//rmmod drive
 int lynq_wifi_disable(void);
-int lynq_wifi_wpa_start(void);
-int lynq_wifi_wpacli_start(void);
-//int lynq_wifi_work_mode_set(lynq_wifi_work_mode_m mode);
-int lynq_wifi_work_mode_get(lynq_wifi_work_mode_m *mode);
+//set ssid of ap
 int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid);
+//get ap of ssid
 int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx,char *ap_ssid);
+
+//set frquency for ap
 int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency);
+//get freuency of ap
 int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency);
-//int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth);
+
+//set bandwidth for ap
+int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth);
+//get thr bandwidth of ap
 int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m *bandwidth);
-//int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel);
+
+//set channel for ap
+int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel);
+//get channel of ap 
 int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel);
-int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_ap_auth_s auth);
-int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_ap_auth_s *auth);
-//int lynq_wifi_ap_max_sta_set(lynq_wifi_index_e idx,int max_sta_num);
-int lynq_wifi_ap_max_sta_get(lynq_wifi_index_e idx,int* max_sta_num);
+
+//set auth for ap
+int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth);
+//get ap auth
+int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth);
+
+//start ap
 int lynq_wifi_ap_start(lynq_wifi_index_e idx);
-int lynq_wifi_ap_restart(lynq_wifi_index_e idx);
+//stop ap
 int lynq_wifi_ap_stop(lynq_wifi_index_e idx);
+//restart ap
+int lynq_wifi_ap_restart(lynq_wifi_index_e idx);
+
+//hide ssid
 int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx);
+//unhide ssid
 int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx);
-int lynq_ap_ssid_pass_set(lynq_wifi_index_e idx,char *password);
-int lynq_sta_ssid_pass_set(lynq_wifi_index_e idx,char *password);
 
-int lynq_wifi_sta_ssid_set(lynq_wifi_index_e idx,char *sta_ssid);
-int lynq_wifi_sta_ssid_get(lynq_wifi_index_e idx,char *sta_ssid);
-int lynq_wifi_sta_auth_set(lynq_wifi_index_e idx, lynq_wifi_sta_auth_s auth);
-int lynq_wifi_sta_auth_get(lynq_wifi_index_e idx, lynq_wifi_sta_auth_s* auth);
-int lynq_wifi_sta_connect(lynq_wifi_index_e idx);
-int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx);
+//set the password for ap
+int lynq_ap_password_set(lynq_wifi_index_e idx, char *password);
+//get the ap password
+int lynq_ap_password_get(lynq_wifi_index_e idx, char *password);
 
-int lynq_get_wifi_mac(char* wifi_mac);
+//set the password to connet to dest_ap
+int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password);
+int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password);
+
+//get the ssid of sta
+int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx,char *sta_ssid);
+//get availble device info such as:ssid mac band rssi status auth
+int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx,ap_detail_info_s *info);  
+
+
+
+//get the sta connect ap auth
+int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s* auth);    //auth 0:OPEN 1:WEP 2:WPA-PSK 3:WPA2-PSK
+
+//sta start connect to dest_ap
+int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw);
+//disconnect 
+int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx,char *ssid);
+
+//start sta mode;enable sta
+int lynq_wifi_sta_start(lynq_wifi_index_e idx);
+//stop sta:disable sta
+int lynq_wifi_sta_stop(lynq_wifi_index_e idx);
+
+//Get all device info linked ap
+int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len);        //list info:mac ip hostname status  len:device len
+//Get scan_result info
+int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len);                    //list info:mac ssid band auth rssi    len:scan_info len
+int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid,lynq_wifi_auth_s auth);
+int lynq_get_sta_saved_ap(lynq_wifi_index_e idx,saved_ap_info_s ** list,int * len);      //len: length of saved_ap_info
+
+//start scan availbale ap active
+int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx);
+
+/*
+ * event usage:
+ * first declare a funcion like AP_CALLBACK_FUNC_PTR to recv messge from wifi lib
+ * call lynq_reg_ap_event_callback to register whih private data pointer and the callback fuction pointer declared before
+ * when envent comes the last registered callback function will been called
+ */
+
+typedef void(*AP_CALLBACK_FUNC_PTR)(void *priv, lynq_wifi_ap_status_s status);
+int lynq_reg_ap_event_callback(void *priv, AP_CALLBACK_FUNC_PTR cb);
+int lynq_unreg_ap_event_callback(void *priv);
+
+typedef enum {
+    LYNQ_WIFI_STA_STATUS_DISCONNECT = 0,
+    LYNQ_WIFI_STA_STATUS_CONNECT,
+    LYNQ_WIFI_STA_STATUS_SCAN_RESULT,    //finish sta scan 
+}lynq_wifi_sta_status_s;
+typedef enum
+{
+    LYNQ_TIME_OUT = 0,
+    LYNQ_PSW_ERROR,
+    LYNQ_AUTH_ERROR,
+    LYNQ_NOT_FIND_AP,
+}error_number_s;
+
+typedef void(*STA_CALLBACK_FUNC_PTR)(void *priv, lynq_wifi_sta_status_s status, error_number_s number);
+int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb);
+int lynq_unreg_sta_event_callback(void * priv);
+
+//get current ap status
+int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status);
+//get curent sta status
+int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status);
+
+//set the country code 
+int lynq_get_country_code(lynq_wifi_index_e idx, char *country_code);
+//get current country code
+int lynq_set_country_code(lynq_wifi_index_e idx, char *country_code);
+
+//get wlan0/ap0 ip or mac
+int lynq_get_interface_ip(lynq_wifi_index_e idx,char *ip);
+int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac);   
+
+
+//get current connect ap mac rssi band
+int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac);
+int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi);
+int lynq_get_connect_ap_band(lynq_wifi_index_e idx,lynq_wifi_band_m * band);
 
 
 #endif
diff --git a/src/lynq/lib/liblynq-wifi6/libwifi6.c b/src/lynq/lib/liblynq-wifi6/libwifi6.c
index 6e46687..11155d7 100755
--- a/src/lynq/lib/liblynq-wifi6/libwifi6.c
+++ b/src/lynq/lib/liblynq-wifi6/libwifi6.c
@@ -1,20 +1,26 @@
 /**@File lib_wifi6.c
 *  @Brief :about function test
 *  @details :
-*  @Author : qs.xiong
-*  @Date : 2022-3-14
+*  @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 <stdlib.h>
-#include <stdlib.h>
-#include <pthread.h>
-#include <ctype.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" {
@@ -22,1098 +28,2085 @@
 #ifdef __cplusplus
 }
 #endif
-//max len of cmd include password len
-#define MAX_CMD 256    
-#define MAX_RET 1024
+
+#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;
+
+pthread_t g_sta_watcher_pid = 0;
+volatile int g_sta_watcher_stop_flag = 0;
+volatile int g_sta_scan_finish_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 * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
+
+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 CLOSE_FP_RETURN_VALUE(fp,str,value)\
 {\
-	if((pclose((fp)))==-1){ \
-		perror("close fp failed");\		
-	}\	
-	PRINT_AND_RETURN_VALUE((str),(value));\
+    perror((str));\
+    return (value);\
 }
 
-#define CHECK_NULL_RETURN_VALUE(pt,str,value)\
-{\
-	if((pt)==NULL){ \
-		PRINT_AND_RETURN_VALUE((str),(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 = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
+    if (lynq_wpa_ctrl == NULL)
+        return;
+
+    wpa_ctrl_attach(lynq_wpa_ctrl);
+
+    while (g_ap_watcher_stop_flag == 0) {
+        if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
+            usleep(100*1000);
+            continue;
+        }
+        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)
+    wpa_ctrl_detach(lynq_wpa_ctrl);
+    wpa_ctrl_close(lynq_wpa_ctrl);
 }
 
-char g_lynq_wifi_ok[3]="OK";
+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 = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
+    if (lynq_wpa_ctrl == NULL)
+        return;
 
-void StartThread()
-{
-	char start_cmd[120]="wpa_supplicant -iwlan0 -Dnl80211 -c /data/wifi/wg870/wpa_supplicant.conf -g/var/run/wpa_wlan0_cmd  -dd -t -B";
-	char get_pid_cmd[20]="wpa_supplicant";
-	if((GetPIDbyName(get_pid_cmd))>0){
-			return;
-		}
-	system(start_cmd);
-	for(int i=0;i<5;i++){
-		sleep(2);
-		if((GetPIDbyName(get_pid_cmd))>0){
-			break;
-		}
-		if(i==4){
-			return;
-			}
-	}
-    return;
+    wpa_ctrl_attach(lynq_wpa_ctrl);
+
+    while (g_sta_watcher_stop_flag == 0) {
+        if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
+            usleep(100*1000);
+            continue;
+        }
+        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 (memcpy(pReason, "CONN_FAILED", 11) == 0) {
+                        error = LYNQ_TIME_OUT;
+                    }
+                    else if (memcpy(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);
+            }
+        }
+    }
+    wpa_ctrl_detach(lynq_wpa_ctrl);
+    wpa_ctrl_close(lynq_wpa_ctrl);
 }
 
-
-
 int lynq_wifi_enable(void)
 {
-	system("chmod 777 /data/wl");
-	char *lynq_wifi_enable[]={"insmod /lib/modules/4.19.98/kernel/drivers/net/wireless/bcmdhd/bcmdhd.ko "
-			"firmware_path=/system/etc/firmware/wifi/wg870/w870_rtecdc_mfg.trxs "
-			"nvram_path=/system/etc/firmware/wifi/wg870/cyw955572fcipa_rev2.52.txt\n"};
-	if(system(lynq_wifi_enable[0])==-1){
-		return -1;
-		}
+    int ret = 0;
+    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;
+//    }
 
-	return 0;
+    ret = system(cmd_check_service);
+    if (ret != 0) {
+        return -1;
+    }
+
+    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;
+        }
+    }
+
+    return ret;
 }
 
 int lynq_wifi_disable(void)
 {
-	system("rmmod bcmdhd.ko");
+    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;
 }
-/*
-int lynq_wifi_work_mode_set(lynq_wifi_work_mode_m mode)
-{	
-	FILE *fp;
 
-	switch(mode){
-		case LYNQ_WIFI_WORK_MODE_STA:
-			if((fp=popen("wpa_cli -iwlan0 -p/var/run/wpa_supplicant remove_net all","r"))==NULL){
-				perror("popen error!");
-				return -1;
-			}
-			break;
-		case LYNQ_WIFI_WORK_MODE_AP:
-			if((fp=popen("wpa_cli -iwlan0 -p/var/run/wpa_supplicant remove_net all","r"))==NULL){
-				perror("popen error!");
-				return -1;
-			}
-			break;
-		default:
-			return -1;
-		}
-	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;
 }
-*/
-int lynq_wifi_work_mode_get(lynq_wifi_work_mode_m *mode)
-{	
-    char *lynq_wifi_ssid_cmd[]={"wpa_cli -iwlan0 -p/var/run/wpa_supplicant status"};
-	char lynq_cmd_ret[MAX_RET]={0};
-	char lynq_mode[10]={0};
-	char *strcn="mode=";
-	char sta[MAX_CMD]="station";
-	char ap[MAX_CMD]="AP";
-	
-	FILE *fp;		
-	CHECK_NULL_RETURN_VALUE(mode,"mode is null",-1);
-	if ((fp=popen(lynq_wifi_ssid_cmd[0],"r"))==NULL){
-		return -1;
-		}
 
-	while(1){
-        if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-			if((strncmp(lynq_cmd_ret,strcn,5))==0)
-				{
-					sscanf(lynq_cmd_ret,"%*[^=]=%s",lynq_mode);
-					if(strcmp(lynq_mode,sta)==0){
-						*mode=LYNQ_WIFI_WORK_MODE_STA;
-						}else if((strcmp(lynq_mode,ap))==0){
-							*mode=LYNQ_WIFI_WORK_MODE_AP;
-						}
-					}
-			}
-		if(feof(fp)){
-			break;
-			}
-		}
+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';
+    }
 
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
-
-	return 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 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) {
+    int i=0;
+    CHECK_WPA_CTRL(ap_sta);
+    const char *lynq_wifi_add_network = "ADD_NETWORK";
+
+    DO_REQUEST(lynq_wifi_add_network);
+    if (memcpy(cmd_reply, "FAIL", 4) == 0) {
+        return -1;
+    }
+
+    for(int 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;
+        }
+    }
+
+    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_SSID);
+            if (p != NULL) {
+                strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
+                ret = 0;
+                continue;
+            }
+            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_KEY_MGMT);
+            if (p != NULL) {
+                curr_state->ap->auth = convert_auth_from_key_mgmt(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)) != NULL) {
+            ret = 0;
+            curr_state->net_no = atoi(p);
+            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)
 {
-	FILE *fp;
-	char lynq_wifi_ssid_cmd[80]={0};
-	char lynq_tmp_cmd[MAX_CMD]={0};
-	char lynq_cmd_save[]="IFNAME=wlan0 save_config\n";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	CHECK_NULL_RETURN_VALUE(ap_ssid,"ap_ssid is null",-1);
-	if(((strlen(ap_ssid))>32)){
-		PRINT_AND_RETURN_VALUE("ssid is illgal",-1);
-	}
-	
-	sprintf(lynq_tmp_cmd,"%s%d%s","IFNAME=wlan0 set_network ",idx," ssid ");
-	sprintf(lynq_wifi_ssid_cmd,"%s%s%s%s%s",lynq_tmp_cmd,"\"",ap_ssid,"\"","\n");
+    char lynq_wifi_ssid_cmd[80]={0};
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-			perror("popen error!");
-			return -1;
-		}
+    if (ap_ssid == NULL) {
+        printf("ap_ssid is null\n");
+        return -1;
+    }
+    else {
+        printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
+    }
 
-	fwrite(lynq_wifi_ssid_cmd,sizeof(lynq_wifi_ssid_cmd),1,fp);
-	fwrite(lynq_cmd_save,sizeof(lynq_cmd_save),1,fp);
+    if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
+        return -1;
+    }
 
-	int ret=pclose(fp);
-		if(ret==-1){
-			perror("close file faild");
-			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 lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
 {
-	CHECK_NULL_RETURN_VALUE(ap_ssid,"ap_ssid is null",-1);
-	FILE *fp;	
-	char *lynq_wifi_ssid_cmd[]={"wpa_cli -iwlan0 -p/var/run/wpa_supplicant status"};
-	char lynq_cmd_ret[MAX_RET]={0}; 
-	char *strcn="ssid=";
-	if(idx>10||idx<0){
-		return -1;
-		}
-	if((fp=popen(lynq_wifi_ssid_cmd[0],"r"))==NULL){		
-		perror("popen error!"); 			
-		return -1;	
-		}	
-	while(1){		
-		if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){		
-			if((strncmp(lynq_cmd_ret,strcn,5))==0)
-				{	
-					sscanf(lynq_cmd_ret,"%*[^=]=%s",ap_ssid);
-				}
-			}
-		if(feof(fp)){
-			break;
-			}
-		}
-	
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
-	return 0;
+    CHECK_IDX(idx, CTRL_AP);
+    return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
 }
 
 int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
 {
-	FILE *fp;
-	char lynq_wifi_frequency_cmd[MAX_CMD]={0};
-	char lynq_cmd_mode[MAX_CMD]={0};
-	char lynq_cmd_slect[MAX_CMD]={0};
-	char lynq_cmd_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
-		PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
-	}
-	
-	sprintf(lynq_wifi_frequency_cmd,"IFNAME=wlan0 set_net %d frequency %d\n", idx, lynq_wifi_frequency);
-	sprintf(lynq_cmd_mode, "IFNAME=wlan0 set_net %d mode 2\n", idx);
-	sprintf(lynq_cmd_slect, "IFNAME=wlan0 select_net %d\n", idx);
+	char lynq_wifi_frequency_cmd[128]={0};
+	char lynq_cmd_mode[128]={0};
+    char lynq_cmd_slect[128]={0};
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-			perror("popen error!");
-			return -1;
-		}
+    if((lynq_wifi_frequency != 2437) && (lynq_wifi_frequency != 5180)){
+        PRINT_AND_RETURN_VALUE("set frequency not in range",-1);
+    }
 
-	fwrite(lynq_wifi_frequency_cmd,sizeof(lynq_wifi_frequency_cmd),1,fp);
-	fwrite(lynq_cmd_mode,sizeof(lynq_cmd_mode),1,fp);
-	fwrite(lynq_cmd_save,sizeof(lynq_cmd_save),1,fp);
-	fwrite(lynq_cmd_slect,sizeof(lynq_cmd_slect),1,fp);
+    if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
+        return -1;
+    }
 
-	int ret=pclose(fp);
-	if(ret==-1){
-			perror("close file faild");
-			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(lynq_wifi_frequency_cmd);
+    DO_OK_FAIL_REQUEST(lynq_cmd_mode);
+    DO_OK_FAIL_REQUEST(cmd_save_config);
+    DO_OK_FAIL_REQUEST(lynq_cmd_slect);
 
 	return 0;
 }
 
 int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
 {
-	CHECK_NULL_RETURN_VALUE(lynq_wifi_frequency,"frequency is null",-1);
+    char lynq_frequency_str[MAX_RET] = {0};
 
-	FILE *fp;
-	char lynq_wifi_ssid_cmd[MAX_CMD]="wpa_cli -iwlan0 -p/var/run/wpa_supplicant status";
-	char lynq_cmd_ret[MAX_RET]={0};
-	char *strcn="freq=";
-	char freq[MAX_CMD]={0};
-	if(idx<0||idx>9){
-		return -1;
-		}
-	
-	if((fp=popen(lynq_wifi_ssid_cmd,"r"))==NULL){
-		perror("popen error!");
-		return -1;
-		}
-	while(1){
-		if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-			if((strncmp(lynq_cmd_ret,strcn,5))==0)
-				{
-					sscanf(lynq_cmd_ret,"%*[^=]=%s",freq);
-					*lynq_wifi_frequency=atoi(freq);
-				}
-			}
-		if(feof(fp)){
-			break;
-			}
-		}
+    CHECK_IDX(idx, CTRL_AP);
 
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
+    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;
+    return 0;
 }
-*/
+
 int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
 {
-	if(bandwidth==NULL){
-		PRINT_AND_RETURN_VALUE("call falure",-1);
-	}
-	FILE *fp;
-	char aim[MAX_CMD]={0};
-	char number[10]="80";
-	char lynq_wifi_bandwidth_cmd[MAX_CMD]="/data/wl chanspec\n";
-	char lynq_cmd_ret[MAX_CMD]={0};
-	if(idx<0||idx>9){
-		return -1;
-		}
-	
-	if((fp=popen(lynq_wifi_bandwidth_cmd,"r"))==NULL){
-		perror("popen error!");
-		return -1;
-		}
-	
-	if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-		strncpy(aim,lynq_cmd_ret+3,2);
-		if((strncmp(aim,number,2))==0){
-			*bandwidth=1;
-		}else{
-			*bandwidth=0;
-			}
-		}
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
+    int count = 0;
+    int index = 0;
+    char *split_words[128] = {0};
+    const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
 
-	return 0;
+    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)
 {
-	FILE *fp;
-	int ret;
-	char lynq_cmd_ret[MAX_RET]="";
-	char lynq_cmd_channel[MAX_CMD]="";
-	
-	if(idx<0||idx>9){
-		return -1;
-		}
-	
-	sprintf(lynq_cmd_channel,"/data/wl chanspec %d\n",channel);
-	system("/data/wl down");
+    char lynq_cmd_channel[MAX_CMD]={0};
 
-	fp=popen(lynq_cmd_channel,"r");
-	if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-		if(strncmp(lynq_cmd_ret,g_lynq_wifi_ok,2) != 0){
-			PRINT_AND_RETURN_VALUE("set channel error",-1);
-			}
-		system("/data/wl up");
-		}
-	
-	return 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)
 {
-	CHECK_NULL_RETURN_VALUE(channel,"channel is null",-1);
-	FILE *fp;
-	char lynq_cmd_set[MAX_CMD]={0};
-	char channel_str[MAX_CMD]={0};
-	char lynq_cmd_ret[MAX_RET]={0};
-	if(idx<0||idx>9){
-		return -1;
-		}
-			
-	if((fp=popen("/data/wl chanspec","r"))==NULL){
-		perror("popen error!");
-		return -1;
-		}
+    int count = 0;
+    int index = 0;
+    char *split_words[128] = {0};
+    char lynq_chanspec_cmd[]="DRIVER chanspec\n";
 
-	if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-		strncpy(channel_str,lynq_cmd_ret,2);
-		*channel=atoi(channel_str);
-		}
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
+    CHECK_IDX(idx, CTRL_AP);
 
-	return 0;
+    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_ap_auth_s auth)
+int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
 {
-	FILE *fp;
-	int ret;
-	if(idx<0||idx>9){
-		return -1;
-		}
-	if((auth !=0) && (auth !=1)){
-        PRINT_AND_RETURN_VALUE("set auth is null",-1);
-		}
+    CHECK_IDX(idx, CTRL_AP);
+
+    if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
+        return -1;
+    }
+
+    CHECK_WPA_CTRL(CTRL_AP);
+
 	switch(auth){
 		case LYNQ_WIFI_AUTH_OPEN:
 		{
-			char lynq_auth_cmd[MAX_CMD]={0};
-			char lynq_auth_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-			char lynq_cmd_quit[12]="quit\n";
-			sprintf(lynq_auth_cmd,"IFNAME=wlan0 set_net %d key_mgmt NONE\n",idx);
-			
-			if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-				perror("popen error!");
-				return -1;
-			}
+            char lynq_auth_cmd[64]={0};
+            sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
 
-			fwrite(lynq_auth_cmd,sizeof(lynq_auth_cmd),1,fp);
-			fwrite(lynq_auth_save,sizeof(lynq_auth_save),1,fp);
-			fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
-			
-			ret=pclose(fp);
-			if(ret==-1){
-				perror("close file faild");
-				return -1;
-				}
+            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:
 		{
 
-			char lynq_auth_cmd[MAX_CMD]={0};
-			char lynq_psk_cmd[MAX_CMD]={0};
-			char lynq_pairwise_cmd[MAX_CMD]={0};
-			char lynq_auth_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-			char lynq_cmd_quit[12]="quit\n";
+			char lynq_auth_cmd[64]={0};
+			char lynq_psk_cmd[64]={0};
+            char lynq_pairwise_cmd[64]={0};
 
-			sprintf(lynq_auth_cmd,"IFNAME=wlan0 set_net %d  proto WPA2\n",idx);
-			sprintf(lynq_psk_cmd,"IFNAME=wlan0 set_net %d key_mgmt WPA-PSK\n",idx);
-			sprintf(lynq_pairwise_cmd,"IFNAME=wlan0 set_net %d pairwise CCMP\n",idx);
-			
-			if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-				perror("popen error!");
-				return -1;
-			}
+            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 WPA2", AP_NETWORK_0);
+                sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA2-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);
 
-			fwrite(lynq_auth_cmd,sizeof(lynq_auth_cmd),1,fp);
-			fwrite(lynq_psk_cmd,sizeof(lynq_psk_cmd),1,fp);
-			fwrite(lynq_pairwise_cmd,sizeof(lynq_pairwise_cmd),1,fp);
-			fwrite(lynq_auth_save,sizeof(lynq_auth_save),1,fp);
-			fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
-		
-			ret=pclose(fp);
-			if(ret==-1){
-				perror("close file faild");
-				return -1;
-			}
+            DO_OK_FAIL_REQUEST(lynq_auth_cmd);
+            DO_OK_FAIL_REQUEST(lynq_psk_cmd);
+            DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
+            DO_OK_FAIL_REQUEST(cmd_save_config);
 			break;
-		}
+		}		
 		default:
+        {
+            printf("auth type [%d] not support now\n", auth);
 			return -1;
-			}
+        }
+    }
 
 	return 0;
 }
 
-int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_ap_auth_s *auth)
+int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
 {
-	CHECK_NULL_RETURN_VALUE(auth,"the auth is null",-1);
-	char *lynq_wifi_ssid_cmd[]={"wpa_cli -iwlan0 -p/var/run/wpa_supplicant status"};
-	char lynq_cmd_ret[MAX_RET]={0};
-	char get_str_auth[20]={0};
-	char use_to_cmp[4]="WPA";
-	char strcn[20]="key_mgmt=";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	
-	FILE *fp;
-	if((fp=popen(lynq_wifi_ssid_cmd[0],"r"))==NULL){
-		perror("popen error!");
-		return -1;
-		}
-	while(1){
-		if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-			if((strncmp(lynq_cmd_ret,strcn,9))==0)
-				{
-					sscanf(lynq_cmd_ret,"%*[^=]=%s",get_str_auth);
-					if((strncmp(get_str_auth,use_to_cmp,3))==0){
-						*auth=LYNQ_WIFI_AUTH_WPA_PSK;
-						break;
-						}
-				}
-			}
-		if(feof(fp)){
-			break;
-			}
-		}
-	if(*auth!=LYNQ_WIFI_AUTH_WPA_PSK){
-		*auth=LYNQ_WIFI_AUTH_OPEN;
-		}
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
+    char lynq_auth_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) {
+        *auth = LYNQ_WIFI_AUTH_OPEN;
+    }
+    else {
+        *auth = LYNQ_WIFI_AUTH_WPA_PSK;
+    }
 
 	return 0;
 }
 
-int lynq_wifi_ap_max_sta_get(lynq_wifi_index_e idx,int* max_sta_num)
-{
-	CHECK_NULL_RETURN_VALUE(max_sta_num,"max_sta_num is null",-1);
-	FILE *fp;
-	char get_str_num[5]={0};
-	char lynq_cmd_ret[MAX_RET]={0};
-	if(idx<0||idx>9){
-		return -1;
-		}
-	if((fp=popen("/data/wl maxassoc","r"))==NULL)
-		{
-			perror("popen error!");
-			return -1;
-		}
-	if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
-	{
-		perror("fread fail!");
-		return -1;
-	}
-	strcpy(get_str_num,lynq_cmd_ret);
-	*max_sta_num=atoi(get_str_num);
 
-	int ret=pclose(fp);
-	if(ret==-1)
-	{
-		perror("close file faild");
-		return -1;
-	}
-
-	return 0;
-}
 
 int lynq_wifi_ap_start(lynq_wifi_index_e idx)
 {
-	FILE *fp;
-	char lynq_wifi_cmd[MAX_CMD]={0};
-	char lynq_cmd_quit[MAX_CMD]="quit";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	sprintf(lynq_wifi_cmd,"IFNAME=wlan0 select_net %d\n",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";
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL)
-		{
-			perror("popen error!");
-			return -1;
-		}
-	
-	fwrite(lynq_wifi_cmd,sizeof(lynq_wifi_cmd),1,fp);
-	fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
-	
-	int ret=pclose(fp);
-	if(ret==-1)
-		{
-			perror("close file faild");
-			return -1;
-		}
+    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)
 {
-	int lynq_stop;
-	int lynq_start;
-	if(idx<0||idx>9){
-		return -1;
-		}
-	lynq_stop = lynq_wifi_ap_stop(idx);
-	if ( lynq_stop != 0 )
-	{
-		return -1;
-	}
-
-	lynq_start = lynq_wifi_ap_start(idx);
-	if ( lynq_start != 0 )
-	{
-		return -1;
-	}
-
-	return 0;
-
+    return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx)  : -1;
 }
 
 int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
 {
-	FILE *fp;
-	char lynq_wifi_cmd[MAX_CMD]={0};
-	char lynq_cmd_quit[MAX_CMD]="quit";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	sprintf(lynq_wifi_cmd,"IFNAME=wlan0 disable_net %d\n",idx);
+    char LYNQ_WIFI_CMD[128]={0};
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL)
-		{
-			perror("popen error!");
-			return -1;
-		}
+    CHECK_IDX(idx, CTRL_AP);
 
-	fwrite(lynq_wifi_cmd,sizeof(lynq_wifi_cmd),1,fp);
-	fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
+    CHECK_WPA_CTRL(CTRL_AP);
 
-	int ret=pclose(fp);
-	if(ret==-1)
-		{
-			perror("close file faild");
-			return -1;
-		}
+    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)
 {
-	FILE *fp;
-	char lynq_disable_cmd[MAX_CMD]={0};
-	char lynq_select_cmd[MAX_CMD]={0};
-	char lynq_hide_cmd[]="IFNAME=wlan0 SET HIDE_SSID 1\n";
-	if(idx<0||idx>9){
-		return -1;
-		}
+    char lynq_disable_cmd[128] = {0};
+    char lynq_select_cmd[128] = {0};
+    const char *lynq_hide_cmd = "SET HIDE_SSID 1";
 
-	sprintf(lynq_disable_cmd,"IFNAME=wlan0 disable_net %d\n",idx);
-	sprintf(lynq_select_cmd,"IFNAME=wlan0 select_net %d\n",idx);
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-		perror("popen error!");
-		return -1;
-	}
+    CHECK_IDX(idx, CTRL_AP);
 
-	fwrite(lynq_disable_cmd,sizeof(lynq_disable_cmd),1,fp);
-	fwrite(lynq_hide_cmd,sizeof(lynq_hide_cmd),1,fp);
-	fwrite(lynq_select_cmd,sizeof(lynq_select_cmd),1,fp);
+    CHECK_WPA_CTRL(CTRL_AP);
 	
-	int ret=pclose(fp);
-	if(ret==-1){
-		perror("close file faild");
-		return -1;
-		}	
+    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)
 {
-	FILE *fp;
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-		perror("popen error!");
-		return -1;
-	}
-	char lynq_disable_cmd[MAX_CMD]={0};
-	char lynq_select_cmd[MAX_CMD]={0};
-	char lynq_unhide_cmd[MAX_CMD]="IFNAME=wlan0 SET HIDE_SSID 0\n";
-	if(idx<0||idx>9){
-		return -1;
-		}
+    char lynq_disable_cmd[128] = {0};
+    char lynq_select_cmd[128] = {0};
+    const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
 
-	sprintf(lynq_disable_cmd,"IFNAME=wlan0 disable_net %d\n",idx);
-	sprintf(lynq_select_cmd,"IFNAME=wlan0 select_net %d\n",idx);
+    CHECK_IDX(idx, CTRL_AP);
 
-	fwrite(lynq_disable_cmd,sizeof(lynq_disable_cmd),1,fp);
-	fwrite(lynq_unhide_cmd,sizeof(lynq_unhide_cmd),1,fp);
-	fwrite(lynq_select_cmd,sizeof(lynq_select_cmd),1,fp);
+    CHECK_WPA_CTRL(CTRL_AP);
 
-	int ret=pclose(fp);
-	if(ret==-1){
-		perror("close file faild");
-		return -1;
-		}
+    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_ssid_pass_set(lynq_wifi_index_e idx,char *password)
+int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
 {
-	FILE *fp;
-	int ret;
 	int pass_len;
-	CHECK_NULL_RETURN_VALUE(password,"password is null",-1);
+    char lynq_tmp_cmd[300]={0};
 	pass_len=strlen(password);
-	if(((pass_len+1) > MAX_CMD) || (pass_len<8)){
-		PRINT_AND_RETURN_VALUE("password is illgal",-1);
-	}else if((lynq_get_wifi_space(password))== 1){
-		PRINT_AND_RETURN_VALUE("password is illgal",-1);
-	}
-	if(idx<0||idx>9){
+    if(pass_len < 8 || pass_len >= 64){
 		return -1;
-		}
-	char lynq_tmp_cmd[300]={0};
-	char lynq_pass_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-	char lynq_cmd_quit[12]="quit\n";
-	sprintf(lynq_tmp_cmd,"%s%d%s%s%s%s%s","IFNAME=wlan0 set_net ",idx," psk ","\"",password,"\"","\n");
+    }
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-			perror("popen error!");
-			return -1;
-		}
-	
-	fwrite(lynq_tmp_cmd,sizeof(lynq_tmp_cmd),1,fp);
-	fwrite(lynq_pass_save,sizeof(lynq_pass_save),1,fp);
-	fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
+    CHECK_IDX(idx, CTRL_AP);
 
-	ret=pclose(fp);
-	if(ret==-1){
-		perror("close file faild");
-		return -1;
-	}
+    CHECK_WPA_CTRL(CTRL_AP);
+
+    sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
+
+    DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
+    DO_OK_FAIL_REQUEST(cmd_save_config);
 
 	return 0;
 }
 
-int lynq_sta_ssid_pass_set(lynq_wifi_index_e idx,char *password)
+int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
 {
-	FILE *fp;
-	int pass_len;
-	CHECK_NULL_RETURN_VALUE(password,"password is null",-1);
-	pass_len=strlen(password);
-	if(((pass_len+1) > MAX_CMD) && (pass_len<8)){
-		PRINT_AND_RETURN_VALUE("password is illgal",-1);
-	}else if((lynq_get_wifi_space(password))== 1){
-		PRINT_AND_RETURN_VALUE("password is illgal",-1);
-	}
-	if(idx<0||idx>9){
-		return -1;
-		}
-	char lynq_tmp_cmd[MAX_CMD]={0};
-	char lynq_pass_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-	char lynq_cmd_quit[12]="quit\n";
-	sprintf(lynq_tmp_cmd,"%s%d%s%s%s%s%s","IFNAME=wlan0 set_net ",idx," psk ","\"",password,"\"","\n");
+    FILE * fp;
+    int len, ret;
+    int count, index;
+    char *split_lines[128] = {0};
+    char *buff, *p;
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-			perror("popen error!");
-			return -1;
-		}
+    CHECK_IDX(idx, CTRL_AP);
 
-	fwrite(lynq_tmp_cmd,sizeof(lynq_tmp_cmd),1,fp);
-	fwrite(lynq_pass_save,sizeof(lynq_pass_save),1,fp);
-	fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
+    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;
+    }
 
-	int ret=pclose(fp);
-	if(ret==-1){
-		perror("close file faild");
-		return -1;
-	}
+    buff = alloca(MAX_RET);
+    fseek(fp, 0, SEEK_SET);
+    len = fread(buff, 1, MAX_RET, fp);
+    fclose(fp);
 
-	return 0;
+    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) {
+            continue;
+        }
+        p += 4;
+        if (*p == '\"') {
+            p++;
+        }
+
+        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};
 
-int lynq_wifi_sta_ssid_set(lynq_wifi_index_e idx,char *sta_ssid)
+    if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
+        return -1;
+    }
+
+    *auth = convert_auth_from_key_mgmt(lynq_auth_str);
+    return 0;
+}
+
+int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
 {
-	FILE *fp;
+    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;
+    int count, index;
+    char *split_lines[128] = {0};
+    char *buff, *p;
+
+    CHECK_IDX(idx, CTRL_STA);
+
+    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;
+             }
+             len = buff + index - p;
+        }
+
+        if (strstr(p, ap->ap_ssid) != NULL) {
+            break;
+        }
+    }
+
+    if (index >= len) {
+        return -1;
+    }
+
+    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) {
+            continue;
+        }
+        p += 4;
+        if (*p == '\"') {
+            p++;
+        }
+
+        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};
-	char lynq_tmp_cmd[MAX_CMD]={0};
-	char lynq_cmd_save[]="IFNAME=wlan0 save_config\n";
-	sprintf(lynq_tmp_cmd,"%s%d%s","IFNAME=wlan0 set_network ",idx," ssid ");
-	sprintf(lynq_wifi_ssid_cmd,"%s%s%s%s%s",lynq_tmp_cmd,"\"",sta_ssid,"\"","\n");
-	if(idx<0||idx>9){
-		return -1;
-		}
-	CHECK_NULL_RETURN_VALUE(sta_ssid,"sta_ssid is null",-1);
-	if(((strlen(sta_ssid))>32)){
-		PRINT_AND_RETURN_VALUE("ssid is illgal",-1);
-	}
-	
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-			perror("popen error!");
-			return -1;
-		}
 
-	fwrite(lynq_wifi_ssid_cmd,sizeof(lynq_wifi_ssid_cmd),1,fp);
-	fwrite(lynq_cmd_save,sizeof(lynq_cmd_save),1,fp);
-
-	int ret=pclose(fp);
-		if(ret==-1){
-			perror("close file faild");
-			return -1;
-		}
-
-	return 0;
-}
-
-int lynq_wifi_sta_ssid_get(lynq_wifi_index_e idx,char* sta_ssid)
-{
-	CHECK_NULL_RETURN_VALUE(sta_ssid,"ap_ssid is null",-1);
-	FILE *fp;	
-	char *lynq_wifi_ssid_cmd[]={"wpa_cli -iwlan0 -p/var/run/wpa_supplicant status"};
-	char lynq_cmd_ret[MAX_RET]={0}; 
-	char *strcn="ssid=";
-	if(idx>10||idx<0){
+	if (sta_ssid == NULL) {
+		printf("sta_ssid is null\n");
 		return -1;
-		}
-	if((fp=popen(lynq_wifi_ssid_cmd[0],"r"))==NULL){		
-		perror("popen error!"); 			
-		return -1;
-		}
-	while(1){
-		if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-			if((strncmp(lynq_cmd_ret,strcn,5))==0)
-				{
-					sscanf(lynq_cmd_ret,"%*[^=]=%s",sta_ssid);
-				}
-			}
-		if(feof(fp)){
-			break;
-			}
-		}
-	
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		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;
 
 }
 
-int lynq_wifi_sta_auth_set(lynq_wifi_index_e idx, lynq_wifi_sta_auth_s auth)
+static int inner_sta_start_stop(int net_no, int start_flag, int save)
 {
-	FILE *fp;
-	int ret;
-	if(idx<0||idx>9){
-		return -1;
-		}
-	PRINT_AND_RETURN_VALUE("set auth is error",auth);
-	if((auth !=0) && (auth !=1)){
-        PRINT_AND_RETURN_VALUE("set auth is error",-1);
-		}
+    char lynq_disable_cmd[128]={0};
+    char lynq_select_cmd[128]={0};
+
+    CHECK_WPA_CTRL(CTRL_STA);
+
+    if (save != 0) {
+        DO_OK_FAIL_REQUEST(cmd_save_config);
+    }
+
+    if (start_flag == 0) {
+        sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", net_no);
+        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);
+
+	return inner_get_param(CTRL_STA, idx, "ssid", sta_ssid);
+}
+
+int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
+{
+    scan_info_s *scan_list;
+    saved_ap_info_s *save_list;
+    int scan_len=0;
+    int save_len=0;
+    int best_index = -1;
+    int best_scan_index = -1;
+    int best_rssi = 0;
+    int i, j;
+
+    CHECK_IDX(idx, CTRL_STA);
+    if (info == NULL) {
+        return -1;
+    }
+
+    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)) {
+        memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
+        info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
+        lynq_get_connect_ap_rssi(idx, &info->rssi);
+        return 0;
+    }
+
+    if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
+        return -1;
+    }
+
+    if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
+        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_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;
+        return 0;
+    }
+
+    return -1;
+}
+
+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_STA_AUTH_OPEN:
-		{
-			char lynq_auth_cmd[MAX_CMD]={0};
-			char lynq_auth_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-			char lynq_cmd_quit[12]="quit\n";
-			sprintf(lynq_auth_cmd,"IFNAME=wlan0 set_net %d key_mgmt NONE\n",idx);
+		case LYNQ_WIFI_AUTH_OPEN:
+        {
+            sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
 
-			if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-				perror("popen error!");
-				return -1;
-			}
-
-			fwrite(lynq_auth_cmd,sizeof(lynq_auth_cmd),1,fp);
-			fwrite(lynq_auth_save,sizeof(lynq_auth_save),1,fp);
-			fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
-
-			ret=pclose(fp);
-			if(ret==-1){
-				perror("close file faild");
-				return -1;
-				}
+            DO_OK_FAIL_REQUEST(lynq_auth_cmd);
+//            DO_OK_FAIL_REQUEST(cmd_save_config);
 			break;
 		}
-		case LYNQ_WIFI_STA_AUTH_WPA_PSK:
+		case LYNQ_WIFI_AUTH_WPA_PSK:
+        case LYNQ_WIFI_AUTH_WPA2_PSK:
 		{
-			char lynq_auth_cmd[MAX_CMD]={0};
-			char lynq_psk_cmd[MAX_CMD]={0};
-			char lynq_pairwise_cmd[MAX_CMD]={0};
-			char lynq_auth_save[MAX_CMD]="IFNAME=wlan0 save_config\n";
-			char lynq_cmd_quit[12]="quit\n";
+            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 WPA2", 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);
 
-			sprintf(lynq_auth_cmd,"IFNAME=wlan0 set_net %d  proto WPA2\n",idx);
-			sprintf(lynq_psk_cmd,"IFNAME=wlan0 set_net %d key_mgmt WPA-PSK\n",idx);
-			sprintf(lynq_pairwise_cmd,"IFNAME=wlan0 set_net %d pairwise CCMP\n",idx);
-			
-			if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL){
-				perror("popen error!");
-				return -1;
-			}
+            DO_OK_FAIL_REQUEST(lynq_auth_cmd);
+            DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
+            DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
 
-			fwrite(lynq_auth_cmd,sizeof(lynq_auth_cmd),1,fp);
-			fwrite(lynq_psk_cmd,sizeof(lynq_psk_cmd),1,fp);
-			fwrite(lynq_pairwise_cmd,sizeof(lynq_pairwise_cmd),1,fp);
-			fwrite(lynq_auth_save,sizeof(lynq_auth_save),1,fp);
-			fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
+            if (password != NULL) {
+                sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
+                DO_OK_FAIL_REQUEST(lynq_psk_cmd);
+            }
 
-			ret=pclose(fp);
-			if(ret==-1){
-				perror("close file faild");
-				return -1;
-			}
+//            DO_OK_FAIL_REQUEST(cmd_save_config);
 			break;
-		}
+		}		
 		default:
 			return -1;
-			}
+    }
 
 	return 0;
 }
 
-int lynq_wifi_sta_auth_get(lynq_wifi_index_e idx, lynq_wifi_sta_auth_s* auth)
+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)
 {
-	CHECK_NULL_RETURN_VALUE(auth,"the auth is null",-1);	
-	char *lynq_wifi_ssid_cmd[]={"wpa_cli -iwlan0 -p/var/run/wpa_supplicant status"};
-	char lynq_cmd_ret[MAX_RET]={0};
-	char get_str_auth[20]={0};
-	char use_to_cmp[4]="WPA";
-	char strcn[20]="key_mgmt=";
+    int net_no;
+    CHECK_IDX(idx, CTRL_STA);
 
-	FILE *fp;
-	if((fp=popen(lynq_wifi_ssid_cmd[0],"r"))==NULL){
-		perror("popen error!");
-		return -1;	
-		}
-	while(1){
-		if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-			if((strncmp(lynq_cmd_ret,strcn,9))==0)
-				{
-					sscanf(lynq_cmd_ret,"%*[^=]=%s",get_str_auth);
-					if((strncmp(get_str_auth,use_to_cmp,3))==0){
-						*auth=LYNQ_WIFI_AUTH_WPA_PSK;
-						break;
-						}
-				}
-			}
-		if(feof(fp)){
-			break;
-			}
-		}
-	if(*auth!=LYNQ_WIFI_AUTH_WPA_PSK){
-		*auth=LYNQ_WIFI_AUTH_OPEN;
-		}
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
+    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)
+int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
 {
-	FILE *fp;
-	char lynq_wifi_cmd[MAX_CMD]={0};
-	char lynq_cmd_quit[MAX_CMD]="quit\n";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	sprintf(lynq_wifi_cmd,"IFNAME=wlan0 select_net %d\n",idx);
+    int count, net_no, index;
+    int net_no_list[128];
+    lynq_wifi_auth_s net_auth;
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL)
-		{
-			perror("popen error!");
-			return -1;
-		}
+    if (ssid == NULL || *ssid == '\0') {
+        printf("bad ssid\n");
+        return -1;
+    }
 
-	fwrite(lynq_wifi_cmd,sizeof(lynq_wifi_cmd),1,fp);
-	fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
+    if (LYNQ_WIFI_AUTH_OPEN != auth) {
+        if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
+            printf("bad password\n");
+            return -1;
+        }
+    }
 
-	int ret=pclose(fp);
-	if(ret==-1)
-		{
-			perror("close file faild");
-			return -1;
-		}
-	return 0;
+    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)
+int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
 {
-	FILE *fp;
-	char lynq_wifi_cmd[MAX_CMD]={0};
-	char lynq_cmd_quit[MAX_CMD]="quit\n";
-	if(idx<0||idx>9){
-		return -1;
-		}
-	sprintf(lynq_wifi_cmd,"IFNAME=wlan0 disable_net %d\n",idx);
+    ap_info_s ap;
+    curr_status_info curr_state;
+    ap.ap_ssid[0] = '\0';
 
-	if((fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w"))==NULL)
-		{
-			perror("popen error!");
-			return -1;
-		}
+    if (ssid == NULL || *ssid == '\0') {
+        return -1;
+    }
 
-	fwrite(lynq_wifi_cmd,sizeof(lynq_wifi_cmd),1,fp);
-	fwrite(lynq_cmd_quit,sizeof(lynq_cmd_quit),1,fp);
+    CHECK_IDX(idx, CTRL_STA);
 
-	int ret=pclose(fp);
-	if(ret==-1)
-		{
-			perror("close file faild");
-			return -1;
-		}
+    curr_state.ap = &ap;
+    if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
+        return 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_wpa_start(void){
-	int ret;
-	pthread_t pth;
+int lynq_wifi_sta_start(lynq_wifi_index_e idx) {
 
-	ret=pthread_create(&pth,NULL,StartThread,NULL);
-	if(ret<0){
-		return -1;
-		}
-	pthread_join(pth,NULL);
+    CHECK_IDX(idx, CTRL_STA);
+    if (system("ifconfig | grep wlan0") != 0) {
+        return -1;
+    }
 
-	return 0;
+    return system("connmanctl enable wifi");
 }
 
-int lynq_wifi_wpacli_start(void){
-	FILE *fp;
-	int i=0;
-	char lynq_cmd_ret[MAX_RET]={0};
-	char add_cmd[MAX_CMD]="IFNAME=wlan0 add_net\n";
-	char save_cmd[MAX_CMD]="IFNAME=wlan0 save_config\n";
-	char exit_cmd[20]="quit\n";
-
-	if(lynq_get_idx_number() == 1){
-		PRINT_AND_RETURN_VALUE("don't need add idx agin\n",0);
-	}
-	fp=popen("/usr/bin/wpa_cli -iwpa_wlan0_cmd -p/var/run/","w");
-	if(fp==NULL){
-		perror("start wpa_supplicant faild!");
-		return -1;
-		}
-
-	for(i=0;i<10;i++){
-		fwrite(add_cmd,sizeof(add_cmd),1,fp);
-		}
-	fwrite(save_cmd,sizeof(save_cmd),1,fp);
-	fwrite(exit_cmd,sizeof(exit_cmd),1,fp);
-
-	int ret=pclose(fp);
-	if(ret==-1)
-	{
-		perror("close file faild");
-		return -1;
-	}
-
- 	return 0;
-}
-
-pid_t GetPIDbyName(char* ps_name)
+int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
 {
-	FILE *fp;
-	char *cmd=(char*)calloc(1,200);
-	sprintf(cmd,"pidof %s",ps_name);
-	fp=popen(cmd,"r");
-	fread(cmd,1,200,fp);
-	fclose(fp);
-	return atoi(cmd);
+    CHECK_IDX(idx, CTRL_STA);
+
+    return system("connmanctl disable wifi");
 }
 
-int lynq_get_idx_number(void)
+//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)
 {
-	FILE *fp;
-	const char* number="9";
-	char lynq_wifi_ssid_cmd[MAX_CMD]="wpa_cli -iwlan0 -p/var/run/wpa_supplicant list_net";
-	char lynq_cmd_ret[MAX_RET]={0};
+    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};
 
-	if((fp=popen(lynq_wifi_ssid_cmd,"r"))==NULL){
-		perror("popen error!");	
-		return -1;
-		}	
-	while(1){		
-		if((fgets(lynq_cmd_ret,sizeof(lynq_cmd_ret),fp))!=NULL){
-			if((strncmp(lynq_cmd_ret,number,1))==0)
-			{
-				CLOSE_FP_RETURN_VALUE(fp,"don't need add idx",1);
-			}
-		}
-		if(feof(fp)){
-			break;
-			}
-		}
+    CHECK_IDX(idx, CTRL_AP);
 
-	if((pclose(fp))==-1){
-		perror("close file faild");
-		return -1;
-		}
+    CHECK_WPA_CTRL(CTRL_AP);
 
-	return 0;
+//    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 || memcpy(cmd_reply, "FAIL", 4) == 0) {
+            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]);
+    }
+
+    return 0;
 }
 
-
-int lynq_get_wifi_mac(char* wifi_mac)
+int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
 {
-	FILE * fp;
-	char lynq_cmd_ret[MAX_RET]={0};
-	char lynq_tmp_cmd[MAX_CMD]={0};	
+    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((fp=popen("/data/wl cur_etheraddr","r"))==NULL)
-	{
-		perror("popen error!");
-		return -1;
-	}
-	if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
-	{
-		perror("fread fail!");
-		return -1;
-	}
-	strncpy(wifi_mac,lynq_cmd_ret+14,18);
-	int ret=pclose(fp);
-	if(ret==-1)
-	{
-		perror("close file faild");
-		return -1;
-	 }
+    if (list == NULL || len == NULL) {
+        return -1;
+    }
 
-	return 0;
+    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_get_wifi_space(char* password){
-	int i;
-	for(i=0;password[i];i++){
-		if(isspace(password[i])){
-			return 1;
-		}
-	}
 
-	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;
+    int net_no_list[128];
+    char freq[16];
+
+    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);
+    *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);
+        printf("to get bssid\n");
+        inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
+        printf("to get inner_get_network_auth\n");
+        inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
+        printf("to get frequency\n");
+        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;
+        }
+
+    }
+
+    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);
+
+    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];
+    const char * STATE_COMPLETED = "COMPLETED";
+    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];
+    const char * STATE_COMPLETED = "COMPLETED";
+    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, *ifa;
+   int family, s;
+   char host[NI_MAXHOST];
+   const char * ifaName = "wlan0";
+   if (idx == 1) {
+       ifaName = "ap0";
+   }
+   else if (idx != 0) {
+       return -1;
+   }
+
+   if (getifaddrs(&ifaddr) == -1)
+   {
+       perror("getifaddrs");
+       return -1;
+       //exit(EXIT_FAILURE);
+   }
+
+
+   for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
+   {
+       if (ifa->ifa_addr == NULL)
+           continue;
+       host[0] = '\0';
+       s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
+
+       printf("inter face %s-%s, ip %s, %d - %d \n", ifa->ifa_name, ifaName, host,ifa->ifa_addr->sa_family, AF_INET);
+       if((strcmp(ifa->ifa_name,ifaName)==0))
+       {
+           if (s != 0)
+           {
+               //  printf("getnameinfo() failed: %s", gai_strerror(s));
+               //exit(EXIT_FAILURE);
+           }
+           freeifaddrs(ifaddr);
+           strcpy(ip, host);
+           printf("ip %s\n", ip);
+           return 0;
+           //printf("	Interface : <%s>",ifa->ifa_name );
+           //printf("	  Address : <%s>", host);
+       }
+   }
+   return -1;
+}
+
+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);
+
+    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;
+    }
+    *rssi = atoi(lynq_cmd_ret);
+
+    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;
 }
diff --git a/src/lynq/lib/liblynq-wifi6/makefile b/src/lynq/lib/liblynq-wifi6/makefile
index 9b7e727..e7a3ca8 100755
--- a/src/lynq/lib/liblynq-wifi6/makefile
+++ b/src/lynq/lib/liblynq-wifi6/makefile
@@ -25,8 +25,7 @@
     -L./lib \
     -llog \
     -lpthread \
-    -lstdc++ \
-    -lbootctrl  \
+    -lwpa_client
 
 SOURCES = $(wildcard *.c wildcard *.h )