#include "StdAfx.h"
#include "GobiApiCat.h"
#include "FuncList.h"
#include "StatusCtrl.h"
#include <GobiAPI.h>
#include "GobiConnectionMgmtApi.h"

#define FUNC_CATSendTerminalResponse	L"CATSendTerminalResponse"
#define FUNC_CATSendEnvelopeCommand		L"CATSendEnvelopeCommand"

#define FUNC_SetCATEventCallback		L"SetCATEventCallback"

#define PARAM_UimRefId				    L"UIM Ref ID"
#define PARAM_TRLen  					L"Terminal Response Length"
#define PARAM_TerminalResponse 			L"Terminal Response"

#define PARAM_EnvCmd					L"Envelope Command"
#define PARAM_EnvCmdLen					L"Envelope Command Length"
#define PARAM_EnvCmdData				L"Envelope Command"

#define PARAM_EventMask					L"Event Mask"
#define PARAM_Enable					L"Enable"

CParamList CGobiApiCat::m_sCATSendTerminalResponse;
CParamList CGobiApiCat::m_sCATSendEnvelopeCommand;
CParamList CGobiApiCat::m_sCATEventCallback;

const TableFuncEntry CGobiApiCat::m_TableFunc[] = 
{
	{FUNC_CATSendTerminalResponse, FuncCATSendTerminalResponse, &m_sCATSendTerminalResponse},
	{FUNC_CATSendEnvelopeCommand, FuncCATSendEnvelopeCommand, &m_sCATSendEnvelopeCommand},
	{FUNC_SetCATEventCallback, FuncSetCATEventCallback, &m_sCATEventCallback},
	{FUNC_EndOfList}
};

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

	// Define Send Terminal Response configurable parameters
	m_sCATSendTerminalResponse.Add(PARAM_UimRefId, L"0");
	m_sCATSendTerminalResponse.Add(PARAM_TRLen, L"0");
	m_sCATSendTerminalResponse.Add(PARAM_TerminalResponse, L"0");

	// Define Send Envelope Command configurable parameters
	m_sCATSendEnvelopeCommand.Add(PARAM_EnvCmd, L"0");
	m_sCATSendEnvelopeCommand.Add(PARAM_EnvCmdLen, L"0");
	m_sCATSendEnvelopeCommand.Add(PARAM_EnvCmdData, L"0");

	// Define CAT Event Callback configurable parameters
	m_sCATEventCallback.Add(PARAM_EventMask, L"0");	
	m_sCATEventCallback.Add(PARAM_Enable, L"1");
}

CGobiApiCat::~CGobiApiCat(void)
{
}

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

	int iIndex = 0;

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

	return TRUE;
}

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

	BOOL bRet = FALSE;

	ULONG UimRefId;
	ULONG TRlen;
	CHAR  TerminalResponse[256];

	memset (TerminalResponse, '0', 256);

	if (pParams)
	{
		UimRefId = _ttoi(pParams->GetValue(PARAM_UimRefId));
		TRlen = _ttoi(pParams->GetValue(PARAM_TRLen));
	    WideCharToMultiByte(CP_ACP, 0, pParams->GetValue(PARAM_TerminalResponse), -1, TerminalResponse, TRlen, 0, 0);
	}

	ULONG nRet = CATSendTerminalResponse(UimRefId, TRlen, (BYTE *)TerminalResponse);

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

	if (!nRet)
	{
		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;
	CString strTemp;

	ULONG EnvCmd;
	ULONG EnvCmdLen;
	CHAR  EnvCmdData[256];

	if (pParams)
	{
		EnvCmd = _ttoi(pParams->GetValue(PARAM_EnvCmd));
		EnvCmdLen = _ttoi(pParams->GetValue(PARAM_EnvCmdLen));
	    WideCharToMultiByte(CP_ACP, 0, pParams->GetValue(PARAM_EnvCmdData), -1, EnvCmdData, EnvCmdLen, 0, 0);
	}

	ULONG nRet = CATSendEnvelopeCommand(EnvCmd, EnvCmdLen, (BYTE *)EnvCmdData);

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

	if (!nRet)
	{
		bRet = (nRet == 0)?TRUE:FALSE;
	}

	return bRet;
}


// CAT Callback tests

static void GOBICALLBACK CATEventCallback(ULONG eventID, ULONG eventLen, BYTE * pData)
{
	UNREFERENCED_PARAMETER(pData);
	CString strTemp;
	strTemp.Format(L"Event ID: %d, Event Length: %d", eventID, eventLen);
	ADDSTATUS(strTemp, STATUSMSG_Notif);  
}

BOOL CGobiApiCat::FuncSetCATEventCallback(CParamList* pParams /*= NULL*/)
{
	ADDSTATUS(L"FuncSetCATEventCallback", STATUSMSG_Send);
	ULONG ret = 0;
	ULONG eventMask = 0;
	ULONG errorMask = 0;

	if (0 != _ttoi(pParams->GetValue(PARAM_Enable)))
	{
		eventMask = _tcstol(pParams->GetValue(PARAM_EventMask), NULL, 16);

		ret = SetCATEventCallback(CATEventCallback, eventMask, &errorMask);
	}
	else
	{
		ret = SetCATEventCallback(NULL, eventMask, &errorMask);
	}

	CString strTemp;
	strTemp.Format(L"Result: %d", ret);

	return ADDSTATUS(strTemp, (0==ret)?STATUSMSG_Receive:STATUSMSG_Error);
}
