/******************************************************************************
 ** File Name : al_multiplex.c
 **
 ** Description:	Gives the functionalities for quering as well as manipulating the
 **					middleware database on network related data.
 **
** Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved
**
** Author : kun.dai
**
** $Id$
 ******************************************************************************/
/******************************************************************************
 *	Include Files
 ******************************************************************************/
#include "al_multiplex.h"
#include "ai_database.h"

/******************************************************************************
 *	Global Variables
 ******************************************************************************/
static al_bool g_bInitFlag = al_false;

/******************************************************************************
 *	Macros
 ******************************************************************************/
#define MULTIPLEX_CHECKTYPE(type, typeEnum) ((typeEnum) == ((type) & (typeEnum)))

/******************************************************************************
 *	Service Component APIs
 ******************************************************************************/
/****************************************************//**
 * @brief Initialize Multiplex component.
 *
 * The implementation may perform appropriate Multiplex initialisation when this
 * call is made, if required.
 *
 * @note
 *		1.This API must be called before any multiplex-related function is called; <br>
 * 		2.Must be called once successfully, and still return success when called again.
 *
 * @param eDBType [in]select one or more database to initialize multiplex component.
 *
 * @return
 * 	#AL_SUCCESS indicates success,
 * 	On failure, #AL_FAILURE is returned.
 ********************************************************/
AL_Return_t AI_Multiplex_Init(AL_DB_EDBType_t eDBType)
{
	if (!g_bInitFlag)
	{
		g_bInitFlag = al_true;
	}

	return AL_SUCCESS;
}


/****************************************************//**
 * @brief Terminate Multiplex commponent.
 *
 * The implementation may perform appropriate Multiplex termination when this
 * call is made, if required.
 *
 * @note
 * 		1.This API must be called after any multiplex-related function is called, <br>
 * 		2.Must be called once successfully, and still return success when called again.
 *
 * @param eDBType [in]select one or more database to terminate component.
 *
 * @return
 * 	#AL_SUCCESS indicates success,
 * 	On failure, #AL_FAILURE is returned.
 *
 ********************************************************/
AL_Return_t AI_Multiplex_Term(AL_DB_EDBType_t eDBType)
{
	if (g_bInitFlag)
	{
		g_bInitFlag = al_false;
	}

	return AL_SUCCESS;
}

/****************************************************//**
 * @brief Match multiplex by specified search field.
 *
 * @note Refers to the prototype #AL_DB_RecordMatchFunc.
 *
 * @param param		[in]Search key, type: #AL_Multiplex_SearchKey_t.
 * @param pRec		[in]Multiplex Info.
 *
 * @return
 * 	#AL_SUCCESS indicates success,
 * 	On failure, #AL_FAILURE is returned.
 ********************************************************/
al_bool AL_Multiplex_MatchRecBySearchKey(al_void *param, al_void const*pRec)
{
	if (al_null == param || al_null == pRec)
	{
		return al_false;
	}

	AL_Multiplex_SearchKey_t stKey;

	memset(&stKey, 0, sizeof(AL_Multiplex_SearchKey_t));
	memcpy(&stKey, param, sizeof(AL_Multiplex_SearchKey_t));

	if (AL_DBTYPE_DVB_T == stKey.eDBType)
	{
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_TSID)
			&& (stKey.u16TsId != ((STDBMultiplexInfo_t *)pRec)->usTsId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_NETID)
			&& (stKey.u16NetId != ((STDBMultiplexInfo_t *)pRec)->usNetId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_FREQ)
			&& (stKey.u32Frequency != ((STDBMultiplexInfo_t *)pRec)->uiFreqK))
		{
			return al_false;
		}
		return al_true;
	}
	else if (AL_DBTYPE_DVB_C == stKey.eDBType)
	{
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_TSID)
			&& (stKey.u16TsId != ((SCDBMultiplexInfo_t *)pRec)->usTsId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_NETID)
			&& (stKey.u16NetId != ((SCDBMultiplexInfo_t *)pRec)->usNetId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_FREQ)
			&& (stKey.u32Frequency != ((SCDBMultiplexInfo_t *)pRec)->uiFreqK))
		{
			return al_false;
		}
		return al_true;
	}
	else if (AL_DBTYPE_DVB_S == stKey.eDBType)
	{
		//if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_TSID)
		//	&& (stKey.u16TsId != ((SDBMultiplexInfo_t *)pRec)->usTsId))
		//{
		//	return al_false;
		//}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_NETID)
			&& (stKey.u16NetId != ((SDBMultiplexInfo_t *)pRec)->usNetId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_FREQ)
			&& (stKey.u32Frequency != ((SDBMultiplexInfo_t *)pRec)->usFreqM))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_SYMRATE)
			&& (stKey.u32SymbolRate != ((SDBMultiplexInfo_t *)pRec)->usSymRateK))
		{
			return al_false;
		}
		return al_true;
	}
	#ifdef CONFIG_ISDB_SYSTEM
	if (AL_DBTYPE_DVB_SBTVD == stKey.eDBType)
	{
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_TSID)
			&& (stKey.u16TsId != ((SSBTVDDBMultiplexInfo_t *)pRec)->usTsId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_NETID)
			&& (stKey.u16NetId != ((SSBTVDDBMultiplexInfo_t *)pRec)->usNetId))
		{
			return al_false;
		}
		if (MULTIPLEX_CHECKTYPE(stKey.eSearchField, AL_MULTIPLEX_SEARCHFIELD_FREQ)
			&& (stKey.u32Frequency != ((SSBTVDDBMultiplexInfo_t *)pRec)->uiFreqK))
		{
			return al_false;
		}
		return al_true;
	}
	#endif

	return al_false;
}


