#include "StdAfx.h"
#include <GobiAPI.h>
#include "GobiApiRms.h"
#include "FuncList.h"
#include "StatusCtrl.h"
#include "GobiConnectionMgmtAPI.h"

#define FUNC_GetSMSWake				L"GetSMSWake"
#define FUNC_SetSMSWake				L"SetSMSWake"

#define PARAM_Enabled				L"Enabled"
#define PARAM_WakeMask				L"WakeMask"

CParamList CGobiApiRms::m_sSetSMSWake;

const TableFuncEntry CGobiApiRms::m_TableFunc[] = 
{
	{FUNC_GetSMSWake, FuncGetSMSWake},
	{FUNC_SetSMSWake, FuncSetSMSWake, &m_sSetSMSWake},
	{FUNC_EndOfList}
};

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

	// Define Set SMS Wake configurable parameters
	m_sSetSMSWake.Add(PARAM_Enabled, L"0");
	m_sSetSMSWake.Add(PARAM_WakeMask, L"0");
}

CGobiApiRms::~CGobiApiRms(void)
{
}

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

	int iIndex = 0;

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

	return TRUE;
}

UINT CGobiApiRms::ExecuteFunctions(CFuncList* pFuncList, UINT &nTotalExec)
{
	TBD_FUNCTION(CGobiApiRms::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 CGobiApiRms::FuncGetSMSWake(CParamList* pParams /*= NULL*/)
{
	ADDSTATUS(L"FuncGetSMSWake", STATUSMSG_Send);
	UNREFERENCED_PARAMETER(pParams);

	BOOL bRet = FALSE;
    ULONG bEnabled;
    ULONG wakeMask;

	ULONG nRet = GetSMSWake(&bEnabled, &wakeMask);

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


	if (!nRet)
	{
		strTemp.Format(L"SMS Wake: %s",	bEnabled ? L"Enabled" : L"Disabled");
		ADDSTATUS(strTemp, STATUSMSG_Receive);
		
		if (bEnabled)
		{
			strTemp.Format(L"SMS Wake Mask: %d", wakeMask);
			ADDSTATUS(strTemp, STATUSMSG_Receive);
		}
		else
		{
			strTemp.Format(L"SMS Wake Mask not applicable when Wake is disabled");
			ADDSTATUS(strTemp, STATUSMSG_Receive);
		}

		bRet = TRUE;
	}

	// Compare with QC DLL
	if (m_hQCDll)
	{
		// function prototype
		typedef ULONG (GOBICMAPI*LPFN_GETSMSWAKE) (ULONG*, ULONG*);

		// get pointer to function
		LPFN_GETSMSWAKE pFn = (LPFN_GETSMSWAKE)GetProcAddress_W(m_hQCDll, FUNC_GetSMSWake);

		// execute function
		if (pFn)
		{
			ULONG QcbEnabled;
			ULONG QcwakeMask;

			ULONG nQcRet = pFn(&QcbEnabled, &QcwakeMask);

			if (nQcRet != nRet)
			{
				strTemp.Format(L"QC returned a different status code: %04d", nQcRet);
				ADDSTATUS(strTemp, STATUSMSG_Error);
				bRet = FALSE;
			}
			else if ((QcbEnabled != bEnabled) || ((QcwakeMask != wakeMask)))
			{
				ADDSTATUS(L"Response differs from Qualcomm DLL", STATUSMSG_Error);
				bRet = FALSE;
			}
			else
			{
				ADDSTATUS(L"Response matches Qualcomm DLL", STATUSMSG_Status);
				bRet = TRUE;
			}
		}

	}

	return bRet;
}

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

	BOOL bRet = FALSE;
    ULONG bEnabled;
    ULONG wakeMask;

	// Get user selected parameters
	if (pParams)
	{
		bEnabled = _ttoi(pParams->GetValue(PARAM_Enabled));
		wakeMask = _ttoi(pParams->GetValue(PARAM_WakeMask));
	}

	ULONG nRet = SetSMSWake(bEnabled, wakeMask);

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

	if (!nRet)
	{
		bRet = TRUE;
	}

	// Compare with QC DLL - Since we just did a set, , we do a get here and compare the results.
	if (m_hQCDll)
	{
		// function prototype
		typedef ULONG (GOBICMAPI*LPFN_GETSMSWAKE) (ULONG*, ULONG*);

		// get pointer to function
		LPFN_GETSMSWAKE pFn = (LPFN_GETSMSWAKE)GetProcAddress_W(m_hQCDll, FUNC_GetSMSWake);

		// execute function
		if (pFn)
		{
			ULONG QcbEnabled;
			ULONG QcwakeMask;

			LONG nQcRet = pFn(&QcbEnabled, &QcwakeMask);

			if (nQcRet != nRet)
			{
				strTemp.Format(L"QC returned a different status code: %04d", nQcRet);
				ADDSTATUS(strTemp, STATUSMSG_Error);
				bRet = FALSE;
			}
			else if (QcbEnabled != bEnabled)
			{
				ADDSTATUS(L"Response differs from Qualcomm DLL", STATUSMSG_Error);
				bRet = FALSE;
			}
			else
			{
				ADDSTATUS(L"Response matches Qualcomm DLL", STATUSMSG_Status);
				bRet = TRUE;
			}
		}

	}

	return bRet;
}
