#include "sta_cli.h"
#include "lynq_wifi.h"
#include "mbtk_log.h"
#include "mbtk_wifi_ap.h"


#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#define STA_BUF_SIZE 2048
#define PATH_MAX_LEN 64
#define DEF_INTERFACE "wlan0"
static lynq_wifi_event_handle_sta g_event_handle_sta_cb = NULL;
static lynq_wifi_status_e g_ap_status = LYNQ_WIFI_AP_STATUS_NONE;



int qser_wifi_enable(void)
{
    static char sta_cli_buf[STA_BUF_SIZE] = "OPEN";
    char reply[STA_BUF_SIZE];
    if(sta_cli_cmd_parse(sta_cli_buf, reply, STA_BUF_SIZE))
    {
        if(strlen(reply) > 0)
        {
             LOGE("reply data(%s).\n",reply);
        }
        else
        {
            LOGE("No reply data(%s).\n",sta_cli_buf);
            return LYNQ_ERR_FAIL;
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
        return LYNQ_ERR_FAIL;
    }

    g_ap_status = LYNQ_WIFI_AP_STATUS_ENABLING;
    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_disable(void)
{
    static char sta_cli_buf[STA_BUF_SIZE] = "CLOSE";
    char reply[STA_BUF_SIZE];
    if(sta_cli_cmd_parse(sta_cli_buf, reply, STA_BUF_SIZE)){
        if(strlen(reply) > 0)
        {
           LOGE("reply data(%s).\n",reply);
        }
        else
        {
           LOGE("No reply data(%s).\n",sta_cli_buf);
           return LYNQ_ERR_FAIL;
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
        return LYNQ_ERR_FAIL;
    }

    g_ap_status = LYNQ_WIFI_AP_STATUS_IDLE;
    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_work_mode_set(lynq_wifi_work_mode_e type)
{
    int ret = 0;
     if(LYNQ_WIFI_WORK_MODE_AP0 == type)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "hw_mode", "g");
    }
    else
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "hw_mode", "a");
    }

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_work_mode_get(lynq_wifi_work_mode_e *type)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == type)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }
    
    ret = mbtk_wifi_get_setting(SETTING_FILE, "hw_mode", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    if('g' == value[0])
    {
        *type = LYNQ_WIFI_AP_INDEX_AP0;
    }
    else
    {
        *type = LYNQ_WIFI_AP_INDEX_AP1;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_get_status(lynq_wifi_ap_index_e idx, lynq_wifi_ap_status_t* ap_stat)
{
    if(NULL == ap_stat)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    int ret = mbtk_wifi_get_setting(SETTING_FILE, "ssid", ap_stat->bssid, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    ap_stat->status = g_ap_status;
    memcpy(ap_stat->ifname, DEF_INTERFACE, strlen(DEF_INTERFACE));

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_acl_set(lynq_wifi_ap_index_e idx, lynq_wifi_mac_acl_rule_e acl_rule, char *mac_list)
{
    int ret = 0;
    char path[PATH_MAX_LEN] = {0};
    
    if(NULL == mac_list)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    if(LYNQ_WIFI_MAC_ACL_RULE_WHITE_LIST == acl_rule)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "macaddr_acl", "1");
        memcpy(path, ACL_ACCEPT_FILE, strlen(ACL_ACCEPT_FILE));
        
    }
    else if(LYNQ_WIFI_MAC_ACL_RULE_BLACK_LIST == acl_rule)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "macaddr_acl", "0");
        memcpy(path, ACL_DENY_FILE, strlen(ACL_DENY_FILE));
    }
    else
    {
        LOGE("qser_wifi_ap_acl_set LYNQ_WIFI_MAC_ACL_RULE_NONE");
        return LYNQ_ERR_SUCCESS;
    }

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    ret = mbtk_wifi_set_file(path, mac_list);
    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }


    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_acl_get(lynq_wifi_ap_index_e idx, lynq_wifi_mac_acl_rule_e *acl_rule, char *mac_list)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};
    char path[PATH_MAX_LEN] = {0};

    if(NULL == acl_rule || NULL == mac_list)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "macaddr_acl", value, SETTING_LINE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    if('1' == value[0])
    {
        *acl_rule = LYNQ_WIFI_MAC_ACL_RULE_WHITE_LIST;
        memcpy(path, ACL_ACCEPT_FILE, strlen(ACL_ACCEPT_FILE));
    }
    else if('0' == value[0])
    {
        *acl_rule = LYNQ_WIFI_MAC_ACL_RULE_BLACK_LIST;
        memcpy(path, ACL_DENY_FILE, strlen(ACL_DENY_FILE));
    }
    else
    {
        *acl_rule = LYNQ_WIFI_MAC_ACL_RULE_NONE;
        LOGE("qser_wifi_ap_acl_get LYNQ_WIFI_MAC_ACL_RULE_NONE");
        return LYNQ_ERR_SUCCESS;
    }

    ret = mbtk_wifi_get_file(path, mac_list, SETTING_LINE_MAX_LEN);
    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_ssid_set(lynq_wifi_ap_index_e idx, const char *ssid)
{
    int ret = 0;
     
    if(NULL == ssid)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }
     
    ret = mbtk_wifi_set_setting(SETTING_FILE, "ssid", ssid);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_ssid_get(lynq_wifi_ap_index_e idx, char *ssid)
{
    int ret = 0;

    if(NULL == ssid)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }
    
    ret = mbtk_wifi_get_setting(SETTING_FILE, "ssid", ssid, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_ssid_hide_set(lynq_wifi_ap_index_e idx,bool hide)
{
    int ret = 0;
    if(true == hide)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ignore_broadcast_ssid", "0");
    }
    else
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ignore_broadcast_ssid", "1");
    }

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }
    
    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_ssid_hide_get(lynq_wifi_ap_index_e idx,bool* hide)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == hide)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "ignore_broadcast_ssid", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    if('0' == value[0])
    {
        *hide = true;
    }
    else
    {
        *hide = false;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_mode_set(lynq_wifi_ap_index_e idx, lynq_wifi_mode_type_e mode)
{
    int ret = 0;
    if(LYNQ_WIFI_MODE_80211B == mode)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ieee80211b", "1");
    }
    else if(LYNQ_WIFI_MODE_80211BG == mode)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ieee80211bg", "1");
    }
    else if(LYNQ_WIFI_MODE_80211BGN == mode)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ieee80211bgn", "1");
    }
    else if(LYNQ_WIFI_MODE_80211A == mode)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ieee80211a", "1");
    }
    else if(LYNQ_WIFI_MODE_80211AN == mode)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ieee80211an", "1");
    }
    else if(LYNQ_WIFI_MODE_80211AC == mode)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "ieee80211ac", "1");
    }

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_mode_get(lynq_wifi_ap_index_e idx, lynq_wifi_mode_type_e* mode)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == mode)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "ieee80211b", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    if('1' == value[0])
    {
        *mode = LYNQ_WIFI_MODE_80211B;
    }

    memset(value, 0, SETTING_VALUE_MAX_LEN);
    ret = mbtk_wifi_get_setting(SETTING_FILE, "ieee80211bg", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    if('1' == value[0])
    {
        *mode = LYNQ_WIFI_MODE_80211BG;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_bandwidth_set(lynq_wifi_ap_index_e idx, lynq_wifi_bandwidth_type_e bandwidth)
{
    int ret = 0;
    if(LYNQ_WIFI_BANDWIDTH_HT20 == bandwidth)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "vht_oper_chwidth", "0");
    }
    else if(LYNQ_WIFI_BANDWIDTH_HT40 == bandwidth)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "vht_oper_chwidth", "1");
    }
    else if(LYNQ_WIFI_BANDWIDTH_HT80 == bandwidth)
    {
        ret = mbtk_wifi_set_setting(SETTING_FILE, "vht_oper_chwidth", "2");
    }

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;

}

int qser_wifi_ap_bandwidth_get(lynq_wifi_ap_index_e idx, lynq_wifi_bandwidth_type_e *bandwidth)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == bandwidth)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "vht_oper_chwidth", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    if('0' == value[0])
    {
        *bandwidth = LYNQ_WIFI_BANDWIDTH_HT20;
    }
    else if ('1' == value[0])
    {
        *bandwidth = LYNQ_WIFI_BANDWIDTH_HT40;
    }
    else if ('2' == value[0])
    {
        *bandwidth = LYNQ_WIFI_BANDWIDTH_HT80;
    }


    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_channel_set(lynq_wifi_ap_index_e idx,const char* country_code, int channel)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == country_code)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_set_setting(SETTING_FILE, "country_code", country_code);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    sprintf(value, "%d", channel);
    ret = mbtk_wifi_set_setting(SETTING_FILE, "channel", value);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_channel_get(lynq_wifi_ap_index_e idx,char* country_code, int* channel)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == country_code || NULL ==channel)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "country_code", country_code, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "channel", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }
    
    *channel = atoi(value);
    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_auth_set(lynq_wifi_ap_index_e idx, lynq_wifi_auth_e auth_mode, const char * auth_passwd)
{
    int ret = 0;

    if(NULL == auth_passwd)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_set_setting(SETTING_FILE, "wpa", "2");

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }
    
    ret = mbtk_wifi_set_setting(SETTING_FILE, "wpa_key_mgmt", "WPA-PSK");

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }
    
    ret = mbtk_wifi_set_setting(SETTING_FILE, "wpa_passphrase", auth_passwd);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;

}

int qser_wifi_ap_auth_get(lynq_wifi_ap_index_e idx, lynq_wifi_auth_e *auth_mode, char * auth_passwd)
{
    int ret = 0;

    if(NULL == auth_mode || NULL == auth_passwd)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "wpa_passphrase", auth_passwd, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_auth_get_s(lynq_wifi_ap_index_e idx, lynq_wifi_ap_auth_t* auth_mode)
{
    int ret = 0;

    if(NULL == auth_mode)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "wpa_passphrase", auth_mode->passwd, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_max_sta_set(lynq_wifi_ap_index_e idx, int max_sta_num)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    sprintf(value, "%d", max_sta_num);
    ret = mbtk_wifi_set_setting(SETTING_FILE, "max_num_sta", value);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_max_sta_get(lynq_wifi_ap_index_e idx, int* max_sta_num)
{
    int ret = 0;
    char value[SETTING_VALUE_MAX_LEN] = {0};

    if(NULL == max_sta_num)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    ret = mbtk_wifi_get_setting(SETTING_FILE, "max_num_sta", value, SETTING_VALUE_MAX_LEN);

    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }
    
    *max_sta_num = atoi(value);

    return LYNQ_ERR_SUCCESS;

}



int qser_wifi_lanhost_get_list(lynq_lanhost_ts* lynq_arrays)
{
    if(NULL == lynq_arrays)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    if(0 > mbtk_wifi_get_dhcp((mbtk_lanhost_ts*)lynq_arrays))
    {
        LOGE("qser wifi get lanhost fail");
        return LYNQ_ERR_FAIL;
    }


    if(0 > mbkt_wifi_get_uptime((mbtk_lanhost_ts*)lynq_arrays))
    {
        LOGE("qser wifi get lanhost fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_get_ap_pkt_stats(lynq_wifi_ap_index_e idx, lynq_wifi_pkt_stats_t *pkt_stat)
{
    if(NULL == pkt_stat)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    if(0 > mbtk_wifi_get_pkt((mbtk_wifi_pkt_stats_t*)pkt_stat))
    {
        LOGE("qser wifi get pkt fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_ap_start(lynq_wifi_ap_index_e idx)
{
    int ret = 0;

    if(LYNQ_WIFI_AP_INDEX_AP0 == idx)
    {
         ret = qser_wifi_work_mode_set(LYNQ_WIFI_AP_INDEX_AP0);
    }
    else
    {
        ret = qser_wifi_work_mode_set(LYNQ_WIFI_AP_INDEX_AP1);
    }
    LOGI("qser_wifi_ap_start set hw_mode ret:%d\n", ret);
    if(0 > ret)
    {
        LOGE("qser wifi fail");
        return LYNQ_ERR_FAIL;
    }

   if(0 > mbtk_wifi_ap_start())
    {
        return LYNQ_ERR_FAIL;
    }
    

     g_ap_status = LYNQ_WIFI_AP_STATUS_ENABLED;
     return LYNQ_ERR_SUCCESS;   
}

int qser_wifi_ap_stop(lynq_wifi_ap_index_e idx)
{
    if(0 > mbtk_wifi_ap_stop())
    {
        return LYNQ_ERR_FAIL;
    }

    g_ap_status = LYNQ_WIFI_AP_STATUS_DISABLING;
    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_ap_restart(lynq_wifi_ap_index_e idx)
{
    if(0 > mbtk_wifi_ap_stop())
    {
        return LYNQ_ERR_FAIL;
    }

    if(0 > mbtk_wifi_ap_start())
    {
        return LYNQ_ERR_FAIL;
    }

   return LYNQ_ERR_SUCCESS;
}

int qser_wifi_register_handle(lynq_wifi_event_handle event_handle, lynq_wifi_event_handle_sta event_handle_sta, void *arg)
{
    if(NULL != event_handle_sta)
    {
        g_event_handle_sta_cb = event_handle_sta;
       
    }
    else
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }
   
    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_sta_param_set(lynq_wifi_sta_param_t *param_stat)
{
    if(NULL == param_stat)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }
    
    sta_cli_ssid_set(param_stat->ssid);
    sta_cli_psk_set(param_stat->passwd);
    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_sta_param_get(lynq_wifi_sta_param_t *param_stat)
{
    int len = 0;

    if(NULL == param_stat)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }
    
    sta_cli_ssid_get(param_stat->ssid);
    len = strlen(param_stat->ssid);
    param_stat->ssid[len - 1] = '\0';
    sta_cli_psk_get(param_stat->passwd);
    len = strlen(param_stat->passwd);
    param_stat->passwd[len - 1] = '\0';
    
    LOGE("ssid: %s, passwd: %s", param_stat->ssid, param_stat->passwd);
    
    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_sta_start(void)
{
    char reply[STA_BUF_SIZE];

    if(sta_cli_cmd_parse("ADD_NETWORK", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }


    if(sta_cli_cmd_parse("SELECT_NETWORK", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }

      if(sta_cli_cmd_parse("ENABLE_NETWORK", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }

    if(0 > system("udhcpc -i wlan0"))
    {
        return LYNQ_ERR_FAIL;
    }

    
    return LYNQ_ERR_SUCCESS;
}

int qser_wifi_sta_stop(void)
{
    char reply[STA_BUF_SIZE];

    if(sta_cli_cmd_parse("DISABLE_NETWORK", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }

      if(sta_cli_cmd_parse("REMOVE_NETWORK", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }
    
   return LYNQ_ERR_SUCCESS;
}

int  qser_wifi_sta_get_status(lynq_wifi_sta_status_t *status_stat)
{
    char reply[STA_BUF_SIZE];
    int i = 0, j = 0;
    int status_flag = 0;

    if(NULL == status_stat)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

      if(sta_cli_cmd_parse("STATUS", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }

    
    for(i = 0; i < strlen(reply); i++)
    {
        if(',' == reply[i])
        {
            j = 0;
            status_flag++;
            continue;
        }

        if(0 == status_flag)
        {
            status_stat->status = 4;
            j++;
        }
        else if(1 == status_flag)
        {
            status_stat->ap_bssid[j] = reply[i];
            j++;
        }
        else if(2 == status_flag)
        {
            status_stat->ifname[j] = reply[i];
            j++;
        }
        else if(8 == status_flag)
        {
            status_stat->has_addr = '1';
            status_stat->addr[0].addr[j] = reply[i];
            j++;
        }
      

     
    }
    LOGE("state:%d, ap_bssid:%s, ifname:%s, has_addr:%c, addr:%s\n", status_stat->status, status_stat->ap_bssid, status_stat->ifname, status_stat->has_addr, 
                status_stat->addr[0].addr);

    return LYNQ_ERR_SUCCESS;
    
}


int qser_wifi_get_sta_pkt_stats(lynq_wifi_pkt_stats_t *pkt_stat)
{
    if(NULL == pkt_stat)
    {
        LOGE("qser wifi ptr is null");
        return LYNQ_ERR_BADPARAM;
    }

    if(0 > mbtk_wifi_get_pkt((mbtk_wifi_pkt_stats_t*)pkt_stat))
    {
        LOGE("qser wifi get pkt fail");
        return LYNQ_ERR_FAIL;
    }

    return LYNQ_ERR_SUCCESS;
}


int qser_wifi_sta_start_scan(void)
{
    char reply[STA_BUF_SIZE] = {0};
    lynq_wifi_sta_scan_list_t scan_list = {0};
    int i = 0, j = 0;
    int status_flag = 0;

    if(sta_cli_cmd_parse("SCAN", reply, STA_BUF_SIZE))
    {
		if(strlen(reply) > 0)
		{
           LOGE("reply data(%s).\n",reply);
		}else
        {
			LOGE("No reply data\n");
		}
    }
    else
    {
        LOGE("Parse cmd fail.\n");
    }



    scan_list.cnt = 1;
    
    for(i = 0; i < strlen(reply); i++)
    {
        if(',' == reply[i])
        {
            j = 0;
            status_flag++;
            continue;
        }

        if(0 == status_flag)
        {
            scan_list.info[0].bssid[j] = reply[i];
            j++;
        }
        else if(1 == status_flag)
        {
            //scan_list.info[0].bssid = reply[i];
            j++;
        }
        else if(2 == status_flag)
        {
            //scan_list.info[0].signal = atoi(reply[i]);
            j++;
        }
        else if(4 == status_flag)
        {
            scan_list.info[0].essid[j] = reply[i];
            j++;
        }
      

     
    }


    
    g_event_handle_sta_cb(&scan_list);
    return LYNQ_ERR_SUCCESS;
}

/*
 void lynq_wifi_event_handle_demo(lynq_wifi_event_s *event, void *arg)
 {
    if(event->id == LYNQ_WIFI_EVENT_AP_STATION)
        LOGE("[lynq-wifi-demo] %s:event-id = %d- %d\n", __func__, event->id, event->status);
    else if(event->id == LYNQ_WIFI_EVENT_AP_STA_STATUS)
        LOGE("[lynq-wifi-demo] %s:event-id = %d,%d,%s,%s\n", __func__, event->id,
        event->ap_sta_info.connected, event->ap_sta_info.mac,event->ap_sta_info.hostname);
    if(event->id == LYNQ_WIFI_EVENT_STA_STATUS)
        LOGE("[lynq-wifi-demo] %s:event-id = %d- %d\n", __func__, event->id, event->status);
    else
    return;
 }
 */
 
 void lynq_wifi_event_handle_sta_demo(lynq_wifi_sta_scan_list_t *event)
 {
     int i = 0;
     if(NULL == event)
    {
        LOGE("qser wifi ptr is null");
        return;
    }

     for (i = 0; i < event->cnt; i++)
     {
         LOGE("[lynq-wifi-demo] %s : ap[%d]:%s,%d,%d,%d,%s,%d,%d,%d\n", __func__, i,
         event->info[i].essid, event->info[i].auth,
         event->info[i].cipher, event->info[i].channel, event->info[i].bssid,
         event->info[i].signal_level,event->info[i].frequency,event->info[i].signal);
     }
 }




