📄 mifare.c
字号:
status = CascAnticoll(casc_code,
length,
tmpSnr + i * 4);
if(status == STATUS_SUCCESS)
{
/* select 1st cascade level uid */
status = Select(casc_code, tmpSnr + i * 4, &sak);
/* check if further cascade level is used */
if(status == STATUS_SUCCESS)
{
/* increase number of received bits in parameter */
bcnt = (unsigned char)(SINGLE_UID_LENGTH * (i + 1)); //the actually length of the UID, you can return it.
/* check if cascade bit is set */
if(!(sak & CASCADE_BIT))
{
break;
}
}
}
else
{
break;
}
}
switch(i)
{
case 1: memcpy(snr+3, tmpSnr+4,4); //copy UID to snr buffer without CT(0x88)
break;
case 2: memcpy(snr+6, tmpSnr+4,4);
break;
default: memcpy(snr, tmpSnr,4);
break;
}
return status;
}
/*************************************************
Function: HaltA
Description:
halt the current selected card
Parameter:
NONE
Return:
short status of implement
**************************************************/
short HaltA(void)
{
short status = STATUS_SUCCESS;
/* initialise data buffer */
SerBuffer[0] = HALTA_CMD;
SerBuffer[1] = HALTA_PARAM;
ResetInfo(MInfo);
MInfo.nBytesToSend = HALTA_CMD_LENGTH;
SetTimeOut(200);
status = M522PcdCmd(JCMD_TRANSCEIVE, SerBuffer, &MInfo);
if(status == STATUS_IO_TIMEOUT)
status = STATUS_SUCCESS;
return status;
}
/*************************************************
Function: 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
Return:
short status of implement
**************************************************/
short Authentication(unsigned char auth_mode,
unsigned char *key,
unsigned char *snr,
unsigned char addr)
{
short status;
unsigned char i = 0, RegVal;
ResetInfo(MInfo);
SerBuffer[0] = auth_mode; //key A or key B
SerBuffer[1] = addr; //address to authentication
memcpy(SerBuffer+2,key,6); //6 bytes key
memcpy(SerBuffer+8,snr,4); //4 bytes UID
MInfo.nBytesToSend = 12; //length
SetTimeOut(2000);
status = M522PcdCmd(JCMD_AUTHENT, SerBuffer, &MInfo);
if(status == STATUS_SUCCESS)
{
RegVal = RcGetReg(JREG_STATUS2);
if((RegVal & 0x0f) != 0x08)
status = STATUS_AUTHENT_ERROR;
}
return status;
}
/*************************************************
Function: Read
Description:
read 16 bytes data from a block
Parameter:
addr the address of the block
_data the buffer to save the 16 bytes data
Return:
short status of implement
**************************************************/
short Read(unsigned char addr, unsigned char *_data)
{
short status = STATUS_SUCCESS;
char tmp = 0;
ResetInfo(MInfo);
SerBuffer[0] = MIFARE_READ;
SerBuffer[1] = addr;
MInfo.nBytesToSend = 2;
SetTimeOut(5000);
status = M522PcdCmd(JCMD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status != STATUS_SUCCESS)
{
if (status != STATUS_IO_TIMEOUT ) // no timeout occured
{
if (MInfo.nBitsReceived == 4)
{
SerBuffer[0] &= 0x0f;
if ((SerBuffer[0] & 0x0a) == 0)
{
status = STATUS_AUTHENT_ERROR;
}
else
{
status = STATUS_INVALID_FORMAT;
}
}
}
memset(_data,0,16);
}
else // Response Processing
{
if (MInfo.nBytesReceived != 16)
{
status = STATUS_ACCESS_DENIED;
memset(_data,0,16);
}
else
{
memcpy(_data,SerBuffer,16);
}
}
return status;
}
/*************************************************
Function: Write
Description:
write 16 bytes data to a block
Parameter:
addr the address of the block
_data the data to write
Return:
short status of implement
**************************************************/
short Write( unsigned char addr, unsigned char *_data)
{
short status = STATUS_SUCCESS;
ResetInfo(MInfo);
SerBuffer[0] = MIFARE_WRITE;
SerBuffer[1] = addr;
MInfo.nBytesToSend = 2;
SetTimeOut(10000);
status = M522PcdCmd(JCMD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status != STATUS_IO_TIMEOUT)
{
if (MInfo.nBitsReceived != 4)
{
status = STATUS_BITCOUNT_ERROR;
}
else
{
SerBuffer[0] &= 0x0f;
if ((SerBuffer[0] & 0x0a) == 0)
{
status = STATUS_AUTHENT_ERROR;
}
else
{
if (SerBuffer[0] == 0x0a)
{
status = STATUS_SUCCESS;
}
else
{
status = STATUS_INVALID_FORMAT;
}
}
}
}
if ( status == STATUS_SUCCESS)
{
SetTimeOut(5000);
ResetInfo(MInfo);
memcpy(SerBuffer,_data,16);
MInfo.nBytesToSend = 16;
status = M522PcdCmd(JCMD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status & 0x80)
{
status = STATUS_IO_TIMEOUT;
}
else
{
if (MInfo.nBitsReceived != 4)
{
status = STATUS_BITCOUNT_ERROR;
}
else
{
SerBuffer[0] &= 0x0f;
if ((SerBuffer[0] & 0x0a) == 0)
{
status = STATUS_ACCESS_DENIED;
}
else
{
if (SerBuffer[0] == 0x0a)
{
status = STATUS_SUCCESS;
}
else
{
status = STATUS_INVALID_FORMAT;
}
}
}
}
}
return status;
}
/*************************************************
Function: ValueOper
Description:
block value operation function, increment or decrement the block value
and transfer to a block
Parameter:
OperMode MIFARE_INCREMENT or MIFARE_DECREMENT
addr the address of the block
value the value to be increment or decrement
trans_addr the address to save the resulet of increment or decrement
Return:
short status of implement
**************************************************/
short ValueOper(unsigned char OperMode,
unsigned char addr,
unsigned char *value,
unsigned char trans_addr)
{
short status = STATUS_SUCCESS;
ResetInfo(MInfo);
SerBuffer[0] = OperMode;
SerBuffer[1] = addr;
MInfo.nBytesToSend = 2;
SetTimeOut(5000);
status = M522PcdCmd(JCMD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status != STATUS_IO_TIMEOUT)
{
if (MInfo.nBitsReceived != 4)
{
status = STATUS_BITCOUNT_ERROR;
}
else
{
SerBuffer[0] &= 0x0f;
switch(SerBuffer[0])
{
case 0x00:
status = STATUS_AUTHENT_ERROR;
break;
case 0x0a:
status = STATUS_SUCCESS;
break;
case 0x01:
status = STATUS_INVALID_FORMAT;
break;
default:
status = STATUS_OTHER_ERROR;
break;
}
}
}
if ( status == STATUS_SUCCESS)
{
SetTimeOut(10000);
ResetInfo(MInfo);
memcpy(SerBuffer,value,4);
MInfo.nBytesToSend = 4;
status = M522PcdCmd(JCMD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status == STATUS_IO_TIMEOUT||(status == MIFARE_DECREMENT && OperMode == MIFARE_DECREMENT))
{
status = STATUS_SUCCESS;
}
else
{
status = STATUS_OTHER_ERROR;
}
}
if ( status == STATUS_SUCCESS)
{
ResetInfo(MInfo);
SerBuffer[0] = MIFARE_TRANSFER;
SerBuffer[1] = trans_addr;
MInfo.nBytesToSend = 2;
status = M522PcdCmd(JCMD_TRANSCEIVE,
SerBuffer,
&MInfo);
if (status & MIFARE_ACK_MASK)
{
status = STATUS_SUCCESS;
}
else
{
status = STATUS_OTHER_ERROR;
}
}
return status;
}
/*************************************************
Function: InitBlock
Description:
initialize a block value
Parameter:
addr the address of the block
value the value to be initialized, 4 bytes buffer
Return:
short status of implement
**************************************************/
short InitBlock(unsigned char addr,unsigned char *value)
{
unsigned char tmp[16],i;
short status = STATUS_SUCCESS;
for(i=0;i<4;i++)
{
tmp[i]=value[i];
tmp[i+4]=255-value[i];
tmp[i+8]=value[i];
}
tmp[12]=addr;
tmp[13]=255-addr;
tmp[14]=tmp[12];
tmp[15]=tmp[13];
status=Write(addr,tmp);
return status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -