#include "p_apps.h"
#include "v_task.h"
#include "autoprov.h"

#define MAX_LINE_LEN		128
#define SET_FILE_SDRAM_NUM	4

DCHAR Upgrade_iType;
XCHAR Upgrade_pServerIP[IP_ALEN];

void ProvGetFileName()
{
	DCHAR iLen;

	if (Upgrade_iType == UPGRADE_GET_SET)
	{
		if (Sys_iUpgradeType == UPGRADE_TYPE_ALL)
		{
#ifdef VER_IPH_E00
			p_memcpy_x2x_0((PXCHAR)Sdram_pDataCache, GetVersionTextEx(TRUE, FALSE), 21);
#ifdef CALL_H323
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, "H323");
#elif defined CALL_SIP
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, "SIP");
#elif defined CALL_IAX2
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, "IAX2");
#elif defined CALL_MGCP
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, "MGCP");
#endif
		
#ifdef RES_CN
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, " CN");
#elif defined RES_US
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, " US");
#elif defined RES_TW
			p_strcat_c2x((PXCHAR)Sdram_pDataCache, " TW");
#endif // RES_CN
#else  // VER_IPH_E00
			p_strcpy_x2x((PXCHAR)Sdram_pDataCache, GetHardwareVersion());
#endif			
		}
		else if (Sys_iUpgradeType == UPGRADE_TYPE_MAC)
		{
			p_Mac2String(Local_pMacAddress, (PXCHAR)Sdram_pDataCache);
		}
		else if (Sys_iUpgradeType == UPGRADE_TYPE_PPPID)
		{
			p_strcpy_x2x((PXCHAR)Sdram_pDataCache, PPP_pID);
		}
		else if (Sys_iUpgradeType == UPGRADE_TYPE_ACCOUNT)
		{
			p_strcpy_x2x((PXCHAR)Sdram_pDataCache, Sys_pAccount);
		}
		else if (Sys_iUpgradeType == UPGRADE_TYPE_PHONENUMBER)
		{
			p_strcpy_x2x((PXCHAR)Sdram_pDataCache, Sys_pLocalNumber);
		}
		p_strcat_c2x((PXCHAR)Sdram_pDataCache, (PCCHAR)".set");					
	}
	else
	{
		p_strcpy_x2x((PXCHAR)Sdram_pDataCache, GetHardwareVersion());

		switch (Upgrade_iType)
		{
			case UPGRADE_GET_BIN:
				p_strcat_c2x((PXCHAR)Sdram_pDataCache, (PCCHAR)".bin");
				break;

			case UPGRADE_GET_MAP:
				p_strcat_c2x((PXCHAR)Sdram_pDataCache, (PCCHAR)".map");
				break;

			case UPGRADE_GET_CFG:
				p_strcat_c2x((PXCHAR)Sdram_pDataCache, (PCCHAR)".cfg");
				break;

			case UPGRADE_GET_PHB:
				p_strcat_c2x((PXCHAR)Sdram_pDataCache, (PCCHAR)".phb");
				break;
		}
	}

	iLen = p_strlen_x((PXCHAR)Sdram_pDataCache);
	SdramWriteData(OPTIONS_SDRAM_POS+UPGRADE_FILENAME_LO, iLen);
}

void _SaveVersion(USHORT iNewVersion, UCHAR iIndex)
{
	SdramReadOptions();
	USHORT2PXCHAR(iNewVersion, (PXCHAR)(Sdram_pDataCache + iIndex));
	SdramWriteOptions();
}

void _GetFileName(PXCHAR pFileName)
{
	PXCHAR_D pCur;
	PXCHAR_D pContent;
	XCHAR pVer[6];
	DSHORT iCurVersion, iNewVersion;

	pCur = pFileName;
	pContent = pCur+4;
	if (!p_memcmp_c2x(pCur, (PCCHAR)"BIN", 3))
	{
		pCur = p_strchr_x(pContent, (UCHAR)':');
		pCur[0] = 0;
		pCur ++;

		SdramReadFlags();
		pVer[0] = SOFT_VER_HIGH + '0';	//X_VAL(Sdram_pDataCache, FLAGS_VERSION_HI);
		pVer[1] = SOFT_VER_LOW + '0';	// X_VAL(Sdram_pDataCache, FLAGS_VERSION_LO);
		pVer[2] = SOFT_VER_BUILD + '0';	//X_VAL(Sdram_pDataCache, FLAGS_VERSION_BUILD);
		pVer[3] = 0;
		iCurVersion = p_atoi(pVer, 10);
		iNewVersion = p_atoi(pCur, 10);
		OutputVal(iCurVersion, IDS_CHECK_POINT_7);
		OutputVal(iNewVersion, IDS_CHECK_POINT_8);
		if (iNewVersion > iCurVersion)
		{
			Upgrade_iType = UPGRADE_GET_BIN;
			SdramSaveData(OPTIONS_SDRAM_POS+UPGRADE_FILENAME_LO, pContent, p_strlen_x(pContent));
		}
	}
	else if (!p_memcmp_c2x(pCur, (PCCHAR)"CFG", 3))
	{
		pCur = p_strchr_x(pContent, (UCHAR)':');
		pCur[0] = 0;
		pCur ++;

		SdramReadOptions();
		iCurVersion = PXCHAR2USHORT((PXCHAR)(Sdram_pDataCache + OPTIONS_SETTINGS_VER));
		iNewVersion = p_atoi(pCur, 10);
		OutputVal(iCurVersion, IDS_CHECK_POINT_7);
		OutputVal(iNewVersion, IDS_CHECK_POINT_8);
		if (iNewVersion != iCurVersion)
		{
			_SaveVersion(iNewVersion, OPTIONS_SETTINGS_VER);
			Upgrade_iType = UPGRADE_GET_CFG;
			SdramSaveData(OPTIONS_SDRAM_POS+UPGRADE_FILENAME_LO, pContent, p_strlen_x(pContent));
		}
	}
	else if (!p_memcmp_c2x(pCur, (PCCHAR)"MAP", 3))
	{
		pCur = p_strchr_x(pContent, (UCHAR)':');
		pCur[0] = 0;
		pCur ++;

		SdramReadOptions();
		iCurVersion = PXCHAR2USHORT((PXCHAR)(Sdram_pDataCache + OPTIONS_DIGITMAP_VER));
		iNewVersion = p_atoi(pCur, 10);
		OutputVal(iCurVersion, IDS_CHECK_POINT_7);
		OutputVal(iNewVersion, IDS_CHECK_POINT_8);
		if (iNewVersion != iCurVersion)
		{	
			_SaveVersion(iNewVersion, OPTIONS_DIGITMAP_VER);
			Upgrade_iType = UPGRADE_GET_MAP;
			SdramSaveData(OPTIONS_SDRAM_POS+UPGRADE_FILENAME_LO, pContent, p_strlen_x(pContent));
		}
	}
	else if (!p_memcmp_c2x(pCur, (PCCHAR)"PHB", 3))
	{
		pCur = p_strchr_x(pContent, (UCHAR)':');
		pCur[0] = 0;
		pCur ++;

		SdramReadOptions();
		iCurVersion = PXCHAR2USHORT((PXCHAR)(Sdram_pDataCache + OPTIONS_PHONEBOOK_VER));
		iNewVersion = p_atoi(pCur, 10);
		OutputVal(iCurVersion, IDS_CHECK_POINT_7);
		OutputVal(iNewVersion, IDS_CHECK_POINT_8);
		if (iNewVersion != iCurVersion)
		{	
			_SaveVersion(iNewVersion, OPTIONS_PHONEBOOK_VER);
			Upgrade_iType = UPGRADE_GET_PHB;
			SdramSaveData(OPTIONS_SDRAM_POS+UPGRADE_FILENAME_LO, pContent, p_strlen_x(pContent));
		}
	}
}

void ProvHandleSetFile()
{
	DCHAR iLen, iChar, iPos, iIndexLo;
	XCHAR pBuf[256];
	XCHAR pLine[MAX_LINE_LEN + 1];

	iLen = 0;
	iIndexLo = 0;
	SdramRead(FILE_IN_SDRAM_HI, iIndexLo, SRAM_DMA_HALF_DWORD);
	p_memcpy_x2x(pBuf, (PXCHAR)Sdram_pDataCache, (UCHAR)256);
	iPos = 0;
	iChar = pBuf[iPos];
	while (iChar)
	{
		if (iChar == 0x0d || iChar == 0x0a)
		{
			if (iLen)
			{
				if (iLen <= MAX_LINE_LEN)
				{
					pLine[iLen] = 0;
					_GetFileName(pLine);
					if (Upgrade_iType != UPGRADE_GET_SET)
					{
						break;
					}
				}
				iLen = 0;
			}
		}
		else
		{
			pLine[iLen] = iChar;
			iLen ++;
			if (!p_memcmp_c2x(pLine, (PCCHAR)"END", 3))
			{
				DebugCString((PCCHAR)"End of set file");
				break;
			}
			else
			{
				if (iLen == MAX_LINE_LEN)	iLen = 0;
			}
		}
		iPos ++;
		if (!iPos)
		{
			iIndexLo ++;
			if (iIndexLo >= SET_FILE_SDRAM_NUM)	
			{
				DebugCString((PCCHAR)".set file too big");
				break;
			}
			SdramRead(FILE_IN_SDRAM_HI, iIndexLo, SRAM_DMA_HALF_DWORD);
			p_memcpy_x2x(pBuf, (PXCHAR)Sdram_pDataCache, (UCHAR)256);
		}
		iChar = pBuf[iPos];
	}
}

void ProvStartUpgrade()
{
	if (Upgrade_iType == UPGRADE_GET_SET)
	{
		DebugCString((PCCHAR)"something wrong with program");
		goto End;
	}

	switch (Upgrade_iType)
	{
		case UPGRADE_GET_BIN:
			if (IsFirmwareLenOK(SoloFileGetSize()))
			{
				OnSwitchUpgrade(PROGRAM_UPGRADE_PAGE);
			}
			break;

		case UPGRADE_GET_MAP:
			CopyFileToOption(8, DIAL_MAP_LO);
			OnUpdateOptions();
			break;
		
		case UPGRADE_GET_CFG:
			CfgTxt2Bin();
			OnUpdateOptions();
			break;

		case UPGRADE_GET_PHB:
//			CopyFileToOption(8, DIAL_MAP_LO);
			PhbTxt2Bin();
			OnUpdateOptions();
			break;
		
#ifdef VER_IPH_E00
		case FTP_DEFAULT_CONFIG:
			if (SoloFileGetSize() == 512)
			{
				SdramRead(FILE_IN_SDRAM_HI, 0, SRAM_DMA_DWORD);
				SdramWriteOptions();
				OnSwitchUpgrade(OPTIONS_UPGRADE_PAGE);
			}
			else
			{
				DisplayString(g_cFailed, 1);
			}
			break;

		case FTP_IVR_FILE:
			if (SoloFileGetSize() <= FILE_FULL_PAGE_SIZE)
			{
				// IVR is at page-3
				OnSwitchUpgrade(3);
			}
			else
			{
				DisplayString(g_cFailed, 1);
			}
			break;
#endif
	}

End:
	CloseFileAndCheckLimit();
}

void CloseFileAndCheckLimit()
{
	DLONG lLen;

	lLen = SoloFileClose();
	if (lLen > FILE_ONE_PAGE_SIZE)
	{	// reload DSP after reboot
		XBYTE[Sdram_pDataCache+SDRAM_CHECK_POS] = 0;
		sdram_write(SDRAM_CHECK_HI, SDRAM_CHECK_LO, SRAM_DMA_HALF_DWORD);
		OnReboot();
	}
	OnProvisionEnd();
}


