1,cpu card的触点。共有8个触点,according to ISO7816 standard,排列如下:
top view的时候,c1-c4 on the left,c5-c8 on the right。
Contact Designation Use
C1 Vcc Power connection through which operating power is supplied to the microprocessor chip in the card
C2 RST Reset line to initiate its reset sequence of instructions
C3 CLK Clock signal line clock signal can be provided to the microprocessor chip.
C4 RFU Reserved for future use
C5 GND Ground line providing common electrical ground between the IFD and the ICC
C6 Vpp Programming power connection used to program EEPROM of first generation ICCs.
C7 I/O Input/output line that provides a half-duplex communication channel
C8 RFU Reserved for future use
2,卡的复位和card 的ATR(answer to reset)复位应答。
卡的复位分为两种:冷reset和热reset。
冷reset:是在卡插入卡座后,发生的。终端(就是card reader)先是给card提供电压(vcc),然后在提供clock(时钟),同时reset keep low level,在clock提供后,card要保证在clock提供后的200个周期内将IO set high(接收模式),终端也一样。再后,将card的reset pin set to high level,保持一段时间(40'000 - 45'000个周期)。cpu card在终端将reset set to high后的400-4'000个周期内送出ATR(复位应答),如果card没有在规定时间内送出ATR,终端将启动释放过程。
热复位:在vcc和CLock已经提供的情况下,终端将reset set to low,后面和冷复位一样了:)。
触点释放过程:终端将reset set to low,启动释放时序。然后将clk和io set to low,在card拔出卡座前,vcc set to low(就是断电)。
3,一个字符帧(有效数据一个字节)的传输实现.
Start Parity Next
bit <----- 8 data bits -----> bit Start bit
Z ____ ________________________________......______ __
| | | | | | | | | | | | |
I/O | |ba|bb|bc|bd|be|bf|bg|bh|bi| Guardtime | |
|___|__|__|__|__|__|__|__|__|__| |___|_
A : : : :
0 t1 : t10
: :
:<---- (n+/-0.2) etu --->:
每个字符有一个start bit(low)和8个data bit,和一个parity bit(偶校验bit),所以一个有10个bit。偶校验规则:偶校验bit+8个data bit,共9个bit,里面的“1”one个数为偶。10bit后,还有一个guard time,用来提供多余的时间处理数据和如果偶校验失败,提供错误提示的使用的。如果parity check err,则在parity bit后的0。5ETU开始将IO set to low,keep for 1-2 ETU,发送方则在parity bit后的1ETU开始check IO line状态。如果是high,就认为是对的;如果是low就重发(T=0,必须要重发,最多3次,3次后,就释放卡)。
TS : Initial character
TO : Format character
TAi : Interface character [ codes FI,DI ]
TBi : Interface character [ codes II,PI1 ]
TCi : Interface character [ codes N ]
TDi : Interface character [ codes Yi+1, T ]
T1, ... , TK : Historical characters (max,15)
TCK : Check character
Figure 4 : General configuration of the Answer to Reset
Y1 : indicator for the presence of the interface characters
TA1 is transmitted when b5=1
TB1 is transmitted when b6=1
TC1 is transmitted when b7=1
TD1 is transmitted when b8=1
Yi+1 : indicator for the presence of the interface characters
TAi+1 is transmitted when b5=1
TBi+1 is transmitted when b6=1
TCi+1 is transmitted when b7=1
TDi+1 is transmitted when b8=1
(2):过程字节。
cpu card接收到命令头后,向终端发送一个过程字节。过程字节向终端指明了下一步该作什么。其编码和终端行为的对应关系如下:
Byte | Value | Result
-----+-------+------------------------------------------------------------
| INS | VPP is idle. All remaining data bytes are transferred
| | subsequently.
| |
| INS+1 | VPP is active. All remaining data bytes are transferred
| | subsequently.
ACK | ___ |
| INS | VPP is idle. Next data byte is transferred subsequently.
| _____ |
| INS+1 | VPP is active. Newt data byte is transferred subsequently.
-----+-------+------------------------------------------------------------
NULL | $60 | No futher action on VPP. The interface device waits for a
| | new procedure byte
-----+-------+------------------------------------------------------------
SW1 | SW1 | VPP is idle. The interface device waits for a SW2 byte
T=1时,命令的传输就不一样了。T=1是面向block的协议,T=0是面向字节的协议。
T=1时的block 结构:
----------------------------------------------------------------------------
HEAD FIELD | DATA FIELD |尾field |
----------------------------------------------------------------------------
NAD节点地址 |PCB协议控制字节|LEN长度|INF(APDU或控制信息)|EDC(错误校验码)|
----------------------------------------------------------------------------
※※链接,如果发送的数据超过IFSC和IFSD的大小,就要将其分成几个连续的I block。
I block的链接由PCB的b5定义:
b5=0,表示链接的最后一个block;b5=1,表示后面还有后续的block。
如果接收方收到的数据大于IFSD的大小,应发送一个R block,b0-b3的值为2;
链接的构造:
终端向card;
block (1)
---------------------------
CLA INS P1 P2 | LC| DATA DATA
-----------------------------------------------------
BLOCK(2)-BLOCK(N-1)
------------------------------------------------------
DATA DATA
------------------------------------------------------
BLOCK(N)
------------------------------------------------------
DATA DATA |LC|
------------------------------------------------------
card回复终端;
BLOCK(12)-BLOCK(N-1)
------------------------------------------------------
DATA DATA
------------------------------------------------------
BLOCK(N)
------------------------------------------------------
DATA DATA |SW1 SW2|
------------------------------------------------------
所以T=1时候,命令的执行是这样的:
Head field(nad+I pcb+length)+INF(CLA,INS,P1,P2,P3+CMND DATA)+尾域(EDC)。
card返回:
HEAD FIELD(NAD + I PCB+LENGTH) + INF (DATA + SW1 SW2)+尾域(EDC)。
发送完了命令头的5个命令字节后,开始接收card的返回数据,并判断做出响应处理;
// normal mode too,but a little different;
//如果收到的是命令的补码;
if(Response[0] == ~Cmnd[1])
{
while(2)
{
if (Response[0] == Cmnd[1]) //判断是命令码嘛?是的话,发送剩余的命令data;
{
for (i = count; i < CmndLength; i++)
{
if (SendChar(Cmnd) != 0)
{
return 1;
}
}
//接受card的返回数据;
for (i = 1; i < 35; i++)
{
if (RecChar(0x55) != 0)//返回不等于0,表示数据发送完毕;
{
RspLength = i;
//SendCharComm(0x28);
return 0;
}
}
}
//返回是ins的补码,发送下一个命令byte;
if(SendChar(Cmnd[count]) != 0)
{
//SendCharComm(0x88);
SendCharComm(count);
return 1;
}
else //发送命令字节成功;
{
count++; //指向下一个命令字节;
if(count == CmndLength) //发送完了嘛?
{
for (i = 1; i < 35; i++) //发送完了,就接收card返回数据;
{
back = 0;
back = RecChar(0x55);
if (back != 0)
{
RspLength = i;
SendCharComm(0x11);
break;
}
else
{
Response = RecByte;
}
}
return 0;
}
else //指令没有发送完毕;
{
back = RecChar(0x55); //接收card的返回字节;
if (back != 0)
{
return 1;
}
else
{
Response[0] = RecByte; //保存返回,回到while(2)循环去判断;
}
}
}
}//end of while(2);
/*
for (i = 5; i < CmndLength; i++)
{
if (SendChar(Cmnd) != 0)
{