at88153.c

来自「mifarea卡程序mifarea卡程序mifarea卡程序」· C语言 代码 · 共 181 行

C
181
字号
/****************************************
 * at88153.c                            *
 * functions for 88sc153 card           *
 * designed by liulixun                 *
 * last update: 04-23-2007              *
 ****************************************/

#include "includes.h"

#define POLL_ACK_TMO    100

// PAC addr
static unsigned char PACAddrs[4] = {0x30, 0x38, 0x34, 0x3c};

// start and polling ACK with command "cmd"
static bit At153Command(unsigned char cmd);

int At153ReadEE(unsigned char zone, unsigned char addr, unsigned char *buf, unsigned char len)
{
    unsigned char i;
    
    zone <<= 2;
    zone &= 0x0c;
    zone |= 0xB1;
    
    if(!At153Command(zone))
        return -1;

    if(!IccSyncPutCharWaitACK(addr & 0x3f))
        return -2;
    
    for(i=0; i<len-1; i++)
    {
        buf[i] = IccSyncGetCharSendACK(1);
    }
    buf[i] = IccSyncGetCharSendACK(0);
    
    IccSyncStop();
    
    return i+1;
}

int At153WriteEE(unsigned char zone, unsigned char addr, unsigned char *buf, unsigned char len)
{
    unsigned char i, i1;
    
    zone <<= 2;
    zone &= 0x0c;
    zone |= 0xB0;
    
    for(i1=0; i1<len; addr+=8)
    {
        if(!At153Command(zone))
            return -1;
        
        if(!IccSyncPutCharWaitACK(addr & 0x3f))
            return -2;
        
        for(i=0; (i<8) && (i1<len); i++, i1++)
        {
            if(!IccSyncPutCharWaitACK(buf[i1]))
                return -3;
        }
        
        IccSyncStop();
    }
        
    return i1;
}

int At153ReadFS(void)
{
    int i;
    
    if(!At153Command(0xbe))
        return -1;

    i = IccSyncGetCharSendACK(0);
    IccSyncStop();
    
    return i;
}

int At153WriteFS(unsigned char value)
{
    if(!At153Command(0xba))
        return -1;
    if(!IccSyncPutCharWaitACK(value & 0x07))
        return -2;
    IccSyncStop();
    
    return 0;
}

int At153VerifyPasswd(unsigned char rp, unsigned char *buf)
{
    unsigned char i, j, pac, zone;
    
    rp &= 0x03;
    zone = rp << 2;
    zone |= 0xb3;
    
    for(i=0; i<2; i++)
    {
        if(!At153Command(zone))
            return -1;

        for(j=0; j<3; j++)
        {
            if(!IccSyncPutCharWaitACK(buf[j]))
                return -2;
        }
        IccSyncStop();
        
        // polling ACK with command 0xbd to read PAC in cfg zone
        if(At153ReadEE(3, PACAddrs[rp], &pac, 1) < 0)
            return -3;
    }
    
    return pac;
}

int At153InitAuth(unsigned char *buf)
{
    unsigned char i;
    
    if(!At153Command(0xb2))
        return -1;
        
    for(i=0; i<8; i++)
    {
        if(!IccSyncPutCharWaitACK(buf[i]))
            return -2;
    }
    IccSyncStop();
    
    return 0;
}

int At153VerifyAuth(unsigned char *buf)
{
    unsigned char i, aac;
    
    if(!At153Command(0xb6))
        return -1;
        
    for(i=0; i<8; i++)
    {
        if(!IccSyncPutCharWaitACK(buf[i]))
            return -2;
    }
    IccSyncStop();
    
    // read AAC
    if(At153ReadEE(3, 0x20, &aac, 1) < 0)
        return -3;
    
    return aac;
}

// polling ACK with command "cmd" and send "cmd"
static bit At153Command(unsigned char cmd)
{
    unsigned int starttime;
    
    starttime = GetTickCount();
    while(1)
    {
        IccSyncStart();
        if(!IccSyncPutCharWaitACK(cmd))
        {
            IccSyncStop();
            if(GetTickCount() - starttime >= POLL_ACK_TMO)
                return 0;
        }
        else
        {
            return 1;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?