#include <stdlib.h>
#include "al_console_CtvRs232.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"
//#include "app_video.h"
#include "app_guiobj_source.h"
#ifdef CONFIG_CTV_UART_FAC_MODE
#include "app_guiobj_software_upgrade.h"
#endif

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

#define RS232_DEBUG 0

enum Input_UART_FLAG_TYPE0
{
    Input_FLAG_DTV      = BIT(0),
    Input_FLAG_DTV_C 	= BIT(1),
    Input_FLAG_DTV_S2   = BIT(2),
    Input_FLAG_DTV_T2 	= BIT(3),
    Input_FLAG_DTV_T 	= BIT(4),
    Input_FLAG_ATV      = BIT(5),
    Input_FLAG_SCART1	= BIT(6),
    Input_FLAG_YPBPR1   = BIT(7),
};

enum Input_UART_FLAG_TYPE1
{
    Input_FLAG_YPBPR2   = BIT(0),
    Input_FLAG_VGA      = BIT(1),
    Input_FLAG_HDMI1    = BIT(2),
    Input_FLAG_HDMI2    = BIT(3),
    Input_FLAG_HDMI3    = BIT(4),
    Input_FLAG_HDMI4    = BIT(5),
    Input_FLAG_AV1 	    = BIT(6),
    Input_FLAG_AV2      = BIT(7),
};
enum Input_UART_FLAG_TYPE2
{

    Input_FLAG_AV3      = BIT(0),
    Input_FLAG_DVD      = BIT(1),
    Input_FLAG_KARAOKE  = BIT(2),
    Input_FLAG_USB1     = BIT(3),
    Input_FLAG_USB2     = BIT(4),
    Input_FLAG_USB3     = BIT(5),
    Input_FLAG_USB4     = BIT(6),
    Input_FLAG_CI       = BIT(7),
};
enum Input_UART_FLAG_TYPE3
{

    Input_FLAG_AV_YPP  	  = BIT(0),
    Input_FLAG_EAR  	  = BIT(1),
    Input_FLAG_AV_OUT     = BIT(2),
    Input_FLAG_OPTICAL 	  = BIT(3),
    Input_FLAG_COAXIAL 	  = BIT(4),
    Input_FLAG_LINE_OUT	  = BIT(5),
    Input_FLAG_TF_KEYPAD  = BIT(6),
    Input_FLAG_CTV_KEYPAD = BIT(7),
};
enum Input_UART_FLAG_TYPE4
{
    Input_FLAG_SHIPMENT = BIT(0),
    Input_FLAG_REST_ALL = BIT(1),
    Input_FLAG_HDCP14   = BIT(2),
    Input_FLAG_HDCP22	= BIT(3),
    Input_FLAG_CI_PLUS 	= BIT(4),
    Input_FLAG_MAC	    = BIT(5),
    Input_FLAG_WIFI_MAC = BIT(6),
    Input_FLAG_BT_MAC   = BIT(7),
};
enum Input_UART_FLAG_TYPE5
{
    Input_FLAG_SN  	  	   = BIT(0),
    Input_FLAG_TVID  	   = BIT(1),
    Input_FLAG_GDID        = BIT(2),
    Input_FLAG_CUS_KEYPAD1 = BIT(3),
    Input_FLAG_CUS_KEYPAD2 = BIT(4),
    Input_FLAG_CUS_KEYPAD3 = BIT(5),
    Input_FLAG_CUS_KEYPAD4 = BIT(6),
    Input_FLAG_BLUE_TOOTH  = BIT(7),
};
enum Input_UART_FLAG_TYPE6
{
    Input_FLAG_WIFI  	= BIT(0),
    Input_FLAG_LAN  	= BIT(1),
    Input_FLAG_SDCARD   = BIT(2),
    Input_FLAG_PWM_ADJ  = BIT(3),
    Input_FLAG_MIC 		= BIT(4),
    Input_FLAG_CEC 		= BIT(5),
    Input_FLAG_ARC 		= BIT(6),
    Input_FLAG_MHL  	= BIT(7),
};
enum Input_UART_FLAG_TYPE7
{
    Input_FLAG_SMART = BIT(0),
    Input_FLAG_49 = BIT(1),
    Input_FLAG_50 = BIT(2),
    Input_FLAG_51 = BIT(3),
    Input_FLAG_52 = BIT(4),
    Input_FLAG_53 = BIT(5),
    Input_FLAG_54 = BIT(6),
    Input_FLAG_55 = BIT(7),
};

extern Boolean APP_Video_OSD_PQSet(APP_Video_OsdItemType_t bOsdItem, INT16 wOsdValue);
extern int APP_Factory_GetAutoTestOnOff(void);
extern int MID_PartitionList_GetMountedCount(void);

BOOL Rs232_CMD_test_ready  = FALSE;
UINT8 rs232TaskExit = 0;
UINT8 MApp_Input_UART_0 = 0x00;
UINT8 MApp_Input_UART_1 = 0x00;
UINT8 MApp_Input_UART_2 = 0x00;
UINT8 MApp_Input_UART_3 = 0x00;
UINT8 MApp_Input_UART_4 = 0x00;
UINT8 MApp_Input_UART_5 = 0x00;
UINT8 MApp_Input_UART_6 = 0x00;
UINT8 MApp_Input_UART_7 = 0x00;
static AccuviewRS232 Rs232ConCallBack = NULL;
static RS232_IR_CallBack_t Rs232IR = NULL;
BOOL res232_dtv_ch_to_index = FALSE;
BOOL IsCmdShippingMode = FALSE;

static UINT8* gp_DownLoadBuffer[DB_BUFFER_MAX] = {0};
static UINT8* gp_DownLoadFrame[DB_BUFFER_MAX] = {0};
static UINT32 g_DownLoadFrameSum[DB_BUFFER_MAX] = {0};
static UINT32 u32DownLoad_CRCChecksum = 0;
static bool gp_DownLoadReciveSuccess[DB_BUFFER_MAX] = {FALSE};

static UINT8 u8Rs232CmdList[][3] =
{
	{CTV_RS232_CMD_TEST_CONFIG_NOTIFYE,	  0x12, 0x72},
	{CTV_RS232_CMD_TEST_READY_NOTIFYE,	  0x12, 0x73},
	{CTV_RS232_CMD_PCBA_SERIAL_WRITE,	  0x12, 0x74},
	{CTV_RS232_CMD_PCBA_SERIAL_READ,	  0x12, 0x75},
	{CTV_RS232_CMD_HDCP14_KEY_WRITE,      0x12, 0x81},
	{CTV_RS232_CMD_HDCP14_KEY_CRC_READ,	  0x12, 0x82},
	{CTV_RS232_CMD_HDCP22_KEY_WRITE,	  0x12, 0x83},
	{CTV_RS232_CMD_HDCP22_KEY_READ,		  0x12, 0x84},
    {CTV_RS232_CMD_CI_PLUS_KEY_WRITE,     0x12, 0x85},
    {CTV_RS232_CMD_CI_PLUS_KEY_CRC_READ,  0x12, 0x86},
	{CTV_RS232_CMD_KEY_EVENT,			  0x12, 0x87},
	{CTV_RS232_CMD_SOURCE_CONTROL, 		  0x12, 0x88},
	{CTV_RS232_CMD_SOURCE_SINGAL_CHECK,   0x12, 0x89},
    {CTV_RS232_CMD_CIFUNCTION_CHECK,      0x12, 0x8A},   
	{CTV_RS232_CMD_YPP_ADC_ADJUST, 		  0x12, 0x8B},
	{CTV_RS232_CMD_VGA_ADC_ADJUST, 		  0x12, 0x8C},
	{CTV_RS232_CMD_BL_ADJMAX, 			  0x12, 0x8D},
	{CTV_RS232_CMD_BL_ADJMIN, 			  0x12, 0x8E},
	{CTV_RS232_CMD_BL_ADJDEF, 			  0x12, 0x8F},
	{CTV_RS232_CMD_KEY_EVENT_IR,		  0x12, 0x90},
	{CTV_RS232_CMD_PWM_ADJREAD, 		  0x12, 0x95},
	{CTV_RS232_CMD_BURNNING_TIME, 		  0x12, 0x96},
	{CTV_RS232_CMD_VERSION, 			  0x12, 0x97},
	{CTV_RS232_CMD_TEST_RECORD, 		  0x12, 0x98},
	{CTV_RS232_CMD_FAC_MODE_EXIT, 		  0x12, 0x99},
	{CTV_RS232_CMD_FAC_SHIPPING_MODE,	  0x12, 0xA6},   
	{CTV_RS232_CMD_FAC_RESET,			  0x12, 0x9A},   
	{CTV_RS232_CMD_FAC_RESSET_ALL,		0x12, 0x9B},
	{CTV_RS232_CMD_STORAGE_CHK,			0x12, 0x9C},
	{CTV_RS232_CMD_AUDIO_CONTROL_VOL,	0x12, 0x9F},
	{CTV_RS232_CMD_LED_CONTROL,			0x12, 0xA0},
	{CTV_RS232_CMD_POWER_CONTROL, 		0x50, 0x57},
	{CTV_RS232_CMD_AUDIO_CONTROL_MUT, 	0x4D, 0x55},	
	{CTV_RS232_CMD_TV_CONTROL_ASC, 	   	0x53, 0x43},
	{CTV_RS232_CMD_TV_CONTROL_CAB, 		0x43, 0x41},
	{CTV_RS232_CMD_TV_CONTROL_CCH, 		0x43, 0x43},
	{CTV_RS232_CMD_TV_CONTROL_CHU, 		0x43, 0x48},
	{CTV_RS232_CMD_TV_CONTROL_CHD, 		0x43, 0x48},
	{CTV_RS232_CMD_OTHER_CONTROL_SLP, 	0x53, 0x4C},
	{CTV_RS232_CMD_OTHER_CONTROL_ASP, 	0x41, 0x53},
	{CTV_RS232_CMD_OTHER_CONTROL_ECC, 	0x45, 0x43},
	{CTV_RS232_CMD_KEYPAD_LOCK, 		0x55, 0x43},
	{CTV_RS232_CMD_DTV_CHANGE_CHANNLE, 	0x42, 0x43},//This CMD only have two Byte
	{CTV_RS232_CMD_PANEL_CONTROL, 	  	0x12, 0xA5},	
};

static IrEvent_t g_IrEvent[] = 
{
    {UI_EVENT_KEYPAD_MENU, (UINT8*)"MENU", sizeof("MENU") - 1},
    {UI_EVENT_KEYPAD_DOWN, (UINT8*)"CHDOWN", sizeof("CHDOWN") - 1},
    {UI_EVENT_KEYPAD_UP, (UINT8*)"CHUP", sizeof("CHUP") - 1},
    {UI_EVENT_KEYPAD_SOURCE, (UINT8*)"SOURCE", sizeof("SOURCE") - 1},
    {UI_EVENT_KEYPAD_LEFT, (UINT8*)"VODOWN", sizeof("VODOWN") -1},
    {UI_EVENT_KEYPAD_RIGHT, (UINT8*)"VOUP", sizeof("VOUP") -1},
    {UI_EVENT_KEYPAD_POWER, (UINT8*)"POWER", sizeof("POWER") -1},
};

#if 0
static UINT32 _con_RemoveInvaildData(UINT8* dataBuffer, UINT32 bufferLen)
{
    UINT32 i = 0;
    UINT32 index = 0;
    
    for (i = 0; i < bufferLen; i++)
    {
        if (dataBuffer[i] == 0)
        {
            continue;
        }

        dataBuffer[index] = dataBuffer[i];
        index++;
    }

    return index;
}
#endif
static unsigned long RS232DB_CRC32Tab[] =
{
	0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL,
	0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L,
	0x0edb8832L, 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L,
	0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0x90bf1d91L,
	0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
	0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L,
	0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL,
	0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L,
	0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, 0xa2677172L,
	0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
	0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L,
	0x32d86ce3L, 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L,
	0x26d930acL, 0x51de003aL, 0xc8d75180L, 0xbfd06116L,
	0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL,
	0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
	0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL,
	0x76dc4190L, 0x01db7106L, 0x98d220bcL, 0xefd5102aL,
	0x71b18589L, 0x06b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L,
	0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, 0xe10e9818L,
	0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
	0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL,
	0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L,
	0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL,
	0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L,
	0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
	0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL,
	0x4369e96aL, 0x346ed9fcL, 0xad678846L, 0xda60b8d0L,
	0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, 0xdd0d7cc9L,
	0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L,
	0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
	0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L,
	0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL,
	0xedb88320L, 0x9abfb3b6L, 0x03b6e20cL, 0x74b1d29aL,
	0xead54739L, 0x9dd277afL, 0x04db2615L, 0x73dc1683L,
	0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
	0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L,
	0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL,
	0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L,
	0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL,
	0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
	0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L,
	0xd1bb67f1L, 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL,
	0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, 0x41047a60L,
	0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L,
	0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
	0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL,
	0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L,
	0xc2d7ffa7L, 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL,
	0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, 0x026d930aL,
	0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
	0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L,
	0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L,
	0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL,
	0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L,
	0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
	0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L,
	0xa00ae278L, 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L,
	0xa7672661L, 0xd06016f7L, 0x4969474dL, 0x3e6e77dbL,
	0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L,
	0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
	0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L,
	0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL,
	0xb3667a2eL, 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L,
	0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, 0x2d02ef8dL
};


UINT32 _con_RS232DB_CRC32(unsigned char *data, UINT32 size)
{
     UINT32 i;
     UINT32 crc = 0xFFFFFFFF;

     for (i = 0; i < size; i++)
     	crc = RS232DB_CRC32Tab[(crc ^ data[i]) & 0xff] ^ (crc >> 8);

     return crc^0xFFFFFFFF;
}

static AccuviewRS232Type _con_AccuviewRs232_GetCMD(UINT8 cmdH, UINT8 cmdL)
{

	AccuviewRS232Type enReturnValue = CTV_RS232_CMD_NUM;
	UINT8 i;

	for(i=0;i<CTV_RS232_CMD_NUM;i++)
	{
		if(cmdH == u8Rs232CmdList[i][1]
		&& cmdL == u8Rs232CmdList[i][2])
		{
			enReturnValue = (AccuviewRS232Type)u8Rs232CmdList[i][0];
			break;
		}
	}
	return enReturnValue;
}

static UINT8 _conRs232_Dtata_Checksum(UINT8 * dataBuf ,UINT8 datalen)
{

	UINT8 checkSum;
	UINT8   i=0;
	UINT8 sum=0;
	for (i=0;i<datalen;i++)
	{
		sum = (sum+(int)dataBuf[i]) & 0xff;
	}
	checkSum = ((sum^0xff)+1);
	return checkSum;
}

static UINT8 _conRs232_packageCommander(UINT8 *cmdbuf ,AccuviewRS232Type cmdid, UINT8 *data, UINT8 datalen)
{
	UINT8 i=0;	
    UINT32 cmdNum = sizeof(u8Rs232CmdList)/sizeof(u8Rs232CmdList[0]);
    
	cmdbuf[0] = 0x79;
	switch(cmdid)
	{
		case CTV_RS232_CMD_TEST_CONFIG_NOTIFYE://data commond cmd
				cmdbuf[1] = 0x00;
			break;
			
		case CTV_RS232_CMD_PCBA_SERIAL_READ://data commond ask
				cmdbuf[1] = 0x11;
			break;
			
		case CTV_RS232_CMD_FAC_MODE_EXIT://No ask
				cmdbuf[1] = 0x20;
			break;

		default://normal commond ask
				cmdbuf[1] = 0x10;
			break;
	}

    for (i = 0; i < cmdNum; i++)
    {
        if (u8Rs232CmdList[i][0] == cmdid)
        {
            cmdbuf[2] = u8Rs232CmdList[i][1];
            cmdbuf[3] = u8Rs232CmdList[i][2];
            break;
        }
    }

    if (i == cmdNum)
    {
        char printBuf[128] = {0};
        sprintf(printBuf, "cmdid:0x%x is wrong!!!", cmdid);
        con_echo_sfu_Save2Usb(strlen(printBuf), (UINT8*)printBuf);        
        return 0;
    }
    
	cmdbuf[4] = datalen;

	for(i=0;i<datalen;i++)
	{
		cmdbuf[5+i]=data[i];
	}
	
	cmdbuf[datalen+5] = 0x00;
	cmdbuf[datalen+6] = 0x01;
	cmdbuf[datalen+7] = _conRs232_Dtata_Checksum(cmdbuf+1 ,datalen+6);
	cmdbuf[datalen+8] = 0xFE;

	return datalen+9;
}

static void _AccuviewRs232_AutoTest_SourceSet(void)
{
	UINT8 i;
	UINT8 INPUT_HDMI_VIDEO_COUNT = 0;
	UINT8 INPUT_AV_VIDEO_COUNT = 0;
	UINT8 INPUT_YPBPR_VIDEO_COUNT = 0;
	
	for (i = 0; i < g_u16SourceConfigTable_Size; i++)
	{
		switch(g_stSourceConfigTable[i].eSourceType)
		{
#if DEFAULT_SUPPORT_SOURCE_TYPE_DTV
			case APP_SOURCE_DTV:
				MApp_Input_UART_0 |= Input_FLAG_DTV;
				break;
#endif
#if DEFAULT_SUPPORT_SOURCE_TYPE_ATV
			case APP_SOURCE_ATV:
				MApp_Input_UART_0 |= Input_FLAG_ATV;
				break;
#endif			
#if DEFAULT_SUPPORT_SOURCE_TYPE_SCART	
			case APP_SOURCE_SCART:
				MApp_Input_UART_0 |= Input_FLAG_SCART1;
				break;
#endif	
			case APP_SOURCE_AV:
				INPUT_AV_VIDEO_COUNT++;
				break;			
			case APP_SOURCE_AV1:	
				INPUT_AV_VIDEO_COUNT++;
				break;	
			case APP_SOURCE_AV2:
				INPUT_AV_VIDEO_COUNT++;
				break;
			case APP_SOURCE_YPBPR:
				INPUT_YPBPR_VIDEO_COUNT++;
				break;			
			case APP_SOURCE_YPBPR1:	
				INPUT_YPBPR_VIDEO_COUNT++;
				break;	
			case APP_SOURCE_YPBPR2:
				INPUT_YPBPR_VIDEO_COUNT++;
				break;				
			case APP_SOURCE_HDMI:
				INPUT_HDMI_VIDEO_COUNT++;
				break;			
			case APP_SOURCE_HDMI1:	
				INPUT_HDMI_VIDEO_COUNT++;
				break;	
			case APP_SOURCE_HDMI2:
				INPUT_HDMI_VIDEO_COUNT++;
				break;		
#if DEFAULT_SUPPORT_SOURCE_TYPE_PC_RGB
			case APP_SOURCE_PC:
				MApp_Input_UART_1 |= Input_FLAG_VGA;
				break;
#endif				
#ifdef CONFIG_MEDIA_ENABLE				
			case APP_SOURCE_MEDIA:
				MApp_Input_UART_2 |= Input_FLAG_USB1;
				break;
#endif
			//case APP_SOURCE_NET:
#if DEFAULT_SUPPORT_SOURCE_TYPE_DVD
			case APP_SOURCE_DVD:
				MApp_Input_UART_2 |= Input_FLAG_DVD;
				break;		
#endif
#if DEFAULT_SUPPORT_SOURCE_TYPE_ANDRO
			case APP_SOURCE_ANDRO:
#endif
#if defined(CONFIG_SUPPORT_NES_GAME) || defined(CONFIG_OSD_GAME_SUPPORT)		
			case APP_SOURCE_GAME:
#endif		
			default:
				break;	
		}
	}
	
	if(INPUT_AV_VIDEO_COUNT == 1)
	{
#if DEFAULT_SUPPORT_SOURCE_TYPE_AV
		MApp_Input_UART_1 |= Input_FLAG_AV1;
#endif
	}
	else if(INPUT_AV_VIDEO_COUNT == 2)
	{
#if DEFAULT_SUPPORT_SOURCE_TYPE_AV
		MApp_Input_UART_1 |= Input_FLAG_AV1;
#endif
#if DEFAULT_SUPPORT_SOURCE_TYPE_AV2
		MApp_Input_UART_1 |= Input_FLAG_AV2;
#endif
	}	
	else if(INPUT_AV_VIDEO_COUNT == 3)
	{
#if DEFAULT_SUPPORT_SOURCE_TYPE_AV
		MApp_Input_UART_1 |= Input_FLAG_AV1;
#endif
#if DEFAULT_SUPPORT_SOURCE_TYPE_AV2
		MApp_Input_UART_1 |= Input_FLAG_AV2;
#endif
#if DEFAULT_SUPPORT_SOURCE_TYPE_AV3
		MApp_Input_UART_2 |= Input_FLAG_AV3;
#endif
	}
	
	if(INPUT_YPBPR_VIDEO_COUNT == 1)
	{
#if DEFAULT_SUPPORT_SOURCE_TYPE_YPBPR
		MApp_Input_UART_0 |= Input_FLAG_YPBPR1;
#endif
	}
	else if(INPUT_YPBPR_VIDEO_COUNT == 2)
	{
#if DEFAULT_SUPPORT_SOURCE_TYPE_YPBPR
		MApp_Input_UART_0 |= Input_FLAG_YPBPR1;
#endif
#if DEFAULT_SUPPORT_SOURCE_TYPE_YPBPR2		
		MApp_Input_UART_1 |= Input_FLAG_YPBPR2;
#endif
	}	
	
	if(INPUT_HDMI_VIDEO_COUNT == 1)
	{
		MApp_Input_UART_1 |= Input_FLAG_HDMI1;
	}
	else if(INPUT_HDMI_VIDEO_COUNT == 2)
	{
		MApp_Input_UART_1 |= Input_FLAG_HDMI1;
		MApp_Input_UART_1 |= Input_FLAG_HDMI2;
	}	
	else if(INPUT_HDMI_VIDEO_COUNT == 3)
	{
		MApp_Input_UART_1 |= Input_FLAG_HDMI1;
		MApp_Input_UART_1 |= Input_FLAG_HDMI2;
		MApp_Input_UART_1 |= Input_FLAG_HDMI3;
	}
#ifdef 	CONFIG_DVB_SYSTEM_DVBC_SUPPORT
	MApp_Input_UART_0 |= Input_FLAG_DTV_C;
#endif	
#ifdef 	CONFIG_DVB_SYSTEM_DVBS_SUPPORT
	MApp_Input_UART_0 |= Input_FLAG_DTV_S2;
#endif		
#ifdef 	CONFIG_DVB_SYSTEM_DVBT2_SUPPORT
	MApp_Input_UART_0 |= Input_FLAG_DTV_T2;
#endif	
	MApp_Input_UART_0 |= Input_FLAG_DTV_T;
	
#ifdef 	CONFIG_CI_SUPPORT
	MApp_Input_UART_2 |= Input_FLAG_CI;
#endif
#if defined(CONFIG_HEADPHONE_DETECT_BY_ISR) || defined(CONFIG_HPDET_BY_SWTIMER)
	MApp_Input_UART_3 |= Input_FLAG_EAR;
#endif	

#ifdef 	CONFIG_SUPPORT_CVBSOUT
	MApp_Input_UART_3 |= Input_FLAG_AV_OUT;
#endif	
#ifdef 	CONFIG_SUPPORT_SPDIF_OUT
	MApp_Input_UART_3 |= Input_FLAG_COAXIAL;
#endif
#ifdef 	CONFIG_SUPPORT_SPDIF_OUT
	MApp_Input_UART_3 |= Input_FLAG_COAXIAL;
#endif		

    MApp_Input_UART_4 |= Input_FLAG_HDCP14;
    MApp_Input_UART_4 |= Input_FLAG_HDCP22;
#ifdef CONFIG_CIPLUS_SUPPORT
	MApp_Input_UART_4 |= Input_FLAG_CI_PLUS;
#endif   

    MApp_Input_UART_5 |= Input_FLAG_SN;
	
#ifdef SUPPORT_CEC_TV
	MApp_Input_UART_6 |= Input_FLAG_CEC;
#endif   	
#ifdef	CONFIG_SUPPORT_ARC
	MApp_Input_UART_6 |= Input_FLAG_ARC;
#endif   
}

int con_AccuviewRs232_GetIrEvnt_t(AppGlobalEvent_t irEvent, IrEvent_t *pirEvent)
{
    UINT8 i = 0;

    for (i = 0; i < sizeof(g_IrEvent); i++)
    {
        if (g_IrEvent[i].Irevent == irEvent)
        {
            memcpy(pirEvent, g_IrEvent + i, sizeof(IrEvent_t));
            return 0;
        }
    }

    return 0;
    
}

void con_AccuviewRs232_IR_Response(AppGlobalEvent_t irEvent)
{
    if (!Rs232_CMD_test_ready)
    {
        printf("sf is not ready!!!\n");
        return ;
    }
    
    UINT8 echodata[30] = {0};
    UINT8 returnLen = 0;
    IrEvent_t data_Response;
    UINT8 *data_response;
    UINT8 data_return[] = {0x00};
    UINT8 data_len = 0;
    memset(&data_Response, 0, sizeof(IrEvent_t));

    con_AccuviewRs232_GetIrEvnt_t(irEvent, &data_Response);

    if (data_Response.stringLen)
    {
       #ifdef CONFIG_KEYPAD_SINGLE_REUSE
	       #ifdef SUPPORT_PCB_ONLY_HAVE_ONE_KEYPAD
			if(irEvent == UI_EVENT_KEYPAD_MENU)
			{ 
			    UINT8 i = 0;
				for( i = 0; i < 7; i++)	
			    {
			        memset(&data_Response, 0, sizeof(IrEvent_t));
	        		data_response = g_IrEvent[i].EventString;
	        		data_len = g_IrEvent[i].stringLen;
	        		returnLen = _conRs232_packageCommander(echodata, CTV_RS232_CMD_KEY_EVENT, data_response, data_len);
					con_echo_sfu(returnLen, echodata);
			    }
				return;
			}
			#else
			{
				data_response = data_Response.EventString;
				data_len = data_Response.stringLen;
				returnLen = _conRs232_packageCommander(echodata, CTV_RS232_CMD_KEY_EVENT, data_response, data_len);
			}
			#endif
		#else
			data_response = data_Response.EventString;
        	data_len = data_Response.stringLen;
        	returnLen = _conRs232_packageCommander(echodata, CTV_RS232_CMD_KEY_EVENT, data_response, data_len);
		#endif
    }
    else
    {
      if((irEvent == UI_EVENT_LEFT)||(irEvent == UI_EVENT_RIGHT ))
      	{
	        data_response = data_return;
	        data_len = 1;
	        returnLen = _conRs232_packageCommander(echodata, CTV_RS232_CMD_KEY_EVENT_IR, data_response, data_len);
      	}
    }
    
    con_echo_sfu(returnLen, echodata);

}

void con_AccuviewRs232_CMD_Response(AccuviewRS232Type cmdType, UINT8 returnValue)
{
    if (!Rs232_CMD_test_ready)
    {
        con_echo_sfu_Save2Usb(strlen("sf is not ready!!!"), (UINT8*)"sf is not ready!!!");
        return ;
    }

    if (cmdType >= CTV_RS232_CMD_NUM)
    {
        con_echo_sfu_Save2Usb(strlen("cmdType is verstep!!!"), (UINT8*)"cmdType is verstep!!!");
        return ;
    }
    
    UINT8 echodata[30] = {0};
    UINT8 returnLen = 0;
    UINT8 data_return[] = {0x00};
    data_return[0] = returnValue;

    returnLen = _conRs232_packageCommander(echodata, cmdType, data_return, 1);
    
    con_echo_sfu(returnLen, echodata);

}

bool con_AccuviewRs232_GetOneFrame_From_Packets(UINT8* massData,UINT16 startAddr,UINT8* frame,UINT8 *frameLen)
{
	UINT8 oneFrameData[74]={0};
	UINT8 oneFrameSize = 0;
    INT8 errString[128] = {0};
    CommandFrame_t CurrentFrame={0};
	
	if(!massData || !frame || !frameLen)
	{
		return false;
	}
	memcpy(oneFrameData, massData+startAddr, sizeof(oneFrameData)-1);
	CurrentFrame.dataLen = *(oneFrameData+4);
	oneFrameSize = 5 + CurrentFrame.dataLen + 4;
	CurrentFrame.FrameTail= *(oneFrameData+oneFrameSize-1);
	if(CurrentFrame.FrameTail != ACCUVIEW_RS232_AutoTest_TAIL)
	{
		memset(errString, 0, 128);
		strcpy(errString, "FrameTail is not 0xFE !!");
		con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
		return false;
	}	
	*frameLen = oneFrameSize;
	memcpy(frame, massData+startAddr, oneFrameSize);
	return true;
}


bool con_AccuviewRs232_Demux_KeyData_Packets(pCON_DEV pcon,UINT8* msg, UINT16 msgLen)
{
	bool ret = FALSE;
    CommandFrame_t CurrentFrame;
	UINT8 oneFrameSize = 0;
    UINT16 framePosition = 0;
    INT8 errString[128] = {0};

    memset(&CurrentFrame, 0, sizeof(CurrentFrame));

	CurrentFrame.dataLen = *(msg+4);
	oneFrameSize = 5 + CurrentFrame.dataLen + 4;
	CurrentFrame.FrameTail= *(msg+oneFrameSize-1);
	
	//check first frame tail is right
	if(CurrentFrame.FrameTail != ACCUVIEW_RS232_AutoTest_TAIL)
	{
		memset(errString, 0, 128);
		strcpy(errString, "FrameTail is not 0xFE !!");
		con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
		return false;
	}

	//check msg size is right
	if(msgLen < oneFrameSize)
	{
		memset(errString, 0, 128);
		strcpy(errString, "FrameSize is not right !!");
		con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
		return false;
	}
	else if(msgLen == oneFrameSize)
	{
		//oneFrame
		ret = con_AccuviewRs232_AutoTest_parser(pcon,(UINT8*)(msg+1),oneFrameSize);		
		return ret;
	}
	else
	{
		do{
			UINT8 oneFrameData[74]={0};
			ret = con_AccuviewRs232_GetOneFrame_From_Packets(msg,framePosition,oneFrameData,&oneFrameSize);
			if(ret == false)
				return false;
			ret = con_AccuviewRs232_AutoTest_parser(pcon,(UINT8*)(msg+framePosition+1),oneFrameSize); 	
			if(ret == false)
				return false;
			framePosition = framePosition + oneFrameSize;
		}while(framePosition < msgLen);
	}
	
	return ret;
}


bool con_AccuviewRs232_AutoTest_parser(pCON_DEV pcon, UINT8* msg, UINT8 frameLen)
{
    CommandFrame_t CurrentFrame;
	AccuviewRS232Type cmdIdType;
    UINT8 echodata[30] = {0};
    UINT8 returnLen = 0;
	UINT8 calcframCRC = 0;
    INT8 errString[128] = {0};
	UINT32 u8KeyCode;

    memset(&CurrentFrame, 0, sizeof(CurrentFrame));

    CurrentFrame.frameType = (*(msg+0) & 0xf0) >> 4;
    CurrentFrame.cmdType = *(msg+0) & 0x0f;
    CurrentFrame.Command_H = *(msg+1);
    CurrentFrame.Command_L= *(msg+2);
	if(frameLen >=9)
	{
    	CurrentFrame.dataLen = *(msg+3);
    	CurrentFrame.FrameNo = *(msg+4+CurrentFrame.dataLen);
    	CurrentFrame.FrameSum = *(msg+4+CurrentFrame.dataLen+1);
    	CurrentFrame.FrameCrc = *(msg+4+CurrentFrame.dataLen+2);
    	calcframCRC = _conRs232_Dtata_Checksum(msg,frameLen-3);
	}
    
    cmdIdType = _con_AccuviewRs232_GetCMD(CurrentFrame.Command_H, CurrentFrame.Command_L);

    if (!Rs232_CMD_test_ready)
    {
        if (cmdIdType != CTV_RS232_CMD_TEST_CONFIG_NOTIFYE && cmdIdType != CTV_RS232_CMD_TEST_READY_NOTIFYE)
        {           
            return false;
        }
    }

	if(cmdIdType != CTV_RS232_CMD_TEST_CONFIG_NOTIFYE && cmdIdType != CTV_RS232_CMD_TEST_READY_NOTIFYE)
	{
        if (!CurrentFrame.FrameSum)
        {
            memset(errString, 0, 128);
            strcpy(errString, "CurrentFrame.FrameSum is 0!!");
            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
            con_echo_sfu_Save2Usb(frameLen, msg);
            return false;        
        }
        
		if(calcframCRC != CurrentFrame.FrameCrc)//check crc error
		{
		    memset(errString, 0, 128);
            strcpy(errString, "calcframCRC is error!!");
            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
            con_echo_sfu_Save2Usb(frameLen, msg);
            return false;
		}
	}
    
    if (CurrentFrame.dataLen)
    {
        CurrentFrame.data = malloc(CurrentFrame.dataLen+1);
        if (!CurrentFrame.data)
        {
            memset(errString, 0, 128);
            strcpy(errString, "CurrentFrame.data malloc wrong!!");
            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
            return false;
        }
        memset(CurrentFrame.data, 0, (CurrentFrame.dataLen+1));

        memcpy(CurrentFrame.data, msg+4, CurrentFrame.dataLen);        
    }
		
    AL_FW_ActiveFWModule(FW_RS232_MODE);
#if 0    
    con_echo_sfu(4, (UINT8*)"====");
    con_echo_sfu(1, (UINT8*)&cmdIdType);//AccuviewRS232Type
    con_echo_sfu(1, (UINT8*)&calcframCRC);//calcframCRC
    con_echo_sfu(4, (UINT8*)"----");
#endif

	//check callback function
	if(Rs232IR == NULL)//system must register rs232ir callback
	{
		return false;		 
	}
	
    switch (cmdIdType)
    {
		case CTV_RS232_CMD_POWER_CONTROL:
			if(CurrentFrame.data == 0x00)
			{
				//u8KeyCode = KEY_POWER;
				Rs232IR(UI_EVENT_POWER,0x0, 0x0);
				UINT8 ReturnParam[] ={0x01};
				ReturnParam[0] = 0x00;
				returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam, sizeof(ReturnParam));
				con_echo_sfu(returnLen, echodata);
			}
			break;
		case CTV_RS232_CMD_AUDIO_CONTROL_MUT:
			if(CurrentFrame.data == 0x00)
			{
				//u8KeyCode = KEY_POWER;
				Rs232IR(UI_EVENT_MUTE,0x0, 0x0);
				UINT8 ReturnParam[] ={0x01};
				ReturnParam[0] = 0x00;
				returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam, sizeof(ReturnParam));
				con_echo_sfu(returnLen, echodata);
			}
			break;
		case CTV_RS232_CMD_AUDIO_CONTROL_VOL:
            {
				UINT8 date_SCtr_return[] ={0x00};
                
                if (!CurrentFrame.dataLen)
                {
                    date_SCtr_return[0] = 0x01;
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, date_SCtr_return, sizeof(date_SCtr_return));
                    con_echo_sfu(returnLen, echodata);
                    break;
                }
                
                UINT32 volVal = 0;

                volVal = CurrentFrame.data[0];

                
				if(volVal > 100)
				{
				    date_SCtr_return[0] = 0x01;
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, date_SCtr_return, sizeof(date_SCtr_return));
                    con_echo_sfu(returnLen, echodata);
				}
                else
                {
                    Rs232ConCallBack(cmdIdType, volVal);
                }
                
            }
			break;
            
		 case CTV_RS232_CMD_LED_CONTROL:
            {
    			UINT8 data_LED_Control[] ={0x01};

                if (!CurrentFrame.dataLen)
                {
                    returnLen = _conRs232_packageCommander(echodata,cmdIdType,data_LED_Control,sizeof(data_LED_Control));
                    con_echo_sfu(returnLen, echodata);
                    break;
                }
                
                if (strncmp((char*)CurrentFrame.data, "RED_ON", strlen("RED_ON")) == 0)
                {
                    //Cmd_SET_LED_Status(LED_DISABLE);
                    data_LED_Control[0] = Cmd_SET_LED_Status(LED_ONLY_ENABLE_RED);
                }
                
                if (strncmp((char*)CurrentFrame.data, "GREEN_ON", strlen("GREEN_ON")) == 0)
                {
                    //Cmd_SET_LED_Status(LED_DISABLE);
                    data_LED_Control[0] = Cmd_SET_LED_Status(LED_ONLY_ENABLE_GREEN);
                }
                
                if ((strncmp((char*)CurrentFrame.data, "RED_OFF", strlen("RED_OFF")) == 0)|| (strncmp((char*)CurrentFrame.data, "GREEN_OFF", strlen("GREEN_OFF")) == 0))
                {
                    data_LED_Control[0] = Cmd_SET_LED_Status(LED_DISABLE);
                }

                returnLen = _conRs232_packageCommander(echodata,cmdIdType,data_LED_Control,sizeof(data_LED_Control));
                con_echo_sfu(returnLen, echodata);

            }
			break;		
            
		case CTV_RS232_CMD_TEST_CONFIG_NOTIFYE://send check ietms to PC
            {
                _AccuviewRs232_AutoTest_SourceSet();

                UINT8 date_SourceDefine[] = {MApp_Input_UART_0, MApp_Input_UART_1, MApp_Input_UART_2, MApp_Input_UART_3, 
                                             MApp_Input_UART_4, MApp_Input_UART_5, MApp_Input_UART_6, MApp_Input_UART_7};

                returnLen = _conRs232_packageCommander(echodata,cmdIdType,date_SourceDefine,sizeof(date_SourceDefine));
                con_echo_sfu(returnLen, echodata);
                //return true;
            }
			break;
								
		case CTV_RS232_CMD_TEST_READY_NOTIFYE:
            {
    			Rs232_CMD_test_ready = TRUE;

                while(!rs232TaskExit)
                    usleep(50);
                
    			UINT8 data_TR[] ={0x00};
    		    returnLen = _conRs232_packageCommander(echodata,cmdIdType,data_TR,sizeof(data_TR));
        		con_echo_sfu(returnLen, echodata);
                //return true;
            }

    		break;
							
		case CTV_RS232_CMD_PCBA_SERIAL_WRITE:
            {
				UINT8 data_PCBA_SERIAL_WRITE[] ={0x00};
                if (!CurrentFrame.dataLen)
                {
                    data_PCBA_SERIAL_WRITE[0] = 0x01;
                    returnLen = _conRs232_packageCommander(echodata,cmdIdType,data_PCBA_SERIAL_WRITE,sizeof(data_PCBA_SERIAL_WRITE));
                    con_echo_sfu(returnLen, echodata);
                    break;
                }
                
				returnLen = 0;

                if (CurrentFrame.dataLen > CTV_RS232_PCBA_LEN)
                {
                    memset(errString, 0, 128);
                    strcpy(errString, "CurrentFrame.dataLen is max to CTV_RS232_PCBA_LEN!!");
                    con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
                    break;
                }
				
				#if 0 //def CONFIG_CTV_UART_FAC_MODE
				APP_SETTING_PcbaSNTable_t dataPcbaSN;
				APP_PCBSNDataWriteRead(FALSE,(UINT8 *)&dataPcbaSN);
				memcpy(dataPcbaSN.n_PcbaSN, CurrentFrame.data, CTV_RS232_PCBA_LEN);
				APP_PCBSNDataWriteRead(TRUE,(UINT8 *)&(dataPcbaSN.n_PcbaSN));
				#else
                
                AL_Setting_Read(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER, 0,
                    sizeof(APP_SETTING_FactoryUser_t), &(g_stFactoryUserData));

                memcpy(g_stFactoryUserData.n_PcbaSN, CurrentFrame.data, CTV_RS232_PCBA_LEN);

                AL_Setting_Write(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER,
                    ITEM_OFFSET(APP_SETTING_FactoryUser_t, n_PcbaSN),
                    sizeof(g_stFactoryUserData.n_PcbaSN),&(g_stFactoryUserData.n_PcbaSN));
                
                AL_Setting_Store(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER,
                    ITEM_OFFSET(APP_SETTING_FactoryUser_t, n_PcbaSN),sizeof(g_stFactoryUserData.n_PcbaSN));
                
			   #endif
				returnLen = _conRs232_packageCommander(echodata,cmdIdType,data_PCBA_SERIAL_WRITE,sizeof(data_PCBA_SERIAL_WRITE));
        		con_echo_sfu(returnLen, echodata);
            }
			break;
							
		case CTV_RS232_CMD_PCBA_SERIAL_READ:
            {
				#if 0//def CONFIG_CTV_UART_FAC_MODE
				APP_SETTING_PcbaSNTable_t dataPcbaSN;
				APP_PCBSNDataWriteRead(FALSE,(UINT8 *)&dataPcbaSN);
				UINT8 data_PcbaSN[CTV_RS232_PCBA_LEN] ={0x00};
				memcpy(data_PcbaSN, dataPcbaSN.n_PcbaSN, CTV_RS232_PCBA_LEN);
				#else
                AL_Setting_Read(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER, 0,
                    sizeof(APP_SETTING_FactoryUser_t), &(g_stFactoryUserData));
                
                UINT8 data_PcbaSN[CTV_RS232_PCBA_LEN] ={0x00};
                
                memcpy(data_PcbaSN, g_stFactoryUserData.n_PcbaSN, CTV_RS232_PCBA_LEN);
                #endif    
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_PcbaSN, sizeof(data_PcbaSN));
                con_echo_sfu(returnLen, echodata);
            }
			break;
		
		case CTV_RS232_CMD_HDCP14_KEY_WRITE:
            {

                if (gp_DownLoadReciveSuccess[DB_HDCP14_KEY] && ((CurrentFrame.FrameSum - 1) == CurrentFrame.FrameNo))
                {
                    UINT8 data_HDCP14_return[] ={0x00};
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_HDCP14_return, sizeof(data_HDCP14_return));
                    con_echo_sfu(returnLen, echodata);
                    if (gp_DownLoadBuffer[DB_HDCP14_KEY])
                    {
                        free(gp_DownLoadBuffer[DB_HDCP14_KEY]);
                        gp_DownLoadBuffer[DB_HDCP14_KEY] = NULL;
                    }
                    
                    if (gp_DownLoadFrame[DB_HDCP14_KEY])
                    {
                        free(gp_DownLoadFrame[DB_HDCP14_KEY]);
                        gp_DownLoadFrame[DB_HDCP14_KEY] = NULL;
                    }
                    g_DownLoadFrameSum[DB_HDCP14_KEY] = 0;					
                    break;
                }
                    
                if (!CurrentFrame.dataLen)
                {
                    break;
                }
                
                if (g_DownLoadFrameSum[DB_HDCP14_KEY] == 0)
                {
                    if(gp_DownLoadBuffer[DB_HDCP14_KEY] == NULL)
                    {
                        gp_DownLoadBuffer[DB_HDCP14_KEY] = malloc(CurrentFrame.FrameSum*CMD_DATA_MAX);
                        if (!gp_DownLoadBuffer[DB_HDCP14_KEY])
                        {
                            memset(errString, 0, 128);
                            strcpy(errString, "gp_DownLoadBuffer[DB_HDCP14_KEY] malloc error!!");
                            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
                            break;                        
                        }
                        memset(gp_DownLoadBuffer[DB_HDCP14_KEY], 0, (CurrentFrame.FrameSum*CMD_DATA_MAX));
                    }
                    
                    if (gp_DownLoadFrame[DB_HDCP14_KEY] == NULL)
                    {
                        gp_DownLoadFrame[DB_HDCP14_KEY] = malloc(CurrentFrame.FrameSum);                    
                        if (!gp_DownLoadBuffer[DB_HDCP14_KEY])
                        {
                            memset(errString, 0, 128);
                            strcpy(errString, "gp_DownLoadFrame[DB_HDCP14_KEY] malloc error!!");
                            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
                            if (gp_DownLoadBuffer[DB_HDCP14_KEY])
                            {
                                free(gp_DownLoadBuffer[DB_HDCP14_KEY]);
                                gp_DownLoadBuffer[DB_HDCP14_KEY] = NULL;
                            }
                            break;                        
                        }
                        memset(gp_DownLoadFrame[DB_HDCP14_KEY], 0, (CurrentFrame.FrameSum));
                    }
                }

#if RS232_DEBUG                
                {
                    UINT8 debugMesg[1024] = {0};
                    sprintf((char*)debugMesg, "--->[DB_HDCP14_KEY]urrentFrame.FrameNo:%d<===", CurrentFrame.FrameNo);
                    con_echo_sfu_Save2Usb(strlen((char*)debugMesg), debugMesg);
                }
#endif
                if (gp_DownLoadFrame[DB_HDCP14_KEY][CurrentFrame.FrameNo] == 0)
                {
                    UINT8* u32DownloadBufferAddr = gp_DownLoadBuffer[DB_HDCP14_KEY]+CurrentFrame.FrameNo*CMD_DATA_MAX;                                              
                    memcpy((void*)(u32DownloadBufferAddr), (void*)CurrentFrame.data, CurrentFrame.dataLen);
                    gp_DownLoadFrame[DB_HDCP14_KEY][CurrentFrame.FrameNo] = 1;
                    g_DownLoadFrameSum[DB_HDCP14_KEY]++;
                }

                if(g_DownLoadFrameSum[DB_HDCP14_KEY] == CurrentFrame.FrameSum)
                {
                    UINT32 HdcpKeyLen = (CurrentFrame.FrameSum-1)*CMD_DATA_MAX + CurrentFrame.dataLen;
#if RS232_DEBUG                
                    {
                        UINT8 debugMesg[1024] = {0};
                        sprintf((char*)debugMesg, "--->[DB_HDCP14_KEY]gp_DownLoadBuffer[DB_HDCP14_KEY]:");
                        con_echo_sfu_Save2Usb(strlen((INT8*)debugMesg), debugMesg);
                        con_echo_sfu_Save2Usb(HdcpKeyLen, gp_DownLoadBuffer[DB_HDCP14_KEY]);
                        con_echo_sfu_Save2Usb(4, (UINT8*)"<===");
                    }
#endif
                    u32DownLoad_CRCChecksum =0;
					u32DownLoad_CRCChecksum = _con_RS232DB_CRC32(gp_DownLoadBuffer[DB_HDCP14_KEY], HdcpKeyLen);

                    //write HDCP KEY
                    e_MID_KEYUPDATE_STATUS HdcpUpdateReturn = 0;
                    //HdcpKeyLen = _con_RemoveInvaildData(gp_DownLoadBuffer[DB_HDCP14_KEY], HdcpKeyLen);
                    HdcpUpdateReturn = MID_KEYUPDATE_HDCP_Update(gp_DownLoadBuffer[DB_HDCP14_KEY] , HdcpKeyLen);
                    if (gp_DownLoadBuffer[DB_HDCP14_KEY])
                    {
                        free(gp_DownLoadBuffer[DB_HDCP14_KEY]);
                        gp_DownLoadBuffer[DB_HDCP14_KEY] = NULL;
                    }
                    
                    if (gp_DownLoadFrame[DB_HDCP14_KEY])
                    {
                        free(gp_DownLoadFrame[DB_HDCP14_KEY]);
                        gp_DownLoadFrame[DB_HDCP14_KEY] = NULL;
                    }
                    g_DownLoadFrameSum[DB_HDCP14_KEY] = 0;
                    UINT8 data_HDCP14_return[] ={0x00};
                    if (HdcpUpdateReturn != e_MID_STATUS_UPDATE_SUCCESS)
                    {
                        data_HDCP14_return[0] = 0x01;
                    }
                    else
                    {
                        gp_DownLoadReciveSuccess[DB_HDCP14_KEY] = TRUE;
                    }
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_HDCP14_return, sizeof(data_HDCP14_return));
                    con_echo_sfu(returnLen, echodata);
                }
            }
            break;
	
		case CTV_RS232_CMD_HDCP14_KEY_CRC_READ:
            {
				returnLen=0;
				UINT8 U8HDCP14CRCChecksum[4];
				U8HDCP14CRCChecksum[0] = (UINT8)(u32DownLoad_CRCChecksum >> 24);
				U8HDCP14CRCChecksum[1] = (UINT8)(u32DownLoad_CRCChecksum >> 16);
				U8HDCP14CRCChecksum[2] = (UINT8)(u32DownLoad_CRCChecksum >> 8);
				U8HDCP14CRCChecksum[3] = (UINT8)(u32DownLoad_CRCChecksum&0xFF);
				//printf("=====CRC32:0x%x====U8CIKeyCRCChecksum[0]:0x%x [1]:0x%x [2]:0x%x [3]:0x%x\n",g_UartCommand_ext.u32DownLoad_CRCChecksum,U8CIKeyCRCChecksum[0],U8CIKeyCRCChecksum[1],U8CIKeyCRCChecksum[2],U8CIKeyCRCChecksum[3]);
				returnLen = _conRs232_packageCommander(echodata, cmdIdType, U8HDCP14CRCChecksum, sizeof(U8HDCP14CRCChecksum));
				con_echo_sfu(returnLen, echodata);
                gp_DownLoadReciveSuccess[DB_HDCP14_KEY] = FALSE;
            }
            
			break;
							
		case CTV_RS232_CMD_HDCP22_KEY_WRITE:
			;//
			break;
		case CTV_RS232_CMD_HDCP22_KEY_READ:
			;//
			break;			

        case CTV_RS232_CMD_CI_PLUS_KEY_WRITE:
            {
                if (gp_DownLoadReciveSuccess[DB_CI_PLUS_KEY] && ((CurrentFrame.FrameSum - 1) == CurrentFrame.FrameNo))
                {
                    UINT8 data_CIkey_return[] ={0x00};
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_CIkey_return, sizeof(data_CIkey_return));
                    con_echo_sfu(returnLen, echodata);
					
					if (gp_DownLoadBuffer[DB_CI_PLUS_KEY])
                    {
                        free(gp_DownLoadBuffer[DB_CI_PLUS_KEY]);
                        gp_DownLoadBuffer[DB_CI_PLUS_KEY] = NULL;
                    }
                    
                    if (gp_DownLoadFrame[DB_CI_PLUS_KEY])
                    {
                        free(gp_DownLoadFrame[DB_CI_PLUS_KEY]);
                        gp_DownLoadFrame[DB_CI_PLUS_KEY] = NULL;
                    }
                    g_DownLoadFrameSum[DB_CI_PLUS_KEY] = 0;
                    break;
                }
                
                if (!CurrentFrame.dataLen)
                {
                    break;
                }
                
                if (g_DownLoadFrameSum[DB_CI_PLUS_KEY] == 0)
                {
                    if(gp_DownLoadBuffer[DB_CI_PLUS_KEY] == NULL)
                    {
                        gp_DownLoadBuffer[DB_CI_PLUS_KEY] = malloc(CurrentFrame.FrameSum*CMD_DATA_MAX);
                        if (!gp_DownLoadBuffer[DB_CI_PLUS_KEY])
                        {
                            memset(errString, 0, 128);
                            sprintf(errString, "gp_DownLoadBuffer[DB_CI_PLUS_KEY] malloc wrong!");
                            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
                            break;                        
                        }
                        memset(gp_DownLoadBuffer[DB_CI_PLUS_KEY], 0, (CurrentFrame.FrameSum*CMD_DATA_MAX));
                    }
                    
                    if (gp_DownLoadFrame[DB_CI_PLUS_KEY] == NULL)
                    {
                        gp_DownLoadFrame[DB_CI_PLUS_KEY] = malloc(CurrentFrame.FrameSum);                    
                        if (!gp_DownLoadBuffer[DB_CI_PLUS_KEY])
                        {
                            memset(errString, 0, 128);
                            sprintf(errString, "gp_DownLoadBuffer[DB_CI_PLUS_KEY] malloc wrong!");
                            con_echo_sfu_Save2Usb(strlen(errString), (UINT8*)errString);
                            if (gp_DownLoadBuffer[DB_CI_PLUS_KEY])
                            {
                                free(gp_DownLoadBuffer[DB_CI_PLUS_KEY]);
                                gp_DownLoadBuffer[DB_CI_PLUS_KEY] = NULL;
                            }
                            break;                        
                        }
                        memset(gp_DownLoadFrame[DB_CI_PLUS_KEY], 0, (CurrentFrame.FrameSum));
                    }
                }

#if RS232_DEBUG                
                {      
                    UINT8 debugMesg[1024] = {0};
                    sprintf((char*)debugMesg, "No:%d", CurrentFrame.FrameNo);
                    con_echo_sfu(strlen((char*)debugMesg), debugMesg);
                }
#endif

                if (gp_DownLoadFrame[DB_CI_PLUS_KEY][CurrentFrame.FrameNo] == 0)
                {
                    UINT8* u32DownloadBufferAddr = gp_DownLoadBuffer[DB_CI_PLUS_KEY]+CurrentFrame.FrameNo*CMD_DATA_MAX;                                              
                    memcpy((void*)(u32DownloadBufferAddr), (void*)CurrentFrame.data, CurrentFrame.dataLen);
                    gp_DownLoadFrame[DB_CI_PLUS_KEY][CurrentFrame.FrameNo] = 1;
                    g_DownLoadFrameSum[DB_CI_PLUS_KEY]++;
                }
				
#ifdef CTV_DEBUG
                {      
                    UINT8 debugMesg[1024] = {0};
                    sprintf((char*)debugMesg, "No:%d,%d,%d", CurrentFrame.FrameNo,gp_DownLoadFrame[DB_CI_PLUS_KEY][CurrentFrame.FrameNo],g_DownLoadFrameSum[DB_CI_PLUS_KEY]);
                    con_echo_sfu_Save2Usb(strlen((char*)debugMesg), debugMesg);
                }
#endif
                if(g_DownLoadFrameSum[DB_CI_PLUS_KEY] == CurrentFrame.FrameSum)
                {
                    UINT32 CIkeyKeyLen = (CurrentFrame.FrameSum-1)*CMD_DATA_MAX + CurrentFrame.dataLen;
#if RS232_DEBUG                
                    {
                        UINT8 debugMesg[1024] = {0};
                        sprintf((char*)debugMesg, "--->CIkeyKeyLen:%d, CurrentFrame.dataLen:%d===", CIkeyKeyLen, CurrentFrame.dataLen);
                        con_echo_sfu_Save2Usb(strlen((INT8*)debugMesg), debugMesg);
                        con_echo_sfu_Save2Usb(CIkeyKeyLen, gp_DownLoadBuffer[DB_CI_PLUS_KEY]);
                        con_echo_sfu_Save2Usb(4, (UINT8*)"<===");
                    }
#endif
                    u32DownLoad_CRCChecksum =0;
					u32DownLoad_CRCChecksum = _con_RS232DB_CRC32(gp_DownLoadBuffer[DB_CI_PLUS_KEY], CIkeyKeyLen);
                    //write CIkey KEY
                    e_MID_KEYUPDATE_STATUS CiUpdateReturn = 0;
                    //CIkeyKeyLen = _con_RemoveInvaildData(gp_DownLoadBuffer[DB_CI_PLUS_KEY], CIkeyKeyLen);
                    CiUpdateReturn = MID_KEYUPDATE_CIPlus_Update(gp_DownLoadBuffer[DB_CI_PLUS_KEY] , CIkeyKeyLen);
              
                    UINT8 data_CIkey_return[] ={0x00};
                    if (CiUpdateReturn != e_MID_STATUS_UPDATE_SUCCESS)
                    {
                        data_CIkey_return[0] = 0x01;
                    }
                    else
                    {
                        gp_DownLoadReciveSuccess[DB_CI_PLUS_KEY] = TRUE;
                    }
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_CIkey_return, sizeof(data_CIkey_return));
                    con_echo_sfu(returnLen, echodata);
					
#ifdef CTV_DEBUG
					{	   
						UINT8 debugMesg[1024] = {0};
						sprintf((char*)debugMesg, "KeyUG:%d,Respond PC", data_CIkey_return[0]);
						con_echo_sfu_tx_Save2Usb(strlen((char*)debugMesg), debugMesg);
					}
#endif

					if (gp_DownLoadBuffer[DB_CI_PLUS_KEY])
                    {
                        free(gp_DownLoadBuffer[DB_CI_PLUS_KEY]);
                        gp_DownLoadBuffer[DB_CI_PLUS_KEY] = NULL;
                    }
                    
                    if (gp_DownLoadFrame[DB_CI_PLUS_KEY])
                    {
                        free(gp_DownLoadFrame[DB_CI_PLUS_KEY]);
                        gp_DownLoadFrame[DB_CI_PLUS_KEY] = NULL;
                    }
                    g_DownLoadFrameSum[DB_CI_PLUS_KEY] = 0;
                }              
            }

            break;
            
        case CTV_RS232_CMD_CI_PLUS_KEY_CRC_READ:
				returnLen=0;
				UINT8 U8CIKeyCRCChecksum[4];
				U8CIKeyCRCChecksum[0] = (UINT8)(u32DownLoad_CRCChecksum >> 24);
				U8CIKeyCRCChecksum[1] = (UINT8)(u32DownLoad_CRCChecksum >> 16);
				U8CIKeyCRCChecksum[2] = (UINT8)(u32DownLoad_CRCChecksum >> 8);
				U8CIKeyCRCChecksum[3] = (UINT8)(u32DownLoad_CRCChecksum&0xFF);
				//printf("=====CRC32:0x%x====U8CIKeyCRCChecksum[0]:0x%x [1]:0x%x [2]:0x%x [3]:0x%x\n",g_UartCommand_ext.u32DownLoad_CRCChecksum,U8CIKeyCRCChecksum[0],U8CIKeyCRCChecksum[1],U8CIKeyCRCChecksum[2],U8CIKeyCRCChecksum[3]);
				returnLen = _conRs232_packageCommander(echodata, CTV_RS232_CMD_CI_PLUS_KEY_CRC_READ, U8CIKeyCRCChecksum, sizeof(U8CIKeyCRCChecksum));
				con_echo_sfu(returnLen, echodata);
                { 
					con_echo_sfu_Save2Usb(3, (UINT8*)"CRC");
                    con_echo_sfu_Save2Usb(8, U8CIKeyCRCChecksum);
                }
				con_echo_sfu(8, U8CIKeyCRCChecksum);
                gp_DownLoadReciveSuccess[DB_CI_PLUS_KEY] = FALSE;
				break;

            
		case CTV_RS232_CMD_FAC_SHIPPING_MODE:
			IsCmdShippingMode = TRUE;
            //Rs232IR(UI_EVENT_FAC_SHIPPING_MODE,0x0, 0x0);
			//UINT8 ReturnParam1[] ={0x01};
			//ReturnParam1[0] = 0x00;
			//returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam1, sizeof(ReturnParam1));
			//con_echo_sfu(returnLen, echodata);
			break;

		case CTV_RS232_CMD_FAC_RESET:
            //Rs232IR(UI_EVENT_FAC_SHIPPING_MODE,0x0, 0x0);
			//UINT8 ReturnParam1[] ={0x01};
			//ReturnParam1[0] = 0x00;
			//returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam1, sizeof(ReturnParam1));
			//con_echo_sfu(returnLen, echodata);
			break;	
		
		case CTV_RS232_CMD_FAC_RESSET_ALL:
            Rs232IR(UI_EVENT_FAC_RESET,0x0, 0x0);
			//UINT8 ReturnParam[] ={0x01};
			//ReturnParam[0] = 0x00;
			//returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam, sizeof(ReturnParam));
			//con_echo_sfu(returnLen, echodata);
			break;
				 
		case CTV_RS232_CMD_STORAGE_CHK:
            {
                UINT8 data_StorgeChk_return[] ={0x01};
                int bPartitionCnt = 0;
                bPartitionCnt = MID_PartitionList_GetMountedCount(); 
                if (!CurrentFrame.dataLen)
                {
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_StorgeChk_return, sizeof(data_StorgeChk_return));
                    con_echo_sfu(returnLen, echodata);
                    break;
                }
                if (strncmp((char *)CurrentFrame.data,"USB1", 4)==0)
				{	
					if (1<=bPartitionCnt)
					 {
						 data_StorgeChk_return[0] = 0x00;
					
					 }
				}
                if (strncmp((char *)CurrentFrame.data,"USB2", 4)==0)
				{	
					if (2<=bPartitionCnt)
					 {
						data_StorgeChk_return[0] = 0x00;
					
					 }
				}

                
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_StorgeChk_return, sizeof(data_StorgeChk_return));
                con_echo_sfu(returnLen, echodata);
        
            }
			break;
			
		case CTV_RS232_CMD_YPP_ADC_ADJUST:
					
			break;

		case CTV_RS232_CMD_VGA_ADC_ADJUST:
					
			break;

		case CTV_RS232_CMD_PANEL_CONTROL:
			{
				UINT8 data_PANEL_CONTROL[] ={0x01};
				if (strncmp((char *)CurrentFrame.data,"PANEL_VCC_ON", 12)==0)
				{
	                Cmd_gpio_WriteOnLevel(GPIO_PANEL_ON_PIN);
					data_PANEL_CONTROL[0] = 0x00;
				}
				else if (strncmp((char *)CurrentFrame.data,"PANEL_VCC_OFF", 13)==0)
				{
	                Cmd_gpio_WriteOffLevel(GPIO_PANEL_ON_PIN);
					data_PANEL_CONTROL[0] = 0x00;
				}
				else if (strncmp((char *)CurrentFrame.data,"PANEL_BL_ON", 11)==0)
				{
	                Cmd_gpio_WriteOnLevel(GPIO_BL_ON_PIN);
					data_PANEL_CONTROL[0] = 0x00;
				}
				else if (strncmp((char *)CurrentFrame.data,"PANEL_BL_OFF", 12)==0)
				{
	                Cmd_gpio_WriteOffLevel(GPIO_BL_ON_PIN);
					data_PANEL_CONTROL[0] = 0x00;
				}
	            returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_PANEL_CONTROL, sizeof(data_PANEL_CONTROL));
	            con_echo_sfu(returnLen, echodata);
			}
			break;
				
		case CTV_RS232_CMD_BL_ADJMIN:
            {
                UINT8 u8BackLight_Polarity = 0;
                UINT8 data_BL_ADJMIN[] ={0x01};
				Cmd_gpio_WriteOnLevel(GPIO_BL_ON_PIN);
				MID_TVFE_GetCurrPolarity((unsigned char *)&(u8BackLight_Polarity));
                #if 0 //(HW_ADJ_INVERT == 1)
					if(u8BackLight_Polarity == 1)
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,0);
					}
					else
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,100);
					}
                #else
					if(u8BackLight_Polarity == 1)
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,100);
					}
					else
					{
					
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,0);
				
					}
                 #endif
                data_BL_ADJMIN[0] = 0x00;
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_BL_ADJMIN, sizeof(data_BL_ADJMIN));
                con_echo_sfu(returnLen, echodata);
            }
			break;
		
		case CTV_RS232_CMD_BL_ADJMAX:
			{
                UINT8 u8BackLight_Polarity = 0;
                UINT8 data_BL_ADJMAX[] ={0x01};
				Cmd_gpio_WriteOffLevel(GPIO_BL_ON_PIN);
				MID_TVFE_GetCurrPolarity((unsigned char *)&(u8BackLight_Polarity));
               #if 0//(HW_ADJ_INVERT == 1)
					if(u8BackLight_Polarity == 1)
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,100);
					}
					else
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,0);
				
					}
               #else
					if(u8BackLight_Polarity == 1)
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,0);
					}
					else
					{
						APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,100);
				
					}
				
                 #endif
                data_BL_ADJMAX[0] = 0x00;
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_BL_ADJMAX, sizeof(data_BL_ADJMAX));
                con_echo_sfu(returnLen, echodata);
            }
			break;
		
		case CTV_RS232_CMD_BL_ADJDEF:
			{
				
                UINT8 data_BL_ADJ[] ={0x01};
				AL_Setting_Read(APP_Data_UserSetting_Handle(), SYS_SET_ID_SETUP, 0,
					sizeof(APP_SETTING_Setup_t), &(g_stSetupData));
				
				Cmd_gpio_WriteOnLevel(GPIO_BL_ON_PIN);
				APP_Video_OSD_PQSet(APP_VIDEO_OSD_ITEM_BACKLIGHT,
									g_stSetupData.HomeMode.Backlight[g_stSetupData.HomeMode.Type]);
				
                data_BL_ADJ[0] = 0x00;
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_BL_ADJ, sizeof(data_BL_ADJ));
                con_echo_sfu(returnLen, echodata);
            }
			break;
		
		case CTV_RS232_CMD_PWM_ADJREAD:
            {
                UINT8 data_PWM_Current[] ={(PWM_CURRENT >> 8) & 0xff, PWM_CURRENT & 0xff};
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_PWM_Current, sizeof(data_PWM_Current));
                con_echo_sfu(returnLen, echodata);
            }
			break;
		
		case CTV_RS232_CMD_BURNNING_TIME:
            {
			    #if 0//def CONFIG_CTV_UART_FAC_MODE
				APP_SETTING_BurnInModeTimeTable_t dataBurnInModeTime;
				APP_BurntTimeDataWriteRead(FALSE,(UINT8 *)&dataBurnInModeTime);
				UINT8 data_bunrnTime[] ={0x00};
				data_bunrnTime[0] = dataBurnInModeTime.n_BurnInModeTime/60;
				#else
                AL_Setting_Read(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER, 0,
                    sizeof(APP_SETTING_FactoryUser_t), &(g_stFactoryUserData));
                
                UINT8 data_bunrnTime[] ={0x00};

                data_bunrnTime[0] = g_stFactoryUserData.n_BurnInModeTime/60;
                #endif    
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_bunrnTime, sizeof(data_bunrnTime));
                con_echo_sfu(returnLen, echodata);
                
            }
			break;
		
		case CTV_RS232_CMD_TEST_RECORD:
            {
                UINT8 data_Record[] = {0x01};
                if (!CurrentFrame.dataLen)
                {
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_Record, sizeof(data_Record));
                    con_echo_sfu(returnLen, echodata);
                    break;
                }
                #if 0//def CONFIG_CTV_UART_FAC_MODE
				 APP_SETTING_CtvRs232ResultTable_t dataCtvRs232Result;
                 APP_TestResultDataWriteRead(FALSE,(UINT8 *)&dataCtvRs232Result);   

                if (strncmp((char *)CurrentFrame.data, "PASS", strlen("PASS")))
                {
                    dataCtvRs232Result.n_CtvRs232Result = 0x00;    
                    data_Record[0] = 0x00;
                }
                else if (strncmp((char *)CurrentFrame.data, "FAIL", strlen("FAIL")))
                {
                    dataCtvRs232Result.n_CtvRs232Result = 0x01;    
                    data_Record[0] = 0x00;
                }
                APP_TestResultDataWriteRead(TRUE,(UINT8 *)&dataCtvRs232Result); 
		        #else		
                AL_Setting_Read(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER, 0,
                    sizeof(APP_SETTING_FactoryUser_t), &(g_stFactoryUserData));

                if (strncmp((char *)CurrentFrame.data, "PASS", strlen("PASS")))
                {
                    g_stFactoryUserData.n_CtvRs232Result = 0x00;    
                    data_Record[0] = 0x00;
                }
                else if (strncmp((char *)CurrentFrame.data, "FAIL", strlen("FAIL")))
                {
                    g_stFactoryUserData.n_CtvRs232Result = 0x01;    
                    data_Record[0] = 0x00;
                }
                
                AL_Setting_Write(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER,
                    ITEM_OFFSET(APP_SETTING_FactoryUser_t, n_CtvRs232Result),
                    sizeof(g_stFactoryUserData.n_CtvRs232Result),&(g_stFactoryUserData.n_CtvRs232Result));
                
                AL_Setting_Store(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTUSER,
                    ITEM_OFFSET(APP_SETTING_FactoryUser_t, n_CtvRs232Result),sizeof(g_stFactoryUserData.n_CtvRs232Result));
                #endif   
                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_Record, sizeof(data_Record));
                con_echo_sfu(returnLen, echodata);
                
            }
    		break;
				
		case CTV_RS232_CMD_FAC_MODE_EXIT:

			break;
			
		case CTV_RS232_CMD_VERSION:
            {
                INT8 tmp_str[20]={0};
				INT8 tmp_str1[20]={0};
				
				UINT8 i=0;
				UINT8 j=0;
				memset(tmp_str,0x0,20);
				memset(tmp_str1,0x0,20);

#ifdef DEFAULT_CUSTOMER_VER
                memcpy(tmp_str, DEFAULT_CUSTOMER_BuildTimeVer, sizeof(DEFAULT_CUSTOMER_BuildTimeVer));
#else
                MID_TVFE_GetVersionInfo(MID_TVFE_BuildTimeVer, tmp_str,sizeof(tmp_str));
#endif                
				for (i=0; i<20; i++)
				{
					if((i==4)||(i==7||(i==10)||(i==13)))
					{
						//DEBF("for factory test BuildTime display need remove : -\n");
					}
					else
					{
				
						tmp_str1[j] = tmp_str[i];
						j++;
					}
					
				
				}

                returnLen = _conRs232_packageCommander(echodata, cmdIdType, (UINT8*)tmp_str1, strlen(tmp_str1));
                con_echo_sfu(returnLen, echodata);
            }
			break;
							
		case CTV_RS232_CMD_SOURCE_SINGAL_CHECK:
        // Check if current source has signal
            {
                extern Boolean APP_Video_GetCurrSignalStatus(void);
                UINT8 date_SC[] ={0x00};

                if( FALSE == APP_Video_GetCurrSignalStatus()) // No signal
                {
                    date_SC[0] = 0x01;
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, date_SC, sizeof(date_SC));
                    con_echo_sfu(returnLen, echodata);
                }
                else // Has signal
                {
                    date_SC[0] = 0x00;
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, date_SC, sizeof(date_SC));
                    con_echo_sfu(returnLen, echodata);
                }
            }
            break;

        #ifdef CONFIG_CI_SUPPORT
		case CTV_RS232_CMD_CIFUNCTION_CHECK:
            {
                extern INT8 APP_DVB_CI_Check(void);
                
                UINT8 data_CI_Check[] ={0x00};
				APP_Source_Type_t eSourType = APP_SOURCE_MAX;
				APP_GUIOBJ_Source_GetCurrSource(&eSourType);
				if(eSourType == APP_SOURCE_DTV)
				{
				
                   data_CI_Check[0] = APP_DVB_CI_Check();
				}
				else 
				{
					APP_GUIOBJ_Source_SetAppSource(APP_SOURCE_DTV);
					GL_TaskSleep(2000);
					data_CI_Check[0] = APP_DVB_CI_Check();
				}

                returnLen = _conRs232_packageCommander(echodata, cmdIdType, data_CI_Check, sizeof(data_CI_Check));
                con_echo_sfu(returnLen, echodata);

            }
            break;
		#endif	
            
		case CTV_RS232_CMD_SOURCE_CONTROL:
            {
				UINT8 date_SCtr_return[1] ={0x01};
                if (!CurrentFrame.dataLen)
                {
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, date_SCtr_return, sizeof(date_SCtr_return));
                    con_echo_sfu(returnLen, echodata);
                    break;
                }
                APP_Source_Type_t sourceParam = 0;
                if (strncmp((char *)CurrentFrame.data,"ATV", 3)==0)
				{	
                    sourceParam = APP_SOURCE_ATV;
				}
				else if (strncmp((char *)CurrentFrame.data,"AV1", 3)==0)
				{
                    sourceParam = APP_SOURCE_AV;
				}
				else if (strncmp((char *)CurrentFrame.data,"AV2", 3)==0)
				{
                    sourceParam = APP_SOURCE_AV1;
				}
				else if (strncmp((char *)CurrentFrame.data,"SCART1", 3)==0)
				{
                    sourceParam = APP_SOURCE_SCART;
				}
    			else if (strncmp((char *)CurrentFrame.data,"YPBPR1", 6)==0)
				{
                    sourceParam = APP_SOURCE_YPBPR;
				}
				else if (strncmp((char *)CurrentFrame.data,"YPBPR2", 6)==0)
				{
                    sourceParam = APP_SOURCE_YPBPR1;
				}
				else if (strncmp((char *)CurrentFrame.data,"HDMI1", 5)==0)
				{
                    sourceParam = APP_SOURCE_HDMI;
				}
				else if (strncmp((char *)CurrentFrame.data,"HDMI2", 5)==0)
				{
                    sourceParam = APP_SOURCE_HDMI1;
				}
				else if (strncmp((char *)CurrentFrame.data,"HDMI3", 5)==0)
				{
                    sourceParam = APP_SOURCE_HDMI2;
				}
				else if (strncmp((char *)CurrentFrame.data,"HDMI4", 5)==0)
				{
                    //sourceParam = APP_SOURCE_HDMI1;
				}
				else if (strncmp((char *)CurrentFrame.data,"VGA", 3)==0)
				{
                    sourceParam = APP_SOURCE_PC;
				}
				else if (strncmp((char *)CurrentFrame.data,"DVD", 3)==0)
				{
                    sourceParam = APP_SOURCE_DVD;
				}
				else if (strncmp((char *)CurrentFrame.data,"USB1", 3)==0)
				{
                    sourceParam = APP_SOURCE_MEDIA;
				}
				else if (strncmp((char *)CurrentFrame.data,"USB2", 3)==0)
				{
                    sourceParam = APP_SOURCE_MEDIA;
				}
				else if (strncmp((char *)CurrentFrame.data,"KARAOKE", sizeof("KARAOKE"))==0)
				{
                    sourceParam = APP_SOURCE_KARAOKE;
				}
				else
				{
                    sourceParam = APP_SOURCE_MAX;
				}	
				if(sourceParam == APP_SOURCE_MAX)
				{
					if (strncmp((char *)CurrentFrame.data,"DTV-C", 5)==0)
					{
	                    //Rs232IR(UI_EVENT_AUTO_DTVC,0x0, 0x0);
						date_SCtr_return[0] = 0x00;
					}
					else if (strncmp((char *)CurrentFrame.data,"DTV-S2", 6)==0)
					{
	                   //Rs232IR(UI_EVENT_AUTO_DTVS,0x0, 0x0);
					   date_SCtr_return[0] = 0x00;
					}
					else if (strncmp((char *)CurrentFrame.data,"DTV-S", 6)==0)
					{
	                   //Rs232IR(UI_EVENT_AUTO_DTVS,0x0, 0x0);
					   date_SCtr_return[0] = 0x00;
					}
					else if (strncmp((char *)CurrentFrame.data,"DTV-T2", 6)==0)
					{
	                     Rs232IR(UI_EVENT_AUTO_DTV,0x0, 0x0);
						 date_SCtr_return[0] = 0x00;
					}
					else if (strncmp((char *)CurrentFrame.data,"DTV-T", 5)==0)
					{
	                    Rs232IR(UI_EVENT_AUTO_DTV,0x0, 0x0);
						date_SCtr_return[0] = 0x00;
					}
					else if (strncmp((char *)CurrentFrame.data,"DTV", 3)==0)
					{
	                    Rs232IR(UI_EVENT_AUTO_DTV,0x0, 0x0);
						date_SCtr_return[0] = 0x00;
					}
					else
					{
						date_SCtr_return[0] = 0x01;
					}
					
                    returnLen = _conRs232_packageCommander(echodata, cmdIdType, date_SCtr_return, sizeof(date_SCtr_return));
                    con_echo_sfu(returnLen, echodata);
				}
				else
				{
					Rs232ConCallBack(cmdIdType, sourceParam);
				}
   
            }            
			break;
            
		case CTV_RS232_CMD_TV_CONTROL_ASC:
				//Rs232IR(UI_EVENT_AUTO_SEARCH,0x0, 0x0);
			break;
            
		case CTV_RS232_CMD_TV_CONTROL_CAB:

			break;
		case CTV_RS232_CMD_TV_CONTROL_CCH:

			break;
		case CTV_RS232_CMD_TV_CONTROL_CHU:
            Rs232IR(UI_EVENT_CH_UP,0x0, 0x0);
			UINT8 ReturnParam2[] ={0x01};
			ReturnParam2[0] = 0x00;
			returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam2, sizeof(ReturnParam2));
			con_echo_sfu(returnLen, echodata);
			break;
		case CTV_RS232_CMD_TV_CONTROL_CHD:
            Rs232IR(UI_EVENT_CH_DN,0x0, 0x0);
			UINT8 ReturnParam3[] ={0x01};
			ReturnParam3[0] = 0x00;
			returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam3, sizeof(ReturnParam3));
			con_echo_sfu(returnLen, echodata);
            break;
		case CTV_RS232_CMD_OTHER_CONTROL_SLP:
            Rs232IR(UI_EVENT_SLEEP,0x0, 0x0);
			UINT8 ReturnParam4[] ={0x01};
			ReturnParam4[0] = 0x00;
			returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam4, sizeof(ReturnParam4));
			con_echo_sfu(returnLen, echodata);
			break;
		case CTV_RS232_CMD_OTHER_CONTROL_ASP:
            Rs232IR(UI_EVENT_ASPECT,0x0, 0x0);
			UINT8 ReturnParam5[] ={0x01};
			ReturnParam5[0] = 0x00;
			returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam5, sizeof(ReturnParam5));
			con_echo_sfu(returnLen, echodata);
			break;
		case CTV_RS232_CMD_OTHER_CONTROL_ECC:
            Rs232IR(UI_EVENT_TEXT,0x0, 0x0);
			UINT8 ReturnParam6[] ={0x01};
			ReturnParam6[0] = 0x00;
			returnLen = _conRs232_packageCommander(echodata, cmdIdType, ReturnParam6, sizeof(ReturnParam6));
			con_echo_sfu(returnLen, echodata);
			break;
		case CTV_RS232_CMD_KEYPAD_LOCK:
			{
                if (!CurrentFrame.dataLen)
                {
                    break;
                }
			 #if 0// DEFAUL_Hotel_Mode    
				UINT32 lockVal = 0;
				lockVal = CurrentFrame.data[0];

				AL_Setting_Read(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTHOTEL, 0,
								sizeof(APP_SETTING_FactoryHotel_t), &(g_stFactoryHotelData));
				if (g_stFactoryHotelData.HotelModeOnOff)
				{
					if(lockVal == 0x01 )
						g_stFactoryHotelData.KeypadOperation = 1;
					if(lockVal == 0x00 )
						g_stFactoryHotelData.KeypadOperation = 0;
				}
                AL_Setting_Write(APP_Data_UserSetting_Handle(), SYS_SET_ID_FACTHOTEL,
                    0, sizeof(APP_SETTING_FactoryHotel_t),&(g_stFactoryHotelData));
                
			#endif	
			}
			break;
		case CTV_RS232_CMD_DTV_CHANGE_CHANNLE:
            {
                if (!CurrentFrame.dataLen)
                {
                    break;
                }
    			if (CurrentFrame.data[0] > 100)
    			{
    				switch(CurrentFrame.data[0] / 100)
    				{
    				case 0x00: u8KeyCode = UI_EVENT_0; break;
    				case 0x01: u8KeyCode = UI_EVENT_1; break;
    				case 0x02: u8KeyCode = UI_EVENT_2; break;
    				case 0x03: u8KeyCode = UI_EVENT_3; break;
    				case 0x04: u8KeyCode = UI_EVENT_4; break;
    				case 0x05: u8KeyCode = UI_EVENT_5; break;
    				case 0x06: u8KeyCode = UI_EVENT_6; break;								
    				case 0x07: u8KeyCode = UI_EVENT_7; break;
    				case 0x08: u8KeyCode = UI_EVENT_8; break;								
    				case 0x09: u8KeyCode = UI_EVENT_9; break;
    				  default:  break;
    				}
    					Rs232IR(u8KeyCode,0x0, 0x0);
    			}
    			
    			if (CurrentFrame.data[0] > 10)
    			{
    				switch((CurrentFrame.data[0] % 100) / 10)
    				{
    				case 0x00: u8KeyCode = UI_EVENT_0; break;
    				case 0x01: u8KeyCode = UI_EVENT_1; break;
    				case 0x02: u8KeyCode = UI_EVENT_2; break;
    				case 0x03: u8KeyCode = UI_EVENT_3; break;
    				case 0x04: u8KeyCode = UI_EVENT_4; break;
    				case 0x05: u8KeyCode = UI_EVENT_5; break;
    				case 0x06: u8KeyCode = UI_EVENT_6; break;								
    				case 0x07: u8KeyCode = UI_EVENT_7; break;
    				case 0x08: u8KeyCode = UI_EVENT_8; break;								
    				case 0x09: u8KeyCode = UI_EVENT_9; break;
    				  default:  break;
    				}
    					Rs232IR(u8KeyCode,0x00, 0x0);
    			}

    				switch(CurrentFrame.data[0] % 10)
    				{
    				case 0x00: u8KeyCode = UI_EVENT_0; break;
    				case 0x01: u8KeyCode = UI_EVENT_1; break;
    				case 0x02: u8KeyCode = UI_EVENT_2; break;
    				case 0x03: u8KeyCode = UI_EVENT_3; break;
    				case 0x04: u8KeyCode = UI_EVENT_4; break;
    				case 0x05: u8KeyCode = UI_EVENT_5; break;
    				case 0x06: u8KeyCode = UI_EVENT_6; break;								
    				case 0x07: u8KeyCode = UI_EVENT_7; break;
    				case 0x08: u8KeyCode = UI_EVENT_8; break;								
    				case 0x09: u8KeyCode = UI_EVENT_9; break;
    				  default:  break;
    				}
    					Rs232IR(u8KeyCode,0x00, 0x0);
    					

            }
			break;

		default:
			break;
	}
    
    if (CurrentFrame.data)
    {
        free(CurrentFrame.data);
        CurrentFrame.data= NULL;
    }

    return true;
    
}

void con_AccuviewRs232_HeaderParse(unsigned char* buff, unsigned int bufflen, unsigned int *commandlen)
{
    if (bufflen < 4 || buff[0] != ACCUVIEW_RS232_AutoTest_HEAD || commandlen == NULL)
    {
        return ;
    }
            /*    head type command   data  dataLen No Sum   crc  end_mark */  
    *commandlen = (1 + 1 + 2) + *(buff + 4) + (1 + 1 + 1 + 1 + 1);
}


void con_RegAccuviewRs232CallBackFunc(AccuviewRS232 pCallBackFunc)
{
	Rs232ConCallBack = pCallBackFunc;
}
#if 1
void con_RegAccuviewRs232IR_CallBackFunc(RS232_IR_CallBack_t pIRCallBackFunc)
{
	Rs232IR = pIRCallBackFunc;
}
#endif
void AccuviewRs232StartTask(void *arg)
{
    usleep(1000);
    pthread_t thd = *(pthread_t*)arg;
    pthread_detach(thd);
    Rs232_CMD_test_ready = FALSE;
    UINT8 *tmpMsg = u8Rs232CmdList[0];
    static UINT8 sendRs232ConfigRec = 0;
    rs232TaskExit = 1;
    while(!Rs232_CMD_test_ready)
    {
        con_AccuviewRs232_AutoTest_parser(NULL, tmpMsg,3);
        usleep(1000);
        sendRs232ConfigRec++;
        if (sendRs232ConfigRec == RS232_RETURN_TIME)
        {
            break;
        }
    }
    rs232TaskExit = 1;
    Rs232_CMD_test_ready = TRUE;
}


  


