/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Classe d'interface avec le driver de communication des modules ethernet
	externes (Sealevel).

*/

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

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

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

#include "Externaliomodule.h"
#include <QtGlobal>

CExternalIOModule::CExternalIOModule():
    mIsOpened(false)
{

}
CExternalIOModule::~CExternalIOModule()
{
}

#ifndef WINDOWS_OS
unsigned int CExternalIOModule::SetModuleDriverPtr(CSeaMaxLin *DriverPtr)
{
    if(DriverPtr == 0)
        return RET_ERROR;

    mModuleDriverPtr = DriverPtr;
    return RET_OK;
}

//unsigned int CExternalIOModule::OpenModule(unsigned int SlaveAddress,SeaMaxLin *DriverPtr, unsigned int ModuleID)
//{
////    if(mIsOpened == true)
////        return EXT_MOD_RET_MODULE_ALREADY_OPENED;

//////    if(SetModuleDriverPtr(DriverPtr) == RET_ERROR)
//////    {
//////        CEngLog::instance()->AddLogString("Erreur Fatale: CExternalIOModule::OpenModule, le pointeur DriverPtr est invalide",1);
//////        return EXT_MOD_RET_ERR_INVALID_POINTER;
//////    }

////    unsigned short ModuleType;
////    int Ret;
////    mModuleAddress = SlaveAddress;
////    mModuleID = ModuleID;
//// //   Ret = mModuleDriverPtr->Ioctl(mModuleAddress,IOCTL_GET_EXT_CONFIG,&ModuleType);
////	Ret = SeaMaxLinIoctl(DriverPtr,(unsigned char)SlaveAddress,IOCTL_GET_EXT_CONFIG,&ModuleType);
////    if(Ret != 0)
////    {
////        CEngLog::instance()->AddLogString(QString("").sprintf("Erreur: CExternalIOModule::OpenModule, aucune réponse du module Type: %d, addresse: %d, ID: %d",mModuleType,SlaveAddress,ModuleID),1);
////		return EXT_MOD_RET_ERR_MODULE_OFFLINE;
////    }

////    if((eExternalModuleType_t)ModuleType != mModuleType)
////    {
////        CEngLog::instance()->AddLogString(QString("").sprintf("Erreur: le module à l'addresse %d est configuré de type %d mais est plutôt de type %d",SlaveAddress,(unsigned short)mModuleType,ModuleType),1);
////        mIsOpened = false;
////		return EXT_MOD_RET_ERR_MODULE_TYPE_MISMATCH;
////    }

////    CEngLog::instance()->AddLogString(QString("").sprintf("Module ouvert avec succès. Addresse: %d, Type: %d, ID: %d",SlaveAddress,(unsigned short)mModuleType,ModuleID),3);
////    mIsOpened = true;

////    return EXT_MOD_RET_OK;
//	return EXT_MOD_RET_ERR_MODULE_OFFLINE;


//}

unsigned int CExternalIOModule::OpenModule(unsigned int SlaveAddress,CSeaMaxLin *DriverPtr, unsigned int ModuleID)
{
	if(mIsOpened == true)
		return EXT_MOD_RET_MODULE_ALREADY_OPENED;

	if(SetModuleDriverPtr(DriverPtr) == RET_ERROR)
	{
		CEngLog::instance()->AddLogString("Erreur Fatale: CExternalIOModule::OpenModule, le pointeur DriverPtr est invalide",1);
		return EXT_MOD_RET_ERR_INVALID_POINTER;
	}

	unsigned short buf[50];
	unsigned short ModuleType;
	int Ret;
	mModuleAddress = SlaveAddress;
	mModuleID = ModuleID;
	Ret = mModuleDriverPtr->Ioctl(mModuleAddress,IOCTL_READ_COMM_PARAM/*IOCTL_GET_EXT_CONFIG*/,buf);
	if(Ret != 0)
	{
		CEngLog::instance()->AddLogString(QString("").sprintf("Erreur: CExternalIOModule::OpenModule, aucune réponse du module Type: %d, addresse: %d, ID: %d",mModuleType,SlaveAddress,ModuleID),1);
		return EXT_MOD_RET_ERR_MODULE_OFFLINE;
	}
	ModuleType = buf[0];
	if((eExternalModuleType_t)ModuleType != mModuleType)
	{
		CEngLog::instance()->AddLogString(QString("").sprintf("Erreur: le module à l'addresse %d est configuré de type %d mais est plutôt de type %d",SlaveAddress,(unsigned short)mModuleType,ModuleType),1);
		mIsOpened = false;
		return EXT_MOD_RET_ERR_MODULE_TYPE_MISMATCH;
	}

	CEngLog::instance()->AddLogString(QString("").sprintf("Module ouvert avec succès. Addresse: %d, Type: %d, ID: %d",SlaveAddress,(unsigned short)mModuleType,ModuleID),3);
	mIsOpened = true;

	return EXT_MOD_RET_OK;


}
#endif
