/**************************************************************************
<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 <bios.h>
#include <DOS.H>

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

#ifndef _CARTE_MAX_BYTE
	#define _CARTE_MAX_BYTE         75
#endif

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</CLASS>
	<NAME>CCardReaderValidatorJites</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderValidatorJites::CCardReaderValidatorJites(void)
{
	m_psPort = NULL;
	memset(&m_sParameters, 0, sizeof(m_sParameters));
}

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

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

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::LecteurInit(s_Parameters* shParm)
{
	m_sParameters = *shParm;
	return LecteurInit();
}

int CCardReaderValidatorJites::LecteurInit(void)
{
	int iReturn = ERROR_PORT_COM_NO_CREATED;
		
		/* initialize Magnétic card reader*/
	if (m_psPort)
	{
		if ( com_define( m_sParameters.iPort, 0, 0, m_ucRxData, sizeof(m_ucRxData), m_ucTxData, sizeof(m_ucTxData), m_psPort) 
			&& com_open( m_sParameters.iBaudrate, m_sParameters.iSetting, m_sParameters.iDtr, m_sParameters.iRts, m_psPort)
			&& com_handshake(m_sParameters.uiHandshake, m_psPort))
		{

			iReturn = 0; //port openened, card reader ready for testing
		}
	}
	return iReturn;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::SendData(char *pzch)
{
	int iReturn = ERROR_PORT_COM_NO_CREATED;
	if (m_psPort)
	{
		uchar *command;
		while(com_rx_available(m_psPort)) 
			com_rx(m_psPort);
		for (command = pzch; *command; command++)
			com_tx(*command, m_psPort);
		iReturn = _LECTEUR_GOOD;
	}
	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::ReadData(char *pzdata, int ilen)
{
	int iReturn = ERROR_PORT_COM_NO_CREATED;
	if (m_psPort && _CARTE_MAX_BYTE <= ilen)
	{
		int icpt = 0;
		//char szBuft[128];
		char *pzTp = pzdata;
		//char *pzTp = szBuft;
		memset(pzdata, 0, ilen);

		while (((*pzTp = com_rx(m_psPort)) != 0x0D) 
			&& (icpt++ < _CARTE_MAX_BYTE)) pzTp++;

		//printf("\r\nreaddata : %s\r\n", szBuft);
		//strcpy(pzdata, szBuft);
		iReturn = _LECTEUR_GOOD;
	}
	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::SendReadCommande(char* pzLectBuffer)
{
	return SendData(JITES_READ);
}

int CCardReaderValidatorJites::ReadCommande(char* pzLectBuffer)
{
	int iReturn = _LECTEUR_BUSY; 
	if(PortReceiveCompleted())
	{
		iReturn = ReadData(pzLectBuffer, MAX_BUFFER_VALIDATOR);
	}
	return iReturn;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::SendWriteCommande(const char* pzString)
{
	char szbuf[128];
	sprintf(szbuf, JITES_WRITE, pzString);
	return SendData(szbuf);
}

int CCardReaderValidatorJites::WriteCommande(char* pzString)
{
	int iReturn = _LECTEUR_BUSY; 
	if(PortReceiveCompleted())
	{
		iReturn = ReadData(pzString, MAX_BUFFER_VALIDATOR);
	}
	return iReturn;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::SendEjectCommande(unsigned char ucDirection)
{
	return SendData(JITES_EJECT_FRONT);
}

int CCardReaderValidatorJites::EjectCommande(char* pzString)
{
	int iReturn = _LECTEUR_BUSY; 
	if(PortReceiveCompleted())
	{
		iReturn = ReadData(pzString, MAX_BUFFER_VALIDATOR);
	}
	return iReturn;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::Tempo(char* pzString)
{
	int iReturn = _LECTEUR_BUSY; 
	CTimer chTime;
	
	if(SendData("t0\x0D") == ACK)
	{
		chTime.SetTimer();
		while((!PortReceiveCompleted()) &&  chTime.TimerCompare(_WAIT_TIME_S));
		if(PortReceiveCompleted())
		{
			iReturn = ReadData(pzString, MAX_BUFFER_VALIDATOR);
		}
	}
	return iReturn;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</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 CCardReaderValidatorJites::WaitCard(void)
{
	int c;
	c = com_rx(m_psPort);
	if(c == 'a') 
		return CARD_HOME;
	else
		return _LECTEUR_BUSY;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderValidatorJites</CLASS>
	<NAME>PortReceiveCompleted</NAME>
	<TYPE>LOCAL</TYPE>
	<DESCRIPTION>
	   Determine si la reponse du lecteur est complete
	   C'est-a-dire termin‚ par CR
	   Cette fonction utilise la structure d‚finie par le driver
	   et doit etre modifi‚e si celle-ci change
   </DESCRIPTION>
	<RETURNS>
		<TYPE>int</TYPE>
		<DESCRIPTION>
			
		</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
int CCardReaderValidatorJites::PortReceiveCompleted(void)
{
	int iReturn = ERROR_PORT_COM_NO_CREATED;
	if (m_psPort)
	{
		
		uchar *pucLastcar;
		char c;
		int nbcar;

		nbcar = com_rx_available(m_psPort); 
		_disable();
		if (m_psPort->r_put == m_ucRxData)
			pucLastcar = m_ucRxData + _RX_SIZE - 1;
		else 
			pucLastcar = m_psPort->r_put - 1;
		_enable();
		return((*pucLastcar == '\x0D') && (nbcar > 0)); 
		
		/*return 1;*/
	}
	return iReturn;

}
