/**************************************************************************
<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 "..\comp\cardread\CdRdIJit.h"
#include "..\comp\cardread\lecteur.h"

#define _EJECT_FRONT   "f"
#define _EJECT_REAR    "e"
#define _READ          "r"
#define _WRITE         "w"
#define _CR            "\x0D"
#define _STATUS        "s"

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</CLASS>
	<NAME>CCardReaderJites</NAME>
	<TYPE>GLOBAL</TYPE>
	<DESCRIPTION></DESCRIPTION>
</METHOD>
**************************************************************************/
CCardReaderJites::CCardReaderJites(void)
{
	memset(m_arucRxBuffer, 0, sizeof(m_arucRxBuffer));
	memset(m_arucTxBuffer, 0, sizeof(m_arucTxBuffer));
	memset(m_arucBuffCommande, 0, sizeof(m_arucBuffCommande));
}

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


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


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::ReadCommande(char* pzLectBuffer)
{
	int iStatus = 0;

	strcpy(m_arucBuffCommande,_READ);
	strcat(m_arucBuffCommande,_CR);

	/* Send command and wait the response*/
	iStatus = Tcr(m_arucBuffCommande,1);
	if (iStatus == eTimeOut)
	{
		return(3);
	}

	
	strcpy(pzLectBuffer, m_szLectBuffer);

	if(pzLectBuffer[0] == 'x')
	{
		iStatus = TestRetour(pzLectBuffer[2]);
	}
	else
	{
		iStatus = 0;
	}
	pzLectBuffer[74] = 0x00;
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::WriteCommande(const char* pzString)
{
	int iStatus = 0;

	/* Construction of command*/
	strcpy(m_arucBuffCommande, _WRITE);
	strcat(m_arucBuffCommande, pzString);
	strcat(m_arucBuffCommande, _CR);

	/* Send command and wait the response*/
	iStatus = Tcr(m_arucBuffCommande,1);

	/* test if time out*/
	if (iStatus == eTimeOut)
		return(3);

	iStatus = TestRetour(m_szLectBuffer[2]);
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::EjectCommande(unsigned char ucDirection)
{
	int iStatus = 0;
	if(ucDirection == EJECT_REAR)
		strcpy(m_arucBuffCommande, _EJECT_REAR);
	else
		strcpy(m_arucBuffCommande, _EJECT_FRONT);
	strcat(m_arucBuffCommande, _CR);


	/* Send command and wait the response*/
	iStatus = Tcr(m_arucBuffCommande,1);
	if (iStatus == eTimeOut)
		return(4);

	if(m_szLectBuffer[2] == '0')
		iStatus = CARD_NO;
	else 
		iStatus = TestRetour(m_szLectBuffer[2]);

	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::StatusCommande(void)
{
	int iStatus = 0;
	

	strcpy(m_arucBuffCommande, _STATUS);
	strcat(m_arucBuffCommande, _CR);

	/* Send command and wait the response*/
	iStatus = Tcr(m_arucBuffCommande, 1);
	if (iStatus == eTimeOut)
		return(4);

	iStatus = strcmp(m_szLectBuffer,"x80");
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::Tempo(void)
{
	int iStatus = 0;

	strcpy(m_arucBuffCommande,"t2\x0D");


	/* Send command and wait the response*/
	iStatus = Tcr(m_arucBuffCommande,1);
	return(iStatus);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::WaitCard(void)
{
	if(McrRx() != 'a')
		return(CARD_NO);
	else
		return(CARD_HOME);
}


/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::Tcr(char *Buff_Commande,int etat)
{
	int iStatus = 0;
	int c, i;
	ulong temptimes;
	unsigned long *timer = ( unsigned long * )0x46C;
	int tmp = 3;
	char ok;

	memset(m_szLectBuffer, 0, sizeof(m_szLectBuffer));

	McrBg();
	while ((c = McrRx()) != -1);
	if(etat)
	{
		while (*Buff_Commande)
		{
			if (McrTx(*Buff_Commande++) == -1)
				return(eTimeOut);
		}
	}
	iStatus = McrCheckState();
	if (iStatus != 0)
		return(eTimeOut); 

	i = 0;
	McrBg();
	temptimes = *timer;         /* Valeur du temps */
	ok = '1';
	while ((*timer - temptimes <= (unsigned long) tmp * 19) && ok != 0x00 )
	{
		McrBg();
		while((( c = McrRx()) != -1) && (i < 75))
		{
			//ok = c = (c == 0x0D) ? 0x00: c;
			if(c == 0x0D)
				c = 0x00;
			ok = c;
			m_szLectBuffer[i++] = c;
		}
	}
	m_szLectBuffer[i] = 0x00;
	return(0);
}

/**************************************************************************
<METHOD>
	<CLASS>CCardReaderJites</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 CCardReaderJites::TestRetour(char etat)
{
	int iStatus = 0;
	/*DRO*/
	/*
	if( etat >= '0' && etat <= '9')
	{
		if(etat == '7')
			iStatus = 6;
		else
			iStatus = (int) etat - '0';
	}
	else if(etat == 'a')
	{
		iStatus = 10;
	}
	else
	{
		iStatus = 4;
	}
	return(iStatus);*/

	switch(etat)
	{
	case '0' : iStatus = ACK;  //     ACK                   
		break;
	case '1' : iStatus = UNKNOWN_COMMAND;  //     Unknown Commande      
		break;
	case '2' : iStatus = READING_ERROR;  //     Reading Error
		break;
	case '3' : iStatus = WRITING_ERROR;  //     Writing Error         
		break;
	case '5' : iStatus = STR_TOO_LONG;  //     Str too long          
		break;
	case '6' :
	case '7' : iStatus = CARD_REJECTED;  //     Card Rejected         
		break;
	case '8' : iStatus = NO_CARD;  //     No Card      ????     
		break;
	case '9' : iStatus = TRANSMISSION_ERROR;  //     Transmission Error    
		break;
	case 'a' : iStatus = A_NORMAL_CARD_DETECTED; //     Abnormal Card Detected
	case 'c' : iStatus = A_NORMAL_CARD_DETECTED; //     Abnormal Card Detected
		break;
	default  :
		iStatus = TIMEOUT_ERROR;
		break;
	}

	return(iStatus);
}


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


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