#include <stdlib.h>
#include "al_console_HkRs232.h"
#include "kmf_ioctl.h"
#include "ap_extern.h"
#include "drv_pq_external.h"
#include "nvm_ioctl.h"
#include "util_ioctl.h"
#include "al_fw.h"
#include "subcustomer_setting.h"
#include "umf_ir.h"
#include "MM_media_qsd_gui.h"
#include "drv_tuner_external.h"
#include <sys/time.h>
#include "mid_tvfe.h"
#include "drv_uart_def.h"
#include "keyupdate.h"
#include "app_data_setting.h"
#include "pin_config.h"
#include "util_ioctl.h"
#include "app_led_control.h"
#include "board_config.h"
#include "customize.h"


#define RS232_RETURN_TIME (8)
#define BIT(x)  (1<<x)

#define RS232_DEBUG 0

static HkRs232_autotest_Cbk_pf g_pfpHkRs232_AutoTestCallback = NULL;

const UINT16 DynamicAllocationSizeTable[]=
{
	7001,7002,7003,7004,7005,7006,7007,7008,7009,
};
const UINT16 g_DynamicAllocationTableSize = sizeof(DynamicAllocationSizeTable)/sizeof(UINT16);

const HkPacketEventMapping hkCmdTable[]=
{
	{HKCMD_CMD_ExitFactory,			{HKCMD_HEADER,	HKCMD_TYPE_0000,	8015,	0x0000,	0x00}},
	{HKCMD_CMD_EnterBurnning, 		{HKCMD_HEADER,	HKCMD_TYPE_0000,	8013,	0x0001, 0x00}},
	{HKCMD_CMD_ExitBurnning, 		{HKCMD_HEADER,	HKCMD_TYPE_0000,	8013,	0x0000, 0x00}},
	//{HKCMD_CMD_GetHeatRunTime,		{HKCMD_HEADER,	HKCMD_TYPE_0000,	8014,	0x0000, 0x00}},
	{HKCMD_CMD_DCPowerOff,			{HKCMD_HEADER,	HKCMD_TYPE_0000,	9004,	0x0000, 0x00}},
	//{HKCMD_CMD_DCPowerOn,			{HKCMD_HEADER,	HKCMD_TYPE_0000,	9004,	0x0001, 0x00}},
	{HKCMD_CMD_ChangeSrcToATV,		{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bba,	0x0000, 0x00}},
	{HKCMD_CMD_ChangeSrcToDVBT,		{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bba, 0x0001, 0x00}},
	{HKCMD_CMD_ChangeSrcToDVBC, 	{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bba, 0x0002, 0x00}},
	{HKCMD_CMD_ChangeSrcToDVBS, 	{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bba, 0x0004, 0x00}},
	{HKCMD_CMD_ChangeSrcToAir, 		{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bba, 0x0005, 0x00}},
	{HKCMD_CMD_ChangeSrcToCable, 	{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bba, 0x0006, 0x00}},
	{HKCMD_CMD_ChangeSrcToHDMI1, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0007, 0x00}},
	{HKCMD_CMD_ChangeSrcToHDMI2, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0008, 0x00}},
	{HKCMD_CMD_ChangeSrcToHDMI3, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0009, 0x00}},
	//{HKCMD_CMD_ChangeSrcToHDMI4, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x000a, 0x00}},
	{HKCMD_CMD_ChangeSrcToVideo1, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x000b, 0x00}},
	{HKCMD_CMD_ChangeSrcToVideo2, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x000c, 0x00}},
	{HKCMD_CMD_ChangeSrcToYpbpr1, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x000d, 0x00}},
	{HKCMD_CMD_ChangeSrcToYpbpr2, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x000e, 0x00}},
	{HKCMD_CMD_ChangeSrcToVGA1, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x000f, 0x00}},
	//{HKCMD_CMD_ChangeSrcToVGA2, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0010, 0x00}},
	{HKCMD_CMD_ChangeSrcToUSB1, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0011, 0x00}},
	{HKCMD_CMD_ChangeSrcToUSB2, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0012, 0x00}},
	//{HKCMD_CMD_ChangeSrcToUSB3, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0013, 0x00}},
	//{HKCMD_CMD_ChangeSrcToYouTube, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0014, 0x00}},
	//{HKCMD_CMD_ChangeSrcToNetflix, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0015, 0x00}},
	//{HKCMD_CMD_ChangeSrcToHome, 	{HKCMD_HEADER, 	HKCMD_TYPE_0000,	0x0bba, 0x0016, 0x00}},
	{HKCMD_CMD_GetCurrentInput, 	{HKCMD_HEADER,	HKCMD_TYPE_0000,	0x0bbb, 0x0000, 0x00}},

};

const UINT16 g_hkCmdTableLen = sizeof(hkCmdTable)/sizeof(hkCmdTable[0]);


UINT8 con_HkRs232_CalcPaketCheckSum(unsigned char *data, UINT16 size)
{
     UINT16 i;
     UINT8 checkSum = 0x0;

     for (i = 0; i < size; i++)
		 checkSum += data[i];

     return checkSum & 0xFF;
}

void con_HkRs232_CMD_Response(UINT16 eCommond,eHkRspType type,UINT8 *data,UINT8 len)
{
	char tempStr[20]={0};
	switch(type)
	{
		case HkRsp_State_OK:
			snprintf(tempStr,sizeof(tempStr),"%d-%s",eCommond,RespOK);
			con_echo_sfu(strlen(tempStr), (UINT8*)tempStr);
			break;
			
		case HkRsp_State_ERR:
			snprintf(tempStr,sizeof(tempStr),"%d-%s",eCommond,RespERR);
			con_echo_sfu(strlen(tempStr), (UINT8*)tempStr);
			break;

		case HkRsp_Data_Invalid:
		default:
			snprintf(tempStr,sizeof(tempStr),"%d-%s",eCommond,RespDataInvalid);
			con_echo_sfu(strlen(tempStr), (UINT8*)tempStr);
			break;
			
		case HkRsp_Data:
			if(data != NULL)
			{
				con_echo_sfu(len, data);
				return;
			}		
	}
}


bool con_HkRs232_AutoTest_parser(pCON_DEV pcon, UINT8 *data)
{
	int ret = 0;
	UINT16 getHeader = 0;
	UINT16 getCmdLen = 0;
	UINT16 CalcCheckSum = 0;
	UINT16 getCommond = 0;

	//check data valid
	getCmdLen = pcon->n;
	/* 2+2 + 2+2 +1 = 9*/
	if(getCmdLen < 9)
		return 1;

	getHeader =  (data[0]<<8) | data[1];
	getCommond = (data[4]<<8) | data[5];

	if(getHeader != HKCMD_HEADER)
		return 1;

	CalcCheckSum = con_HkRs232_CalcPaketCheckSum(data,getCmdLen-1);
	if(data[getCmdLen-1] != CalcCheckSum)
	{
		return 1;
	}
	

	//you can deal with here, also bypass to app deal with
	//pass to app paser
	ret = g_pfpHkRs232_AutoTestCallback(pcon,data);
	switch(ret)
	{
		case HkRsp_Data_Invalid:
			con_HkRs232_CMD_Response(getCommond,HkRsp_Data_Invalid,NULL,0);
			break;
		case HkRsp_State_OK:
			con_HkRs232_CMD_Response(getCommond,HkRsp_State_OK,NULL,0);
			break;				
		case HkRsp_State_ERR:
			con_HkRs232_CMD_Response(getCommond,HkRsp_State_ERR,NULL,0);
			break;
		case HkRsp_Need_App_Resp:
		default:
			break;
	}
	
	return ret;
}

void al_hkrs232_autotest_init(HkRs232_autotest_Cbk_t *pConsole_autotest_Cfg)
{
	if ((pConsole_autotest_Cfg != NULL) && (pConsole_autotest_Cfg->pfCallBack != NULL))
	{
		g_pfpHkRs232_AutoTestCallback = pConsole_autotest_Cfg->pfCallBack;
	}
}



