#include "StdAfx.h"
#include "GobiApiSar.h"
#include "FuncList.h"
#include "StatusCtrl.h"
#include "SwiWwanCmApi.h"

#define PARAM_RfSarState			L"RF SAR State"

#define FUNC_SetRfSarState          L"SetRfSarState"
#define FUNC_GetRfSarState          L"GetRfSarState"

CParamList CGobiApiSar::m_sSetRfSarState;

const TableFuncEntry CGobiApiSar::m_TableFunc[] = 
{
	{FUNC_SetRfSarState, FuncSetRfSarState, &m_sSetRfSarState},
    {FUNC_GetRfSarState, FuncGetRfSarState},
    {FUNC_EndOfList}
};

CGobiApiSar::CGobiApiSar( HMODULE hQCDll )
{
    // Initialize the Qualcomm Dll pointer
	m_hQCDll = hQCDll;

    // Define SetRfSarState configuration parameters
    m_sSetRfSarState.Add( PARAM_RfSarState, L"0" );
}

CGobiApiSar::~CGobiApiSar( void )
{
}

BOOL CGobiApiSar::GetAvailFuncList(CFuncList* pFuncList)
{
	TBD_FUNCTION(CGobiApiSar::GetAvailFuncList);

	int iIndex = 0;

	while (m_TableFunc[iIndex].szFuncName.Compare(FUNC_EndOfList))
	{
		pFuncList->Add(L"Specific Absorption Rate", m_TableFunc[iIndex].szFuncName, m_TableFunc[iIndex].pParamList);
		TBD_OUTPUT(TBDCT_INFO, TBDOL_INFORMATION, m_TableFunc[iIndex].szFuncName);
		iIndex++;
	}

	return TRUE;
}

UINT CGobiApiSar::ExecuteFunctions(CFuncList* pFuncList, UINT &nTotalExec)
{
	TBD_FUNCTION(CGobiApiSar::Execute);

	int iIndex = 0;
	m_nNumErrors = 0;
	nTotalExec = 0;

	while (m_TableFunc[iIndex].szFuncName.Compare(FUNC_EndOfList))
	{
		if (pFuncList->Find(m_TableFunc[iIndex].szFuncName))
		{
			// Get list of parameters
			CParamList sParams;
			pFuncList->GetFuncParams(m_TableFunc[iIndex].szFuncName, sParams);

			// execute function
			if (!m_TableFunc[iIndex].pFunc(&sParams))
				// increment error counter
				m_nNumErrors++;

			// increment total functions executed
			nTotalExec++;
		}

		iIndex++;
	}

	return m_nNumErrors;
}

BOOL CGobiApiSar::FuncSetRfSarState(CParamList* pParams /*= NULL*/)
{
	ADDSTATUS(L"FuncSetRfSarState", STATUSMSG_Send);

	BOOL bRet = FALSE;
	
	ULONG nRet = 0;
	CString strTemp;

	ULONG nRfSarState = 0;

	// Get user selected parameters
	if (pParams)
	{
		nRfSarState = _ttoi(pParams->GetValue(PARAM_RfSarState));
	}

	nRet = SetRfSarState(nRfSarState);

	strTemp.Format(L"SetRfSarState - %i - Return: %04d", nRfSarState, nRet);
	ADDSTATUS(strTemp, STATUSMSG_Send);

	if (!nRet)
	{
		bRet = TRUE;
	}
	
	return bRet;
}

BOOL CGobiApiSar::FuncGetRfSarState(CParamList* pParams /*= NULL*/)
{
    UNREFERENCED_PARAMETER(pParams);
	ADDSTATUS(L"FuncGetRfSarState", STATUSMSG_Send);

	BOOL bRet = FALSE;
	CString strTemp;
	ULONG nRfSarState = 0;

	ULONG nRet = GetRfSarState(&nRfSarState);

	strTemp.Format(L"Return: %04d ", nRet);
	ADDSTATUS(strTemp, STATUSMSG_Receive);

	if (!nRet)
	{
        strTemp.Format( L"RF SAR state: %d", nRfSarState );
		ADDSTATUS(strTemp, STATUSMSG_Receive);
	}

	bRet = (nRet == 0)?TRUE:FALSE;

	return bRet;
}