#include <reg52.h>

#include "p_apps.h"
#include "palmchip.h"
#include "v_task.h"
#include "p_http.h"
#include "page12.h"

void _OnImpossibleKey()
{
	BootIndication(0);
	CopyFileToOption(0, 0);
	DebugCString((PCCHAR)"");
	DisplayCursor(TRUE);
	DisplayInitialize();
	G7231GetFrameLen(0);
	GetHardwareVersion();
	GetSoftwareVersion();
	GetVersionText();
	HttpSendError();
	HttpSendOK(0, HTTP_CT_TEXT_HTML);
	IsBroadcastIP((PXCHAR)0);
	IsFirmwareLenOK(0);
	IsFunctionMode();
	IsInCall();
	IsPassword((PXCHAR)0);
	IsUpgrading();
	OnReboot();
	OnUpdateOptions();
	OnUpdateRingFile(0);
	OutputPXCHAR((PXCHAR)1, 0);
	p_atol((PXCHAR)0, 0);
	p_IsNewLine((PXCHAR)0);
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
	p_Mac2String((PXCHAR)0, (PXCHAR)0);
	p_String2Mac((PXCHAR)0, (PXCHAR)0);
	ProxyGetLocalIP((PXCHAR)0);
	SdramCopyData(0, (PXCHAR)0);
	SdramCopyData_0(0, (PXCHAR)0);
	SoloFileCloseRead();
	SoloFileGetSize();
	SoloFileIsRunning();
	SoloFileRead((PXCHAR)0, 0);
	SoloFileSetLimit(0);
	SoloFileWriteSdramCache((UCHAR)256);
	SysLoadOptions(0);
	SysStoreOptions(0);
	TaskWaitPacket();
	TcbConnected(0);
	UdpSetTos(0, 0);
}

void LeavePage12()
{
	SwitchPage(6);	// continue to load g.729
	_OnImpossibleKey();
}

void main(void)
{
	DCHAR iCounter = 0;

#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP
	SdramReadOptions();
	if (X_VAL(Sdram_pDataCache, OPTIONS_NAT_TRAVERSAL) == NAT_TRAVERSAL_ENABLE)
	{
		p_memcpy_x2x_0(Sys_pNatAddr, (PXCHAR)(Sdram_pDataCache+OPTIONS_NAT_DNS), MAX_DNS_ADDR_LEN);
		if (p_String2IP(Sys_pNatAddr, Sys_pProxyServerIP))
		{
			if (IsPublicIP(Sys_pProxyServerIP))
			{
				SdramUpdateFlagByte(FLAGS_PAGE12_ATTR, PAGE12_FIXED_IP);
				LeavePage12();
			}
		}
	}
	else
	{
		LeavePage12();
	}
#else
	LeavePage12();
#endif

	SysReadOptions();

	ControlInitialize();
	if (!Ne2000Initialize())
	{
		SwitchPage(0);
	}
	TcpipInitialize();
	switch (Sys_iIPType)
	{
	case IP_TYPE_DHCP:
		DisplayString(g_cDHCP, 0);
		DisplayString(g_cConnecting, 1);
		break;
	case IP_TYPE_PPPOE:
		DisplayString(g_cPPPoE, 0);
		DisplayString(g_cConnecting, 1);
		break;
	}

    while (1)
	{
		Ne2000Run();
		UdpDebugUpdate();
		if (Timer_bIndicated)	// indicate 0.02 second timer
		{
			iCounter ++;
			if (iCounter == 50)
			{
				TcpipHandleTimer();
				iCounter = 0;
			}
			Timer_bIndicated = FALSE;
		}
	}
}

void PalmInterrupt() interrupt 2	// using 2
{
	DCHAR iDPS, iIntVal;

	iDPS = DPS;	// Push DPS value
	DPS = 0;

    iIntVal = XBYTE[INT_VALUE];

    if (iIntVal & INT_MASK_IO)
	{
		// Check PIO 3, 4, 5, 6, 12, 13, 14, 15 key
		XBYTE[IO_LO_INTCLR] = 0xff;	// Clear all io edge irq
		XBYTE[IO_HI_INTCLR] = 0xff;	// Clear all io edge irq
		XBYTE[IO_HHI_INTCLR] = 0xff;	// Clear all io edge irq
	}
	if (iIntVal & INT_MASK_DMA)
	{
		XBYTE[SDRAM_DMA_INT1] = DMA_MASK_AC97_OUT+DMA_MASK_AC97_IN;
	}
	if (iIntVal & INT_MASK_DSP)
	{
		XBYTE[INT_CLR] |= INT_MASK_DSP;
	}
	if (iIntVal & INT_MASK_KEY)
	{
		XBYTE[KEY_INTCLR] = 0x7f;	// Clear keypad irq
	}

	DPS = iDPS;	// Pop DPS value
}
