📄 rc531.c
字号:
temp=Command_Send(4,buff,Transmit);
if (temp==FALSE)
return(FM222_DECRERR);
return(FM222_OK);
}
/********************************************************************************************************/
/*名称: MIF_Restore */
/*功能: 该函数实现MIFARE卡自动恢复,备份操作 */
/* */
/*输入: */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* FM222_NOTAGERR: 无卡 */
/* FM222_BYTECOUNTERR: 接收字节错误 */
/* FM222_NOTAUTHERR: 未经权威认证 */
/* FM222_EMPTY: 数据溢出错误 */
/* FM222_CRCERR: CRC校验错 */
/* FM222_PARITYERR: 奇偶校验错 */
/* FM222_RESTERR: 卡片恢复,备份操作失败 */
/* FM222_OK: 应答正确 */
/********************************************************************************************************/
uchar MIF_Restore(uchar Block_Adr)
{
uchar temp,i;
*buffer=RF_CMD_RESTORE;
*(buffer+1)=Block_Adr;
temp=Command_Send(2,buffer,Transceive);
if (temp==FALSE)
return(FM222_NOTAGERR);
temp=FIFO_Length;
if (temp==0)
return(FM222_BYTECOUNTERR);
Read_FIFO(buffer);
temp=*buffer;
switch(temp)
{
case 0x00 :
return(FM222_NOTAUTHERR);
case 0x04:
return(FM222_EMPTY);
case 0x0a:
break;
case 0x01:
return(FM222_CRCERR);
case 0x05:
return(FM222_PARITYERR);
default:
return(FM222_RESTERR);
}
for (i=0;i<4;i++)
buffer[i]=0x00;
temp=Command_Send(4,buffer,Transmit);
if (temp==FALSE)
return(FM222_RESTERR);
return(FM222_OK);
}
/********************************************************************************************************/
/*名称: MIF_Transfer */
/*功能: 该函数实现MIFARE卡电子钱包保存操作 */
/* */
/*输入: */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* FM222_NOTAGERR: 无卡 */
/* FM222_BYTECOUNTERR: 接收字节错误 */
/* FM222_NOTAUTHERR: 未经权威认证 */
/* FM222_EMPTY: 数据溢出错误 */
/* FM222_CRCERR: CRC校验错 */
/* FM222_PARITYERR: 奇偶校验错 */
/* FM222_TRANSERR: 卡片恢复,备份操作失败 */
/* FM222_OK: 应答正确 */
/********************************************************************************************************/
uchar MIF_Transfer(uchar Block_Adr)
{
uchar temp;
buffer[0]=RF_CMD_TRANSFER;
buffer[1]=Block_Adr;
temp=Command_Send(2,buffer,Transceive);
if (temp==FALSE)
return(FM222_NOTAGERR);
temp=FIFO_Length;
if (temp==0)
return(FM222_BYTECOUNTERR);
Read_FIFO(buffer);
temp=*buffer;
switch(temp)
{
case 0x00 :
return(FM222_NOTAUTHERR);
case 0x04:
return(FM222_EMPTY);
case 0x0a:
return(FM222_OK);
case 0x01:
return(FM222_CRCERR);
case 0x05:
return(FM222_PARITYERR);
default:
return(FM222_TRANSERR);
}
}
/********************************************************************************************************/
/*名称: HL_Initial */
/*功能: 该函数实现高级初始化命令 */
/* */
/*输入: */
/* buff: 待写入初始化数据的首地址 */
/* Secnr: 扇区号 */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* 操作状态码 */
/********************************************************************************************************/
uchar HL_Initial(uchar idata *buff,uchar Block_Adr,uchar Mode)
{
uchar temp;
Secnr=Block_Adr/4;
//Halt
MIF_Halt();
//Request
temp=Request(RF_CMD_REQUEST_ALL );
if(temp!=FM222_OK)
return(FM222_REQERR);
//AntiCol and Select
temp=AntiColl();
if(temp!=FM222_OK)
return(FM222_ANTICOLLERR);
temp=Select_Card();
if(temp!=FM222_OK)
return(FM222_SELERR);
//LoadKey
Load_keyE2_CPY(Secnr,Mode);
//Authentication
temp=Authentication(UID,Secnr,Mode);
if(temp!=FM222_OK)
return(FM222_AUTHERR);
//Initial
temp=MIF_Initival(buff,Block_Adr);
if(temp!=FM222_OK)
return(FM222_INTIVALERR);
//Halt
MIF_Halt();
return(FM222_OK);
}
/********************************************************************************************************/
/*名称: HL_Read */
/*功能: 该函数实现高级读命令 */
/* */
/*输入: */
/* Secnr: 扇区号 */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* 操作状态码 */
/* 读出数据存于buffer中 */
/********************************************************************************************************/
uchar HL_Read(uchar idata *buff,uchar Block_Adr,uchar Mode)
{
uchar temp;
Secnr=Block_Adr/4;
//Halt
MIF_Halt();
//Request
temp=Request(RF_CMD_REQUEST_STD);
if(temp!=FM222_OK)
return(FM222_REQERR);
//AntiCol and Select
temp=AntiColl();
if(temp!=FM222_OK)
return(FM222_ANTICOLLERR);
temp=Select_Card();
if(temp!=FM222_OK)
return(FM222_SELERR);
//LoadKey
Load_keyE2_CPY(Secnr,Mode);
//Authentication
temp=Authentication(UID,Secnr,Mode);
if(temp!=FM222_OK)
return(FM222_AUTHERR);
//Read
temp=MIF_READ(buff,Block_Adr);
//Halt
MIF_Halt();
if(temp!=FM222_OK)
return(FM222_READERR);
}
/********************************************************************************************************/
/*名称: HL_Write */
/*功能: 该函数实现高级写命令 */
/* */
/*输入: */
/* buff: 待写入数据的首地址 */
/* Secnr: 扇区号 */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* 操作状态码 */
/********************************************************************************************************/
uchar HL_Write(uchar idata *buff,uchar Block_Adr,uchar Mode)
{
uchar temp;
Secnr=Block_Adr/4;
//Halt
MIF_Halt();
//Request
temp=Request(RF_CMD_REQUEST_STD );
if(temp!=FM222_OK)
return(FM222_REQERR);
//AntiCol and Select
temp=AntiColl();
if(temp!=FM222_OK)
return(FM222_ANTICOLLERR);
temp=Select_Card();
if(temp!=FM222_OK)
return(FM222_SELERR);
//LoadKey
Load_keyE2_CPY(Secnr,Mode);
//Authentication
temp=Authentication(UID,Secnr,Mode);
if(temp!=FM222_OK)
return(FM222_AUTHERR);
//Write
temp=MIF_Write(buff,Block_Adr);
//Halt
MIF_Halt();
if(temp!=FM222_OK)
return(FM222_WRITEERR);
}
/********************************************************************************************************/
/*名称: HL_Increment */
/*功能: 该函数实现高级增款命令 */
/* */
/*输入: */
/* buff: 待写入增款值的首地址 */
/* Secnr: 扇区号 */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* 操作状态码 */
/********************************************************************************************************/
uchar HL_Increment(uchar idata *buff,uchar Block_Adr,uchar Mode)
{
uchar temp;
Secnr=Block_Adr/4;
//Halt
MIF_Halt();
//Request
temp=Request(RF_CMD_REQUEST_STD );
if(temp!=FM222_OK)
return(FM222_REQERR);
//AntiCol and Select
temp=AntiColl();
if(temp!=FM222_OK)
return(FM222_ANTICOLLERR);
temp=Select_Card();
if(temp!=FM222_OK)
return(FM222_SELERR);
//LoadKey
Load_keyE2_CPY(Secnr,Mode);
//Authentication
temp=Authentication(UID,Secnr,Mode);
if(temp!=FM222_OK)
return(FM222_AUTHERR);
//Increment
temp=MIF_Increment(buff,Block_Adr);
if(temp!=FM222_OK)
return(FM222_INCRERR);
//Transfer
temp=MIF_Transfer(Block_Adr);
//Halt
MIF_Halt();
if(temp!=FM222_OK)
return(FM222_TRANSERR);
}
/********************************************************************************************************/
/*名称: HL_Decrement */
/*功能: 该函数实现高级扣款命令 */
/* */
/*输入: */
/* buff: 待写入扣款值的首地址 */
/* Secnr: 扇区号 */
/* Block_Adr: 块地址 */
/* */
/*输出: */
/* 操作状态码 */
/********************************************************************************************************/
uchar HL_Decrement(uchar idata *buff,uchar Block_Adr,uchar Mode)
{
uchar temp;
Secnr=Block_Adr/4;
//Halt
MIF_Halt();
//Request
temp=Request(RF_CMD_REQUEST_STD );
if(temp!=FM222_OK)
return(FM222_REQERR);
//AntiCol and Select
temp=AntiColl();
if(temp!=FM222_OK)
return(FM222_ANTICOLLERR);
temp=Select_Card();
if(temp!=FM222_OK)
return(FM222_SELERR);
//LoadKey
Load_keyE2_CPY(Secnr,Mode);
//Authentication
temp=Authentication(UID,Secnr,Mode);
if(temp!=FM222_OK)
return(FM222_AUTHERR);
//decrement
temp=MIF_Decrement(buff,Block_Adr);
if(temp!=FM222_OK)
return(FM222_DECRERR);
//Transfer
temp=MIF_Transfer(Block_Adr);
//Halt
MIF_Halt();
if(temp!=FM222_OK)
return(FM222_TRANSERR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -