#include "Page1.h"
#include "p_dsp.h"
#include "p_rtp.h"
#include "v_task.h"
#ifdef CALL_MGCP
#include "p_mgcp.h"
#endif

static XSHORT _iQuietDuration;
static XCHAR _iKeyPlayingPos = 0;
static XCHAR _iToneTotal, _iToneTotal2; 
static XSHORT _iQuietTotal, _iQuietTotal2; 
static BOOLEAN _bToneTotalFlag;

BOOLEAN _IsRinging()
{
	if (Ac97_iDtmfCode == DTMF_SIGNAL_RING)		return TRUE;
	return FALSE;
}

void DtmfRun()
{
	if (Ac97_iDtmfCode < DTMF_SIGNAL_DIAL)
	{
		if (!Ac97_iDtmfDuration)
		{
			if (_iKeyPlayingPos)
			{
				if (_iQuietDuration)
				{
					_iQuietDuration --;
					return;
				}
				else
				{
					if (DtmfPlayKeys())	
					{
						return;
					}
					else
					{
						TaskCall();
					}
				}
			}
			Ac97_iDtmfCode = DTMF_SIGNAL_QUIET;
		}
	}
	else if (Ac97_iDtmfCode == DTMF_SIGNAL_DIAL)
	{
		Ac97_iDtmfDuration = 1;
	}
	else if (Ac97_iDtmfCode > DTMF_SIGNAL_DIAL && Ac97_iDtmfCode < DTMF_SIGNAL_QUIET)
	{
		_iKeyPlayingPos = 0;
		if (Ac97_iDtmfDuration)
		{
			if (_IsRinging())
			{
				UIRingIndicate(TRUE);
			}
		}
		else
		{
			if (_IsRinging())
			{
				UIRingIndicate(FALSE);
			}

			if (_iQuietDuration)
			{
				_iQuietDuration --;
			}
			else
			{
				if (_bToneTotalFlag)
				{
					Ac97_iDtmfDuration = _iToneTotal;
					_iQuietDuration = _iQuietTotal;
					_bToneTotalFlag = FALSE;
				}
				else
				{
					Ac97_iDtmfDuration = _iToneTotal2;
					_iQuietDuration = _iQuietTotal2;
					_bToneTotalFlag = TRUE;
				}
			}
		}
	}

	if (!Ac97_iDtmfDuration || Ac97_iDtmfCode >= DTMF_SIGNAL_DIAL)
	{
		Task_bInbandDtmf = FALSE;
	}
}

void DtmfPlayKey(UCHAR iKeyID)
{
	DCHAR iDtmfCode;

	if (iKeyID == 'T')
		return;

	if (iKeyID == '.')
	{
		iKeyID = '*';
	}
	iDtmfCode = p_getKeyId(iKeyID);
	if (iDtmfCode == MAP_KEY_UNKNOWN)	
		return;

	Ac97Start();
	Ac97_iDtmfCode = iDtmfCode;
	DspClearDecodeData();
	Ac97_iDtmfDuration = DTMF_KEY_TIME/Rtp_cFrameTime[Dsp_iType];
}

BOOLEAN DtmfPlayKeys()
{
	DCHAR iVal;

	iVal = Key_pBuf[_iKeyPlayingPos];
	if (iVal)
	{
		DtmfPlayKey(iVal);
//		Ac97_iDtmfDuration >>= 1;	// play faster
		_iKeyPlayingPos ++;
		_iQuietDuration = Ac97_iDtmfDuration;
#ifdef CALL_MGCP
		MgcpNotifyKey(iVal);
#endif
		return TRUE;
	}
	_iKeyPlayingPos = 0;
	return FALSE;
}

void DtmfDialTone()
{
	Ac97Start();
	_iKeyPlayingPos = 0;
	Ac97_iDtmfCode = DTMF_SIGNAL_DIAL;
}

void DtmfStopAll()
{
	_iKeyPlayingPos = 0;
	Ac97_iDtmfCode = DTMF_SIGNAL_QUIET;
	Ac97_iDtmfDuration = 0;
}

void DtmfStopRing()
{
	if (_IsRinging())
	{
		DtmfStopAll();
	}
}

#define DTMF_TYPE_NUM	4		//	busy,waiting,congestion,ring
static CSHORT _cDtmf1_On[DTMF_TYPE_NUM]  = {DTMF_BUSY1_ON_TIME,  DTMF_WAITING1_ON_TIME,  DTMF_CONGESTION1_ON_TIME,  DTMF_RING1_ON_TIME};
static CSHORT _cDtmf1_Off[DTMF_TYPE_NUM] = {DTMF_BUSY1_OFF_TIME, DTMF_WAITING1_OFF_TIME, DTMF_CONGESTION1_OFF_TIME, DTMF_RING1_OFF_TIME};
static CSHORT _cDtmf2_On[DTMF_TYPE_NUM]  = {DTMF_BUSY2_ON_TIME,  DTMF_WAITING2_ON_TIME,  DTMF_CONGESTION2_ON_TIME,  DTMF_RING2_ON_TIME};
static CSHORT _cDtmf2_Off[DTMF_TYPE_NUM] = {DTMF_BUSY2_OFF_TIME, DTMF_WAITING2_OFF_TIME, DTMF_CONGESTION2_OFF_TIME, DTMF_RING2_OFF_TIME};

void _DtmfSignal(UCHAR iType)
{
	DCHAR iDtmfType, iIndex, iDuration;

	iDtmfType = iType;
	iIndex = iDtmfType - DTMF_SIGNAL_BUSY;

	Ac97Start();
	iDuration = Rtp_cFrameTime[Dsp_iType];
	_iToneTotal = _cDtmf1_On[iIndex]/iDuration;
	_iQuietTotal = _cDtmf1_Off[iIndex]/iDuration;
	_iToneTotal2 = _cDtmf2_On[iIndex]/iDuration;
	_iQuietTotal2 = _cDtmf2_Off[iIndex]/iDuration;
	Ac97_iDtmfDuration = _iToneTotal;
	_iQuietDuration = _iQuietTotal;
	_bToneTotalFlag = FALSE;
	Ac97_iDtmfCode = iDtmfType;
}

void DtmfRing()
{
	_DtmfSignal(DTMF_SIGNAL_RING);
}

void DtmfBusy()
{
	_DtmfSignal(DTMF_SIGNAL_BUSY);
	Task_iTimer = 0;
}

#ifdef CALL_SIP
void DtmfCongestion()
{
	_DtmfSignal(DTMF_SIGNAL_CONGESTION);
}
#endif

/*
void DtmfWaiting()
{
	_DtmfSignal(DTMF_SIGNAL_WAITING);
}
*/
