/**************************************************************************
<MODULE NAME>
<FILENAME></FILENAME>
<HISTORY>
	<MODIFICATION>
		<DATE>04/01/2006</DATE>
		<AUTHOR>DRO</AUTHOR>
		<DESCRIPTION>
		</DESCRIPTION>
	</MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/
#ifndef __CdRdIDig_H__
#define __CdRdIDig_H__

/**************************************************************************
INCLUDE FILE
**************************************************************************/
#include "..\comp\cardread\CdRdMger.h"
#include "..\comp\ComPort\Serial.h"
//#include "..\comp\CardRead\StMchDig\StateDig.h" 


class CStateMachineDigCard;
/**************************************************************************
<CLASS>
	<NAME>CCardReaderDig</NAME>
	<MOTHERS>
		<MOTHER></MOTHER>
	</MOTHERS>
	<DESCRIPTION>
	</DESCRIPTION>
</CLASS>
**************************************************************************/
class CCardReaderDig  : public CCardReaderManager
{

public:

	friend CStateMachineDigCard;

	struct s_Response
	{
	public:
		char cSTX;
		char cType;
		char cCommandCode;
		char cParameterCode;
		char cStatusCode[2];
		char szData[122];
	};

	union u_ReaderDataStruct
	{
	public:
		s_Response sResponse;
		char szResult[128];
	};

	CCardReaderDig(void);
	virtual ~CCardReaderDig(void);


	/* Method Public managment card*/
	virtual int GetProtocol(char* pzLectBuffer);
	virtual int GetVersionCommande(char* pzLectBuffer);
	virtual int LecteurInit(s_Parameters* shParm);
	virtual int LecteurInit(void);
	virtual int ReadCommande(char* pzLectBuffer);
	virtual int WriteCommande(const char* pzString);
	virtual int EjectCommande(unsigned char ucDirection);
	virtual int WaitCard(void);
	virtual int StatusCommande(void);
	virtual int Tempo(void);
	virtual void CheckQueue(void);
	int TestRetour(char* pzEtat);
	virtual int CloseConnection(void);
	virtual char* CommandDirect(char cCommande, char cParam, char* pzData);
	virtual int CaptureCard(void);
	virtual int WaitTillCardRemoved(void);
	virtual int CardPosition(void); /*PMD*/

	static CCardReaderManager* Instance(void);

/*State Machine*/
	int ChangeState(CStateMachineDigCard* pcState);
	CStateMachineDigCard* GetState(void) {return m_pcStateMachine;}
	int CancelCommand(void);
	int SendEnq(void);
	int SendDleEot(void);
	int WaitResponce(void);
	int WaitAck(void);
	int SendCommand(char* Command);
	int ReSendCommand(void);
	int TimeOut(void);
	int EnableWrite(void);
	int SetRetries(void);
	int ReturnPosition(void);

	// inline
	int GetRetries(void)	{ return m_iCounterRetries;}
	void ResetRetries(void)	{ m_iCounterRetries = 0;}

/* Build or valid frame*/
	char BuildBcc(char* pzData);
	int BuildFrame(char* pzData, uchar ucBcc=0, uchar ucCR=0);
	int ValidFrame(uchar ucBcc=0, uchar ucCR=0);

#ifdef STESUD_TRACE // only debug
	void ShowResult(void);
#endif

protected:
	int ClearMemory(void);
	int Enable(void);
	int Disable(void);


private:

	enum e_ControlCaract
	{
		eStx  = 0x02,
		eEtx  = 0x03,
		eCr   = 0x0d,
		eEnq  = 0x05,
		eAck  = 0x06,
		eNack = 0x15,
		eDle  = 0x10,
		eEot  = 0x04
	};
	enum e_TypeReader
	{
		eNoneAvailable = 0,
		eReadOnly,
		eReadWrite
	};
	int m_iCounterTimeout;
	int m_iCounterRetries;
	
	char m_szInitString[15];	
	char m_szCommand[120];
	u_ReaderDataStruct m_Result;	
	int m_iNbrCharValid;
	e_TypeReader m_eTypeReader;

	CSerial m_chSerial;
	CStateMachineDigCard* m_pcStateMachine; 
	s_Parameters m_sParametersInit;


};

#endif /*__CdRdIDig_H__*/