#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <linux/ioctl.h>
#include <unistd.h>
#include <sys/types.h>  
#include <sys/socket.h>  
#include <wifi_intf.h>
#include <uvdbus/log.h>
#include <response_code.h>
#include <command_listener.h>

#define MSG_LEN_MX 256
#define CMD_LEN_MX 128

#define DEFAULT_SOFTAP_ESSID       "SmartAudio-HOSTAPD"
#define DEFAULT_SOFTAP_PWD         "neteaseihw"
#define DEFAULT_SOFTAP_CHANNEL     "6"
#define DEFAULT_SOFTAP_AP_PORT     "wlan0"
#define DEFAULT_SOFTAP_ENCRYPTION  "wpa2-psk"


#define SOFTAP_OLD_ACT   0


static int nSystem(char* cmd)
{
    int i_ret = 0;
    char result_buf[512] = {0};
    FILE *fp;
	
    if(cmd == NULL)
    {
        LOG_EX(LOG_Error,"<%s> %s L%d param ERROR!\n", __FILE__, __FUNCTION__, __LINE__);
        return -1;
    }
	
    fp = popen(cmd, "r");
    if(NULL == fp)
    {
        LOG_EX(LOG_Error,"<%s> popen ERROR!\n", __FILE__);
        return -1;
    }

    while(fgets(result_buf, sizeof(result_buf), fp) != NULL)
    {
        //todo
        printf("%s\n", result_buf);
    }

    /*等待命令执行完毕并关闭管道及文件指针*/
    i_ret = pclose(fp);
    if(-1 == i_ret)
    {
        LOG_EX(LOG_Info,"<%s> pclose!\n", __FILE__);
        return i_ret;
    }

	return i_ret;
}

int nSoftapUp(char* ssid, char* pwd)
{
    tRESPONSE_CODE ret_code = SOFTAP_STATUS_RESULT;
	char msg[MSG_LEN_MX] = {0};
	char cmd[CMD_LEN_MX] = {0};
	int agc = 8;
	int len = 0;

    if(ssid == NULL)
    {
        LOG_EX(LOG_Error,"<%s> param error!\n", __FUNCTION__);
        return -1;
    }
	
	char **agv = (char **)malloc(sizeof(char *) * agc);

	agv[agc-1] = NULL;
    agv[0] = (char *)"setsoftap";
    agv[1] = (char *)"set";
    agv[2] = (char *)DEFAULT_SOFTAP_AP_PORT;
    agv[3] = ssid;
	agv[4] = (char *)"broadcast"; /*broadcast or hidden*/
    agv[5] = (char *)DEFAULT_SOFTAP_CHANNEL;
	if(pwd != NULL && strlen(pwd) > 0)
        agv[6] = (char *)DEFAULT_SOFTAP_ENCRYPTION;
	else
	    agv[6] = (char *)"open";
	agv[7] = pwd;
	if (!!pwd) {
        LOG_EX(LOG_Info,"AP SSID:%s, PASS:%s\n", ssid, pwd);
	}
	
	nSystem("ifconfig wlan0 down");
//	nSystem("killall -9 wpa_supplicant");
	nSystem("/etc/init.d/netease_wifi_service stop");
    usleep(1000 * 1000);
	nSystem("/etc/wifi/wifi stop");
	usleep(3000000);

	LOG_EX(LOG_Info,"Start to reload firmware!\n");

	agv[3] = "AP";
	sprintf(cmd,"fwreload");
	len = sizeof(msg);
	run_softap_command(&ret_code,msg,cmd,&len,agc,agv);
	if(ret_code != SOFTAP_STATUS_RESULT)
	{
		LOG_EX(LOG_Info,"Reload firmware failed! Code is %d\n",ret_code);
		free(agv);
		return -1;
	}
	
	LOG_EX(LOG_Info,"Message is: %s\n",msg);
	LOG_EX(LOG_Info,"Reload firmware finished!\n");

	LOG_EX(LOG_Info,"Start to set softap!\n");
	agv[3] = ssid;
	sprintf(cmd,"set");
	len = MSG_LEN_MX;
	run_softap_command(&ret_code,msg,cmd,&len,agc,agv);
	if(ret_code != SOFTAP_STATUS_RESULT)
	{
		LOG_EX(LOG_Info,"Set softap failed! Code is %d\n",ret_code);
		free(agv);
		return -1;
	}
	LOG_EX(LOG_Info,"Message is: %s\n",msg);
	LOG_EX(LOG_Info,"Set softap finished!\n");

	nSystem("ifconfig wlan0 up");
	usleep(2000000);
    nSystem("ifconfig");

	LOG_EX(LOG_Info,"Start to start softap!\n");
	sprintf(cmd,"startap");
	len = MSG_LEN_MX;
	run_softap_command(&ret_code,msg,cmd,&len,agc,agv);
	if(ret_code != SOFTAP_STATUS_RESULT)
	{
		LOG_EX(LOG_Info,"Start softap failed! Code is %d\n",ret_code);
		free(agv);
		return -1;
	}
	LOG_EX(LOG_Info,"Message is: %s\n",msg);
	LOG_EX(LOG_Info,"Start softap finished!\n");

	nSystem("ifconfig wlan0 192.168.5.1 netmask 255.255.255.0");
//	nSystem("udhcpd /etc/wifi/udhcpd.conf");x
//	usleep(500000);
	nSystem("/etc/init.d/dnsmasq restart");
	nSystem("echo 1 > /proc/sys/net/ipv4/ip_forward");
	nSystem("iptables -A FORWARD -i wlan0 -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT");
	nSystem("iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT");
	nSystem("iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT");
	nSystem("iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE");

	LOG_EX(LOG_Info,"***************************\n");
	LOG_EX(LOG_Info,"Hostapd test successed!\n");
	LOG_EX(LOG_Info,"***************************\n");

	free(agv);
	return 0;
}

int nSoftapDown(void)
{
    tRESPONSE_CODE ret_code = SOFTAP_STATUS_RESULT;
	char msg[MSG_LEN_MX] = {0};
	char cmd[CMD_LEN_MX] = {0};
	int agc = 8;
	int len = 0;
	char **agv = (char **)malloc(sizeof(char *) * agc);
	agv[agc-1] = NULL;
	#if 0
    agv[0] = (char *)"setsoftap";
    agv[1] = (char *)"set";
    agv[2] = (char *)DEFAULT_SOFTAP_AP_PORT;
    agv[3] = (char *)SOFTAP_ESSID;
    agv[4] = (char *)"broadcast";
    agv[5] = (char *)DEFAULT_SOFTAP_CHANNEL;
    agv[6] = (char *)DEFAULT_SOFTAP_ENCRYPTION;
	agv[7] = (char *)SOFTAP_PWD;
    #endif

	LOG_EX(LOG_Info,"***************************\n");
	LOG_EX(LOG_Info,"Start to shutdown hostapd!\n");
	LOG_EX(LOG_Info,"***************************\n");

#if SOFTAP_OLD_ACT
	nSystem("ifconfig wlan0 down");
	usleep(2000000);

#endif

	LOG_EX(LOG_Info,"Start to reload firmware!\n");

    sprintf(cmd, "stopap");
    len = sizeof(msg);
    run_softap_command(&ret_code,msg,cmd,&len,agc,agv);
    if(ret_code != SOFTAP_STATUS_RESULT)
    {
        LOG_EX(LOG_Info,"stop ap failed! Code is %d\n",ret_code);
        free(agv);
        return -1;
    }
	
#if SOFTAP_OLD_ACT
	agv[3] = "STA";
	sprintf(cmd,"fwreload");
	len = sizeof(msg);
	run_softap_command(&ret_code,msg,cmd,&len,agc,agv);
	if(ret_code != SOFTAP_STATUS_RESULT)
	{
		LOG_EX(LOG_Info,"Reload firmware failed! Code is %d\n",ret_code);
		free(agv);
		return -1;
	}

#endif

    nSystem("/etc/init.d/dnsmasq stop");
	usleep(1000 * 1000);
	nSystem("killall -9 hostapd");
	//nSystem("ip addr del 192.168.5.1 dev wlan0");

#if !SOFTAP_OLD_ACT
	nSystem("/etc/rftest/wifi_exit_test.sh");
    LOG_EX(LOG_Info,"----do new process\r\n");
#endif

#if SOFTAP_OLD_ACT	
    nSystem("/etc/wifi/wifi restart");
    usleep(1000 * 1000);
	nSystem("ifconfig wlan0 up");
	usleep(1000 * 1000);

#endif

	nSystem("/etc/init.d/netease_wifi_service restart");
	usleep(2000 * 1000);

	LOG_EX(LOG_Info,"***************************\n");
	LOG_EX(LOG_Info,"Shutdown Hostapd successed!\n");
	LOG_EX(LOG_Info,"***************************\n");

    nSystem("ps | grep wpa");

	free(agv);
	return 0;
}
