#include "p_apps.h"
#include "ac97.h"
#include "gdiconst.h"

extern PXCHAR _pCommandLine;	// set options command line

//
// set options
//

BOOLEAN _SetMacAddress(PXCHAR pDst, PCCHAR cLabel)
{
	DCHAR iLen;
	PXCHAR_D pCur;

	pCur = _pCommandLine;
	iLen = p_strlen_c(cLabel);
	if (!p_memcmp_c2x(pCur, cLabel, iLen))
	{
#if !defined(OEM_MEDIARING) && !defined(OEM_RINGCHAT)
		pCur += iLen;
		return p_String2Mac(pCur, pDst);
#else
		pCur = pDst;
#endif
	}
	return FALSE;
}

BOOLEAN _SetIPAddress(PXCHAR pDst, PCCHAR cLabel)
{
	DCHAR iLen;
	PXCHAR_D pCur;

	pCur = _pCommandLine;
	iLen = p_strlen_c(cLabel);
	if (!p_memcmp_c2x(pCur, cLabel, iLen))
	{
		pCur += iLen;
		return p_String2IP(pCur, pDst);
	}
	return FALSE;
}

BOOLEAN _SetByteVal_DCHAR(volatile PDCHAR pDst, PCCHAR cLabel, UCHAR iMax)
{
	DCHAR iLen;
	DSHORT iVal;
	PXCHAR_D pCur;

	pCur = _pCommandLine;
	iLen = p_strlen_c(cLabel);
	if (!p_memcmp_c2x(pCur, cLabel, iLen))
	{
		pCur += iLen;
		if (p_strlen_x(pCur) <= 5)
		{
			iVal = p_atoi(pCur, 10);
			if (iVal < iMax)
			{
				pDst[0] = (UCHAR)iVal;
				return TRUE;
			}
		}
	}
	return FALSE;
}

BOOLEAN _SetByteVal(PXCHAR pDst, PCCHAR cLabel, UCHAR iMax)
{
	DCHAR iLen;
	DSHORT iVal;
	PXCHAR_D pCur;

	pCur = _pCommandLine;
	iLen = p_strlen_c(cLabel);
	if (!p_memcmp_c2x(pCur, cLabel, iLen))
	{
		pCur += iLen;
		if (p_strlen_x(pCur) <= 5)
		{
			iVal = p_atoi(pCur, 10);
			if (iVal < iMax)
			{
				pDst[0] = (UCHAR)iVal;
				return TRUE;
			}
		}
	}
	return FALSE;
}

BOOLEAN _SetPortVal(PXSHORT pDst, PCCHAR cLabel)
{
	DCHAR iLen;
	PXCHAR_D pCur;

	pCur = _pCommandLine;
	iLen = p_strlen_c(cLabel);
	if (!p_memcmp_c2x(pCur, cLabel, iLen))
	{
		pCur += iLen;
		if (p_strlen_x(pCur) <= 5)
		{
			pDst[0] = p_atoi(pCur, 10);
			return TRUE;
		}
	}
	return FALSE;
}

BOOLEAN _SetStringVal(PXCHAR pDst, PCCHAR cLabel, UCHAR iMaxLen)
{
	DCHAR iLen;
	PXCHAR_D pCur;

	pCur = _pCommandLine;
	iLen = p_strlen_c(cLabel);
	if (!p_memcmp_c2x(pCur, cLabel, iLen))
	{
		pCur += iLen;
		if (p_strlen_x(pCur) <= iMaxLen)
		{
			p_strcpy_x2x(pDst, pCur);
			return TRUE;
		}
	}
	return FALSE;
}

BOOLEAN SetOptions(PXCHAR pCommandLine)
{
	_pCommandLine = pCommandLine;

	if (Task_bSupervisor)
	{
		if (_SetByteVal(&Udp_iOutputDebug, _cDebugLabel, DEBUG_LEVEL_MAX))					return TRUE;
		if (_SetStringVal(Sys_pSuperPassword, _cSuperPasswordLabel, MAX_SUPERPASSWORD_LEN))	return TRUE;

		if (_SetPortVal(&Sys_iRegisterTTL, _cRegisterTTLLabel))								return TRUE;
		if (_SetStringVal(Sys_pServiceAddr, _cServiceAddrLabel, MAX_DNS_ADDR_LEN))			return TRUE;
		if (_SetStringVal(Sys_pAccount, _cCCIDLabel, MAX_ACCOUNT_LEN))							return TRUE;
		if (_SetStringVal(Sys_pPin, _cCCPINLabel, MAX_PIN_LEN))									return TRUE;
#ifndef CALL_IAX2
		if (_SetPortVal(&Sys_iRtpPort, _cRtpLabel))											return TRUE;
#endif
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP || defined CALL_N2P || defined CALL_IAX2
		if (_SetPortVal(&Sys_iRegisterPort, _cRasLabel))									return TRUE;
#endif

#if defined CALL_SIP || defined CALL_N2P
		if (_SetStringVal(Sys_pServiceID, _cGKIDLabel, MAX_SERVICE_ID_LEN))					return TRUE;
#endif

#ifdef CALL_H323
		if (_SetByteVal(&Sys_iLocalType, _cLocalAliasLabel, H323_LOCAL_TYPE_MAX))			return TRUE;
		if (_SetByteVal(&Sys_iServiceType, _cServicetypeLabel, H323_SERVICE_TYPE_MAX))		return TRUE;
		if (_SetStringVal(Sys_pLocalNumber, _cPhoneNumberLabel, PHONE_NUMBER_LEN))			return TRUE;
		if (_SetByteVal(&Sys_iNatTraversal, _cNatTraversalLabel, H323_NAT_TRAVERSAL_MAX))	return TRUE;
		if (_SetStringVal(Sys_pNatAddr, _cNatAddrLabel, MAX_DNS_ADDR_LEN))					return TRUE;
		if (_SetPortVal(&Sys_iNatTTL, _cNatTTLLabel))										return TRUE;
		if (_SetPortVal(&Sys_iSignalPort, _cQ931Label))										return TRUE;
		if (_SetPortVal(&Sys_iControlPort, _cH245Label))									return TRUE;
		if (_SetByteVal(&Rtp_iTos, _cRtpTosLabel, MAX_RTP_TOS_LEN))							return TRUE;
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, H323_DTMF_TYPE_MAX))					return TRUE;
#elif defined CALL_SIP
		if (_SetByteVal(&Sys_iServiceType, _cServicetypeLabel, SIP_SERVICE_TYPE_MAX))		return TRUE;
		if (_SetByteVal(&Sys_iNatTraversal, _cNatTraversalLabel, SIP_NAT_TRAVERSAL_MAX))	return TRUE;
		if (_SetStringVal(Sys_pNatAddr, _cNatAddrLabel, MAX_DNS_ADDR_LEN))					return TRUE;
		if (_SetPortVal(&Sys_iNatTTL, _cNatTTLLabel))										return TRUE;
		if (_SetStringVal(Sys_pLocalNumber, _cPhoneNumberLabel, PHONE_NUMBER_LEN))			return TRUE;
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, SIP_DTMF_TYPE_MAX))					return TRUE;
		if (_SetByteVal(&Sys_iDtmfPayloadType, _cDtmfPayLoadLabel, MAX_DTMF_PTYPE))			return TRUE;
		if (_SetByteVal(&Rtp_iTos, _cRtpTosLabel, MAX_RTP_TOS_LEN))							return TRUE;
#elif defined CALL_MGCP
		if (_SetByteVal(&Sys_iServiceType, _cServicetypeLabel, MGCP_SERVICE_TYPE_MAX))		return TRUE;
		if (_SetByteVal(&Sys_iNatTraversal, _cNatTraversalLabel, MGCP_NAT_TRAVERSAL_MAX))	return TRUE;
		if (_SetStringVal(Sys_pNatAddr, _cNatAddrLabel, MAX_DNS_ADDR_LEN))					return TRUE;
		if (_SetPortVal(&Sys_iNatTTL, _cNatTTLLabel))										return TRUE;
		if (_SetStringVal(Sys_pLocalNumber, _cPhoneNumberLabel, PHONE_NUMBER_LEN))			return TRUE;
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, MGCP_DTMF_TYPE_MAX))					return TRUE;
		if (_SetByteVal(&Sys_iDtmfPayloadType, _cDtmfPayLoadLabel, MAX_DTMF_PTYPE))			return TRUE;
		if (_SetByteVal(&Rtp_iTos, _cRtpTosLabel, MAX_RTP_TOS_LEN))							return TRUE;
		if (_SetByteVal(&Sys_iLocalType, _cLocalAliasLabel, MGCP_LOCAL_TYPE_MAX))			return TRUE;
#elif defined CALL_N2P
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, N2P_DTMF_TYPE_MAX))					return TRUE;
#elif defined CALL_IAX2
		if (_SetStringVal(Sys_pLocalNumber, _cPhoneNumberLabel, PHONE_NUMBER_LEN))			return TRUE;
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, DTMF_TYPE_MAX))						return TRUE;
		if (_SetByteVal(&Rtp_iTos, _cRtpTosLabel, MAX_RTP_TOS_LEN))							return TRUE;
#elif defined CALL_EPN
		if (_SetStringVal(Sys_pLocalNumber, _cPhoneNumberLabel, PHONE_NUMBER_LEN))			return TRUE;
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, DTMF_TYPE_MAX))					return TRUE;
		if (_SetByteVal(&Rtp_iTos, _cRtpTosLabel, MAX_RTP_TOS_LEN))							return TRUE;
#elif defined CALL_WP
		if (_SetStringVal(Sys_pLocalNumber, _cPhoneNumberLabel, PHONE_NUMBER_LEN))			return TRUE;
		if (_SetByteVal_DCHAR(&Sys_iDtmfType, _cDTMFLabel, DTMF_TYPE_MAX))					return TRUE;
#endif
	}

#ifdef VER_IPH_E00
	if (_SetMacAddress(Local_pMacAddress, _cSetMacLabel))								return TRUE;
#else
	if (_SetMacAddress(Local_pMacAddress, _cMacLabel))									return TRUE;
#endif
	if (_SetIPAddress(Local_pIPAddress, _cIPLabel))											return TRUE;
	if (_SetIPAddress(Local_pSubnetMask, _cMaskLabel))										return TRUE;
	if (_SetIPAddress(Local_pRouteIP, _cRouterLabel))										return TRUE;
	if (_SetIPAddress(Dns_pServerIP, _cDNSLabel))											return TRUE;
	if (_SetIPAddress(Dns_pServerIP2, _cDNSLabel2))											return TRUE;
	if (_SetByteVal(&Sys_iIPType, _cIPTypeLabel, IP_TYPE_MAX))								return TRUE;
	if (_SetStringVal(PPP_pID, _cPppIDLabel, PPP_ID_LEN))									return TRUE;
	if (_SetStringVal(PPP_pPassword, _cPppPinLabel, PPP_PASSWORD_LEN))						return TRUE;
	if (_SetPortVal(&VLAN_iID, _cVlanID))													return TRUE;

	if (_SetByteVal(&Sys_iInnerLine, _cInnerLineLabel, INNER_LINE_MAX))					return TRUE;
	if (_SetStringVal(Sys_pDualModePrefix, _cDualModePrefixLabel, MAX_DUALMODE_PREFIX_LEN))			return TRUE;
	if (_SetStringVal(Sys_pInnerLinePrefix, _cInnerLinePrefixLabel, MAX_INNERLINE_PREFIX_LEN))				return TRUE;
	if (_SetByteVal(&Sys_iDualMode, _cDualModeLabel, DUAL_MODE_MAX))					return TRUE;
	if (_SetByteVal(&Sys_iUseDialPlan, _cDialplanLabel, DIALPLAN_MAX))					return TRUE;
	if (_SetStringVal(Sys_pDialNumber, _cDialNumberLabel, DIAL_NUMBER_LEN))				return TRUE;
	if (_SetStringVal(Sys_pCountryCode, _cCountrycodeLabel, MAX_COUNTRY_CODE_LEN))		return TRUE;
	if (_SetStringVal(Sys_pIntlPrefix, _cIDDprefixLabel, MAX_INTL_PREFIX_LEN))			return TRUE;
	if (_SetStringVal(Sys_pAreaCode, _cAreacodeLabel, MAX_AREA_CODE_LEN))				return TRUE;
	if (_SetStringVal(Sys_pLongPrefix, _cDDDprefixLabel, MAX_LONG_PREFIX_LEN))			return TRUE;

	if (_SetByteVal(&Sys_iIlbcPType, _cIlbcPayLoadLabel, MAX_ILBC_PTYPE))			return TRUE;
	if (_SetByteVal(&Sys_iAudioFrames, _cAudioframeLabel, AUDIO_FRAMES_NUM))			return TRUE;
	if (_SetByteVal(&Sys_pCodecType[0], _cCodec1Label, CODEC_MAX))						return TRUE;
	if (_SetByteVal(&Sys_pCodecType[1], _cCodec2Label, CODEC_MAX))						return TRUE;
	if (_SetByteVal(&Sys_pCodecType[2], _cCodec3Label, CODEC_MAX))						return TRUE;
	if (_SetByteVal(&Sys_pCodecType[3], _cCodec4Label, CODEC_MAX))						return TRUE;
	if (_SetByteVal(&Sys_pCodecType[4], _cCodec5Label, CODEC_MAX))						return TRUE;
	if (_SetByteVal(&Sys_pCodecType[5], _cCodec6Label, CODEC_MAX))						return TRUE;
	if (_SetByteVal(&Ac97_iHandsetInputVol, _cHandsetInputLabel, AC97_MIC_VOL_MAX+1))		return TRUE;
	if (_SetByteVal(&Ac97_iHandfreeInputVol, _cHandfreeInputLabel, AC97_MIC_VOL_MAX+1))		return TRUE;
	if (_SetByteVal(&Ac97_iHandsetOutputVol, _cHandsetOutputLabel, AC97_SPK_VOL_MAX+1))		return TRUE;
	if (_SetByteVal(&Ac97_iHandfreeOutputVol, _cHandfreeOutputLabel, AC97_SPK_VOL_MAX+1))	return TRUE;
	if (_SetByteVal(&Sys_iJitterSize, _cJitterSizeLabel, JITTER_SIZE_MAX))				return TRUE;

	if (_SetIPAddress(Sys_pNtsIP, _cNtsIPLabel))											return TRUE;
	if (_SetStringVal(Sys_pForwardNumber, _cForwardNumberLabel, FORWARD_NUMBER_LEN))		return TRUE;
	if (_SetByteVal(&Sys_iUpgradeType, _cUpgradeTypeLabel, UPGRADE_TYPE_MAX))				return TRUE;
	if (_SetByteVal(&Task_iRingNumber, _cRingNumberLabel, RING_NUM))						return TRUE;
	if (_SetByteVal_DCHAR(&Task_iRingType, _cRingTypeLabel, RING_TYPE_MAX))						return TRUE;
	if (_SetStringVal(Sys_pPassword, _cPasswordLabel, MAX_PASSWORD_LEN))					return TRUE;
	if (_SetStringVal(Sys_pUpgradeUrl, _cUpgradeAddrLabel, MAX_DNS_ADDR_LEN))			
	{
		SetUpgradeAddr();
		return TRUE;
	}

	return FALSE;	
}

BOOLEAN SetTelnetOptions(PXCHAR pCommandLine)
{
	XCHAR iVal;		// must use XCHAR because we will make use of &iIndex as PXCHAR

	_pCommandLine = pCommandLine;

	if (Task_bSupervisor)
	{
		if (_SetByteVal(&iVal, _cUseGKLabel, 2))
		{
			Sys_bUseService = iVal; 
			return TRUE;
		}

#ifdef CALL_SIP
		if (_SetByteVal(&iVal, _cFaststartLabel, 2))
		{
			if (iVal)
			{
				Sys_iCallType |= FAST_START_OR; 
			}
			else
			{
				Sys_iCallType &= FAST_START_AND; 
			}
			return TRUE;
		}

		if (_SetByteVal(&iVal, _cSipPrackLabel, 2))
		{
			if (iVal)
			{
				Sys_iCallType |= SIP_PRACK_OR; 
			}
			else
			{
				Sys_iCallType &= SIP_PRACK_AND; 
			}
			return TRUE;
		}
#endif

#ifdef CALL_IAX2
		if (_SetByteVal(&iVal, _cRemoteDialplanLabel, 2))
		{
			if (iVal)
			{
				Sys_iCallType |= REMOTE_DIALPLAN_OR; 
			}
			else
			{
				Sys_iCallType &= REMOTE_DIALPLAN_AND; 
			}
			return TRUE;
		}
#endif
	}

	if (_SetByteVal(&iVal, _cVlan, 2))
	{
		VLAN_bEnable = iVal; 
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cDigitMapLabel, 2))
	{
		Sys_bUseDigitMap = iVal; 
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cHighrateLabel, 2))
	{
		if (iVal)
		{
			Dsp_iOptions |= USE_HIGH_RATE_OR; 
		}
		else
		{
			Dsp_iOptions &= USE_HIGH_RATE_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cVadLabel, 2))
	{
		if (iVal)
		{
			Dsp_iOptions |= USE_VAD_OR; 
		}
		else
		{
			Dsp_iOptions &= USE_VAD_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cAgcLabel, 2))
	{
		if (iVal)
		{
			Dsp_iOptions |= USE_AGC_OR; 
		}
		else
		{
			Dsp_iOptions &= USE_AGC_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cAecLabel, 2))
	{
		if (iVal)
		{
			Dsp_iOptions |= USE_AEC_OR; 
		}
		else
		{
			Dsp_iOptions &= USE_AEC_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cFwdAlwaysLabel, 2))
	{
		if (iVal)
		{
			Sys_iForwardType |= FORWARD_ALWAYS_OR; 
		}
		else
		{
			Sys_iForwardType &= FORWARD_ALWAYS_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cFwdBusyLabel, 2))
	{
		if (iVal)
		{
			Sys_iForwardType |= FORWARD_BUSY_OR; 
		}
		else
		{
			Sys_iForwardType &= FORWARD_BUSY_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cFwdNoAnswerLabel, 2))
	{
		if (iVal)
		{
			Sys_iForwardType |= FORWARD_NOANSWER_OR; 
		}
		else
		{
			Sys_iForwardType &= FORWARD_NOANSWER_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cFwdPowerOffLabel, 2))
	{
		if (iVal)
		{
			Sys_iForwardType |= FORWARD_POWEROFF_OR; 
		}
		else
		{
			Sys_iForwardType &= FORWARD_POWEROFF_AND; 
		}
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cCallWaitingLabel, 2))
	{
		if (iVal)
		{
			Sys_iForwardType |= CALL_WAITING_OR; 
		}
		else
		{
			Sys_iForwardType &= CALL_WAITING_AND; 
		}
		return TRUE;
	}

	iVal = Sys_iTimeZone & TIMEZONE_DAYLIGHT_OR;

	if (_SetByteVal(&Sys_iTimeZone, _cTimeZoneLabel, TIME_ZONE_MAX))
	{
		Sys_iTimeZone += iVal;
		return TRUE;
	}

	if (_SetByteVal(&iVal, _cDaylightLabel, 2))
	{
		if (iVal)
		{
			Sys_iTimeZone |= TIMEZONE_DAYLIGHT_OR; 
		}
		else
		{
			Sys_iTimeZone &= TIMEZONE_DAYLIGHT_AND; 
		}
		return TRUE;
	}

	return FALSE;
}
