#include "p_apps.h"
#include "p_mgcp.h"
#include "my_mgcp.h"

#define DIGITHEX_NUM	16
CCHAR _cDigitHex[DIGITHEX_NUM] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 
};

PXCHAR _StrchrBrkt_x(PXCHAR pData, UCHAR iChar)
{
	PXCHAR_D pCur;
	BOOLEAN bNoBracket;

	bNoBracket = TRUE;
	pCur = pData;
	while (*pCur)
	{
		if (*pCur == iChar && bNoBracket)
		{
			return pCur;
		}
		else if (*pCur == '(')
			bNoBracket = FALSE;
		else if (*pCur == ')')
			bNoBracket = TRUE;
		pCur ++;
	}
	return 0;
}

void _NewToken(PXCHAR pToken, UCHAR iLen)
{
	DCHAR i, index;
	
	for (i = 0; i < iLen; i ++)
	{
		index = p_rand() % DIGITHEX_NUM;
		pToken[i] = _cDigitHex[index];
	}
	pToken[iLen] = 0;
}

void MgcpCopyFromSdram(UCHAR iSdramLo)
{
	if (SdramReadString((USHORT)(PROTOCOL_SDRAM_POS+iSdramLo)))
	{
		p_line_add_x((PXCHAR)Sdram_pDataCache);
		p_line_add_newline();
	}
}

 void MgcpNotifyKey(UCHAR iKey)
{
	DCHAR iKeyID;

	iKeyID = iKey;
	if (iKeyID == '*')
	{
		MgcpNotify(MGCP_EVENT_DTMF_STAR);
	}
	else if (iKeyID == '#')
	{
		MgcpNotify(MGCP_EVENT_DTMF_POUND);
	}
	else
	{
		MgcpNotify((UCHAR)(MGCP_EVENT_DTMF_0 + iKeyID - '0'));
	}
}

BOOLEAN p_strstr_nocase_c(PXCHAR pDst, PCCHAR pSrc)
{
	DCHAR i, iDstLen, iSrcLen;

	iDstLen = p_strlen_x(pDst);
	iSrcLen = p_strlen_c(pSrc);
	if (iDstLen < iSrcLen)	return FALSE;

	iDstLen -= iSrcLen;
	for (i = 0; i <= iDstLen; i ++)
	{
		if (p_memcmp_nocase_c2x((PXCHAR)(pDst+i), pSrc, iSrcLen))	return TRUE;
	}
	return FALSE;
}
