#include "stdafx.h"
#include "MasterRD.h"

#define INITPROCADDR(dllIns,funcName) (FARPROC&)funcName = GetProcAddress(dllIns,#funcName);\
	if(NULL==funcName){\
	CString szMsg;\
	szMsg.Format(_T("Function: %s can't be found."),#funcName);\
	AfxMessageBox(szMsg);\
	FreeLibrary(dllIns);\
	return FALSE;\
}

int    (WINAPI *rf_GetVer)(UINT *nVer);
int    (WINAPI *rf_DES_Encrypt)(BYTE *pcOut, BYTE *pcIn, UINT nInLen, BYTE *pcKey, UINT nKeyLen);
int    (WINAPI *rf_DES_Decrypt)(BYTE *pcOut, BYTE *pcIn, UINT nInLen, BYTE *pcKey, UINT nKeyLen);
int    (WINAPI *rf_COM_Open)(UINT port, int baud);
int    (WINAPI *rf_COM_Close)(UINT port);
BOOL   (WINAPI *rf_COM_IsOpen)(UINT port);
int    (WINAPI *rf_COM_GetBaud)(UINT port);
int    (WINAPI *rf_Net_Open)(LPCSTR szIPAddr, UINT port);
int    (WINAPI *rf_Net_Close)(LPCSTR szIPAddr, UINT port);
BOOL   (WINAPI *rf_Net_IsOpen)(LPCSTR szIPAddr, UINT port);
int    (WINAPI *rf_Reader_ComOpen)(UINT port, WORD nReaderID);
int    (WINAPI *rf_Reader_TcpOpen)(LPCSTR szIPAddr, UINT port, WORD nReaderID);
int    (WINAPI *rf_Reader_Close)(WORD nReaderID);
BOOL    (WINAPI *rf_Reader_IsOpen)(WORD nReaderID);
int    (WINAPI *rf_Reader_SetID)(WORD nReaderID, WORD nNewReaderID);
int    (WINAPI *rf_Reader_GetID)(WORD *pnReaderID);
int    (WINAPI *rf_Reader_GetFirmwareVer)(WORD nReaderID, WORD *pnVersion);
int    (WINAPI *rf_Reader_GetSN)(WORD nReaderID, BYTE *pcSNR);
int    (WINAPI *rf_Reader_SetBeep)(WORD nReaderID, BYTE cTmie);
int    (WINAPI *rf_Reader_SetLight)(WORD nReaderID, BYTE cColor);
int    (WINAPI *rf_Reader_AntennaON)(WORD nReaderID);
int    (WINAPI *rf_Reader_AntennaOFF)(WORD nReaderID);
int    (WINAPI *rf_SAM_InitBaudrate)(WORD nReaderID, BYTE cSamNo, UINT nBaudRate);
int    (WINAPI *rf_SAM_RST)(WORD nReaderID, BYTE cSamNo, BYTE *pcRstInfo, BYTE *pcDataLen);
int    (WINAPI *rf_SAM_COS)(WORD nReaderID, BYTE cSamNo, BYTE *pcApduIn, BYTE cApduLen, BYTE *pcResultOut, BYTE *pcResultLen, DWORD dwTimeOut);
int    (WINAPI *rf_SAM_SetPPS)(WORD nReaderID, BYTE cSamNo, UINT nBaudRate);
int    (WINAPI *rf_Reader_SetISOType)(WORD nReaderID, BYTE cType);
int    (WINAPI *rf_ISO14443A_GetCard)(WORD nReaderID, BYTE cMode, BYTE *pcDataOut, BYTE *pcDataLen);
int    (WINAPI *rf_ISO14443A_Request)(WORD nReaderID, BYTE cMode, USHORT *cpATQA);
int    (WINAPI *rf_ISO14443A_AnticollisionAndSelect)(WORD nReaderID, BYTE *pcOutData, BYTE *pcDataLen);
int    (WINAPI *rf_ISO14443A_AnticollisionLevel1)(WORD nReaderID, BYTE *pcSNR);
int    (WINAPI *rf_ISO14443A_AnticollisionLevel2)(WORD nReaderID, BYTE *pcSNR);
int    (WINAPI *rf_ISO14443A_AnticollisionLevel3)(WORD nReaderID, BYTE *pcSNR);
int    (WINAPI *rf_ISO14443A_SelectLevel1)(WORD nReaderID, BYTE *pcSNR, BYTE *pcSAK);
int    (WINAPI *rf_ISO14443A_SelectLevel2)(WORD nReaderID, BYTE *pcSNR, BYTE *pcSAK);
int    (WINAPI *rf_ISO14443A_SelectLevel3)(WORD nReaderID, BYTE *pcSNR, BYTE *pcSAK);
int    (WINAPI *rf_ISO14443A_Halt)(WORD nReaderID);
int    (WINAPI *rf_Reader_SetAuthenticationStandard)(WORD nReaderID, BYTE cStandard);
int    (WINAPI *rf_M1_Authentication)(WORD nReaderID, BYTE cMode, BYTE cBlock, BYTE *pcKey);
int    (WINAPI *rf_M1_DownloadKeyToReader )(WORD nReaderID, BYTE cKeyIndex, BYTE *pcKey);
int    (WINAPI *rf_M1_AuthenticateByDownloadedKey)(WORD nReaderID, BYTE cMode, BYTE cBlock, BYTE cKeyIndex);
int    (WINAPI *rf_M1_Read)(WORD nReaderID, BYTE cBlock, BYTE *pData/*, BYTE* pLen*/);
int    (WINAPI *rf_M1_Write)(WORD nReaderID, BYTE cBlock, BYTE *pData/*, BYTE len*/);
int    (WINAPI *rf_M1_PurseCreate)(WORD nReaderID, BYTE cBlock, int iValue);
int    (WINAPI *rf_M1_PurseRead)(WORD nReaderID, BYTE cBlock, int *piValue);
int    (WINAPI *rf_M1_PurseDecrement)(WORD nReaderID, BYTE cBlock, int iValue);
int    (WINAPI *rf_M1_PurseIncrement)(WORD nReaderID, BYTE cBlock, int iValue);
int    (WINAPI *rf_M1_PurseBackup)(WORD nReaderID, BYTE cSrcBlock, BYTE cDestBlock);
int    (WINAPI *rf_M1_PurseRestore)(WORD nReaderID, BYTE cBlock);
int    (WINAPI *rf_M1_PurseTransfer)(WORD nReaderID, BYTE cBlock);
int    (WINAPI *rf_UltraLight_Write)(WORD nReaderID, BYTE cBlock, BYTE *pcData);
int    (WINAPI *rf_UltraLight_Read)(WORD nReaderID, BYTE cBlock, BYTE *pcData);
int    (WINAPI *rf_ISO14443_4_COS)(WORD nReaderID, BYTE *pcApduIn, BYTE cApduLen, BYTE *pcResultOut, BYTE* pcResultLen, DWORD dwTimeOut);
int    (WINAPI *rf_ISO14443_4_TypeA_RATS)(WORD nReaderID, BYTE *pcATS, BYTE *pcLen);
int    (WINAPI *rf_ISO14443B_Request)(WORD nReaderID, BYTE cMode, BYTE *pcATQB/*, BYTE *pDataLen*/);
int    (WINAPI *rf_ISO14443B_GetCard)(WORD nReaderID, BYTE cMode, BYTE cAFI, BYTE cNSlot, BYTE *pcATQB);
int    (WINAPI *rf_ISO14443B_Halt)(WORD nReaderID, unsigned char *pcPUPI);
int    (WINAPI *rf_AT88RF020_Check)(WORD nReaderID, BYTE *pcKey);
int    (WINAPI *rf_AT88RF020_Read)(WORD nReaderID, BYTE cPageAddr, BYTE *pDataOut/*, BYTE* pMsgLen*/);
int    (WINAPI *rf_AT88RF020_write)(WORD nReaderID, BYTE cPageAddr, BYTE *pDataIn/*,BYTE len*/);
int    (WINAPI *rf_AT88RF020_Lock)(WORD nReaderID, BYTE *pDataIn/*,BYTE len*/);
int    (WINAPI *rf_AT88RF020_Count)(WORD nReaderID, BYTE *pDataIn/*, BYTE len*/);
int    (WINAPI *rf_AT88RF020_Deselect)(WORD nReaderID);
int    (WINAPI *rf_ST_GetCard)(WORD nReaderID, BYTE *pcCardID);
int    (WINAPI *rf_ST_Completion)(WORD nReaderID);
int    (WINAPI *rf_ST_SR176_Select)(WORD nReaderID, BYTE* pcCardID);
int    (WINAPI *rf_ST_SR176_Completion)(WORD nReaderID);
int    (WINAPI *rf_ST_SR176_ReadBlock)(WORD nReaderID, BYTE cBlock, BYTE *pcDataOut/*, BYTE *pcDataLen*/);
int    (WINAPI *rf_ST_SR176_WriteBlock)(WORD nReaderID, BYTE cBlock, BYTE *pcDataIn);
int    (WINAPI *rf_ST_SR176_ProtectBlock)(WORD nReaderID, BYTE cLockReg);
int    (WINAPI *rf_ST_SR176_GetProtectReg)(WORD nReaderID, BYTE *cLockReg);
int    (WINAPI *rf_ST_SRI_Select)(WORD nReaderID, BYTE *pcCardID);
int    (WINAPI *rf_ST_SRI_Completion)(WORD nReaderID);
int    (WINAPI *rf_ST_SRI_GetUID)(WORD nReaderID, BYTE *pcUID/*, BYTE *pLen*/);
int    (WINAPI *rf_ST_SRI_ReadBlock)(WORD nReaderID, BYTE cBlock, BYTE *pcData/*, BYTE *pLen*/);
int    (WINAPI *rf_ST_SRI_WriteBlock)(WORD nReaderID, BYTE cBlock, BYTE *pcData);
int    (WINAPI *rf_ST_SRIX_Authenticate)(WORD nReaderID, BYTE *pcRND, BYTE *pcSIG/*, BYTE *pLen*/);
int    (WINAPI *rf_ST_SRI_WriteLockReg)(WORD nReaderID, BYTE cLockReg);
int    (WINAPI *rf_ISO15693_Inventory)(WORD nReaderID, BYTE *pData, BYTE *pLen);
int    (WINAPI *rf_ISO15693_MultiInventory)(WORD nReaderID, BYTE cAFIEnable, BYTE AFI, BYTE *pData, BYTE *pLen);
int    (WINAPI *rf_ISO15693_StayQuiet)(WORD nReaderID, BYTE *UID);
int    (WINAPI *rf_ISO15693_Select)(WORD nReaderID, BYTE *UID);
int    (WINAPI *rf_ISO15693_ResetToReady)(WORD nReaderID, BYTE mode, BYTE *UID);
int    (WINAPI *rf_ISO15693_Read)(WORD nReaderID, BYTE  model, BYTE  *UID, BYTE  block, BYTE  number, BYTE  *pData, BYTE  *pLen);
int    (WINAPI *rf_ISO15693_Write)(WORD nReaderID, BYTE  model, BYTE  *UID, BYTE  block, BYTE *data);
int    (WINAPI *rf_ISO15693_LockBlock)(WORD nReaderID, BYTE model, BYTE *UID, BYTE block);
int    (WINAPI *rf_ISO15693_WriteAFI)(WORD nReaderID, BYTE model, BYTE *UID, BYTE AFI);
int    (WINAPI *rf_ISO15693_LockAFI)(WORD nReaderID, BYTE model, BYTE *UID);
int    (WINAPI *rf_ISO15693_WriteDSFID)(WORD nReaderID, BYTE model, BYTE *UID, BYTE DSFID);
int    (WINAPI *rf_ISO15693_LockDSFID)(WORD nReaderID, BYTE model, BYTE *UID);
int    (WINAPI *rf_ISO15693_GetSystemInfomation)(WORD nReaderID, BYTE  model, BYTE  *UID, BYTE  *pData, BYTE  *pLen);
int    (WINAPI *rf_ISO15693_GetBlockSecurity)(WORD nReaderID, BYTE  model, BYTE  *UID, BYTE  block, BYTE  number, BYTE  *pData, BYTE  *pLen);
int    (WINAPI *rf_Reader_DigtalDisplay)(WORD nReaderID, BYTE cDecodeModel, BYTE cDpPosition, BYTE * DisplayData);
int    (WINAPI *rf_Reader_FlashWrite)(WORD nReaderID, UINT iBlock, BYTE cStartAddress, BYTE cBytes, BYTE * WriteData);
int    (WINAPI *rf_Reader_FlashRead)(WORD nReaderID, UINT iBlock, BYTE cStartAddress, BYTE cBytes, BYTE * pData);
int    (WINAPI *rf_AT88RF080_Request)(WORD nReaderID, BYTE cAFI, BYTE cParameter, BYTE *pData, BYTE *pLen);
int    (WINAPI *rf_MFP_L0WritePerso)(WORD nReaderID,USHORT block,BYTE *data);
int    (WINAPI *rf_MFP_L0CommitPerso)(WORD nReaderID);
int    (WINAPI *rf_MFP_SwitchToHighLevel)(WORD nReaderID, BYTE cLevel,BYTE *switchkey);
int    (WINAPI *rf_MFP_DataBlockAuthentication)(WORD nReaderID,BYTE keytype,BYTE blockid,BYTE *keydata/*,BYTE len*/);
int    (WINAPI *rf_MFP_L3PurseCreate)(WORD nReaderID, BYTE blockid, UINT purseval);
int    (WINAPI *rf_MFP_L3PurseRead)(WORD nReaderID, BYTE blockid, UINT *purseval);
int    (WINAPI *rf_MFP_L3PurseIncrement)(WORD nReaderID, BYTE blockid, UINT purseval);
int    (WINAPI *rf_MFP_L3PurseDecrement)(WORD nReaderID, BYTE blockid, UINT purseval);
int    (WINAPI *rf_MFP_L3PurseBackup)(WORD nReaderID, BYTE SrcBlock, BYTE DestBlock);
int    (WINAPI *rf_MFP_FirstAuthentication)(WORD nReaderID, USHORT AES_keyaddr, BYTE *AES_keydata);
int    (WINAPI *rf_MFP_FollowingAuthentication)(WORD nReaderID, USHORT AES_keyaddr, BYTE *AES_keydata);
int    (WINAPI *rf_MFP_L3Read)(WORD nReaderID, USHORT blockid, BYTE blockcount, BYTE *data);
int    (WINAPI *rf_MFP_L3Write)(WORD nReaderID, USHORT blockid, BYTE blockcount, BYTE *data);
int    (WINAPI *rf_IDCard_Request)(WORD nReaderID, BYTE *cardinfo, UINT *infolen);
int    (WINAPI *rf_IDCard_COS)(WORD nReaderID, BYTE *command, UINT cmdLen, BYTE *pData, UINT *Length);

BOOL MasterRD_Load_rfLib(HINSTANCE *phInstMaster)
{
	TCHAR szBuf[MAX_PATH];	
	GetModuleFileName(AfxGetInstanceHandle(), (LPTSTR)szBuf, MAX_PATH);
	*_tcsrchr(szBuf, _T('\\')) = (TCHAR)0;    
	CString strDll;
	strDll += szBuf;
	strDll += _T("\\MasterRD.Dll");

	*phInstMaster = LoadLibrary(strDll);	 
	if(!(*phInstMaster))
	{
		AfxMessageBox(_T("Can't load MasterRD.dll!"));
		exit(0);
		return FALSE;
	}
	
	INITPROCADDR((*phInstMaster), rf_GetVer);
	INITPROCADDR((*phInstMaster), rf_DES_Encrypt);
	INITPROCADDR((*phInstMaster), rf_DES_Decrypt);
	INITPROCADDR((*phInstMaster), rf_COM_Open);
	INITPROCADDR((*phInstMaster), rf_COM_Close);
	INITPROCADDR((*phInstMaster), rf_COM_IsOpen);
	INITPROCADDR((*phInstMaster), rf_COM_GetBaud);
	INITPROCADDR((*phInstMaster), rf_Net_Open);
	INITPROCADDR((*phInstMaster), rf_Net_Close);
	INITPROCADDR((*phInstMaster), rf_Net_IsOpen);
	INITPROCADDR((*phInstMaster), rf_Reader_TcpOpen);
	INITPROCADDR((*phInstMaster), rf_Reader_ComOpen);
	INITPROCADDR((*phInstMaster), rf_Reader_Close);
	INITPROCADDR((*phInstMaster), rf_Reader_IsOpen);
	INITPROCADDR((*phInstMaster), rf_Reader_SetID);
	INITPROCADDR((*phInstMaster), rf_Reader_GetID);
	INITPROCADDR((*phInstMaster), rf_Reader_GetFirmwareVer);
	INITPROCADDR((*phInstMaster), rf_Reader_GetSN);
	INITPROCADDR((*phInstMaster), rf_Reader_SetBeep);
	INITPROCADDR((*phInstMaster), rf_Reader_SetLight);
	INITPROCADDR((*phInstMaster), rf_Reader_AntennaON);
	INITPROCADDR((*phInstMaster), rf_Reader_AntennaOFF);
	INITPROCADDR((*phInstMaster), rf_SAM_InitBaudrate);
	INITPROCADDR((*phInstMaster), rf_SAM_RST);
	INITPROCADDR((*phInstMaster), rf_SAM_COS);
	INITPROCADDR((*phInstMaster), rf_SAM_SetPPS);
	INITPROCADDR((*phInstMaster), rf_Reader_SetISOType);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_Request);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_AnticollisionAndSelect);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_AnticollisionLevel1);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_AnticollisionLevel2);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_AnticollisionLevel3);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_SelectLevel1);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_SelectLevel2);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_SelectLevel3);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_Halt);
	INITPROCADDR((*phInstMaster), rf_ISO14443A_GetCard);
	INITPROCADDR((*phInstMaster), rf_ISO14443_4_TypeA_RATS);
	INITPROCADDR((*phInstMaster), rf_M1_DownloadKeyToReader );
	INITPROCADDR((*phInstMaster), rf_Reader_SetAuthenticationStandard);
	INITPROCADDR((*phInstMaster), rf_M1_AuthenticateByDownloadedKey);
	INITPROCADDR((*phInstMaster), rf_M1_Authentication);
	INITPROCADDR((*phInstMaster), rf_M1_Read);
	INITPROCADDR((*phInstMaster), rf_M1_Write);
	INITPROCADDR((*phInstMaster), rf_M1_PurseCreate);
	INITPROCADDR((*phInstMaster), rf_M1_PurseRead);
	INITPROCADDR((*phInstMaster), rf_M1_PurseDecrement);
	INITPROCADDR((*phInstMaster), rf_M1_PurseIncrement);
	INITPROCADDR((*phInstMaster), rf_M1_PurseRestore);
	INITPROCADDR((*phInstMaster), rf_M1_PurseTransfer);
	INITPROCADDR((*phInstMaster), rf_M1_PurseBackup);
	INITPROCADDR((*phInstMaster), rf_UltraLight_Write);
	INITPROCADDR((*phInstMaster), rf_UltraLight_Read);
	INITPROCADDR((*phInstMaster), rf_ISO14443_4_COS);
	INITPROCADDR((*phInstMaster), rf_ISO14443B_Request);
	INITPROCADDR((*phInstMaster), rf_ISO14443B_GetCard);
	INITPROCADDR((*phInstMaster), rf_ISO14443B_Halt);
	INITPROCADDR((*phInstMaster), rf_AT88RF020_Check);
	INITPROCADDR((*phInstMaster), rf_AT88RF020_Read);
	INITPROCADDR((*phInstMaster), rf_AT88RF020_write);
	INITPROCADDR((*phInstMaster), rf_AT88RF020_Lock);
	INITPROCADDR((*phInstMaster), rf_AT88RF020_Count);
	INITPROCADDR((*phInstMaster), rf_AT88RF020_Deselect);
	INITPROCADDR((*phInstMaster), rf_ST_GetCard);
	INITPROCADDR((*phInstMaster), rf_ST_Completion);
	INITPROCADDR((*phInstMaster), rf_ST_SR176_Select);
	INITPROCADDR((*phInstMaster), rf_ST_SR176_Completion);
	INITPROCADDR((*phInstMaster), rf_ST_SR176_ReadBlock);
	INITPROCADDR((*phInstMaster), rf_ST_SR176_WriteBlock);
	INITPROCADDR((*phInstMaster), rf_ST_SR176_ProtectBlock);
	INITPROCADDR((*phInstMaster), rf_ST_SR176_GetProtectReg);
	INITPROCADDR((*phInstMaster), rf_ST_SRI_ReadBlock);
	INITPROCADDR((*phInstMaster), rf_ST_SRI_WriteBlock);
	INITPROCADDR((*phInstMaster), rf_ST_SRIX_Authenticate);
	INITPROCADDR((*phInstMaster), rf_ST_SRI_Select);
	INITPROCADDR((*phInstMaster), rf_ST_SRI_Completion);
	INITPROCADDR((*phInstMaster), rf_ST_SRI_GetUID);
	INITPROCADDR((*phInstMaster), rf_ST_SRI_WriteLockReg);
	INITPROCADDR((*phInstMaster), rf_ISO15693_Inventory);
	INITPROCADDR((*phInstMaster), rf_ISO15693_MultiInventory);
	INITPROCADDR((*phInstMaster), rf_ISO15693_StayQuiet);
	INITPROCADDR((*phInstMaster), rf_ISO15693_Select);
	INITPROCADDR((*phInstMaster), rf_ISO15693_ResetToReady);
	INITPROCADDR((*phInstMaster), rf_ISO15693_Read);
	INITPROCADDR((*phInstMaster), rf_ISO15693_Write);
	INITPROCADDR((*phInstMaster), rf_ISO15693_LockBlock);
	INITPROCADDR((*phInstMaster), rf_ISO15693_WriteAFI);
	INITPROCADDR((*phInstMaster), rf_ISO15693_LockAFI);
	INITPROCADDR((*phInstMaster), rf_ISO15693_WriteDSFID);
	INITPROCADDR((*phInstMaster), rf_ISO15693_LockDSFID);
	INITPROCADDR((*phInstMaster), rf_ISO15693_GetSystemInfomation);
	INITPROCADDR((*phInstMaster), rf_ISO15693_GetBlockSecurity);
	INITPROCADDR((*phInstMaster), rf_Reader_DigtalDisplay);
	INITPROCADDR((*phInstMaster), rf_Reader_FlashWrite);
	INITPROCADDR((*phInstMaster), rf_Reader_FlashRead);
	INITPROCADDR((*phInstMaster), rf_AT88RF080_Request);
	INITPROCADDR((*phInstMaster), rf_MFP_L0WritePerso);
	INITPROCADDR((*phInstMaster), rf_MFP_L0CommitPerso);
	INITPROCADDR((*phInstMaster), rf_MFP_SwitchToHighLevel);
	INITPROCADDR((*phInstMaster), rf_MFP_DataBlockAuthentication);
	INITPROCADDR((*phInstMaster), rf_MFP_L3PurseCreate);
	INITPROCADDR((*phInstMaster), rf_MFP_L3PurseRead);
	INITPROCADDR((*phInstMaster), rf_MFP_L3PurseIncrement);
	INITPROCADDR((*phInstMaster), rf_MFP_L3PurseDecrement);
	INITPROCADDR((*phInstMaster), rf_MFP_L3PurseBackup);
	INITPROCADDR((*phInstMaster), rf_MFP_FirstAuthentication);
	INITPROCADDR((*phInstMaster), rf_MFP_FollowingAuthentication);
	INITPROCADDR((*phInstMaster), rf_MFP_L3Read);
	INITPROCADDR((*phInstMaster), rf_MFP_L3Write);
	INITPROCADDR((*phInstMaster), rf_IDCard_Request);
	INITPROCADDR((*phInstMaster), rf_IDCard_COS);

	return TRUE;
}

BOOL MasterRD_Free_rfLib(HINSTANCE *phInstMaster)
{
	BOOL rt = FALSE;
	if(*phInstMaster)
	{ 
		rt = FreeLibrary(*phInstMaster);
		*phInstMaster = NULL;
	}
	return rt;
}
