/**************************************************************************
<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 <string.h>
*/
#include "..\comp\timer\Timer.h"
#include "..\comp\TypePc\TypePc.h"

#ifdef __cplusplus
/*C++ define*/

#include "..\comp\cardread\CdRdIDig.h"
#include "..\comp\cardread\CdRdIJit.h"
#endif
#include "LibCrdRd.h"
/* #include "..\comp\cardread\CdRdMger.h"*/

/**************************************************************************
MACRO
**************************************************************************/

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int InitLibCardReader(int iTypePc)
{
	int iResult = 0;
	switch(iTypePc)
	{
		case CTypePc::eJiteMavVer1:
		case CTypePc::eJiteMavVer2:
			CCardReaderJites::Instance();
			break;
		case CTypePc::eDigivotMav1:
		case CTypePc::eDigivotMav2:
			CCardReaderDig::Instance();
			break;
		default :
			iResult = -1;
			break;
	}
	return iResult;
}

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int Lecteur_Init(s_Parameters* shParm)
{
	if( CCardReaderManager::Inst() == NULL)
		return ERROR_INSTANCE;
	else
		return CCardReaderManager::Inst()->LecteurInit(shParm);
}

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
void FreeResourceCardMagn()
{
	CCardReaderManager::DestroyInst();
}

/* << 8.67c */
/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int Status_Commande(void)
{
		return CCardReaderManager::Inst()->StatusCommande();
}
/* 8.67c >> */

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int Read_Commande(char* pzLectBuffer)
{
	if( CCardReaderManager::Inst() == NULL)
		return ERROR_INSTANCE;
	else
		return CCardReaderManager::Inst()->ReadCommande(pzLectBuffer);
}

/***************************************************************************/

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int Write_Commande(const char* pzString)
{
	if( CCardReaderManager::Inst() == NULL)
		return ERROR_INSTANCE;
	else
		return  CCardReaderManager::Inst()->WriteCommande(pzString);
}

/**************************************************************************
<FUNCTION>
	<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>
			ERROR_EJECTION (21)
			ERROR_RETIES   (-130)
		</DESCRIPTION>
	</RETURNS>
</FUNCTION>
**************************************************************************/
int Eject_Commande(unsigned char ucDirection)
{
	int iReturn = ERROR_INSTANCE;
	if( CCardReaderManager::Inst() != NULL)
	{
		CTimer chTimer;
		uchar ucCptEject = 0;
		do
		{
			iReturn = CCardReaderManager::Inst()->EjectCommande(ucDirection);
			/*Captute timer*/
			chTimer.SetTimer();

			/*Wait until card removed */
			while( iReturn == CARD_HOME && (chTimer.TimerCompare(SLEEP_WAIT_TILL)))
			{
				iReturn = CCardReaderManager::Inst()->WaitTillCardRemoved();
			}
			ucCptEject++;
		}
		while(ucCptEject < MAX_RETIES && iReturn == CARD_HOME);

		if(ucCptEject >= MAX_RETIES)
		{ /*Error*/
			if(iReturn == CARD_HOME)
			{
				iReturn = ERROR_EJECTION;
			}
			else if(iReturn == CARD_GATE)
			{
				iReturn = ERROR_RETIES;
			}
		}
	}
	return iReturn;
}

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int Tempo(void)
{
	if( CCardReaderManager::Inst() == NULL)
		return ERROR_INSTANCE;
	else
		return  CCardReaderManager::Inst()->Tempo();
}

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
int Wait_Card(void)
{
	if( CCardReaderManager::Inst() == NULL)
		return ERROR_INSTANCE;
	else
		return  CCardReaderManager::Inst()->WaitCard();
}

/**************************************************************************
<FUNCTION>
	<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>
</FUNCTION>
**************************************************************************/
void CheckQueue(void)
{
	if( CCardReaderManager::Inst() != NULL)
		CCardReaderManager::Inst()->CheckQueue();
}

char* CommandDirect(char cCommande, char cParam, char* pzData)
{
	if( CCardReaderManager::Inst() != NULL)
		return CCardReaderManager::Inst()->CommandDirect(cCommande, cParam, pzData);

	 return NULL;
}