#include "stdafx.h"
#include "MPosControllerDevices.h"

//////////////////////////////////////////////////////////////////////////

CMPosControllerDevices::CMPosControllerDevices()
{
	LoadDll(_T("MPosControllerDevices.dll"));
}

CMPosControllerDevices::CMPosControllerDevices(const CString& strFileName)
{
	LoadDll(strFileName);
}

CMPosControllerDevices::~CMPosControllerDevices()
{
	UnloadDll();
}


void CMPosControllerDevices::UnloadDll()
{
	FreeLibrary(m_hDll);
	m_hDll = NULL;
}

bool CMPosControllerDevices::LoadDll(const CString& strFileName)
{ 	
	// This file supports all Platforms, but this sample code only supports PC Platforms
	// so if you want to use this sample code in PDA platform, you should create a new project for 
	// PC Platform and add file in your project.	
	m_hDll = LoadLibrary(strFileName);
	
	int res = GetLastError();

	if(m_hDll == 0)
		return FALSE;

	fselectInterface				= (pSelectInterface)GetProcAddress(m_hDll, "selectInterface");
	fselectCommandMode				= (pSelectCommandMode)GetProcAddress(m_hDll, ("selectCommandMode"));
	fopenService					= (pOpenService)GetProcAddress(m_hDll, ("openService"));
	fopenServiceById				= (pOpenServiceById)GetProcAddress(m_hDll, ("openServiceById"));
	fcloseService					= (pCloseService)GetProcAddress(m_hDll, ("closeService"));
	fisOpen							= (pIsOpen)GetProcAddress(m_hDll, ("isOpen"));
	fgetDeviceId					= (pGetDeviceId)GetProcAddress(m_hDll, ("getDeviceId"));
	fdirectIO						= (pDirectIO)GetProcAddress(m_hDll, ("directIO"));
	fsendFile						= (pSendFile)GetProcAddress(m_hDll, ("sendFile"));
	fsetTransaction					= (pSetTransaction)GetProcAddress(m_hDll, ("setTransaction"));

	return TRUE;
}


int CMPosControllerDevices::selectInterface(int interfaceType, LPCWSTR address)
{
	return (m_nBgtId = fselectInterface(interfaceType, address));
}

int CMPosControllerDevices::selectCommandMode(int commandMode)
{
	return fselectCommandMode(m_nBgtId, commandMode);
}

int CMPosControllerDevices::openService()
{
	int ret = fopenService(m_nBgtId);
	if(ret == RES_SUCCESS || ret == RES_ALREADY_OPEN)
		m_nDeviceId = fgetDeviceId(m_nBgtId);

	return ret;
}

int CMPosControllerDevices::openServiceById(int openDeviceId, int timeout)
{
	int ret = fopenServiceById(m_nBgtId, openDeviceId, timeout);
	if(ret == RES_SUCCESS || ret == RES_ALREADY_OPEN)
		m_nDeviceId = fgetDeviceId(m_nBgtId);

	return ret;
}

int CMPosControllerDevices::closeService(int closeTimeout)
{
	return fcloseService(m_nBgtId, closeTimeout);
}

bool CMPosControllerDevices::isOpen()
{
	return fisOpen(m_nBgtId);
}

int CMPosControllerDevices::getDeviceId()
{
	return fgetDeviceId(m_nBgtId);
}

int CMPosControllerDevices::directIO(char* data, int dataLength)
{
	return fdirectIO(m_nBgtId, data, dataLength);
}

int CMPosControllerDevices::sendFile(LPCWSTR fileName)
{
	return fsendFile(m_nBgtId, fileName);
}

int CMPosControllerDevices::setTransaction(int transactionMode)
{
	return fsetTransaction(m_nBgtId, transactionMode);
}

