#include "version.h"
#include "PalmType.h"
#include "p_util.h"
#include "p_tcpip.h"
#include "debug.h"
#include "port.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DEVICE_ATTR_NONE		0x00
#define DEVICE_ATTR_SPEAKER		0x01
#define DEVICE_ATTR_DUALMODE	0x02
#define DEVICE_ATTR_FXO			0x04

#define MAP_TIME_OUT_0		30
#define MAP_TIME_OUT_1		30
#define MAP_TIME_OUT_2		5
#define NONE_MAP_TIME_OUT	10

#define	SECONDS_PER_MINUTE	60
#define	SECONDS_PER_HOUR	3600
#define SECONDS_PER_DAY		86400
#define DAYS_PER_4_YEAR		1461
#define DAYS_IN_YEAR_0		366
#define DAYS_IN_YEAR_1		731
#define DAYS_IN_YEAR_2		1096
#define MONTHS_PER_YEAR		12
#define DAYS_PER_WEEK		7
#define SECONDS_PER_99HRS	356400
#define SECONDS_FIRST_4_YEAR	126144000	// from 1900 - 1903, no leap year in these 4 years.

extern CCHAR g_cWeekDay[DAYS_PER_WEEK][4];
extern CCHAR g_cMonth[MONTHS_PER_YEAR][4];

extern CCHAR g_cSoftwareVersion[5];

extern CCHAR Sys_cOptions[OPTIONS_TOTAL_LEN];
extern CCHAR Sys_cFlags[MAX_TAG_LENGTH*2];

extern CCHAR g_cPasswordPrompt[];
extern CCHAR g_cSettings[];

extern CCHAR g_cEnableLabel[];
extern CCHAR g_cDisableLabel[];
extern CCHAR g_cInvalidLabel[];
extern CCHAR g_cSuperLabel[];

extern CCHAR g_cBooting[];
extern CCHAR g_cReady[];
#ifdef VER_IPH_E00
extern CCHAR g_cLocalIP[IP_TYPE_MAX][17];
#else
extern CCHAR g_cLocalIP[];
#endif
extern CCHAR g_cSubnetMask[]; 
extern CCHAR g_cRouterIP[];
extern CCHAR g_cServiceIP[];
extern CCHAR g_cPhoneNumber[];
extern CCHAR g_cMissedCalls[];
extern CCHAR g_cAnsweredCalls[];
extern CCHAR g_cDialedCalls[];
extern CCHAR g_cEmpty[];
extern CCHAR g_cMemory[];
extern CCHAR g_cSpeedDial[];
extern CCHAR g_cVolPlus[];
extern CCHAR g_cVolMinus[];
extern CCHAR g_cRedial[];
extern CCHAR g_cCall[];
extern CCHAR g_cCalling[];
extern CCHAR g_cRinging[];
extern CCHAR g_cAnswering[];
extern CCHAR g_cConnected[];
extern CCHAR g_cBlank[];	
extern CCHAR g_cFailed[];
extern CCHAR g_cHandsetUp[];
extern CCHAR g_cHandsetDown[];
extern CCHAR g_cNetwork[];
extern CCHAR g_cDHCP[];
extern CCHAR g_cPPPoE[];
extern CCHAR g_cLogon[]; 
extern CCHAR g_cLogoff[]; 
extern CCHAR g_cUpdating[];
extern CCHAR g_cPleaseWait[];
extern CCHAR g_cTestMode[];
extern CCHAR g_cStart[];
extern CCHAR g_cStop[];
extern CCHAR g_cRestart[];
extern CCHAR g_cPercentStart[]; 
extern CCHAR g_cTimeStart[];
extern CCHAR g_cConnecting[];
extern CCHAR g_cDisconnecting[];
extern CCHAR g_cModem[];	
extern CCHAR g_cPeerHangup[];
extern CCHAR g_cInvalidNumber[];
extern CCHAR g_cPeerBusy[];
extern CCHAR g_cUnknownNumber[];
extern CCHAR g_cCallHold[];
extern CCHAR g_cPleaseDial[];
extern CCHAR g_cNoAnswer[];
extern CCHAR g_cPeerReject[];
extern CCHAR g_cLocalHangup[];
extern CCHAR g_cWaitLogon[];
extern CCHAR g_cNetworkError[];
extern CCHAR g_cServiceClearCall[];
extern CCHAR g_cPleaseHangup[];
extern CCHAR g_cCallForward[];
extern CCHAR g_cDuplicateNumber[];
extern CCHAR g_cCallRestricted[];
extern CCHAR g_cCallTerminated[];
extern CCHAR g_cRecharge[];
extern CCHAR g_cAccountExpired[];
extern CCHAR g_cInvalidPassword[];
extern CCHAR g_cBlockedNumber[];
//extern CCHAR g_cDuplicatedIP[];
extern CCHAR g_cCallWaiting[];
extern CCHAR g_cNewMessage[];
extern CCHAR g_cProtocol[];
extern CCHAR g_cCallTransfer[];
extern CCHAR g_cProvisioning[];
extern CCHAR g_cCallMuted[];
// options

extern XCHAR Ac97_iHandsetOutputVol, Ac97_iHandsetInputVol, Ac97_iHandfreeInputVol, Ac97_iHandfreeOutputVol;
extern volatile DCHAR Task_iRingType;
extern XCHAR Task_iRingNumber;
extern BOOLEAN Task_bSupervisor;

#define DNS_TYPE_NONE		0
#define DNS_TYPE_SERVICE	1
#define DNS_TYPE_PROXY		2
#define DNS_TYPE_SNTP		3
extern XCHAR Task_iDnsType;

// Key
extern XCHAR Key_pBuf[MAX_KEYBUF_LEN+1];
extern DCHAR Key_iCurrentID;
extern XCHAR PIO_iHookFlashTimer;

extern BOOLEAN PIO_bHandsetUp, PIO_bHandFree;
#ifdef _USE_FAKE_FXO
extern BOOLEAN Line_b1S1OStatus;
#endif
extern volatile DCHAR Slic_iDtmfKey;
extern volatile BOOLEAN Slic_bDtmfFlag;

extern volatile DCHAR Ac97_iDtmfCode;
extern volatile DSHORT Ac97_iDtmfDuration;
extern volatile DCHAR Ac97_iBuf;

extern BOOLEAN Sys_bHighSpeed;

extern volatile DCHAR Dsp_iType;

#if defined AM29LV002T
#define OPTIONS_PAGE	3
#elif defined AM29LV004T || defined MX29LV004T || defined AM29F040
#define OPTIONS_PAGE	7
#elif defined AM29LV008T
#define OPTIONS_PAGE	15
#elif defined MT28F016
#define OPTIONS_PAGE	31
#endif

#if defined AM29LV004T || defined MX29LV004T || defined AM29LV008B
#define SETTINGS_PAGE	7
#elif defined AM29LV008T || defined MX29LV008B || defined MT28F016
#define SETTINGS_PAGE	15
#endif

void OnOutgoingData(UCHAR iLen, PXCHAR pData);

// Functions in Ac97.c
void Ac97SetOutputVol();
void Ac97SetInputVol();
void Ac97OutputVolHigher();
void Ac97OutputVolLower();
void Ac97InputVolHigher();
void Ac97InputVolLower();
void Ac97OnHandsetChange();

void Ac97InitBuffer();
void Ac97SwapBuffer();

void Ac97SetInputSampleRate(BOOLEAN b8KSampling);

void Ac97Start();
void Ac97Stop();

// Functions in Dsp.c
void DspStart();
void DspCall();
void DspHandleInterrupt();

void DspHandleDecodeData(UCHAR iLength, PXCHAR pData);
BOOLEAN DspHandleEncodeData();

void DspClearDecodeData();

// Functions in Options.c
void SetUpgradeAddr();
void SysReadOptions();
void SysWriteOptions();
void SysLoadOptions(UCHAR iIndex);
void SysStoreOptions(UCHAR iIndex);
void WriteTcpipOptions();
void SysLogoff();
void OnLogoff();
void OnReboot();
BOOLEAN IsPassword(PXCHAR pSrc);
void OnUpdateOptions();
void OnUpdateRingFile(ULONG lFileLength);
void OnSwitchUpgrade(UCHAR iPage);

// Functions in telnet_options.c
BOOLEAN SetOptions(PXCHAR pCommandLine);
BOOLEAN SetTelnetOptions(PXCHAR pCommandLine);

// Functions in http_options.c
BOOLEAN SetHttpOptions(PXCHAR pCommandLine);

// Functions in version.c
PXCHAR GetVersionTextEx(BOOLEAN bEng, BOOLEAN bCfg);
PXCHAR GetVersionText();
PXCHAR GetHardwareVersion();
PXCHAR GetSoftwareVersion();
BOOLEAN CheckFileVersion(UCHAR iPage);
void CopyFileToOption(UCHAR iNum, UCHAR iStart);
BOOLEAN IsUpgradeFileOK(UCHAR iSdramHi);
BOOLEAN IsFirmwareLenOK(ULONG iFileLen);
void CreateUpgradeFile();
BOOLEAN IsUpgrading();

// Functions in SpeedDial.c
PXCHAR ReadSpeedDial(UCHAR iIndex);		// index range: 0-223
void WriteSpeedDial(PXCHAR pBuf, UCHAR iIndex);

// Functions in Update.c
void RunUpgrade();

#ifdef _MCS51
// Functions in ReadFile.c
void ReadFileOpen(UCHAR iSdramHi);
void ReadFile(UCHAR iLength);
#endif

// Functions in lcd.c
void DisplayInitialize();
void DisplayClearScreen();
void Display(PXCHAR pData, UCHAR iLine, UCHAR iPos, BOOLEAN bPadding);

void DisplayVal(USHORT iVal, PCCHAR pPrefix, UCHAR iLine);
void DisplayCursor(BOOLEAN bShow);
void DisplayStatus(PCCHAR pStatus, UCHAR iStatus, UCHAR iNum);

#ifdef VER_IPH_E00
void VidaShowDuration(ULONG duration);
#endif

void DisplayLine(PXCHAR pData, UCHAR iLine);
void DisplayString(PCCHAR pDisplay, UCHAR iLine);

// Functions in display.c
void DisplayFileStatus();
void GetTimeDisplay(ULONG lTime, PUCHAR pBuf);
void GetDateTimeDisplay(PXCHAR pBuf, BOOLEAN bLcdDisplay, ULONG lTime);
void DisplayOutputVol();

// Functions in key.c
void KeypadStartup();
void KeypadHandleInterrupt();
UCHAR KeypadRead();
void KeypadClear();
void KeypadBackspace();
void KeypadRun();
void IoKeyHandleInterrupt();
void OnHandset();
void HandleNumberKeys();
void HandleFunctionKeys();
void OnHookUp(BOOLEAN bUp);
void OnHookFlash();
BOOLEAN GetNetLinkStatus();

// Functions in Telnet.c
void TelnetInitialize();
BOOLEAN TelnetRun();
void TelnetHandleTimer();

// Functions in Http.c
void HttpInitialize();
BOOLEAN HttpRun();
void HttpHandleTimer();

// Functions in stun.c
#define STUN_TASK_NONE		0
#define STUN_TASK_LOGON		1
#define STUN_TASK_CALL		2
#define STUN_TASK_ANSWER	3
#define STUN_TASK_CHECKNAT	4
void StunInitialize();
BOOLEAN StunRun();
void StunHandleTimer();
void StunCheckIPPort(UCHAR iTask, USHORT iSrcPort);

extern BOOLEAN Sntp_bFinished; 
void SntpInitialize();
void SntpSend(PXCHAR pServerIP);
void SntpRetry();
void SntpRun();
void SntpHandleTimer();
void SntpStart();
void SntpDisplayHandleTimer();

// Functions in upgrade.c
void UpgradeInitialize();
BOOLEAN UpgradeRun();
void UpgradeHandleTimer();

// Functions in report.c
void ReportRun();

// Functions in mapMsg.c
void MapLoadMsg();
void p_addEntry(PXCHAR pMapEntry);
UCHAR p_matchMap(PXCHAR pEvent, UCHAR iEventNum);

// Functions in std_Chip.c
extern volatile BOOLEAN Timer_bIndicated;		// indicate 0.02 second timer
extern volatile BOOLEAN Timer_bIndicated2;
void ControlInitialize();

// Functions in function.c
BOOLEAN IsCallMode();
void ProtocolLogon();
void HangupPhone();
void OnGetUpgradeIP(PXCHAR pIP);

// Functions in std_func.c
void OnHandFree();

// boot.c
void BootIndication(UCHAR iPos);
void write_data_byte(UCHAR ch);
void write_control_byte(UCHAR ch);
void SysSleep(ULONG lUSecond);

// Functions in serial.c
#ifdef _USE_RS232
void SerialHandle();
void SerialRun();
void SerialInitialize();
#endif

// etalk.c
void eTalkHandleAdmCfm(PXCHAR pMsg);
void eTalkHandleRegCfm(PXCHAR pMsg);

void IpnHandleAdmCfm(PXCHAR pMsg);
void IpnHandleRegCfm(PXCHAR pMsg);

void AsiainfoHandleAdmCfm(PXCHAR pMsg);

// loadstring.c
#define SNTP_DNS_NUM		2
void LoadStrings();

// Functions in pcmring.c
void LoadPcmRing();

// Functions in loadflag.c
void LoadFlags();

// Functions in default.c
extern BOOLEAN	Sys_bLoadDefault;
extern CCHAR Sys_cSafeOptions[512];

void DefaultSettings();

// Functions in slic.c
#ifdef _USE_FXS
extern volatile DCHAR Slic_iCidStatus;
#define CID_STATUS_START_OR		0x01
#define CID_STATUS_START_AND	0xfe
#define CID_STATUS_FINISH_OR	0x02
#define CID_STATUS_FINISH_AND	0xfd

void SlicStartRing();
void SlicStopRing();
void SlicRingHandleTimer(UCHAR iVal);
#endif

#ifdef _USE_FXS_AM79R70
void SlicIdleMode();
void SlicOHTMode();
void SlicActiveMode();
void SlicStandbyMode();
void SlicActivePRMode();
#endif

#ifdef _USE_FXS_AG1170
void SlicHandleTimer(UCHAR iVal);
void SlicActivePolarityReversal();
#endif

// fake_fxo.c
#ifdef _USE_FAKE_FXO
void DualModeStartup();
void DualModeHandleTimer();
void SwitchToFXO(BOOLEAN bFXO);
void CheckDualModePrefix();
void CheckDualModeStatus();
void DualModeRun();
void HandleRingIn();
void HandleOffHook();
#endif

// Functions in http_ld.c
void HttpLoadMsg();

// Functions in ne2000.c, ne2000_1.c, ne2000_2.c
BOOLEAN	Ne2000Initialize();													// Ne2000.c
void	Ne2000Run();
void	Ne2000Send(USHORT iLength, PXCHAR pData);
void	Ne2000Read(USHORT sAddr, PXCHAR pBuf, USHORT sLen);
void	Ne2000Write(USHORT sAddr, PXCHAR pBuf, USHORT sLen);

void	Ne2000HandleInterrupt();
void	Ne2000Initialize_Step1();											// Ne2000_1.c	
void	Ne2000Initialize_Step2();											// Ne2000_2.c	

#ifdef __cplusplus
}
#endif

