#include "palm1lib.h"
#include "sdram.h"
#include "p_strs.h"

#ifdef __cplusplus
extern "C" {
#endif

#define SRAM_START          0x6000
#define SRAM_CONST_START	0x6800
#define SRAM_DMA_START		0x7000	// 7000-71ff are used to exchange data between SDRAM and SRAM

#define NE2000_RECV_CACHE	0x200
#define NE2000_SEND_CACHE	0x260

#define SDRAM_DATA_CACHE_0	0x800	
#define SDRAM_DATA_CACHE_2	0x900	

#define	Sdram_pDataCache	(SRAM_CONST_START+SDRAM_DATA_CACHE_0)
#define	Sdram_pDataCache2	(SRAM_CONST_START+SDRAM_DATA_CACHE_2)

extern volatile BOOLEAN Dsp_bFinished;
extern volatile  BOOLEAN	Modem_bInUse;

void	SdramRead(UCHAR iIndexHigh, UCHAR iIndexLow, UCHAR iDWordNum);		// Sdram_rw.c
void	SdramWrite(UCHAR iIndexHigh, UCHAR iIndexLow, UCHAR iDWordNum);

void	SdramLoadData(PCCHAR pData, USHORT iPos, USHORT iLength);			// Sdram_ld.c
//void	SdramLoadCode(PCCHAR pCode, USHORT iPos, USHORT iLength);			// Sdram_cd.c
void	SdramLoadCompressedCode(PCCHAR pLoadCode, USHORT iSdramPos, USHORT iLen);
void	SdramReadOptions();													// Sdram_op.c
void	SdramWriteOptions();
void	SdramReadFlags();
void	SdramWriteFlags();
void	SdramUpdateFlagByte(UCHAR iPos, UCHAR iVal);

void	SdramWriteData(USHORT iIndex, UCHAR iLen);
UCHAR	SdramReadData(USHORT iIndex);
UCHAR	SdramCopyData(USHORT iSrcIndex, PXCHAR pDst);
UCHAR	SdramCopyData_0(USHORT iSrcIndex, PXCHAR pDst);
void	SdramSaveData(USHORT iDstIndex, PXCHAR pSrc, UCHAR iLen);
void	SdramReadAll(USHORT iIndex);
void	SdramWriteAll(USHORT iIndexLow);
void	SdramCopyAll(USHORT iIndex, PXCHAR pDst);
void	SdramSaveAll(USHORT iIndex, PXCHAR pSrc);
void	SdramCopyEthernetFrame(USHORT iIndex, PXCHAR pDst, USHORT iLen);
void	SdramSaveEthernetFrame(USHORT iIndex, PXCHAR pSrc, USHORT iLen);
void	SdramClearDataCache();

UCHAR	SdramReadString(USHORT iIndex);														// sdram_str.c
UCHAR	SdramWriteString(USHORT iIndex);
//void	SdramSaveString(USHORT iIndex, PXCHAR pStr);
#define SdramSaveString(i, s)			SdramSaveData((i), (s), p_strlen_x(s))


void	p_ltoa(ULONG iVal, PXCHAR pBuf, UCHAR iRadix);										// p_ltoa.c

void	p_char2asc_str(PXCHAR pDst, PXCHAR pSrc, UCHAR iLen, BOOLEAN bUpper);				// p_asc.c
UCHAR	p_char2asc(UCHAR iChar, BOOLEAN bUpper);

//	void string_to_lower(PXCHAR pStr);							// p_case.c

//#define MAX_CASE_COMPARE_SIZE	64
//	BOOLEAN strcmp_lower_c2x(PXCHAR pXStr, PCCHAR pCStr);
UCHAR	p_count_space(PXCHAR pString);
UCHAR	p_count_char(PXCHAR pString, UCHAR iChar);

void	p_line_start(PXCHAR pDst);											// p_line.c
void	p_line_backup();
void	p_line_restore();
USHORT	p_line_get_len();
void	p_line_set_len(USHORT iLen);
void	p_line_inc_len(USHORT iLen);
PXCHAR	p_line_get_buf();
void	p_line_update_len();
//void	p_line_add_c(PCCHAR pStr);
#define p_line_add_c(s)			\
	{	p_strcpy_c2x(p_line_get_buf(), (s));		\
		p_line_update_len();						\
	}

//void	p_line_add_x(PXCHAR pStr);
#define p_line_add_x(s)			\
	{	p_strcpy_x2x(p_line_get_buf(), (s));		\
		p_line_update_len();						\
	}

//void	p_line_add_p(USHORT iSdramIndex);
#define p_line_add_p(i)			\
	{	SdramReadString((i));	\
		p_line_add_x((PXCHAR)Sdram_pDataCache);		\
	}

//void	p_line_add_val(USHORT iVal);
#define p_line_add_val(v)		\
	{	p_itoa((v), p_line_get_buf(), 10);	\
		p_line_update_len();				\
	}

//void	p_line_add_long(ULONG lVal);
#define p_line_add_long(l)		\
	{	p_ltoa((l), p_line_get_buf(), 10);	\
		p_line_update_len();				\
	}

//void	p_line_add_ip(PXCHAR pIPAddr);
#define p_line_add_ip(ip)		\
	{	p_IP2String((ip), p_line_get_buf());	\
		p_line_update_len();					\
	}

void	p_line_add_char(UCHAR iChar);
/*
void	p_line_add_space();
void	p_line_add_comma();
void	p_line_add_colon();
void	p_line_add_newline();
*/
#define p_line_add_space()		p_line_add_char(' ')
#define p_line_add_comma()		\
	{	p_line_add_char(',');	\
		p_line_add_char(' ');	\
	}
#define p_line_add_colon()		\
	{	p_line_add_char(':');	\
		p_line_add_char(' ');	\
	}
#define p_line_add_newline()	\
	{	p_line_add_char('\r');	\
		p_line_add_char('\n');	\
	}


/*
	void	VLANStartup();					// vlan.c
	void	VLANDisable();
	USHORT	VLANRegRead(UCHAR iPhy, UCHAR iReg);
	void	VLANRegWrite(UCHAR iPhy, UCHAR iReg, USHORT iData);
*/

// Functions in md5.c
#define MD5_SIGNATURE_SIZE      16      // 16 bytes in a MD5 message digest
void	MD5GenValue(PXCHAR pDst, PXCHAR pSrc, UCHAR iLen);

// Functions in sha1.c
/*
#define SHA1HashSize	20
//	void	SHA1GenValue(PXCHAR pDst, PXCHAR pSrc, UCHAR iLen);
	void	HMAC_SHA1(PXCHAR pDst, PXCHAR pSrc1, PXCHAR pSrc2, UCHAR iLen);
*/

#define IP_ALEN					4
#define HW_ALEN					6

#define OPTIONS_FWD_ALWAYS		1
#define OPTIONS_FWD_BUSY		2
#define OPTIONS_FWD_NOANSWER	3
#define OPTIONS_FWD_POWEROFF	4
#define OPTIONS_CALL_WAITING	5
#define CALL_WAITING_OR			0x01
#define FORWARD_ALWAYS_OR		0x10
#define FORWARD_BUSY_OR			0x20
#define FORWARD_NOANSWER_OR		0x40
#define FORWARD_POWEROFF_OR		0x80
#define FORWARD_ALWAYS_AND		0xef
#define FORWARD_BUSY_AND		0xdf
#define FORWARD_NOANSWER_AND	0xbf
#define FORWARD_POWEROFF_AND	0x7f
#define CALL_WAITING_AND		0xfe

#define OPTIONS_USE_HIGH_RATE	111
#define OPTIONS_USE_VAD			112
#define OPTIONS_USE_AGC			113
#define OPTIONS_USE_AEC			114
#define USE_HIGH_RATE_OR		0x10
#define USE_VAD_OR				0x20
#define USE_AGC_OR				0x40
#define USE_AEC_OR				0x80
#define USE_HIGH_RATE_AND		0xef
#define USE_VAD_AND				0xdf
#define USE_AGC_AND				0xbf
#define USE_AEC_AND				0x7f

#define OPTIONS_FAST_START		121
#define OPTIONS_SIP_PRACK		122
#define OPTIONS_REMOTE_DIALPLAN	123
#define REMOTE_DIALPLAN_OR		0x20
#define SIP_PRACK_OR			0x40
#define FAST_START_OR			0x80
#define REMOTE_DIALPLAN_AND		0xdf
#define SIP_PRACK_AND			0xbf
#define FAST_START_AND			0x7f

#define MAP_KEY_UNKNOWN		0xff
#define MAP_KEY_NUM			13
#define DIGITMAP_NOTMATCH		0	/* no match within the map */
#define DIGITMAP_UNDERMATCH		1	/* possible match with more keys pressed */
#define DIGITMAP_MATCH			2	/* exact match with one item in the map */
#define DIGITMAP_TOMATCH		3	/* match only with x.T */

#define	OPTIONS_DAYLIGHT		201
#define TIMEZONE_DAYLIGHT_OR	0x80
#define TIMEZONE_DAYLIGHT_AND	0x7f

#define	ITSP_NULL				0
#define	ITSP_VIDA				1
#define	ITSP_CHIEF				2
#define	ITSP_MEDIARING			3
#define	ITSP_TEK				4
#define	ITSP_TTN				5
#define	ITSP_TELESYNERGY		6
#define	MAX_ITSP_NUM			7

extern		XCHAR	Sys_iServiceType;
extern		XCHAR	Sys_pAccount[MAX_ACCOUNT_LEN+1];
extern		XCHAR	Sys_pPin[MAX_PIN_LEN+1];
extern		XCHAR	Sys_pServiceIP[IP_ALEN];
extern		XCHAR	Sys_pGWIP[IP_ALEN];
extern		BOOLEAN	Sys_bUseService;
extern		XCHAR	Sys_pServiceID[MAX_SERVICE_ID_LEN+1];
extern		volatile DCHAR	Sys_iDtmfType;
extern		XCHAR	Sys_iDtmfPayloadType;
extern		XCHAR	Sys_iIlbcPType;
extern		XSHORT	Sys_iSignalPort;
extern		XSHORT	Sys_iRegisterPort;
extern		XSHORT	Sys_iControlPort;
extern		XSHORT	Sys_iServicePort;
extern		XSHORT	Sys_iNatPort;
extern		XCHAR	Sys_iCallType;
extern		XCHAR	Sys_iLocalType;
extern		XCHAR	Sys_iAudioType;
extern	  XCHAR   Sys_iJitterSize;
extern		XCHAR	Sys_iAudioFrames;
extern		XSHORT	Sys_iRtpPort;
extern		XCHAR	Sys_pLocalNumber[PHONE_NUMBER_LEN+1];
extern		XCHAR	Sys_pRemoteNumber[DIALED_MAX_LEN+1];
extern		XCHAR	Sys_pPeerDisplay[PHONE_NUMBER_LEN+1];
extern		XCHAR	Sys_iForwardType;
extern		XCHAR	Sys_pForwardNumber[FORWARD_NUMBER_LEN+1];
extern		XCHAR	Sys_pCodecType[SYS_CODEC_NUM];

// dial plan settings
extern		XCHAR	Sys_pCountryCode[MAX_COUNTRY_CODE_LEN+1];
extern		XCHAR	Sys_pIntlPrefix[MAX_INTL_PREFIX_LEN+1];
extern		XCHAR	Sys_iUseDialPlan;
extern		XCHAR	Sys_pAreaCode[MAX_AREA_CODE_LEN+1];
extern		XCHAR	Sys_pLongPrefix[MAX_LONG_PREFIX_LEN+1];
extern		XCHAR	Sys_iInnerLine;
extern		XCHAR	Sys_pDualModePrefix[MAX_DUALMODE_PREFIX_LEN+1];
extern		XCHAR	Sys_pInnerLinePrefix[MAX_INNERLINE_PREFIX_LEN+1];
extern		XCHAR	Sys_iDualMode;

// System settings
extern		XCHAR	Sys_pPassword[MAX_PASSWORD_LEN+1];
extern		XCHAR	Sys_pSuperPassword[MAX_SUPERPASSWORD_LEN+1];
extern		XCHAR	Sys_pServiceAddr[MAX_DNS_ADDR_LEN+1];
extern		XCHAR	Sys_pUpgradeUrl[MAX_DNS_ADDR_LEN+1];
extern		XCHAR	Sys_pNatAddr[MAX_DNS_ADDR_LEN+1];
extern		XCHAR	Sys_pDialNumber[DIAL_NUMBER_LEN+1];
extern		XCHAR	Sys_iUpgradeType;
extern		XCHAR	Sys_iUpgradeProtocol;
extern		PXCHAR	Sys_pUpgradeAddr;

// Time settings
extern		XCHAR	Sys_iTimeZone;
extern		XCHAR	Sys_pNtsIP[IP_ALEN];

// Proxy settings
extern		XCHAR	Sys_iNatTraversal;
extern		XCHAR	Sys_pProxyIP[IP_ALEN];
extern		BOOLEAN Sys_bNATed;
extern	  XCHAR   Sys_pProxyServerIP[IP_ALEN];
extern	  XSHORT	Sys_iNatTTL;

// Tcpip settings
extern		XCHAR	Local_pIPAddress[IP_ALEN];
extern		XCHAR	Local_pSubnetMask[IP_ALEN];
extern		XCHAR	Local_pRouteIP[IP_ALEN];
extern		XCHAR	Local_pMacAddress[HW_ALEN];
extern		XCHAR	Dns_pServerIP[IP_ALEN];
extern		XCHAR	Dns_pServerIP2[IP_ALEN];
extern		XCHAR	Udp_iOutputDebug;
extern		XCHAR	Sys_iIPType;
extern		XCHAR	PPP_pID[PPP_ID_LEN+1];
extern		XCHAR	PPP_pPassword[PPP_PASSWORD_LEN+1];
extern		BOOLEAN VLAN_bEnable;
extern		XSHORT	VLAN_iID;


// Dsp settings
extern		XCHAR	Dsp_iOptions;

// DigitMap settings
extern		XCHAR	Map_iTimeOut;
extern		BOOLEAN Sys_bUseDigitMap;
extern		XCHAR	Sys_iDigitMapNum;

// Misc
extern		XCHAR	Sys_pRemoteIP[IP_ALEN];
extern		XLONG	Sys_lCurrentTime;
extern		XCHAR	Sys_pProgramButtons[PROGRAM_BUTTONS_NUM];
extern		XCHAR	Sys_iPhoneAttr;

extern		XSHORT	Sys_iHttpsPort;

extern		XCHAR	Sys_iNetRetry;
extern		XSHORT	Sys_iRegisterTTL;

extern		XCHAR	Rtp_iTos;

extern		XCHAR	Sys_iProtocol;

extern		XCHAR	Sys_iDeviceAttr;

// Functions in asn1util.c
#define MAX_ASN1_EXTENSION_NUM	32

void	AsnEncodeStart(PXCHAR pMsgBuf);
USHORT	AsnEncodeStop();

void	AsnDecodeStart(PXCHAR pMsgBuf);

void	AsnDecodeBackup();
void	AsnDecodeRestore();
PXCHAR	AsnGetCurrentPointer();
USHORT	AsnGetCurrentPos();
void	AsnSetCurrentPos(USHORT iPos);
UCHAR	AsnGetLastDecodeLen();

void	AsnEncodeBOOL(BOOLEAN bVal);
BOOLEAN	AsnDecodeBOOL();

void	AsnEncodeUnsignedChar(UCHAR iVal, UCHAR iMin, UCHAR iMax);
UCHAR	AsnDecodeUnsignedChar(UCHAR iMin, UCHAR iMax);

void	AsnEncodeUnsignedShort(USHORT iVal, USHORT iMin);
USHORT	AsnDecodeUnsignedShort(USHORT iMin);

ULONG	AsnDecodeInteger(ULONG lMin);

void	AsnEncodeShortInteger(USHORT iLen);
USHORT	AsnDecodeShortInteger();

void	AsnEncodeOctetString(UCHAR iMin, UCHAR iMax, PXCHAR pOctet);
PXCHAR	AsnDecodeOctetString(UCHAR iMin, UCHAR iMax);

void	AsnEncodeBmpString(UCHAR iMin, USHORT iMax, PXCHAR pStr);
PXCHAR	AsnDecodeBmpString(UCHAR iMin, USHORT iMax);

void	AsnEncodeIa5String(UCHAR iMin, UCHAR iMax, PXCHAR pStr);
PXCHAR	AsnDecodeIa5String(UCHAR iMin, UCHAR iMax);

void	AsnEncodeObjectID(UCHAR iLen, PCCHAR pID);
PXCHAR	AsnDecodeObjectID();

void	AsnEncodeExtensionsNumber(UCHAR iNum);

UCHAR	AsnDecodeExtensions(PXCHAR pExtensions, BOOLEAN bSkipAll);
void	AsnSkipExtension(UCHAR iExtension);
void	AsnSkipChoiceExtension();

void	AsnEncodeExtensionStart();
void	AsnEncodeExtensionStop();

void	AsnEncodeExtensionLength(USHORT iLen);
USHORT	AsnDecodeExtensionLength();

void	AsnEncodeChoice(UCHAR iIndex, UCHAR iFixed, BOOLEAN bHasExtension);
UCHAR	AsnDecodeChoice(UCHAR iFixed, BOOLEAN bHasExtension);

void	AsnEncodeSequenceOfNumber(UCHAR iNum);
UCHAR	AsnDecodeSequenceOfNumber();

UCHAR	AsnDecodeSetSize(UCHAR iMin, UCHAR iMax);

void	AsnEncodePadding();
void	AsnDecodePadding();

BOOLEAN IsDisplayChar(UCHAR iChar);
BOOLEAN	IsE164Char(UCHAR iChar);
UCHAR	Digit_2_E164(UCHAR iDigit);				
UCHAR	E164_2_Digit(UCHAR iE164);

#ifdef __cplusplus
}
#endif
