/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Classe d'interface avec le pilote Linux COMEDILib pour la carte d'I/O PCI.

*/

/* ************************************************************************** */
/* Revision:
### 20121219 JFM
	Verision d'origine.

### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

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

#include "Comedilibinterface.h"
#include <QString>

CComediLibInterface::CComediLibInterface():
	mOpened(false)
{
	mInputBuf = 0;
}

CComediLibInterface::~CComediLibInterface()
{
#ifndef NO_PCI_CARD_INSTALLED
	comedi_close(mComediDevice);
#endif
}


unsigned int CComediLibInterface::OpenPCIInterface()
{

	if(mOpened)
		return PCIIO_DEVICE_ALREADY_OPENED;

	QString comediFilename = "/dev/comedi0";


	CEngLog::instance()->AddLogString(QString("Ouverture de la carte PCI :") + comediFilename,3);
	mComediDevice = comedi_open(comediFilename.toAscii().data());
	if(!mComediDevice)
	{

		CEngLog::instance()->AddLogString("L'ouverture de la carte PCI a échouée",1);
		return PCIIO_CANNOT_OPEN_DEVICE;
	}
	else
	{
		QString boardname;
		boardname = comedi_get_board_name(mComediDevice);

		//Check that we have the good board installed.
		if(boardname != PCI_DEVICE_NAME)
		{
			CEngLog::instance()->AddLogString(QString("").sprintf("Erreur. Incohérence du type de carte; Voulu: %s, Réel: %s",PCI_DEVICE_NAME,boardname.toAscii().data()),1);
			return PCIIO_DEVICE_MISMATCH;
		}
		else
		{
			CEngLog::instance()->AddLogString(QString("Carte PCI initialisée :") + boardname,3);
		}

//		int NbSubDevices = comedi_get_n_subdevices(mComediDevice);
//		qDebug("Nb Subdevices: %d",NbSubDevices);

		//Check that the inputs and outputs subdevices are correct.
		int SubDeviceType = comedi_get_subdevice_type(mComediDevice,PCI_OUTPUTS_SUBDEVICE_ID);
		if(SubDeviceType != COMEDI_SUBD_DO)
		{
			CEngLog::instance()->AddLogString(QString("").sprintf("Erreur. Incohérence du type de Output Subdevice; Voulu: %d, Réel: %d",SubDeviceType,PCI_OUTPUTS_SUBDEVICE_ID),1);
			 return PCIIO_OUTPUTS_SUBDEVICE_MISMATCH;
		}

		SubDeviceType = comedi_get_subdevice_type(mComediDevice,PCI_INPUTS_SUBDEVICE_ID);
		if(SubDeviceType != COMEDI_SUBD_DI)
		{
			CEngLog::instance()->AddLogString(QString("").sprintf("Erreur. Incohérence du type de Output Subdevice; Voulu: %d, Réel: %d",SubDeviceType,PCI_INPUTS_SUBDEVICE_ID),1);
			return PCIIO_INPUTS_SUBDEVICE_MISMATCH;
		}
		//qDebug("type");
	}

	CEngLog::instance()->AddLogString("Carte PCI ouverte avec succès",3);
	mOpened = true;
	return PCIIO_OK;
}

unsigned int CComediLibInterface::GetInputs()
{
	unsigned int Buf;
	mMutex.lock();
	comedi_dio_bitfield2(mComediDevice,0,0xFFFFFFFF,&Buf,0);
//	Buf = mInputBuf;
	mMutex.unlock();

	return Buf;
}

comedi_t *CComediLibInterface::GetPCIDevice()
{
	if(mOpened == false)
		return NULL;

	return mComediDevice;
}





