/**************************************************************************
<MODULE NAME>
<FILENAME></FILENAME>
<HISTORY>
	<MODIFICATION>
		<DATE></DATE>
		<AUTHOR></AUTHOR>
		<DESCRIPTION>
		</DESCRIPTION>
	</MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/


/**************************************************************************
INCLUDE FILE
**************************************************************************/
#include "..\comp\CardRead\StMchDig\StEnqDig.h"
#include "..\comp\CardRead\StMchDig\StRepDig.h"
#include "..\comp\CardRead\StMchDig\StTmODig.h"
#include "..\comp\CardRead\StMchDig\StAnlDig.h"
#include "..\comp\CardRead\StMchDig\StCmdDig.h"
#include "..\comp\cardread\CdRdIDig.h"
/**************************************************************************
LOCAL VARIABLES
Always start with « static »-keyword
**************************************************************************/
CStateMachineDigCard* CStEnqDig::m_pcInstance = NULL;
/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME>CStEnqDig</NAME>
	<TYPE > LOCAL  GLOBAL </TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
</METHOD>
**************************************************************************/
CStEnqDig::CStEnqDig()
{
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::CStEnqDig");
#endif
	;
}


/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME>~CStEnqDig</NAME>
	<TYPE> LOCAL | GLOBAL </TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO>IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
</METHOD>
**************************************************************************/
CStEnqDig::~CStEnqDig()
{
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::~CStEnqDig");
#endif
	;
}

/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
CStateMachineDigCard* CStEnqDig::Instance(void)
{
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::Instance");
#endif
	if(m_pcInstance == NULL)
		m_pcInstance = new CStEnqDig();
	return m_pcInstance;
}

/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CStEnqDig::CancelCommand(CCardReaderDig* pcParent)
{
	int iResult = ERROR_NONE_AVAILABLE;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::CancelCommand");
#endif
	iResult = ChangeState(pcParent, CStCancelDig::Instance());
	if(iResult >= 0)
		iResult = pcParent->GetState()->WaitAck(pcParent);
	return iResult;
}
/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CStEnqDig::ReceiveTimeOut(CCardReaderDig* pcParent)
{
	int iResult = ERROR_NONE_AVAILABLE;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::ReceiveTimeOut");
#endif

	if(pcParent->TimeOut() != ACK)
	{
		iResult = ChangeState(pcParent, CStTimOutDig::Instance());
		if(iResult >= 0)
			iResult = pcParent->GetState()->ReceiveTimeOut(pcParent);
	}
	else
	{
		iResult = pcParent->ReSendCommand();
		if(iResult >= 0)
			iResult = pcParent->GetState()->WaitAck(pcParent);
		else
			iResult = ChangeState(pcParent, CStCommandDig::Instance());
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CStEnqDig::ReceiveAck(CCardReaderDig* pcParent)
{
	int iResult = ERROR_NONE_AVAILABLE;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::ReceiveAck");
#endif

	iResult = pcParent->SendEnq();
	
	if(iResult >= 0)
	{
		iResult = ChangeState(pcParent, CStResponceDig::Instance());
		if(iResult >= 0)
			iResult = pcParent->GetState()->WaitResponce(pcParent);
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CStEnqDig::ReceiveNack(CCardReaderDig* pcParent)
{
	int iResult;
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::ReceiveNack");
#endif
	if(pcParent->SetRetries() == 0)
	{
		if(pcParent->GetRetries() >= SEND_DLE_EOT_AFTER)
		{
			iResult = CancelCommand(pcParent);
		}
		else
		{
			iResult = pcParent->ReSendCommand();
			if(iResult >= 0)
			{
				iResult = pcParent->GetState()->WaitAck(pcParent);
			}
			else
			{
				iResult = ChangeState(pcParent, CStCommandDig::Instance());
			}
		}
	}
	else
	{
		iResult = ChangeState(pcParent, CStCommandDig::Instance());
		iResult = ERROR_RETIES;
	}
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/

int CStEnqDig::ReceiveDleEot(CCardReaderDig* pcParent)
{
	int iResult;
	iResult = CancelCommand(pcParent);
	return iResult;
}

/**************************************************************************
<METHOD>
	<CLASS>CStEnqDig</CLASS>
	<NAME></NAME>
	<TYPE >LOCAL | GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</IO>
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CStEnqDig::WaitAck(CCardReaderDig* pcParent)
{
#ifdef STESUD_TRACE
	CTrace::Instance()->Show("CStEnqDig::WaitAck");
#endif
	return  pcParent->WaitAck();
}
