/*
 * AppTaskUart0.c
 *
 *  Created on: 
 *      Author: QiXiang_CHENJIE
 */
#include "AppTaskUart0.h"
#include "stdio.h"
void GlobeVariableHandle_W0(void);
const McuAtCmdFunc_t McuAtCmdFunc[] =
{
	{MAT, "AT", McuAtCallbackFunc},
	{MAT_Version, "AT+VERSION?", McuAtCallbackFunc},
	{MAT_Connect, "AT+CONNECT=", McuAtCallbackFunc},
	{MAT_DisConnect, "AT+DISCONNECT", McuAtCallbackFunc},
	{MAT_Msg, "AT+MSG=", McuAtCallbackFunc},
	{MAT_NameSet, "AT+NAME=", McuAtCallbackFunc},
	{MAT_Power, "AT+POWER=", McuAtCallbackFunc},
	{MAT_Role, "AT+ROLE=", McuAtCallbackFunc},
	{MAT_GetMac, "AT+MAC=?", McuAtCallbackFunc},
#if WIFI_MASTER
	{MAT_W, "AT+W", McuAtCallbackFunc},			              /* AT 测试指令*/
	{MAT_W_Set, "AT+WSET=", McuAtCallbackFunc},               /* 设置WiFi名称和密码*/
	{MAT_W_IPSet, "AT+WIPSET=", McuAtCallbackFunc},           /* 设置IP地址，子网掩码*/
	{MAT_W_PortClose, "AT+WPORTClose", McuAtCallbackFunc},    /* 关闭server服务 */
	{MAT_W_PortSet, "AT+WPORT=", McuAtCallbackFunc},          /* 开启server服务，设置端口号 如果重新设置端口号需要先关闭server*/
	{MAT_W_IP, "AT+WIP", McuAtCallbackFunc},                  /* 获取IP信息 */
	{MAT_W_TcpDisconnect, "AT+WTcpDisconnect", McuAtCallbackFunc},/* 断开所有TCP链接 */
	{MAT_W_msg, "AT+WMSG=", McuAtCallbackFunc},//此命令变化，416行长度也需要同步修改 类型，长度，数据
	{MAT_W_conchk, "AT+WCONNCHK=?", McuAtCallbackFunc},
#elif WIFI_SLAVE
	{MAT_S, "AT+S", McuAtCallbackFunc},
	{MAT_S_CONTWIFI, "AT+SConWiFi=", McuAtCallbackFunc},
	{MAT_S_TCPCONT, "AT+STcpCon=", McuAtCallbackFunc},
#endif
	{MAT_CMD_MAXNUM,"ERROR",McuAtCallbackFunc},
};
void UartTask(void *pvParameters)
{
	(void)pvParameters;
	ConnSemaphore = xSemaphoreCreateBinary();
	McuDataSendHandle = xQueueCreate(2, 10);
	McuDataRecvHandle = xQueueCreate(1, sizeof(QueueDataTrans_Type));
	McuDataSend_wifi = xQueueCreate(2, 10);
	McuDataRecv_wifi = xQueueCreate(1, sizeof(QueueDataTrans_Type));
	char AtCmdRead[128] = {0};
	char AtCmdAns[256] = {0};
	uint16 AtCmdAnsLen;
	uint8 RecvRet = 1;
	uint16 ReadLen = 0;
	for (;;)
	{
		memset(AtCmdRead,0x00,sizeof(AtCmdRead));
		RecvRet = UART_Receive_Data(UART_LPUART0, (uint8*)AtCmdRead,(uint16*)&ReadLen, 100);
		if(!RecvRet)
		{
			AtCmdAnsLen = 0;
			memset(AtCmdAns,0x00,sizeof(AtCmdAns));
			McuATCmdTransmit((char *)AtCmdRead, ReadLen, (char *)AtCmdAns,(uint8*)&AtCmdAnsLen);
			UART_Send_Data(UART_LPUART0,(const uint8*)AtCmdAns,AtCmdAnsLen,100);
		}
		GlobeVariableHandle_W0();
	}
}
void McuATCmdTransmit(char *Readptr, uint16 Readlen, char *AnsPtr, uint8 *AnsPtrLen)
{

	uint8 ret = 0;
	for (uint8 i = 0; i < MAT_CMD_MAXNUM; i++)
	{
		ret = AtStrCompare(Readptr, McuAtCmdFunc[i].str);
		if (ret)
		{
			McuAtCmdFunc[i].cb(Readptr,Readlen, i, AnsPtr, AnsPtrLen);
			return;
		}
	}
	McuAtCmdFunc[MAT_CMD_MAXNUM].cb(Readptr,Readlen, MAT_CMD_MAXNUM, AnsPtr, AnsPtrLen);
	return;
}
sint8 McuAtCallbackFunc(char *Readptr, uint16 ReadLen, McuAtCmd McuAtIdx, char *AnsPtr, uint8 *AnsPtrLen)
{



	return 0;
}

void GlobeVariableHandle_W0(void)
{
	sint8 ATRet = 0;
	if(AT_WAT_Flag > 1)
	{
		ATRet = AT_WAT_Flag;
		AT_WAT_Flag = 0;
	}
	else if(AT_IPINFO_SET_Flag > 1)
	{
		ATRet = AT_IPINFO_SET_Flag;
		AT_IPINFO_SET_Flag = 0;
	}
	else if(AT_WPROTCLOSE_FLAG > 1)
	{
		ATRet = AT_WPROTCLOSE_FLAG;
		AT_WPROTCLOSE_FLAG = 0;
	}
	else if(AT_WPROTSET_FLAG > 1)
	{
		ATRet = AT_WPROTSET_FLAG;
		AT_WPROTSET_FLAG = 0;
	}
	else if(AT_WIFI_INFO_FLAG > 1)
	{
		ATRet = AT_WIFI_INFO_FLAG;
		AT_WIFI_INFO_FLAG = 0;
	}
	else if(W_IP_FLAG > 1)
	{
		if(2 == W_IP_FLAG)
		{

			ATRet = 5;
		}
		else
		{
		ATRet = W_IP_FLAG;
		}
		W_IP_FLAG = 0;
	}
	else if(W_TcpDisconnect > 1)
	{
		ATRet = W_TcpDisconnect;
		W_TcpDisconnect = 0;
	}

	if(2 == ATRet)
	{
		UART_Send_Data(UART_LPUART0,(const uint8*)"Command executed successfully\r\n",strlen("Command executed successfully\r\n"),100);
	}
	else if(3 == ATRet)
	{
		UART_Send_Data(UART_LPUART0,(const uint8*)"Command execution failed\r\n",strlen("Command execution failed\r\n"),100);
	}
	else if(4 == ATRet)
	{
		UART_Send_Data(UART_LPUART0,(const uint8*)"Command executed but not changed\r\n",strlen("Command executed but not changed\r\n"),100);
	}
}
