RFID—MFRC522 win32驱动


//------------------------------------------------------------------------------
// INCLUDE FILES  
//------------------------------------------------------------------------------
#include "bsp.h"
#include <windows.h>
#include "strucinf.h"
#include "rc522.h"
#include "Gpiocfgdefs.h"
#include "RFIDAPI32.h"




//------------------------------------------------------------------------------
// GLOBAL DEFINITIONS  
//------------------------------------------------------------------------------
#define SCNAPI32MSGQKEYDOWN   TEXT("SCNAPI32_MSGQ_KEYDOWN")
#define SCNAPI32MSGQKEYUP   TEXT("SCNAPI32_MSGQ_KEYUP")
#define RFID_API WINAPI
#define RFID_DEBUG 0
#define RFID_ERROR 0
#define MIN(a,b) ((a)>(b)?(b):(a))
#define I2C1_FID _T("I2C1:")
#define RFID_KEYDOWN   TEXT("RFID_MSGQ_KEYDOWN")
#define RFID_MAXHANDLE 0x1000
#define RFID_ANTENNA_NUM 0x1


//This is for PT20 device configuration
#define RFID_I2C 0x11




//------------------------------------------------------------------------------
// GLOBAL OR STATIC VARIABLES  
//------------------------------------------------------------------------------
//For PT20, device configuration
DEVICE_CFG g_Devcfg;


HANDLE g_hButtonThread; //Thread handle the Button event
HANDLE g_hMsgQButtonDown;            //To receive message key when tigger button is pressed
HANDLE g_hMsgQButtonUp;
BOOL g_bTriggerButtonThread = FALSE;
DWORD g_dwRFIDHandleNum = 0x1000;


BOOL g_bTriggerEnable;
BOOL g_bOpenReader;
BOOL g_bTerminateThread = FALSE;
DWORD g_dwTriggerMask;
DWORD g_dwReadMode;
BYTE g_ucRemoveSelRecIdx;


static TAG_MASK g_tagMask;
static TAG_MASK_EX g_tagMaskEx;
static TAG_LIST g_tagList;
static TAG_LIST_EX g_tagListEx;
static RFID_CAPS g_Caps;


#define E_AUD_NOTSUPPORTED 0xFF


typedef void (*LPTRIGGERCALLBACKFUNC)(DWORD dwTriggerStatus);
LPTRIGGERCALLBACKFUNC g_lpTriggerCallbackFunc;


// IOCTL to trigger scanner
#define CFG_IOCTL_TRIGGER_PIN_SET CTL_CODE(            \
                                        FILE_DEVICE_UNKNOWN, \
                                        0x1000,              \
                                        METHOD_BUFFERED,     \
                                        FILE_ANY_ACCESS      \
                                        )


extern unsigned char ReadRawRC(unsigned char RegAddr);
extern void Rc522Init(void);
extern void SetPowerDown(unsigned char ucFlag);


//------------------------------------------------------------------------------
//  FUNCTION:       PrvTriggerButtonHandler_thread
//
//  DESCRIPTION:   The PrvTriggerButtonHandler_thread is used to handle trigger button
// and start scanning
//
//  PARAMETERS:    void
//
//  RETURNS:        None.
//------------------------------------------------------------------------------
DWORD PrvTriggerButtonHandler_thread(void)
{
DWORD waitStatus;
DWORD tmp, dwBytesReturned, dwFlags;
HANDLE hWait[2];
hWait[0] = g_hMsgQButtonDown;
hWait[1] = g_hMsgQButtonUp;


//Clear All Event
ReadMsgQueue(g_hMsgQButtonDown, &tmp, sizeof(tmp), &dwBytesReturned, 1, &dwFlags);
ReadMsgQueue(g_hMsgQButtonUp, &tmp, sizeof(tmp), &dwBytesReturned, 1, &dwFlags);


//Enter the loop
while (g_bTriggerButtonThread)
{
waitStatus = WaitForMultipleObjects(2, hWait, FALSE, INFINITE);
if (waitStatus == WAIT_OBJECT_0)
{
ReadMsgQueue(g_hMsgQButtonDown, &tmp, sizeof(tmp), &dwBytesReturned, 1, &dwFlags);
g_lpTriggerCallbackFunc(1);
}
else if(waitStatus == WAIT_OBJECT_0 + 1)
{
ReadMsgQueue(g_hMsgQButtonUp, &tmp, sizeof(tmp), &dwBytesReturned, 1, &dwFlags);      
g_lpTriggerCallbackFunc(0);
}
}
CloseHandle(g_hButtonThread);
ExitThread(0);
}


//------------------------------------------------------------------------------
//FUNCTION:CreateButtonHandlerThread
//Description: The CreateButtonHandlerThread is used to handle trigger button
// create  event;
//Parameters: void
//RETURN: None.
//------------------------------------------------------------------------------
BOOL CreateButtonHandlerThread(void)
{
    MSGQUEUEOPTIONS msgOptions;
BOOL bSuccess = FALSE;    
    
//Create MsgQ to receive keydown event
  msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
msgOptions.dwFlags = MSGQUEUE_ALLOW_BROKEN;
msgOptions.dwMaxMessages = 1;
msgOptions.cbMaxMessage = 0x10;
msgOptions.bReadAccess = TRUE;
    RETAILMSG(RFID_DEBUG, (TEXT("+CreateButtonHandlerThread +\r\n")));
  g_hMsgQButtonDown = CreateMsgQueue(SCNAPI32MSGQKEYDOWN, &msgOptions);
if(g_hMsgQButtonDown == NULL)
{
RETAILMSG(RFID_DEBUG, (TEXT("+CreateButtonHandlerThread: CreateMsgQueue SCNAPI32MSGQKEYDOWN Error!\r\n")));
goto cleanUp;
}

//Create MsgQ to receive keyup event
msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
msgOptions.dwFlags = MSGQUEUE_ALLOW_BROKEN;
msgOptions.dwMaxMessages = 1;
msgOptions.cbMaxMessage = 0x10;
msgOptions.bReadAccess = TRUE;
g_hMsgQButtonUp = CreateMsgQueue(SCNAPI32MSGQKEYUP, &msgOptions);
if(g_hMsgQButtonUp == NULL)
{
RETAILMSG(RFID_DEBUG, (TEXT("+CreateButtonHandlerThread: CreateMsgQueue SCNAPI32MSGQKEYUP Error!\r\n")));
        goto cleanUp; 
}

g_hButtonThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PrvTriggerButtonHandler_thread, NULL, 0, NULL);
if (g_hButtonThread == NULL)
{
RETAILMSG(RFID_DEBUG, (TEXT("+CreateButtonHandlerThread: CreateThread PrvTriggerButtonHandler_thread Error!\r\n")));
goto cleanUp; 
}
bSuccess = TRUE;


cleanUp:
    RETAILMSG(RFID_DEBUG, (TEXT("-CreateButtonHandlerThread  -\n")));
    return bSuccess;


}




static BOOL IsTagMaskEx(TYPE_TAG_EX *pTagType)
{
int j;
int startbyte;
int bytelen;


startbyte = g_tagMaskEx.nBitStartOffset / 8;
bytelen = MIN(pTagType->dataLength-startbyte, g_tagMaskEx.nBitLen/8);
if (bytelen<0)
bytelen = 0;


for (j=0; j<bytelen; j++)
if (pTagType->tagID[startbyte] & g_tagMaskEx.cTagMask[j])
{
break;
}
if (!bytelen || j<bytelen)
return TRUE;
else
return FALSE;
}




void PrvGetDeviceConfig(DEVICE_CFG* p_DevCfg)
{
HANDLE      handle;
BOOL ret;
handle = CreateFile (TEXT("CFG1:"), GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if (handle)
{
ret = DeviceIoControl(handle, CFG_IOCTL_Get_DEVCFG, NULL, 0, p_DevCfg, sizeof(DEVICE_CFG), NULL, NULL);
CloseHandle(handle);
}
}






//------------------------------------------------------------------------------
// EXPORTED FUNCTIONS
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_DllMain
//
//  DESCRIPTION:    DllMain function
//
//------------------------------------------------------------------------------
BOOL WINAPI RFID_DllMain(HINSTANCE hDllHandle, DWORD  dwReason, LPVOID lpreserved)
{
    BOOL bRc = TRUE;
lpreserved = NULL;
    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
          DEBUGREGISTER(hDllHandle);
       DisableThreadLibraryCalls((HMODULE) hDllHandle);
        break;


        case DLL_PROCESS_DETACH:
DEBUGMSG(ZONE_INIT,(_T("*** DLL_PROCESS_DETACH - Current Process: 0x%x, ID: 0x%x ***\r\n"),
                GetCurrentProcess(), GetCurrentProcessId()));
        break;


        default:
        break;
    }
    return bRc;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_Close
//
//  DESCRIPTION:    The RFID_Close function denitializes the RFID API, 
// resetting the HANDLE pointer on RFID_SUCCESS.
//
//------------------------------------------------------------------------------
DWORD RFID_API RFID_Close(HANDLE *phReader)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_Close\r\n"))); 


if (*phReader != NULL)
*phReader = NULL;


//Decrement the handle number
g_dwRFIDHandleNum--;

//Force the Button Thread Exit
g_bTriggerButtonThread = FALSE;

//Close All Openned Message Queue
if (g_hMsgQButtonDown != NULL)
CloseMsgQueue(g_hMsgQButtonDown);

if (g_hMsgQButtonUp != NULL)
CloseMsgQueue(g_hMsgQButtonUp);




RETAILMSG(RFID_DEBUG, (TEXT("-RFID_Close\r\n"))); 

return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_CloseReader
//
//  DESCRIPTION:    The RFID_CloseReader, CloseReader function closes the RFID
// readerˇs communication port.
//
//------------------------------------------------------------------------------
DWORD RFID_API RFID_CloseReader(HANDLE hReader)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_CloseReader\r\n"))); 


//Check if the library is open properly or not yet?
if (hReader == NULL) 
return RFID_INVALID_HANDLE;


g_bTerminateThread = TRUE;
g_bOpenReader = FALSE;
Sleep(100);


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_CloseReader\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_EnableTriggerCallback
//
//  DESCRIPTION:    The EnableTrigger functions allow the application to receive
// indication of a trigger pull, or release. Two types of
// EnableTrigger functions are provided. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_EnableTriggerCallback(HANDLE *phTrigger, HANDLE hReader,
                   BOOL bFlag, void (*pTriggerCallbackParam)(DWORD dwTriggerStatus),
                   DWORD dwTriggerMask)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_EnableTriggerCallback\r\n"))); 

if (hReader == NULL) 
return RFID_INVALID_HANDLE;


//This is uncertain. May need to modify later
*phTrigger = hReader;

g_bTriggerEnable = bFlag;
g_lpTriggerCallbackFunc = pTriggerCallbackParam;
g_dwTriggerMask = dwTriggerMask;

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_EnableTriggerCallback\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_EnableTriggerMessage
//
//  DESCRIPTION:    The EnableTrigger functions allow the application to receive
// indication of a trigger pull, or release. Two types of
// EnableTrigger functions are provided. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_EnableTriggerMessage(HANDLE *phTrigger, HANDLE hReader,
                    BOOL bFlag, HWND hDlg, unsigned int nTriggerMessage,
                    DWORD dwTriggerMask)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_EnableTriggerMessage\r\n"))); 
if (hReader == NULL) 
return RFID_INVALID_HANDLE;

//STILL NEED TO IMPLEMENT
*phTrigger = hReader;

g_bTriggerEnable = bFlag;
    
g_dwTriggerMask = dwTriggerMask;
//by yxchen 0627
PostMessage(hDlg, nTriggerMessage, (WPARAM)0, (LPARAM)0);

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_EnableTriggerMessage\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_EraseTag
//
//  DESCRIPTION:    The RFID_EraseTag, EraseTag function erases a programmed tag
// resetting its content to 0s.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_EraseTag(HANDLE hReader, unsigned char cTagVerifyCount, unsigned char cEraseAttempts)
{
UINT16 i;
unsigned char status;
unsigned char ucTempbuf[20];
unsigned char data1[1024];
  unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

RETAILMSG(RFID_DEBUG, (TEXT("+RFID_EraseTag\r\n"))); 
if (hReader == NULL) 
return RFID_INVALID_HANDLE;

status = PcdRequest(PICC_REQALL, ucTempbuf); 
if( status != MI_OK )

RETAILMSG(RFID_DEBUG, (_T(" PcdRequest  failed!\r\n")));  
return RFID_PORT_READ_ERROR;
}

status =  PcdAnticoll(ucTempbuf);
    if(status)
return RFID_PORT_READ_ERROR;

status = PcdSelect(ucTempbuf); 
    if(status)
return RFID_PORT_READ_ERROR;
 
    status = Authentication(0x60, DefaultKey, ucTempbuf, 1024);
if(status)
return RFID_PORT_READ_ERROR;

for (i=0; i<1024; i++)
data1[i] = 0;

for (i=0; i<cEraseAttempts; i++)
{
   status = PcdWrite(1024, data1);
if(status)
return RFID_PORT_WRITE_ERROR;
}

for (i=0; i<cTagVerifyCount; i++)
{
status =PcdRead(1024, ucTempbuf);
if(status)
return RFID_PORT_READ_ERROR;
else
   {
for (i=0; i<1024; i++)
{
if (ucTempbuf[i] != 0)
{
return RFID_PORT_READ_ERROR;
}
}
   }
}


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_EraseTag\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_FindClose
//
//  DESCRIPTION:    The RFID_FindClose, FindClose function closes an RFID find 
// session on specified find handle.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_FindClose(HANDLE hReader, HANDLE hFind)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_Find\r\n"))); 
if (hReader == NULL) 
return RFID_INVALID_HANDLE;

if (hFind != NULL)
hFind = NULL;


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_Find\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_FindFirst
//
//  DESCRIPTION:    An application must open an RFID device via the 
// RFID_OpenReader, OpenReader function call. A call to the 
// RFID_FindFirst, FindFirst function locates the first, and 
// currently only, RFID device within a given system.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_FindFirst(HANDLE hReader, RFID_FINDINFO *ptRFIDFindInfo, LPHANDLE phFind)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_FindFirst\r\n"))); 
if (hReader == NULL) 
return RFID_INVALID_HANDLE;


ptRFIDFindInfo->nPortNumber = 1;
ptRFIDFindInfo->nPortIndex = 0;
ptRFIDFindInfo->dwMagic = 1;
_tcscpy(ptRFIDFindInfo->tszDeviceName, _T("I2C1"));


*phFind = (HANDLE)1;

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_FindFirst\r\n")));

return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_FindNext
//
//  DESCRIPTION:    The RFID_FindNext, FindNext function finds the next available 
// RFID reader within the system. Currently, only one RFID module 
// can be in a single system.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_FindNext(HANDLE hReader, RFID_FINDINFO *lptRFIDFindInfo, HANDLE hFind)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_FindNext\r\n"))); 


if (hReader == NULL || hFind == NULL) return RFID_INVALID_HANDLE;


lptRFIDFindInfo->nPortNumber= 0;
lptRFIDFindInfo->nPortIndex = 0;
lptRFIDFindInfo->dwMagic = 0;
_tcscpy(lptRFIDFindInfo->tszDeviceName, _T(""));


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_FindNext\r\n")));

return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_FlashFirmware
//
//  DESCRIPTION:    The RFID_FlashFirmware, FlashFirmware function starts the 
// flash download process using the provided file name. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_FlashFirmware(HANDLE hReader, TCHAR *szFlashFileNameParam)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_FlashFirmware\r\n"))); 


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_FlashFirmware\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_FlashFirmwareStop
//
//  DESCRIPTION:    The RFID_FlashFirmwareStop, FlashFirmwareStop function 
// aborts flash firmware upgrade.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_FlashFirmwareStop(HANDLE hReader)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_FlashFirmwareStop\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_FlashFirmwareStop\r\n"))); 
return E_AUD_NOTSUPPORTED;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetCapCurrValue
//
//  DESCRIPTION:    The RFID_GetCurrValue, GetCapCurrValue functions return the 
// current value(s) of the capability into the supplied buffer 
// on RFID_SUCCESS. 
//
//------------------------------------------------------------------------------
DWORD RFID_GetCapCurrValue(HANDLE hReader, DWORD dwCapId, DWORD *pdwNumItems, DWORD dwValueBufSize, LPVOID pvValueBuf)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetCapCurrValue\r\n"))); 


switch (dwCapId)
{
case RFID_TAGCAP_SUPPORTED_TYPES:
if (dwValueBufSize <= RFID_TAG_TYPE_MAX)
memcpy(pvValueBuf, g_Caps.bSupportedTagTypes, dwValueBufSize);
else
memcpy(pvValueBuf, g_Caps.bSupportedTagTypes, RFID_TAG_TYPE_MAX);
break;
case RFID_TAGCAP_ENABLED_TYPES:
if (dwValueBufSize <= RFID_TAG_TYPE_MAX)
memcpy(pvValueBuf, g_Caps.bEnabledTagTypes, dwValueBufSize);
else
memcpy(pvValueBuf, g_Caps.bEnabledTagTypes, RFID_TAG_TYPE_MAX);
break;


case RFID_TAGCAP_G2_REMOVE_SELECT_RECORD_INDEX:
*(BYTE*)pvValueBuf = g_ucRemoveSelRecIdx;
break;


default:
return RFID_CAPNOTSUPPORTED;
}


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetCapCurrValue\r\n")));


return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetCapDfltValue
//
//  DESCRIPTION:    The RFID_GetCapDfltValue, GetCapDfltValue functions return 
// default value(s) of capability into supplied buffer on 
// RFID_SUCCESS. 
//
//------------------------------------------------------------------------------
DWORD RFID_GetCapDfltValue(HANDLE hReader, DWORD dwCapId, DWORD *pdwNumItems, DWORD dwValueBufSize, LPVOID pvValueBuf)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetCapDfltValue\r\n"))); 


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetCapDfltValue\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetCapInfo
//
//  DESCRIPTION:    The RFID_GetCapInfo, GetCapInfo functions fill the CAPINFO 
// structure with information regarding dwCapId.
//
//------------------------------------------------------------------------------
DWORD RFID_GetCapInfo(HANDLE hReader, DWORD dwCapId, CAPINFO *pCapInfo)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetCapInfo\r\n"))); 


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetCapInfo\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetCapList
//
//  DESCRIPTION:    The RFID_GetCapList, GetCapList functions return a list of 
// Capability IDs as DWORDs. If *pNumCaps is 0, the function 
// changes it to the number of capability Ids. This allows caller 
// to reserve the proper number of DWORDs for the call.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetCapList(HANDLE hReader, DWORD *pdwCapId, DWORD *pNumCaps)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetCapList\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetCapList\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetCaps
//
//  DESCRIPTION:    The RFID_GetCaps, GetCaps function fills in the RFID_CAPS 
// structure with RFID module capabilities, and other information.
// Some of the information includes:
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetCaps(HANDLE hReader, RFID_CAPS *pCaps)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetCaps\r\n"))); 
memcpy(pCaps, &g_Caps, sizeof(g_Caps));
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetCaps\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetCommandStatusText
//
//  DESCRIPTION:    The RFID_GetCommandStatusText, GetCommandStatusText function
// returns RFID API status codes as English text strings.
//
//------------------------------------------------------------------------------
const TCHAR * WINAPI RFID_GetCommandStatusText(HANDLE hReader, int stat)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetCommandStatusText\r\n"))); 


switch (stat)
{
case RFID_SUCCESS:
return _T("Function succeeded.");
case RFID_PORT_OPEN_ERROR:
return _T("Port not open error.");
case RFID_CRC_ERROR:
return _T("Invalid parameter given.");
case RFID_PARAM_ERROR:
return _T("CRC receive error.");
case RFID_CANNOT_ALLOC_MEM:
return _T("Cannot allocate memory.");
case RFID_PORT_WRITE_ERROR:
return _T("Error writing to port.");
case RFID_PORT_READ_ERROR:
return _T("Error reading from port.");
case RFID_UNKNOWN_ERROR:
return _T("Unable to determine error.");
case RFID_MAX_TAGS_EXCEEDED:
return _T("Tag list is full, tag cannot be stored.");
case RFID_COMMAND_TIMEOUT:
return _T("Command timed out, no response from RFID module.");
case RFID_UPLOADOK:
return _T("Upload of firmware packet OK (Used internal to API).");
case RFID_UPLOADEND:
return _T("Upload of firmware done.");
case RFID_ENGINE_BUSY:
return _T("Cannot perform function, module or port is already busy.");
case RFID_PORT_NOT_OPEN:
return _T("Port not open.");
case RFID_UNKNOWN_COMM_TYPE:
return _T("Unknown error code returned from RFID module.");
case RFID_BUFFER_TO_SMALL:
return _T("Assigned memory buffer to small.");
case RFID_FIND_COMPLETE:
return _T("Attempt to find RFID device is complete");
case RFID_INVALID_HANDLE:
return _T("Invalid device handle provided.");
case RFID_NO_DEVICE_POWER:
return _T("Function cannot proceed without RFID_SetDevicePower TRUE.");
case RFID_INVALID_FLASHFILE:
return _T("Flash file provided is invalid.");
case RFID_CAPNOTSUPPORTED:
return _T("The specified capability is not supported.");
case RFID_CAPREADONLY:
return _T("The specified capability is read only.");
case RFID_TAG_DETECTED:
return _T("An RFID tag was detected.");
case RFID_TAGTYPE_DISABLED:
return _T("The tag type specified is disabled.");
case RFID_TAGTYPE_NOSUPPORT:
return _T("The tag type specified is not supported.");
case RFID_FILTER_EXISTS:
return _T("Tag filter already exists, either by name, or filter byte match.");
case RFID_FILTER_DOES_NOT_EXISTS:
return _T("Tag filter does not exist.");
case RFID_CMD_UNKLEN:
return _T("nvalid RFID module command parameter length.");
case RFID_CMD_UNKVAL:
return _T("Invalid RFID module command value used.");
case RFID_CMD_UNKCMD:
return _T("Invalid RFID module command used.");
case RFID_CMD_UNKTAGCMD:
return _T("Invalid RFID module tag command used.");
case RFID_CMD_OVERERR:
return _T("Data overflow error during last command.");
case RFID_CMD_NOTAG:
return _T("No Tag found, command cannot proceed.");
case RFID_CMD_ERASEFAIL:
return _T("Failure to erase tag.");
case RFID_CMD_PROGFAIL:
return _T("Failure to program tag.");
case RFID_CMD_TAGLOCK:
return _T("Tag locked, command cannot proceed.");
case RFID_CMD_KILLFAIL:
return _T("Failed to kill tag.");
case RFID_CMD_LOCKFAIL:
return _T("Failed to lock tag.");
case RFID_CMD_DATASIZE:
return _T("Command data size error.");
case RFID_CMD_HWERR:
return _T("Hardware error, failure to find valid antenna.");
case RFID_CMD_LISTFULL:
return _T("RFID module’s internal tag list is full");
case RFID_CMD_UPLOADERR:
return _T("Failure to upload firmware.");
case RFID_CMD_UPLOADINVALID:
return _T("Invalid firmware upload buffer.");
case RFID_CMD_UPLOADCRC:
return _T("CRC error in firmware upload buffer.");
default :
return _T("Not Supported .");
}

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetCommandStatusText\r\n"))); 
return NULL;//E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetPowerState
//
//  DESCRIPTION:    The RFID_GetCommandStatusText, GetCommandStatusText function
// returns RFID API status codes as English text strings.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetPowerState(HANDLE hReader, BOOL *pbPowerState)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetPowerState\r\n"))); 
*pbPowerState = g_Caps.bPowerState;
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetPowerState\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetRFAttenuation
//
//  DESCRIPTION:    The RFID_GetRFAttenuation, GetRFAttenuation function returns
// the attenuation level for reading/writing tags in 10'ths of 
// a DB. The higher the number, the lower the RF power level. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetRFAttenuation(HANDLE hReader, unsigned char *cRFAttenuationDB)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetRFAttenuation\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetRFAttenuation\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetStats
//
//  DESCRIPTION:    The RFID_GetStats, GetStats function fills in the RFID_STATS
// structure with RFID module statistics. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetStats(HANDLE hReader, RFID_STATS *pStats)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetStats\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetStats\r\n"))); 
return E_AUD_NOTSUPPORTED;
}


DWORD Prv_GetTagID(TYPE_TAG *pTag, DWORD Option)
{
unsigned char status;
unsigned char ucTempbuf[RFID_MAX_TAG_ID_LENGTH_EX];
int total_length=0;
UINT8 iMaxTAGIDLength = 0;
Rc522Init();
status = PcdRequest(PICC_REQALL, ucTempbuf); 
if( status != MI_OK )

RETAILMSG(RFID_DEBUG, (_T(" PcdRequest  failed!\r\n")));  
goto Err;
}
else 
{   
Sleep(100);

//Need to clear buffer or in some case, 
//Previous data will still in the same buffer.
memset(ucTempbuf, 0, RFID_MAX_TAG_ID_LENGTH_EX);


status=AnticollSelect(0, ucTempbuf);
if( status != MI_OK )

RETAILMSG(RFID_DEBUG, (_T(" AnticollSelect  failed!\r\n")));
goto Err;
}
else 
{
int k,i,j;
UINT8 iMastStart, iMaskFinish;
BOOL bMaskCheck = TRUE;
RETAILMSG(RFID_DEBUG, (_T(" AnticollSelect  OK!\r\n")));

if (Option == 0) //RFID_GetTagID
iMaxTAGIDLength = RFID_MAX_TAG_ID_LENGTH;
else
iMaxTAGIDLength = RFID_MAX_TAG_ID_LENGTH_EX;

//by yxchen
for(k=0; k<iMaxTAGIDLength; k++)
{
if((ucTempbuf[k]) != 0x0)
{
total_length++;
}
else           
break;
}


//by yxchen 
//Mask Check is un-sured as no reference from Symbol device.
//May need to re-implemented.
//According to document, first 16 bits of tag are reserved.
//So currently we will ignore
iMastStart = g_tagMask.cBitStartOffset - 16;
if (iMastStart < 0)
iMastStart = 0;

iMaskFinish = g_tagMask.cBitLen / 4;

for (i=iMastStart; i<iMaskFinish; i++)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetTagMask g_tagMask->cTagMask %x %i\r\n"), g_tagMask.cTagMask[i], i));
if (g_tagMask.cTagMask[i] == 0)
{
continue;
}
else
{
if (g_tagMask.cTagMask[i] != ucTempbuf[i])
{
RETAILMSG(RFID_DEBUG, (_T("Mask Not OK!!! %x\r\n"), i));
bMaskCheck = FALSE;
break;
}
}
}

if (bMaskCheck)
{
for(j= 0; j < total_length; j++)
{
RETAILMSG(RFID_DEBUG, (_T("\n   AnticollSelect(ucTempbuf) ==  +++0x%x++++!\r\n"), ucTempbuf[j]));
pTag->tagID[j] = ucTempbuf[j];
pTag->dataLength = total_length;
pTag->antennaNum = RFID_ANTENNA_NUM;
GetLocalTime(&(pTag->lastSeen));
pTag->dwType = RFID_TAG_TYPE_EPC_CLASS0;
pTag->status = RFID_GETTAG_OK; 
}
}
else
{
goto Err;
}
}
}




return RFID_SUCCESS;
Err:
HaltA();
pTag->dataLength = 0;
pTag->status = RFID_GETTAG_NOTAG; 
RETAILMSG(RFID_DEBUG, (_T("\n  FAIL pTag->status ==  +++0x%x++++!\r\n"),pTag->status));

return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetTagID
//
//  DESCRIPTION:    The RFID_GetTagID, GetTagID function attempts to read a 
// single RFID tag into pTag. The tag is also added to the
// current TAG_LIST maintained internally by the RFID API.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetTagID(HANDLE hReader, TYPE_TAG *pTag)
{
DWORD Ret;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetTagID\r\n"))); 
Ret = Prv_GetTagID(pTag, 0);
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetTagID\r\n")));
return Ret;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetTagIDEX
//
//  DESCRIPTION:    The RFID_GetTagID, GetTagID function attempts to read a 
// single RFID tag into pTag. The tag is also added to the
// current TAG_LIST maintained internally by the RFID API.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetTagIDEX(HANDLE hReader, TYPE_TAG_EX *pTag)
{
DWORD Ret;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetTagIDEX\r\n"))); 
Ret = Prv_GetTagID(pTag, 1);
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetTagIDEX\r\n")));
return Ret;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetTagMask
//
//  DESCRIPTION:    The RFID_GetTagMask, GetTagMask function gets the current 
// TagMask used during RFID tag reading functions. The mask
// limits the list of tags that can be read based on the current
// mask.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetTagMask(HANDLE hReader, TAG_MASK *pTagMask)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetTagMask\r\n"))); 
memcpy(pTagMask, &g_tagMask, sizeof(TAG_MASK));
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetTagMask\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_GetTagMaskEX
//
//  DESCRIPTION:    The RFID_GetTagMask, GetTagMask function gets the current 
// TagMask used during RFID tag reading functions. The mask 
// limits the list of tags that can be read based on the current
// mask.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_GetTagMaskEX(HANDLE hReader, TAG_MASK_EX *pTagMask)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_GetTagMaskEx\r\n"))); 


memcpy(pTagMask, &g_tagMaskEx, sizeof(TAG_MASK_EX));


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_GetTagMaskEx\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_KillTag
//
//  DESCRIPTION:    The RFID_KillTag, KillTag function kills Tag, making it 
// programmable again. This command should be used to reset
// a locked tag. It is like unlocking and erasing a tag.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_KillTag(HANDLE hReader, unsigned char *pTagID,
                             unsigned char cTagLength, unsigned char cTagVerifyCount,
                             unsigned char cKillAttempts, unsigned char cTagKillCode)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_KillTag\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_KillTag\r\n"))); 
return RFID_PORT_WRITE_ERROR;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_LockTag
//
//  DESCRIPTION:    The RFID_LockTag, LockTag function locks a Tag so it cannot 
// be changed. Once locked, tag must be killed in order to
// reprogram tag.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_LockTag(HANDLE hReader, unsigned char cTagLength,
                          unsigned char cTagVerifyCount, unsigned char cLockAttempts,
                          unsigned char cTagKillCode)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_LockTag\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_LockTag\r\n"))); 
return RFID_PORT_WRITE_ERROR;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_Open
//
//  DESCRIPTION:    For those utilizing the RFID 'C' API interface, the 
// RFID_Open function is the first function that must be called. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_Open(HANDLE *phReader)
{
    RETAILMSG(RFID_DEBUG, (TEXT("+RFID_Open\r\n")));
   
    //Use Device Config to check if this is a RFID device
PrvGetDeviceConfig(&g_Devcfg);
if (g_Devcfg.RFIDType != RFID_I2C)
return RFID_CMD_HWERR;  
       
if (g_dwRFIDHandleNum > RFID_MAXHANDLE)
{
RETAILMSG(RFID_DEBUG, (TEXT("RFID already opened!\r\n"))); 
return RFID_PORT_OPEN_ERROR;
}

g_bTriggerEnable = FALSE;
g_bTerminateThread = FALSE;
g_bOpenReader = FALSE;
g_ucRemoveSelRecIdx = 0;


ZeroMemory(&g_tagMask, sizeof(g_tagMask));
ZeroMemory(&g_tagMaskEx, sizeof(g_tagMaskEx));
ZeroMemory(&g_tagList, sizeof(g_tagList));


ZeroMemory(&g_Caps, sizeof(g_Caps));
g_Caps.dwCurrentBaud = 115200;
g_Caps.dwRFIDDeviceCount = 1;
_tcscpy(g_Caps.szAPIVersionString, _T("1.01"));
_tcscpy(g_Caps.szFirmwareVersion, _T("1.00"));


*phReader = (HANDLE) g_dwRFIDHandleNum;
g_dwRFIDHandleNum++;

g_bTriggerButtonThread = TRUE;

//Initilize the RFID Module 
Rc522Init();

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_Open g_dwRFIDHandleNum: %x\r\n"), g_dwRFIDHandleNum));

return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_OpenReader
//
//  DESCRIPTION:    The RFID_OpenReader, OpenReader function opens the RFID 
// communications port, and initializes the RFID module. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_OpenReader(HANDLE hReader, int port)
{
HANDLE m_hI2C; //by  yxchen
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_OpenReader\r\n"))); 


//Check if the library is open properly or not yet?
if (hReader == NULL) 
return RFID_INVALID_HANDLE;


g_bOpenReader = TRUE;

// Power It Up
RFID_SetPowerState(hReader, TRUE);


// //Initilize the RFID Module 
// Rc522Init();

// Power It Down
RFID_SetPowerState(hReader, FALSE);


//Initilize the Trigger Button Threading
if (CreateButtonHandlerThread() == FALSE)
return RFID_CANNOT_ALLOC_MEM;


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_OpenReader\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_ProgramTags
//
//  DESCRIPTION:    The RFID_ProgramTags, ProgramTags function programs a tag 
// with a new TagID. Tag must not be locked.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_ProgramTags(HANDLE hReader, unsigned char *pTagID,
                             unsigned char cTagLength, unsigned char cTagVerifyCount,
                             unsigned char cEraseAttempts,unsigned char cProgramAttempts)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_ProgramTags\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_ProgramTags\r\n"))); 
return RFID_PORT_WRITE_ERROR;
}




//PrvReadTagInventory
//Helper function to read TAG ID via I2C Interface
DWORD PrvReadTagInventory(HANDLE hReader, TAG_LIST *pTagList, BOOL bClearInventory)
{
DWORD dwTotalTags;
int i,k;

RETAILMSG(RFID_DEBUG, (TEXT("+RFID_ReadTagInventory pTagList->dwTotalTags %x\r\n"), pTagList->dwTotalTags));
dwTotalTags = pTagList->dwTotalTags;

if (bClearInventory == TRUE)
{
pTagList->dwNewTags = 0;
pTagList->dwTotalTags = 0;
}
if((RFID_GetTagID(hReader, &(pTagList->Tags[dwTotalTags])) == RFID_SUCCESS))
{
k=0;
for(i=dwTotalTags-1;  i>=0; i--)
{    
if(  (pTagList->Tags[dwTotalTags].tagID[0] != pTagList->Tags[i].tagID[0]) 
|| (pTagList->Tags[dwTotalTags].tagID[1] != pTagList->Tags[i].tagID[1])
|| (pTagList->Tags[dwTotalTags].tagID[2] != pTagList->Tags[i].tagID[2])
|| (pTagList->Tags[dwTotalTags].tagID[3] != pTagList->Tags[i].tagID[3]))
{
++k;
}
else
{
pTagList->Tags[i].readCount++;
break;
}
}
if(k == dwTotalTags)
{
//This is a new TAG
pTagList->Tags[dwTotalTags].readCount = 1;
pTagList->dwNewTags = 1;
pTagList->dwTotalTags += 1; 

}
else
{
//This is an existing TAG
pTagList->dwNewTags=0;
}
}
else 

//No TAG Found
pTagList->dwNewTags=0;
}

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_ReadTagInventory\r\n")));
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_ReadTagInventory
//
//  DESCRIPTION:    The RFID_ReadTagInventory, ReadTagInventory function supports
// reading multiple tags utilizing the ReadTagInventory command.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_ReadTagInventory(HANDLE hReader, TAG_LIST *pTagList, BOOL bClearInventory)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_ReadTagInventory pTagList->dwTotalTags %x\r\n"), pTagList->dwTotalTags));


PrvReadTagInventory(hReader, pTagList, bClearInventory);


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_ReadTagInventory\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_ReadTagInventoryEX
//
//  DESCRIPTION:    The RFID_ReadTagInventory, ReadTagInventory function supports
// reading multiple tags utilizing the ReadTagInventory command.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_ReadTagInventoryEX(HANDLE hReader, TAG_LIST_EX *pTagList, BOOL bClearInventory)
{


int i,k; 


RETAILMSG(RFID_DEBUG, (TEXT("+RFID_ReadTagInventoryEX pTagList->dwTotalTags\r\n"), pTagList->dwTotalTags)); 

PrvReadTagInventory(hReader, (TAG_LIST *) pTagList, bClearInventory);

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_ReadTagInventoryEX\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_ReadTagInventoryScroll
//
//  DESCRIPTION:    The RFID_ReadTagInventory, ReadTagInventory function supports
// reading multiple tags utilizing the ReadTagInventory command. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_ReadTagInventoryScroll(HANDLE hReader, TAG_LIST *pTagList, BOOL bClearScrollTagInventory)
{
DWORD dwRet;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_ReadTagInventoryScroll\r\n"))); 


PrvReadTagInventory(hReader, pTagList, bClearScrollTagInventory);


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_ReadTagInventoryScroll\r\n")));

return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_RemoveTagMaskEX
//
//  DESCRIPTION:    The RFID_RemoveTagMaskEX function removes the TagMask (select
// record) used during RFID tag reading functions. The mask
// limits the list of tags that can be read based on the current
// mask. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_RemoveTagMaskEX(HANDLE hReader)
{
DWORD dwIdx = 0;
DWORD dwTotalTagsDeleted = 0;


RETAILMSG(RFID_DEBUG, (TEXT("+RFID_RemoveTagMaskEX\r\n"))); 


dwIdx = 0;
dwTotalTagsDeleted = 0;
while (dwIdx<g_tagListEx.dwTotalTags)
{
if (g_ucRemoveSelRecIdx==0)
break;


if (IsTagMaskEx(&g_tagListEx.Tags[dwIdx]))
{
memset(&g_tagListEx.Tags[dwIdx], 0, sizeof(g_tagListEx.Tags[0]));
dwTotalTagsDeleted ++;
if (g_ucRemoveSelRecIdx < 0xFF)
g_ucRemoveSelRecIdx --;
}
dwIdx ++;
}
g_tagListEx.dwTotalTags -= dwTotalTagsDeleted;
g_tagListEx.dwNewTags -=  dwTotalTagsDeleted;


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_RemoveTagMaskEX\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetCapCurrValue
//
//  DESCRIPTION:    The RFID_SetCapCurrValue, SetCapCurrValue functions set the
// current value(s) of the supplied capability ID to pvValueBuf
//
//------------------------------------------------------------------------------
DWORD RFID_SetCapCurrValue(HANDLE hReader, DWORD dwCapId, DWORD *pdwNumItems, DWORD dwValueBufSize, LPVOID pvValueBuf)
{
DWORD dwActSize;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetCapCurrValue\r\n"))); 


switch (dwCapId)
{
case RFID_DEVCAP_IP_PORT :
//dwPort = *pvValueBuf;
break;
case RFID_DEVCAP_IP_NAME :
//memcpy(szIPAddress, pvValueBuf, dwValueBufSize);
break;
case RFID_DEVCAP_ANTENNA_SEQUENCE:
//memcpy(cAntennaSequence, pvValueBuf, dwValueBufSize);
dwActSize = MIN(dwValueBufSize, RFID_ANTENNA_SEQUENCE_LENGTH);
memcpy(g_Caps.AntennaSequence, pvValueBuf, dwActSize);
break;
case RFID_READCAP_RF_ATTENUATION:
//cReadAttenuation
break;
case RFID_WRITECAP_RF_ATTENUATION:
//cWriteAttenuation
break;


case RFID_TAGCAP_SUPPORTED_TYPES:
dwActSize = MIN(dwValueBufSize, RFID_TAG_TYPE_MAX);
memcpy(g_Caps.bSupportedTagTypes, pvValueBuf, dwActSize);
break;
case RFID_TAGCAP_ENABLED_TYPES:
dwActSize = MIN(dwValueBufSize, RFID_TAG_TYPE_MAX);
memcpy(g_Caps.bEnabledTagTypes, pvValueBuf, dwActSize);
break;


case RFID_READCAP_READMODE:
g_dwReadMode = *(BYTE*)pvValueBuf;
break;


case RFID_TAGCAP_G2_REMOVE_SELECT_RECORD_INDEX:
g_ucRemoveSelRecIdx = *(BYTE*)pvValueBuf;
break;


case RFID_WRITECAP_G2_SELECT_TARGET:
//ch
break;
case RFID_WRITECAP_G2_SELECT_ACTION:
//ch
break;


case RFID_READCAP_G2_SESSION:
//ch
break;
case RFID_READCAP_G2_SEL:
//ch
break;
case RFID_READCAP_G2_TARGET:
//ch
break;
case RFID_READCAP_OUTLOOP:
//g_Caps.
break;
case RFID_READCAP_INLOOP:
break;


default:
return RFID_CAPNOTSUPPORTED;
}


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetCapCurrValue\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetCapDflts
//
//  DESCRIPTION:    The RFID_SetCapDflts, SetCapDflts functions set all 
// capability values to their default values. 
//
//------------------------------------------------------------------------------
DWORD RFID_SetCapDflts(HANDLE hReader)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetCapDflts\r\n"))); 


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetCapDflts\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetCapDfltValue
//
//  DESCRIPTION:    The RFID_SetCapDfltValue, SetCapDfltValue functions set the
// specified capability to its default value. 
//
//------------------------------------------------------------------------------
DWORD RFID_SetCapDfltValue(HANDLE hReader, DWORD dwCapId)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetCapDfltValue\r\n"))); 


switch (dwCapId)
{
case RFID_INFCAP_SUPPORTEDCAPS:
break;
case RFID_READCAP_RF_ATTENUATION:
break;
case RFID_READCAP_EVENTNAME:
break;
case RFID_READCAP_EVENTTAGPTR:
break;

case RFID_WRITECAP_RF_ATTENUATION:
break;
case RFID_WRITECAP_TAGTYPE:
break;

case RFID_TAGCAP_LOCKCODE:
break;
case RFID_TAGCAP_SUPPORTED_TYPES:
break;
case RFID_TAGCAP_ENABLED_TYPES:
break;


case RFID_DEVCAP_IP_NAME:
break;
case RFID_DEVCAP_IP_PORT:
break;
case RFID_TAGCAP_C0_SINGULATION_FIELD:
break;


case RFID_READCAP_EVENT_ALLTAGS:
break;


case RFID_READCAP_METHOD:
break;
case RFID_READCAP_OUTLOOP:
break;
case RFID_READCAP_INLOOP:
break;


case RFID_DEVCAP_ANTENNA_SEQUENCE:
break;


case RFID_READCAP_READMODE:
break;


case RFID_WRITECAP_ANTENNA:
break;


case RFID_WRITECAP_C0_PAGE:
break;
case RFID_READCAP_C0_PAGE:
break;


case RFID_READCAP_EVENTTAGPTR_EX:
break;
case RFID_READCAP_DUTYCYCLE_ONTIME:
break;
case RFID_READCAP_DUTYCYCLE_OFFTIME_PCNT:
break;


// Gen 2 inventory
case RFID_READCAP_G2_SEL:
break;
case RFID_READCAP_G2_SESSION:
break;
case RFID_READCAP_G2_TARGET:
break;
case RFID_READCAP_G2_START_Q:
break;


// Gen 2 read tag's memory bank (20h)
//#define RFID_TAGCAP_G2_SELECT_MEM_BANK 0x001C
case RFID_TAGCAP_G2_RF_ON_TIME:
break;
case RFID_TAGCAP_G2_MEM_BANK:
break;
case RFID_TAGCAP_G2_WORD_POINTER:
break;


case RFID_WRITECAP_G2_WRITE_OPTIONS:
break;
case RFID_WRITECAP_G2_LOCK_OPTIONS:
break;




// Gen 2 lock tags' memory (23h)
case RFID_WRITECAP_G2_LOCK_MASK:
break;
case RFID_WRITECAP_G2_LOCK_ACTION:
break;


// Gen 2 kill tag (24h)
case RFID_TAGCAP_G2_ACCESS_PASSWORD:
break;
case RFID_TAGCAP_G2_KILL_PASSWORD:
break;


case RFID_DEVCAP_VALID_ANTENNA_LIST:
break;


case RFID_DEVCAP_ANTENNA_GROUP:
break;


case RFID_WRITECAP_G2_SELECT_ACTION:
break;
case RFID_WRITECAP_G2_SELECT_TARGET:
break;
//#define RFID_WRITECAP_G2_SELECT_MEM_BANK 0x0029
case RFID_TAGCAP_G2_REMOVE_SELECT_RECORD_INDEX:
break;
 
case RFID_READCAP_G2_MEM_BANK_WORD_LEN:
break;
}


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetCapDfltValue\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetPowerState
//
//  DESCRIPTION:    The RFID_SetPowerState, SetPowerState function sets the power
// state of the RFID reader. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_SetPowerState(HANDLE hReader, BOOL bPowerState)
{
   
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetPowerState\r\n"))); 


if (g_Caps.bPowerState == bPowerState)
return RFID_SUCCESS;


if (bPowerState)
{
RETAILMSG(RFID_DEBUG, (TEXT("Power Up\r\n"))); 
SetPowerDown(0);
g_Caps.bPowerState = bPowerState;
}
else
{
g_Caps.bPowerState = bPowerState;
RETAILMSG(RFID_DEBUG, (TEXT("Power Down\r\n")));
SetPowerDown(1);
Sleep(20); // delay 20 ms before close the port.
}
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetPowerState\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetStatusMessage
//
//  DESCRIPTION:    The RFID_SetStatusMessage, SetStatusMessage function instructs
// the API to send windows message during flash upgrade of the
// hardware. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_SetStatusMessage(HANDLE hReader, HWND hWnd, int nMessage)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetStatusMessage\r\n"))); 


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetStatusMessage\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetTagMask
//
//  DESCRIPTION:    The RFID_SetTagMask, SetTagMask function sets the current 
// TagMask used during RFID tag reading functions. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_SetTagMask(HANDLE hReader, TAG_MASK *pTagMask)
{
INT8 i = 0;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetTagMask\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetTagMask pTagMask->cBitLen %x \r\n"), pTagMask->cBitLen));
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetTagMask pTagMask->cBitStartOffset %x \r\n"), pTagMask->cBitStartOffset));
for (i=0; i<RFID_MAX_TAG_ID_LENGTH; i++)
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetTagMask pTagMask->cTagMask %x %i\r\n"), pTagMask->cTagMask[i], i));
memcpy(&g_tagMask, pTagMask, sizeof(TAG_MASK));
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetTagMask\r\n")));
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetTagMaskEX
//
//  DESCRIPTION:    The RFID_SetTagMask, SetTagMask function sets the current
// TagMask used during RFID tag reading functions. 
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_SetTagMaskEX(HANDLE hReader, TAG_MASK_EX *pTagMask)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetTagMaskEx\r\n"))); 

memcpy(&g_tagMaskEx, pTagMask, sizeof(*pTagMask));


RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetTagMaskEx\r\n"))); 
return RFID_SUCCESS;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_SetRFAttenuation
//
//  DESCRIPTION:    The RFID_SetRFAttenuation, SetRFAttenuation functions set
// both the read and write attenuation levels to same value in
// 10'ths of a DB.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_SetRFAttenuation(HANDLE hReader, unsigned char cRFAttenuationDB)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_SetRFAttenuation\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_SetRFAttenuation\r\n"))); 
return E_AUD_NOTSUPPORTED;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_TagFilterAdd
//
//  DESCRIPTION:    Adds a tag filter that will be used by the API to limit tag
// reads to those tags that match the provided byte sequence. 
//
//------------------------------------------------------------------------------
DWORD RFID_TagFilterAdd( HANDLE hReader,
          TCHAR *pFilterName, 
          DWORD dwBitsToMatch, 
          DWORD dwBitMatchOffset, 
          unsigned char *pMatchBits,
          DWORD dwOptionMask,
          void (*pMatchCallBack)(TYPE_TAG *pTag, TCHAR *pFilterName, 
          DWORD dwBitsToMatch, 
          DWORD dwBitMatchOffset, 
          const unsigned char *pMatchBits,
          DWORD dwOptionMark))
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_TagFilterAdd\r\n"))); 
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_TagFilterAdd\r\n"))); 
return E_AUD_NOTSUPPORTED;
}
// by  yxchen 110719
//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_Flush 
//
//  DESCRIPTION:    The RFID_Flush function cancels all of the reads submitted on 
// an open scanner handle. 
//
//  PARAMETERS:     hReader - Handle of an open scanner returned by RFID_Open.
//
//  RETURNS:        If the function succeeds, the return value is RFID_SUCCESS.
// If the function fails, the return value is E_AUD_NOTSUPPORTED 
// or E_SCN_NOTENABLED.
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_Flush(HANDLE hReader)
{
UINT i = 0;
UINT j = 0;
DWORD dwTotalTags;
RETAILMSG(RFID_DEBUG, (TEXT("RFID_Flush +\r\n")));
if (!hReader)
return E_AUD_NOTSUPPORTED;


for (i = 0; i < g_tagList.dwTotalTags; i++)
{
for(j=0;j < g_tagList.Tags[i].dataLength; j++)
{
g_tagList.Tags[i].tagID[j] = 0;
}
}
    g_tagList.dwTotalTags = 0;
return RFID_SUCCESS;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_MiFare_Read
//
//  DESCRIPTION:    This function reads a specified memory bank of tag(s).
//
// PARAMETER:
// auth_mode  specify key A or key B -- MIFARE_AUTHENT_A or MIFARE_AUTHENT_A
//     key        the buffer stored the key(6 bytes)
// snr        the buffer stored the selected card's UID
// addr       the block address of a sector
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_MiFare_Read(HANDLE hReader, UINT8 blockAddress, UINT8 *pData, UINT8 byteCount) 
{
UINT8 Ret = RFID_SUCCESS;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_MiFare_Read blockAddress %x, byteCount %x\r\n"), blockAddress, byteCount)); 
if (PcdRead(blockAddress, pData) != rSTATUS_SUCCESS)
{
HaltA();
Ret = RFID_PORT_READ_ERROR;
}

RETAILMSG(RFID_DEBUG, (TEXT("pData %x %x %x %x\r\n"), pData[0], pData[1], pData[2], pData[3])); 


    RETAILMSG(RFID_DEBUG, (TEXT("-RFID_MiFare_Read\r\n"))); 
return Ret;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_MiFare_Write
//
//  DESCRIPTION:    This function writes to a specified memory bank of tag(s).
//
// PARAMETER:
// auth_mode  specify key A or key B -- MIFARE_AUTHENT_A or MIFARE_AUTHENT_A
//     key        the buffer stored the key(6 bytes)
// snr        the buffer stored the selected card's UID
// addr       the block address of a sector
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_MiFare_Write(HANDLE hReader, UINT8 blockAddress, UINT8 *pData, UINT8 byteCount) 
{
UINT8 Ret = RFID_SUCCESS;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_MiFare_Write\r\n")));
if (PcdWrite(blockAddress, pData) != rSTATUS_SUCCESS)
{
HaltA();
Ret = RFID_PORT_WRITE_ERROR;
}
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_MiFare_Write\r\n"))); 
return Ret;
}








//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_MiFare_Authentication
//
//  DESCRIPTION:    Authentication the password for a sector of mifare card
//
// PARAMETER:
// auth_mode  specify key A or key B -- MIFARE_AUTHENT_A or MIFARE_AUTHENT_A
//     key        the buffer stored the key(6 bytes)
// snr        the buffer stored the selected card's UID
// addr       the block address of a sector
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_MiFare_Authentication(HANDLE hReader, AUTH_MODE authMode, UINT8 *pKey, UINT8 *pUID, UINT8 blockAddress)
{
UINT8 Ret = RFID_PORT_WRITE_ERROR;


    UINT8  status;
    UINT8  AuthKey;
int j;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_MiFare_Authentication\r\n")));


if (PcdRequest(PICC_REQALL, pUID) == rSTATUS_SUCCESS)
{
if (PcdAnticoll(pUID) == rSTATUS_SUCCESS)
{
if (PcdSelect(pUID) == rSTATUS_SUCCESS)
{
if (authMode == KEY_A)
AuthKey = 0x60;
else
AuthKey = 0x61;

if (Authentication(AuthKey, pKey, pUID, blockAddress) == rSTATUS_SUCCESS)
{
Ret = RFID_SUCCESS;
}
}
}
}

if (Ret != RFID_SUCCESS)
HaltA();

RETAILMSG(RFID_DEBUG, (TEXT("-RFID_MiFare_Authentication\r\n")));


return   Ret;
}


//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_RawAuthentication
//
//  DESCRIPTION:    Authentication the password for a sector of mifare card
//
// PARAMETER:
// auth_mode  specify key A or key B -- MIFARE_AUTHENT_A or MIFARE_AUTHENT_A
//     key        the buffer stored the key(6 bytes)
// snr        the buffer stored the selected card's UID
// addr       the block address of a sector
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_RawAuthentication(HANDLE hReader, unsigned char auth_mode, unsigned char *key, unsigned char *snr, unsigned char addr)
{
    short  status;
int j;
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_RawAuthentication\r\n")));


    status = PcdRequest(PICC_REQALL, snr); 
    if(status)
      goto end;
    status =  PcdAnticoll(snr);
    if(status)
  goto end;
else
    {
          RETAILMSG(1, (_T(" PcdAnticoll  OK!\r\n")));
     for(j=0;j<4;j++)
 {
      RETAILMSG(RFID_DEBUG, (_T("\n   PcdAnticoll(snr) ==  +++0x%x++++!\r\n"),snr[j]));
 }
    }  
status = PcdSelect(snr); 
    if(status)
  goto end;
   
status = Authentication(auth_mode, key, snr, addr);
if(status)
goto  end;
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_RawAuthentication\r\n"))); 


return MI_OK;
end:
HaltA();
return   MI_ERR;
}
//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_RawRead
//
//  DESCRIPTION:    Read the RFID data
//
// PARAMETER:
// addr the address of the block
//     pData the buffer to save the 16 bytes data
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_RawRead(HANDLE hReader, unsigned char cAddr, unsigned char *pData)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_RawRead\r\n"))); 
PcdRead(cAddr,pData);
    RETAILMSG(RFID_DEBUG, (TEXT("-RFID_RawRead\r\n"))); 


return MI_OK;
end:
HaltA();
return   MI_ERR;
}




//------------------------------------------------------------------------------
//
//  FUNCTION:       RFID_RawWrite
//
//  DESCRIPTION:    Read the RFID data
//
// PARAMETER:
// addr       the address of the block
// pData      the data to write
//
//------------------------------------------------------------------------------
DWORD WINAPI RFID_RawWrite(HANDLE hReader, unsigned char addr,unsigned char *pData)
{
RETAILMSG(RFID_DEBUG, (TEXT("+RFID_RawWrite\r\n")));
    PcdWrite(addr, pData);
RETAILMSG(RFID_DEBUG, (TEXT("-RFID_RawWrite\r\n"))); 

return MI_OK;
end:
HaltA();
return   MI_ERR;
}


//#endif
阅读更多
想对作者说点什么? 我来说一句

RFID RC522 stm32f103c8t6 读写demo

2016年04月06日 281KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭