/******************************************************************************
 ** File Name : al_network.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_network.h"
#include "ai_database.h"

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

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

/******************************************************************************
 *	Service Component APIs
 ******************************************************************************/
/****************************************************//**
 * @brief Initialize network component.
 *
 * The implementation may perform appropriate network initialisation when this
 * call is made, if required.
 *
 * @note
 *		1.This API must be called before any network-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 network component.
 *
 * @return
 * 	#AL_SUCCESS indicates success,
 * 	On failure, #AL_FAILURE is returned.
 ********************************************************/
AL_Return_t AI_Network_Init(AL_DB_EDBType_t eDBType)
{
	if (!g_bInitFlag)
	{
		g_bInitFlag = al_true;
	}

	return AL_SUCCESS;
}


/****************************************************//**
 * @brief Terminate network commponent.
 *
 * The implementation may perform appropriate network termination when this
 * call is made, if required.
 *
 * @note
 *		1.This API must be called after any network-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 network component.
 *
 * @return
 * 	#AL_SUCCESS indicates success,
 * 	On failure, #AL_FAILURE is returned.
 ********************************************************/
AL_Return_t AI_Network_Term(AL_DB_EDBType_t eDBType)
{
	if (g_bInitFlag)
	{
		g_bInitFlag = al_false;
	}

	return AL_SUCCESS;
}

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

	AL_Network_SearchKey_t stKey;

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

	if (AL_DBTYPE_DVB_T == stKey.eDBType)
	{
		if (NET_CHECKTYPE(stKey.eSearchField, AL_NET_SEARCHFIELD_NETID)
			&& (stKey.u16NetId != ((STDBNetworkInfo_t *)pRec)->usNetId))
		{
			return al_false;
		}

		return al_true;
	}
	#ifdef CONFIG_ISDB_SYSTEM
	if (AL_DBTYPE_DVB_SBTVD == stKey.eDBType)
	{
		if (NET_CHECKTYPE(stKey.eSearchField, AL_NET_SEARCHFIELD_NETID)
			&& (stKey.u16NetId != ((SSBTVDDBNetworkInfo_t *)pRec)->usNetId))
		{
			return al_false;
		}

		return al_true;
	}
	#endif

	return al_false;
}


