/**************************************************************************
<MODULE NAME>
	<FILENAME></FILENAME>
	<HISTORY>
		<MODIFICATION>
			<DATE>05/06/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 "..\comp\UrnCrd\CdrdUJit.h"
#include "..\comp\timer\Timer.h"

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderUrneJites</CLASS>
	<NAME>CCardReaderUrneJites</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderUrneJites::CCardReaderUrneJites(void)
{
	memset(m_arucRxBuffer, 0, sizeof(m_arucRxBuffer));
	memset(m_arucTxBuffer, 0, sizeof(m_arucTxBuffer));
	
	m_sParameters.iIoAddr = 0;
	m_sParameters.iIrq = 0;
	m_sParameters.iPort = 0;
	m_sParameters.iSetting = 0;
	m_sParameters.iBaudrate = 0;
}

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


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderUrneJites</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 CCardReaderUrneJites::LecteurInit(s_Parameters* shParm)
{
	m_sParameters = *shParm;
	return LecteurInit();
}
int CCardReaderUrneJites::LecteurInit(void)
{
	return(McrInit(m_sParameters.iIoAddr, m_sParameters.iIrq, m_arucRxBuffer, sizeof(m_arucRxBuffer), 
		m_arucTxBuffer,sizeof(m_arucTxBuffer)));
}

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

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

int CCardReaderUrneJites::ReadCommande(char* pzLectBuffer)
{
	if(TcrRx(pzLectBuffer))
		return(_LECTEUR_BUSY);
	return _LECTEUR_GOOD;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderUrneJites</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 CCardReaderUrneJites::SendEjectCommande(unsigned char ucDirection)
{
	if (ucDirection == EJECT_REAR)
		TcrSnd(JITES_EJECT_REAR);
	else
		TcrSnd(JITES_EJECT_FRONT_FAST);

	return _LECTEUR_GOOD;
}

int CCardReaderUrneJites::EjectCommande(char* pzString)
{
	if (TcrRx(pzString))
		return _LECTEUR_BUSY;
	return _LECTEUR_GOOD;
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderUrneJites</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 CCardReaderUrneJites::SendStatusCommande(void)
{
	TcrSnd(JITES_POSITION);
	return _LECTEUR_GOOD;
}

int CCardReaderUrneJites::StatusCommande(char* pzString)
{
	if(FinRx(pzString))
		return _LECTEUR_BUSY;
	return _LECTEUR_GOOD;
}

int CCardReaderUrneJites::SendDeblock(void)
{
	TcrSnd(JITES_BLOQ);
	return _LECTEUR_GOOD;
}

int CCardReaderUrneJites::Deblock(char* pzString)
{
	if (TcrRx(pzString))
		return _LECTEUR_BUSY;
	else
		return _LECTEUR_GOOD;
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderUrneJites</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 CCardReaderUrneJites::Tempo(char* pzString)
{
	int iStatus = 0;
	char szBuffCommande[64];	
	CTimer chTime;

	strcpy(szBuffCommande,"t2\x0D");
	McrBg();
	TcrSnd(szBuffCommande);

	chTime.SetTimer();
	while(chTime.TimerCompare(5))
	{
		;
	}

	chTime.SetTimer();
	do
	{
		McrBg();
	} 
	while((TcrRx(pzString)) && (chTime.TimerCompare(_WAIT_TIME_S)));
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderUrneJites</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 CCardReaderUrneJites::WaitCard(void)
{
	int iReturn = McrRx();
	if(iReturn != 'a')
		iReturn = _LECTEUR_BUSY;

	return iReturn;
}

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

int CCardReaderUrneJites::TcrSnd(char *Buff_Commande)
{
	while(McrRx() != -1);
	while (*Buff_Commande)
		McrTx(*Buff_Commande++);
	return(true);
}
/***************************************************************************/

int CCardReaderUrneJites::TcrRx(char *Buff_Commande)
{
	char *p;
	int c;
	unsigned long far *timer = ( unsigned long * )0x46C;
	unsigned long temptimes;

	int k = 0;   
	char ok; 

	ok = '1';
	p = Buff_Commande;

	if(Mcr_End() == -1) return(1);  /* Il y'a pas d'elements dans la queue */

	temptimes = *timer;         /* Valeur du temps */

	while ((*timer - temptimes <= (unsigned long)45) && (ok != 0x0D) && (k < 75) )
	{
		while (((c = McrRx()) != -1) && (k < 75) && (ok != 0x0D))
		{
			ok = c ;
			//*p++ = (c == 0x0D) ? 0x00 : c ;
			if(c == 0x0D)
				*p++ = 0x00;
			else
				*p++ = c;

			k++;
		}
	}
	if(ok != 0x0D)
	{
		*p = 0x00;
		while ((c = McrRx()) != -1);
	}
	if(k <= 2) 
		return(1);
	else 
		return(0);
}
/***************************************************************************/

int CCardReaderUrneJites::FinRx(char *Buff_Commande)
{
	char *p;
	int c;
	unsigned long far *timer = ( unsigned long * )0x46C;
	unsigned long temptimes;

	int k = 0;   
	char ok; 

	ok = '1';
	p = Buff_Commande;

	if(Mcr_End() == -1) 
		return(1);  /* Il y'a pas d'elements dans la queue */

	temptimes = *timer;         /* Valeur du temps */

	while ((*timer - temptimes <= (unsigned long)45) && (ok != 0x0D) && (k < 75) )
	{
		while (((c = McrRx()) != -1) && (k < 75) && (ok != 0x0D))
		{
			ok = c ;
			// *p++ = (c == 0x0D) ? 0x00 : c ;
			if(c == 0x0D)
				*p++ = 0x00 ;
			else
				*p++ = c ;
			k++;
		}
	}
	if(ok != 0x0D)
	{
		*p = 0x00;
		while ((c = McrRx()) != -1);
	}
	if(k <= 1) 
		return(1);
	else 
		return(0);
}

int CCardReaderUrneJites::CloseConnection(void)
{
	McrExit();
	return _LECTEUR_GOOD;
}