📄 protocol14443ahandler.cpp
字号:
/** @file
* Protocol14443AHandler.cpp
*
* INTEL CONFIDENTIAL
* Copyright 2006 Intel Corporation All Rights Reserved.
* The source code contained or described herein and all documents related
* to the source code ("Material") are owned by Intel Corporation or its
* suppliers or licensors. Title to the Material remains with Intel Corporation
* or its suppliers and licensors. The Material contains trade secrets and
* proprietary and confidential information of Intel or its suppliers and licensors.
* The Material is protected by worldwide copyright and trade secret laws and
* treaty provisions. No part of the Material may be used, copied, reproduced,
* modified, published, uploaded, posted, transmitted, distributed, or disclosed
* in any way without Intel抯 prior express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be express
* and approved by Intel in writing.
*
*/
#include "StdAfx.h"
#include "Protocol14443AHandler.h"
using namespace std;
const string ANTICOLLISIONCMD = "A001"; // Anti-collision command
#define CMD_LEN 1024 //Max lenght of command string
#define MAX_UID 32 //Max number of UIDs
#define UID_LEN 68 //Max lenght of an UID
Protocol14443AHandler::~Protocol14443AHandler(void)
{
}
/*This function removes the BCC (block character check) to get the UID.
*/
void Protocol14443AHandler::I14AuidRemoveBCC(char *pDest, char *pSrc)
{
char *p, *d;
size_t i, l = strlen(pSrc);
p = pSrc;
d = pDest;
for (i = 0; i < 6; i++) *d++ = *p++;
if (l < 11)
{
*d++ = *p++;
*d++ = *p++;
*d = 0;
return;
}
p += 2;
for (i = 0; i < 6; i++) *d++ = *p++;
if (l < 19)
{
*d++ = *p++;
*d++ = *p++;
*d = 0;
return;
}
p += 2;
for (i = 0; i < 8; i++) *d++ = *p++;
*d = 0;
}
// Find the continuous blob of hexadecimal characters
// input ppStr
// output pWord
bool Protocol14443AHandler::getXWord (char **ppStr, char *pWord)
{
char *p = *ppStr, *d;
bool good = true;
for (d = pWord; *p && *p != ',' && *p != ']'; )
{
if (! isxdigit(*p))
{
good = false;
break;
}
*d++ = *p++;
}
*d = 0;
*ppStr = p;
return good;
}
/* This function parse the response string of Anti-Collision command to get the list of UIDs.
@return status code
@retval #vector<string> List of UIDs.
*/
RF_Status Protocol14443AHandler::GetUIDList(vector<string> &UidList)
{
RF_Status Status = RF_Success;
char cRet[CMD_LEN];
DWORD dwSize = CMD_LEN;
if (!mpCom->PackageAndWrite(ANTICOLLISIONCMD.c_str()))
{
Status = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
Status = RF_ReadFailure;
}
if ( (RF_WriteFailure == Status) || (RF_ReadFailure == Status) )
{
Status = RF_DeviceUnResponsive;
return Status;
}
// a valid response from device should contain '['
char *q = strchr(cRet, '[');
if (!q)
{
// not found valid response; try again
dwSize = CMD_LEN;
Sleep(10);
if (!mpCom->PackageAndWrite(ANTICOLLISIONCMD.c_str()))
{
Status = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
Status = RF_ReadFailure;
}
if ( (RF_WriteFailure == Status) || (RF_ReadFailure == Status) )
{
Status = RF_DeviceUnResponsive;
return Status;
}
}
// check error code
char *p = strstr(cRet, "[x");
if (p)
{
Status = RF_UidNotFound;
return Status; //empty list
}
p = cRet;
char UIDs[MAX_UID][UID_LEN];
size_t cnt = 0;
for (size_t i = 0; i < 4*MAX_UID; i++)
{
// find next starting position
p = strchr(p, '[');
if (!p)
break;
p++;
if (*p && (*p == ']' || *p == 'z' || *p == 'Z'))
continue;
// Get UID from current starting position
char uid[34], uid2[34];
if (!getXWord(&p, uid))
continue;
size_t uidlen = strlen(uid);
if (! (uidlen == 10 || uidlen == 18 || uidlen == 26))
{
//"Invalid UID length"
continue;
}
I14AuidRemoveBCC(uid2, uid);
strcpy(UIDs[cnt], uid2);
cnt++;
if (cnt >= MAX_UID)
break;
}
if (cnt > 0)
{
for (size_t i = 0; i < cnt; i++)
{
string TagUid(UIDs[i]);
UidList.push_back(TagUid);
}
}
else
{
Status = RF_UidNotFound;
}
return Status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -