#include "CommandsDefine.h"
#include "string.h"
#include "usart.h"
#include "Helper.h"

u32 RecvedPackageCount=0;
u32 SendBackPackageCount=0;
u8  RegisteredMatserStartTime[BYTES_MASTER_STARTTIME]="2022-01-01 00:00:00";
//////////////////////////////////////////////////////////////////////////////
// 			2203/8/X GHz_ljw 合并版
//////////////////////////////////////////////////////////////////////////////
//初始化命令
//cmdLen命令总长度
//cmdCode命令号
//*pData:需要发送的数据，其长度应与dataLen匹配
//dataLen数据长度，其大小应与pData大小匹配
//resultBuffer:结果buffer
int Command_FormatCmdBuffer(u8 cmdCode,u8 *pData,int dataLen,u8* resultBuffer)
{

	resultBuffer[0] = PACKAGE_HEADER_BYTE1;
	resultBuffer[1] = PACKAGE_HEADER_BYTE2;
	resultBuffer[2] = PACKAGE_HEADER_BYTE3;
	resultBuffer[3] = cmdCode;
	resultBuffer[4] = (u8)((dataLen) & 0xFF);
	resultBuffer[5] = (u8)((dataLen) >> 8);
	
	if(dataLen>0)
	{
		memcpy(&resultBuffer[PACKAGE_HEAD_BYTES],pData,dataLen);
	}
	resultBuffer[PACKAGE_HEAD_BYTES+dataLen] = PACKAGE_TAIL_BYTE1;
	resultBuffer[PACKAGE_HEAD_BYTES+dataLen+1] = PACKAGE_TAIL_BYTE2;
	return (PACKAGE_HEAD_BYTES+dataLen+PACKAGE_TAIL_BYTES);
}


#define MaxBytesCurrPackage (2048+32) 
u8 currPackage[MaxBytesCurrPackage];
void Command_ProcessRequestCmds(void)
{
	Usart_Header* pUsart_Header;
	u16 loop_buffer_index=Usart_RecvedPackage[Usart_ProcessPackageIndex];
	
	if (loop_buffer_index==0xffff)
		goto Finished;

	Usart_RecvedPackage[Usart_ProcessPackageIndex]=0xffff;
	
	for(int i=0;i<MaxBytesCurrPackage;i++)
	{
		currPackage[i]=USART_RX_BUF[loop_buffer_index];
		loop_buffer_index=(loop_buffer_index+1) % USART_REC_LEN;
	}
	if (currPackage[0]!=0x55 || currPackage[1]!=0xaa || currPackage[2]!=0xbb)
			goto Finished;

	pUsart_Header=(Usart_Header*)&currPackage;
	if ((currPackage[pUsart_Header->packHeader.len+6]!=0x0d) || (currPackage[pUsart_Header->packHeader.len+6+1]!=0x0a))
			goto Finished;
	loop_buffer_index=Usart_RecvedPackage[Usart_ProcessPackageIndex];
	
	CmdPackageHeader *pCmdPackageHeader;
	 
	pCmdPackageHeader=(CmdPackageHeader *)&currPackage[0];
	
	for(u8 cmdIndex=0;cmdIndex<INCLUDE_CMD_COUNT;cmdIndex++)
	{
		if (RequestCmdTables[cmdIndex].cmd==pCmdPackageHeader->Cmd)
		{	
			RequestCmdTables[cmdIndex].Process(&currPackage[PACKAGE_HEAD_BYTES],pCmdPackageHeader->Bytes,RequestCmdTables[cmdIndex].bIsContentBitInverted_Recv,RequestCmdTables[cmdIndex].bIsContentBitInverted_SendBack_SendBack);
			break;
		}
	}
	Finished:
	Usart_ProcessPackageIndex=(Usart_ProcessPackageIndex+1) % 20;
}

void Command_SendbackOKMessage()
{
	u8 dataBytes=2;
	USART_WR_BUF[0]=CMD0xC0_Request_CmdIsOK;
	USART_WR_BUF[1]=CMD0xC0_Request_CmdIsOK;
	USART6_Send(CMD0xC0_Request_CmdIsOK,dataBytes,1);	
}

void Command_SendbackErrorMessage(u8 errorCode,u8 bIsContentBitInverted_SendBack)
{
	u8 dataBytes=1;
	USART_WR_BUF[0]=errorCode;

	USART6_Send(CMD0xB0_Request_GetErrorCode,dataBytes,bIsContentBitInverted_SendBack);	
}

void Command_SendbackAppStartTime()
{
		//response!!!
		u8 dataBytes=BYTES_MASTER_STARTTIME;
		u8 bIsContentBitInverted_SendBack=1;
		
	  memcpy(USART_WR_BUF,RegisteredMatserStartTime,dataBytes);

		USART6_Send(CMD0x07_Request_ReadbackAppStartTime,dataBytes,bIsContentBitInverted_SendBack);	
}
