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

#define FUNC_SWIOMADMInitApi				L"SWIOMADMInitApi"
#define FUNC_SWIOMADMShutdownApi			L"SWIOMADMShutdownApi"
#define FUNC_SWIOMADMSessionStart			L"SWIOMADMSessionStart"
#define FUNC_SWIOMADMSessionCancel			L"SWIOMADMSessionCancel"
#define FUNC_SWIOMADMSessionGetInfo			L"SWIOMADMSessinGetInfo"
#define FUNC_SWIOMADMSelection				L"SWIOMADMSelection"
#define FUNC_SWIOMADMGetSettings			L"SWIOMADMGetSettings"
#define FUNC_SWIOMADMSetSettings			L"SWIOMADMSetSettings"
#define FUNC_SWIOMADMSetEventReport			L"SWIOMADMSetEventReport"

#define FUNC_SetSWIOMADMFotaCallback		L"SetSWIOMADMFotaCallback"
#define FUNC_SetSWIOMADMAlertCallback		L"SetSWIOMADMAlertCallback"

#define PARAM_SessionType					L"SessionType"
#define PARAM_SessionID						L"SessionID"
#define PARAM_SessionState					L"SessionState"
#define PARAM_UserInput						L"UserInput"
#define PARAM_Enabled						L"Enabled"
#define PARAM_FWAutoLoad					L"FWAutoLoad"
#define PARAM_FWAutoUpdate					L"FWAutoUpdate"
#define PARAM_Enable						L"Enable"

CParamList CGobiApiSwiOma::m_sSessionsStart;
CParamList CGobiApiSwiOma::m_sSessionsCancel;
CParamList CGobiApiSwiOma::m_sSessionsGetInfo;
CParamList CGobiApiSwiOma::m_sSelection;
CParamList CGobiApiSwiOma::m_sGetSettings;
CParamList CGobiApiSwiOma::m_sSetSettings;
CParamList CGobiApiSwiOma::m_sSetEventReport;
CParamList CGobiApiSwiOma::m_sSetSWIOMADMFotaCallback;
CParamList CGobiApiSwiOma::m_sSetSWIOMADMAlertCallback;

const TableFuncEntry CGobiApiSwiOma::m_TableFunc[] = 
{
	{FUNC_SWIOMADMInitApi, FuncSWIOMADMInitApi, NULL},
	{FUNC_SWIOMADMShutdownApi, FuncSWIOMADMShutdownApi, NULL},
	{FUNC_SWIOMADMSessionStart, FuncSWIOMADMSessionStart, &m_sSessionsStart},
 	{FUNC_SWIOMADMSessionCancel, FuncSWIOMADMSessionCancel, &m_sSessionsCancel},
	{FUNC_SWIOMADMSelection, FuncSWIOMADMSelection, &m_sSelection},
	{FUNC_SWIOMADMGetSettings, FuncSWIOMADMGetSettings, &m_sGetSettings},
	{FUNC_SWIOMADMSetSettings, FuncSWIOMADMSetSettings, &m_sSetSettings},
	{FUNC_SWIOMADMSetEventReport, FuncSWIOMADMSetEventReport, &m_sSetEventReport},
	{FUNC_EndOfList}
};

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

	// Define Session Type configurable parameters
	m_sSessionsStart.Add(PARAM_SessionType, L"0");

	// Define Session cancel configurable parameters
	m_sSessionsCancel.Add(PARAM_SessionType, L"0");

	// Define Session Get Info configurable parameters
	m_sSessionsGetInfo.Add(PARAM_SessionType, L"0");
	m_sSessionsGetInfo.Add(PARAM_SessionState, L"0");

	// Define Selection configurable paramaeters
	m_sSelection.Add(PARAM_UserInput, L"0");

	// Define Set Settings parameters
	m_sSetSettings.Add(PARAM_FWAutoLoad, L"0");
	m_sSetSettings.Add(PARAM_FWAutoUpdate, L"0");

	m_sSetSWIOMADMFotaCallback.Add(PARAM_Enable , L"0");
	m_sSetSWIOMADMAlertCallback.Add(PARAM_Enable, L"0");
}

CGobiApiSwiOma::~CGobiApiSwiOma(void)
{
}

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

	int iIndex = 0;

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

	return TRUE;
}

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

	BOOL bRet = FALSE;

	ULONG nRet = SWIOMADMInitApi();	

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

	if (!nRet)
	{
		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;

	ULONG nRet = SWIOMADMShutdownApi();	

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

	if (!nRet)
	{
		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;
	UINT8 sessionType = 0;

	// Get user selected parameters
	if (pParams)
	{
		sessionType = (UINT8)_ttoi(pParams->GetValue(PARAM_SessionType));
	}
  
	ULONG nRet = SWIOMADMSessionStart(sessionType);	

	CString strTemp;
	strTemp.Format(L"Return: %04d", nRet);
	ADDSTATUS(strTemp, STATUSMSG_Receive);
   
	if (!nRet)
	{
		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;
	UINT8 sessionType = 0;
   
	// Get user selected parameters
	if (pParams)
	{
    	sessionType = (UINT8)_ttoi(pParams->GetValue(PARAM_SessionType));      
	}

	ULONG nRet = SWIOMADMSessionCancel(sessionType);

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

	if (!nRet)
	{
		strTemp.Format(L"SWIOMADM Session Cancel successful");
		ADDSTATUS(strTemp, STATUSMSG_Receive);

		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;

	UINT8 SessionState = 0;
	UINT8 SessionType = 0;

   // Get user selected parameters
	if (pParams)
	{
    	SessionState = (UINT8)_ttoi(pParams->GetValue(PARAM_SessionState));
		SessionType = (UINT8)_ttoi(pParams->GetValue(PARAM_SessionType));
	}
	
//   ULONG nRet = SWIOMADMSessionGetInfo(&SessionType, &SessionState);
	ULONG nRet = SWIOMADMSessionGetInfo(SessionType, SessionState);

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

	if (!nRet)
	{
		strTemp.Format(L"SessionType: %i", SessionType);
		ADDSTATUS(strTemp, STATUSMSG_Receive);
		strTemp.Format(L"SessionState: %i", SessionState);
		ADDSTATUS(strTemp, STATUSMSG_Receive);

		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;

	UINT8 userInput = 0;

	// Get user selected parameters
	if (pParams)
	{		
		userInput = (UINT8)_ttoi(pParams->GetValue(PARAM_UserInput));
	}

    ULONG nRet = SWIOMADMSelection(userInput);

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

	if (!nRet)
	{
		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;

	UINT32 enabled = 0xFFFFFFFF;
	UINT8 fwAutoLoad = 0xFF;
   UINT8 fwAutoUpdate = 0xFF;
	
//   ULONG nRet = SWIOMADMSessionGetInfo(&SessionType, &SessionState);
	ULONG nRet = SWIOMADMGetSettings(enabled, fwAutoLoad, fwAutoUpdate);

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

	if (!nRet)
	{
      // TBD
      /*
		strTemp.Format(L"Enable: %i", SessionType);
		ADDSTATUS(strTemp, STATUSMSG_Receive);
		strTemp.Format(L"SessionState: %i", SessionState);
		ADDSTATUS(strTemp, STATUSMSG_Receive);
      */
		bRet = TRUE;
	}

	return bRet;
}

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

	BOOL bRet = FALSE;
	UINT8 FWAutoLoad = 0;
	UINT8 FWAutoUpdate = 0;

	// Get user selected parameters
	if (pParams)
	{
		FWAutoLoad = (UINT8)_ttoi(pParams->GetValue(PARAM_FWAutoLoad));
		FWAutoUpdate = (UINT8)_ttoi(pParams->GetValue(PARAM_FWAutoUpdate));
	}
  
	ULONG nRet = SWIOMADMSetSettings(FWAutoLoad, FWAutoUpdate);	

	CString strTemp;
	strTemp.Format(L"Return: %04d", nRet);
	ADDSTATUS(strTemp, STATUSMSG_Receive);
   
	if (!nRet)
	{
		bRet = TRUE;
	}	

	return bRet;
}


// OMA-DM Callback tests

static void SWICALLBACK SWIOMADMFotaCallback(
	UINT8 SessionState,
	UINT8 UserInputReq,
	UINT16 UserInputTimeout,
	UINT32 FWDownloadSize,
	UINT32 FWDownloadComplete,
	UINT16 UpdateStatus,
	UINT8 Severity,
	UINT16 VersionLength,
	BYTE Version[],
	UINT16 NameLength,
	BYTE PackageName[], 
	UINT16 DescriptionLength, 
	BYTE Description[])
{
   CString strTemp;
   strTemp.Format(L"SWIOMADM_EVENT_IND, TLV-0x10");
   ADDSTATUS(strTemp, STATUSMSG_Notif);   
	strTemp.Format(L"State: %d", SessionState);
   ADDSTATUS(strTemp, STATUSMSG_Notif); 
   strTemp.Format(L"User Input Req: %d, User Input Timeout: %d", UserInputReq, UserInputTimeout);
   ADDSTATUS(strTemp, STATUSMSG_Notif); 
   strTemp.Format(L"FW Download Size: %d, FW Download Complete: %d", FWDownloadSize, FWDownloadComplete);
   ADDSTATUS(strTemp, STATUSMSG_Notif);
   strTemp.Format(L"Update Status: %d, Severity: %d", UpdateStatus, Severity );
	ADDSTATUS(strTemp, STATUSMSG_Notif);
   strTemp.Format(L"Version Length: %d", VersionLength );
	ADDSTATUS(strTemp, STATUSMSG_Notif); 

   strTemp.Empty();
   strTemp.Format(L"Version:");
   for( UINT16 i=0; i< VersionLength; i++)
   {
	   if( Version[i] != NULL )
		   strTemp += Version[i];
   }
   
   ADDSTATUS(strTemp, STATUSMSG_Notif); 



   strTemp.Format(L"Name Length: %d", NameLength );
   ADDSTATUS(strTemp, STATUSMSG_Notif);

   strTemp.Empty();
   strTemp.Format(L"Package Name:");
   for (UINT16 i=0; i< NameLength; i++)
   {
	   if( PackageName[i] != NULL )
		   strTemp += PackageName[i];
   }
   ADDSTATUS(strTemp, STATUSMSG_Notif);


   strTemp.Format(L"Description Length: %d", DescriptionLength);
   ADDSTATUS(strTemp, STATUSMSG_Notif);

   strTemp.Empty();
   strTemp.Format(L"Description:");
   for( UINT16 i=0; i< DescriptionLength; i++)
   {
	   if( Description[i] != NULL )
		   strTemp += Description[i];
   }
   
   ADDSTATUS(strTemp, STATUSMSG_Notif);  



}

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

	ULONG ret = 0;

	if (0 != _ttoi(pParams->GetValue(PARAM_Enable)))
	{
		ret = SetSWIOMADMFotaCallback(SWIOMADMFotaCallback);
	}
	else
	{
		ret = SetSWIOMADMFotaCallback(NULL);
	}

	CString strTemp;
	strTemp.Format(L"Result: %04d", ret);
	ADDSTATUS(strTemp, (0==ret)?STATUSMSG_Receive:STATUSMSG_Error);

	return !ret;
}



static void SWICALLBACK SWIOMADMAlertCallback(UINT8 SessionState,UINT8 UserInputReq,UINT16 UserInputTimeout,
                                               UINT16 AlertMsgLength, BYTE AlertMsg[])
{
   CString strTemp;
   strTemp.Format(L"SWIOMADM_EVENT_IND, TLV-0x11");
   ADDSTATUS(strTemp, STATUSMSG_Notif); 
	strTemp.Format(L"Session Type: %d", SessionState);
   ADDSTATUS(strTemp, STATUSMSG_Notif); 
   strTemp.Format(L"User Input Req: %d, User Input Timeout: %d", UserInputReq, UserInputTimeout);
   ADDSTATUS(strTemp, STATUSMSG_Notif); 
   strTemp.Format(L"Alert Message Length: %d", AlertMsgLength);
   ADDSTATUS(strTemp, STATUSMSG_Notif);

   strTemp.Empty();
   strTemp.Format(L"Alert Message:");
   for( UINT16 i=0; i< AlertMsgLength; i++)
   {
	   if( AlertMsg[i] != NULL )
		   strTemp += AlertMsg[i];
   }
   
   ADDSTATUS(strTemp, STATUSMSG_Notif);
   
}

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

	ULONG ret = 0;

	if (0 != _ttoi(pParams->GetValue(PARAM_Enable)))
	{
		ret = SetSWIOMADMAlertCallback(SWIOMADMAlertCallback);
	}
	else
	{
		ret = SetSWIOMADMAlertCallback(NULL);
	}

	CString strTemp;
	strTemp.Format(L"Result: %04d", ret);
	ADDSTATUS(strTemp, (0==ret)?STATUSMSG_Receive:STATUSMSG_Error);

	return !ret;
}


BOOL CGobiApiSwiOma::FuncSWIOMADMSetEventReport(CParamList* pParams /*= NULL*/)
{
	UNREFERENCED_PARAMETER(pParams);
	// Set callback
	ADDSTATUS(L"FuncSetSWIOMADMFotaCallback", STATUSMSG_Send);
	ULONG ret = 0;

	ret = SetSWIOMADMFotaCallback(SWIOMADMFotaCallback);
	
	CString strTemp;
	strTemp.Format(L"Result: %04d", ret);
	ADDSTATUS(strTemp, (0==ret)?STATUSMSG_Receive:STATUSMSG_Error);

	if( ret != 0 )
		return ret;

	ADDSTATUS(L"FuncSetSWIOMADMAlertCallback", STATUSMSG_Send);

	ret = 0;

	ret = SetSWIOMADMAlertCallback(SWIOMADMAlertCallback);
		
	strTemp.Format(L"Result: %04d", ret);
	ADDSTATUS(strTemp, (0==ret)?STATUSMSG_Receive:STATUSMSG_Error);

	if ( ret != 0 )
		return ret;

	ADDSTATUS(L"FuncSWIOMADMSetEventReport", STATUSMSG_Send);

	BOOL bRet = FALSE;
   	  
	ULONG nRet = SWIOMADMSetEventReport();	

//	CString strTemp;
	strTemp.Format(L"Return: %04d", nRet);
	ADDSTATUS(strTemp, STATUSMSG_Receive);
   
	if (!nRet)
	{
		bRet = TRUE;
	}
	return bRet;
}
