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

#include <reg52.h>

BOOLEAN PIO_bHandsetUp;

void Ac97Stop()
{
}

void OnProvisionEnd()
{
	OnLogoff();
	EA = 0;		// disable all interrupts
	DisplayStatus(g_cBooting, 255, 8);
//	Ne2000Initialize_Step1();
//	SysSleep(1000000);
	Ne2000Initialize_Step2();
	SwitchPage(12);	// continue to check other options in page12
}

void OnLogoff()
{
	if (PPPoE_bInUse)
	{
		DisplayString(g_cPPPoE, 0);
		DisplayString(g_cDisconnecting, 1);
		PPPoEDisconnect();
	}
	UdpDebugUpdate();
}

BOOLEAN _ResolveUpgradeAddr()
{
	XCHAR pIP[IP_ALEN];

	if (!p_strlen_x(Sys_pUpgradeAddr))	return TRUE;	// return TRUE means go to next step directly

	if (p_String2IP(Sys_pUpgradeAddr, pIP))
	{
		if (IsValidIP(pIP))
		{
			OnGetUpgradeIP(pIP);
		}
		else
		{
			return TRUE;
		}
	}
	else
	{
		DnsSend(Sys_pUpgradeAddr, FALSE);
	}
	return FALSE;
}


void TaskInitialize()
{
	DisplayString(g_cProvisioning, 0);
	UpgradeInitialize();

#ifndef OEM_NET2PHONE 
	switch (Sys_iUpgradeProtocol)
	{
	case UPGRADE_PROTOCOL_FTP:
		FtpInitialize();
		break;
	case UPGRADE_PROTOCOL_TFTP:
		TftpInitialize();
		break;
	case UPGRADE_PROTOCOL_HTTP:
		HttpClientInitialize();
		break;
	case UPGRADE_PROTOCOL_UNKNOWN:
		break;
	}
#else
	HttpClientInitialize();
#endif

	if (_ResolveUpgradeAddr())
	{
		OnProvisionEnd();
	}
}


void TaskSignalMsg(UCHAR iType)
{
	switch (iType)
	{
/*	case SIGNAL_IP_CONFLICT:
		DisplayString(g_cDuplicatedIP, 0);
		SysSleep(5000000);
		break;
*/
	case SIGNAL_DHCP_FAILED:
		OnProvisionEnd();
		break;
	}
}

void TaskHandleTimer()
{
	UpgradeHandleTimer();

#ifndef OEM_NET2PHONE 
	switch (Sys_iUpgradeProtocol)
	{
	case UPGRADE_PROTOCOL_FTP:
		FtpControlHandleTimer();
		FtpDataHandleTimer();
		break;

	case UPGRADE_PROTOCOL_TFTP:
		TftpHandleTimer();
		break;

	case UPGRADE_PROTOCOL_HTTP:
		HttpClientHandleTimer();
		break;

	case UPGRADE_PROTOCOL_UNKNOWN:
		break;
	}
#else
	HttpClientHandleTimer();
#endif
}

void OnGetUpgradeIP(PXCHAR pIP)
{
#ifdef OEM_NET2PHONE
	VoiceLineStartUpgrade(pIP, HTTP_SERVER_PORT);
#else
	p_memcpy_ip(Upgrade_pServerIP, pIP);
	if (Sys_iUpgradeType)
	{
		switch (Sys_iUpgradeProtocol)
		{
		case UPGRADE_PROTOCOL_FTP:
			FtpUpgrade(UPGRADE_GET_SET);
			break;
		case UPGRADE_PROTOCOL_TFTP:
			TftpUpgrade(UPGRADE_GET_SET);
			break;
		case UPGRADE_PROTOCOL_HTTP:
			HttpUpgrade(UPGRADE_GET_SET);
			break;
		case UPGRADE_PROTOCOL_UNKNOWN:
			OnProvisionEnd();
			break;
		}
	}
	else
	{
		OnProvisionEnd();
	}
#endif
}


void TaskDnsCompleted()				
{
	OnGetUpgradeIP(Dns_pResolvedIP);
}
			
void TaskDnsFailed()				
{
	OutputString(IDS_DNS_FAILED);
	OnProvisionEnd();
}

void TaskWaitPacket()
{
	SysSleep(p_rand()&1023);
}

void TaskSendPacket(USHORT iLen, PXCHAR pPacket)
{
	Ne2000Send(iLen, pPacket);
}

BOOLEAN	TaskRunTcp()
{
	if (UpgradeRun())		return TRUE;

#ifdef OEM_NET2PHONE
	if (HttpClientRun())	return TRUE;
#else
	if (Sys_iUpgradeProtocol == UPGRADE_PROTOCOL_FTP)
	{
		if (FtpDataRun())		return TRUE;
		if (FtpControlRun())	return TRUE;
	}
	else if (Sys_iUpgradeProtocol == UPGRADE_PROTOCOL_HTTP)
	{
		if (HttpClientRun())	return TRUE;
	}
#endif

	return FALSE;
}

BOOLEAN TaskRunUdp()
{
#ifndef OEM_NET2PHONE
	if (Sys_iUpgradeProtocol == UPGRADE_PROTOCOL_TFTP)
	{
		if (TftpRun())	return TRUE;
	}
#endif

	// Unknown udp data, ignore
	return FALSE;
}
