/**
* Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved.
*
* @file al_channel_search.h
*
* @brief Channel search function framework implement
*
*/

#ifndef __AL_CHANNEL_SEARCH_H__
#define __AL_CHANNEL_SEARCH_H__

/**<header files*/
#include "al_basictypes.h"
#include "al_area_info.h"
#include "al_database.h"
#ifdef CONFIG_DTV_SUPPORT
#include "mid_dtv_scan.h"
#endif
#ifdef CONFIG_ATV_SUPPORT
#include "mid_atv_scan.h"
#endif

/**<Macro define*/
#define AL_CS_SUPPORT_GET_FREQ_FROM_NIT		(1 << 0)
#define AL_CS_FILTER_SCRAMBLE_SERVICE		(1 << 1)
#define AL_CS_SUPPORT_STEP_BLIND_SCAN		(1 << 2)
#define AL_CS_SUPPORT_BLIND_SCAN_METHOD_1	(1 << 3)
#define AL_CS_SUPPORT_BLIND_SCAN_METHOD_2	(1 << 4)
#define AL_CS_SUPPORT_BLIND_SCAN_METHOD_3	(1 << 5)
#define AL_CS_SUPPORT_NIT_FREQ_LIST_SCAN	(1 << 6)
#define AL_CS_SUPPORT_DVB_T2_IMMEDIATE_MODE (1 << 7)
#define AL_CS_SUPPORT_DVB_T2_DELAY_MODE     (1 << 8)
#define AL_CS_SUPPORT_SELECT_TP_SCAN_MODE   (1 << 9) //for dvb-s/s2
#ifdef CONFIG_SUPPORT_FAST_SCANNING
#define AL_CS_SUPPORT_M7_FAST_SCAN          (1 << 10) //for dvb-s/s2
#endif


#define AL_CS_SET_SERVICE_COUNT(ser_type, tv, radio, data) do { \
	if (ser_type == AL_RECTYPE_DVBTV) \
	{ \
		tv++; \
	} \
	else if (ser_type == AL_RECTYPE_DVBRADIO) \
	{ \
		radio++;\
	} \
	else if (ser_type == AL_RECTYPE_DVBDATA) \
	{ \
		data++; \
	} \
} while(0);

/**<Type define*/

/**
* @brief dvb-s/s2 scan sequence type define.
*/
typedef enum
{
    AL_CS_DVBSX_S_TO_S2     = 0,
    AL_CS_DVBSX_S2_TO_S     = 1,
    AL_CS_DVBSX_ONLY_S      = 2,
    AL_CS_DVBSX_ONLY_S2     = 3,
    AL_CS_DVBSX_INVALID,
}DVBSX_ScanSequenceMode_e;
#define DVBSX_SCAN_SEQUENCE_MODE AL_CS_DVBSX_S_TO_S2


/**
* @brief channel search type define.
*/
typedef enum
{
	AL_CS_TYPE_MIN = -1,
	AL_CS_TYPE_ANALOG, /**< analog auto search */
	AL_CS_TYPE_ANALOG_MANUAL, /**<  analog manual search */
	AL_CS_TYPE_DIGITAL, /**< digital auto search or istallation */
	AL_CS_TYPE_DIGITAL_MANUAL, /**< digital manual search */
	AL_CS_TYPE_DIGITAL_BLIND,	/**<  dvbs digital blind search */
#ifdef CONFIG_SUPPORT_FAST_SCANNING
	AL_CS_TYPE_DIGITAL_M7_FAST, /**<  dvbs digital M7 Fast scan */
#endif
	AL_CS_TYPE_ACI,	/**< auto channel installation */

	AL_CS_TYPE_ANALOG_TABLE,

	AL_CS_TYPE_MAX
} al_cs_type_en;

/**
* @brief channel search notify uplayer message type.
*/
typedef enum
{
	AL_CS_MSG_TYPE_NONE = 0,
	AL_CS_MSG_TYPE_NEW_BAND, /**< blind scan next band */
	AL_CS_MSG_TYPE_NEW_CH, /**< Lock next channel */
	AL_CS_MSG_TYPE_LOCK_CH, /**< Current channel locked */
	AL_CS_MSG_TYPE_FINISHED, /**< Channel search finished */

	CS_MSG_TYPE_ABORT ,
	CS_MSG_TYPE_PAUSE,
	CS_MSG_TYPE_RESUME,
} al_cs_msg_type_en;

/**
* @brief tuner type define.
*/
typedef enum
{
	AL_CS_TUNER_QPSK = 0, /**< QPSK type */
	AL_CS_TUNER_QAM, /**< QAM type, for DVB_C */
	AL_CS_TUNER_OFDM, /**< OFDM type, for DVB_T */
	AL_CS_TUNER_TDS_OFDM, /**< TDS_OFDM type, for DVB_T */
	AL_CS_TUNER_BST_OFDM, /**< BST_OFDM type, for SBTVD */
	AL_CS_TUNER_MAX
} al_tuner_type_en;

/**
* @brief DVB_C modulation type.
*/
typedef enum
{
	AL_QAM_AUTO = 0,
	AL_QAM_16,
	AL_QAM_32,
	AL_QAM_64,
	AL_QAM_128,
	AL_QAM_256,
	AL_QAM_MAX
} al_qam_modulation_en;

/**
* @brief DVB_C Search Mode type.
*/
typedef enum
{
	AL_DVBC_SEARCHMODE_FULL = 0,
	AL_DVBC_SEARCHMODE_FULL_NETWORK,
	AL_DVBC_SEARCHMODE_QUICK,
	AL_DVBC_SEARCHMODE_QUICK_NETWORK,
	AL_DVBC_SEARCHMODE_MAX
} al_DVBC_SearchMode_en;

/**
* @brief DVB_C Operator type.
*/
typedef enum
{
	AL_DVBC_OPERATOR_AUTO = 0,
	AL_DVBC_OPERATOR_YOU_SEE,
	AL_DVBC_OPERATOR_UNITY_MEDIA,
	AL_DVBC_OPERATOR_KDG,
	AL_DVBC_OPERATOR_KBW,
	AL_DVBC_OPERATOR_COMHEM,
	AL_DVBC_OPERATOR_ZIGGO,
	AL_DVBC_OPERATOR_TELIA_STOFA,
	AL_DVBC_OPERATOR_MAX
} al_DVBC_Operator_en;

/**
* @brief DVB_S ploarity  type.
*/
typedef enum
{
	VERTICAL = 0,
	HORIZONTAL,
	BOTH_H_V,
	POLAR_MAX,
} al_polarity_en;

/**
* @brief DVB_S service  type.
*/
typedef enum
{
	ALL_SER = 0,
	FREE_SER,
	SER_MAX,
} al_service_type_en;


/**
* @brief DVB_S channel  type.
*/
typedef enum
{
	ALL_CHN = 0,
	TV_ONLY,
	CHAN_MAX,
} al_channel_type_en;

/**
* @brief Scan mode for DVB_T2
*/
typedef enum
{
    AL_SCAN_DVB_T_T2_DEFAULT_MODE   = (0),//default

    AL_SCAN_DVB_T2_NOT_SUPPORT      = (1<<0), //not support dvb-t2

    AL_SCAN_ONLY_DVBT               = (1<<1), //current scanning dvb-t
    AL_SCAN_ONLY_DVBT2              = (1<<2), //current scanning dvb-t2

    AL_SCAN_DVB_T_T2_TOGETHER_MODE  = (1<<3),
    AL_SCAN_TOGETHER_DVBT           = (1<<4), //current scanning dvb-t(scan dvb-t/t2 same time)
    AL_SCAN_TOGETHER_DVBT2          = (1<<5), //current scanning dvb-t2(scan dvb-t/t2 same time)

    AL_SCAN_DVB_T_T2_SEPARATE_MODE  = (1<<6),
    AL_SCAN_SEPARATE_DVBT           = (1<<7), //current scanning dvb-t(scan dvb-t/t2 separate time)
    AL_SCAN_SEPARATE_DVBT2          = (1<<8), //current scanning dvb-t2(scan dvb-t/t2 separate time)

    AL_SCAN_INVALID                 = (1<<9), //INVALID
} AL_ScanDVB_T2_mode_t;

#ifdef CONFIG_ATV_SUPPORT
/**
* @brief video format supported.
*/
typedef enum
{
 	AL_ANALOG_SCAN_VIDEO_FORMAT_AUTO    = ANA_SCAN_VIDEO_FORMAT_AUTO,
	AL_ANALOG_SCAN_VIDEO_FORMAT_NTSC358 = ANA_SCAN_VIDEO_FORMAT_NTSC358,
	AL_ANALOG_SCAN_VIDEO_FORMAT_NTSC443 = ANA_SCAN_VIDEO_FORMAT_NTSC443,
	AL_ANALOG_SCAN_VIDEO_FORMAT_PAL     = ANA_SCAN_VIDEO_FORMAT_PAL,
	AL_ANALOG_SCAN_VIDEO_FORMAT_PALM    = ANA_SCAN_VIDEO_FORMAT_PALM,
	AL_ANALOG_SCAN_VIDEO_FORMAT_PALCN   = ANA_SCAN_VIDEO_FORMAT_PALCN,
	AL_ANALOG_SCAN_VIDEO_FORMAT_PAL60   = ANA_SCAN_VIDEO_FORMAT_PAL60,
	AL_ANALOG_SCAN_VIDEO_FORMAT_SECAM   = ANA_SCAN_VIDEO_FORMAT_SECAM,
	AL_ANALOG_SCAN_VIDEO_FORMAT_SECAML  = ANA_SCAN_VIDEO_FORMAT_SECAML,
} AL_ScanVideoFormat_t;

/**
* @brief audio format supported.
*/
typedef enum
{
	AL_ANALOG_SCAN_AUDIO_STD_NULL = 0 ,
	AL_ANALOG_SCAN_AUDIO_STD_M 	= (1 << 0),
	AL_ANALOG_SCAN_AUDIO_STD_BG = (1 << 1),
	AL_ANALOG_SCAN_AUDIO_STD_DK = (1 << 2),
	AL_ANALOG_SCAN_AUDIO_STD_I 	= (1 << 3),
	AL_ANALOG_SCAN_AUDIO_STD_L 	= (1 << 4),
	AL_ANALOG_SCAN_AUDIO_STD_L1 = (1 << 5),
} AL_ScanAudioStd_t;

#endif

/**
* @brief channel search input parameter strunction define.
*/
typedef struct
{
	al_cs_type_en	enCsType; /**< Channel search type */

	al_uint32		uiStartFreq; /**< Start frequency */
	al_uint32		uiCurrentFreq; /**< current frequency  for ATV */
	al_uint32		uiEndFreq;	/**< End frequency */
	AL_BandWidth_Info_st		stBandwidth; /**< Band width */

	union
	{
#ifdef CONFIG_ATV_SUPPORT
		struct
		{
			al_uint32		uiDirection; /**< Direction type for analog */
			al_uint32       uiScanCnt;
			al_uint32      *puiScanTable;
			al_uint8		ucAreaIdx; 	/*ATV area*/
			al_uint8		ucBlueScreen; /*Blue Screen*/
			AL_ScanVideoFormat_t VideoFormat;
			AL_ScanAudioStd_t AudioStd;
			al_uint32 Manual_Num;
			bool Manual_Scan;
		} atv;
#endif
#ifdef CONFIG_DTV_SUPPORT
		struct
		{
			struct
			{
					al_uint32 uiSymRate; /**< sym rate for dvb_c */
					al_uint32 network_id; /**< network id */
					al_qam_modulation_en enModulation; /**< modulation type for dvb_c */
					al_DVBC_SearchMode_en uiDVBCSearchMode; /**< Search mode for dvb_c */
					al_uint8 uiNetworkIdAutoEn; /**< User set network id is auto or not for dvb_c */
					al_uint8 uiFreqAutoEn; /**< User set freq is auto or not for dvb_c */
					al_uint32 uiNetworkScanFreq; /**< User set freq to start when network id search for dvb_c */
			} qam_info;

			struct
			{
					al_uint8 u8FreqStep;
					al_uint8 u8Polarization;
					al_uint32 uiSymRate; /**< sym rate for dvb_s */
					AL_RecHandle_t hNet; /**< satellite  handle */
                    al_uint32 uiFeType; /**< fe type: DVBSX_ScanSequenceMode_e */
			} qpsk_info;

			struct
			{
					al_uint32 FreqlistHdl; /**< Freq list handle derived from frequency_list_descriptor */
					AL_ScanDVB_T2_mode_t	dvbt2mode; /** <for DVB-T2 mode */
			} ofdm_info;

			const void* pstScanTable; /**< current default area table for dtv*/
			al_tuner_type_en enTunerType; /**< tuner type for dtv, TUNER_QPSK/TUNER_QAM/TUNER_OFDM*/
			al_uint8	ucAreaIdx; /**< area index for dtv*/
			AL_ScanDVB_T2_mode_t	dvbt2mode; /** <for DVB-T2 mode */
#ifdef CONFIG_SUPPORT_FAST_SCANNING
			al_uint16	uiM7FstPid;
			al_bool 	bM7CheckVersion;
#endif
		} dtv;
#endif
	} u;

	al_uint32 		uiCapabilities; /**< Capabilities select*/
} al_cs_input_params_st;

/**
* @brief atv channel search infomation.
*/
typedef struct
{
	al_uint16 usCurChaInx; /**< current search channel index */
	al_uint16 usCurChaCnt; /**< current channel number have be connected */
} al_cs_atv_info_st;

/**
* @brief dtv channel search infomation.
*/
typedef struct
{
	al_uint8 ucTotalBand; /**< total band number at DTV */
	al_uint8 ucCurBand; /**< current band number at DTV */
	al_uint16 usRadioCnt; /**< current radio number at DTV */
	al_uint16 usDataServCnt; /**< current data service number have searched at DTV */
	al_uint16 usTotalTpNum; /**< total TP number at current DTV area table */
	al_uint16 usCurTpCnt; /**< current TP number have searched at DTV */
	al_uint32 uiProgress; /**< current band process at DTV */
} al_cs_dtv_info_st;

/**
* @brief channel search infomation.
*/
typedef struct
{
	al_uint32 uiCurFreq; /**< current frequency */
	al_uint32 uiCurBandwidth; /**< current bandwidth */
	al_uint16 usTVCnt; /**< total channel number should be connected at ATV or total TV number have be connected at DTV */

	union
	{
		al_cs_atv_info_st atv; /**< atv channel search infomation */
		al_cs_dtv_info_st dtv; /**< dtv channel search infomation */
	} u;

} al_cs_search_info_st;

/**
* @brief Call back function prototype for uplayer to register.
*/
typedef AL_Return_t (*al_cs_search_save_st)(const void* pstCurScanInfo, const void* pstChannelInfo, void* pstCurScanResult);
typedef AL_Return_t (*al_cs_search_over_st)(void);
typedef AL_Return_t (*al_cs_search_start_st)(void);
typedef AL_Return_t (*al_cs_search_update_st)(al_cs_msg_type_en, const al_cs_search_info_st*);
typedef al_DVBC_Operator_en (*al_cs_search_query_operator)(void);

#ifdef CONFIG_DVB_SYSTEM_DVBS_SUPPORT
al_int32 AL_CS_DTV_GetStartEndFreq(AL_RecHandle_t hNet, al_uint16 usLNBFreq, al_uint32 *puiStartFreq, al_uint32 *puiEndFreq);
al_uint32 AL_CS_DVBSX_InitScanFEType(al_uint32 fe_type);
#endif

/**
* @brief Start the channel search function.
*
* @param input_params[input] the start params of channel search function.
*
* @param search_start[input] Initilize callback function called.
*
* @param search_over[input] Uninitilize callback function.
*
* @param search_update[input] Update callback function used to update UI display.
*
* @param search_save[input] save program into database function.
*
* @return AL_SUCCESS start channel search is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_CS_Start(al_cs_input_params_st* input_params,
	al_cs_search_start_st search_start,
	al_cs_search_over_st search_over,
	al_cs_search_update_st search_update,
	al_cs_search_save_st search_save);

/**
* @brief Set the search direction mainly used in ATV.
*
* @param direction[input] zero for positive, or other for negative.
*
* @return AL_SUCCESS set search direction is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_CS_SetSearchDirection(al_int32 direction);

/**
* @brief Send message to stop the channel search flow.
*
* @return AL_SUCCESS stop channel search is successful, AL_FAILURE otherwise.
*/
AL_Return_t AL_CS_Stop(void);

/**
* @brief Send message to pause the search flow.
*
* @return AL_SUCCESS channel search pause is success, AL_FAILURE otherwise.
*/
AL_Return_t AL_CS_Pause(void);

/**
* @brief Send message to resume the search flow.
*
* @return AL_SUCCESS channel search resume is success, AL_FAILURE otherwise.
*/
AL_Return_t AL_CS_Resume(void);

/**
* @brief Is the channel search module stop or not.
*
* @return al_true channel search is stoped, al_false channel search is not stoped.
*/
al_bool AL_CS_IsStop(void);
al_bool AL_CS_IsStopFinish(void);

al_uint16 AL_CS_AtvCurIdx(void);
#ifdef CONFIG_SUPPORT_ATV_SCAN_NTSCM_PALM_N

al_bool AL_CS_UpdateParam(al_cs_input_params_st* input_params,al_uint32 i);
#endif
#ifdef CONFIG_DTV_SUPPORT
/*****************************************************************************
** FUNCTION : AL_CS_DtvMinBW
**
** DESCRIPTION : Get BW of special area
**
**
** PARAMETERS :
**              stBW - BW struct
**				pbBW - BW
**
** RETURN VALUES:
**				AL_FAILURE - operation failure
**				AL_SUCCESS - operation success
*****************************************************************************/
AL_Return_t AL_CS_DtvMinBW(AL_BandWidth_Info_st stBW, UINT8 *pbBW);

/*****************************************************************************
** FUNCTION : AL_CS_DtvMaxBW
**
** DESCRIPTION : Get BW of special area
**
**
** PARAMETERS :
**              stBW - BW struct
**				pbBW - BW
**
** RETURN VALUES:
**				AL_FAILURE - operation failure
**				AL_SUCCESS - operation success
*****************************************************************************/
AL_Return_t AL_CS_DtvMaxBW(AL_BandWidth_Info_st stBW, UINT8 *pbBW);

/*****************************************************************************
** FUNCTION : AL_CS_DtvNextBW
**
** DESCRIPTION : Get BW of special area
**
**
** PARAMETERS :
**              stBW - BW struct
**				pbBW - BW
**              bDirect - search direct
**
** RETURN VALUES:
**				AL_FAILURE - operation failure
**				AL_SUCCESS - operation success
*****************************************************************************/
AL_Return_t AL_CS_DtvNextBW(AL_BandWidth_Info_st stBW, UINT8 *pbBW, INT8 bDirect);

/*****************************************************************************
** FUNCTION : AL_CS_DtvSetSpecialSpec
**
** DESCRIPTION : set special spec
**
**
** PARAMETERS :
**              ePriSpec - DVB private special spec
**
** RETURN VALUES:
**				AL_FAILURE - operation failure
**				AL_SUCCESS - operation success
*****************************************************************************/
AL_Return_t AL_CS_DtvSetSpecialSpec(AL_DBPrivateSpec_t ePriSpec);
#endif
#ifdef SUPPORT_LCN
DBLCNConfInfo_t* AL_LCN_GetLCNConfChannel(al_void);

bool AL_LCN_ProcessAndJudgeWhetherPopToUser(AL_DB_EDBType_t eNetType, al_uint8 u8Country, al_bool bForceAutoProc);
#endif
al_uint32 AL_CS_GetCurFreq(void);
al_uint16 AL_CS_GetAtvCurChannel(void) ;

al_void AL_CS_SetOperatorQueryFunc(al_cs_search_query_operator search_query_op);

al_uint32 AL_CS_GetCurPolar(void);
al_uint32 AL_CS_GetCurSymbolRate(void);
#endif	/* __AL_CHANNEL_SEARCH_H__ */
/*****************************************************************************
** $Rev$
**
*****************************************************************************/
