/**
	@file AZBoxTVSDK_DEF.h
	@brief header of AZBoxTVSDK

	@author miki 
	@ingroup L1SW2
	@date 2009.10.18
**/

#ifndef __AZBOXTVSDK_DEF_H__
#define __AZBOXTVSDK_DEF_H__
#include <time.h>
#include "hx_types.h"
#include "list_head.h"
#include "DTV.h"

/********************************************************************************************
  CHANNEL SDK 						
********************************************************************************************/

#define SELECT_TUNER_ALL -1
#define SELECT_TUNER_0 0
#define SELECT_TUNER_1 1
#define SELECT_TUNER_2 2
#define SELECT_TUNER_3 3



#define VCODEC_FLAG_NONE			0x00
#define VCODEC_FLAG_HD				0x01
#define VCODEC_FLAG_INT				0x02


#define MAX_CHANNEL 						15000
#define MAX_PROMO_CHANNEL 					10
#define MAX_TV_CHANNEL 						10000
#define MAX_RADIO_CHANNEL					5000
#define MAX_FAV_NUM							200
#define MAX_FAV								50
#define MAX_RETURN_CHANNEL					20
//------------------ group -------------------------------
#define MAX_PROVIDER						2000
#define MAX_GENRE							300
#define MAX_COUNTRY							300	
#define MAX_SATELITES 						255

#define GENRE_DAT_NAME "genre.dat"
#define COUNTRY_DAT_NAME "country.dat"
#define ALL_NETWORK		0xFF

#define MAX_LIM_NUM						10

#define ETH_CHANNEL_NUM_START			(MAX_CHANNEL+1)

#define CHAPI_OK 0 
/********************************************************************
 * About channel.type
 *
 * - high 8bit : source type
 * 		examples: Satellite, Terrestrial, Cable, Ethernet, HDD and etc
 *
 * - low 8bit : service type
 * 		examples: TV, Radio, Data and etc
 */


 // - high 8bit : source type
#define SOURCE_TYPE_NONE		0x00
#define SOURCE_TYPE_DVBS		0x01
#define SOURCE_TYPE_DVBT		0x02
#define SOURCE_TYPE_ISDBT		0x03

#define SOURCE_TYPE_UDP			0x04
#define SOURCE_TYPE_CIP			0x05
#define SOURCE_TYPE_HDD			0x06
#define SOURCE_TYPE_GTV			0x07
#define SOURCE_TYPE_RECFILE	0x08
#define SOURCE_TYPE_ATSC		0x09
#define SOURCE_TYPE_ARIB		0x0A
#define SOURCE_TYPE_DVBC		0x0B
#define SOURCE_TYPE_HTTP		0x0C

// - low 8bit : service type
#define SERVICE_TYPE_NONE			0x00
#define SERVICE_TYPE_TV				0x01
#define SERVICE_TYPE_RADIO			0x02
#define SERVICE_TYPE_ANALOG		0x03
#define SERVICE_TYPE_MPROMO		0x80
#define SERVICE_TYPE_PROMO			0x81


#define VINDEX_TYPE_DEFAULT	0
#define VINDEX_TYPE_TV 		1
#define VINDEX_TYPE_RADIO 	2
#define VINDEX_TYPE_USER	3
#define VINDEX_TYPE_TEMP		4

#define VINDEX_TYPE_FAV_MIN	10
#define VINDEX_TYPE_FAV_MAX	MAX_FAV + 9

#define SORT_DEFAULT			0
#define SORT_SATELLITE			1
#define SORT_CH_NAME			2
#define SORT_TRANSPONDER		3
#define SORT_FTA				4
#define SORT_LOCK				5
#define SORT_NUMBER				6
#define SORT_BOUQUET			7
#define SORT_SERVICE_ID			8
#define SORT_TUNER_ID			9	
#define SORT_NONE				255

#define GROUP_LIST_ALL_CH	 	0
#define GROUP_LIST_SATELLITE_CH	1
#define GROUP_LIST_GENRE_CH		2
#define GROUP_LIST_PROVIDER_CH	3
#define GROUP_LIST_COUNTRY_CH	4
#define GROUP_LIST_FTACAS_CH	5
#define GROUP_LIST_FAV_CH		6

#define TVY     	0
#define TVY7        1
#define TVY7FV      2
#define TVG     	3
#define TVPG        4
#define TV14        5
#define TVMA        6

#define TV_RATING_MIN 0
#define TV_RATING_MAX 6

#define STR_RATING(i) \
    (i >= TVY)?"TV-Y":\
    (i == TVY7)?"TV-Y7":\
    (i == TVY7FV)?"TV-Y7FV":\
    (i == TVG)?"TV-G":\
    (i == TVPG)?"TV-PG":\
    (i == TV14)?"TV-14":\
    (i <= TVMA)?"TV-MA":"TV-Y"

#define STR_VCH(i) \
	(i == VINDEX_TYPE_DEFAULT)?"ALL CH":\
    (i == VINDEX_TYPE_TV)?"TV CH":\
    (i == VINDEX_TYPE_RADIO)?"RADIO CH":\
    (i == VINDEX_TYPE_USER)?"USER CH":\
    (i >= VINDEX_TYPE_FAV_MIN) &&  (i <= VINDEX_TYPE_FAV_MAX)?"FAV CH":\
    (i < VINDEX_TYPE_DEFAULT)||(i > VINDEX_TYPE_FAV_MAX)?"NONE":"NONE"

#define STR_GROUP_LIST(i) \
	(i == GROUP_LIST_ALL_CH)?"ALL":\
    (i == GROUP_LIST_SATELLITE_CH)?"SATELLITE":\
    (i == GROUP_LIST_GENRE_CH)?"GENRE":\
    (i == GROUP_LIST_PROVIDER_CH)?"PROVIDER":\
    (i == GROUP_LIST_COUNTRY_CH)?"COUNTRY":\
    (i == GROUP_LIST_FTACAS_CH)?"FTACAS":\
    (i == GROUP_LIST_FAV_CH)?"FAV":"NONE"

#define STR_TUNER(i) \
    (i == 0)? "A":"B"

/********************************************************************/


#define CHANNEL_ST_VERSION 9

typedef struct channel_group_st {
	HX_INT8 type;
	HX_INT16 id;
	HX_INT16 name_len;
	HX_INT8 name[MAX_SERVICE_NAME_LEN+1];
}stChannelGroup;

typedef struct channel_st {

	struct list_head list;

	name_list_st channel_name_list;

//	struct channel_group_st provider_info;

	HX_INT16 country_id;
	HX_INT16 genre_id;
	HX_INT16 provider_id;

	HX_UINT8 net_index;			

	HX_INT64 freq;					// If channel type is UDP, this is inet addr
									// If channel type is CIP, this is CIP channel_num
									// If channel type is HTTP, this is stream_id(index)
									// If channel type is HDD, this is offset
	HX_UINT16 type;

	HX_UINT16 ts_id;				// If channel type is UDP, this is port num

	HX_INT16 pat_version_number;
	HX_INT16 pmt_version_number;

	HX_UINT16 original_network_id;	// If channel type is UDP, this is buffering size(kbytes)
									// If channel type is CIP, this is buffering size(kbytes)
									// If channel type is HTTP, this is buffering size(kbytes)

	HX_UINT16 service_id;

	HX_UINT8 polarisation;	

	HX_UINT16 pmt_pid;
	HX_UINT16 pcr_pid;

	HX_UINT8 video_tag;
	HX_UINT16 video_pid;
	HX_UINT16 video_ecm_pid;	// es ecm_pid

	HX_UINT8 audio_tag;
	HX_UINT16 audio_pid;
	HX_UINT16 audio_ecm_pid;
	HX_INT8 audio_lang[4];

	private_data_st pdata[PRIVATE_DATA_MAX];
	HX_INT16 pdata_num;

	HX_UINT16 ecm_pid;			// program ecm_pid

#define CHANNEL_FLAG_NONE           0x00
#define CHANNEL_FLAG_VALID          0x01
#define CHANNEL_FLAG_LOCK           0x02
#define CHANNEL_FLAG_SCRAMBLED      0x04
#define CHANNEL_FLAG_SKIP           0x08
#define CHANNEL_FLAG_AUTOPID        0x10
#define CHANNEL_FLAG_DSMCC			0x20
#define CHANNEL_FLAG_HD				0x40
#define CHANNEL_FLAG_INTERLACE    	0x80
	HX_UINT8 flag;

	HX_UINT8 use_num;

	HX_UINT8 SIDtype;		
	HX_UINT16 private_subtitling_pid;
	HX_UINT16 private_audio_pid;
	HX_INT8 private_flag;
//	HX_INT8 reserved[2];
	HX_INT16 user_ch_num;
}stChannel;


typedef struct virtual_channel_st {
	HX_INT8 tuner_id; // 0, 1
	HX_INT8 reserved; 
	HX_INT16 major_ch_num; 
	HX_INT16 minor_ch_num; 

	HX_UINT8 net_index;
	HX_UINT32 frequency;	/* intermedate frequency in Hz for QPSK */
	HX_UINT8 polar;
	HX_UINT16 service_id;
	HX_UINT16 original_network_id; //
	HX_UINT16 transport_id; //

	struct channel_st *r_ch;
}stVirtualChannel;

typedef struct tp_ch_st {
	struct list_head list;

	HX_UINT16 transport_id;
	HX_UINT32 frequency;	/* intermedate frequency in Hz for QPSK */
	HX_UINT8 polarisation;	
	HX_UINT16 rch_num;	

	struct channel_st rch;
}stTPCH;


typedef struct network_ch_st {
	struct list_head list;
	HX_UINT8 net_index;	

	HX_UINT16 network_id; //original_network_id
	HX_UINT8 name[MAX_SAT_NAME_LEN];

	HX_UINT16 tp_num;	
	struct tp_ch_st tp_ch;
}stNetworkCH;


typedef struct channel_list_st {
	HX_INT16 version;
/********************************************************************
 * About select_tuner
 * 	examples: 	SELECT_TUNER_ALL, SELECT_TUNER_0(A), SELECT_TUNER_1(B),
 *				SELECT_TUNER_2,SELECT_TUNER_3
 *********************************************************************/
	HX_INT8 select_tuner;

#ifdef _USE_EMBEDDED_IVOS
	HX_INT8 use_sort_list;
	// ALL_CH_LIST, CAS, PROVIDER, Alphabet, TRANSPONDER, 
#else
	HX_INT8 reserved;
#endif

	HX_UINT16 bouquet_id;

/********************************************************************
 * About use_group_list
 *
 * 	examples: 	GROUP_LIST_ALL_CH,GROUP_LIST_SATELLITE_CH,GROUP_LIST_GENRE_CH,
 *				GROUP_LIST_PROVIDER_CH,GROUP_LIST_COUNTRY_CH,GROUP_LIST_FTACAS_CH, GROUP_LIST_FAV_CH
 *********************************************************************/
	HX_INT8 use_group_list; 


/********************************************************************
 * About service_type
 *
 * 	examples: SERVICE_TYPE_TV,	SERVICE_TYPE_RADIO, etc
 *********************************************************************/
	HX_UINT8 service_type;

	HX_UINT8 net_index;


/********************************************************************
 * About sub_group_id
 * Select 'use_group_list' for ID
 * examples: provider_info.id, genre_info.id, country_info.id, favorite_group_info.id, etc
 *********************************************************************/

	HX_INT16 sub_group_id;

	struct channel_group_st provider_info[MAX_PROVIDER];
	HX_INT16 provider_number;

	struct channel_group_st genre_info[MAX_GENRE];
	HX_INT16 genre_number;

	struct channel_group_st country_info[MAX_COUNTRY];
	HX_INT16 country_number;

	struct channel_group_st favorite_group_info[MAX_FAV];
	HX_INT8 favorite_group_number;

/********************************************************************
 * About vch_type
 * 	examples: VINDEX_TYPE_DEFAULT, VINDEX_TYPE_TV , VINDEX_TYPE_RADIO, 
 				VINDEX_TYPE_USER ,FAV...
 *********************************************************************/

	HX_INT16 vch_type; 		/* 	0 	: default original_vch 
								1   : digital television
								2	: digital radio sound 
								3	: USER  				
								10 ~ 59 : fav_vch 
							*/
/********************************************************************
 * About *vch
 * select 'virtual_channel_st'
 * 	examples: original_vch, tv_vch , radio_vch, favorite_vch[i],user_vch
 *********************************************************************/

	struct virtual_channel_st *vch; //current list
	HX_INT16 *vnumber;

	struct virtual_channel_st original_vch[MAX_CHANNEL];
	HX_INT16 original_vnumber;

	struct virtual_channel_st tv_vch[MAX_TV_CHANNEL];
	HX_INT16 tv_vnumber;

	struct virtual_channel_st radio_vch[MAX_RADIO_CHANNEL];
	HX_INT16 radio_vnumber;

	struct virtual_channel_st favorite_vch[MAX_FAV][MAX_FAV_NUM];
	HX_INT16 favorite_vnumber[MAX_FAV];

	struct virtual_channel_st user_vch[MAX_CHANNEL];
	HX_INT16 user_vnumber;

	struct virtual_channel_st promo_vch[MAX_PROMO_CHANNEL];
	HX_INT16 promo_vnumber;

	HX_UINT16 network_num;
	struct network_ch_st network;

}stChannelList;

/********************************************************************************************
  RESERVED SDK 						
********************************************************************************************/

#define CHECK_30SEC                     (30)
#define CHECK_1MIN                      (60)
#define CHECK_5MIN                      (300)
#define CHECK_1HOUR                     (3600)
#define CHECK_1DAY                      (86400)
#define CHECK_5DAY                      (432000)
#define CHECK_10DAY                     (864000)
#define CHECK_14DAY                     (1209600)
#define CHECK_30DAY                     (2592000)
#define CHECK_60DAY                     (5184000)

#define RES_CHECK_RUNTIME				10 //10sce

//reserved status 


#define RES_STATUS(i) \
	(i == RES_STA_NONE)?"RES_STA_NONE":\
    (i == RES_STA_RECORD)?"RES_STA_RECORD":\
    (i == RES_STA_RECORDING)?"RES_STA_RECORDING":\
    (i == RES_STA_CANCEL)?"RES_STA_CANCEL":\
    (i == RES_STA_FINISHED)?"RES_STA_FINISHED":\
    (i == RES_STA_VIEW)?"RES_STA_VIEW":\
    (i == RES_STA_VIEWING)?"RES_STA_VIEWING":\
    (i == RES_STA_LOCK)?"RES_STA_LOCK":"NONE"



#define RESERVE_TYPE_VIEW		0x01
#define RESERVE_TYPE_REC		0x02
#define MAX_FILE_PATH_SIZE              128
#define MAX_FILE_NAME_SIZE              128

typedef struct reserved_info_st {

	struct list_head list;

#define RES_STA_NONE		0
#define RES_STA_RECORD		1
#define RES_STA_RECORDING	2
#define RES_STA_CANCEL		3
#define RES_STA_FINISHED	4
#define RES_STA_VIEW		5
#define RES_STA_VIEWING		6
#define RES_STA_LOCK		7

   int    status;					// current status

	short  major;                   // Do not use
	short  minor;                   // Do not use

	short  ch_name_len;
	char   ch_name[MAX_FILE_NAME_SIZE];

	short  title_len;
	char   title[MAX_FILE_NAME_SIZE];       // record info title size is 128
	
#define REC_DEV_NONE		0
#define REC_DEV_HDD			1
#define REC_DEV_USB			2
	int    rec_devices_type;

	char   rec_path[MAX_FILE_PATH_SIZE]; //device name
    char   rec_file[MAX_FILE_NAME_SIZE]; //full path : device + file path +.tmp
    time_t real_start;
    time_t preserve; //Do net use

	unsigned char reservation_flag;
	unsigned char reservation_type;
	unsigned char reservation_mode;

    int    mode;

#define RES_REPEAT_ONE		0
#define RES_REPEAT_WEEKLY	1
    int    repeat;			

    int    mask;				//bit-mask: 0=SUN, 1=MON, 2=TUE, 3=WED, 4=THU, 5=FRI, 6=SAT

//	=======================

	HX_UINT8	net_index;
	HX_UINT32	freq;
	unsigned short original_network_id;
	unsigned short transport_stream_id;
	unsigned short polar;
	unsigned short service_id;
	unsigned short event_id;
	time_t start_time; //reserved start time
	time_t duration;
	unsigned short version;
	HX_UINT8	tuner_id; // 0: A, 1: B

////		Do not use ////////////////////
	unsigned char free_CA_mode; 

	/* content descriptor */
	unsigned char nibble_level_1;
	unsigned char nibble_level_2;
	unsigned char user_nibble_level_1;
	unsigned char user_nibble_level_2;

	/* parental rating descriptor*/
	unsigned char parental_rating;

	/* short event descriptor */
	unsigned char event_lang[3];

	char tv_type;
////////////////////////////////////////

}stReservedInfo;

typedef struct reserved_list_st {

	unsigned long list_crc;
	int version;
	int total_count;

	int record_cnt; //Do not use
	int reserved_cnt;

	int max_record_cnt;//Do not use
	int max_reserved_cnt;

	int cur_cnt;

	struct reserved_info_st info;
}stReservedList;

typedef struct section_map_st {
	HX_INT32 filter_index;
	HX_UINT16 pid;
	HX_UINT8 table_id;
	HX_UINT16 table_id_ext;
	HX_INT16 version_number;
	HX_INT16 last_section_number;
	HX_UINT8 section_done[32]; // 2 ^ 8 = 256 -> 256/8 = 32

	HX_INT16 done;

	HX_UINT32 timeout;
	HX_UINT32 start_time;
}stSectionMap;


#define TUNER_COMBINATION_COMBO 0
#define TUNER_COMBINATION_TWIN  1

#define TUNER_NONE_NAME         "TUNER_NONE"
#define TUNER_ATSC_NAME         "TUNER_ATSC"
#define TUNER_DVBT_NAME         "TUNER_DVBT"
#define TUNER_DVBS_NAME         "TUNER_DVBS"
#define TUNER_ISDBT_NAME        "TUNER_ISDBT"
#define TUNER_DVBC_NAME         "TUNER_DVBC"


typedef struct tuner_info_st {

// type : DTV_NONE, DTV_ATSC, DTV_DVBS, DTV_DVBT, DTV_CIPTV, DTV_ARIB, DTV_ISDBT, DTV_DVBC
	HX_INT type;

	HX_INT status;
	HX_INT tuner_id;
	HX_UINT8 net_index;
	HX_UINT32 freq;
	HX_UINT8 polar;
}stTunerInfo;

#define FIND_TUNER_A 1
#define FIND_TUNER_B 2

typedef struct tuner_list_st {
	HX_INT combination_type;
	struct tuner_info_st tuner[MAX_TUNER_COUNT];
	char find_tuner; //A:0x01, B:0x02
	char count;
}stTunerList;

typedef void (*filterCallback) (HX_UINT tid, HX_UINT16 pid, HX_UINT8 *pBuffer, HX_UINT size, HX_INT32 param1, HX_INT32 param2);


#define EIT_EVENT_NAME_LAN 255
#define EIT_EVENT_TEXT_LAN 512

typedef struct component_info_st {
	char stream_content;
	char component_type;
}stComponentInfo;

typedef struct eit_info_st {
	HX_UINT8 net_index;
	HX_UINT32 freq;
	HX_UINT8 polar;
	HX_UINT16 service_id;

	HX_UINT16 net_id;
	HX_UINT16 ts_id;
	HX_UINT16 event_id;
	HX_UINT16 table_id;
	HX_UINT16 channel_index;

	time_t start_time;
	time_t duration;
	HX_UINT16 version;

	unsigned char free_CA_mode;

	/* content descriptor */
	unsigned char nibble_level_1;
	unsigned char nibble_level_2;
	unsigned char user_nibble_level_1;
	unsigned char user_nibble_level_2;

	/* parental rating descriptor*/
	unsigned char parental_rating;

	/* short event descriptor */
	unsigned char event_lang[4];

	unsigned short event_name_len;
	unsigned char event_name[EIT_EVENT_NAME_LAN+1]; // UTF8

	unsigned short event_text_len;
	unsigned char event_text[EIT_EVENT_TEXT_LAN+1]; // UTF8

	unsigned short event_detail_len;
	unsigned char event_detail[4096];	// UTF8

	char tv_type;

	HX_UINT8 SIDtype;

	struct component_info_st component_info;
}stEitInfo;

#endif // __AZBOXTVSDK_DEF_H__

