/**************************************************************************
<MODULE NAME>
<FILENAME></FILENAME>
<HISTORY>
	<MODIFICATION>
		<DATE></DATE>
		<ATHOR></ATHOR>
		< DESCRIPTION>
		< /DESCRIPTION>
	</MODIFICATION>
</HISTORY>
</MODULE NAME>
**************************************************************************/


/**************************************************************************
INCLUDE FILE
**************************************************************************/
#include <string.h>
#include <dos.h>
#include <STDIO.H>
#include "..\comp\PcDetec\PcDetec.h"
#include "..\comp\cardread\CdRdIDig.h"
#include "..\comp\system\system.h"

#define MAX_TRYES_FOR_READY 1000000L

CDetecPC::CDetecPC()
{
}

CDetecPC::~CDetecPC()
{
}

/**************************************************************************
<METHOD>
	<NAME>TypeOfPc</NAME>
	<TYPE >GLOBAL</TYPE>
	<DESCRIPTION>Detect the type of Pc</DESCRIPTION>
    <RETURNS>
		<TYPE>e_PcType</TYPE>
		<DESCRIPTION>
			return the type of pc
		</DESCRIPTION>
    </RETURNS>
</METHOD>
**************************************************************************/
CTypePc::e_PcType CDetecPC::TypeOfPc(void)
{
	CTypePc::e_PcType hResult = CTypePc::eUnknown;
	hResult = VerifyJites();

	if(hResult == CTypePc::eUnknown)
	{
		hResult = VerifyDigiVote();
	}
	return hResult;
}

/**************************************************************************
<METHOD>
	<NAME>VerifyJites</NAME>
	<TYPE >GLOBAL</TYPE>
	<DESCRIPTION>Vérifiy, if is jites machines</DESCRIPTION>
	<RETURNS>
			<TYPE>e_PcType</TYPE>
			<DESCRIPTION>
				Return, the type of machine
			</DESCRIPTION>
	</RETURNS>
</METHOD>
**************************************************************************/
CTypePc::e_PcType CDetecPC::VerifyJites(void)
{
	CTypePc::e_PcType hResult = CTypePc::eUnknown;
	char szTerminal[16];

  if(strcmp((char*)ReadMemory((char*)szTerminal , JITES_MAV_ADD_VER1, 15), JITES_MAV_TXT_VER1) == 0)
  {
		hResult = CTypePc::eJiteMavVer1;
  }
  else if(strcmp(ReadMemory((char*)szTerminal , JITES_MAV_ADD_VER2, 15), JITES_MAV_TXT_VER2) == 0)
  {
		hResult = CTypePc::eJiteMavVer2;
  }
  else if(strcmp(ReadMemory((char*)szTerminal , JITES_URNE_ADD_VER1, 13), JITES_URNE_TXT_VER1) == 0)
  {
		hResult = CTypePc::eJiteUrneVer1;
  }
  else if(strcmp(ReadMemory((char*)szTerminal , JITES_URNE_ADD_VER2, 13), JITES_URNE_TXT_VER2) == 0)
  {
		hResult = CTypePc::eJiteUrneVer2;
  }
	return hResult;
}

/**************************************************************************
<METHOD>
	<NAME></NAME>
	<TYPE > LOCAL | GLOBAL </TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</ IO >
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
    <RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
    </RETURNS>
</METHOD>
**************************************************************************/
CTypePc::e_PcType CDetecPC::VerifyDigiVote(void)
{
	CTypePc::e_PcType eResult = CTypePc::eUnknown;
	uchar ucRep = CardReader();
	if(ucRep == 1)
	{// validator
		if(Keyboard())
		{
			//Error, detection of keyboard;
			;
		}
		else
		{
			eResult = CTypePc::eDigivotMav1;
		}
	}
	else if(ucRep == 3)
	{// urne + validator
		if(Keyboard())
		{
			eResult = CTypePc::eDigivotUrne2;
		}
		else
		{
			// Error, pas de clavier sur une urne.
			;
		}
	}
	else if(ucRep == 2)
	{// urne only ???
		;
	}
	else
	{ // error
		;
	}
	return eResult;
}


/**************************************************************************
<METHOD>
	<NAME></NAME>
	<TYPE > LOCAL | GLOBAL </TYPE>
	<DESCRIPTION></DESCRIPTION>
	<PARAMETERS>
		<PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO> IN | OUT | INOUT</ IO >
			<DESCRIPTION>
			</DESCRIPTION>
		</PARAMETER>
    <RETURNS>
		<TYPE></TYPE>
		<DESCRIPTION>
		</DESCRIPTION>
    </RETURNS>
</METHOD>
**************************************************************************/
char* CDetecPC::ReadMemory(char* pzString, unsigned long ulAddress, unsigned int uiSize )
{
	unsigned char ucIndex;
	unsigned char far *pointy;

	/*Read direcly in the memory*/
	pointy = (unsigned char far*)ulAddress;

  for(ucIndex = 0;ucIndex < uiSize;ucIndex++)
		  pzString[ucIndex] = *pointy++;

	/* insert null*/
	pzString[ucIndex] = 0x00;
	return pzString;
}

uchar CDetecPC::Keyboard(void)
{
	uchar ucRep;
	//KbSendCommand(0xEE);
	outp(0x60,0xEE);
	delay(50);
	//kbWaitForReady();
	ucRep = inp(0x60);
	if(ucRep == 0xEE)
	{
		// ACK received -> keyboard present
		return 1;
	}
	return 0;  // no ACK       -> no keyboard
}

uchar CDetecPC::CardReader(void)
{
	uchar ucRep = 0;
	s_Parameters shParmValidator, shParmUrne;
	CCardReaderDig chValidator, chUrne;
	shParmUrne.iPort = 0;
	shParmUrne.iSetting = _COM_CHR7 | _COM_STOP1 | _COM_EVENPARITY| _COM_9600;
	shParmValidator.iPort = 1;
	shParmValidator.iSetting = _COM_CHR7 | _COM_STOP1 | _COM_EVENPARITY| _COM_9600;

	int iRep = chValidator.LecteurInit(&shParmValidator);
	if(iRep == 0)
	{
		ucRep += 1;
	}
	iRep = chUrne.LecteurInit(&shParmUrne);
	if(iRep == 0)
	{
		ucRep += 2;
	}
	return ucRep;
}

void CDetecPC::kbWaitForReady(void)
{
	volatile long i=MAX_TRYES_FOR_READY;
	while (i-- && (inportb(0x64) & 0x02)) ;
}

void CDetecPC::KbSendCommand(unsigned char c)
{
	kbWaitForReady();
	outportb(0x64,c);
}
