/**
* Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved.
*
* @file
*
* @brief The interface of application framework module.
*
* The file list app framework commponent related APIs.
*
*/
#include <stdbool.h>
#include "gl_queue.h"
#include "gl_mutex.h"
#include "gl_task.h"
#include "gl_timer.h"
#include "ai_debug_config.h"
#include "ai_osal.h"
#include "al_fw.h"
#include "ai_datasaving.h"
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
#include "ai_prog_reminder.h"
#include "mid_common.h"
#endif
#ifdef CONFIG_DTV_SUPPORT
#include "ai_event.h"
#include "ai_dvb_monitor.h"
#include "ai_psi_monitor.h"
#include "mid_siutil_dvb.h"
#endif
#include "ai_time.h"
#include "ai_setting.h"
#ifdef CONFIG_SUPPORT_EWS
#include "ai_ews_monitor.h"
#endif
#ifdef CONFIG_SUPPORT_TTX
#include "ai_teletext.h"
#endif
#ifdef CONFIG_SUPPORT_SUBTITLE
#include "ai_subtitle.h"
#endif

#include "drv_event.h"
#include "drv_hdmi_external.h"
#include "mid_tvfe.h"
#include "mid_dtv_display.h"
#include "umf_event.h"
#include "al_console.h"
#include "old_autoDetect.h"
#ifdef CONFIG_SUPPORT_CEC_TV
#include "umf_cec.h"
#endif
#include "al_console_customer.h"

#ifdef SUPPORT_TURKY_NEW_CHNLIST
#include "mid_playback.h"
#endif

#ifdef CONFIG_CTV_UART_FAC_MODE
#include "al_console_CtvRs232.h"
#endif

#ifdef CONFIG_CC_SUPPORT
#include "ai_cc.h"
#endif
#ifdef CONFIG_SUPPORT_SW_AFC
#include "mid_afc.h"
#endif
#ifdef SUPPORT_ASTRA_LCN_MONITOR
#include "ai_astralcn_monitor.h"
#endif
#ifdef CONFIG_SUPPORT_ISDB_SUBTITLE
#include "ai_sbtvd_subtitle.h"
#endif
#ifdef CONFIG_VCHIP_SUPPORT
#include "ai_parent_control.h"
#endif

#include "app_guiobj_source.h"
#ifdef CONFIG_SMART_MODULE_SUPPORT
#include "app_smart.h"
#endif
#include <drv_ddcci.h>
#include "nvm_ioctl.h"
#include "cust_nvm_cfg.h"
#include "app_data_setting.h"

/**
* @brief define the debug message control
*/
#if FW_DBG_FLAG
	#define FW_DEBUG_MESSAGE(fmt, arg...) \
	do { \
		UMFDBG(0,"[Debug]%s: %d Line\n", __FUNCTION__, __LINE__); \
		UMFDBG(0,fmt, ##arg); \
	} while(0)
#else
	#define FW_DEBUG_MESSAGE(fmt, arg...);
#endif

/**
* @brief define the error message control
*/
#if FW_ERROR_FLAG

	#define FW_ERROR_MESSAGE(fmt, arg...) \
	do { \
		UMFDBG(0,"[Error]%s: %d Line\n", __FUNCTION__, __LINE__); \
		UMFDBG(0,fmt, ##arg); \
	} while(0)
#else
	#define FW_ERROR_MESSAGE(fmt, arg...);
#endif

/**
* @brief defines the unexpected error message
*
* This macro just causes an oops and it's easy for debugging.
*/
#define ASSERT(condition, str) \
	if (!(condition)) \
	{ \
		FW_ERROR_MESSAGE("%s %d >> Assert (%s) : \"%s\"\n", __FUNCTION__, __LINE__, #condition,str); \
		*(int *)0 = 0; \
	}

/**
* @brief defines the version number of AFW
*
* These macro is made of three part, major number, minor number and Makeup version
*/
#define AL_FW_MAJOR					(1)
#define AL_FW_MINOR					(0)
#define MK_VERSION(x, y)			(((x) << 8) + (y))

/**
* @brief defines data structure used in FW commponent
*/
typedef struct {
	FW_mode_t fw_mode;
	GL_Task_t fw_task_id;
	GL_Mutex_t fw_mutex_id;
	GL_Queue_t fw_qid;
	frmwrk_app_cb_t fw_app_cb;
	frmwrk_pfOpConext_t fw_context_cb;
	AFW_event_t msg_to_app;
}  FW_data_struct_t;

/**
* @brief Structure defining the messages to be sent to task
*/
typedef struct {
	AFW_event_t msg_type;
	al_uint32 u32Userparam;
	#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
	union {
		/**< Add new parameters here when new commponent requires */
		AL_SCH_sched_id_t fw_sch_id;
		AL_RecHandle_t cdbi_serv;
	} msg_data;
	#endif
} FW_msg_t;

/**< static variables */
static al_uint8 afw_running = al_false;
static FW_data_struct_t fw_data_struct;
#ifdef CONFIG_DTV_SUPPORT
static UINT32 DemuxState = 0xffffffff;
static al_bool DvbMonState = false;
#endif
#ifdef CONFIG_SUPPORT_NOSIGNAL_AUTO_CHANGE_SOURCE
static al_uint32 u32HDMIActive = 0;
#endif
extern void osd_init( void );

AL_Return_t AL_FW_ShowQueue(void)
{
	GL_Status_t ret = AL_FAILURE;
	FW_msg_t message;
	al_uint32 uValue;

	memset(&message, 0, sizeof(FW_msg_t));

	if (fw_data_struct.fw_qid == NULL)
	{
		return AL_FAILURE;
	}
	ret = GL_QueuePeek(fw_data_struct.fw_qid, &uValue);
	FW_DEBUG_MESSAGE("[%s:%d] uValue ================= %d\n",__FUNCTION__,__LINE__,uValue);
	uValue = FW_QUEUE_MSG_CNT - uValue;
	while (uValue)
	{
		ret = GL_QueueReceive(fw_data_struct.fw_qid, &message, sizeof(FW_msg_t), GL_NO_WAIT);
		if (ret == GL_QUEUE_EMPTY)
		{
			return AL_SUCCESS;
		}
		else if (ret == GL_FAILURE)
		{
			UMFDBG(0,"clear FW queue failure\n");
			return AL_FAILURE;
		}
		else
		{
			FW_DEBUG_MESSAGE("[%s:%d] message =========== %d\n",__FUNCTION__,__LINE__, message.msg_type);
			if (GL_SUCCESS != GL_QueueSend(fw_data_struct.fw_qid, &message, sizeof(FW_msg_t)))
			{
				FW_ERROR_MESSAGE( "send msg error!!!!!!!!!!!!!!\n");
			}
			uValue --;
		}
	}
	return AL_SUCCESS;
}
#if defined (CONFIG_DTV_SUPPORT) || defined (CONFIG_ATV_SUPPORT)
static al_int32  FW_DataSaving_callback(AI_DataSaving_Msg_Type_t MessageType, al_void* pParam)
{
	FW_msg_t msg;

	switch (MessageType)
	{
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
		case AI_DATASAVING_MSG_ATVDB_STORE_START:
			msg.msg_type = FW_DATASAVING_ATVDB_STORE_START_EVENT;
			break;

		case AI_DATASAVING_MSG_ATVDB_STORE_END:
			msg.msg_type = FW_DATASAVING_ATVDB_STORE_END_EVENT;
			break;

		case AI_DATASAVING_MSG_DVBDB_STORE_START:
			msg.msg_type = FW_DATASAVING_DVBDB_STORE_START_EVENT;
			break;

		case AI_DATASAVING_MSG_DVBDB_STORE_END:
			msg.msg_type = FW_DATASAVING_DVBDB_STORE_END_EVENT;
			break;
#endif
		case AI_DATASAVING_MSG_USERSETING_STORE_START:
			msg.msg_type = FW_DATASAVING_USERSETING_STORE_START_EVENT;
			break;

		case AI_DATASAVING_MSG_USERSETING_STORE_END:
			msg.msg_type = FW_DATASAVING_USERSETING_STORE_END_EVENT;
			break;

		default:
			msg.msg_type = FW_INVALID_EVENT;
			break;
	}

	if (msg.msg_type != FW_INVALID_EVENT)
	{
		/* Send msg to frmwrk task */
		FW_DEBUG_MESSAGE("FW_DataSaving_callback msg.msg_type = %d\n", msg.msg_type);
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}

	return 0;

}
#endif
#ifdef CONFIG_SUPPORT_TTX
static al_void FW_TTX_CallBack(al_uint8 datastate)
{
	FW_msg_t msg;

	if(datastate == 0)
	{
		msg.msg_type = FW_TT_NO_DATA;
		/* Send msg to frmwrk task */
		FW_DEBUG_MESSAGE("FW_TTX_CallBack msg.msg_type = %x\n", msg.msg_type);
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}
}
#endif
#ifdef CONFIG_CC_SUPPORT
static al_void FW_CC_CallBack(al_uint8 data1, al_uint8 data2)
{
    FW_msg_t msg;
    msg.msg_type = FW_CC_VCHIP_DATA;
    msg.u32Userparam = data1 | data2 << 8;
    /* Send msg to frmwrk task */
    //FW_DEBUG_MESSAGE("FW_cc_callback msg.msg_type = %d\n", msg.msg_type);
    GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
}
#endif
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
static void FW_cdbi_callback(ECDBIDBType_t db_type, EDBEvent_t event_type, void *entry)
{
	FW_msg_t msg;
	msg.msg_type = FW_INVALID_EVENT;

	/* Service got changed */
	if (event_type & DB_SERVICE_INFO)
	{
		//FW_DEBUG_MESSAGE("\n\n\n*******DB_SERVICE_INFO*******\n\n\n\n");
		if (event_type & DB_ENTRY_REMOVED)
		{
			if ((entry != NULL)&&(db_type!=CDBI_DBTYPE_DVB_ATV))
			{
				msg.msg_data.cdbi_serv = *((CDBIHandle_t *)entry);
				msg.msg_type = FW_SERVICE_DELETED_EVENT;
			}
		}
	}
	else if (event_type & DB_FULLDB_RESTORED)
	{
		msg.msg_type = FW_DB_LOAD_DEFAULT;
		msg.u32Userparam = db_type;
		if (CDBI_DBTYPE_DVB_S == db_type)
		{
			FW_DEBUG_MESSAGE("$$$$$$$$$$$$$$$$$$$$$$$$$$$ db_type = CDBI_DBTYPE_DVB_S\n");
		}
	}

	if (msg.msg_type != FW_INVALID_EVENT)
	{
		/* Send msg to frmwrk task */
		FW_DEBUG_MESSAGE("FW_cdbi_callback msg.msg_type = %d\n", msg.msg_type);
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}
	return;
}
#endif
#if defined(CONFIG_DVB_SYSTEM_DVBT_SUPPORT)||defined(CONFIG_DVB_SYSTEM_DVBC_SUPPORT)||defined(CONFIG_DVB_SYSTEM_DVBS_SUPPORT)||defined(CONFIG_ISDB_SYSTEM)
static void FW_cdbi_service_callback(ECDBIDBType_t db_type, EDBEvent_t event_type, void *entry)
{
	/* Service got changed */
    UINT8 flag = 0;
    if ((event_type & DB_SERVICE_INFO) || (event_type & DB_FULLDB_RESTORED))
    {
        flag = 1;
        if(fw_data_struct.fw_context_cb)
        {
            fw_data_struct.fw_context_cb(FW_CONTEXT_TYPE_DB_UPDATED, FW_CONTEXT_OP_TYPE_WRITE, &flag, 0);
        }
    }
}
#endif

#ifdef CONFIG_DTV_SUPPORT
static al_void FW_scheduler_callback(AL_SCH_event_type_t sch_event,
	AL_SCH_sched_id_t fw_sch_id, AL_SCH_details_t *sch_detail)
{
	static FW_msg_t msg;
	al_uint32 curr_loc_time = 0;

	msg.msg_type = FW_INVALID_EVENT;

	switch (sch_event)
	{
		case AL_SCH_TYPE_PRG_SCH:
		case AL_SCH_TYPE_SRV_SCH:
			if (AL_FAILURE == AL_Time_GetCurTimeInSecs(&curr_loc_time))
			{
				FW_ERROR_MESSAGE(" Error .... failed to get Current time \n");
				break;
			}
			FW_DEBUG_MESSAGE("sch_detail->sch_time = %d\n",sch_detail->sch_time);
			FW_DEBUG_MESSAGE("curr_loc_time		   =  %d\n",curr_loc_time);
			if (curr_loc_time >= sch_detail->sch_time)
			{
				msg.msg_type = FW_SCHEDULER_EVENT;
				FW_DEBUG_MESSAGE("start reminder\n");
			}
			else
			{
				FW_DEBUG_MESSAGE("show popmsg\n");
				msg.msg_type = FW_SCHEDULER_MSG;
			}
			break;
		case AL_SCH_TYPE_PRG_EXPIRE_SCH:
			FW_DEBUG_MESSAGE("we will stop this event\n");
			msg.msg_type = FW_SCHEDULER_EXPIRE;
			break;
		default:
			msg.msg_type = FW_INVALID_EVENT;
			break;

	}

	if (msg.msg_type != FW_INVALID_EVENT )
	{
		msg.msg_data.fw_sch_id = fw_sch_id;
		/* Send msg to frmwrk task */
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}

	return;
}

#define TICK_RANGE	(1000)
static GL_Timer_t TimerId = 0;
static al_bool NeedSend = al_false;
static void TimerUpdateFunction(void *pArg)
{
	FW_msg_t msg;
	msg.msg_type = FW_EPG_SCHEDULE_UPDATE_EVENT;
	msg.u32Userparam = 0;
	NeedSend = al_false;
	/* Send msg to frmwrk task */
	FW_DEBUG_MESSAGE("FW_event_callback msg.msg_type = %d\n", msg.msg_type);
	GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
}

static al_int32  FW_event_callback(AI_Event_Message_Type_t EventType, al_void* pParam)
{
	FW_msg_t msg;
	static al_uint32 old_timing = 0;
	al_bool bforce_process_event = al_false;
	/* No need to handle event update message when it's not in EPG mode */
	if (fw_data_struct.fw_mode != FW_EPG_MODE)
	{
		if(fw_data_struct.fw_context_cb)
		{
			fw_data_struct.fw_context_cb(FW_CONTEXT_TYPE_CHECK_EVENTSTATE, FW_CONTEXT_OP_TYPE_READ,&bforce_process_event,0);
		}
		if((bforce_process_event != al_true)||(pParam == 0))
		{
			return 0;
		}
	}

	if ((EventType != AL_EVENT_NOWNEXT_EVNET_UPDATE && GL_GetRtc32() - old_timing < TICK_RANGE)
		|| (EventType == AL_EVENT_NOWNEXT_EVNET_UPDATE && GL_GetRtc32() - old_timing < TICK_RANGE/4))
	{
		/* if GL_GetRtc32() overflows, old_timeing should be updated too in this case*/
		if (old_timing > GL_GetRtc32())
		{
			old_timing = GL_GetRtc32();
		}

		if (NeedSend == al_false)
		{
			if (TimerId == 0)
			{
				GL_TimerCreate("event update", (TimerUpdateFunction), NULL, 1, TICK_RANGE*2, 0 ,&TimerId);
			}
			else
			{
				GL_TimerSchedule(TimerId, TICK_RANGE*2);
			}
		}
		NeedSend = al_true;
		return 0;
	}

	old_timing = GL_GetRtc32();

	switch (EventType)
	{
		case AL_EVENT_NOWNEXT_EVNET_UPDATE:
			msg.msg_type = FW_EPG_NOWNEXT_UPDATE_EVENT;
			msg.u32Userparam = 0;
			if (pParam != al_null)
			{
				msg.u32Userparam = *(UINT32*)pParam;
			}
			NeedSend = al_false;
			break;

		case AL_EVENT_SCHEDULE_EVNET_UPDATE:
			msg.msg_type = FW_EPG_SCHEDULE_UPDATE_EVENT;
			msg.u32Userparam = 0;
			if (pParam != al_null)
			{
				msg.u32Userparam = *(UINT32*)pParam;
			}
			NeedSend = al_false;
			break;

		case AL_EVENT_PREF_SERVICE_NAME_UPDATE:
			msg.msg_type = FW_EPG_PREF_SERVICE_NAME_UPDATE_EVENT;
			NeedSend = al_false;
			break;

		case AL_EVENT_SERIES_EVENT_UPDATE:
			msg.msg_type = FW_EPG_SERIES_UPDATE_EVENT;
			NeedSend = al_false;
			break;

		default:
			msg.msg_type = FW_INVALID_EVENT;
			break;
	}

	if (msg.msg_type != FW_INVALID_EVENT )
	{
		/* Send msg to frmwrk task */
		FW_DEBUG_MESSAGE("FW_event_callback msg.msg_type = %d\n", msg.msg_type);
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}

	return 0;

}

static al_int32  FW_DvbMon_callback(AI_DVBMon_Message_Type_t EventType, al_uint32 u32Param)
{
	FW_msg_t msg;

	msg.msg_type = FW_INVALID_EVENT;

	if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
		|| fw_data_struct.fw_mode == FW_EPG_MODE
		|| fw_data_struct.fw_mode == FW_RS232_MODE
		)
	{
		switch (EventType)
		{
			case AI_DVB_MONITOR_TUNER_LOCKED:
				DvbMonState = true;
				msg.msg_type = FW_MONITOR_TUNER_LOCKED;
				break;

			case AI_DVB_MONITOR_TUNER_UNLOCKED:
				DvbMonState = false;
				msg.msg_type = FW_MONITOR_TUNER_UNLOCKED;
				break;

			case AI_DVB_MONITOR_STREAM_NO_DATA:
				DvbMonState = false;
				msg.msg_type = FW_MONITOR_STREAM_NO_DATA;
				AI_PSI_Monitor_ReplaceProcess(AL_PSIMONITOR_STATE_NOT_RUNNING_REPLACEMENT_EXIST);
				break;

			case AI_DVB_MONITOR_STREAM_INVALID_DATA:
				DvbMonState = true;
				msg.msg_type = FW_MONITOR_STREAM_INVALID_DATA;
				break;

			case AI_DVB_MONITOR_STREAM_UNSUPPORT_DATA:
				DvbMonState = true;
				msg.msg_type = FW_MONITOR_STREAM_UNSUPPORT_DATA;
				break;

			case AI_DVB_MONITOR_STREAM_HAS_DATA:
				DvbMonState = true;
				msg.msg_type = FW_MONITOR_STREAM_HAS_DATA;
				break;

			case AI_DVB_MONITOR_STREAM_SCRAMBLE:
				msg.msg_type = FW_MONITOR_SERVICE_VIDEO_AUDIO_SCRAMBLE;
				break;

			case AI_DVB_MONITOR_STREAM_NO_SCRAMBLE:
				msg.msg_type = FW_MONITOR_SERVICE_NO_SCRAMBLE;
				break;

			case AI_DVB_MONITOR_PARENTAL_LOCKED:
				msg.msg_type = FW_MONITOR_PARENTAL_LOCKED;
				break;

			case AI_DVB_MONITOR_PARENTAL_UNLOCKED:
				msg.msg_type = FW_MONITOR_PARENTAL_UNLOCKED;
				break;

			default:
				break;
		}

		if (msg.msg_type != FW_INVALID_EVENT )
		{
			/* Send msg to frmwrk task */
			FW_DEBUG_MESSAGE("FW_DvbMon_callback msg.msg_type = %d\n", msg.msg_type);
			GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
		}
	}

	return 0;

}

#ifdef CONFIG_SUPPORT_EWS
static al_int32  FW_EwsMon_callback(AI_EWSMon_Message_Type_t EventType, al_uint32 u32Param)
{
	FW_msg_t msg;

	msg.msg_type = FW_INVALID_EVENT;

	if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
		|| fw_data_struct.fw_mode == FW_EPG_MODE
		|| fw_data_struct.fw_mode == FW_RS232_MODE
		)
	{
		switch (EventType)
		{
			case AI_EWS_MONITOR_RECV_ANC:
				msg.msg_type = FW_EWS_REC_ANCM;
				break;

			case AI_EWS_MONITOR_NON_ANC:
				msg.msg_type = FW_EWS_NON_ANCM;
				break;

			case AI_EWS_MONITOR_FIND_AUD:
				msg.msg_type = FW_EWS_FIND_AUD;
				msg.u32Userparam = u32Param;
				break;

			default:
				break;
		}

		if (msg.msg_type != FW_INVALID_EVENT )
		{
			/* Send msg to frmwrk task */
			FW_DEBUG_MESSAGE("FW_EwsMon_callback msg.msg_type = %d\n", msg.msg_type);
			GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
		}
	}

	return 0;

}
#endif

#ifdef SUPPORT_ASTRA_LCN_MONITOR
static al_int32  FW_AstraLcnMon_callback(AI_AstraLcnMon_Message_Type_t EventType, al_uint32 u32Param)
{
	FW_msg_t msg;

	msg.msg_type = FW_INVALID_EVENT;

	if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
		|| fw_data_struct.fw_mode == FW_EPG_MODE
		|| fw_data_struct.fw_mode == FW_RS232_MODE
		)
	{
		switch (EventType)
		{
			case AI_ASTRALCN_MONITOR_RECVED_PMT:
				msg.msg_type = FW_ASTRALCN_RECED_PMT;
				break;

			case AI_ASTRALCN_MONITOR_START_RECVPMT:
				msg.msg_type = FW_ASTRALCN_START_RECVPMT;
				break;

			default:
				break;
		}

		if (msg.msg_type != FW_INVALID_EVENT )
		{
			/* Send msg to frmwrk task */
			FW_DEBUG_MESSAGE("FW_AstraLcnMon_callback msg.msg_type = %d\n", msg.msg_type);
			GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
		}
	}

	return 0;

}

#endif

static al_int32  FW_PsiMon_callback(AI_PsiMon_Message_Type_t EventType, al_uint32 u32Param)
{
	FW_msg_t msg;
	al_uint32 u32TotalNum = 0;
    
#ifdef SUPPORT_TURKY_NEW_CHNLIST  // SUPPORT_DYNAMIC_SRVUPDATE_S
    AL_RecHandle_t hCurrProgHdl = AL_DB_INVALIDHDL;
    AL_DB_EDBType_t eDBType = AL_DBTYPE_MAX;
    if ((MID_SUCCESS == MID_PlaybackGetCurProgHandle(al_null, &hCurrProgHdl))
        && (hCurrProgHdl != AL_DB_INVALIDHDL))
    {
        AL_DB_GetRecordType(hCurrProgHdl, &eDBType, al_null);
    }
#endif

	msg.msg_type = FW_INVALID_EVENT;

	if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
		|| fw_data_struct.fw_mode == FW_EPG_MODE
		|| fw_data_struct.fw_mode == FW_RS232_MODE
		)
	{
		switch (EventType)
		{
			case AI_PSIMONITOR_SERVICE_MOVE:
				msg.msg_type = FW_MONITOR_SERVICE_MOVE;
				break;

			case AI_PSIMONITOR_AUDIO_UPDATE:
				msg.msg_type = FW_MONITOR_AUDIO_UPDATE;
				break;

			case AI_PSIMONITOR_SUBTITLE_UPDATE:
				msg.msg_type = FW_MONITOR_SUBTITLE_UPDATE;
				break;
			case AI_PSIMONITOR_CLOSECAPTION_UPDATE: //add for CC
				msg.msg_type = FW_MONITOR_CLOSECAPTION_UPDATE;
				break;
			case AI_PSIMONITOR_TELETECT_UPDATE:
				msg.msg_type = FW_MONITOR_TELETEXT_UPDATE;
				break;

			case AI_PSIMONITOR_SRV_UPDATE:
				msg.msg_type = FW_MONITOR_UPDATE_SERVICE;
				break;

			case AI_PSIMONITOR_NETWORK_UPDATE:
				msg.msg_type = FW_MONITOR_UPDATE_NETWORK;
				break;

			case AI_PSIMONITOR_UPDATE_LCN:
				msg.msg_type = FW_MONITOR_UPDATE_SERLCN;
				break;

			case AI_PSIMONITOR_OTHERTP_SRV_FOUND:
				msg.msg_type = FW_MONITOR_OTHERTP_SERVICE_FOUND;
				break;

			case AI_PSIMONITOR_ADD_SRV:
#ifdef SUPPORT_TURKY_NEW_CHNLIST // SUPPORT_DYNAMIC_SRVUPDATE_S
                if (eDBType == AL_DBTYPE_DVB_S)
                {
                    break;
                }
#endif
				AI_PSI_Monitor_AddSrv(&u32TotalNum);

				if (u32TotalNum > 0)
				{
					msg.msg_type = FW_MONITOR_ADD_SERVICE;
				}
				break;

			case AI_PSIMONITOR_REMOVE_SRV:
#ifdef SUPPORT_TURKY_NEW_CHNLIST // SUPPORT_DYNAMIC_SRVUPDATE_S
                if (eDBType == AL_DBTYPE_DVB_S)
                {
                    break;
                }
#endif
				AI_PSI_Monitor_RemoveSrv(&u32TotalNum);

				if (u32TotalNum > 0)
				{
					msg.msg_type = FW_MONITOR_REMOVE_SERVICE;
				}
				break;

			case AI_PSIMONITOR_TS_CHANGED:
				msg.msg_type = FW_MONITOR_TS_CHANGED;
				msg.u32Userparam = u32Param;
				break;

			case AI_PSIMONITOR_NEW_TSID:
				msg.msg_type = FW_MONITOR_NEW_TSID;
				msg.u32Userparam = u32Param;
				break;

			case AI_PSIMONITOR_FTADATA_UPDATE:
				msg.msg_type = FW_MONITOR_FTADATA_UPDATE;
				break;
			case AI_PSIMONITOR_TIME_UPDATE:
			case AI_PSIMONITOR_TIME_OFFSET_UPDATE:
					FW_DEBUG_MESSAGE("tot or tdt comes to update system time#####################\n");
                    msg.msg_type = FW_MONITOR_TIME_UPDATE;
					if (AI_Time_GetTimeFristPowerON() == al_false)
					{
						al_uint32 u32CurTimeInSec = 0;
						AL_Time_GetCurTimeInSecs(&u32CurTimeInSec);
						u32CurTimeInSec -=AL_Time_GetGMTOffset();
						{
								AL_Time_t  p_RTCDate;
								AL_Time_Secs2Date(&u32CurTimeInSec,&p_RTCDate);
								FW_DEBUG_MESSAGE("[debug]%s		%d\n",__FUNCTION__,__LINE__);
								FW_DEBUG_MESSAGE("year %d, month %d, day %d, hour %d, min %d, sec %d\n\n\n\n",p_RTCDate.Year,
									p_RTCDate.Month,p_RTCDate.Day,p_RTCDate.Hour,p_RTCDate.Min,p_RTCDate.Sec);
						}

						if(AL_REMINDER_EVENT_EXPIRED == AL_PR_Update(0, u32CurTimeInSec))
						{
							msg.msg_type = FW_MONITOR_REMINDER_EXPIRE;
						}
						AI_Time_SetTimeFristPowerON(al_true);
					}
					else
					{
						if (AI_Time_GetTimeSync() == al_false)
						{
							AI_Time_SetTimeSync(al_true);
							al_uint32 u32CurTimeInSec = 0;
							AL_Time_GetCurTimeInSecs(&u32CurTimeInSec);
							{
								AL_Time_t  p_RTCDate;
								AL_Time_Secs2Date(&u32CurTimeInSec,&p_RTCDate);
								FW_DEBUG_MESSAGE("[debug]%s		%d\n",__FUNCTION__,__LINE__);
								FW_DEBUG_MESSAGE("year %d, month %d, day %d, hour %d, min %d, sec %d\n\n\n\n",p_RTCDate.Year,
									p_RTCDate.Month,p_RTCDate.Day,p_RTCDate.Hour,p_RTCDate.Min,p_RTCDate.Sec);
							}
							/*<When the platform first get the new system time  from the stream,
							 not to change the PR events */
							#if 0
								AL_PR_Update(u32CurTimeInSec - u32Param, 0);
							#else
								AL_PR_Update(0, u32CurTimeInSec);
							#endif
						}
						else
						{
							FW_DEBUG_MESSAGE("[debug]%s		%d\n",__FUNCTION__,__LINE__);
							FW_DEBUG_MESSAGE("EventType is %d,AL_Time_GetGMTOffset %d,AI_Time_GetOldTimeOffset %d\n\n",EventType,AL_Time_GetGMTOffset(),AI_Time_GetOldTimeOffset());
							if (AI_PSIMONITOR_TIME_OFFSET_UPDATE ==  EventType && AL_Time_GetGMTOffset() != AI_Time_GetOldTimeOffset())
							{
								al_uint32 u32CurTimeInSec = 0;
								AL_Time_GetCurTimeInSecs(&u32CurTimeInSec);
								{
									AL_Time_t  p_RTCDate;
									AL_Time_Secs2Date(&u32CurTimeInSec,&p_RTCDate);
									FW_DEBUG_MESSAGE("[debug]%s		%d\n",__FUNCTION__,__LINE__);
									FW_DEBUG_MESSAGE("year %d, month %d, day %d, hour %d, min %d, sec %d\n\n\n\n",p_RTCDate.Year,
										p_RTCDate.Month,p_RTCDate.Day,p_RTCDate.Hour,p_RTCDate.Min,p_RTCDate.Sec);
								}
								AL_PR_Update(u32CurTimeInSec - u32Param, 0);
							}
							else
							{
								AL_PR_Update(0, 0);
							}
							msg.u32Userparam = u32Param;
							msg.msg_type = FW_MONITOR_REMINDER_TIME_CHANGE;
						}
					}
				break;
			case AI_PSIMONITOR_SERVICE_REPLACE:
				msg.msg_type = FW_MONITOR_REPLACE_SERVICE;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
			case AI_PSIMONITOR_SERVICE_SIMULCASE_REPLACE:
				FW_DEBUG_MESSAGE("[%s:%d] AI_PSIMONITOR_SERVICE_SIMULCASE_REPLACE !!!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
				msg.msg_type = FW_MONITOR_REPLACE_SIMULCASE_SERVICE;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
			case AI_PSIMONITOR_FIND_NEW_TPLIST:
				msg.msg_type = FW_MONITOR_FIND_NEW_TPLIST;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
		#ifdef SUPPORT_NETWORK_UPDATE_RESCAN
			case AI_PSIMONITOR_NWK_UPDATE_STATIC:
				msg.msg_type = FW_MONITOR_UPDATE_NWK_STATIC;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
			case AI_PSIMONITOR_NWK_UPDATE_RESCAN:
				msg.msg_type = FW_MONITOR_UPDATE_NWK_RESCAN;
				break;
		#endif
			case AI_PSIMONITOR_PMT_UPDATE:
				FW_DEBUG_MESSAGE("[%s:%d] AI_PSIMONITOR_PMT_UPDATE \n",__FUNCTION__,__LINE__);
				msg.msg_type = FW_MONITOR_PMT_UPDATE;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
			case AI_PSIMONITOR_SERTYPE_CHANGED:
				msg.msg_type = FW_MONITOR_SERTYPE_CHANGED;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
#ifdef CONFIG_SUPPORT_INDONESIA_EWS
			case AI_PSIMONITOR_INDONESIA_EWS_UPDATE:
				FW_DEBUG_MESSAGE("[%s:%d] AI_PSIMONITOR_INDONESIA_EWS_UPDATE \n",__FUNCTION__,__LINE__);
				msg.msg_type = FW_INDONESIA_EWS_UPDATE;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
			case AI_PSIMONITOR_INDONESIA_EWS_STOP:
				FW_DEBUG_MESSAGE("[%s:%d] AI_PSIMONITOR_INDONESIA_EWS_STOP \n",__FUNCTION__,__LINE__);
				msg.msg_type = FW_INDONESIA_EWS_STOP;
				msg.u32Userparam = (al_uint32)u32Param;
				break;
#endif
			default:
				break;
		}

		if (msg.msg_type != FW_INVALID_EVENT )
		{
			/* Send msg to frmwrk task */
			FW_DEBUG_MESSAGE("FW_PsiMon_callback msg.msg_type = %d\n", msg.msg_type);
			GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
		}
	}

	return 0;

}
#endif
#ifdef CONFIG_SUPPORT_SW_AFC
void FW_AFT_callback(MID_AFC_EVENT_t eventID)
{
	FW_msg_t msg;
	msg.msg_type = FW_INVALID_EVENT;

	switch(eventID)
	{
		case MID_AFT_PROCESS_DONE:
			msg.msg_type = FW_AFT_PROCESS_DONE;
			break;
		case MID_AFT_ADEMOD_PQSET:
			msg.msg_type = FW_AFT_ADEMOD_PQSET;
			break;
		default:
			break;
	}
	if (msg.msg_type != FW_INVALID_EVENT )
	{
		/* Send msg to frmwrk task */
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}
}
#endif

void FW_TVFE_BURNMODE_callback(MID_TVFE_BurnnMode_e eventID,UINT32 u32Param)
{
	FW_msg_t msg;
	msg.msg_type = FW_INVALID_EVENT;

	switch(eventID)
	{
		case MID_TVFE_BURNMODE_TIME:
			msg.msg_type = FW_TVFE_TYPE_BURNNTIME;
            msg.u32Userparam = (al_uint32)u32Param;
			break;
	
#ifdef CONFIG_SUPPORT_DETECT_USBPLL
		case MID_TVFE_USBPLL_FAIL:
			msg.msg_type = FW_TYPE_USBPLLFail;
            msg.u32Userparam = (al_uint32)u32Param;
			break;
#endif			
		default:
			break;
	}
	if (msg.msg_type != FW_INVALID_EVENT )
	{
		/* Send msg to frmwrk task */
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}
}
static al_int32 FW_Time_callback(AI_Time_Message_Type_t EventType, al_uint32 u32Param)
{
	#ifdef CONFIG_DTV_SUPPORT	/* DVB related module */
	FW_msg_t msg;

	msg.msg_type = FW_INVALID_EVENT;

	if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
		|| fw_data_struct.fw_mode == FW_EPG_MODE
		|| fw_data_struct.fw_mode == FW_RS232_MODE
		)
	{
		switch (EventType)
		{
			case AI_TIME_UPDATE:
					FW_DEBUG_MESSAGE("al time update system time#####################\n");
					msg.msg_type = FW_MONITOR_TIME_UPDATE;

					if (AI_Time_GetTimeFristPowerON() == al_false)
					{
						al_uint32 u32CurTimeInSec = 0;
						AL_Time_GetCurTimeInSecs(&u32CurTimeInSec);
						{
								AL_Time_t  p_RTCDate;
								AL_Time_Secs2Date(&u32CurTimeInSec,&p_RTCDate);
								FW_DEBUG_MESSAGE("[debug]%s		%d\n",__FUNCTION__,__LINE__);
								FW_DEBUG_MESSAGE("year %d, month %d, day %d, hour %d, min %d, sec %d\n\n\n\n",p_RTCDate.Year,
									p_RTCDate.Month,p_RTCDate.Day,p_RTCDate.Hour,p_RTCDate.Min,p_RTCDate.Sec);
						}

						if(AL_REMINDER_EVENT_EXPIRED == AL_PR_Update(0, u32CurTimeInSec))
						{
							msg.msg_type = FW_MONITOR_REMINDER_EXPIRE;
						}
						AI_Time_SetTimeFristPowerON(al_true);
					}
					else
					{
						al_uint32 u32CurTimeInSec = 0;
						AL_Time_GetCurTimeInSecs(&u32CurTimeInSec);
						{
								AL_Time_t  p_RTCDate;
								AL_Time_Secs2Date(&u32CurTimeInSec,&p_RTCDate);
								FW_DEBUG_MESSAGE("[debug]%s		%d\n",__FUNCTION__,__LINE__);
								FW_DEBUG_MESSAGE("year %d, month %d, day %d, hour %d, min %d, sec %d\n\n\n\n",p_RTCDate.Year,
									p_RTCDate.Month,p_RTCDate.Day,p_RTCDate.Hour,p_RTCDate.Min,p_RTCDate.Sec);
						}
						AL_PR_Update(u32CurTimeInSec - u32Param, 0);
					}
				break;
			default:
				break;
		}

		if (msg.msg_type != FW_INVALID_EVENT )
		{
			/* Send msg to frmwrk task */
			FW_DEBUG_MESSAGE("FW_PsiMon_callback msg.msg_type = %d\n", msg.msg_type);
			GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
		}
	}
	#endif

	return 0;

}

static al_void FW_event_handler(FW_msg_t *msg)
{
#ifdef CONFIG_DTV_SUPPORT
	AL_PR_details_t  *details_message;
	AL_Return_t ret;
#endif

#ifdef CONFIG_DTV_SUPPORT
	ret = AL_FAILURE;
	details_message = al_null;
#endif
#ifdef CONFIG_SUPPORT_EWS
	DBPIDInfo_t stAudioPid;
	memset(&stAudioPid, 0, sizeof(DBPIDInfo_t));
	stAudioPid.usDataPID = DB_DVB_INVALID_PID;
#endif
	switch (msg->msg_type)
	{
#ifdef CONFIG_DTV_SUPPORT
		case FW_SCHEDULER_EVENT:
			details_message = (AL_PR_details_t *)GL_MemAlloc(sizeof(AL_PR_details_t));
            if (!details_message)
            {
                break;
            }
			ret = AL_PR_GetById(msg->msg_data.fw_sch_id, details_message);
			if (ret != AL_SUCCESS)
			{
				FW_DEBUG_MESSAGE("get details error, id= %d ret = %d\n", msg->msg_data.fw_sch_id,ret);
				AL_PR_Delete(msg->msg_data.fw_sch_id);
				GL_MemFree(details_message);
                details_message = al_null;
				break;
			}
			else
			{
#ifdef CONFIG_SUPPORT_PVR /*This is a temp test*/
				AL_FW_PVR_SetProgReminderId(msg->msg_data.fw_sch_id);
#endif
				FW_DEBUG_MESSAGE("%s	%d\n",__FUNCTION__,__LINE__);
				FW_DEBUG_MESSAGE("handle = 0x%x\n",details_message->rem_details.handle);
			}

			/* Some message can be of expired event */
			if (AI_PR_HandleTrigger(msg->msg_data.fw_sch_id) != AL_SUCCESS)
			{
				AI_PR_HandleAction(msg->msg_data.fw_sch_id );
			}

			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, details_message);
			}
			break;

		case FW_SCHEDULER_MSG:
			/*Message to app*/
			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, &msg->msg_data.fw_sch_id);
			}
			break;

		case FW_SCHEDULER_EXPIRE:
			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, &msg->msg_data.fw_sch_id);
			}
			break;

		case FW_SERVICE_DELETED_EVENT:
			/* Check if the current service is deleted */
			AI_PR_HandleSrvDelete(msg->msg_data.cdbi_serv);
			break;

		case FW_SERVICE_UPDATE_EVENT:
			FW_DEBUG_MESSAGE("\n\n\n****SERVICE UPDATED EVENT******\n\n\n\n");
			fw_data_struct.msg_to_app = FW_SERVICE_UPDATE_EVENT;

			if ((fw_data_struct.fw_app_cb != NULL ) && (fw_data_struct.msg_to_app != FW_INVALID_EVENT))
			{
				/* Inform to the application thru callback */
				(fw_data_struct.fw_app_cb)(fw_data_struct.msg_to_app , NULL);
			}
			break;
#endif
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
		case FW_DB_LOAD_DEFAULT:
			FW_DEBUG_MESSAGE("\n\n\n****LOAD DEFAULT DB******\n\n\n\n");
			fw_data_struct.msg_to_app = FW_DB_LOAD_DEFAULT;
			if ((fw_data_struct.fw_app_cb != NULL ) && (fw_data_struct.msg_to_app != FW_INVALID_EVENT))
			{
				/* Inform to the application thru callback */
				(fw_data_struct.fw_app_cb)(fw_data_struct.msg_to_app , &msg->u32Userparam);
			}
			break;
#endif
#ifdef CONFIG_DTV_SUPPORT
		case FW_EPG_NOWNEXT_UPDATE_EVENT:
		//case FW_EPG_SCHEDULE_UPDATE_EVENT:
			FW_DEBUG_MESSAGE("\n\n\n ***FW_EPG_NOWNEXT_UPDATE_EVENT*\n\n\n");
#ifndef CONFIG_USE_COMPRESSED_EPGMODULE
			if (msg->u32Userparam != 0)
			{
				al_uint16 offset = 0;
				al_uint8 FTA_Content_Desc[3] = {0};
				/*  */
				if (AL_SUCCESS == AL_Event_GetDescriptor(
					(AL_Event_t)msg->u32Userparam, DESCR_FTA_CONTENT_MGR, &offset, FTA_Content_Desc))
				{
					AI_PSI_Monitor_FTADataParse(FTA_Content_Desc[2], DTGCMG_TBL_EIT);
				}
			}
#endif
			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, &(msg->u32Userparam));
			}
			break;


		case FW_EPG_SCHEDULE_UPDATE_EVENT:
			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, NULL);
			}
			break;
#endif
#ifdef CONFIG_ATV_SUPPORT
		case FW_DATASAVING_ATVDB_STORE_START_EVENT:
		case FW_DATASAVING_ATVDB_STORE_END_EVENT:
#endif
#ifdef CONFIG_DTV_SUPPORT
		case FW_DATASAVING_DVBDB_STORE_START_EVENT:
		case FW_DATASAVING_DVBDB_STORE_END_EVENT:
#endif
		case FW_DATASAVING_USERSETING_STORE_START_EVENT:
		case FW_DATASAVING_USERSETING_STORE_END_EVENT:
			FW_DEBUG_MESSAGE("\n\n\n ***FW_DATA_SAVING_EVENT*\n\n\n");

			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, NULL);
			}
			break;

		case FW_MONITOR_TUNER_LOCKED:
		case FW_MONITOR_TUNER_UNLOCKED:
		case FW_MONITOR_STREAM_NO_DATA:
		case FW_MONITOR_STREAM_INVALID_DATA:
		case FW_MONITOR_STREAM_UNSUPPORT_DATA:
		case FW_MONITOR_STREAM_HAS_DATA:
		case FW_MONITOR_SERVICE_VIDEO_AUDIO_SCRAMBLE:
		case FW_MONITOR_SERVICE_ONLY_VIDEO_SCRAMBLE:
		case FW_MONITOR_SERVICE_ONLY_AUDIO_SCRAMBLE:
		case FW_MONITOR_SERVICE_NO_SCRAMBLE:
		case FW_MONITOR_PARENTAL_LOCKED:
		case FW_MONITOR_PARENTAL_UNLOCKED:
		case FW_MONITOR_SERVICE_MOVE:
		case FW_MONITOR_AUDIO_UPDATE:
		case FW_MONITOR_SUBTITLE_UPDATE:
		case FW_MONITOR_CLOSECAPTION_UPDATE:
		case FW_MONITOR_TELETEXT_UPDATE:
		case FW_MONITOR_UPDATE_SERVICE:
		case FW_MONITOR_UPDATE_NETWORK:
		case FW_MONITOR_UPDATE_SERLCN:
		case FW_MONITOR_OTHERTP_SERVICE_FOUND:
		case FW_MONITOR_ADD_SERVICE:
		case FW_MONITOR_REMOVE_SERVICE:
		case FW_MONITOR_REMINDER_EXPIRE:
		case FW_MONITOR_REMINDER_TIME_CHANGE:
		case FW_MONITOR_REPLACE_SERVICE:
		case FW_MONITOR_REPLACE_SIMULCASE_SERVICE:
		case FW_MONITOR_TS_CHANGED:
		case FW_MONITOR_NEW_TSID:
 		case FW_MONITOR_TIME_UPDATE:
		case FW_MONITOR_FIND_NEW_TPLIST:
	#ifdef SUPPORT_NETWORK_UPDATE_RESCAN
		case FW_MONITOR_UPDATE_NWK_STATIC:
		case FW_MONITOR_UPDATE_NWK_RESCAN:
	#endif
		case FW_MONITOR_PMT_UPDATE:
		case FW_MONITOR_SERTYPE_CHANGED:
		case FM_DTV_AUDIO_STARTED:
		case FM_DTV_AUDIO_SAMPLE_STOPPED:
		case FM_DTV_AUDIO_STOPPED:
#ifdef CONFIG_SUPPORT_INDONESIA_EWS
		case FW_INDONESIA_EWS_UPDATE:
		case FW_INDONESIA_EWS_STOP:
#endif
			FW_DEBUG_MESSAGE("\n\n\n FW MONITOR MSG: %d\n\n\n", msg->msg_type);
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
#ifdef CONFIG_SUPPORT_EWS
		case FW_EWS_REC_ANCM:
			printf("[%s:%d] FW_EWS_REC_ANCM ========================\n",__FUNCTION__,__LINE__);
			if (AL_SUCCESS == AI_EWS_Monitor_FindAudioPid(&stAudioPid))
			{
				if (stAudioPid.usDataPID == DB_DVB_INVALID_PID)
				{
					AI_EWS_Monitor_Start();
				}
				else
				{
					AI_EWS_Monitor_Stop(TRUE);
					//printf("[%s:%d] uAudioPid =========== %d\n",__FUNCTION__,__LINE__,stAudioPid.usDataPID);
					if (fw_data_struct.fw_app_cb != al_null)
					{
						DBPIDInfo_t *pAudioPid = al_null;
						pAudioPid = (DBPIDInfo_t *)zero_malloc(sizeof(DBPIDInfo_t));
						if (pAudioPid != al_null)
						{
							memcpy(pAudioPid, &stAudioPid, sizeof(DBPIDInfo_t));
							msg->u32Userparam = (al_uint32)pAudioPid;
							(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
						}
						else
						{
							FW_ERROR_MESSAGE("\n\n\n [%s:%d] alloc memery fail\n\n\n", __FUNCTION__,__LINE__);
						}
					}
				}
			}
			break;
		case FW_EWS_NON_ANCM:
			printf("[%s:%d] FW_EWS_NON_ANCM ========================\n",__FUNCTION__,__LINE__);
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
		case FW_EWS_FIND_AUD:
			printf("[%s:%d] FW_EWS_FIND_AUD ========================\n",__FUNCTION__,__LINE__);
			memcpy(&stAudioPid, (DBPIDInfo_t *)(msg->u32Userparam), sizeof(DBPIDInfo_t));
			AI_EWS_Monitor_Stop(TRUE);
			//printf("[%s:%d] uAudioPid =========== %d\n",__FUNCTION__,__LINE__,stAudioPid.usDataPID);
			if (stAudioPid.usDataPID != DB_DVB_INVALID_PID)
			{
				if (fw_data_struct.fw_app_cb != al_null)
				{
					(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
				}
				else
				{
					GL_MemFree((DBPIDInfo_t *)(msg->u32Userparam));
					msg->u32Userparam = 0;
				}
			}
			else
			{
				GL_MemFree((DBPIDInfo_t *)(msg->u32Userparam));
				msg->u32Userparam = 0;
			}
			break;
		case FW_EWS_ACTIVE:
		case FW_EWS_INACTIVE:
			FW_DEBUG_MESSAGE("\n\n\n FW MONITOR MSG: %d\n\n\n", msg->msg_type);
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
            break;
#ifdef SUPPORT_ASTRA_LCN_MONITOR
		case FW_ASTRALCN_RECED_PMT:
			AI_AstraLcn_Monitor_StopGetSgtPid();
			if (AL_SUCCESS == AI_AstraLcn_Monitor_SetPid())
			{
				AI_AstraLcn_Monitor_Start();
			}
			break;

		case FW_ASTRALCN_START_RECVPMT:
			AI_AstraLcn_Monitor_StartGetSgtPid();
			break;
#endif

#endif
#ifdef CONFIG_SUPPORT_TTX
		case FW_TT_NO_DATA:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, al_null);
			}
			break;
#endif
		case FW_SIGNAL_PRESENT:
        case FW_VIP_FINISH:
		case FW_VIP_STATUS:
		case FW_NO_SIGNAL:
		case FW_SIGNAL_IN_PROGRESS:
		case FW_NOT_SUPPORT:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, al_null);
			}
			break;
		case FW_HDMI_CHANGE_SOURCE:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
		case FW_AUDIO_HEADPHONE_PLUG_IN:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
		case FW_AUDIO_HEADPHONE_PLUG_OUT:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
		case FW_AUDIO_ADC_TURNOFFSOUND:
		case FW_ADC_AUTOCHANGE_SOURCE:
		case FW_ADC_AUTOADJUST:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
#ifdef CONFIG_SUPPORT_AUDIO_CODEC_OVERLAY
		case FW_AUDIO_LOADCODEC_OK:
		case FW_AUDIO_LOADCODEC_ERROR:
		case FW_AUDIO_LOADCODEC_TYPE:
			FW_DEBUG_MESSAGE("\n\n\n FW MONITOR MSG: %d, u32Userparam: %d\n\n\n", msg->msg_type, msg->u32Userparam);
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
#endif
		case FW_SCART_INPUTSRC_CHANGE:
		case FW_SCART_INPUTSTATUS_CHANGE:
		case FW_HDMI_HDMIDVI_CHANGE:
		case FW_HDMI_ACPTYPE_CHANGE:
		case FW_HDMI_PROCESSING:
        case FW_USB_PLUGOUT:
        case FW_USB_PLUGIN:
        case FW_USB_HUNGTASK:			
		case FW_HDMI_MHL_RCP_CMD:
		case FW_HDMI_MHL_CONNECTED:
		case FW_AUDIO_SIF_SAS_OK:
		case FW_AUDIO_SIF_SCS_OK:
			FW_DEBUG_MESSAGE("aaaaaaaaaaaaaaaa msg->msg_type=%d\n",msg->msg_type);
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
#ifndef SUPPORT_CEC_TV
		case FW_HDMI_PORTSTATUS:
			FW_DEBUG_MESSAGE("aaaaaaaaaaaaaaaa msg->msg_type=%d \n",msg->msg_type);
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
#ifdef CONFIG_SUPPORT_NOSIGNAL_AUTO_CHANGE_SOURCE
				u32HDMIActive = msg->u32Userparam;
#endif
			}
			break;
#endif
		case FW_QSD_ATV_SetCH:
		case FW_QSD_DTV_SetCH:
		case FW_QSD_Media_Play:
		case FW_QSD_Source_Set:
        case FW_QSD_DTV_SetFreq:
#ifdef CONFIG_CTV_UART_FAC_MODE
        case FW_RS232_SET_SOURCE:
        case FW_RS232_SET_VOL:
#endif
#ifdef SUPPORT_FACTORY_AUTO_TEST_MODE4
		case FW_FAC_TEST_SET_SOURCE:
		case FW_AUTOTEST_RESET:
		case FW_AUTOTEST_UPDATESW:
		case FW_FAC_UPDATE_HDCP_KEY:
#endif
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
#ifdef CONFIG_VCHIP_SUPPORT
        case FW_CC_VCHIP_DATA:
            {
                al_uint8 u8Data[2];
                al_bool bLocked = al_false;
                /* Character1 & Character1*/
                u8Data[0] = msg->u32Userparam & 0xff;
                u8Data[1] = (msg->u32Userparam >> 8) & 0xff;
                AI_PC_CheckXDSRating(u8Data, &bLocked);
                if (bLocked == al_true)
                {
                    (fw_data_struct.fw_app_cb)(FW_VCHIP_LOCK, al_null);
                }
                else
                {
                    (fw_data_struct.fw_app_cb)(FW_VCHIP_UNLOCK, al_null);
                }
            }
            break;
        case FW_VCHIP_LOCK:
        case FW_VCHIP_UNLOCK:
            if (fw_data_struct.fw_app_cb != al_null)
            {
                (fw_data_struct.fw_app_cb) (msg->msg_type, al_null);
            }
            break;
#endif
#ifdef CONFIG_SUPPORT_SW_AFC
		case FW_AFT_ADEMOD_PQSET:
		case FW_AFT_PROCESS_DONE:
			if (fw_data_struct.fw_mode != FW_CHAN_SRCH_MODE)
			{
				if (fw_data_struct.fw_app_cb != NULL)
				{
					(fw_data_struct.fw_app_cb)(msg->msg_type, NULL);
				}
			}
			break;
#endif
#ifdef CONFIG_HDMI_SPD
		case FW_HDMI_SPD_INFOFRAME_UPDATE:
			if (fw_data_struct.fw_app_cb != NULL)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
#endif
        case FW_TVFE_TYPE_BURNNTIME:
            {
            frmwrk_pfOpConext_t pfGetContext = al_null;

        	AL_FW_GetContextCb(&pfGetContext);
            UINT32 BurnTime = msg->u32Userparam;
        	if (pfGetContext != al_null)
        	{
        		pfGetContext(FW_CONTEXT_TYPE_TYPE_BURNNTIME, FW_CONTEXT_OP_TYPE_WRITE, (al_void *)&BurnTime ,0);
        	}
            }
            break;
#ifdef CONFIG_SUPPORT_DETECT_USBPLL
		case FW_TYPE_USBPLLFail:
		{
            frmwrk_pfOpConext_t pfGetContext = al_null;

        	AL_FW_GetContextCb(&pfGetContext);
            UINT32 Flag = msg->u32Userparam;
        	if (pfGetContext != al_null)
        	{
        		pfGetContext(FW_CONTEXT_TYPE_TYPE_USBPLLFail, FW_CONTEXT_OP_TYPE_WRITE, (al_void *)&Flag ,0);
        	}
        }
		break;
#endif
#ifdef CONFIG_SUPPORT_NET_SETTING
		case FW_NETWORK_ENABLE:
		case FW_NETWORK_DISABLE:
	          if (fw_data_struct.fw_app_cb != al_null)
	          {
	              (fw_data_struct.fw_app_cb) (msg->msg_type, NULL);
	          }
	          break;
#endif
		case FW_MCCS_IMAGE_LUMINANCE:
		case FW_MCCS_IMAGE_CONTRAST:
		case FW_MCCS_IMAGE_SHARPNESS:
		case FW_MCCS_IMAGE_SATURATION:
		case FW_MCCS_IMAGE_RGAIN:
		case FW_MCCS_IMAGE_GGAIN:
		case FW_MCCS_IMAGE_BGAIN:
		case FW_MCCS_IMAGE_ROFFSET:
		case FW_MCCS_IMAGE_GOFFSET:
		case FW_MCCS_IMAGE_BOFFSET:
		case FW_MCCS_IMAGE_COLORLUT_SATGAIN_R:
		case FW_MCCS_IMAGE_COLORLUT_SATGAIN_Y:
		case FW_MCCS_IMAGE_COLORLUT_SATGAIN_G:
		case FW_MCCS_IMAGE_COLORLUT_SATGAIN_C:
		case FW_MCCS_IMAGE_COLORLUT_SATGAIN_B:
		case FW_MCCS_IMAGE_COLORLUT_SATGAIN_M:
		case FW_MCCS_CLOSE_USERMENU:
		case FW_MCCS_IMAGE_GAMMA_TABLE_INDEX:
		case FW_MCCS_IMAGE_CONTRAST_AUTO:
		case FW_MCCS_FLASH_CTRL:
		case FW_MCCS_OSD_HPOSITION:
		case FW_MCCS_OSD_VPOSITION:
		case FW_MCCS_AUDIOVOLUME:
		case FW_MCCS_OSD_LANGUAGE:
		case FW_MCCS_SET_VGASETTING_AUTO:
		case FW_MCCS_SET_VGASETTING_HPOSITION:
		case FW_MCCS_SET_VGASETTING_VPOSITION:
		case FW_MCCS_SET_VGASETTING_CLOCK:
		case FW_MCCS_SET_VGASETTING_PHASE:
		case FW_MCCS_POWER_SETMODE:
		case FW_MCCS_AUDIOMUTE:
		case FW_MCCS_OSDLOCK:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, (al_void *)&(msg->u32Userparam));
			}
			break;
		case FW_MCCS_FBOSD_LANGUAGE:
			if (fw_data_struct.fw_app_cb != al_null)
			{
				(fw_data_struct.fw_app_cb)(msg->msg_type, al_null);
			}
			break;
		default:
			break;
	}
	return;
}

static void FW_frmwrk_task(void)
{
	FW_msg_t msg;
	GL_Status_t osal_err;

	while (al_true)
	{
		/*
		 ** Keep waiting till messages received from middleware callback
		 ** and component callbacks
		 */
		 memset(&msg, 0, sizeof(FW_msg_t));

		osal_err = GL_QueueReceive(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t), FW_QUEUE_TIMEOUT);

		if (osal_err != GL_SUCCESS)
		{
			FW_ERROR_MESSAGE("Error .... OSAL_QUEUE ERROR() \n");
			ASSERT(1,"OSAL_QUEUE ERROR ");
		}
		if (msg.msg_type != FW_TASK_EXIT) {

			if (GL_SUCCESS != GL_MutexLock(fw_data_struct.fw_mutex_id))
			{
				FW_ERROR_MESSAGE("Error .... GL_MutexLock() \n");
				ASSERT(1,"GL_SemaphoreTake() failed");
			}
			switch (fw_data_struct.fw_mode)
			{
				case FW_NORMAL_VIEW_MODE:
				case FW_EPG_MODE:
					FW_event_handler(&msg);
					break;

				case FW_POWER_DOWN_MODE:
					if ((msg.msg_type == FW_SCHEDULER_EVENT) ||(msg.msg_type == FW_SCHEDULER_MSG))
					{
						FW_event_handler(&msg);
					}
					break;

				case FW_CHAN_SRCH_MODE:
					if (msg.msg_type == FW_SERVICE_DELETED_EVENT ||
						msg.msg_type == FW_DATASAVING_ATVDB_STORE_START_EVENT ||
						msg.msg_type == FW_DATASAVING_ATVDB_STORE_END_EVENT ||
						msg.msg_type == FW_DATASAVING_DVBDB_STORE_START_EVENT ||
						msg.msg_type == FW_DATASAVING_DVBDB_STORE_END_EVENT ||
						msg.msg_type == FW_SIGNAL_PRESENT ||
						msg.msg_type == FW_VIP_FINISH ||
						msg.msg_type == FW_NO_SIGNAL ||
						msg.msg_type == FW_SIGNAL_IN_PROGRESS||
						msg.msg_type == FW_SCART_INPUTSRC_CHANGE ||
						msg.msg_type == FW_SCART_INPUTSTATUS_CHANGE ||
						msg.msg_type == FW_AUDIO_HEADPHONE_PLUG_IN ||
						msg.msg_type == FW_AUDIO_HEADPHONE_PLUG_OUT)
					{
						FW_event_handler(&msg);
					}
					break;

				case FW_UPGRADE_MODE:
					break;

				case FW_RESET_MODE:
					break;

                case FW_RS232_MODE:
				case FW_QSD_MODE:
					FW_event_handler(&msg);
					break;

				default:
					break;
			}
			if (GL_SUCCESS != GL_MutexUnlock(fw_data_struct.fw_mutex_id))
			{
				FW_ERROR_MESSAGE("Error .... GL_MutexUnlock() \n ");
				ASSERT(1,"GL_SemaphoreGive() failed");
			}
		}
		else
		{
			if (GL_TaskSelfDelete() != GL_SUCCESS)
			{
				FW_ERROR_MESSAGE( "Frmwrk task delete Failed \n");
				return ;
			}

			break;
		}
	}
}

static AL_Return_t FW_InitResources(al_void)
{
	/*creating Semaphore */
	if (GL_SUCCESS != GL_MutexCreate(FW_SEM_NAME, &fw_data_struct.fw_mutex_id))
	{
		FW_ERROR_MESSAGE("Semaphore creation failed\n");
		goto Fail;
	}

	if (GL_SUCCESS != GL_QueueCreate(FW_QUEUE_NAME, FW_QUEUE_MSG_SIZE, FW_QUEUE_MSG_CNT,
		FW_QUEUE_MSG_URGENT_CNT, &fw_data_struct.fw_qid))
	{
		FW_ERROR_MESSAGE("Frmwrk queue creation failed\n");
		goto Fail;
	}

	if (GL_SUCCESS != GL_TaskCreate(FW_TASK_NAME, (void *)FW_frmwrk_task, NULL,
		FW_TASK_PRIORITY, FW_TASK_STK_SIZE, FW_TASK_FLAG, &fw_data_struct.fw_task_id))
	{
		FW_ERROR_MESSAGE("Frmwrk task creation failed\n");
		goto Fail;
	}
	if (GL_SUCCESS != GL_TaskActivate(fw_data_struct.fw_task_id))
	{
		FW_ERROR_MESSAGE("Frmwrk task creation failed\n");
		goto Fail;
	}
	return AL_SUCCESS;

Fail:
	if (fw_data_struct.fw_mutex_id != 0)
	{
		GL_MutexDelete(fw_data_struct.fw_mutex_id);
		fw_data_struct.fw_mutex_id = 0;
	}

	if (fw_data_struct.fw_qid != 0)
	{
		GL_QueueDelete(fw_data_struct.fw_qid);
		fw_data_struct.fw_qid = 0;
	}

	if (fw_data_struct.fw_task_id)
	{
		GL_TaskDestroyOne(fw_data_struct.fw_task_id);
		fw_data_struct.fw_task_id = 0;
	}

	return AL_FAILURE;
}

void FW_KMFtoUMF_callback(unsigned char ID, unsigned char Type, unsigned char *Info, unsigned char size)
{
	FW_msg_t msg;
	MID_TVFE_InputStatus_e eStatus;
	msg.msg_type = FW_INVALID_EVENT;
	APP_Source_Type_t eSourType = APP_SOURCE_MAX;
	APP_GUIOBJ_Source_GetCurrSource(&eSourType);

	switch(ID)
	{
#ifdef CONFIG_DTV_SUPPORT
		case KMF2UMF_EVID_DTV:
			if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
				|| fw_data_struct.fw_mode == FW_EPG_MODE
				|| fw_data_struct.fw_mode == FW_RS232_MODE
				)
			{
				switch (Type)
				{
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_SCRAMBLED_DATA://Audio&Video Scramble
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_VIDEO_SCRAMBLED_DATA://Video Scramble,but Audio not scramble.
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_AUDIO_SCRAMBLED_DATA:
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_DATA:
					case KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_INVALID:
					case KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_NOT_SUPPORT:
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_NO_DATA:
					case KMF2UMF_EVTYPE_DTV_DEMUX_AV_INVALID:
					case KMF2UMF_EVTYPE_DTV_DEMUX_AUDIO_INVALID:
						MID_TVFE_GetDRVInputStatus(Info, &eStatus);
						break;
					default:
						break;
				}
				switch (Type)
				{

					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_SCRAMBLED_DATA:
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_HAS_SCRAMBLED_DATA;//Video&Audio Scrambled.
						msg.msg_type = FW_MONITOR_SERVICE_VIDEO_AUDIO_SCRAMBLE;
						AI_PSI_Monitor_ReplaceProcess(AL_PSIMONITOR_STATE_SCRAMBLED_REPLACEMENT_EXIST);
						break;

					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_VIDEO_SCRAMBLED_DATA://Video Scramble,but Audio not scramble.
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_HAS_VIDEO_SCRAMBLED_DATA;
						msg.msg_type = FW_MONITOR_SERVICE_ONLY_VIDEO_SCRAMBLE;
						AI_PSI_Monitor_ReplaceProcess(AL_PSIMONITOR_STATE_SCRAMBLED_REPLACEMENT_EXIST);
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_AUDIO_SCRAMBLED_DATA:
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_HAS_AUDIO_SCRAMBLED_DATA;
						msg.msg_type = FW_MONITOR_SERVICE_ONLY_AUDIO_SCRAMBLE;
						AI_PSI_Monitor_ReplaceProcess(AL_PSIMONITOR_STATE_SCRAMBLED_REPLACEMENT_EXIST);
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_DATA:
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_HAS_DATA;
						//if (DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_HAS_SCRAMBLED_DATA)
						{
							msg.msg_type = FW_MONITOR_SERVICE_NO_SCRAMBLE;
						}
						//else
						{
							//msg.msg_type = FW_MONITOR_STREAM_HAS_DATA;
						}
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_INVALID:
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_INVALID;
						//msg.msg_type = FW_MONITOR_STREAM_NO_DATA;
						//AI_PSI_Monitor_ReplaceProcess(AL_PSIMONITOR_STATE_NOT_RUNNING_REPLACEMENT_EXIST);
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_NOT_SUPPORT:
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_NOT_SUPPORT;
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_HAS_NO_DATA:
						DemuxState = KMF2UMF_EVTYPE_DTV_DEMUX_HAS_NO_DATA;
						//msg.msg_type = FW_MONITOR_STREAM_NO_DATA;
						//AI_PSI_Monitor_ReplaceProcess(AL_PSIMONITOR_STATE_NOT_RUNNING_REPLACEMENT_EXIST);
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_AV_INVALID:
					case KMF2UMF_EVTYPE_DTV_DEMUX_AUDIO_INVALID:
						DemuxState = Type;
						break;
					case KMF2UMF_EVTYPE_DTV_AUDIO_STARTED:
						msg.msg_type = FM_DTV_AUDIO_STARTED;
						break;
					case KMF2UMF_EVTYPE_DTV_AUDIO_SAMPLE_STOPPED:
						msg.msg_type = FM_DTV_AUDIO_SAMPLE_STOPPED;
						break;
					case KMF2UMF_EVTYPE_DTV_AUDIO_STOPPED:
						msg.msg_type = FM_DTV_AUDIO_STOPPED;
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_EWS_ACTIVE:
						msg.msg_type = FW_EWS_ACTIVE;
						break;
					case KMF2UMF_EVTYPE_DTV_DEMUX_EWS_INACTIVE:
						msg.msg_type = FW_EWS_INACTIVE;
						break;
					default:
						break;
				}
			}
			break;
#endif
		case KMF2UMF_EVID_DRV:
			if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
				|| fw_data_struct.fw_mode == FW_CHAN_SRCH_MODE
				|| fw_data_struct.fw_mode == FW_EPG_MODE
				|| fw_data_struct.fw_mode == FW_RS232_MODE
				)
			{
				switch (Type)
				{
					case KMF2UMF_EVTYPE_DRV_INFORM_SIGNALSTATUS:
						MID_TVFE_GetDRVInputStatus(Info, &eStatus);
						if (eStatus == MID_TVFE_INPUTSTATUS_NOSIGNAL)
						{
							FW_DEBUG_MESSAGE("[%s] INFORM_NOSIGNAL\n", __FUNCTION__);
							msg.msg_type = FW_NO_SIGNAL;
							#ifdef CONFIG_SMART_MODULE_SUPPORT
							if(APP_SOURCE_HDMI == eSourType)
								APP_Smart_Set_Respond(FALSE);
							#endif
						}
						else if (eStatus == MID_TVFE_INPUTSTATUS_SIGNALOK)
						{
							FW_DEBUG_MESSAGE("[%s] UNMUTE_HAS_READY\n", __FUNCTION__);
							msg.msg_type = FW_SIGNAL_PRESENT;
							#ifdef CONFIG_SMART_MODULE_SUPPORT
							if(APP_SOURCE_HDMI == eSourType)
								APP_Smart_Set_Respond(TRUE);
							#endif
						}
						else if (eStatus == MID_TVFE_INPUTSTATUS_NOTSUPPORT)
						{
							FW_DEBUG_MESSAGE("[%s] INFORM_NOTSUPPORT\n", __FUNCTION__);
							msg.msg_type = FW_NOT_SUPPORT;
							#ifdef CONFIG_SMART_MODULE_SUPPORT
							if(APP_SOURCE_HDMI == eSourType)
								APP_Smart_Set_Respond(FALSE);
							#endif
						}
						else if (eStatus == MID_TVFE_INPUTSTATUS_SIGNALWEAK)
						{
							FW_DEBUG_MESSAGE("[%s] INFORM_SIGNALWEAK\n", __FUNCTION__);
							msg.msg_type = FW_SIGNAL_IN_PROGRESS;
							#ifdef CONFIG_SMART_MODULE_SUPPORT
							if(APP_SOURCE_HDMI == eSourType)
								APP_Smart_Set_Respond(FALSE);
							#endif
						}
						break;
					case KMF2UMF_EVTYPE_DRV_INFORM_VIP_FINISH:
						FW_DEBUG_MESSAGE("[%s] INFORM_VIP_FINISH\n", __FUNCTION__);
						MID_TVFE_UpdateCurrentVideoInfo();
						msg.msg_type = FW_VIP_FINISH;
						break;
					case KMF2UMF_EVTYPE_DRV_INFORM_VIP_STATUS:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_DRV_INFORM_VIP_STATUS\n", __FUNCTION__);
						MID_TVFE_UpdateCurrentVideoInfo();
						msg.msg_type = FW_VIP_STATUS;
					  	break;
					case KMF2UMF_EVTYPE_DRV_INFORM_HDMI_CHANGESRC:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_DRV_INFORM_HDMI_CHANGESRC\n", __FUNCTION__);
						unsigned char value = 0xff;
						if (Info != al_null)
						{
							value = *Info;
						}
						msg.msg_type = FW_HDMI_CHANGE_SOURCE;
						msg.u32Userparam = value;
						FW_DEBUG_MESSAGE("msg.u32Userparam=%d\n",msg.u32Userparam);
						break;
					default:
						break;
				}
			}
			break;

		case KMF2UMF_EVID_AUDIO:
			{
				unsigned char value = 0xff;
				if (Info != al_null)
				{
					value = *Info;
				}
				switch (Type)
				{
					case KMF2UMF_EVTYPE_AUDIO_HEADPHONE:
						if (value == 1)
						{
							msg.msg_type = FW_AUDIO_HEADPHONE_PLUG_IN;
						}
						else if (value == 0)
						{
							msg.msg_type = FW_AUDIO_HEADPHONE_PLUG_OUT;
						}
						break;

					case KMF2UMF_EVTYPE_SIF_SAS_OK:
						msg.msg_type = FW_AUDIO_SIF_SAS_OK;
						break;

					case KMF2UMF_EVTYPE_SIF_SCS_OK:
						msg.msg_type = FW_AUDIO_SIF_SCS_OK;
						break;

#ifdef CONFIG_SUPPORT_AUDIO_CODEC_OVERLAY
					case KMF2UMF_EVTYPE_AUDIO_LOADCODEC_OK:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_AUDIO_LOADCODEC_OK\n", __FUNCTION__);
						msg.msg_type = FW_AUDIO_LOADCODEC_OK;
						break;

					case KMF2UMF_EVTYPE_AUDIO_LOADCODEC_ERROR:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_AUDIO_LOADCODEC_ERROR\n", __FUNCTION__);
						msg.msg_type = FW_AUDIO_LOADCODEC_ERROR;
						break;

					case KMF2UMF_EVTYPE_AUDIO_LOADCODEC_TYPE:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_AUDIO_LOADCODEC_TYPE  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_AUDIO_LOADCODEC_TYPE;
						msg.u32Userparam = value;
						break;
#endif
					default:
						break;
				}
			}
			break;

		case KMF2UMF_EVID_ADC:
			{
				switch (Type)
				{
					case KMF2UMF_EVTYPE_ADC_TURNOFFSOUND:
						msg.msg_type = FW_AUDIO_ADC_TURNOFFSOUND;
						break;
					case KMF2UMF_EVTYPE_ADC_VGAPLUGIN:
						{
							unsigned char value = 0xff;
							if (Info != al_null)
							{
								value = *Info;
							}
							msg.msg_type = FW_ADC_AUTOCHANGE_SOURCE;
							msg.u32Userparam = value;
							printf("msg.u32Userparam=%d\n",msg.u32Userparam);
						}
						break;
					case KMF2UMF_EVTYPE_ADC_AUTOADJUST:
						{
							unsigned char value = 0xff;
							if (Info != al_null)
							{
								value = *Info;
							}
							msg.msg_type = FW_ADC_AUTOADJUST;
							msg.u32Userparam = value;
							printf("msg.u32Userparam=%d\n",msg.u32Userparam);
						}
						break;
					default:
						break;
				}
			}
			break;
#ifdef CONFIG_SCART_SUPPORT
		case KMF2UMF_EVID_SCART:
			if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
				|| fw_data_struct.fw_mode == FW_CHAN_SRCH_MODE
				|| fw_data_struct.fw_mode == FW_EPG_MODE
				|| fw_data_struct.fw_mode == FW_RS232_MODE
				)
			{
				switch (Type)
				{
					case KMF2UMF_EVTYPE_SCART_INPUTTYPE:
						msg.msg_type = FW_SCART_INPUTSRC_CHANGE;
						msg.u32Userparam = *Info;
						break;
					case KMF2UMF_EVTYPE_SCART_PIN8:
						msg.msg_type = FW_SCART_INPUTSTATUS_CHANGE;
						msg.u32Userparam = *Info;
						break;
					default:
						break;
				}
			}
			break;
#endif
		case KMF2UMF_EVID_HDMI:
			if (fw_data_struct.fw_mode == FW_NORMAL_VIEW_MODE
				|| fw_data_struct.fw_mode == FW_RS232_MODE
                )
			{
				FW_DEBUG_MESSAGE("aaaaaaaaaaaaaaaaaType===========%d\n",Type);
				switch (Type)
				{
					case KMF2UMF_EVTYPE_HDMI_HDMIMODE:
						MID_TVFE_UpdateCurrentVideoInfo();
						msg.msg_type = FW_HDMI_HDMIDVI_CHANGE;
						msg.u32Userparam = *Info;
						break;
					case KMF2UMF_EVTYPE_HDMI_HDMIAUDIOACP:
						msg.msg_type = FW_HDMI_ACPTYPE_CHANGE;
						msg.u32Userparam = *Info;
						break;
					case KMF2UMF_EVTYPE_HDMI_PORTSTATUS:
						msg.u32Userparam = *(unsigned int *)Info;
					#ifdef SUPPORT_CEC_TV
						CECTV_HDMIPortStatus(msg.u32Userparam);
					#else
						msg.msg_type = FW_HDMI_PORTSTATUS;	
					#endif
						break;
					case KMF2UMF_EVTYPE_HDMI_PROCESSING:
						msg.msg_type = FW_HDMI_PROCESSING;
						msg.u32Userparam = *(unsigned int *)Info;
						break;
					case KMF2UMF_EVTYPE_HDMI_MHL_RCP_CMD:
						FW_DEBUG_MESSAGE("[%s:%d] KMF2UMF_EVTYPE_HDMI_MHL_RCP_CMD !!!!!!!!!!!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
						msg.msg_type = FW_HDMI_MHL_RCP_CMD;
						msg.u32Userparam = *(unsigned int *)Info;
						break;

					case KMF2UMF_EVTYPE_HDMI_MHL_CONNECTED:
						FW_DEBUG_MESSAGE("[%s:%d] KMF2UMF_EVTYPE_HDMI_MHL_RCP_CMD !!!!!!!!!!!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
						msg.msg_type = FW_HDMI_MHL_CONNECTED;
						msg.u32Userparam = *(unsigned int *)Info;
						break;

#ifdef CONFIG_HDMI_SPD
					case KMF2UMF_EVTYPE_HDMI_SPD_INFOFRAME_UPDATE:
						FW_DEBUG_MESSAGE("[%s:%d] KMF2UMF_EVTYPE_HDMI_SPD_INFOFRAME_UPDATE !!!!!!!!!!!!!!!!!!!!!\n",__FUNCTION__,__LINE__);

						struct hdmi_spd_infoframe spd_info;
						spd_info =*(struct hdmi_spd_infoframe *)Info;


						FW_DEBUG_MESSAGE("Device vendor %s, Default vender %s\n",spd_info.vendor,CONFIG_HDMI_SPD_Vender);
						FW_DEBUG_MESSAGE("Device product %s, Default product %s\n",spd_info.product,CONFIG_HDMI_SPD_Product);
						if(strncmp(spd_info.vendor,CONFIG_HDMI_SPD_Vender,strlen(CONFIG_HDMI_SPD_Vender)) || strncmp(spd_info.product,CONFIG_HDMI_SPD_Product,strlen(CONFIG_HDMI_SPD_Product)))
						{
							msg.msg_type = FW_HDMI_SPD_INFOFRAME_UPDATE;
							msg.u32Userparam = 0;
						}
						else
						{
							msg.msg_type = FW_HDMI_SPD_INFOFRAME_UPDATE;
							msg.u32Userparam = 1;
						}
						break;
#endif
					default:
						break;
				}
			}
			break;

        case KMF2UMF_EVID_USB:

                if(Type == KMF2UMF_EVTYPE_USB_PLUGIN)
                {
                    USBDrvInfo_t drvInfo = *(USBDrvInfo_t *)Info;
                    msg.msg_type = FW_USB_PLUGIN;
                    if(drvInfo.usbType & 0x4) //0x4:USB_MASS_STORAGE_FLAG
                        msg.u32Userparam = 1; //storage usb
                    else
                        msg.u32Userparam = 0; //wifi dongle
                    save_usb_drvInfo((void *)Info);
                }
                else if(Type == KMF2UMF_EVTYPE_USB_PLUGOUT)
                {
                    msg.msg_type = FW_USB_PLUGOUT;
                }
				 else if(Type == KMF2UMF_EVTYPE_USB_HUNG_TASK)
                {
                    msg.msg_type = FW_USB_HUNGTASK;
                }
            break;

        case KMF2UMF_EVID_PARTITION:

                if(Type == KMF2UMF_EVTYPE_PART_PLUGIN)
                {
                    printf("[CHWUU]UMF:IN %s\n",(char*)Info);
                }
                else if(Type == KMF2UMF_EVTYPE_PART_PLUGOUT)
                {
                    printf("[CHWUU]UMF:OUT %s\n",(char*)Info);
                }
            break;

		case KMF2UMF_EVID_DEBUG:
			switch(Type)
			{
				case KMF2UMF_EVTYPE_DEBUG_HELP:
					printf("\n ========================= RS HELP ================================= \n\n");
					printf(" Format:   echo rs [command] > /dev/kmf \n");
					printf(" command:  (1) usb [value] \n");
					printf(" (1) usb [value]  \n");
					printf("     [value]: [3] means to save both UMF debug message and console debug message to usb flash in RSdbg.txt \n");
					printf("     [value]: [2] means to save UMF debug message to usb flash in RSdbg.txt \n");
					printf("     [value]: [1] means to save console debug message to usb flash in RSdbg.txt \n");
					printf("     [value]: [0] means to disable this function \n");
					printf("     Example. echo rs usb 1 > /dev/kmf \n");
					break;

				case KMF2UMF_EVTYPE_DEBUG_USB:
					printf(" RS USB On :%d \n", *Info);
					con_SetDbgMsgtoUSB(*Info);
					break;

				default:
					printf(" Input CMD error !!! , evnetType:%d \n", *Info);
					printf(" Please use rs help to see all commands \n");
					printf(" Example. echo rs help >/dev/kmf \n");
					break;
			}
			break;

		case KMF2UMF_EVID_MCCS:
			{
				unsigned char value = 0xff;
				if (Info != al_null)
				{
					value = *Info;
				}
				switch (Type)
				{
					case KMF2UMF_EVTYPE_MCCS_IMAGE_LUMINANCE:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_IMAGE_LUMINANCE  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_LUMINANCE;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_IMAGE_CONTRAST:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_IMAGE_CONTRAST  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_CONTRAST;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_IMAGE_SHARPNESS:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_IMAGE_SHARPNESS  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_SHARPNESS;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_IMAGE_SATURATION:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_IMAGE_SATURATION  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_SATURATION;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_RGAIN:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_RGAIN  value: %d\n", __FUNCTION__,*(unsigned short *)Info);
						msg.msg_type = FW_MCCS_IMAGE_RGAIN;
						msg.u32Userparam = *(unsigned short *)Info;
						break;
					case KMF2UMF_EVTYPE_MCCS_GGAIN:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_GGAIN  value: %d\n", __FUNCTION__,*(unsigned short *)Info);
						msg.msg_type = FW_MCCS_IMAGE_GGAIN;
						msg.u32Userparam = *(unsigned short *)Info;
						break;
					case KMF2UMF_EVTYPE_MCCS_BGAIN:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_BGAIN  value: %d\n", __FUNCTION__,*(unsigned short *)Info);
						msg.msg_type = FW_MCCS_IMAGE_BGAIN;
						msg.u32Userparam = *(unsigned short *)Info;
						break;
					case KMF2UMF_EVTYPE_MCCS_ROFFSET:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_ROFFSET  value: %d\n", __FUNCTION__,*(unsigned short *)Info);
						msg.msg_type = FW_MCCS_IMAGE_ROFFSET;
						msg.u32Userparam = *(unsigned short *)Info;
						break;
					case KMF2UMF_EVTYPE_MCCS_GOFFSET:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_GOFFSET  value: %d\n", __FUNCTION__,*(unsigned short *)Info);
						msg.msg_type = FW_MCCS_IMAGE_GOFFSET;
						msg.u32Userparam = *(unsigned short *)Info;
						break;
					case KMF2UMF_EVTYPE_MCCS_BOFFSET:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_BOFFSET  value: %d\n", __FUNCTION__,*(unsigned short *)Info);
						msg.msg_type = FW_MCCS_IMAGE_BOFFSET;
						msg.u32Userparam = *(unsigned short *)Info;
						break;
					case KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_R:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_R  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_COLORLUT_SATGAIN_R;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_Y:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_Y  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_COLORLUT_SATGAIN_Y;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_G:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_G  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_COLORLUT_SATGAIN_G;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_C:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_C  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_COLORLUT_SATGAIN_C;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_B:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_B  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_COLORLUT_SATGAIN_B;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_M:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_COLORLUT_SATGAIN_M  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_COLORLUT_SATGAIN_M;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_CLOSE_USERMENU:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_CLOSE_USERMENU  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_CLOSE_USERMENU;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_GAMMA_TABLE_INDEX:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_GAMMA_TABLE_INDEX  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_GAMMA_TABLE_INDEX;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_CONTRAST_AUTO:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_CONTRAST_AUTO  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_IMAGE_CONTRAST_AUTO;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_EEPROM_RW:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_EEPROM_RW  value: %d\n", __FUNCTION__,value);
						eeprom_ctl *ctl_data = (eeprom_ctl *) Info;
						AL_FW_RW_PseudoEEPROM((UINT8 *)ctl_data);
						break;
					case KMF2UMF_EVTYPE_MCCS_FLASH_CTRL:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_FLASH_CTRL  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_FLASH_CTRL;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_OSDHPOSITION:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_OSDHPOSITION  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_OSD_HPOSITION;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_OSDVPOSITION:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_OSDVPOSITION  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_OSD_VPOSITION;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_AUDIOVOLUME:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_AUDIOVOLUME  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_AUDIOVOLUME;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_OSDLANGUAGE:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_OSDLANGUAGE  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_OSD_LANGUAGE;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_FBOSDLANGUAGE:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_FBOSDLANGUAGE\n", __FUNCTION__);
						msg.msg_type = FW_MCCS_FBOSD_LANGUAGE;
						break;
					case KMF2UMF_EVTYPE_MCCS_SET_VGASETTING:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_SET_VGASETTING  value: %d\n", __FUNCTION__,value);
						vga_setting vga_setup_data = *(vga_setting *)Info;
						value = vga_setup_data.value;
						switch(vga_setup_data.index)
						{
							case VGA_SETUP_AUTO:
								msg.msg_type = FW_MCCS_SET_VGASETTING_AUTO;
								break;
							case VGA_SETUP_H_POSITION:
								msg.msg_type = FW_MCCS_SET_VGASETTING_HPOSITION;
								msg.u32Userparam = value;
								break;
							case VGA_SETUP_V_POSITION:
								msg.msg_type = FW_MCCS_SET_VGASETTING_VPOSITION;
								msg.u32Userparam = value;
								break;
							case VGA_SETUP_PHASE:
								msg.msg_type = FW_MCCS_SET_VGASETTING_PHASE;
								msg.u32Userparam = value;
								break;
							case VGA_SETUP_CLOCK:
								msg.msg_type = FW_MCCS_SET_VGASETTING_CLOCK;
								msg.u32Userparam = value;
								break;
						}
						break;	
					case KMF2UMF_EVTYPE_MCCS_POWER_SETMODE:
						FW_DEBUG_MESSAGE("[%s] FW_MCCS_POWER_SETMODE  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_POWER_SETMODE;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_AUDIOMUTE:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_AUDIOMUTE  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_AUDIOMUTE;
						msg.u32Userparam = value;
						break;
					case KMF2UMF_EVTYPE_MCCS_SET_INPUTSOURCE:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_SET_INPUTSOURCE  value: %d\n", __FUNCTION__,value);
						if(value == INPUT_TYPE_PC)
							eSourType = APP_SOURCE_PC;
						else
							eSourType = APP_SOURCE_HDMI;
						APP_GUIOBJ_Source_SetAppSource(eSourType);
						break;
					case KMF2UMF_EVTYPE_MCCS_OSDLOCK:
						FW_DEBUG_MESSAGE("[%s] KMF2UMF_EVTYPE_MCCS_AUDIOMUTE  value: %d\n", __FUNCTION__,value);
						msg.msg_type = FW_MCCS_OSDLOCK;
						msg.u32Userparam = value;
						break;	
					default:
						break;
				}
			}
			break;

		default:
			break;
	}

	if (msg.msg_type != FW_INVALID_EVENT )
	{
		/* Send msg to frmwrk task */
		if (GL_SUCCESS != GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t)))
		{
			FW_ERROR_MESSAGE( "send msg error!!!!!!!!!!!!!!\n");
		}
	}
}
#ifdef CONFIG_VCHIP_SUPPORT
static al_int32  FW_PC_callback(AI_PC_Message_Type_t EventType, al_uint32 u32Param)
{
    return 0;
}
#endif

void FW_Console_callback(al_uint8 eventID, al_uint8 data1, al_uint8 data2)
{
	FW_msg_t msg;
	msg.msg_type = FW_INVALID_EVENT;

	switch(eventID)
	{
#ifdef CONFIG_QSD
		case FW_QSD_ATV_SetCH:
			msg.msg_type = FW_QSD_ATV_SetCH;
			msg.u32Userparam = data1;
			break;

		case FW_QSD_DTV_SetCH:
			msg.msg_type = FW_QSD_DTV_SetCH;
			msg.u32Userparam = data1;
			break;

		case FW_QSD_Media_Play:
			msg.msg_type = FW_QSD_Media_Play;
			msg.u32Userparam = data1;
			break;

		case FW_QSD_Source_Set:
			msg.msg_type = FW_QSD_Source_Set;
			msg.u32Userparam = data1;
			break;

		case FW_QSD_DTV_SetFreq:
			msg.msg_type = FW_QSD_DTV_SetFreq;
			msg.u32Userparam = data1;
			break;
#endif
#ifdef SUPPORT_FACTORY_AUTO_TEST_MODE4
		case FW_FAC_TEST_SET_SOURCE:
			msg.msg_type = FW_FAC_TEST_SET_SOURCE;
			msg.u32Userparam = data1;
			break;
		case FW_AUTOTEST_RESET:
			msg.msg_type = FW_AUTOTEST_RESET;
			msg.u32Userparam = data1;
			break;

		case FW_AUTOTEST_UPDATESW:
			msg.msg_type = FW_AUTOTEST_UPDATESW;
			msg.u32Userparam = data1;
			break;
		case FW_FAC_UPDATE_HDCP_KEY:
			msg.msg_type = FW_FAC_UPDATE_HDCP_KEY;
			msg.u32Userparam = data1;
			break;
#endif
		default:
			break;
	}
	if (msg.msg_type != FW_INVALID_EVENT )
	{
		/* Send msg to frmwrk task */
		GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}
}

#ifdef CONFIG_CTV_UART_FAC_MODE
void FW_Console_Rs232Callback(al_uint8 eventID, al_uint32 addr)
{
	FW_msg_t msg;
	msg.msg_type = FW_INVALID_EVENT;
    //con_echo_sfu(12, (UINT8*)"-----01-----");
	switch(eventID)
	{
        case CTV_RS232_CMD_SOURCE_CONTROL:
            {
                msg.msg_type = FW_RS232_SET_SOURCE;
                msg.u32Userparam = addr;
            }
            break;
            
        case CTV_RS232_CMD_AUDIO_CONTROL_VOL:
            {
                msg.msg_type = FW_RS232_SET_VOL;
                msg.u32Userparam = addr;
            }
            break;
        
		default:
			break;
	}
	if (msg.msg_type != FW_INVALID_EVENT )
	{
		/* Send msg to frmwrk task */
        FW_event_handler(&msg);
		//GL_QueueSend(fw_data_struct.fw_qid, &msg, sizeof(FW_msg_t));
	}
}
#endif
/**
* @brief framwork init.
*
* This application framework init function will initialize all the middleware components and framework components.
*
* @param frmwrk_init_module[input] frmwrk mudule Initialize select.
*
* @param frmwrk_app_cb[input] App framework callback.
*
* @param frmwrk_context_cb[input] App register to framwork callback for al layer get context.
*
* @return AL_SUCCESS if initialization is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_Init(AFW_Init_t frmwrk_init_module,
	frmwrk_app_cb_t frmwrk_app_cb, frmwrk_pfOpConext_t frmwrk_context_cb)
{
	/* Init sub AFW component here */
	/* Register callback function to AFW */
	if (!afw_running)
	{
		memset(&fw_data_struct, 0, sizeof(FW_data_struct_t));
		fw_data_struct.fw_app_cb = frmwrk_app_cb;

		if ((frmwrk_init_module & FW_MAIN_INIT) && (frmwrk_context_cb == NULL))
		{
			FW_ERROR_MESSAGE("AFW Init Fail!\n");
			return AL_FAILURE;
		}
		else
		{
			fw_data_struct.fw_context_cb = frmwrk_context_cb;
		}
	}

	/* Main module Initialize */
	if (frmwrk_init_module & FW_MAIN_INIT)
	{
#ifdef CONFIG_SUPPORT_NOSIGNAL_AUTO_CHANGE_SOURCE
		u32HDMIActive = 0;
#endif
		osd_init();
		/* regist KMF Signal dispose callback function */
		even_RegConsoleCallBackFunc((void *)CONSOLE_INIT, con_mmiotest_status);
#ifdef CONFIG_QSD
		event_Console(SIG_RS232ON, NULL, NULL);
#endif
#if defined(CONFIG_QSD)|| defined(SUPPORT_FACTORY_AUTO_TEST_MODE4)
		con_RegCallBackFunc(FW_Console_callback);
#endif
        BurnMode_RegCallBackFunc(FW_TVFE_BURNMODE_callback);

#ifdef CONFIG_CTV_UART_FAC_MODE
		con_RegAccuviewRs232CallBackFunc(FW_Console_Rs232Callback);
#endif
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
		AL_DB_Init_t  InitParam;
        memset(&InitParam, 0, sizeof(AL_DB_Init_t));
		fw_data_struct.fw_context_cb(FW_CONTEXT_TYPE_GET_DBNVM_HANDER
			, FW_CONTEXT_OP_TYPE_READ, &InitParam, 0);
		if (AL_DB_Init(&InitParam) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AL_DB_Init  Failed.  \n");
			ASSERT(1, "AL_DB_Init  Failed");
		}
		else
		{
			al_uint32 MAINAPP_ResetDefVal = 0;
			fw_data_struct.fw_context_cb(FW_CONTEXT_TYPE_GET_DBRESTORE_SETTER
				, FW_CONTEXT_OP_TYPE_READ, &MAINAPP_ResetDefVal, 0);
			AL_DB_RegisterResetDefValFunc((AL_DB_ResetDefValFunc *)MAINAPP_ResetDefVal);
		}
#endif
		/* Initialize setting component */
		if (AI_Setting_Init() != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_Setting_Init  Failed.  \n");
			ASSERT(1, "AI_Setting_Init  Failed");
		}

		/* Initialize time component */
		if (AI_Time_Init(FW_Time_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_Time_Init  Failed \n");
			ASSERT(1, "AI_Time_Init  Failed");
		}
#if defined (CONFIG_DTV_SUPPORT) || defined (CONFIG_ATV_SUPPORT)
		/* Initialize datasaving component */
		if (AI_DataSaving_Init(FW_DataSaving_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_DataSaving_Init  Failed \n");
			ASSERT(1, "AI_DataSaving_Init  Failed");
		}
#endif
#ifdef CONFIG_VCHIP_SUPPORT
        /*Initialize parent control*/
        if (AI_PC_Init(FW_PC_callback) != AL_SUCCESS)
        {
            FW_ERROR_MESSAGE( "AI_PC_Init  Failed \n");
            ASSERT(1, "AI_PC_Init  Failed");
        }
#endif
#ifdef CONFIG_CC_SUPPORT
		if(AI_CC_Init(&FW_CC_CallBack) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE("AL_Closedcaption_Init  Failed \n");
			ASSERT(1, "AL_Closedcaption_Init  Failed");
		}
#endif
#ifdef CONFIG_SUPPORT_TTX
		/**< Initialize teletext component */
		if (AI_TTX_Init(FW_TTX_CallBack) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE("AL_Teletext_Init  Failed \n");
			ASSERT(1, "AL_Teletext_Init  Failed");
		}
#endif
#ifdef CONFIG_SUPPORT_SUBTITLE
		/**< Initialize subtitle component */
		if (AI_Subtitle_Init() != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE("AL_Subtitle_Init  Failed \n");
			ASSERT(1, "AL_Subtitle_Init  Failed");
		}
#endif
#ifdef CONFIG_SUPPORT_ISDB_SUBTITLE
		/**< Initialize isdb subtitle component */
		if (AI_SBSUB_Init() != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE("AI_SBSUB_Init  Failed \n");
			ASSERT(1, "AI_SBSUB_Init  Failed");
		}
#endif
	}

#ifdef CONFIG_DTV_SUPPORT	/* DVB related module Initialize */
	if (frmwrk_init_module & (FW_DVB_T_INIT |FW_DVB_C_INIT |FW_DVB_S_INIT
#ifdef CONFIG_ISDB_SYSTEM
		|FW_DVB_SBTVD_INIT
#endif
		))
	{
		AL_DB_EDBType_t DBType = AL_DBTYPE_MAX;
#if defined(CONFIG_DVB_SYSTEM_DVBT_SUPPORT)||defined(CONFIG_DVB_SYSTEM_DVBC_SUPPORT)||defined(CONFIG_DVB_SYSTEM_DVBS_SUPPORT)||defined(CONFIG_ISDB_SYSTEM)
		EDBEvent_t event_type;
		EDBEvent_t service_event_type;
#endif

		if (frmwrk_init_module & FW_DVB_T_INIT)
		{
			DBType = AL_DBTYPE_DVB_T;
		}
		else if (frmwrk_init_module & FW_DVB_C_INIT)
		{
			DBType = AL_DBTYPE_DVB_C;
		}
		else if (frmwrk_init_module & FW_DVB_S_INIT)
		{
			DBType = AL_DBTYPE_DVB_S;
		}
#ifdef CONFIG_ISDB_SYSTEM
		else if (frmwrk_init_module & FW_DVB_SBTVD_INIT)
		{
			DBType = AL_DBTYPE_DVB_SBTVD;
		}
#endif
#if defined(CONFIG_DVB_SYSTEM_DVBT_SUPPORT)||defined(CONFIG_DVB_SYSTEM_DVBC_SUPPORT)||defined(CONFIG_DVB_SYSTEM_DVBS_SUPPORT)||defined(CONFIG_ISDB_SYSTEM)
		event_type = DB_SERVICE_INFO | DB_ENTRY_MODIFIED | DB_ENTRY_REMOVED | DB_FULLDB_RESTORED |DB_FULLDB_INFO;
		service_event_type = DB_SERVICE_INFO | DB_ENTRY_ADDED | DB_ENTRY_MODIFIED | DB_ENTRY_REMOVED | DB_ENTRY_DELETED | DB_FULLDB_RESTORED | DB_FULLDB_INFO;
#endif

#ifdef CONFIG_DVB_SYSTEM_DVBT_SUPPORT
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_T, event_type, &FW_cdbi_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_T, service_event_type, &FW_cdbi_service_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
#endif
#ifdef CONFIG_DVB_SYSTEM_DVBC_SUPPORT
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_C, event_type, &FW_cdbi_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_C, service_event_type, &FW_cdbi_service_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
#endif
#ifdef CONFIG_DVB_SYSTEM_DVBS_SUPPORT
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_S, event_type, &FW_cdbi_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_S, service_event_type, &FW_cdbi_service_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
#endif
#ifdef CONFIG_ISDB_SYSTEM
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_SBTVD, event_type, &FW_cdbi_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
		if (CDBIRegisterDBWatcher(AL_DBTYPE_DVB_SBTVD, service_event_type, &FW_cdbi_service_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}
#endif

		AL_FW_SwitchDBModule(DBType);

#if defined(CONFIG_DVB_SYSTEM) ||  defined(CONFIG_DTMB_SYSTEM)
		#ifdef SUPPORT_DVBS_LCN
		if (DBType == AL_DBTYPE_DVB_C || DBType == AL_DBTYPE_DVB_T || DBType == AL_DBTYPE_DVB_S)
		#else
		if (DBType == AL_DBTYPE_DVB_C || DBType == AL_DBTYPE_DVB_T)
		#endif
		{
			CDBIActiveLCNProc4DB(DBType, TRUE);
		}
		else
		{
			CDBIActiveLCNProc4DB(DBType, FALSE);
		}
#endif
		AI_Event_Init_t stEPGInit =
		{
#ifdef CONFIG_USE_COMPRESSED_EPGMODULE
			.TotalSize = TOTAL_SIZE,
#else
			.TotalChannel = AL_EVENT_MAX_CHANNEL,
			.TotalEvent = AL_EVENT_MAX_EVENT,
			.TotalExtendEvent = AL_EVENT_MAX_EXTEND_EVENT,
#endif
			.fpcallback = FW_event_callback
		};

		if (AI_Event_Init(DBType, &stEPGInit) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AL_Event_init  Failed \n");
			ASSERT(1, "AL_Event_init  Failed");
		}

		/*Initialize dvb monitor*/
		if (AI_DVB_Monitor_Init(FW_DvbMon_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_DVB_Monitor_Init  Failed \n");
			ASSERT(1, "AI_DVB_Monitor_Init  Failed");
		}

		/*Initialize psi monitor*/
		if (AI_PSI_Monitor_Init(FW_PsiMon_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_PSI_Monitor_Init  Failed \n");
			ASSERT(1, "AI_PSI_Monitor_Init  Failed");
		}

		/*Initialize ews monitor*/
#ifdef CONFIG_SUPPORT_EWS
		if (AI_EWS_Monitor_Init(FW_EwsMon_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_EWS_Monitor_Init  Failed \n");
			ASSERT(1, "AI_EWS_Monitor_Init  Failed");
		}
#endif

#ifdef SUPPORT_ASTRA_LCN_MONITOR
		if (AI_AstraLcn_Monitor_Init(FW_AstraLcnMon_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_AstraLcn_Monitor_Init  Failed \n");
			ASSERT(1, "AI_AstraLcn_Monitor_Init  Failed");
		}
#endif

		/* Initialize the Scheduler component */
		/*< we must init schedule moulder befor program reminder moulder */
		if (AL_SCH_Init(FW_scheduler_callback) != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AL_SCH_Init	Failed \n");
			ASSERT(1, "AL_SCH_Init	Failed");
		}

		/* Initialize the Program reminder component */
		/*we must set init param for prog reminder by AL_PR_SetInitParam() in application layer*/
		if (AL_PR_Init() != AL_SUCCESS)
		{
			FW_ERROR_MESSAGE( "AI_PR_Init  Failed \n");
			ASSERT(1, "AI_PR_Init  Failed");
		}
	}
#endif
#ifdef CONFIG_ATV_SUPPORT
	/* ATV related module initialize */
	if (frmwrk_init_module & FW_ATV_INIT)
	{

		EDBEvent_t event_type;

		CDBIActiveDBModule(CDBI_DBTYPE_DVB_ATV, TRUE);

		event_type = DB_SERVICE_INFO | DB_ENTRY_MODIFIED | DB_ENTRY_REMOVED;
		if (CDBIRegisterDBWatcher(CDBI_DBTYPE_DVB_ATV, event_type, &FW_cdbi_callback) != DB_SUCCESS)
		{
			ASSERT(1,"CDBIRegisterDBWatcher() failed");
		}

#ifdef CONFIG_SUPPORT_SW_AFC
		AFT_RegCallBackFunc(FW_AFT_callback);
#endif
	}
#endif
	/* FilePlayer related module initialize */
	if (frmwrk_init_module & FW_FILEPLAYER_INIT)
	{

	}

	/* Network related module initialize */
	if (frmwrk_init_module & FW_NETWORK_INIT)
	{

	}

	/* Framework control module initialize */
	if (afw_running)
	{
		return AL_SUCCESS;
	}

	if (AL_SUCCESS != FW_InitResources())
	{
		FW_ERROR_MESSAGE("FW_InitResources Failed \n");
		ASSERT(1, "FW_InitResources Failed");
		return AL_FAILURE;
	}
	event_RegCallBackFunc(FW_KMFtoUMF_callback, NULL);

	fw_data_struct.fw_mode = FW_NORMAL_VIEW_MODE;
	afw_running = al_true;

	return AL_SUCCESS;
}

/**
* @brief framwork exit.
*
* This application framework exit function will deinitialize all the middleware components and framework components.
*
* @return AL_SUCCESS if exit is successful, AL_FAILURE otherwise.
*/
#ifdef CONFIG_MOD_PACK_DTV
AL_Return_t AL_FW_DTVExit(al_void)
{
	AI_PSI_Monitor_Uninit();
	AI_DVB_Monitor_Uninit();
	AI_Event_Term(AL_DBTYPE_DVB_T);
#ifdef CONFIG_SUPPORT_OTA_UPGRADE
	extern MID_Status_t MID_OtaMonUnInit(void);
	MID_OtaMonUnInit();
#endif
#ifdef SUPPORT_ASTRA_LCN_MONITOR
	AI_AstraLcn_Monitor_Uninit();
#endif
	MID_SIUTIL_Uninit();
	return AL_SUCCESS;
}
#endif

#ifdef CONFIG_MOD_PACK_DTV
AL_Return_t AL_FW_InitDtvModule(AFW_Init_t frmwrk_init_module)
{
#ifdef CONFIG_DTV_SUPPORT	/* DVB related module Initialize */
		if (frmwrk_init_module & (FW_DVB_T_INIT |FW_DVB_C_INIT |FW_DVB_S_INIT
#ifdef CONFIG_ISDB_SYSTEM
			|FW_DVB_SBTVD_INIT
#endif
			))
		{
			AL_DB_EDBType_t DBType = AL_DBTYPE_MAX;
	
			if (frmwrk_init_module & FW_DVB_T_INIT)
			{
				DBType = AL_DBTYPE_DVB_T;
			}
			else if (frmwrk_init_module & FW_DVB_C_INIT)
			{
				DBType = AL_DBTYPE_DVB_C;
			}
			else if (frmwrk_init_module & FW_DVB_S_INIT)
			{
				DBType = AL_DBTYPE_DVB_S;
			}
#ifdef CONFIG_ISDB_SYSTEM
			else if (frmwrk_init_module & FW_DVB_SBTVD_INIT)
			{
				DBType = AL_DBTYPE_DVB_SBTVD;
			}
#endif
	
			AI_Event_Init_t stEPGInit =
			{
#ifdef CONFIG_USE_COMPRESSED_EPGMODULE
				.TotalSize = TOTAL_SIZE,
#else
				.TotalChannel = AL_EVENT_MAX_CHANNEL,
				.TotalEvent = AL_EVENT_MAX_EVENT,
				.TotalExtendEvent = AL_EVENT_MAX_EXTEND_EVENT,
#endif
				.fpcallback = FW_event_callback
			};
	
			if (AI_Event_Init(DBType, &stEPGInit) != AL_SUCCESS)
			{
				FW_ERROR_MESSAGE( "AL_Event_init  Failed \n");
				ASSERT(1, "AL_Event_init  Failed");
			}
	
			/*Initialize dvb monitor*/
			if (AI_DVB_Monitor_Init(FW_DvbMon_callback) != AL_SUCCESS)
			{
				FW_ERROR_MESSAGE( "AI_DVB_Monitor_Init	Failed \n");
				ASSERT(1, "AI_DVB_Monitor_Init	Failed");
			}
	
			/*Initialize psi monitor*/
			if (AI_PSI_Monitor_Init(FW_PsiMon_callback) != AL_SUCCESS)
			{
				FW_ERROR_MESSAGE( "AI_PSI_Monitor_Init	Failed \n");
				ASSERT(1, "AI_PSI_Monitor_Init	Failed");
			}
	
			/*Initialize ews monitor*/
#ifdef CONFIG_SUPPORT_EWS
			if (AI_EWS_Monitor_Init(FW_EwsMon_callback) != AL_SUCCESS)
			{
				FW_ERROR_MESSAGE( "AI_EWS_Monitor_Init	Failed \n");
				ASSERT(1, "AI_EWS_Monitor_Init	Failed");
			}
#endif
	
#ifdef SUPPORT_ASTRA_LCN_MONITOR
			if (AI_AstraLcn_Monitor_Init(FW_AstraLcnMon_callback) != AL_SUCCESS)
			{
				FW_ERROR_MESSAGE( "AI_AstraLcn_Monitor_Init  Failed \n");
				ASSERT(1, "AI_AstraLcn_Monitor_Init  Failed");
			}
#endif
		}
		return AL_SUCCESS;
#endif
}
#endif
/**
* @brief get framwork version.
*
* API gets the al layor version. version info is composed of major and minor version number.
* major and minor version number occupy the least two bytes and the highest two bytes are reserved currently.
* The follow lists the bytes order. Version can by represented with notation of XX XX MAJOR Minor, above which XX represents nothing.
*
* @param version[output] Pointer to Version Number.
*
* @return AL_SUCCESS if get version is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_GetVersion(al_uint32 *version)
{
	if (al_null == version)
	{
		return AL_INVALID_PARAM;
	}

	*version = MK_VERSION(AL_FW_MAJOR, AL_FW_MINOR);
	return AL_SUCCESS;
}

/**
* @brief To Terminate NVM storing task.
*
* @param IsSync[input]
*
* @return AL_SUCCESS if terminate is successful, AL_FAILURE otherwise.
*/
#if defined (CONFIG_DTV_SUPPORT) || defined (CONFIG_ATV_SUPPORT)
AL_Return_t AL_FW_TermStoreDevice(al_bool IsSync)
{
	return AI_DataSaving_Term(IsSync);
}
#endif

/**
* @brief Swtich DB module at all the DVB DB module.
*
* @param eDBType[input] the DB module will be active(other DVB DB Module will deactive).
*
* @return AL_SUCCESS if switch is successful, AL_FAILURE otherwise.
*/
#ifdef CONFIG_DTV_SUPPORT
AL_Return_t AL_FW_SwitchDBModule(AL_DB_EDBType_t eDBType)
{
	CDBIActiveDBModule(eDBType, TRUE);
#ifndef USE_ROMTER
	if (eDBType == AL_DBTYPE_DVB_C)
	{
		CDBIActiveDBModule(AL_DBTYPE_DVB_T, FALSE);
		CDBIActiveDBModule(AL_DBTYPE_DVB_S, FALSE);
		#ifdef CONFIG_ISDB_SYSTEM
		CDBIActiveDBModule(AL_DBTYPE_DVB_SBTVD, FALSE);
		#endif
	}
	else if (eDBType == AL_DBTYPE_DVB_T)
	{
		CDBIActiveDBModule(AL_DBTYPE_DVB_C, FALSE);
		CDBIActiveDBModule(AL_DBTYPE_DVB_S, FALSE);
		#ifdef CONFIG_ISDB_SYSTEM
		CDBIActiveDBModule(AL_DBTYPE_DVB_SBTVD, FALSE);
		#endif
	}
	else if (eDBType == AL_DBTYPE_DVB_S)
	{
		CDBIActiveDBModule(AL_DBTYPE_DVB_T, FALSE);
		CDBIActiveDBModule(AL_DBTYPE_DVB_C, FALSE);
		#ifdef CONFIG_ISDB_SYSTEM
		CDBIActiveDBModule(AL_DBTYPE_DVB_SBTVD, FALSE);
		#endif
	}
	#ifdef CONFIG_ISDB_SYSTEM
	else if (eDBType == AL_DBTYPE_DVB_SBTVD)
	{
		CDBIActiveDBModule(AL_DBTYPE_DVB_T, FALSE);
		CDBIActiveDBModule(AL_DBTYPE_DVB_C, FALSE);
		CDBIActiveDBModule(AL_DBTYPE_DVB_S, FALSE);
	}
	#endif
#endif
#ifdef CONFIG_DTV_SUPPORT
	#ifdef SUPPORT_DVBS_LCN
	CDBIActiveLCNProc4DB(eDBType, TRUE);
	#else
	if (eDBType != AL_DBTYPE_DVB_S)
	{
#if defined(CONFIG_DVB_SYSTEM) ||  defined(CONFIG_DTMB_SYSTEM)
		CDBIActiveLCNProc4DB(eDBType, TRUE);
#endif
	}
	#endif
#endif
	return AL_SUCCESS;
}
#endif
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
/**
* @brief active DB module.
*
* @param eDBType[input] the DB module will be active.
*
* @return AL_SUCCESS if switch is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_ActiveDBModule(AL_DB_EDBType_t eDBType)
{
	return CDBIActiveDBModule(eDBType, TRUE);
}


/**
* @brief deactive DB module.
*
* @param eDBType[input] the DB module will be deactive.
*
* @return AL_SUCCESS if switch is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_DeActiveDBModule(AL_DB_EDBType_t eDBType)
{
	return CDBIActiveDBModule(eDBType, FALSE);
}

/**
* @brief Reload DB module.
*
* @param eDBType[input] the DB module will be reload.
*
* @return AL_SUCCESS if reload is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_ReLoadDBModule(AL_DB_EDBType_t eDBType)
{
	CDBIActiveDBModule(eDBType, FALSE);
	CDBIActiveDBModule(eDBType, TRUE);
#ifdef CONFIG_DTV_SUPPORT
	#ifdef SUPPORT_DVBS_LCN
	CDBIActiveLCNProc4DB(eDBType, TRUE);
	#else
	if (eDBType != AL_DBTYPE_DVB_S)
	{
		CDBIActiveLCNProc4DB(eDBType, TRUE);
	}
	#endif
#endif
	return AL_SUCCESS;
}

/**
* @brief Check Active Database module.
*
* @param eDBType[input] indicate which database to check
*
* @return al_true if active , al_false otherwise.
*/
al_bool AL_FW_CheckActiveDBModule(AL_DB_EDBType_t eDBType)
{
	bool bActive = al_false;

	CDBIGetDBModuleActive(eDBType, &bActive);

	return bActive;
}
#endif
/**
* @brief Set FW to specific mode.
*
* @param eFWMode[input] indicated which mode to set.
*
* @return AL_SUCCESS if active is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_ActiveFWModule(FW_mode_t eFWMode)
{
	fw_data_struct.fw_mode = eFWMode;

	return AL_SUCCESS;
}

/**
* @brief Get FW to specific mode.
*
* @param peFWMode[input] indicated which mode to set.
*
* @return AL_SUCCESS if active is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_GetFWModule(FW_mode_t *peFWMode)
{
	if (peFWMode == NULL)
	{
		return AL_FAILURE;
	}
	*peFWMode = fw_data_struct.fw_mode;

	return AL_SUCCESS;
}

/**
* @brief get context callback funcion.
*
* @param ppfGetContext[output] callback function.
*
* @return AL_SUCCESS get callback funcion is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_GetContextCb(frmwrk_pfOpConext_t *ppfOpContext)
{
	if (ppfOpContext == al_null)
	{
		return AL_INVALID_PARAM;
	}

	if (fw_data_struct.fw_context_cb == al_null)
	{
		return AL_FAILURE;
	}

	*ppfOpContext = fw_data_struct.fw_context_cb;

	return AL_SUCCESS;
}

/**
* @brief clear fw queue funcion.
*
* @param None.
*
* @return AL_SUCCESS clear queue funcion is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_FW_ReleaseQueue(void)
{
	GL_Status_t ret = AL_FAILURE;
	FW_msg_t message;

	memset(&message, 0, sizeof(FW_msg_t));

	if (fw_data_struct.fw_qid == NULL)
	{
		return AL_FAILURE;
	}

	while (1)
	{
		ret = GL_QueueReceive(fw_data_struct.fw_qid, &message, sizeof(FW_msg_t), GL_NO_WAIT);
		if (ret == GL_QUEUE_EMPTY)
		{
			return AL_SUCCESS;
		}
		else if (ret == GL_FAILURE)
		{
			UMFDBG(0,"clear FW queue failure\n");
			return AL_FAILURE;
		}
	}
}

/**
* @brief check dtv demux has data funcion.
*
* @param None.
*
* @return AL_SUCCESS check dtv demux has data function is successful, AL_FAILURE otherwise.
*/
#ifdef CONFIG_DTV_SUPPORT
AL_Return_t AL_FW_Check_DTVDemux_HasData(al_bool *flag)
{
	if (al_null == flag)
	{
		return AL_INVALID_PARAM;
	}

	*flag = al_false;

	if(DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_HAS_SCRAMBLED_DATA
		||DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_HAS_VIDEO_SCRAMBLED_DATA
		||DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_HAS_AUDIO_SCRAMBLED_DATA
		||DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_HAS_DATA
		||DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_INVALID
		||DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_VIDEO_NOT_SUPPORT
		|| DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_AV_INVALID
		|| DemuxState == KMF2UMF_EVTYPE_DTV_DEMUX_AUDIO_INVALID
		||DvbMonState == true)
	{
		*flag = al_true;
	}

	return AL_SUCCESS;
}
#endif
#ifdef CONFIG_SUPPORT_NOSIGNAL_AUTO_CHANGE_SOURCE
al_uint32 AL_FW_Check_HDMI_PortStatus(void)
{
	return u32HDMIActive;
}
#endif

al_uint32 AL_FW_RW_PseudoEEPROM(unsigned char *extraData)
{
    eeprom_ctl *ctl_data;
	UINT8 tmp_data[1024];
	UINT16 ofs = 0, len;
	UINT32 addr_flash,size_flash;
    
    if(extraData)
    {
        
        ctl_data = (eeprom_ctl *)extraData;

		if(!ctl_data->bAsset)
		{
		    len = sizeof(APP_SETTING_DDCCIPseudoEEPROM_t);
			Cmd_NVMRead(ENVMPurposeIDDCCIPseudoEEPROM,0, len, tmp_data);

			ofs = (ctl_data->offset)%len;
	        if(ctl_data->rw)
	        {
	            memcpy(tmp_data+ofs,ctl_data->pData , ctl_data->size);
		        Cmd_NVMWrite(ENVMPurposeIDDCCIPseudoEEPROM, 0, len, tmp_data);
	        }
			else
				memcpy(ctl_data->pData, tmp_data+ofs, ctl_data->size);
		}
		else
		{
			Cmd_GetAddrAndSizeByFlashID((UINT8)FLASH_ID_EDID, &addr_flash, &size_flash);
			if((ctl_data->offset<size_flash) && (ctl_data->size<size_flash) && 
				(addr_flash+ctl_data->offset+ctl_data->size) < (addr_flash+size_flash))
			{
				if(ctl_data->rw)
			        Cmd_FlashWrite((UINT32)(addr_flash+ctl_data->offset),(UINT32)ctl_data->pData, (UINT32)ctl_data->size);
				else
					Cmd_FlashRead((UINT32)ctl_data->pData,(UINT32)(addr_flash+ctl_data->offset), (UINT32)ctl_data->size);
			}
			else
				return AL_FAILURE;
		}
    }
	else
		return AL_FAILURE;
	
	return AL_SUCCESS;
}

/*****************************************************************************
** $Rev: 1913 $
**
*****************************************************************************/
