#include "includes.h"
#include <stdarg.h>
#include <ctype.h>
#include "macro_common.h"
#include "trace.h"
#include "console.h"

extern S_CLI_CMD_LIST	g_cli_cmd_list;
int	g_db_on;

/*lint -save -e715*/
/*extern int enable_show_cmd_cmd(u8 argc,const char **argv);*/
/*lint -restore*/	
DEFCLICMD(enable_show_cmd_cmd,enable_show_cmd_struct,
			"list","List all command string")
{
	u8 i = 0;
	NO_USE(argc);
	NO_USE(argv);
	for( i = 0; i < g_cli_cmd_list.num;i++ )
	{
		(void)cmd_out("%s"NEWLINE,((PS_CLI_CMD)(g_cli_cmd_list.cmd[i]))->cmd);
		(void)cmd_out("	%s"NEWLINE,((PS_CLI_CMD)(g_cli_cmd_list.cmd[i]))->description);
	}

	return 0;
}

/*extern int enable_cmd_help_cmd(u8 argc,const char **argv);*/
/*lint -save -e818*/	
DEFCLICMD(enable_cmd_help_cmd,enable_cmd_help_struct,
			"help CMDSTRING",	"show help")
{
	u8 i = 0;

	if(argc < 2)
	{
		(void)cmd_out("Argument -1!"NEWLINE);
		return -1;
	}

	for( i = 0; i < g_cli_cmd_list.num;i++ )
	{
		if(strncmp(((PS_CLI_CMD)(g_cli_cmd_list.cmd[i]))->cmd, argv[1], strlen(argv[1])) == 0)
		{
			(void)cmd_out("%s"NEWLINE,((PS_CLI_CMD)(g_cli_cmd_list.cmd[i]))->description);
			return 0;
		}
	}
	(void)cmd_out("%s",UNKNOWNCMD);

	return 0;
}
/*lint -restore*/


/*lint -save -e818*/	
DEFCLICMD(enable_cmd_debug_cmd,enable_cmd_debug_struct,
			"debug (on|off)","turn on/off debug infomation")
{
	NO_USE(argc);
	if(strncmp("on",argv[1], strlen(argv[1])) == 0)
	{
		g_db_on = TRUE;
		(void)cmd_out("open the debug msg"NEWLINE);
	}
	else if(strncmp("off",argv[1], strlen(argv[1])) == 0)
	{
		g_db_on = FALSE;
		(void)cmd_out("shutdown the debug msg"NEWLINE);
	}
	else
	{
		return -1;
	}

	return 0;
}

/*lint -restore*/
#define SOFTWARE_VERSION			"1.1.2"

/*打印本版本软件信息*/
static void cli_print_softinfo(void)
{
	(void)cmd_out("\r\n 编译时间:   %s %s",__TIME__,__DATE__);
	(void)cmd_out("\r\n 软件版本:   ");
	(void)cmd_out(SOFTWARE_VERSION);
}


DEFCLICMD(enable_cmd_show_cmd,enable_cmd_show_struct,
			"show","show all int")
{
	NO_USE(argc);
	NO_USE(argv);
	if(strncmp("",argv[1], strlen(argv[1])) == 0)
	{
		(void)cmd_out("\r\n	缺少参数:");
		(void)cmd_out("\r\n	version:	查看版本");
		(void)cmd_out("\r\n	softinfo:	查看软件版本信息");
	}
	else if(strncmp("softinfo",argv[1], strlen(argv[1])) == 0)
	{
		cli_print_softinfo();
	}
	else
	{
		(void)cmd_out("\r\n	缺少参数:");
		(void)cmd_out("\r\n	version:	查看版本");
		(void)cmd_out("\r\n	softinfo:	查看软件版本信息");
	}
	(void)cmd_out(NEWLINE);
	return 0;
}

DEFCLICMD(enable_cpu_usage_cmd,enable_cpu_usage_struct,
			"cpu usage","cpu")
{
	NO_USE(argc);
	NO_USE(argv);

	(void)cmd_out("\r\n	CPU 利用率 : %d %%", OSStatTaskCPUUsage);
	(void)cmd_out(NEWLINE);

	return 0;
}
extern int maqtt_check_connect_status(uint8_t type);
DEFCLICMD(enable_cmd_mqtt_cmd,enable_cmd_mqtt_struct,
                        "mqtt","mqtt_status")
{
    NO_USE(argc);
    NO_USE(argv);
    if(strncmp("",argv[1], strlen(argv[1])) == 0)
    {
        (void)cmd_out("\r\n	缺少参数:");
    }
    else if(strncmp("connect",argv[1], strlen(argv[1])) == 0)
    {
        maqtt_check_connect_status(1);
    }
    else
    {
        (void)cmd_out("\r\n	缺少参数:");
    }
    (void)cmd_out(NEWLINE);
    return 0;
}

void cmd_test_install(void)
{
    (void)cmd_install(&enable_show_cmd_struct);
    (void)cmd_install(&enable_cmd_help_struct);
    (void)cmd_install(&enable_cmd_debug_struct);
    (void)cmd_install(&enable_cmd_show_struct);
    (void)cmd_install(&enable_cpu_usage_struct);
    (void)cmd_install(&enable_cmd_mqtt_struct);
}
