#include "PalmType.h"
#include "p_util.h"
#include "p_tcpip.h"

volatile BOOLEAN Task_bCaller, Task_bConnected;
BOOLEAN Task_bLogoned, Task_bCallHold, Task_bOnHold, Task_bCallTransfer, Task_bRingBack, Task_bConnecting, Task_bDisconnecting, Task_bNewMessage, Task_bCallWaiting;

BOOLEAN IsFunctionMode()
{
	if (Task_bConnected)		return FALSE;
	if (Task_bConnecting)		return FALSE;
	if (Task_bDisconnecting)	return FALSE;
	return TRUE;
}

BOOLEAN	p_String2IP(PXCHAR pString, PXCHAR pDestIp)
{
	DCHAR iVal, iCount;
	DSHORT iTotal;
	XCHAR pIp[IP_ALEN];

	iTotal = 0;
	iCount = 0;
	do
	{
		iVal = (UCHAR)(*pString);
		pString ++;
		if (iVal >= '0' && iVal <= '9')
		{
			iTotal *= 10;
			iTotal += iVal - '0';
			if (iTotal >= 256)
			{
				return FALSE;
			}
		}
		else
		{
			pIp[iCount] = (UCHAR)iTotal;
			iTotal = 0;
			iCount ++;
			if (iCount < IP_ALEN)
			{
				if (iVal != '.' && iVal != '*')
				{
					return FALSE;
				}
			}
			else
			{
				break;
			}
/*			else if (iCount > IP_ALEN)
			{
				return FALSE;
			}
*/
		}
	} while (iVal);

/*	if (iCount != IP_ALEN)
	{
		return FALSE;
	}
*/
	p_memcpy_ip(pDestIp, pIp);
	return TRUE;
}

void p_IP2String(PXCHAR pIP, PXCHAR pString)
{
//	XCHAR pTemp[4];
	DCHAR i;

	pString[0] = 0;
	for (i = 0; i < IP_ALEN; i ++)
	{
		p_itoa(pIP[i], (PXCHAR)(pString + p_strlen_x(pString)), 10);
//		p_strcat_x2x(pString, pTemp);
		if (i < IP_ALEN-1)
		{
			p_strcat_char2x(pString, '.');
		}
	}
}

void p_Mac2String(PXCHAR pMac, PXCHAR pString)
{
	DCHAR i, iVal, iPos;

	iPos = 0;
	for (i = 0; i < HW_ALEN; i ++)
	{
		iVal = pMac[i];
		if (iVal < 0x10)
		{
			pString[iPos] = '0';
			iPos ++;
			p_itoa(iVal, (PXCHAR)(pString+iPos), 16);
			iPos ++;
		}
		else
		{
			p_itoa(iVal, (PXCHAR)(pString+iPos), 16);
			iPos += 2;
		}
		pString[iPos] = '-';
		iPos ++;
	}
	iPos --;
	pString[iPos] = 0;
}

BOOLEAN _IsHexChar(UCHAR iChar)
{
	if ((iChar >= '0' && iChar <= '9') 
		|| (iChar >= 'a' && iChar <= 'f')
		|| (iChar >= 'A' && iChar <= 'F'))
	{
		return TRUE;
	}
	else
		return FALSE;

}

BOOLEAN	p_String2Mac(PXCHAR pString, PXCHAR pDestMac)
{
	DCHAR i, iLen;
	XCHAR pMac[3];
	PXCHAR_D pCur;

	iLen = p_strlen_x(pString);
	if (iLen != 17) // xx-xx-xx-xx-xx-xx
	{
		return FALSE;
	}

	for (i = 0; i < iLen; i ++)
	{
		if (!_IsHexChar(pString[i]) && pString[i] != '-')
		{
			return FALSE;
		}
	}

	pCur = pString;
	pMac[2] = 0;
	for (i = 0; i < HW_ALEN; i ++)
	{
		pMac[0] = *pCur ++;
		pMac[1] = *pCur ++;
		if (i < HW_ALEN -1 )
		{
			if ((*pCur ++) != '-')	return FALSE;
		}
		pDestMac[i] = (UCHAR)p_atoi(pMac, 16);
	}
	return TRUE;
}

UCHAR p_IsNewLine(PXCHAR pData)
{
	if (pData[0] == 0x0d)
	{
		if (pData[1] == 0x0a)	return 2;
		else					return 1;
	}
	else if (pData[0] == 0x0a)
	{
		if (pData[1] == 0x0d)	return 2;
		else					return 1;
	}
	return 0;
}

PXCHAR p_strcat_char2x(PXCHAR pDst, UCHAR iSrc)
{
	XCHAR pSrc[2];

	pSrc[0] = iSrc;
	pSrc[1] = 0;
	return p_strcat_x2x(pDst, pSrc);
}

PXCHAR p_memcpy_mac(PXCHAR dst, PXCHAR src)
{
	return p_memcpy_x2x(dst, src, HW_ALEN);
}

PXCHAR p_memcpy_ip(PXCHAR dst, PXCHAR src)
{
	return p_memcpy_x2x(dst, src, IP_ALEN);
}

UCHAR p_memcmp_ip(PXCHAR dst, PXCHAR src)
{
	return p_memcmp_x2x(dst, src, IP_ALEN);
}


PXCHAR p_memset_ip_0(PXCHAR dst)
{
	return p_memset_x(dst, 0, IP_ALEN);
}

