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

#include "gdiconst.h"

#define TELNET_STATE_INIT		0
#define TELNET_STATE_PASSWORD	1
#define TELNET_STATE_OK			2

#define TELNET_TASK_NONE		0
#define TELNET_TASK_GET_ALL		1
#define TELNET_TASK_SET_HELP	2

#define MAX_RECV_LEN			64		//224
#define MAX_PASSWORD_RETRY		3
#define TELNET_TIME_OUT			240

#define CHAR_BACKSPACE	0x08
#define CHAR_SPACE		0x20
#define CHAR_STAR		0x2a
#define CHAR_SE			0xf0
#define CHAR_SB			0xfa
#define CHAR_WILL		0xfb
#define CHAR_WONT		0xfc
#define CHAR_DO			0xfd
#define CHAR_DONT		0xfe
#define CHAR_IAC		0xff

#define COMMAND_ECHO			1
#define COMMAND_SUPPRESS_GA		3

// Tcp connection data
static XCHAR _pRecvPacket[MAX_RECV_LEN];

static XSHORT _iCurMsgPos;
static XCHAR _iExtraDataFlag, _iExtraDataTask;
static XCHAR _iRecvLen;
static BOOLEAN _bTelnetWrite;
static XCHAR _iState, _iLoginRetry, _iLineLength;

static CCHAR _cMaskPassword[] = "*****";

CCHAR _cRebooting[] = "rebooting...";

static CCHAR _cNetWorkSettingsLabel[] = "Network";
static CCHAR _cProtocolSettingsLabel[] = "Protocol";
static CCHAR _cPhoneSettingsLabel[] = "Dial Plan";
static CCHAR _cAudioSettingsLabel[] = "Audio";
static CCHAR _cOtherSettingsLabel[] = "Other";
static CCHAR _cSettings[] = " Settings";

UCHAR _DecodeCommand(PXCHAR pData, UCHAR iTotalLen)
{
	DCHAR iLen, iCode;
	PXCHAR_D pCur;

	pCur = pData;
	iCode = pCur[1];

	if (iCode >= CHAR_WILL && iCode <= CHAR_DONT)
	{
		return 3;
	}

	if (iCode > CHAR_SE && iCode < CHAR_SB)
	{
		return 2;
	}

	if (iCode == CHAR_SB)
	{
		pCur += 2;
		for (iLen = 2; iLen < (iTotalLen-1); iLen ++)
		{
			if (pCur[0] == CHAR_IAC && pCur[1] == CHAR_SE)
			{
				iLen += 2;
				return iLen;
			}
			pCur ++;
		}
	}

	return 1;
}

void _SendChar(UCHAR iChar)
{
	X_VAL(Adapter_pSendTcpData, _iCurMsgPos) = iChar;
	_iCurMsgPos ++;
	_iLineLength ++;
}

void _SendLine(PCCHAR pLine)
{
	DCHAR iLength;
	PCCHAR_D pString;

	pString = pLine;

	iLength = p_strlen_c(pString);
	if (iLength)
	{
		p_strcpy_c2x((PXCHAR)(Adapter_pSendTcpData+_iCurMsgPos), pString);
		_iCurMsgPos += iLength;
		_iLineLength += iLength;
	}
}

void _SendNewLine(PCCHAR pString)
{
	_SendChar('\r');
	_SendChar('\n');
	_iLineLength = 0;
	_SendLine(pString);
}

/*
void _SendSetHelp(PCCHAR pString)
{
	_SendNewLine(_cSetLabel);
	_SendLine(pString);
}
*/

void _SendTabLine(PCCHAR pString)
{
	if (_iLineLength < 24)
	{
		do
		{
			_SendChar('\t');
			_iLineLength += 7;
			if (_iLineLength >= 24)
			{
				_iLineLength = 24;
				break;
			}
		} while (1);
	}
	else if (_iLineLength < 56)
	{
		do
		{
			_SendChar('\t');
			_iLineLength += 7;
			if (_iLineLength >= 56)
			{
				_iLineLength = 56;
				break;
			}
		} while (1);
	}
	_SendLine(pString);
}

void _SendEnableText(BOOLEAN bEnable)
{
	if (bEnable)
	{
		_SendLine(g_cEnableLabel);
	}
	else
	{
		_SendLine(g_cDisableLabel);
	}
}

void _SendPrompt()
{
	_SendNewLine((PCCHAR)"P:\\>");
}

void _SendString(PXCHAR pStr)
{
	DCHAR iLength;
	PXCHAR_D pString;

	pString = pStr;

	iLength = p_strlen_x(pString);
	if (iLength)
	{
		p_strcpy_x2x((PXCHAR)(Adapter_pSendTcpData+_iCurMsgPos), pString);
		_iCurMsgPos += iLength;
		_iLineLength += iLength;
	}
	else
	{
		_SendLine(_cEmptyLabel);
	}
}

void _SendRemarkLine(UCHAR iIndex)
{
	if (Sys_bLoadDefault)	return;

	_SendChar('[');
	SdramRead(STRING_SDRAM_HI, iIndex, SRAM_DMA_HALF_DWORD);
	_SendString((PXCHAR)Sdram_pDataCache);
	_SendChar(']');
}

void _SendShort(USHORT iVal)
{
	XCHAR pBuf[6];

	p_itoa(iVal, pBuf, 10);
	_SendString(pBuf);
}

void _SendIP(PXCHAR pIP)
{
	XCHAR pBuf[16];

	p_IP2String(pIP, pBuf);
	_SendString(pBuf);
}

void _SendVal(UCHAR iVal)
{
	XCHAR pBuf[4];

	p_itoa(iVal, pBuf, 10);			
	_SendString(pBuf);
}

void _SendSeparator(PCCHAR pString)
{
	_SendNewLine(_cMaskPassword);
	_SendLine(_cMaskPassword);
	_SendLine(_cMaskPassword);
	_SendLine(pString);
	_SendLine(_cSettings);
	_SendLine(_cMaskPassword);
	_SendLine(_cMaskPassword);
	_SendLine(_cMaskPassword);
}

void _GetAll()
{
	XCHAR pBuf[18];

	switch (_iExtraDataFlag)
	{
	case 2:
		_SendSeparator(_cNetWorkSettingsLabel);

		_SendNewLine(_cIPTypeLabel);						// 13
		_SendVal(Sys_iIPType);					
		_SendRemarkLine((UCHAR)(IP_TYPE_SDRAM_LO+Sys_iIPType));

		switch(Sys_iIPType)
		{
		case IP_TYPE_PPPOE:
			_SendTabLine(_cPppIDLabel);						// 31
			_SendString(PPP_pID);

			_SendNewLine(_cPppPinLabel);					// 25
#ifdef VER_IPH_E00
			_SendLine(_cMaskPassword);
#else
			_SendString(PPP_pPassword);
#endif
			_SendNewLine(_cDNSLabel);						// 23
			_SendIP(Dns_pServerIP);

			_SendTabLine(_cDNSLabel2);						// 23
			_SendIP(Dns_pServerIP2);

			_SendTabLine(_cMacLabel);						// 23
			p_Mac2String(Local_pMacAddress, pBuf);
			_SendString(pBuf);
			break;

		case IP_TYPE_STATIC:
			_SendNewLine(_cIPLabel);						// 20
			_SendIP(Local_pIPAddress);

			_SendTabLine(_cMaskLabel);						// 28
			_SendIP(Local_pSubnetMask);

			_SendTabLine(_cRouterLabel);					// 24
			_SendIP(Local_pRouteIP);

			_SendNewLine(_cDNSLabel);						// 23
			_SendIP(Dns_pServerIP);

			_SendTabLine(_cDNSLabel2);						// 23
			_SendIP(Dns_pServerIP2);

			_SendTabLine(_cMacLabel);						// 23
			p_Mac2String(Local_pMacAddress, pBuf);
			_SendString(pBuf);
			break;

		case IP_TYPE_DHCP:
			_SendNewLine(_cMacLabel);						// 23
			p_Mac2String(Local_pMacAddress, pBuf);
			_SendString(pBuf);
			break;
		}

		_SendNewLine(_cVlan);						// 20
		_SendEnableText(VLAN_bEnable);

		if (VLAN_bEnable)
		{
			_SendTabLine(_cVlanID);				// 61
			_SendShort(VLAN_iID);
		}

		_SendSeparator(_cAudioSettingsLabel);		//Audio Settings

		_SendNewLine(_cCodec1Label);					// 20 
		_SendVal(Sys_pCodecType[0]);
		_SendRemarkLine((UCHAR)(AUDIO_TYPE_SDRAM_LO+Sys_pCodecType[0]));

		_SendTabLine(_cCodec2Label);					// 20 
		_SendVal(Sys_pCodecType[1]);
		_SendRemarkLine((UCHAR)(AUDIO_TYPE_SDRAM_LO+Sys_pCodecType[1]));

		_SendTabLine(_cCodec3Label);					// 20 
		_SendVal(Sys_pCodecType[2]);
		_SendRemarkLine((UCHAR)(AUDIO_TYPE_SDRAM_LO+Sys_pCodecType[2]));

		_SendNewLine(_cCodec4Label);					// 20 
		_SendVal(Sys_pCodecType[3]);
		_SendRemarkLine((UCHAR)(AUDIO_TYPE_SDRAM_LO+Sys_pCodecType[3]));

		_SendTabLine(_cCodec5Label);					// 20 
		_SendVal(Sys_pCodecType[4]);
		_SendRemarkLine((UCHAR)(AUDIO_TYPE_SDRAM_LO+Sys_pCodecType[4]));

		_SendTabLine(_cCodec6Label);					// 20 
		_SendVal(Sys_pCodecType[5]);
		_SendRemarkLine((UCHAR)(AUDIO_TYPE_SDRAM_LO+Sys_pCodecType[5]));

		_SendNewLine(_cVadLabel);						// 7
		_SendEnableText((BOOLEAN)((Dsp_iOptions & USE_VAD_OR) ? TRUE : FALSE));

		_SendTabLine(_cAgcLabel);						// 9
		_SendEnableText((BOOLEAN)((Dsp_iOptions & USE_AGC_OR) ? TRUE : FALSE));

		_SendTabLine(_cAecLabel);						// 9
		_SendEnableText((BOOLEAN)((Dsp_iOptions & USE_AEC_OR) ? TRUE : FALSE));

		_SendNewLine(_cAudioframeLabel);				// 16
		_SendVal(Sys_iAudioFrames);

		_SendTabLine(_cHighrateLabel);					// 14
		_SendEnableText((BOOLEAN)((Dsp_iOptions & USE_HIGH_RATE_OR) ? TRUE : FALSE));

		_SendTabLine(_cIlbcPayLoadLabel);				// 61
		_SendVal(Sys_iIlbcPType);

		_SendNewLine(_cJitterSizeLabel);				// 61
		_SendVal(Sys_iJitterSize);

		_SendTabLine(_cHandsetInputLabel);				// 18
		_SendVal(Ac97_iHandsetInputVol);

		_SendTabLine(_cHandsetOutputLabel);				// 19
		_SendVal(Ac97_iHandsetOutputVol);

#ifdef _USE_FAKE_FXO
		_SendNewLine(_cDualModeLabel);					// 22
		_SendVal(Sys_iDualMode);
		_SendRemarkLine((UCHAR)(DUAL_MODE_SDRAM_LO+Sys_iDualMode));

		if (Sys_iDualMode)
		{
			_SendTabLine(_cDualModePrefixLabel);					// 16
			_SendString(Sys_pDualModePrefix);
		}
#else
#ifndef _USE_FXS
		_SendNewLine(_cRingTypeLabel);					// 21
		_SendVal(Task_iRingType);
		_SendRemarkLine((UCHAR)(RING_TYPE_SDRAM_LO+Task_iRingType));

		_SendTabLine(_cHandfreeOutputLabel);			// 20
		_SendVal(Ac97_iHandfreeOutputVol);

		_SendTabLine(_cHandfreeInputLabel);				// 19
		_SendVal(Ac97_iHandfreeInputVol);
#endif
#endif

#ifndef CALL_MGCP
		_SendSeparator(_cPhoneSettingsLabel);		//Phone Settings

		_SendNewLine(_cDialplanLabel);					// 22
		_SendVal(Sys_iUseDialPlan);
		_SendRemarkLine((UCHAR)(DIALPLAN_SDRAM_LO+Sys_iUseDialPlan));

		switch(Sys_iUseDialPlan)
		{
		case DIALPLAN_ENABLE:
			_SendTabLine(_cAreacodeLabel);					// 14
			_SendString(Sys_pAreaCode);

			_SendNewLine(_cCountrycodeLabel);				// 18
			_SendString(Sys_pCountryCode);	

			_SendTabLine(_cIDDprefixLabel);					// 17
			_SendString(Sys_pIntlPrefix);

			_SendTabLine(_cDDDprefixLabel);					// 16
			_SendString(Sys_pLongPrefix);
			break;

		case DIALPLAN_DIALNUM:
		case DIALPLAN_PREFIX:
			_SendTabLine(_cDialNumberLabel);				// 28
			_SendString(Sys_pDialNumber);

			_SendTabLine(_cAreacodeLabel);					// 14
			_SendString(Sys_pAreaCode);

			_SendNewLine(_cCountrycodeLabel);				// 18
			_SendString(Sys_pCountryCode);	

			_SendTabLine(_cIDDprefixLabel);					// 17
			_SendString(Sys_pIntlPrefix);

			_SendTabLine(_cDDDprefixLabel);					// 16
			_SendString(Sys_pLongPrefix);

			break;

		case DIALPLAN_HOTLINE:
			_SendTabLine(_cDialNumberLabel);				// 28
			_SendString(Sys_pDialNumber);
			break;
		}

		_SendNewLine(_cInnerLineLabel);					// 22
		_SendVal(Sys_iInnerLine);
		_SendRemarkLine((UCHAR)(INNER_LINE_SDRAM_LO+Sys_iInnerLine));

		if (Sys_iInnerLine)
		{
			_SendTabLine(_cInnerLinePrefixLabel);					// 19
			_SendString(Sys_pInnerLinePrefix);
		}

#ifndef OEM_263
		_SendTabLine(_cCallWaitingLabel);  
		_SendEnableText((BOOLEAN)((Sys_iForwardType & CALL_WAITING_OR) ? TRUE : FALSE));

		if((Sys_iForwardType & FORWARD_ALWAYS_OR) || (Sys_iForwardType & FORWARD_BUSY_OR) || (Sys_iForwardType & FORWARD_NOANSWER_OR) || (Sys_iForwardType & FORWARD_POWEROFF_OR))
		{
			_SendNewLine(_cForwardNumberLabel);						// 31
			_SendString(Sys_pForwardNumber);

			_SendTabLine(_cFwdPowerOffLabel);						//  
			_SendEnableText((BOOLEAN)((Sys_iForwardType & FORWARD_POWEROFF_OR) ? TRUE : FALSE));
		}
		else
		{
			_SendNewLine(_cFwdPowerOffLabel);						//  
			_SendEnableText((BOOLEAN)((Sys_iForwardType & FORWARD_POWEROFF_OR) ? TRUE : FALSE));
		}

		_SendNewLine(_cFwdAlwaysLabel);						//  
		_SendEnableText((BOOLEAN)((Sys_iForwardType & FORWARD_ALWAYS_OR) ? TRUE : FALSE));
		_SendTabLine(_cFwdBusyLabel);						//  
		_SendEnableText((BOOLEAN)((Sys_iForwardType & FORWARD_BUSY_OR) ? TRUE : FALSE));
		_SendTabLine(_cFwdNoAnswerLabel);						//  
		_SendEnableText((BOOLEAN)((Sys_iForwardType & FORWARD_NOANSWER_OR) ? TRUE : FALSE));

		if(Sys_iForwardType & FORWARD_NOANSWER_OR)
		{
			_SendTabLine(_cRingNumberLabel);				// 12
			_SendVal(Task_iRingNumber);
		}
#endif
		_SendNewLine(_cDigitMapLabel);						//9
		_SendEnableText(Sys_bUseDigitMap);
#endif

		break;											// total 1023

	case 1:
		if(Task_bSupervisor)
		{
			_SendSeparator(_cProtocolSettingsLabel);		//Protocol Settings
#ifdef CALL_H323
			_SendNewLine(_cUseGKLabel);						// 20
			_SendEnableText(Sys_bUseService);

			_SendTabLine(_cRegisterTTLLabel);				// 61
			_SendShort(Sys_iRegisterTTL);

			_SendNewLine(_cServicetypeLabel);					// 33
			_SendVal(Sys_iServiceType);
			_SendRemarkLine((UCHAR)(SERVICE_TYPE_SDRAM_LO+Sys_iServiceType));

			switch(Sys_iServiceType)
			{
			case SERVICE_TYPE_UTSTARCOM:
			case H323_SERVICE_TYPE_ASIASOFT:
			case H323_SERVICE_TYPE_ASIAINFO:
			case H323_SERVICE_TYPE_SMARTCON:
				_SendNewLine(_cServiceAddrLabel);				// 61
				_SendString(Sys_pServiceAddr);
				break;

			default:
				_SendTabLine(_cServiceAddrLabel);				// 61
				_SendString(Sys_pServiceAddr);
				break;
			}

			_SendNewLine(_cNatTraversalLabel);						// 17
			_SendVal(Sys_iNatTraversal);
			_SendRemarkLine((UCHAR)(PROXY_SDRAM_LO+Sys_iNatTraversal));

			_SendNewLine(_cNatAddrLabel);						// 20
			_SendString(Sys_pNatAddr);

			_SendTabLine(_cNatTTLLabel);				// 61
			_SendShort(Sys_iNatTTL);
#elif defined CALL_SIP
			_SendNewLine(_cUseGKLabel);						// 20
			_SendEnableText(Sys_bUseService);

			_SendTabLine(_cRegisterTTLLabel);				// 61
			_SendShort(Sys_iRegisterTTL);

			_SendNewLine(_cServicetypeLabel);					// 33
			_SendVal(Sys_iServiceType);
			_SendRemarkLine((UCHAR)(SERVICE_TYPE_SDRAM_LO+Sys_iServiceType));

			switch(Sys_iServiceType)
			{
			case SIP_SERVICE_TYPE_MEDIARING:
			case SERVICE_TYPE_UTSTARCOM:
			case SIP_SERVICE_TYPE_SIPPHONE:
			case SIP_SERVICE_TYPE_INPHONEX:
			case SIP_SERVICE_TYPE_NET2PHONE:
			case SIP_SERVICE_TYPE_RINGCHAT:
			case SIP_SERVICE_TYPE_TONLINE:
				_SendNewLine(_cServiceAddrLabel);				// 61
				_SendString(Sys_pServiceAddr);
				break;

			default:
				_SendTabLine(_cServiceAddrLabel);				// 61
				_SendString(Sys_pServiceAddr);
				break;
			}

			switch(Sys_iServiceType)
			{
			case SIP_SERVICE_TYPE_MEDIARING:
			case SERVICE_TYPE_UTSTARCOM:
			case SIP_SERVICE_TYPE_SIPPHONE:
			case SIP_SERVICE_TYPE_INPHONEX:
			case SIP_SERVICE_TYPE_NET2PHONE:
			case SIP_SERVICE_TYPE_RINGCHAT:
			case SIP_SERVICE_TYPE_TONLINE:
				if (p_strlen_x(Sys_pServiceAddr) > 11)
				{
					if (p_strlen_x(Sys_pServiceID) > 14)
					{
						_SendNewLine(_cGKIDLabel);						// 35
						_SendString(Sys_pServiceID);
					}
					else
					{
						_SendTabLine(_cGKIDLabel);						// 35
						_SendString(Sys_pServiceID);
					}
				}
				else
				{
					_SendTabLine(_cGKIDLabel);						// 35
					_SendString(Sys_pServiceID);
				}
				break;

			default:
				if (p_strlen_x(Sys_pServiceAddr) > 19)
				{
					_SendNewLine(_cGKIDLabel);						// 35
					_SendString(Sys_pServiceID);
				}
				else if (p_strlen_x(Sys_pServiceID) > 14)
				{
					_SendNewLine(_cGKIDLabel);						// 35
					_SendString(Sys_pServiceID);
				}
				else
				{
					_SendTabLine(_cGKIDLabel);						// 35
					_SendString(Sys_pServiceID);
				}
				break;
			}

			_SendNewLine(_cNatTraversalLabel);						// 17
			_SendVal(Sys_iNatTraversal);
			_SendRemarkLine((UCHAR)(PROXY_SDRAM_LO+Sys_iNatTraversal));

			_SendNewLine(_cNatAddrLabel);						// 20
			_SendString(Sys_pNatAddr);

			_SendTabLine(_cNatTTLLabel);				// 61
			_SendShort(Sys_iNatTTL);
#elif defined CALL_MGCP
			_SendNewLine(_cServicetypeLabel);					// 33
			_SendVal(Sys_iServiceType);
			_SendRemarkLine((UCHAR)(SERVICE_TYPE_SDRAM_LO+Sys_iServiceType));

			_SendNewLine(_cServiceAddrLabel);				// 61
			_SendString(Sys_pServiceAddr);

			_SendTabLine(_cRegisterTTLLabel);				// 61
			_SendShort(Sys_iRegisterTTL);

			_SendNewLine(_cNatTraversalLabel);						// 17
			_SendVal(Sys_iNatTraversal);
			_SendRemarkLine((UCHAR)(PROXY_SDRAM_LO+Sys_iNatTraversal));

			_SendNewLine(_cNatAddrLabel);						// 20
			_SendString(Sys_pNatAddr);

			_SendTabLine(_cNatTTLLabel);				// 61
			_SendShort(Sys_iNatTTL);
#elif defined CALL_N2P
			_SendNewLine(_cServiceAddrLabel);				// 61
			_SendString(Sys_pServiceAddr);

			if (p_strlen_x(Sys_pServiceAddr) > 11)
			{
				if (p_strlen_x(Sys_pServiceID) > 14)
				{
					_SendNewLine(_cGKIDLabel);						// 35
					_SendString(Sys_pServiceID);

					_SendTabLine(_cRegisterTTLLabel);				// 61
					_SendShort(Sys_iRegisterTTL);
				}
				else
				{
					_SendTabLine(_cGKIDLabel);						// 35
					_SendString(Sys_pServiceID);

					_SendNewLine(_cRegisterTTLLabel);				// 61
					_SendShort(Sys_iRegisterTTL);
				}
			}
			else
			{
				_SendTabLine(_cGKIDLabel);						// 35
				_SendString(Sys_pServiceID);

				_SendNewLine(_cRegisterTTLLabel);				// 61
				_SendShort(Sys_iRegisterTTL);
			}
#elif defined CALL_IAX2
			_SendNewLine(_cUseGKLabel);						// 20
			_SendEnableText(Sys_bUseService);

			_SendTabLine(_cRegisterTTLLabel);				// 61
			_SendShort(Sys_iRegisterTTL);

			_SendNewLine(_cServiceAddrLabel);				// 61
			_SendString(Sys_pServiceAddr);

			_SendTabLine(_cRemoteDialplanLabel);						//  
			_SendEnableText((BOOLEAN)((Sys_iCallType & REMOTE_DIALPLAN_OR) ? TRUE : FALSE));
#elif defined CALL_WP
			_SendNewLine(_cServiceAddrLabel);				// 61
			_SendString(Sys_pServiceAddr);

			_SendTabLine(_cRegisterTTLLabel);				// 61
			_SendShort(Sys_iRegisterTTL);
#elif defined CALL_EPN
			_SendNewLine(_cServiceAddrLabel);				// 61
			_SendString(Sys_pServiceAddr);

			_SendTabLine(_cRegisterTTLLabel);				// 61
			_SendShort(Sys_iRegisterTTL);
#endif

#ifdef CALL_N2P
			_SendNewLine(_cCCIDLabel);					// 33
			_SendString(Sys_pAccount);

			if (p_strlen_x(Sys_pAccount) > 15 && p_strlen_x(Sys_pPin) > 20)
			{
				_SendNewLine(_cCCPINLabel);						// 25
#ifdef VER_IPH_E00
				_SendLine(_cMaskPassword);
#else
				_SendString(Sys_pPin);
#endif
			}
			else
			{
				_SendTabLine(_cCCPINLabel);						// 25
#ifdef VER_IPH_E00
				_SendLine(_cMaskPassword);
#else
				_SendString(Sys_pPin);
#endif
			}
#else
			_SendNewLine(_cPhoneNumberLabel);				// 31
			_SendString(Sys_pLocalNumber);

			if (p_strlen_x(Sys_pLocalNumber) > 11)
			{
				_SendNewLine(_cCCIDLabel);					// 33
				_SendString(Sys_pAccount);

				if (p_strlen_x(Sys_pAccount) > 15 && p_strlen_x(Sys_pPin) > 20)
				{
					_SendNewLine(_cCCPINLabel);						// 25
#ifdef VER_IPH_E00
					_SendLine(_cMaskPassword);
#else
					_SendString(Sys_pPin);
#endif
				}
				else
				{
					_SendTabLine(_cCCPINLabel);						// 25
#ifdef VER_IPH_E00
					_SendLine(_cMaskPassword);
#else
					_SendString(Sys_pPin);
#endif
				}
			}
			else
			{
				_SendTabLine(_cCCIDLabel);					// 33
				_SendString(Sys_pAccount);

				if (p_strlen_x(Sys_pAccount) > 23 || p_strlen_x(Sys_pPin) > 20)
				{
					_SendNewLine(_cCCPINLabel);						// 25
#ifdef VER_IPH_E00
					_SendLine(_cMaskPassword);
#else
					_SendString(Sys_pPin);
#endif
				}
				else
				{
					_SendTabLine(_cCCPINLabel);						// 25
#ifdef VER_IPH_E00
					_SendLine(_cMaskPassword);
#else
					_SendString(Sys_pPin);
#endif
				}
			}
#endif

#ifdef CALL_H323
			_SendNewLine(_cRasLabel);						// 19
			_SendShort(Sys_iRegisterPort);

			_SendTabLine(_cQ931Label);						// 18
			_SendShort(Sys_iSignalPort);

			_SendTabLine(_cH245Label);						// 18
			_SendShort(Sys_iControlPort);

			_SendNewLine(_cRtpLabel);						// 16
			_SendShort(Sys_iRtpPort);

			_SendTabLine(_cRtpTosLabel);				// 61
			_SendVal(Rtp_iTos);

			_SendTabLine(_cLocalAliasLabel);				// 25
			_SendVal(Sys_iLocalType);
			_SendRemarkLine((UCHAR)(LOCAL_TYPE_SDRAM_LO+Sys_iLocalType));

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));
#elif defined CALL_SIP
			_SendNewLine(_cRasLabel);						// 19
			_SendShort(Sys_iRegisterPort);

			_SendTabLine(_cRtpLabel);						// 16
			_SendShort(Sys_iRtpPort);

			_SendTabLine(_cRtpTosLabel);				// 61
			_SendVal(Rtp_iTos);

			_SendNewLine(_cDtmfPayLoadLabel);				// 61
			_SendVal(Sys_iDtmfPayloadType);

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));

			_SendNewLine(_cSipPrackLabel);						//  
			_SendEnableText((BOOLEAN)((Sys_iCallType & SIP_PRACK_OR) ? TRUE : FALSE));

			_SendTabLine(_cFaststartLabel);						//  
			_SendEnableText((BOOLEAN)((Sys_iCallType & FAST_START_OR) ? TRUE : FALSE));
#elif defined CALL_MGCP
			_SendNewLine(_cRasLabel);						// 19
			_SendShort(Sys_iRegisterPort);

			_SendTabLine(_cRtpLabel);						// 16
			_SendShort(Sys_iRtpPort);

			_SendTabLine(_cRtpTosLabel);				// 61
			_SendVal(Rtp_iTos);

			_SendNewLine(_cDtmfPayLoadLabel);				// 61
			_SendVal(Sys_iDtmfPayloadType);

			_SendTabLine(_cLocalAliasLabel);				// 25
			_SendVal(Sys_iLocalType);
			_SendRemarkLine((UCHAR)(LOCAL_TYPE_SDRAM_LO+Sys_iLocalType));

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));
#elif defined CALL_N2P
			_SendNewLine(_cRasLabel);						// 19
			_SendShort(Sys_iRegisterPort);

			_SendTabLine(_cRtpLabel);						// 16
			_SendShort(Sys_iRtpPort);

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));
#elif defined CALL_IAX2
			_SendNewLine(_cRasLabel);						// 19
			_SendShort(Sys_iRegisterPort);

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));

			_SendTabLine(_cRtpTosLabel);				// 61
			_SendVal(Rtp_iTos);
#elif defined CALL_WP
			_SendTabLine(_cRtpLabel);						// 16
			_SendShort(Sys_iRtpPort);

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));
#elif defined CALL_EPN
			_SendNewLine(_cRtpLabel);						// 16
			_SendShort(Sys_iRtpPort);

			_SendTabLine(_cRtpTosLabel);				// 61
			_SendVal(Rtp_iTos);

			_SendTabLine(_cDTMFLabel);						// 24 
			_SendVal(Sys_iDtmfType);
			_SendRemarkLine((UCHAR)(DTMF_TYPE_SDRAM_LO+Sys_iDtmfType));
#endif
			}
		
		_SendSeparator(_cOtherSettingsLabel);		//Other Settings
		if (Task_bSupervisor)
		{
			_SendNewLine(_cSuperPasswordLabel);					// 27
			_SendString(Sys_pSuperPassword);

			_SendTabLine(_cDebugLabel);						// 8
			_SendVal(Udp_iOutputDebug);
			_SendRemarkLine((UCHAR)(DEBUG_LEVEL_SDRAM_LO+Udp_iOutputDebug));
		}

		_SendNewLine(_cPasswordLabel);					// 27
#ifdef VER_IPH_E00
		_SendLine(_cMaskPassword);
#else
		_SendString(Sys_pPassword);
#endif		

		_SendTabLine(_cUpgradeTypeLabel);				// 25
		_SendVal(Sys_iUpgradeType);
		_SendRemarkLine((UCHAR)(UPGRADE_TYPE_SDRAM_LO+Sys_iUpgradeType));

		_SendNewLine(_cUpgradeAddrLabel);				// 57
		_SendString(Sys_pUpgradeUrl);

		_SendNewLine(_cNtsIPLabel);						// Add it for Sntp, do not calculate the byte yet.
		_SendIP(Sys_pNtsIP);

		_SendTabLine(_cDaylightLabel);						// Add it for Sntp, do not calculate the byte yet.
		_SendEnableText((BOOLEAN)((Sys_iTimeZone & TIMEZONE_DAYLIGHT_OR) ? TRUE : FALSE));

		_SendNewLine(_cTimeZoneLabel);
		_SendVal((UCHAR)(Sys_iTimeZone & TIMEZONE_DAYLIGHT_AND));
		_SendRemarkLine((UCHAR)(TIME_ZONE_SDRAM_LO+(Sys_iTimeZone & TIMEZONE_DAYLIGHT_AND)));

		_SendPrompt();									// 5
		break;											// 
	}

	_iExtraDataFlag --;
}

void _CloseTelnet()
{
	TcpClose(TELNET_SDRAM_LO);
	TcpSend(_iCurMsgPos);
	_iCurMsgPos = 0;
}

void _HandleCommand()
{
	PXCHAR_D pCur;

	pCur = _pRecvPacket;

	if (!p_memcmp_c2x(pCur, _cSetLabel, SET_LABEL_LENGTH))
	{
		pCur += SET_LABEL_LENGTH;
		if (!SetOptions(pCur) && !SetTelnetOptions(pCur))
		{
			_SendNewLine(g_cInvalidLabel);
		}
		_SendPrompt();
		return;
	}

	if (!p_memcmp_c2x(pCur, _cStoreLabel, STORE_LABEL_LENGTH))
	{
		pCur += STORE_LABEL_LENGTH;
		SysStoreOptions((UCHAR)p_atoi(pCur, 10));
		_SendPrompt();
		return;
	}

	if (!p_memcmp_c2x(pCur, _cLoadLabel, LOAD_LABEL_LENGTH))
	{
		pCur += LOAD_LABEL_LENGTH;
		SysLoadOptions((UCHAR)p_atoi(pCur, 10));
		_SendPrompt();
		return;
	}

	if (!p_memcmp_c2x(pCur, _cDefaultLabel, DEFAULT_LABEL_LENGTH))
	{
		DefaultSettings();
		SysReadOptions();
		_SendNewLine(_cRebooting);
		_bTelnetWrite = TRUE;
		_CloseTelnet();
//		OnUpdateOptions();
		return;
	}

	if (!p_strcmp_c2x(pCur, _cGetLabel))
	{
		_iExtraDataTask = TELNET_TASK_GET_ALL;
		_iExtraDataFlag = 2;
		//_GetAll();
		return;
	}

	if (!p_strcmp_c2x(pCur, _cExitLabel))
	{
		SysLoadOptions(0);
		_CloseTelnet();
		return;
	}

	if (!p_strcmp_c2x(pCur, _cWriteLabel))
	{
		_SendNewLine(_cRebooting);
		_bTelnetWrite = TRUE;
		_CloseTelnet();
		return;
	}

	_SendNewLine(_cSetLabel);
	_SendNewLine(_cGetLabel);
	_SendTabLine((PCCHAR)"list settings");
	_SendNewLine(_cStoreLabel);
	_SendLine((PCCHAR)"x\tstore current to xth settings");
	_SendNewLine(_cLoadLabel);
	_SendLine((PCCHAR)"x\tload xth settings to current");
	_SendNewLine(_cDefaultLabel);
	_SendNewLine(_cExitLabel);
	_SendNewLine(_cWriteLabel);
	_SendTabLine((PCCHAR)"save settings");
	_SendPrompt();
}

void _StartTelnet()
{
	_iState = TELNET_STATE_PASSWORD;
	_SendString(GetVersionText());
	_SendLine(g_cSettings);
	_SendNewLine(g_cPasswordPrompt);
}

void _RunTelnet()
{
	DCHAR iVal;
	DSHORT iIndex;
	PXCHAR_D pCur;

	iIndex = 0;
	while (iIndex < Adapter_iReceiveLen)
	{
		pCur = (PXCHAR)(Adapter_pReceiveTcpData+iIndex);
		iVal = pCur[0];
		if (p_IsNewLine(pCur))
		{
			_pRecvPacket[_iRecvLen] = 0;				// Andy
			if (_iState == TELNET_STATE_PASSWORD)
			{
				if (IsPassword(_pRecvPacket))
				{
					_SendPrompt();
					_iLoginRetry = 0;
					_iState = TELNET_STATE_OK;
				}
				else
				{
					_iLoginRetry ++;
					if (_iLoginRetry >= MAX_PASSWORD_RETRY)
					{
						_CloseTelnet();
					}
					else
					{
						_StartTelnet();
					}
				}
			}
			else if (_iState == TELNET_STATE_OK)
			{
				if (_iRecvLen)
				{
					_HandleCommand();
				}
				else
				{
					_SendPrompt();
				}
			}
			_iRecvLen = 0;
			return;
		}
		else if (iVal == CHAR_IAC)
		{
			iIndex += _DecodeCommand(pCur, (UCHAR)(Adapter_iReceiveLen-iIndex));
		}
		else
		{
			if (iVal != CHAR_BACKSPACE)
			{
				if ((_iRecvLen + iIndex) < MAX_RECV_LEN)
				{
					_pRecvPacket[_iRecvLen] = iVal;
					_iRecvLen ++;
// Below Andy added
					_SendChar((UCHAR)((_iState == TELNET_STATE_PASSWORD) ? CHAR_STAR : iVal));
				}
			}
			else		// iVal == CHAR_BACKSPACE
			{
				if (_iRecvLen)
				{
					_iRecvLen --;
					_SendChar(CHAR_BACKSPACE);
					_SendChar(CHAR_SPACE);
					_SendChar(CHAR_BACKSPACE);
				}
			}
// Above Andy added
			iIndex ++;
		}
	}
}

void TelnetInitialize()
{
	// Tcp init
	TcbInit(TELNET_SERVER_PORT, TELNET_TIME_OUT, TELNET_SDRAM_LO);
	_bTelnetWrite = FALSE;
	Task_bSupervisor = FALSE;

	_iCurMsgPos = 0;
	_iExtraDataFlag = 0;
	_iExtraDataTask = TELNET_TASK_NONE;
	_iRecvLen = 0;
	_iState = TELNET_STATE_INIT;
	_iLoginRetry = 0;

//	OutputString(IDS_TELNET_INIT);
}

BOOLEAN TelnetRun()
{
	if (!TcbRun(TELNET_SDRAM_LO))	return FALSE;
/*	if (Tcp_bAcceptEvent)
	{
		Tcp_bAcceptEvent = FALSE;
	}
*/
	if (Tcp_bConnectEvent)
	{
		// Send Command
		_SendChar(CHAR_IAC);
		_SendChar(CHAR_WILL);
		_SendChar(COMMAND_ECHO);
//		_SendChar(CHAR_IAC);
//		_SendChar(CHAR_WILL);
//		_SendChar(COMMAND_SUPPRESS_GA);
		_StartTelnet();
		SysStoreOptions(0);
		Tcp_bConnectEvent = FALSE;
	}
    if (Tcp_bDataEvent)
    {
		_RunTelnet();
		Tcp_bDataEvent = FALSE;
    }
	TcpSend(_iCurMsgPos);
	_iCurMsgPos = 0;
	if (TcbAcked() && _iExtraDataFlag)
	{
//		TaskWaitPacket();
		if (_iExtraDataTask == TELNET_TASK_GET_ALL)
		{
			_GetAll();
			TcpSend(_iCurMsgPos);
			_iCurMsgPos = 0;
		}
	}

    if (Tcp_bCloseEvent)
    {
		Tcp_bCloseEvent = FALSE;
		if (_bTelnetWrite)
		{
			// Upgrade options here and restart
			SysWriteOptions();
			OnUpdateOptions();
		}
		TelnetInitialize();
	}

	if (Tcp_bResetEvent)
	{
		Tcp_bResetEvent = FALSE;
		TelnetInitialize();	
	}

	return TRUE;
}

void TelnetHandleTimer()
{
//	OutputString(IDS_TELNET_TIMER);

	if (TcbHandleTimer(TELNET_SDRAM_LO))
	{
		return;
	}

	OutputString(IDS_TELNET_TIMER_FAILED);

	// Handle time out failure
	_CloseTelnet();
	TelnetInitialize();	// re-initialize
}

