/**
* Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved.
*
* @file ai_psi_monitor.h
*
* @brief this file defines psi monitor interface for app layer.
*
* The file supports the psi monitor functions by used for app layer.
* The psi monitor check all psi and si table changed,
* if table change parse and update information to database and notify application dispose.
*
*/

#ifndef __AI_PSI_MONITOR_H__
#define __AI_PSI_MONITOR_H__

#include "al_basictypes.h"
#include "al_database.h"
#include "al_psi_monitor.h"

#ifdef __cplusplus
	extern "C"{
#endif

/**
* @brief Define message type of psi monitor component, usted to notify framwork.
*/
typedef enum
{
	AI_PSIMONITOR_SERVICE_MOVE, /**< When received service move descriptor */
	AI_PSIMONITOR_AUDIO_UPDATE, /**< Received audio steam, and check audio update */
	AI_PSIMONITOR_SUBTITLE_UPDATE, /**< Received subtitle descriptor */
	AI_PSIMONITOR_TELETECT_UPDATE, /**< Received teletext related descriptor */
	AI_PSIMONITOR_SRV_UPDATE, /**< Service name or service attribute update */
	AI_PSIMONITOR_OTHERTP_SRV_FOUND, /**< In NIT or SDT other found other tp service list */
	AI_PSIMONITOR_ADD_SRV, /**< Add current tp service to database */
	AI_PSIMONITOR_UPDATE_LCN, /**< lCN has changed, need process LCN again */
	AI_PSIMONITOR_REMOVE_SRV, /**< Remove current tp service from database */
	AI_PSIMONITOR_TIME_UPDATE, /**< Received TOT or TDT */
	AI_PSIMONITOR_TIME_OFFSET_UPDATE, /**<TOT offset changes */
	AI_PSIMONITOR_FTADATA_UPDATE, /**< Received FTA  FTA_content_management_descriptor*/
	AI_PSIMONITOR_SERVICE_REPLACE,/**< Received linkdescriptor dispose service replace*/
	AI_PSIMONITOR_SERVICE_SIMULCASE_REPLACE, /**< Received linkdescriptor dispose simulcase replace*/
	AI_PSIMONITOR_CLOSECAPTION_UPDATE, /**< Received CC descriptor */
	AI_PSIMONITOR_TS_CHANGED, /**< Dynmic TS changed */
	AI_PSIMONITOR_PMT_UPDATE, /**< Refind PMT after PAT update */
	AI_PSIMONITOR_NEW_TSID,	/**< New TS_id is found in NIT */
	AI_PSIMONITOR_FIND_NEW_TPLIST, /**< Find new freq list in nit */
	AI_PSIMONITOR_SERTYPE_CHANGED, /**< service type changed */
	AI_PSIMONITOR_NETWORK_UPDATE, /**< network name update */
#ifdef CONFIG_SUPPORT_INDONESIA_EWS
	AI_PSIMONITOR_INDONESIA_EWS_UPDATE, /**< Receive Indonesia EWS */
	AI_PSIMONITOR_INDONESIA_EWS_STOP, /**< Indonesia EWS signal stream stops */
#endif
#ifdef SUPPORT_NETWORK_UPDATE_RESCAN
	AI_PSIMONITOR_NWK_UPDATE_STATIC,
	AI_PSIMONITOR_NWK_UPDATE_RESCAN,
#endif
	AI_PSIMONITOR_MAX,
} AI_PsiMon_Message_Type_t;

/**
* @brief Define sipsi state.
*/
typedef enum
{
	AI_PSIMONITOR_STATE_NOT_RUNNING_FLAG = (1<<1), /**< Current program is not running, sdt running status is 0x01 */
	AI_PSIMONITOR_STATE_NOT_RUNNING_REPLACEMENT_EXIST = (1<<2), /**< Not running replacement exist, lingkage type is 0x05 */
	AI_PSIMONITOR_STATE_SCRAMBLED_REPLACEMENT_EXIST = (1<<3), /**< Scramble replacement exist, lingkage type is 0x03 */
	AI_PSIMONITOR_STATE_SIMULCAST_REPLACEMENT_EXIST = (1<<4), /**< Simulcast replacement exist, lingkage type is 0x82 , for NORDIG spec */
	AI_PSIMONITOR_STATE_NOT_RUNNING_REPLACED = (1<<5), /**< Not running replaced,  has disposed not running replacement */
	AI_PSIMONITOR_STATE_SCRAMBLED_REPLACED = (1<<6),  /**< Scramble replaced,  has disposed scramble replacement */
	AI_PSIMONITOR_STATE_SIMULCAST_REPLACED = (1<<7), /**< Simulcast replaced, has dispose simulcase scramble replacement */

	AI_PSIMONITOR_STATE_MAX,
} AI_PsiMon_State_Type_t;


/**<Type define*/
typedef enum
{
	AI_PSIMONITOR_TIMER_SERVICE_ADD = 0,
	AI_PSIMONITOR_TIMER_SERVICE_REMOVE,
	AI_PSIMONITOR_TIMER_SERVICE_UPDATE,
	AI_PSIMONITOR_TIMER_INDONESIA_EWS_STOP,
	AI_PSIMONITOR_TIMER_NUMBER_MAX,
}AI_PsiMon_Timer_Type_t;

/**
* @brief Call back function prototype for framwork to register.
*/
typedef	al_int32 (* AI_pfPsiMonCallback)(AI_PsiMon_Message_Type_t EventType, al_uint32 u32Param);

/**
* @brief psi monitor init function.
*
* @param pfPsiMonCallbks[input] psi monitor notify framwork callback function.
*
* @return AL_SUCCESS if initialization is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_Init(AI_pfPsiMonCallback pfPsiMonCallbks);

/**
* @brief psi monitor init function.
*
* @param al_void.
*
* @return AL_SUCCESS if uninitialization is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_Uninit(al_void);

/**
* @brief psi monitor add service list(sdt service list/nit service list/pat service list) to database.
*
* Add current TP sdt service list/nit service list/pat service list to database.
*
* @param pu32AddTotalNum[output] add services total number.
*
* @return AL_SUCCESS add  current tp service is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_AddSrv(al_uint32 *pu32AddTotalNum);

/**
* @brief psi monitor remove service from database.
*
* Remove current TP service from database.
*
* @param pu32RemovTotalNum[output] remove services total number.
*
* @return AL_SUCCESS remove service is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_RemoveSrv(al_uint32 *pu32RemovTotalNum);

/**
* @brief Add and remove and update service timer clear.
*
* Clear add and remove and update service timer state.
*
* @param u32ClearFlag[input] need be clear time.
*
* @return AL_SUCCESS timer check is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_TimerEventClear(al_uint32 u32ClearFlag);

/**
* @brief Add service and remove service timer check.
*
* Add service and remove service need a timer to check, not instant dispose.
*
* @param u32MSec[input] timer valure in ms.
*
* @return AL_SUCCESS timer check is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_TimerEventCheck(al_uint32 u32MSec);

/**
* @brief Set psi monitor sipsi state.
*
* This function used to record psi monitor sipsi info state,(PAT_ARRIVAL, SDT_ARRIVAL, SERVICE_PMT_LOST,
* NOT_RUNNING_FLAG, NOT_RUNNING_REPLACEMENT_EXIST, SCRAMBLED_REPLACEMENT_EXIST,
* NOT_RUNNING_REPLACED, SCRAMBLED_REPLACED).
*
* @param u32State[input] sipsi state.
*
* @param pParam[input] sipsi state's parameter, program handle for replacement.
*
* @return AL_SUCCESS set sipsi state is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_SetState(al_uint32 u32State, AL_RecHandle_t *pParam);

/**
* @brief Clear psi monitor sipsi state.
*
* This function used to clear psi monitor sipsi info state,(PAT_ARRIVAL, SDT_ARRIVAL, SERVICE_PMT_LOST,
* NOT_RUNNING_FLAG, NOT_RUNNING_REPLACEMENT_EXIST, SCRAMBLED_REPLACEMENT_EXIST,
* NOT_RUNNING_REPLACED, SCRAMBLED_REPLACED).
*
* @param u32State[input] sipsi state.
*
* @return AL_SUCCESS clear sipsi state is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_ClearState(al_uint32 u32State);

/**
* @brief Check psi monitor sipsi state.
*
* This function used to check psi monitor sipsi info state,(PAT_ARRIVAL, SDT_ARRIVAL, SERVICE_PMT_LOST,
* NOT_RUNNING_FLAG, NOT_RUNNING_REPLACEMENT_EXIST, SCRAMBLED_REPLACEMENT_EXIST,
* NOT_RUNNING_REPLACED, SCRAMBLED_REPLACED).
*
* @param u32State[input] sipsi state.
*
* @param pParam[output] sipsi state's parameter, program handle for replacement.
*
* @return al_ture if state exist, al_false if state not exist.
*/
al_bool AI_PSI_Monitor_CheckState(al_uint32 u32State, AL_RecHandle_t *pParam);

/**
* @brief psi monitor FTa data update dispose.
*
* Parse FTA_content_management_descriptor and notify uplayer dtg cmg data update in NIT/EIT/SDT.
*
* @param u8Data[input] FTA_content_management_descriptor last one byte data.
*
* @ param u8Tbl[input] tabla type include NIT_1ST_LOOP/NIT_2ST_LOOP/SDT/EIT.
*
* @return AL_SUCCESS FTA_content_management_descriptor parse success is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_FTADataParse(al_uint8 u8Data, al_uint8 u8Tbl);

/**
* @brief psi monitor set FTA data table type.
*
* The tabla type include NIT_1ST_LOOP/NIT_2ST_LOOP/SDT/EIT.
*
* @ param u8Tbl[input] tabla type include NIT_1ST_LOOP/NIT_2ST_LOOP/SDT/EIT.
*
* @return AL_SUCCESS set table type is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_SetFTADataTbl(al_uint8 u8Tbl);

/**
* @brief psi monitor replace service process.
*
* Dispose replace service, include not running/ scramble.
*
* @ param ReplaceState[input] replace service type.
*
* @return AL_SUCCESS set table type is successful, AL_FAILURE otherwise.
*/
AL_Return_t AI_PSI_Monitor_ReplaceProcess(AL_PsiMon_State_Type_t ReplaceType);

#ifdef EPG_REPLACE
AL_Return_t AI_PSI_Monitor_EPG_ReplaceCheck(al_void);
#endif

#ifdef __cplusplus
}
#endif
#endif

