📄 xllp_usim_atr.c
字号:
/******************************************************************************
**
** COPYRIGHT (C) 2001, 2002 Intel Corporation.
**
** This software as well as the software described in it is furnished under
** license and may only be used or copied in accordance with the terms of the
** license. The information in this file is furnished for informational use
** only, is subject to change without notice, and should not be construed as
** a commitment by Intel Corporation. Intel Corporation assumes no
** responsibility or liability for any errors or inaccuracies that may appear
** in this document or any software that may be provided in association with
** this document.
** Except as permitted by such license, no part of this document may be
** reproduced, stored in a retrieval system, or transmitted in any form or by
** any means without the express written consent of Intel Corporation.
**
** FILENAME: xllp_usim_atr.c
**
** PURPOSE: contains functions that read configuration information
** received by the card after the answer to reset (ATR)
**
******************************************************************************/
#include "xllp_usim.h"
#include "xllp_usim_ATR.h"
#include "xllp_usim_registers.h"
#include "xllp_usim_os_depend.h"
#include "xllp_ost.h"
static volatile P_XLLP_OST_T TimerRegBaseP = (P_XLLP_OST_T)OST_REGISTER_BASE;
/******************************************************************************
Function Name: XllpUsimGetAtrBytes
Description: This function WAITS until the card responds with the ATR bytes.
it then reads those bytes and stores them in a structure supplied
to this function as an input paramter. See xllp_usim_ATR.h
It is assumed the card operates at a 9600 Hertz.
Global Registers:
Input Arguments:
XllpUsimBase: Base address of the USIM module
ATR: a pointer to an ATR structure.
Output Arguments:
ATR: A pointer to a modified ATR structure
Return Value:
XLLP_STATUS_T:
OK: ATR Bytes received ok.
XLLP_ATR_FAILURE: ATR was not received correctly.
*******************************************************************************/
XLLP_USIM_STATUS_T XllpUsimGetAtrBytes(void *XllpUsimBase, XLLP_USIM_ATR_T *ATR)
{
XLLP_UINT8_T BitMap = 0, TransProtocolEqualZero = 1, TCKEorTest;
XLLP_UINT8_T i = 0;
XLLP_UINT32_T ReceiveFifoBytes = 0;
XLLP_UINT8_T started = 0;
XLLP_USIM_T *USIM = (XLLP_USIM_T *) XllpUsimBase;
/**
* Given the fact that the processor can read data faster then the card can provide,
* the read function has to wait until the data is deposited into the receive fifo.
* The USIM card requires a maximum reponse time of 40000 card clock cycles, which
* translates to 40msec at the lowest clock frequency of 9600
*/
for(i=0; i < 40 && started == 0; i++)
{
ReceiveFifoBytes = (USIM->FSR & XLLP_USIM_FSR_RX_LENGTH) >> XLLP_USIM_FSR_RX_LENGTH_SHIFT;
if(ReceiveFifoBytes)
started = 1;
else
XllpOstDelayMilliSeconds(TimerRegBaseP, 1);
}
if(i == 40) // if maximum period elapsed, then card failed to respond
return XLLP_USIM_ATR_FAILURE;
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TS), 10) & XLLP_USIM_RBR_RB)
!= XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TO), 10) & XLLP_USIM_RBR_RB)
!= XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
//Store TO to EOR with following bytes for TCK byte test.
TCKEorTest = ATR->TO;
BitMap = ATR->TO & XLLP_USIM_ATR_TO_BitMap_Mask;
// Retrieve the historical bytes:
ATR->LengthOfHistoricalBytes = ATR->TO & XLLP_USIM_ATR_HistoricalByte;
/**
* Retrieve the Interface bytes
* Loop until a bitmap indicates there are no more interface bytes coming up next.
* To be consistent with ISO standard notation, byte TA1 will be stored in TA[1] not TA[0]
*/
for(i=1; BitMap && ATR->LengthOfInterfaceBytes <=
(ATR_Max_Interface_Bytes - ATR->LengthOfHistoricalBytes); i++)
{
if(BitMap & TA_bit_Mask)
{
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TA[i]), 10) & XLLP_USIM_RBR_RB)
!= XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
ATR->LengthOfTA++;
TCKEorTest ^= ATR->TA[i];
}
if(BitMap & TB_bit_Mask)
{
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TB[i]), 10) & XLLP_USIM_RBR_RB)
!= XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
ATR->LengthOfTB++;
TCKEorTest ^= ATR->TB[i];
}
if(BitMap & TC_bit_Mask)
{
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TC[i]), 10) & XLLP_USIM_RBR_RB)
!= XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
ATR->LengthOfTC++;
TCKEorTest ^= ATR->TC[i];
}
if(BitMap & TD_bit_Mask)
{
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TD[i]), 10) & XLLP_USIM_RBR_RB)
!= XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
ATR->LengthOfTD++;
TCKEorTest ^= ATR->TD[i];
}
BitMap = ATR->TD[i] & XLLP_USIM_ATR_TD_BitMap_Mask;
//Test to see if TCK byte will exit
//TCK will not exist if T=0 otherwise must retrive TCK byte
if (BitMap & 0xF)
TransProtocolEqualZero = 0;
// Store the correct number of interface bytes
ATR->LengthOfInterfaceBytes = ATR->LengthOfTA + ATR->LengthOfTB +
ATR->LengthOfTC + ATR->LengthOfTD;
}
for(i=0; i < ATR->LengthOfHistoricalBytes; ++i)
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->HistoricalBytes[i]), 10) &
XLLP_USIM_RBR_RB) != XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
if (!TransProtocolEqualZero)
{
if((XllpUsimReceiveCharacterWait(USIM, &(ATR->TCK), 10) &
XLLP_USIM_RBR_RB) != XLLP_USIM_OK)
return XLLP_USIM_ATR_FAILURE;
if (TCKEorTest ^ ATR->TCK)
return XLLP_USIM_ATR_FAILURE;
// Addeds total length of ATR with TCK (3: TS, T0, TCK)
ATR->LengthOfAtr = ATR->LengthOfInterfaceBytes + ATR->LengthOfHistoricalBytes + 3;
ATR->TCKPresent = 1;
}
else
{
// Addeds total length of ATR without TCK (2: TS, T0)
ATR->LengthOfAtr = ATR->LengthOfInterfaceBytes + ATR->LengthOfHistoricalBytes + 2;
ATR->TCKPresent = 0;
}
// Test to verify maximum ATR is less then 34 bytes.
if(ATR->LengthOfAtr > ATR_Max_Bytes)
return XLLP_USIM_ATR_FAILURE;
return XLLP_USIM_OK;
}
/******************************************************************************
Function Name: XllpUsimInitializeAtrStructure
Description: Initizlizes the ATR structure.
Global Registers:
Input Arguments:
ATR: a pointer to an ATR structure.
Output Arguments:
Return Value:
0: the ATR structure is correct
Non-Zero: The ATR structure is NOT correct
*******************************************************************************/
void XllpUsimInitializeAtrStructure(XLLP_USIM_ATR_T *ATR)
{
XLLP_UINT8_T *Atr_Ptr = (XLLP_UINT8_T *) ATR;
XLLP_UINT8_T Length = sizeof(*ATR);
XLLP_UINT8_T i=0;
for(i = 0; i < Length; ++i)
*(Atr_Ptr+i) = 0;
}
/******************************************************************************
Function Name: XllpUsimVerifyAtrCorrectness
Description: Uses the 'check character' of the ATR to verify that the ATR
was received correctly.
Global Registers:
Input Arguments:
ATR: a pointer to an ATR structure.
ExpectedAtr: a pointer to the expected ATR array
Output Arguments:
None
Return Value:
0: the ATR structure is correct
Non-Zero: The ATR structure is NOT correct
*******************************************************************************/
XLLP_USIM_STATUS_T XllpUsimVerifyAtrCorrectness(XLLP_USIM_ATR_T *ATR, XLLP_INT8_T *ExpectedAtr)
{
XLLP_UINT8_T i = 0, x = 0 ;
XLLP_UINT8_T LengthOf = ATR->LengthOfTA;
// Check the initial character
if (ATR->TS != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
// Check the format character
x += 1;
if (ATR->TO != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
// Check the interface characters
x += 1;
for(i = 1; i <= LengthOf; ++i)
{
if (ATR->TA[i] != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
x += 1;
}
LengthOf = ATR->LengthOfTB;
for(i = 1; i <= LengthOf; ++i)
{
if (ATR->TB[i] != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
x += 1;
}
LengthOf = ATR->LengthOfTC;
for(i = 1; i <= LengthOf; ++i)
{
if (ATR->TC[i] != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
x += 1;
}
LengthOf = ATR->LengthOfTD;
for(i = 1; i <= LengthOf; ++i)
{
if (ATR->TD[i] != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
x += 1;
}
// Check the historical characters
LengthOf = ATR->LengthOfHistoricalBytes;
for(i = 0; i < LengthOf; ++i)
{
if (ATR->HistoricalBytes[i] != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
x += 1;
}
// Test the check character if present (T!=0)
if (ATR->TCKPresent)
if (ATR->TCK != ExpectedAtr[x])
return XLLP_USIM_ATR_NOT_CORRECT;
return XLLP_USIM_OK ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -