/*! Time-stamp: <@(#)ATBoardTunDVBT2.h   07/02/2011 - 08:00:00   P.Hoogervorst>
*********************************************************************
*  @file   : ATBoardTunDVBT2.h
*
*          : Device supported: AT78, AT788
*
*  Author  : P.Hoogervorst                              Date: 07/05/2007
*
*  Purpose : Implementation of methods for class 
*
*********************************************************************
* Version History:
*
* V 0.10  07/02/2011  BN : First Revision
*
*********************************************************************
*/

/**********************************************************************************************************/
// includes
#include "ATBoardTunDVBT2.h"

/**********************************************************************************************************/
// defines

/**********************************************************************************************************/
// functions
// constructor
CATBoardTunDVBT2::CATBoardTunDVBT2(void)
{
}

// destructor
CATBoardTunDVBT2::~CATBoardTunDVBT2(void)
{
}


/*
 * Do the one time initialize and reset of the tuner. Is done once at startup.
 */
BOOL CATBoardTunDVBT2::Init()
{
	// reset tuner controller
	m_pAtBoard->DvbSource(SRC_RESET, NULL);
	// init tuner controller
	m_pAtBoard->DvbSource(SRC_INIT, NULL);

	return TRUE;
}

/*
 * Set the tuner.
 */
BOOL CATBoardTunDVBT2::Set(u32 nFreq, src_bandwidth_t BandWidth, u8 nPLPId)
{
	dvb_frontend_parameters Info;

	// set parameters
	Info.frequency = nFreq;
	Info.u.ofdm.bandwidth = BandWidth;
	Info.u.ofdm.plp_id = nPLPId;

	// send parameters to device
	return m_pAtBoard->DvbSource(SRC_SET_FRONTEND, &Info);
}

/*
 * Get status from the tuner
 */
BOOL CATBoardTunDVBT2::GetStatus(BOOL &bInSync,
								 BOOL &bLock,
								 src_spectral_inversion_t &Inversion,
								 s32 &nSnr,
								 u32 &nBer)

{
	// set to defaults
	bInSync = FALSE;
	bLock = FALSE;
	nSnr = 0;
	nBer = 0;
	Inversion = INVERSION_OFF;
	
	// get status
	src_status_t eStatus;
	if (!m_pAtBoard->DvbSource(SRC_READ_STATUS, &eStatus))
		return FALSE;

	if (eStatus & SRC_HAS_SYNC)
		bInSync = TRUE;
	else
		bInSync = FALSE;

	if (eStatus & SRC_HAS_LOCK)
		bLock = TRUE;
	else
		bLock = FALSE;

	// get signal to noise ratio
	if (!m_pAtBoard->DvbSource(SRC_READ_SNR, &nSnr))
		return FALSE;

	// get bit error rate (BER)
	if (!m_pAtBoard->DvbSource(SRC_READ_BER, &nBer))
		return FALSE;

	// get modulation status
	dvb_frontend_parameters Info;
	if (!m_pAtBoard->DvbSource(SRC_GET_FRONTEND, &Info))
		return FALSE;

	// get inversion
	Inversion = Info.inversion;

	return TRUE;
}

// Get L1PRE status from the tuner.
BOOL CATBoardTunDVBT2::GetL1PreStatus (sony_dvb_dvbt2_l1pre_t &L1Pre)
{
	if (!m_pAtBoard->DvbSource(SRC_READ_DVBT2_L1PRE, &L1Pre))
		return FALSE;

	return TRUE;
}

// Get L1POST status from the tuner.
BOOL CATBoardTunDVBT2::GetL1PostStatus (sony_dvb_dvbt2_l1post_t &L1Post)
{
	if (!m_pAtBoard->DvbSource(SRC_READ_DVBT2_L1POST, &L1Post))
		return FALSE;

	return TRUE;
}

// Get Tune info from the tuner.
BOOL CATBoardTunDVBT2::GetTuneInfo (sony_dvb_demod_t2_preset_t &PresetInfo)
{
	if (!m_pAtBoard->DvbSource(SRC_READ_DVBT2_TUNEINFO, &PresetInfo))
		return FALSE;

	return TRUE;
}

// Get PLP info from the tuner.
BOOL CATBoardTunDVBT2::GetPLPInfo (sony_dvb_dvbt2_plp_info_t &PLPInfo)
{
	PLPInfo.PLPType = SONY_DVB_DVBT2_PLP_DATA;
	if (!m_pAtBoard->DvbSource(SRC_READ_DVBT2_PLP, &PLPInfo))
		return FALSE;

	return TRUE;
}
