#ifndef __CONSOLE_CTV_RS232_H_
#define __CONSOLE_CTV_RS232_H_

#include "types.h"
#include "al_basictypes.h"
#include "app_event.h"
#include "al_console.h"
#include "al_console_customer.h"

#define ACCUVIEW_RS232_AutoTest_HEAD               0x79
#define ACCUVIEW_RS232_AutoTest_TAIL               0xFE
#define CMD_DATA_MAX 64

/*
Frame_Head	Type	Cmd_ID	Data_Len	Data_Content	Frame No	Frame Sum	Frame CRC	Frame Tail
1B		4b(H)4b(L)	2B			1B			64B			1B			1B			1B			1B
Frame Head:0x79
Data Length:max is 64B
Frame No:start with 0
Frame Sum:start with 1
CRC: frame type -> frame Sum
Frame Tail:0xFE
*/
typedef struct CommandFrame_t_{
	UINT8 FrameHead;
    UINT8 frameType;
    UINT8 cmdType;
    UINT8 Command_H;
	UINT8 Command_L;
	UINT8 dataLen;
	UINT8 * data;
	UINT8 FrameNo;
	UINT8 FrameSum;
	UINT8 FrameCrc;
	UINT8 FrameTail;
}CommandFrame_t;

typedef struct IrEvent_t_{
    UINT32 Irevent;
    UINT8 *EventString;
    UINT8 stringLen;
}IrEvent_t;

typedef enum DownloadBuffer_e_ {
    DB_CI_PLUS_KEY,
    DB_HDCP14_KEY,
    //DB_HDCP22_KEY,
    DB_BUFFER_MAX,

}DownloadBuffer_e;

typedef enum{
	CTV_RS232_CMD_TEST_CONFIG_NOTIFYE 	= 0x0,
	CTV_RS232_CMD_TEST_READY_NOTIFYE 	= 0x1,
	CTV_RS232_CMD_PCBA_SERIAL_WRITE 	= 0x2,	
	CTV_RS232_CMD_PCBA_SERIAL_READ		= 0x3,	
	CTV_RS232_CMD_HDCP14_KEY_WRITE		= 0x4,	
	CTV_RS232_CMD_HDCP14_KEY_CRC_READ	= 0x5,		
	CTV_RS232_CMD_HDCP22_KEY_WRITE		= 0x6,	
	CTV_RS232_CMD_HDCP22_KEY_READ		= 0x7,			
    CTV_RS232_CMD_CI_PLUS_KEY_WRITE		= 0x8,
    CTV_RS232_CMD_CI_PLUS_KEY_CRC_READ	= 0x9,
	CTV_RS232_CMD_KEY_EVENT				= 0xa,
	CTV_RS232_CMD_SOURCE_CONTROL		= 0xb,
	CTV_RS232_CMD_SOURCE_SINGAL_CHECK	= 0xc,
	CTV_RS232_CMD_CIFUNCTION_CHECK		= 0xd, 
	CTV_RS232_CMD_YPP_ADC_ADJUST		= 0xe,
	CTV_RS232_CMD_VGA_ADC_ADJUST		= 0xf,
	CTV_RS232_CMD_BL_ADJMAX				= 0x10,
	CTV_RS232_CMD_BL_ADJMIN				= 0x11,
	CTV_RS232_CMD_BL_ADJDEF				= 0x12,
	CTV_RS232_CMD_KEY_EVENT_IR			= 0x13,
	CTV_RS232_CMD_PWM_ADJREAD			= 0x14,
	CTV_RS232_CMD_BURNNING_TIME			= 0x15,
	CTV_RS232_CMD_VERSION				= 0x16,
	CTV_RS232_CMD_TEST_RECORD			= 0x17,
	CTV_RS232_CMD_FAC_MODE_EXIT			= 0x18,
	CTV_RS232_CMD_FAC_SHIPPING_MODE		= 0x19,
	CTV_RS232_CMD_FAC_RESSET_ALL		= 0x1a,
	CTV_RS232_CMD_STORAGE_CHK			= 0x1b,
	CTV_RS232_CMD_AUDIO_CONTROL_VOL		= 0x1c,
	CTV_RS232_CMD_LED_CONTROL			= 0x1d,
	CTV_RS232_CMD_POWER_CONTROL			= 0x1e,//not use yet
	CTV_RS232_CMD_AUDIO_CONTROL_MUT		= 0x1f,
	CTV_RS232_CMD_TV_CONTROL_ASC		= 0x20,
	CTV_RS232_CMD_TV_CONTROL_CAB		= 0x21,
	CTV_RS232_CMD_TV_CONTROL_CCH		= 0x22,
	CTV_RS232_CMD_TV_CONTROL_CHU		= 0x23,
	CTV_RS232_CMD_TV_CONTROL_CHD		= 0x24,
	CTV_RS232_CMD_OTHER_CONTROL_SLP		= 0x25,
	CTV_RS232_CMD_OTHER_CONTROL_ASP		= 0x26,
	CTV_RS232_CMD_OTHER_CONTROL_ECC		= 0x27,
	CTV_RS232_CMD_KEYPAD_LOCK			= 0x28,
	CTV_RS232_CMD_DTV_CHANGE_CHANNLE	= 0x29,	
	CTV_RS232_CMD_PANEL_CONTROL,
	CTV_RS232_CMD_FAC_RESET,
	CTV_RS232_CMD_NUM,
}AccuviewRS232Type;

typedef	void (* AccuviewRS232)(al_uint8 eventID, al_uint32 addr);
typedef	void (* RS232_IR_CallBack_t)(UINT32 KeyCode, UINT32 KeyValue,UINT8 keyP);

bool con_AccuviewRs232_AutoTest_parser(pCON_DEV pcon, UINT8* msg, UINT8 frameLen);
bool con_AccuviewRs232_Demux_KeyData_Packets(pCON_DEV pcon,UINT8* msg, UINT16 frameLen);
void con_RegAccuviewRs232CallBackFunc(AccuviewRS232 pCallBackFunc);
void con_AccuviewRs232_HeaderParse(unsigned char* buff, unsigned int bufflen, unsigned int *commandlen);

void con_RegAccuviewRs232IR_CallBackFunc(RS232_IR_CallBack_t pfCallBack);
void AccuviewRs232StartTask(void *arg);

/*****************************************************************************
** FUNCTION : con_AccuviewRs232_CMD_Response
**
** DESCRIPTION :
**				 Response command result to control when app execute funtion.
**
** PARAMETERS :
**				cmdType	 : command event type
**                        returnValue: result
**
** RETURN VALUES:
**				None
*****************************************************************************/
void con_AccuviewRs232_CMD_Response(AccuviewRS232Type cmdType, UINT8 returnValue);

/*****************************************************************************
** FUNCTION : con_AccuviewRs232_IR_Response
**
** DESCRIPTION :
**				 Response ir command to control when app execute funtion.
**
** PARAMETERS :
**				irEvent	-Ir event type
**
**
** RETURN VALUES:
**				None
*****************************************************************************/
void con_AccuviewRs232_IR_Response(AppGlobalEvent_t irEvent);

#endif
