#include "p_apps.h"
#include "gdiconst.h"
#include "Ac97.h"

#define SETTINGS_ENTRY_LEN	64
#define SETTINGS_SDRAM_NUM	8
#define LIST_NUM_LEN		1		
#define PHONEBOOK_ENTRY_LEN	64
#define PHONEBOOK_SDRAM_NUM	16

#define END_LABEL_LEN	5
static CCHAR _cEndLabel[] = "[END]";
static BOOLEAN _bEnd;

CCHAR _cProtocolDisplay[PROTOCOL_MAX][PROTOCOL_DISPLAY_LEN] = {
"h323",
"sip",
"mgcp",
"n2p",
"iax2",
"wp",
"epn"
};

BOOLEAN _CompareVal(UCHAR iLen, UCHAR iMax)
{
	if (iLen > iMax)
		return FALSE;
	else 
		return TRUE;
}

BOOLEAN _CompareString(PXCHAR pSrc, UCHAR iLen)
{
	DCHAR iStringLen;
	DSHORT i;

	iStringLen = p_strlen_x(pSrc);
	if (iLen)
	{
		if (iStringLen)
		{
			if (iStringLen > iLen)
				return FALSE;
		}
		else
			return FALSE;
	}

	for (i = 0; i < iStringLen; i ++)
	{
		if (pSrc[i] < '0' || pSrc[i] > '9')
				return FALSE;
	}
	return TRUE;
}

void _SetFlag(USHORT iIndex, UCHAR iFlag)
{
	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	X_VAL(Sdram_pDataCache, iIndex) |= iFlag;
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

void _ClearFlag(USHORT iIndex, UCHAR iFlag)
{
	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	X_VAL(Sdram_pDataCache, iIndex) &= iFlag;
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

void _WriteVal(USHORT iIndex, UCHAR iValue)
{
	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	X_VAL(Sdram_pDataCache, iIndex) = iValue;
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

void _WriteString(USHORT iIndex, PXCHAR pString, UCHAR iMax)
{
	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+iIndex), pString, iMax);
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

void _WritePort(USHORT iIndex, USHORT iValue)
{
	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	USHORT2PXCHAR(iValue, (PXCHAR)(Sdram_pDataCache+iIndex));
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

void _WriteIP(USHORT iIndex, PXCHAR pIP)
{
	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	p_memcpy_ip((PXCHAR)(Sdram_pDataCache + iIndex), pIP);
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

void _WriteTimezone(UCHAR iValue)
{
	DCHAR iTimezone;

	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	iTimezone = X_VAL(Sdram_pDataCache, OPTIONS_TIME_ZONE);
	iTimezone = iValue + (iTimezone & TIMEZONE_DAYLIGHT_OR);
	X_VAL(Sdram_pDataCache, OPTIONS_TIME_ZONE) = iTimezone;
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
}

BOOLEAN _CfgParseEntry(PXCHAR pEntry)
{
	PXCHAR_D pValue;
	PXCHAR_D pBlank;
	DCHAR iChar;
	DSHORT iShort;
	XCHAR pIP[IP_ALEN];
	XCHAR pName[32];

	pValue = p_strchr_x (pEntry, '=');
	if (!pValue)	return TRUE;

	pBlank = p_strchr_x (pEntry, ' ');
	while (pBlank)
	{
		pBlank[0] = 0;
		pBlank ++;
		p_strcat_x2x((PXCHAR)pEntry, (PXCHAR)pBlank);
		pBlank = p_strchr_x (pEntry, ' ');
	}

	pValue = p_strchr_x (pEntry, '=');
	pValue[0] = 0;
	pValue ++;
	p_strcpy_x2x(pName, pEntry);
	p_strcat_c2x(pName, " ");

	if (!p_strcmp_c2x(pName, _cProtocolLabel))
	{
		if(p_strcmp_c2x(pValue, (PCCHAR)_cProtocolDisplay[Sys_iProtocol]))
			return FALSE;
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDeviceAttrLabel))
		return TRUE;

	/*  Network Settings  */
	if(!p_strcmp_c2x(pName, _cPppIDLabel))
		return TRUE;

	/*  Protocol Settings  */
	if (!p_strcmp_c2x(pName, _cUseGKLabel))
	{
		if (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_SIP || Sys_iProtocol == PROTOCOL_IAX2)
		{
			if( _CompareString(pValue, LIST_NUM_LEN) )
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				if( _CompareVal(iChar, 2) )
					_WriteVal(OPTIONS_USE_GK, iChar);
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cServicetypeLabel))
	{
		if (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_MGCP || Sys_iProtocol == PROTOCOL_SIP)
		{
			if (_CompareString (pValue, 2))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				switch(Sys_iProtocol)
				{
				case PROTOCOL_H323:
					if (_CompareVal (iChar, H323_SERVICE_TYPE_MAX))
						_WriteVal(OPTIONS_SERVICE_TYPE, iChar);
					break;

				case PROTOCOL_SIP:
					if (_CompareVal (iChar, SIP_SERVICE_TYPE_MAX))
						_WriteVal(OPTIONS_SERVICE_TYPE, iChar);
					break;

				case PROTOCOL_MGCP:
					if (_CompareVal (iChar, MGCP_SERVICE_TYPE_MAX))
						_WriteVal(OPTIONS_SERVICE_TYPE, iChar);
					break;
				}
			}
		}
		return TRUE;
	}
//	if (!p_strcmp_c2x(pName, (PCCHAR)"serviceaddr")|| !p_strcmp_c2x(pName, (PCCHAR)"sipproxy") || !p_strcmp_c2x(pName, (PCCHAR)"caaddr"))
	if (!p_strcmp_c2x(pName, _cServiceAddrLabel))
	{
		if(_CompareVal(p_strlen_x(pValue), MAX_DNS_ADDR_LEN))
			_WriteString(OPTIONS_SERVICE_DNS, pValue, MAX_DNS_ADDR_LEN);
		return TRUE;
	}
//	if (!p_strcmp_c2x(pName, (PCCHAR)"serviceid"))
#if defined CALL_SIP || defined CALL_N2P
	if (!p_strcmp_c2x(pName, _cGKIDLabel))
	{
		if(_CompareVal(p_strlen_x(pValue), MAX_SERVICE_ID_LEN))
			_WriteString(OPTIONS_SERVICE_ID, pValue, MAX_SERVICE_ID_LEN);
		return TRUE;
	}
#endif
	if (!p_strcmp_c2x(pName, _cRegisterTTLLabel))
	{
		iShort = p_atoi(pValue, 10);
		if (iShort)
			_WritePort(OPTIONS_REGISTER_TTL, iShort);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cNatTraversalLabel))
	{
		if (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_MGCP || Sys_iProtocol == PROTOCOL_SIP)
		{
			if (_CompareString (pValue, 2))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				switch(Sys_iProtocol)
				{
				case PROTOCOL_H323:
					if (_CompareVal (iChar, H323_NAT_TRAVERSAL_MAX))
						_WriteVal(OPTIONS_NAT_TRAVERSAL, iChar);
					break;

				case PROTOCOL_SIP:
					if (_CompareVal (iChar, SIP_NAT_TRAVERSAL_MAX))
						_WriteVal(OPTIONS_NAT_TRAVERSAL, iChar);
					break;

				case PROTOCOL_MGCP:
					if (_CompareVal (iChar, MGCP_NAT_TRAVERSAL_MAX))
						_WriteVal(OPTIONS_NAT_TRAVERSAL, iChar);
					break;
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cNatAddrLabel))
	{
		if (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_MGCP || Sys_iProtocol == PROTOCOL_SIP)
		{
			if(_CompareVal(p_strlen_x(pValue), MAX_DNS_ADDR_LEN))
				_WriteString(OPTIONS_NAT_DNS, pValue, MAX_DNS_ADDR_LEN);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cNatTTLLabel))
	{
		if(Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_MGCP || Sys_iProtocol == PROTOCOL_SIP)
		{
			iShort = p_atoi(pValue, 10);
			if (iShort)
				_WritePort(OPTIONS_NAT_TTL, iShort);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cPhoneNumberLabel))
	{
		if (Sys_iProtocol != PROTOCOL_N2P)
		{
			if(_CompareVal(p_strlen_x(pValue), PHONE_NUMBER_LEN))
				_WriteString(OPTIONS_PHONE_NUMBER, pValue, PHONE_NUMBER_LEN);
		}
		return TRUE;
	}
//	if (!p_strcmp_c2x(pName, (PCCHAR)"account") || !p_strcmp_c2x(pName, (PCCHAR)"endpointid"))
	if (!p_strcmp_c2x(pName, _cCCIDLabel))
	{
		if(_CompareVal(p_strlen_x(pValue), MAX_ACCOUNT_LEN))
			_WriteString(OPTIONS_ACCOUNT, pValue, MAX_ACCOUNT_LEN);
		return TRUE;
	}
//	if (!p_strcmp_c2x(pName, (PCCHAR)"pin"))
	if (!p_strcmp_c2x(pName, _cCCPINLabel))
	{
		if(_CompareVal(p_strlen_x(pValue), MAX_PIN_LEN))
			_WriteString(OPTIONS_PIN, pValue, MAX_PIN_LEN);
		return TRUE;
	}
/*	if (!p_strcmp_c2x(pName, (PCCHAR)"domain"))
	{
		switch(Sys_iProtocol)
		{
		case PROTOCOL_MGCP:
			if(_CompareVal(p_strlen_x(pValue), MAX_PIN_LEN))
				_WriteString(OPTIONS_PIN, pValue, MAX_PIN_LEN);
			break;

		case PROTOCOL_SIP:
			if(_CompareVal(p_strlen_x(pValue), MAX_SERVICE_ID_LEN))
				_WriteString(OPTIONS_SERVICE_ID, pValue, MAX_SERVICE_ID_LEN);
			break;
		}
		return TRUE;
	}
*/
//	if (!p_strcmp_c2x(pName, (PCCHAR)"rasport") || !p_strcmp_c2x(pName, (PCCHAR)"localport") || !p_strcmp_c2x(pName, (PCCHAR)"registerport"))
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP || defined CALL_N2P || defined CALL_IAX2
	if (!p_strcmp_c2x(pName, _cRasLabel))
	{
		iShort = p_atoi(pValue, 10);
		if (iShort)
			_WritePort(OPTIONS_RAS_PORT, iShort);
		return TRUE;
	}
#endif
#ifdef CALL_H323
//	if (!p_strcmp_c2x(pName, (PCCHAR)"q931port"))
	if (!p_strcmp_c2x(pName, _cQ931Label))
	{
		if (Sys_iProtocol == PROTOCOL_H323)
		{
			iShort = p_atoi(pValue, 10);
			if (iShort)
				_WritePort(OPTIONS_Q931_PORT, iShort);
		}
		return TRUE;
	}
//	if (!p_strcmp_c2x(pName, (PCCHAR)"h245port"))
	if (!p_strcmp_c2x(pName, _cH245Label))
	{
		if (Sys_iProtocol == PROTOCOL_H323)
		{
			iShort = p_atoi(pValue, 10);
			if (iShort)
				_WritePort(OPTIONS_H245_PORT, iShort);
		}
		return TRUE;
	}
#endif
#ifndef CALL_IAX2
	//	if (!p_strcmp_c2x(pName, (PCCHAR)"rtpport") || !p_strcmp_c2x(pName, (PCCHAR)"voiceport"))
	if (!p_strcmp_c2x(pName, _cRtpLabel))
	{
		if (Sys_iProtocol != PROTOCOL_IAX2)
		{
			iShort = p_atoi(pValue, 10);
			if (iShort)
				_WritePort(OPTIONS_RTP_PORT, iShort);
		}
		return TRUE;
	}
#endif
	if (!p_strcmp_c2x(pName, _cRtpTosLabel))
	{
		if (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_SIP || Sys_iProtocol == PROTOCOL_MGCP || Sys_iProtocol == PROTOCOL_EPN)
		{
			if (_CompareString (pValue, 2))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				if (_CompareVal (iChar, MAX_RTP_TOS_LEN))
					_WriteVal(OPTIONS_RTP_TOS, iChar);
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cLocalAliasLabel))
	{
		if (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_MGCP)
		{
			if (_CompareString (pValue, LIST_NUM_LEN))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				switch(Sys_iProtocol)
				{
				case PROTOCOL_H323:
					if (_CompareVal(iChar, H323_LOCAL_TYPE_MAX))
						_WriteVal(OPTIONS_LOCAL_ALIAS, iChar);
					break;

				case PROTOCOL_MGCP:
					if (_CompareVal(iChar, MGCP_LOCAL_TYPE_MAX))
						_WriteVal(OPTIONS_LOCAL_ALIAS, iChar);
					break;
				}
			}
		}
		return TRUE;
	}
#ifdef CALL_SIP
//	if (!p_strcmp_c2x(pName, (PCCHAR)"outboundproxy"))
	if (!p_strcmp_c2x(pName, _cFaststartLabel))
	{
		if (Sys_iProtocol == PROTOCOL_SIP)
		{
			if (_CompareString (pValue, LIST_NUM_LEN))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				if (_CompareVal(iChar, 2))
				{
					if (iChar == 1)
					{
						_SetFlag(OPTIONS_CALL_TYPE, FAST_START_OR);
					}
					else if (iChar == 0)
					{
						_ClearFlag(OPTIONS_CALL_TYPE, FAST_START_AND);
					}
				}
			}
		}
		return TRUE;
	}
//	if (!p_strcmp_c2x(pName, (PCCHAR)"prack"))
	if (!p_strcmp_c2x(pName, _cSipPrackLabel))
	{
		if (Sys_iProtocol == PROTOCOL_SIP)
		{
			if (_CompareString (pValue, LIST_NUM_LEN))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				if (_CompareVal(iChar, 2))
				{
					if (iChar == 1)
					{
						_SetFlag(OPTIONS_CALL_TYPE, SIP_PRACK_OR);
					}
					else if (iChar == 0)
					{
						_ClearFlag(OPTIONS_CALL_TYPE, SIP_PRACK_AND);
					}
				}
			}
		}
		return TRUE;
	}
#endif
#ifdef CALL_IAX2
	if (!p_strcmp_c2x(pName, _cRemoteDialplanLabel))
	{
		if (Sys_iProtocol == PROTOCOL_IAX2)
		{
			if (_CompareString (pValue, LIST_NUM_LEN))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				if (_CompareVal(iChar, 2))
				{
					if (iChar == 1)
					{
						_SetFlag(OPTIONS_CALL_TYPE, REMOTE_DIALPLAN_OR);
					}
					else if (iChar == 0)
					{
						_ClearFlag(OPTIONS_CALL_TYPE, REMOTE_DIALPLAN_AND);
					}
				}
			}
		}
		return TRUE;
	}
#endif
	if (!p_strcmp_c2x(pName, _cDTMFLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			switch(Sys_iProtocol)
			{
			case PROTOCOL_H323:
				if (_CompareVal(iChar, H323_DTMF_TYPE_MAX))
					_WriteVal(OPTIONS_DTMF_TYPE, iChar);
				break;

			case PROTOCOL_SIP:
				if( _CompareVal(iChar, SIP_DTMF_TYPE_MAX) )
					_WriteVal(OPTIONS_DTMF_TYPE, iChar);
				break;

			case PROTOCOL_MGCP:
				if( _CompareVal(iChar, MGCP_DTMF_TYPE_MAX) )
					_WriteVal(OPTIONS_DTMF_TYPE, iChar);
				break;

			case PROTOCOL_N2P:
				if( _CompareVal(iChar, N2P_DTMF_TYPE_MAX) )
					_WriteVal(OPTIONS_DTMF_TYPE, iChar);
				break;

			default:
				if( _CompareVal(iChar, DTMF_TYPE_MAX) )
					_WriteVal(OPTIONS_DTMF_TYPE, iChar);
				break;
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDtmfPayLoadLabel))
	{
		if (Sys_iProtocol == PROTOCOL_SIP || Sys_iProtocol == PROTOCOL_MGCP)
		{
			if (_CompareString(pValue, 3))
			{
				iChar = (UCHAR)p_atoi(pValue, 10);
				if( _CompareVal(iChar, MAX_DTMF_PTYPE) )
					_WriteVal(OPTIONS_DTMF_PTYPE, iChar);
			}
		}
		return TRUE;
	}
	/*  Audio Settings  */
	if (!p_strcmp_c2x(pName, _cCodec1Label))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if( _CompareVal(iChar, CODEC_MAX) )
				_WriteVal(OPTIONS_CODEC_TYPE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCodec2Label))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if( _CompareVal(iChar, CODEC_MAX) )
				_WriteVal(OPTIONS_CODEC_TYPE+1, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCodec3Label))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if( _CompareVal(iChar, CODEC_MAX) )
				_WriteVal(OPTIONS_CODEC_TYPE+2, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCodec4Label))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if( _CompareVal(iChar, CODEC_MAX) )
				_WriteVal(OPTIONS_CODEC_TYPE+3, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCodec5Label))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if( _CompareVal(iChar, CODEC_MAX) )
				_WriteVal(OPTIONS_CODEC_TYPE+4, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCodec6Label))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if( _CompareVal(iChar, CODEC_MAX) )
				_WriteVal(OPTIONS_CODEC_TYPE+5, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cHighrateLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_DSP_OPTION, USE_HIGH_RATE_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_DSP_OPTION, USE_HIGH_RATE_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cVadLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_DSP_OPTION, USE_VAD_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_DSP_OPTION, USE_VAD_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cAgcLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_DSP_OPTION, USE_AGC_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_DSP_OPTION, USE_AGC_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cAecLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_DSP_OPTION, USE_AEC_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_DSP_OPTION, USE_AEC_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cIlbcPayLoadLabel))
	{
		if (_CompareString(pValue, 3))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, MAX_ILBC_PTYPE))
				_WriteVal(OPTIONS_ILBC_PTYPE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cAudioframeLabel))
	{
		if (_CompareString(pValue, 1))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, AUDIO_FRAMES_NUM))
				_WriteVal(OPTIONS_AUDIO_FRAMES, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cJitterSizeLabel))
	{
		if (_CompareString (pValue, 2))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, JITTER_SIZE_MAX))
				_WriteVal(OPTIONS_JITTER_SIZE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cRingTypeLabel))
	{
		if (_CompareString (pValue, 3))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, RING_TYPE_MAX))
				_WriteVal(OPTIONS_RING_TYPE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cHandsetInputLabel))
	{
		if (_CompareString (pValue, 0))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, AC97_MIC_VOL_MAX))
				_WriteVal(OPTIONS_HANDSET_INPUT, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cHandsetOutputLabel))
	{
		if (_CompareString (pValue, 0))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, AC97_SPK_VOL_MAX))
				_WriteVal(OPTIONS_HANDSET_OUTPUT, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cHandfreeInputLabel))
	{
		if (_CompareString (pValue, 0))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, AC97_MIC_VOL_MAX))
				_WriteVal(OPTIONS_HANDFREE_INPUT, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cHandfreeOutputLabel))
	{
		if (_CompareString (pValue, 0))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, AC97_SPK_VOL_MAX))
				_WriteVal(OPTIONS_HANDFREE_OUTPUT, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDualModeLabel))
	{
		if (_CompareString (pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, DUAL_MODE_MAX))
				_WriteVal(OPTIONS_DUAL_MODE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDualModePrefixLabel))
	{
		if (_CompareString (pValue, 0))
		{
			if(_CompareVal(p_strlen_x(pValue), MAX_DUALMODE_PREFIX_LEN))
				_WriteString(OPTIONS_DUALMODE_PREFIX, pValue, MAX_DUALMODE_PREFIX_LEN);
		}
		return TRUE;
	}
	/*  Dialplan Settings  */
	if (!p_strcmp_c2x(pName, _cDialplanLabel))
	{
		if (_CompareString (pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, DIALPLAN_MAX))
				_WriteVal(OPTIONS_USE_DIALPLAN, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDialNumberLabel))
	{
		if (_CompareVal (p_strlen_x(pValue), PHONE_NUMBER_LEN-1))
			_WriteString (OPTIONS_DIAL_NUMBER, pValue, DIAL_NUMBER_LEN);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cAreacodeLabel))
	{
		if (_CompareString (pValue, 0))
		{
			if (_CompareVal (p_strlen_x(pValue), MAX_AREA_CODE_LEN))
				_WriteString (OPTIONS_AREA_CODE, pValue, MAX_AREA_CODE_LEN);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDDDprefixLabel))
	{
		if (_CompareString (pValue, 0))
		{
			if (_CompareVal (p_strlen_x(pValue), MAX_LONG_PREFIX_LEN))
				_WriteString (OPTIONS_LONG_PREFIX, pValue, MAX_LONG_PREFIX_LEN);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCountrycodeLabel))
	{
		if (_CompareString (pValue, 0))
		{
			if (_CompareVal (p_strlen_x(pValue), MAX_COUNTRY_CODE_LEN))
				_WriteString (OPTIONS_COUNTRY_CODE, pValue, MAX_COUNTRY_CODE_LEN);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cIDDprefixLabel))
	{
		if (_CompareString (pValue, 0))
		{
			if (_CompareVal (p_strlen_x(pValue), MAX_INTL_PREFIX_LEN))
				_WriteString (OPTIONS_INTL_PREFIX, pValue, MAX_INTL_PREFIX_LEN);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cInnerLineLabel))
	{
		if (_CompareString (pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, INNER_LINE_MAX))
				_WriteVal(OPTIONS_INNER_LINE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cInnerLinePrefixLabel))
	{
		if (_CompareString (pValue, 0))
		{
			if (_CompareVal (p_strlen_x(pValue), MAX_INNERLINE_PREFIX_LEN))
				_WriteString (OPTIONS_INNERLINE_PREFIX, pValue, MAX_INNERLINE_PREFIX_LEN);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDigitMapLabel))
	{
		if (_CompareString (pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, 2))
				_WriteVal(OPTIONS_USE_DIGITMAP, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cForwardNumberLabel))
	{
		if (_CompareVal (p_strlen_x(pValue), PHONE_NUMBER_LEN))
			_WriteString (OPTIONS_FWD_NUMBER, pValue, FORWARD_NUMBER_LEN);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cFwdAlwaysLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_FWD_TYPE, FORWARD_ALWAYS_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_FWD_TYPE, FORWARD_ALWAYS_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cFwdBusyLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_FWD_TYPE, FORWARD_BUSY_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_FWD_TYPE, FORWARD_BUSY_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cFwdNoAnswerLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_FWD_TYPE, FORWARD_NOANSWER_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_FWD_TYPE, FORWARD_NOANSWER_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cFwdPowerOffLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_FWD_TYPE, FORWARD_POWEROFF_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_FWD_TYPE, FORWARD_POWEROFF_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cCallWaitingLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_FWD_TYPE, CALL_WAITING_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_FWD_TYPE, CALL_WAITING_AND);
				}
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cRingNumberLabel))
	{
		if (_CompareString (pValue, 2))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, 61))
				_WriteVal(OPTIONS_RING_NUMBER, iChar);
		}
		return TRUE;
	}
	/*  Other Settings  */
	if (!p_strcmp_c2x(pName, _cSuperPasswordLabel))
	{
		if (_CompareVal (p_strlen_x(pValue), MAX_SUPERPASSWORD_LEN))
			_WriteString (OPTIONS_SUPER_PASSWORD, pValue, MAX_SUPERPASSWORD_LEN);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDebugLabel))
	{
		if (_CompareString (pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, DEBUG_LEVEL_MAX))
				_WriteVal(OPTIONS_OUTPUT_DEBUG, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cPasswordLabel))
	{
		if (_CompareVal (p_strlen_x(pValue), MAX_PASSWORD_LEN))
			_WriteString (OPTIONS_PASSWORD, pValue, MAX_PASSWORD_LEN);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cUpgradeTypeLabel))
	{
		if (_CompareString (pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal (iChar, UPGRADE_TYPE_MAX))
				_WriteVal(OPTIONS_UPGRADE_TYPE, iChar);
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cUpgradeAddrLabel))
	{
		if(_CompareVal(p_strlen_x(pValue), MAX_DNS_ADDR_LEN))
			_WriteString(OPTIONS_UPGRADE_DNS, pValue, MAX_DNS_ADDR_LEN);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cNtsIPLabel))
	{
		if (p_String2IP(pValue, pIP))
			_WriteIP(OPTIONS_NTS_ADDR, pIP);
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cTimeZoneLabel))
	{
		if (_CompareString (pValue, 3))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, TIME_ZONE_MAX))
			{
				_WriteTimezone(iChar);
			}
		}
		return TRUE;
	}
	if (!p_strcmp_c2x(pName, _cDaylightLabel))
	{
		if (_CompareString(pValue, LIST_NUM_LEN))
		{
			iChar = (UCHAR)p_atoi(pValue, 10);
			if (_CompareVal(iChar, 2))
			{
				if (iChar == 1)
				{
					_SetFlag(OPTIONS_TIME_ZONE, TIMEZONE_DAYLIGHT_OR);
				}
				else if (iChar == 0)
				{
					_ClearFlag(OPTIONS_TIME_ZONE, TIMEZONE_DAYLIGHT_AND);
				}
			}
		}
		return TRUE;
	}

	return TRUE;
}

void _WritePhoneBook(PXCHAR pSpeedDial)
{
	PXCHAR_D pCur;
	PXCHAR_D pContent;
	XCHAR pName[PHONE_NUMBER_LEN+1], pNumber[PHONE_NUMBER_LEN+1];
	DCHAR iIndex, iLen;

	pCur = pSpeedDial;
	pContent = pCur+6;
	if (!p_memcmp_c2x(pCur, (PCCHAR)"memory", 6))
	{
		pCur = p_strchr_x(pContent, (UCHAR)':');
		pCur[0] = 0;
		pCur ++;
		iIndex = p_atoi(pContent, 10) - 1;
		iLen = p_strlen_x(pCur);
		p_memcpy_x2x(pName, pCur, iLen);
		pName[iLen] = 0;
		WriteSpeedDial(pName, iIndex*2+1);
	}
	else if (!p_memcmp_c2x(pCur, (PCCHAR)"number", 6))
	{
		pCur = p_strchr_x(pContent, (UCHAR)':');
		pCur[0] = 0;
		pCur ++;
		iIndex = p_atoi(pContent, 10) - 1;
		if (iIndex == (SPEED_DIAL_NUM-1))
			_bEnd = TRUE;
		iLen = p_strlen_x(pCur);
		p_memcpy_x2x(pNumber, pCur, iLen);
		pNumber[iLen] = 0;
		WriteSpeedDial(pNumber, iIndex*2);
	}
}

void PhbTxt2Bin()
{
	XCHAR pTemp[PHONEBOOK_ENTRY_LEN+1];
	XCHAR pBuf[256];
	DCHAR iLen, iChar, iPos, iIndexLo;

	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];
	_bEnd = FALSE;
	while (iChar)
	{
		if (iChar == 0x0d || iChar == 0x0a)
		{
			if (iLen)
			{
				if (iLen <= PHONEBOOK_ENTRY_LEN)
				{
					pTemp[iLen] = 0;
					_WritePhoneBook(pTemp);
				}
				iLen = 0;
			}
		}
		else
		{
			pTemp[iLen] = iChar;
			iLen ++;
			if (_bEnd)
			{
				DebugCString((PCCHAR)"End of Cfg file");
				break;
			}
			else
			{
				if (iLen == PHONEBOOK_ENTRY_LEN)	iLen = 0;
			}
		}
		iPos ++;
		if (!iPos)
		{
			iIndexLo ++;
			if (iIndexLo >= PHONEBOOK_SDRAM_NUM)	break;
			SdramRead(FILE_IN_SDRAM_HI, iIndexLo, SRAM_DMA_HALF_DWORD);
			p_memcpy_x2x(pBuf, (PXCHAR)Sdram_pDataCache, (UCHAR)256);
		}
		iChar = pBuf[iPos];
	}
}

void CfgTxt2Bin()
{
	XCHAR pTemp[SETTINGS_ENTRY_LEN+1];
	XCHAR pMap[256];
	DCHAR iLen, iChar, iPos, iIndexLo;
	DSHORT iMapVersion, iCfgVersion, iPhbVersion;

	SdramReadOptions();
	iMapVersion = P_MAKEWORD(X_VAL(Sdram_pDataCache, OPTIONS_DIGITMAP_VER+1), X_VAL(Sdram_pDataCache, OPTIONS_DIGITMAP_VER));
	iCfgVersion = P_MAKEWORD(X_VAL(Sdram_pDataCache, OPTIONS_SETTINGS_VER+1), X_VAL(Sdram_pDataCache, OPTIONS_SETTINGS_VER));
	iPhbVersion = P_MAKEWORD(X_VAL(Sdram_pDataCache, OPTIONS_PHONEBOOK_VER+1), X_VAL(Sdram_pDataCache, OPTIONS_PHONEBOOK_VER));
	SdramWriteAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);

	iLen = 0;
	iIndexLo = 0;
	SdramRead(FILE_IN_SDRAM_HI, iIndexLo, SRAM_DMA_HALF_DWORD);
	p_memcpy_x2x(pMap, (PXCHAR)Sdram_pDataCache, (UCHAR)256);
	iPos = 0;
	iChar = pMap[iPos];
	while (iChar)
	{
		if (iChar == 0x0d || iChar == 0x0a)
		{
			if (iLen)
			{
				if (iLen <= SETTINGS_ENTRY_LEN)
				{
					pTemp[iLen] = 0;
					if (!_CfgParseEntry(pTemp))
					{
						DebugCString((PCCHAR)"Invalid cfg file!");
						break;
					}
				}
				iLen = 0;
			}
		}
		else
		{
			pTemp[iLen] = iChar;
			iLen ++;
			if (!p_memcmp_c2x(pTemp, _cEndLabel, END_LABEL_LEN))
			{
				DebugCString((PCCHAR)"End of Cfg file");
				break;
			}
			else
			{
				if (iLen == SETTINGS_ENTRY_LEN)	iLen = 0;
			}
		}
		iPos ++;
		if (!iPos)
		{
			iIndexLo ++;
			if (iIndexLo >= SETTINGS_SDRAM_NUM)	break;
			SdramRead(FILE_IN_SDRAM_HI, iIndexLo, SRAM_DMA_HALF_DWORD);
			p_memcpy_x2x(pMap, (PXCHAR)Sdram_pDataCache, (UCHAR)256);
		}
		iChar = pMap[iPos];
	}

	SdramReadAll(OPTIONS_SDRAM_POS+CFG_TEMP_SDRAM_LO);
	WriteTcpipOptions();
	if (Sys_iUpgradeType == UPGRADE_TYPE_MAC)
	{
		goto End;
	}
	else if (Sys_iUpgradeType == UPGRADE_TYPE_PPPID)
	{
		p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_PPP_ID), PPP_pID, PPP_ID_LEN);
		goto End;
	}
	else if (Sys_iUpgradeType == UPGRADE_TYPE_ACCOUNT)
	{
		p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_ACCOUNT), Sys_pAccount, MAX_ACCOUNT_LEN);
		goto End;
	}
	else if (Sys_iUpgradeType == UPGRADE_TYPE_PHONENUMBER)
	{
		p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_PHONE_NUMBER), Sys_pLocalNumber, PHONE_NUMBER_LEN);
		goto End;
	}

	p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_ACCOUNT), Sys_pAccount, MAX_ACCOUNT_LEN);
	p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_PIN), Sys_pPin, MAX_PIN_LEN);
#ifndef VER_IPH_E00
	p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_PHONE_NUMBER), Sys_pLocalNumber, PHONE_NUMBER_LEN);
#endif
	X_VAL(Sdram_pDataCache, OPTIONS_HANDSET_INPUT) = Ac97_iHandsetInputVol;
	X_VAL(Sdram_pDataCache, OPTIONS_HANDFREE_INPUT) = Ac97_iHandfreeInputVol;
	X_VAL(Sdram_pDataCache, OPTIONS_HANDSET_OUTPUT) = Ac97_iHandsetOutputVol;
	X_VAL(Sdram_pDataCache, OPTIONS_HANDFREE_OUTPUT) = Ac97_iHandfreeOutputVol;
#ifdef VER_IPH_E00
	X_VAL(Sdram_pDataCache, OPTIONS_RING_TYPE) = Task_iRingType;
	X_VAL(Sdram_pDataCache, OPTIONS_TIME_ZONE) = Sys_iTimeZone;
	p_memcpy_ip((PXCHAR)(Sdram_pDataCache+OPTIONS_NTS_ADDR), Sys_pNtsIP);
	p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+OPTIONS_PASSWORD), Sys_pPassword, MAX_PASSWORD_LEN);
#endif
End:
	X_VAL(Sdram_pDataCache, OPTIONS_DIGITMAP_VER) = P_HIBYTE(iMapVersion);
	X_VAL(Sdram_pDataCache, OPTIONS_DIGITMAP_VER+1) = P_LOBYTE(iMapVersion);
	X_VAL(Sdram_pDataCache, OPTIONS_SETTINGS_VER) = P_HIBYTE(iCfgVersion);
	X_VAL(Sdram_pDataCache, OPTIONS_SETTINGS_VER+1) = P_LOBYTE(iCfgVersion);
	X_VAL(Sdram_pDataCache, OPTIONS_PHONEBOOK_VER) = P_HIBYTE(iPhbVersion);
	X_VAL(Sdram_pDataCache, OPTIONS_PHONEBOOK_VER+1) = P_LOBYTE(iPhbVersion);
	SdramWriteOptions();
}