/**************************************************************************
<MODULE NAME>
	<FILENAME></FILENAME>
	<HISTORY>
		<MODIFICATION>
			<DATE>30/01/2006</DATE>
			<AUTHOR>DRO</AUTHOR>
			<DESCRIPTION>
				Create one interface for
			</DESCRIPTION>
		</MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/

/**************************************************************************
INCLUDE FILE
**************************************************************************/
#include <conio.h>
#include <stdio.h>
#include <STDLIB.H>
#include <string.h>

#include "..\comp\UrnCrd\CdRdudig.h"
#include "..\comp\timer\Timer.h"

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</CLASS>
	<NAME>CCardReaderValidatorDig</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderValidatorDig::CCardReaderValidatorDig(void)
{
	ResetTransation();
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</CLASS>
	<NAME></NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderValidatorDig::~CCardReaderValidatorDig(void)
{
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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 CCardReaderValidatorDig::LecteurInit(s_Parameters* shParm)
{
	return m_cReaderValidator.LecteurInit(shParm);
}

int CCardReaderValidatorDig::LecteurInit(void)
{
	return m_cReaderValidator.LecteurInit();
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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>
**************************************************************************/
#pragma argsused
int CCardReaderValidatorDig::SendReadCommande(char* pzLectBuffer)
{
	int iStatus = 0;
	if(!GetTransation())
	{
		SetTransation();
		memset(m_ucRxData, 0, sizeof(m_ucRxData));

		m_iStatus = m_cReaderValidator.ReadCommande(m_ucRxData);
		iStatus = m_iStatus;
	}
	return iStatus;
}

int CCardReaderValidatorDig::ReadCommande(char* pzLectBuffer)
{
	int iStatus = 0;
	if(GetTransation())
	{
		if(m_iStatus != 0)
		{
			/*iStatus =*/ TestRetour(m_iStatus, pzLectBuffer);
		}
		else
		{
			strcpy(pzLectBuffer , m_ucRxData);
		}
		ResetTransation();
	}
	return iStatus;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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 CCardReaderValidatorDig::SendWriteCommande(const char* pzString)
{
	int iStatus = 0;
	if(!GetTransation())
	{
		SetTransation();

		m_iStatus = m_cReaderValidator.WriteCommande(pzString);

		iStatus = m_iStatus;
	}
	return iStatus;
}

int CCardReaderValidatorDig::WriteCommande(char* pzString)
{
	int iStatus = 0;
	if(GetTransation())
	{
		/*iStatus =*/ TestRetour(m_iStatus, pzString);
		ResetTransation();
	}
	return iStatus;
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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 CCardReaderValidatorDig::SendEjectCommande(unsigned char ucDirection)
{
	int iStatus = 0;
	if(!GetTransation())
	{
		SetTransation();

		m_iStatus = m_cReaderValidator.EjectCommande(ucDirection);/*PMD*/
		iStatus = m_iStatus;
	}
	return iStatus;
}

int CCardReaderValidatorDig::EjectCommande(char* pzString)
{
	int iStatus = 0;
	if(GetTransation())
	{
		/*iStatus = */TestRetour(m_iStatus, pzString);/*PMD*/
		ResetTransation();
	}
	return iStatus;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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 CCardReaderValidatorDig::SendStatusCommande(void)
{
	int iStatus = 0;
	if(!GetTransation())
	{
		SetTransation();

		m_iStatus = m_cReaderValidator.StatusCommande();

		iStatus = m_iStatus;
		
	}
	
	return iStatus;
}

int CCardReaderValidatorDig::StatusCommande(char* pzString)
{
	int iStatus = 0;
	if(GetTransation())
	{
		if(m_iStatus == CARD_NO)
		{
			iStatus = 0;
			strcpy(pzString , "x82\0");
		}
		else if(m_iStatus == CARD_GATE || m_iStatus == CARD_HOME)
		{
			iStatus = 0;
			strcpy(pzString , "x84\0");
		}
		else
		{
			iStatus = TestRetour(m_iStatus, pzString);
		}
		ResetTransation();
	}
	return iStatus;
}
int CCardReaderValidatorDig::SendDeblock(void)
{
	return SendEjectCommande(0);
}

int CCardReaderValidatorDig::Deblock(char* pzString)
{
	return EjectCommande(pzString);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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>
**************************************************************************/
#pragma argsused
int CCardReaderValidatorDig::Tempo(char* pzString)
{
	int iStatus = 0;
	strcpy(pzString, "x00");
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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 CCardReaderValidatorDig::WaitCard(void)
{
	int iStatus = 0;
	if(!GetTransation())
	{
		iStatus = m_cReaderValidator.WaitCard();
		/*
		 if(m_cReaderValidator.WaitCard() == CARD_HOME)
			iStatus = 'a';
		 else
			iStatus = _LECTEUR_BUSY ;
		*/
	}
	
	return(iStatus);
}
/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</CLASS>
	<NAME>TestRetour</NAME>
	<TYPE>LOCAL</TYPE>
	<DESCRIPTION>Transform a char into interger</DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME>etat</NAME>
			<TYPE>char</TYPE>
			<IO>IN</IO>
			<DESCRIPTION>
				Character transformed into integer
			</DESCRIPTION>
		</PARAMETER>
	</PARAMETERS>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
			0	ACK
			1	Unknown Commande
			2	Reading Error
			3	Writing Error
			5	Str too long
			6	Card Rejected
			7	Card Rejected
			8	No Card
			9	Transmission Error
			a	Abnormal Card Detected
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderValidatorDig::TestRetour(int etat, char* pzRetourString)
{
	int iStatus = 0;
	/*DRO*/

	switch(etat)
	{
	case CARD_NO:
		{
			iStatus = 0;  //NO card present
			strcpy(pzRetourString,"x00\0");
			break;
		}
	case CARD_HOME:
		{
		iStatus = 0;  //card ready to read
		strcpy(pzRetourString,"x00\0");
		break;
		}
	case CARD_GATE:
		{
			iStatus = 0;  //Card in gate
			strcpy(pzRetourString,"x00\0");
			break;
		}
	case ERROR_READER_CARD: /*PMD*/
		strcpy(pzRetourString,"x0e\0");
		break;
	case ACK:
		{
			iStatus = ACK;  //ACK
			strcpy(pzRetourString,"x00\0");
			break;
		}
	case UNKNOWN_COMMAND:
		{
			iStatus = 1;  //     Unknown Commande
			strcpy(pzRetourString,"x01\0");
			break;
		}
	case READING_ERROR:
		{
			iStatus = READING_ERROR;  //     Reading Error
			strcpy(pzRetourString,"x02\0");
			break;
		}
	case WRITING_ERROR:
		{
			iStatus = WRITING_ERROR;  //     Writing Error
			strcpy(pzRetourString,"x03\0");
			break;
		}
	case A_NORMAL_CARD_DETECTED:
		{
			iStatus = A_NORMAL_CARD_DETECTED; //     Abnormal Card Detected
			strcpy(pzRetourString,"x0c0\0");
			break;
		}
	case ERROR_OPEN_SERIAL : //error rs232		
	case ERROR_CLOSE_SERIAL : //error rs232		
	case ERROR_TIMEOUT_SERIAL : //error rs232	
	case ERROR_SEND_DATA_SERIAL : //error rs232	
		{
			iStatus = TRANSMISSION_ERROR; //     Abnormal Card Detected
			strcpy(pzRetourString,"x0d0\0");
			break;
		}
	default:
		{
			if(GetType() == '1') /* Validator */
			{
				iStatus = UNKNOWN_COMMAND; // Unknown Commande
				strcpy(pzRetourString,"x01\0");
			}
			else /* Ballot Box : there are some cards reader which
			      allows to introduce card in a bad way or card which are not init.*/
			{
				return(TestRetour(READING_ERROR, pzRetourString));
			}

			break;
		}
	}
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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>
**************************************************************************/
CCardReaderUrneManager* CCardReaderValidatorDig::Instance(void)
{
	CCardReaderUrneManager* m_pcInst;
	//if(m_pcInst == NULL)
	{
		m_pcInst = new CCardReaderValidatorDig();
	}
	return m_pcInst;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorDig</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>
**************************************************************************/
void CCardReaderValidatorDig::CheckQueue(void)
{
	;
}