/*
 * AppTaskUart0.c
 *
 *  Created on: 2022年2月8日
 *      Author: QiXiang_CHENJIE
 */
#include "AppTaskUart0.h"
const McuAtCmdFunc_t McuAtCmdFunc[] = {
	{MAT, "AT", McuAtCallbackFunc},
	{MAT_Version, "AT+VERSION?", McuAtCallbackFunc},
	{MAT_Connect, "AT+CONNECT=", McuAtCallbackFunc},
	{MAT_ConnectAsk,"AT+CONNECTION=?",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}};
void UartTask(void *pvParameters)
{
	(void)pvParameters;
	ConnSemaphore = xSemaphoreCreateBinary();//连接状态信号量
	McuDataSendHandle = xQueueCreate(2, 10); // Mcu的AT数据指令发送队列
	McuDataRecvHandle = xQueueCreate(1, sizeof(BleRecvDataFromUart2)); // Mcu的AT数据数据获取队列
	char AtCmdRead[50+7];
	char McuAtCmdAns[300] = {0};
	uint8 McuAtCmdAnsLen;
	uint8 RecvRet = 1;
	uint16 ReadLen = 0;
	uint8 *UartRecvPtr=NULL;
	for (;;)
	{
		vTaskDelay(pdMS_TO_TICKS(10));
		UART_Receive_Data(UART_LPUART0,&UartRecvPtr,&ReadLen, 1000);
		memset(AtCmdRead,0x00,sizeof(AtCmdRead));
		memcpy(AtCmdRead, UartRecvPtr, ReadLen);
		if(ReadLen>0)
		{
			memset(McuAtCmdAns,0x00,sizeof(McuAtCmdAns));
			McuAtCmdAnsLen = 0;
			McuATCmdTransmit(AtCmdRead, ReadLen, McuAtCmdAns, &McuAtCmdAnsLen);
			UART_Send_Data(UART_LPUART0,McuAtCmdAns,McuAtCmdAnsLen,1000);
		}
		if(UartRecvPtr != NULL)
		{
			free(UartRecvPtr);
		}
		UartRecvPtr = NULL;
	}
}
void McuATCmdTransmit(char *Readptr, uint16 Readlen, char *AnsPtr, uint8 *AnsPtrLen)
{
	uint8 ret = 0;
	for (uint8 i = 0; i < MAT_Num; i++)
	{
		ret = AtStrCompare(Readptr, McuAtCmdFunc[i].str);
		if (ret)
		{
			McuAtCmdFunc[i].cb(Readptr,Readlen, i, AnsPtr, AnsPtrLen);
			return;
		}
	}
	*AnsPtrLen = strlen("ERROR");
	memcpy(AnsPtr, "ERROR", *AnsPtrLen);
	return;
}
sint8 McuAtCallbackFunc(char *Readptr,uint16 ReadLen, McuAtCmd McuAtIdx, char *AnsPtr, uint8 *AnsPtrLen)
{
	char *SucAns = "OK\r\n";
	char *ErrAns = "ERROR\r\n";
	char *OffAns = "OFF\r\n";
	char *OnAns = "ON\r\n";
	char *SlaveAns = "S";
	char *MasterAns = "M";
	switch (McuAtIdx)
	{
	case MAT:
	{
		*AnsPtrLen = strlen(SucAns);
		memcpy(AnsPtr, SucAns, *AnsPtrLen);
		break;
	}
	case MAT_Version:
	{
		uint8 chartemp;
		memcpy(AnsPtr, SucAns, strlen(SucAns));
		*AnsPtrLen = strlen(SucAns);
		for (uint8 i = 0; i < sizeof(BleVersion) * 2; i++)
		{
			chartemp = HexToChar((BleVersion >> (28 - 4 * i))&0x0F);
			memcpy(AnsPtr + *AnsPtrLen + i, &chartemp, 1);
		}

		*AnsPtrLen = sizeof(BleVersion) * 2 + *AnsPtrLen;
		break;
	}
	case MAT_Connect:
	{
		if (ReadLen == (strlen((McuAtCmdFunc[McuAtIdx].str)) + 12))
		{
			if (BleConnectFlg)
			{
				memcpy(AnsPtr, SucAns, strlen(SucAns));
				*AnsPtrLen = strlen(SucAns);
				memcpy(AnsPtr + *AnsPtrLen, (char *)("SUC\n"), strlen((char *)("SUC\n")));
				*AnsPtrLen = *AnsPtrLen + strlen((char *)("SUC\n"));
			}
			else
			{
				if (BleCurrentRole == 1)
				{
					for(uint8 i=0;i<6;i++)
					{
						uint8 temp1 = 0x00;
						uint8 temp2 = 0x00;
						temp1 = (CharToHex(*(Readptr + i*2 + strlen(McuAtCmdFunc[McuAtIdx].str)))<<4);
						temp2 = CharToHex(*(Readptr + i*2 + 1 + strlen(McuAtCmdFunc[McuAtIdx].str)));
						BleTargetMacAddr[i] = temp1|temp2;
					}
					BleConnectEnable = 1;
					BleDisconnectEnable = 0;
					cmdSwapBatt = 0;
					swapBattDone = 0;
					lockCmdFlg = 0;
					//if( xSemaphoreTake( ConnSemaphore, 20000 ) == pdTRUE )
					if(1)
					{
						memcpy(AnsPtr, SucAns, strlen(SucAns));
						*AnsPtrLen = strlen(SucAns);
						memcpy(AnsPtr + *AnsPtrLen, (char *)("SUC\n"), strlen((char *)("SUC\n")));
						*AnsPtrLen = *AnsPtrLen + strlen((char *)("SUC\n"));
					}
					else
					{
						memcpy(AnsPtr, ErrAns, strlen(ErrAns));
						*AnsPtrLen = strlen(ErrAns);
						memcpy(AnsPtr + *AnsPtrLen, (char *)("-1\n"), strlen((char *)("-1\n")));
						*AnsPtrLen = *AnsPtrLen + strlen((char *)("-1\n"));
					}
				}
				else
				{
					*AnsPtrLen = strlen((char *)ErrAns);
					memcpy(AnsPtr, ErrAns, *AnsPtrLen);
				}
			}
		}
		else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == '?')
		{
			if (BleConnectFlg)
			{
				if (BleCurrentRole == 1)
				{

					memcpy(AnsPtr, SucAns, strlen(SucAns));
					*AnsPtrLen = strlen(SucAns);
					memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
					*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
					for(uint8 i=0;i<6;i++)
					{
						*(AnsPtr + *AnsPtrLen + i*2) = HexToChar((BleTargetMacAddr[i]>>4)&0x0F);
						*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar(BleTargetMacAddr[i]&0x0F);
					}
					*AnsPtrLen = *AnsPtrLen + sizeof(BleTargetMacAddr)*2;
					break;
				}
				else
				{
					memcpy(AnsPtr, SucAns, strlen(SucAns));
					*AnsPtrLen = strlen(SucAns);
					memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
					*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
				}
			}
			else
			{
				memcpy(AnsPtr, SucAns, strlen(SucAns));
				*AnsPtrLen = strlen(SucAns);
				memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
				*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
			}
		}
		else
		{
			*AnsPtrLen = strlen((char *)ErrAns);
			memcpy(AnsPtr, ErrAns, *AnsPtrLen);
		}
		break;
	}
	case MAT_ConnectAsk:
	{
		if (BleConnectFlg)
		{
			memcpy(AnsPtr, SucAns, strlen(SucAns));
			*AnsPtrLen = strlen(SucAns);
			memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
			*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
		}
		else
		{
			memcpy(AnsPtr, SucAns, strlen(SucAns));
			*AnsPtrLen = strlen(SucAns);
			memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
			*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
		}
		break;
	}
	case MAT_DisConnect:
	{
		BleDisconnectEnable = 1;
		BleConnectEnable = 0;
		memcpy(AnsPtr, SucAns, strlen(SucAns));
		*AnsPtrLen = strlen(SucAns);
		break;
	}
	case MAT_Msg:
	{
		uint8 Databuffer[10] = {0};
		uint16 CrcChkGet;
		uint16 CrcChkCal;
		//数据传输通道
		if(BleConnectFlg||1)
		{
			if(*(Readptr+7)=='0' && *(Readptr+9)=='1'&& *(Readptr+10)=='0'&& ReadLen==22)//Hex
			{
				memcpy(Databuffer,Readptr+12,8);
				CrcChkGet = (*(Readptr+12+8))|(*(Readptr+12+9)<<8);
			}
			else if(*(Readptr+7)=='1'&&*(Readptr+9)=='2'&& *(Readptr+10)=='0' && ReadLen==32)//string
			{
				for(uint8 i=0;i<8;i++)
				{
					Databuffer[i] = (CharToHex(*(Readptr+12+i*2))<<4) | (CharToHex(*(Readptr+12+i*2+1))<<0);
				}
				CrcChkGet = (CharToHex(*(Readptr+12+18))<<12) | (CharToHex(*(Readptr+12+19))<<8)|(CharToHex(*(Readptr+12+16))<<4) | (CharToHex(*(Readptr+12+17))<<0);
			}
			else
			{
				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
				*AnsPtrLen = strlen(ErrAns);
				memcpy(AnsPtr + *AnsPtrLen, (char *)("-1\n"), strlen((char *)("-1\n")));
				*AnsPtrLen = *AnsPtrLen + strlen((char *)("-1\n"));
				break;
			}
			CrcChkCal = CRC16_Modbus (Databuffer, 8);
			if(CrcChkCal==CrcChkGet||1)
			{
				AT_MsgFlag = true;
				Databuffer[8] = CrcChkGet&0xff;
				Databuffer[9] = (CrcChkGet>>8)&0xff;
				xQueueSend(McuDataSendHandle,Databuffer,1000);
				//此处应等待Msg的信息返回后发送 oswait
				BleRecvDataFromUart2 RecvDatabuffer={0,NULL};
				sint8 QueneRetRev = -1;
				xQueueReset(McuDataRecvHandle);
				QueneRetRev = xQueueReceive(McuDataRecvHandle,&RecvDatabuffer,1000);
				if(QueneRetRev==pdPASS)
				{
					uint8 DataLen = RecvDatabuffer.RealLen;
					CrcChkGet = 0x00;
					CrcChkCal = 0xff;
					CrcChkGet = (*(RecvDatabuffer.DataPtr+DataLen-1)<<8)| (*(RecvDatabuffer.DataPtr+DataLen-2));
					CrcChkCal = CRC16_Modbus(RecvDatabuffer.DataPtr,DataLen-2);
					if(CrcChkCal==CrcChkGet)
					{
						memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:OK,"), strlen((char *)("Recv:OK,")));
						*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:OK,"));
						//需要转换为ASCII码发出
						for(uint8 i=0;i<DataLen;i++)
						{
							*(AnsPtr + *AnsPtrLen + i*2 + 0) = HexToChar((*(RecvDatabuffer.DataPtr+i)>>4)&0x0F);
							*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar((*(RecvDatabuffer.DataPtr+i)&0x0F));
						}
						*AnsPtrLen = *AnsPtrLen + DataLen*2;
						//直接发出memcpy(AnsPtr + *AnsPtrLen,RecvDatabuffer.DataPtr,DataLen);
						//*AnsPtrLen = *AnsPtrLen + DataLen;
					}
					else
					{
						memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:ERROR,-2"), strlen((char *)("Recv:ERROR,-2")));
						*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:ERROR,-2"));
					}
				}
				else
				{
					memcpy(AnsPtr + *AnsPtrLen, (char *)("Recv:ERROR,-1"), strlen((char *)("Recv:ERROR,-1")));
					*AnsPtrLen = *AnsPtrLen + strlen((char *)("Recv:ERROR,-1"));
				}
				if(RecvDatabuffer.DataPtr!=NULL)
				{
					free(RecvDatabuffer.DataPtr);
				}
				RecvDatabuffer.DataPtr = NULL;
				break;
			}
			else
			{
				memcpy(AnsPtr, ErrAns, strlen(ErrAns));
				*AnsPtrLen = strlen(ErrAns);
				memcpy(AnsPtr + *AnsPtrLen, (char *)("-2\n"), strlen((char *)("-2\n")));
				*AnsPtrLen = *AnsPtrLen + strlen((char *)("-2\n"));
				break;
			}

		}
		else
		{
			memcpy(AnsPtr, ErrAns, strlen(ErrAns));
			*AnsPtrLen = strlen(ErrAns);
			memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
			*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
		}
		break;
	}
	case MAT_NameSet:
	{
		*AnsPtrLen = strlen((char *)ErrAns);
		memcpy(AnsPtr, ErrAns, *AnsPtrLen);
		break;
	}
	case MAT_Power:
	{
		if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == '?')
		{
			if (BlePwrState == 0) //为0开机
			{
				memcpy(AnsPtr, SucAns, strlen(SucAns));
				*AnsPtrLen = strlen(SucAns);
				memcpy(AnsPtr + *AnsPtrLen, OnAns, strlen(OnAns));
				*AnsPtrLen = *AnsPtrLen + strlen(OnAns);
			}
			else
			{
				memcpy(AnsPtr, SucAns, strlen(SucAns));
				*AnsPtrLen = strlen(SucAns);
				memcpy(AnsPtr + *AnsPtrLen, OffAns, strlen(OffAns));
				*AnsPtrLen = *AnsPtrLen + strlen(OffAns);
			}
		}
		else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == 'O' && *(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str) + 1) == 'N')
		{
			BlePwrControlEnable = 1;
			BlePwrControlFlg = 0;
			memcpy(AnsPtr, SucAns, strlen(SucAns));
			*AnsPtrLen = strlen(SucAns);
		}
		else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == 'O' && *(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str) + 1) == 'F' && *(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str) + 2) == 'F')
		{
			BlePwrControlEnable = 1;
			BlePwrControlFlg = 1;
			memcpy(AnsPtr, SucAns, strlen(SucAns));
			*AnsPtrLen = strlen(SucAns);
		}
		else
		{
			*AnsPtrLen = strlen((char *)ErrAns);
			memcpy(AnsPtr, ErrAns, *AnsPtrLen);
		}
		break;
	}
	case MAT_Role: //角色查询与设置
	{
		if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == '?')
		{
			if (BleCurrentRole == 0) //为0从机
			{
				memcpy(AnsPtr, SucAns, strlen(SucAns));
				*AnsPtrLen = strlen(SucAns);
				memcpy(AnsPtr + *AnsPtrLen, SlaveAns, strlen(SlaveAns));
				*AnsPtrLen = *AnsPtrLen + strlen(SlaveAns);
			}
			else
			{
				memcpy(AnsPtr, SucAns, strlen(SucAns));
				*AnsPtrLen = strlen(SucAns);
				memcpy(AnsPtr + *AnsPtrLen, MasterAns, strlen(MasterAns));
				*AnsPtrLen = *AnsPtrLen + strlen(MasterAns);
			}
		}
		else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == *MasterAns)
		{
			BleSetRoleEnable = 1;
			BleSetRole = 1;
			memcpy(AnsPtr, SucAns, strlen(SucAns));
			*AnsPtrLen = strlen(SucAns);
		}
		else if (*(Readptr + strlen(McuAtCmdFunc[McuAtIdx].str)) == *SlaveAns)
		{
			BleSetRoleEnable = 1;
			BleSetRole = 0;
			memcpy(AnsPtr, SucAns, strlen(SucAns));
			*AnsPtrLen = strlen(SucAns);
		}
		else
		{
			*AnsPtrLen = strlen((char *)ErrAns);
			memcpy(AnsPtr, ErrAns, *AnsPtrLen);
		}
		break;
	}
	case MAT_GetMac:
	{
		memcpy(AnsPtr, SucAns, strlen(SucAns));
		*AnsPtrLen = strlen(SucAns);
		for(uint8 i=0;i<6;i++)
		{
			*(AnsPtr + *AnsPtrLen + i*2) = HexToChar((BleMacAddr[i]>>4)&0x0F);
			*(AnsPtr + *AnsPtrLen + i*2 + 1) = HexToChar(BleMacAddr[i]&0x0F);
		}
		*AnsPtrLen = *AnsPtrLen + sizeof(BleMacAddr)*2;
		break;
	}
	default:
		break;
	}
	return 0;
}
