/*****************************************************************************
** al_event.h: AL Layer Event Header File
**
** Description: Gives the functionalities for quering as well as manipulating
**				the events data related to certail service.
**
** Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved
**
** Author : anlzhao
**
** $Id: al_event.h 1910 2011-01-24 05:05:35Z anlzhao_c1 $
*****************************************************************************/

#ifndef __AL_EVENT_H__
#define __AL_EVENT_H__

#include "al_basictypes.h"
#if defined(CONFIG_DTV_SUPPORT) || defined(CONFIG_ATV_SUPPORT)
#include "al_database.h"
#endif
#ifdef CONFIG_DTV_SUPPORT
#include "dvb_epg.h"
#endif
#include "al_time.h"

/*
** Marco define
*/

#ifdef __cplusplus
	extern "C"{
#endif

#ifdef CONFIG_DTV_SUPPORT
#define AL_EVENT_SHORTNAME_LEN 	(MAX_DATA_SIZE)		/*!< \brief text length of event_name_char */
#define AL_EVENT_TEXT_LEN 			(MAX_DATA_SIZE)		/*!< \brief text length short event text_char */
#define AL_EVENT_EXTEND_LEN 		(MAX_DATA_SIZE)	/*!< \brief text length of extended event text_char */
#define AL_EVENT_MAX_EVENT_DES	(EPG_EXTEND_EVENT_DESC_NUM)
#define ISO_639_LANGUAGE_CODE		(4)

/* Prototype of AL_Event_t */
typedef MID_EPG_EventHandle_t AL_Event_t;

/**
 *	\brief event date and time discription
 */
typedef struct _AL_Event_Time_Descript_t
{
	AL_Time_t stStartTime;		/*!< \brief start date */
	AL_Time_t stEndTime;			/*!< \brief end time */
} AL_Event_Time_Descript_t;

/* Events details */
typedef struct
{
	AL_Event_t event_hdl;
	AL_Event_Time_Descript_t event_time;
	AL_Event_Time_Descript_t event_utc_time;
	al_uint8 event_name_length;
	al_uint8 event_text_length;
	al_uint8 event_rating;
	al_uint8 event_name[AL_EVENT_SHORTNAME_LEN];
	al_uint8 event_text[AL_EVENT_TEXT_LEN];
	al_uint8 event_langcode[ISO_639_LANGUAGE_CODE];
	al_uint16 event_Id;
	al_uint32 event_offset;
	al_uint32 extend_num;
	al_uint8  content_classify; //zhongbaoxing added for NorDig Test 08:42 @20120808
} AL_Event_Details_t;

/* extend event Events */
typedef struct
{
	al_uint8 item_description[AL_EVENT_EXTEND_LEN];
	al_uint8 extend_text[AL_EVENT_EXTEND_LEN];
	al_uint8 event_langcode[ISO_639_LANGUAGE_CODE];
	al_uint8 extend_text_exsit :4;
	al_uint8 item_description_exsit :4;
	al_uint8 item_description_length;
	al_uint8 extend_text_length;
} AL_Event_Desciption_t;

#endif
/***************************************************************************
** FUNCTION : AL_Event_SetCountryCode
**
** DESCRIPTION :
**				Set country code.
**
** PARAMETERS :
**				countrycode -country code
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_SetCountryCode(al_int8 *countrycode);

/***************************************************************************
** FUNCTION : AL_Event_SetDTGSupport
**
** DESCRIPTION :
**				set DTG support flag. This flag may used during parsing EIT process
**
** PARAMETERS :
**				flag	-al_true means set, al_false means unset.
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_SetDTGSupport(al_bool flag);

/***************************************************************************
** FUNCTION : AL_Event_SetDTGSupport
**
** DESCRIPTION :
**				set DTG support flag. This flag may used during parsing EIT process
**
** PARAMETERS :
**				flag	- al_true means set, al_false means unset.
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
al_bool AL_Event_GetEDTGSupport(al_void);

/***************************************************************************
** FUNCTION : AL_Event_SetPreferLanguageCode
**
** DESCRIPTION :
**				Set prefer language code for event module. this may be useful for event
**				to provide the event with prefer language code.
** PARAMETERS :
**				langptr1 - prefer language code
**				langptr2 - prefer language code
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_SetPreferLanguageCode(void * langptr1, void * langptr2);
#if defined(CONFIG_DTV_SUPPORT) && !defined(CONFIG_USE_COMPRESSED_EPGMODULE)
/***************************************************************************
** FUNCTION : AL_Event_GetEventRatingByHdl
**
** DESCRIPTION :
**				API fills details of First 'n' events of given service.
**
** PARAMETERS :
**				eventhdl	- Events handle
**				Rating	- Event parental rating
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetEventRatingByHdl(AL_Event_t eventhdl, al_uint8 *pRating);
#endif

#ifdef CONFIG_DTV_SUPPORT
/***************************************************************************
** FUNCTION : AL_Event_Stop
**
** DESCRIPTION :
**				Stop the event component, collecting of EIT stops.
**
** PARAMETERS :
**				DBType: Specify database on which to stop.
**				is_exit_task : al_true: exit EPG task, else no exit
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_Stop(AL_DB_EDBType_t DBType, al_bool is_exit_task);

/***************************************************************************
** FUNCTION : AL_Event_LockChannels
**
** DESCRIPTION :
**				start channels by given network id, ts id and serivce.
**
** PARAMETERS :
**				DBType -Specify database on which to stop.
**				net_id - network id
**				trans_id -ts id
**				serv_id - service id
**				chan_num - tells how many channels to open simultaneously
**				focus_index- tells which channel has higher priority. it's useful when cache
**					is not enough, events with lower priority will be replaced by higher ones.
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_LockChannels(AL_DB_EDBType_t DBType,  al_uint16 *net_id,
	al_uint16 *trans_id, al_uint16 *serv_id, al_uint8 chan_num, al_int32 focus_index);

/***************************************************************************
** FUNCTION : AL_Event_UnLockChannels
**
** DESCRIPTION :
**				unlock channels by given network id, ts id and serivce. this API will
**				clear cache that pre-occupied by the channel. Note: Set both
**				net_id 0XFF and trans_id 0xFF for clear entire cache.
**
** PARAMETERS :
**				DBType -Specify database on which to stop.
**				net_id - network id. clear entire cache if set 0xFF
**				trans_id -ts id. clear entire cache if set 0xFF
**				serv_id - service id
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_UnLockChannels(AL_DB_EDBType_t DBType,  al_uint16 net_id,
	al_uint16 trans_id, al_uint16 serv_id);

/***************************************************************************
** FUNCTION : AL_Event_GetEventHdlByStartTime
**
** DESCRIPTION :
**				API fills details of First 'n' events of given service.
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_start_time - the start time of the event
**				duration	- duration in minutes
**				events_req	- Events required
**				eventhdl_list	- Event handles to be filled into
**				events_fnd	- Events handle found
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetEventHdlByStartTime(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	AL_Time_t *event_start_time, al_uint32 duration,
	al_uint16 events_req, AL_Event_t *eventhdl_list, al_uint16 *events_fnd);

/***************************************************************************
** FUNCTION : AL_Event_GetNextNumofEvents
**
** DESCRIPTION :
**				API fills details of Next 'n' events of given service, including current one
**				indicated by event_hdl
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_ref	- Ref Event
**				events_req	- Events required
**				events_detail	- Event data structure to be filled
**				events_fnd	- Events found
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetNextNumofEvents(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	AL_Event_t event_hdl, al_uint16 events_req,
	AL_Event_Details_t *events_detail, al_uint16 *events_fnd);

/***************************************************************************
** FUNCTION : AL_Event_GetPrevNumofEvents
**
** DESCRIPTION :
**				API fills details of previous 'n' events of given service.
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_ref	- Ref Event
**				events_req	- Events required
**				events_detail	- Event data structure to be filled
**				events_fnd	- Events found
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetPrevNumofEvents(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	AL_Event_t event_hdl, al_uint16 events_req,
	AL_Event_Details_t *events_detail, al_uint16 *events_fnd);

/***************************************************************************
** FUNCTION : AL_Event_GetLastNumofEvents
**
** DESCRIPTION :
**				API fills details of last 'n' events of given service.
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_ref	- Ref Event
**				events_req	- Events required
**				events_detail	- Event data structure to be filled
**				events_fnd	- Events found
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetLastNumofEvents(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	al_uint16 events_req, AL_Event_Details_t *events_detail,
	al_uint16 *events_fnd);

/***************************************************************************
** FUNCTION : AL_Event_GetCurrNextEvents
**
** DESCRIPTION :
**				API fill the details of current/next event of given Service.
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_cur	- event structure to hold current event info
**				event_next	- event structure to hold next event info
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetCurrNextEvents(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	AL_Event_Details_t *event_cur, AL_Event_Details_t *event_next);

/***************************************************************************
** FUNCTION : AL_Event_GetCurrNextEventsForCurrRating
**
** DESCRIPTION :
**			  API fill the details of current/next event of given Service.
**
** PARAMETERS :
**			  DBType  - Database type(DVB_T/DVB_C etc)
**			  net_id  - Network ID
**			  trans_id- Transport Stream ID
**			  serv_id - Service ID
**			  event_cur   - event structure to hold current event info
**			  event_next  - event structure to hold next event info
**
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetCurrNextEventsForCurrRating(AL_DB_EDBType_t DBType,
  al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
  AL_Event_Details_t *event_cur, AL_Event_Details_t *event_next);

/***************************************************************************
** FUNCTION : AL_Event_GetEventsBetweenTime
**
** DESCRIPTION :
**				Provide events number between time for a service.
**				Notice: when event_start_time or event_start_time
**				is null, it means the user don't care about it. For example,
**				both event_start_time and event_end_time is set to null,
**				then, AL_Event_GetTotalEventsBetweentime returns the total
**				events number of certail service.
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_start_time- Event start time(Not include offset)
**				event_end_time	- Event end time(Not include offset)
**				offset	- offset to get result in event list
**				num_req	- number required to get
**				got_num - actual number has been got
**				EventDetaillist - pointer to event list that contain detail information as required
**				total_num - total number of the event that meet the requirment.
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetEventsBetweenTime(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	AL_Time_t *event_start_time, AL_Time_t *event_end_time,
	al_uint16 offset, al_uint16 num_req, al_uint16 *got_num,
	AL_Event_Details_t *EventDetaillist, al_uint16 *total_num);

/***************************************************************************
** FUNCTION : AL_Event_GetEventsBetweenTime
**
** DESCRIPTION :
**				Provide events number between time for a service.
**				Notice: when event_start_time or event_start_time
**				is null, it means the user don't care about it. For example,
**				both event_start_time and event_end_time is set to null,
**				then, AL_Event_GetTotalEventsBetweentime returns the total
**				events number of certail service.
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_start_time- Event start time(Not include offset)
**				event_end_time	- Event end time(Not include offset)
**				offset	- offset to get result in event list
**				num_req	- number required to get
**				got_num - actual number has been got
**				EventDetaillist - pointer to event list that contain detail information as required
**				total_num - total number of the event that meet the requirment.
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetAllSchduleEventsBetweenTime(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id,
	AL_Time_t *event_start_time, AL_Time_t *event_end_time,
	al_uint16 offset, al_uint16 num_req, al_uint16 *got_num,
	AL_Event_Details_t *EventDetaillist, al_uint16 *total_num);


/***************************************************************************
** FUNCTION : AL_Event_GetEventsDescription
**
** DESCRIPTION :
**				Provide extend events details by given event.
**				Notice: when ExtendEvent_t is null, means user only cares
**				about total number of the extend event. For example,
**
** PARAMETERS :
**				DBType	- Database type(DVB_T/DVB_C etc)
**				net_id	- Network ID
**				trans_id- Transport Stream ID
**				serv_id	- Service ID
**				event_hdl - event handle
**				offset - tells the position to get the extend event
**				num_req	- number required to get
**				got_num - actual number has been got
**				ExtendDetail_list_t - pointer to list that contain extend detail information as required
**				total_num - total number of the event that meet the requirment.
** RETURN VALUES : AL_Return_t
***************************************************************************/
AL_Return_t AL_Event_GetEventsDescription(AL_DB_EDBType_t DBType,
	al_uint16 net_id, al_uint16 trans_id, al_uint16 serv_id, AL_Event_t event_hdl,
	al_uint16 offset, al_uint16 req_num, al_uint16 *got_num,
	AL_Event_Desciption_t *ExtendDetail_list_t, al_uint16 *total_num);

#endif
/**
* @brief Get descriptor of the event by event id and tag
*
* @param event_hdl - event handle
* @param tag - descriptor tag
* @param offset - position to get the descriptor
* @param pDesc - pointer to the descriptor data
*
* @retval  AL_SUCCESS means succss. AL_INITIALIZE_ALREADY means already initialised.
* AL_FAILURE means failed
*/
#ifdef CONFIG_USE_COMPRESSED_EPGMODULE
AL_Return_t AL_Event_GetDescriptor(AL_Event_t event_hdl, al_uint16 OrgNetId, al_uint16 TSId, 
	al_uint16 SrvId, al_uint8 tag, al_uint16 *offset, void *pDesc);
#else
#ifdef CONFIG_DTV_SUPPORT
AL_Return_t AL_Event_GetDescriptor(AL_Event_t event_hdl,
	al_uint8 tag, al_uint16 *offset, void *pDesc);
#endif
#endif
/**
* @brief according raw data to get code page and remove codepage data from raw data
*
* @param pCodePage, output defualt code page.
*
* @param rawchr, point to raw data.
*
* @param size, raw data size.
*
* @return AL_SUCCESS  is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_Event_GetCodePage(al_uint32 *pCodePage, UINT8 *rawchr, INT32 *size);

/**
* @brief if string not carry codepage, need to get defualt code page
*
* @param pCodePage, output defualt code page.
*
* @return AL_SUCCESS  is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_Event_GetDefaultCodePage(al_uint32 *pCodePage);
al_uint32 AL_Event_MonUtf16IsBEorLE(UINT8 *rawchr, INT32 *size);

#ifdef CONFIG_USE_COMPRESSED_EPGMODULE
#ifdef SUPPORT_CLEAN_EX_DESCRIPTOR
al_void AL_Event_CleanExDescriptor(al_void);
al_void AL_Event_ReceiveExDescriptor(al_void);
#endif

#ifdef SUPPORT_CLEAN_EPG
al_void AL_Event_CleanEPG(al_void);
al_void AL_Event_ReceiveEPG(al_void);
#endif
#endif

#ifdef __cplusplus
	}
#endif /* __cplusplus */

#endif /* __AL_EVENT_H__ */

/*****************************************************************************
** $Rev: 1910 $
**
*****************************************************************************/
