/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Classe d'interface avec la carte d'I/O PCI1756.

*/

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

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

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

#include "PCI1756Interface.h"
#include "PCI1756Definitions.h"
#include <QString>
#include <QDebug>

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

CPCI1756Interface::~CPCI1756Interface()
{
#ifndef NO_PCI_CARD_INSTALLED
    if(mOpened)
        mInputCtrl->Dispose();
#endif
}


unsigned int CPCI1756Interface::OpenPCIInterface()
{

    if(mOpened)
		return PCIIO_DEVICE_ALREADY_OPENED;



	CEngLog::instance()->AddLogString(QString("Ouverture de la carte PCI :"),3);

	mInputCtrl = AdxInstantDiCtrlCreate();
	ICollection<DeviceTreeNode> *supportedDevices = mInputCtrl->getSupportedDevices();
	if(supportedDevices->getCount() == 0)
	{
		CEngLog::instance()->AddLogString("L'ouverture de la carte PCI a échouée (Aucune carte n'est installée)",1);
		return PCIIO_CANNOT_OPEN_DEVICE;
	}

	bool found = false;
	for (int i = 0; i < supportedDevices->getCount(); i++)
	{
		DeviceTreeNode const &node = supportedDevices->getItem(i);
		QString DeviceDescription(QString::fromWCharArray(node.Description));
		qDebug() << "Device Nbr: " << node.DeviceNumber << " : "<< DeviceDescription;

		if(QString::fromWCharArray(node.Description).contains(PCI_DAQNAVI_NAME))
		{
			if(mInputCtrl->setSelectedDevice(DeviceInformation(node.Description)) != Success)
			{
				CEngLog::instance()->AddLogString("L'ouverture de la carte PCI a échouée (Carte occupée)",1);
				supportedDevices->Dispose();
				mInputCtrl->Dispose();
				return PCIIO_CANNOT_OPEN_DEVICE;
			}
			CEngLog::instance()->AddLogString(QString("Carte PCI initialisée :") + QString::fromWCharArray(node.Description),3);
			found = true;
			break;
		}
	}
	if(found == false)
	{
		CEngLog::instance()->AddLogString(QString("").sprintf("Erreur. Incohérence du type de carte"),1);
		return PCIIO_DEVICE_MISMATCH;
	}

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

}

unsigned int CPCI1756Interface::GetInputs()
{
	unsigned int Buf = 0;
	unsigned char data[4];
	memset(data,0,4);
	mMutex.lock();
	ErrorCode errorCode = Success;
	errorCode = mInputCtrl->Read(0,4,(unsigned char *)&Buf);
	if(errorCode != Success)
	{
		qDebug("PCI read Error");
	}

	mMutex.unlock();


	return Buf;
}




