#include <stdlib.h>
#include "al_console_inn_rs232.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

//header must include all cmd first char
const char gInnHeaderTable[]={
	'A','B','C','D','E','F','G',
	'H','I','K','L','M','O','R',
	'S','T','U','V','W'
};
const UINT8 gInnHeaderTable_Size = sizeof(gInnHeaderTable) / sizeof(gInnHeaderTable[0]);


static InnRs232_autotest_Cbk_pf g_pfpInnRs232_AutoTestCallback = NULL;

UINT8 con_InnRs232_CheckSyncAndValid(UINT8 sync,UINT8 len)
{
	UINT8 i =0 ;

	if(len != CMDPARAM_SIZE)
		return 0;
	
	for(i=0;i< gInnHeaderTable_Size;i++)
	{
		if(sync == (UINT8)gInnHeaderTable[i])
			return 1;
	}

	return 0;
}


void con_InnRs232_CMD_Response(eInnRspType type,UINT8 *data,UINT8 len)
{
	switch(type)
	{
		case InnRsp_State_OK:
			con_echo_sfu(strlen(RespOK), (UINT8*)RespOK);
			break;
			
		case InnRsp_State_ERR:
			con_echo_sfu(strlen(RespERR), (UINT8*)RespERR);
			break;

		case InnRsp_Data_Invalid:
		default:
			con_echo_sfu(strlen(RespDataInvalid), (UINT8*)RespDataInvalid);
			break;
			
		case InnRsp_Data:
			if(data != NULL)
			{
				con_echo_sfu(len, data);
				return;
			}		
	}
}


bool con_InnRs232_AutoTest_parser(pCON_DEV pcon, UINT8 *data)
{
	int ret = 0;
	
	//check data len valid
	if(pcon->n != CMDPARAM_SIZE)
		return 0;

	//you can deal with here, also bypass to app deal with
	if(memcmp(data,UseToTestResp_OK,strlen(UseToTestResp_OK))==0)
	{
		con_InnRs232_CMD_Response(InnRsp_State_OK,NULL,0);
	}
	else //pass to app paser
	{
		ret = g_pfpInnRs232_AutoTestCallback(pcon,data);
		switch(ret)
		{
			case InnRsp_Data_Invalid:
				con_InnRs232_CMD_Response(InnRsp_Data_Invalid,NULL,0);
				break;
			case InnRsp_State_OK:
				con_InnRs232_CMD_Response(InnRsp_State_OK,NULL,0);
				break;				
			case InnRsp_State_ERR:
				con_InnRs232_CMD_Response(InnRsp_State_ERR,NULL,0);
				break;
			case InnRsp_Need_App_Resp:
			default:
				break;
		}
	}
	
	return ret;
}

void al_innrs232_autotest_init(InnRs232_autotest_Cbk_t *pConsole_autotest_Cfg)
{
	if ((pConsole_autotest_Cfg != NULL) && (pConsole_autotest_Cfg->pfCallBack != NULL))
	{
		g_pfpInnRs232_AutoTestCallback = pConsole_autotest_Cfg->pfCallBack;
	}
}



