?? sim.c
字號:
SIM_dbg_write_trace((SYS_UWORD8 *)"AVCMD", 5);
SIM_dbg_write_trace(p->xbuf, len+5);
SIM_dbg_write_trace((SYS_UWORD8 *)"AVANS", 5);
SIM_dbg_write_trace(rcvSize, 1);
SIM_dbg_write_trace(result, *rcvSize);
SIM_dbg_tmp[0] = (SYS_WORD8)(res>>8);
SIM_dbg_tmp[1] = (SYS_WORD8)res;
SIM_dbg_write_trace(SIM_dbg_tmp, 2);
SIM_dbg_write_trace(SIM_dbg_null, SIM_DBG_NULL);
if (SIM_dbg_tdma_diff > SIM_dbg_max_interchardelay)
SIM_dbg_max_interchardelay = SIM_dbg_tdma_diff;
SIM_dbg_tmp[0] = (SYS_UWORD8)(SIM_dbg_tdma_diff >> 8);
SIM_dbg_tmp[1] = (SYS_UWORD8)(SIM_dbg_tdma_diff);
SIM_dbg_write_trace(SIM_dbg_tmp, 2);
#endif
return(res);
}
/*
* Use to detect end of characters reception
* input: p pointer on sim structure
* n number of extra character to send
*
* output: return 0 if sucess
* SIM_ERR_x in case of error
*
*/
SYS_UWORD16 SIM_WaitReception(SIM_PORT *p)
{
SYS_UWORD16 returncode;
//analyse the nature of the command to execute
if ((p->xbuf[1] == 0x12) || (p->xbuf[1] == 0xB2) || (p->xbuf[1] == 0xB0) || (p->xbuf[1] == 0xF2)
|| (p->xbuf[1] == 0xC0))
//FETCH, READ_RECORD, READ_BINARY, STATUS, GET_RESPONSE commands == receive command
{
if (p->xbuf[4] == 0) //if P3 == 0 when sending receive command
{
p->expected_data = 256;
}
else
{
p->expected_data = p->xbuf[4];
}
p->moderx = 1; //wait for procedure byte
if (returncode = SIM_Waitforchars (p, p->etu9600))
{
return returncode;
}
}
else //direct command : INVALIDATE, REHABILITATE, SLEEP
{
p->moderx = 5; //mode reception of SW1/SW2
if (returncode = SIM_Waitforchars (p, p->etu9600))
{
return returncode;
}
}
return (0);
}
/*
* Use to read file characteristics information
* input: p pointer on sim structure
*
* output: return 0 if sucess
* 1 in case of error
*
*/
SYS_UWORD8 SIM_GetFileCharacteristics(SIM_PORT *p)
{
int res;
SYS_UWORD8 ubuf[40];
SYS_UWORD8 sz;
res = SIM_Select(DF_GSM, ubuf, &sz);
if ((res & 0xFF00) != 0x9F00)
{
res = SIM_Select(DF_DCS1800, ubuf, &sz);
if ((res & 0xFF00) != 0x9F00)
{
return (1);
}
}
res = SIM_GetResponse( ubuf, res & 0x00FF , &sz);
if (res != 0x9000)
return (1);
p->FileC = ubuf[13];
return (0);
}
/*
* Use to determine value of b2 in file caracteristics contained in response
* of SELECT Master File command
* return 0 if no preferred speed during authentication
* 1 if 13/4Mhz mandatory
*
*
*/
void SIM_Interpret_FileCharacteristics(SIM_PORT *p)
{
//interpret b2 bit for operating authentication speed
if((p->conf1 & 0x0020) && (p->FileC & 0x02))
{
p->Freq_Algo = 1;
}
//interpret Clock stop behavior
// modified by J. Yp-Tcha to integrate all the behaviors required by ETSI.
// 18/11/2002 : TI Chip always allowed low level, high level is hard dependant
if ((p->FileC & SIM_CLK_STOP_MASK) == SIM_CLK_STOP_NOT_ALLWD) {
/* Sim Clock Stop Not Allowed */
SIM_sleep_status = SIM_SLEEP_NOT_ALLOWED;
/* There is not need to modifiy p->conf1 */
status_os_sim = NU_Delete_Timer (&SIM_timer);
}
else {
if ((p->FileC & SIM_CLK_STOP_MASK) == SIM_CLK_STOP_ALLWD) {
/* Sim Clock Stop Allowed, no prefered level */
/* Default value for TI Chip shall always be Low Level */
SIM_sleep_status = SIM_SLEEP_DESACT;
p->c->conf1 = p->conf1 &= ~SIM_CONF1_SCLKLEV;
}
else {
/* Clock Stop is allowed, the level shall be checked */
if ((p->FileC & SIM_CLK_STOP_HIGH) == SIM_CLK_STOP_HIGH) {
/* high level is mandatory */
/* OMEGA/NAUSICA can not handle sim stop clock at high level */
#ifndef ANALOG1
SIM_sleep_status = SIM_SLEEP_DESACT;
p->c->conf1 = p->conf1 |= SIM_CONF1_SCLKLEV;
#else
/* Sim Clock Stop Not Allowed because the interface do not support this level */
SIM_sleep_status = SIM_SLEEP_NOT_ALLOWED;
/* There is not need to modifiy p->conf1 */
status_os_sim = NU_Delete_Timer (&SIM_timer);
#endif
}
else {
/* by default, Low Level is allowed */
SIM_sleep_status = SIM_SLEEP_DESACT;
p->c->conf1 = p->conf1 &= ~SIM_CONF1_SCLKLEV;
}
}
}
if (SIM_sleep_status == SIM_SLEEP_NONE)
{
status_os_sim = NU_Delete_Timer (&SIM_timer);
}
}
/*
* Use to evaluate need of sending PTS procedure regarding
* the ATR. If default not used, PTS initiates F and D adequate values
* for speed enhancement.
* In case of 2 wrong PTS answer (speed enhanced), a third PTS with default value
* is used. If the third PTS attempt failed, the ME reset the SIM and use default
* value.
* Return Value : SIM_ERR_READ, SIM_ERRCARDREJECT, SIM_ERR_WAIT
*
*/
SYS_UWORD16 SIM_PTSprocedure(SIM_CARD *cP, SIM_PORT *p)
{
SYS_UWORD8 TA1;
SYS_UWORD8 n;
SYS_UWORD8 err;
p->xbuf[0] = 0xFF; //character of PTS proc to send
p->xbuf[1] = 0;
p->xbuf[2] = 0xFF;
p->xbuf[3] = 0x7B;
//TA1,TB1,TC1,TD1 present in ATR ?
n = 3;
p->PTS_Try++;
if (p->PTS_Try > 4)
{
return (SIM_ERR_CARDREJECT);
} // at the fourth attempt, PTS procedure is unusefull. Use default value.
//TA1 present? Test 27.11.2.6
else if ( p->PTS_Try == 4)
{
SIM_Calcetu (p);
return (0);
}
if(cP->AtrData[1] & 0x10)
{
TA1 = cP->AtrData[2];
}
else //if TA1 not present, return
{
SIM_Calcetu (p);
return (0);
}
if (TA1 == 0x94) //speed enhancement
{
SIM_Calcetu (p);
return (0); //temporary disabling of speed enhancement feature
if (p->PTS_Try <= 2)
{
n = 4;
p->xbuf[1] = 0x10;
p->xbuf[2] = TA1;
}
}
if (TA1 == 0x11)
{
SIM_Calcetu (p);
return (0);
} //if TA1 != 0x11 and 0x94, need to send PTS request
//transmit request of speed enhancement : PTS
SIM_WriteBuffer(p, 0, n);
p->moderx = 0; //mode of normal reception
p->expected_data = n;
if (err = SIM_Waitforchars (p, p->etu9600))
{
return (err);
}
//should received same chars as PTS request
if ((p->rbuf[0] != p->xbuf[0]) || (p->rbuf[1] != p->xbuf[1]) ||
(p->rbuf[2] != p->xbuf[2]))
{
return(SIM_ERR_READ);
}
if (n == 4)
{
if (p->rbuf[3] != p->xbuf[3])
{
return(SIM_ERR_READ);
}
//correct response from SIM : with speed enhanced
p->c->conf1 = p->conf1 |= SIM_CONF1_ETU; //set F=512 D=8
}
SIM_Calcetu (p);
return (0);
}
/*
* procedure of WARM reset consists on asserting
* reset signal at 0 during at least 400 ETU
* input p pointer of type SIM_PORT
*/
void SIM_WARMReset (SIM_PORT *p)
{
p->c->conf1 = p->conf1 &= ~SIM_CONF1_SRSTLEV;
ind_os_sleep (p->etu400); /// wait 400 ETU
p->c->conf1 = p->conf1 |= SIM_CONF1_SRSTLEV;
p->rx_index = 0;
}
/*
* procedure use to get out sleepMode
* input p pointer of type SIM_PORT
*/
void SIM_SleepMode_In (SYS_UWORD32 param)
{
if (SIM_sleep_status == SIM_SLEEP_DESACT)
{
(&(Sim[0]))->c->conf1 &= ~SIM_CONF1_SCLKEN; //disabled the clock for the SIM card
SIM_sleep_status = SIM_SLEEP_ACT;
}
status_os_sim = NU_Control_Timer (&SIM_timer, NU_DISABLE_TIMER);
}
/*
* procedure use to get out sleepMode
* input p pointer of type SIM_PORT
*/
void SIM_SleepMode_Out (SIM_PORT *p)
{
if (SIM_sleep_status == SIM_SLEEP_ACT)
{
p->c->conf1 = p->conf1 |= SIM_CONF1_SCLKEN;
// WCS patch for NU_Sleep(0) bug
if (p->startclock > 0)
ind_os_sleep (p->startclock);
// End WCS patch
SIM_sleep_status = SIM_SLEEP_DESACT;
}
}
/*
* procedure to parse ATR dynamically
* input p pointer of type SIM_PORT
* output return error code
* SIM_ERR_WAIT, p->errorSIM
* SIM_ERR_CARDREJECT,
*
*
*/
SYS_UWORD16 SIM_ATRdynamictreatement (SIM_PORT *p, SIM_CARD *cP)
{
volatile SYS_UWORD8 HistChar;
volatile SYS_UWORD8 InterfChar;
SYS_UWORD16 countT;
SYS_UWORD16 mask;
SYS_UWORD16 returncode;
SYS_UWORD8 i;
SYS_UWORD8 firstprotocol;
SYS_UWORD8 Tx,T;
SYS_UWORD8 TDi;
SYS_UWORD8 position_of_TC1;
SYS_UWORD8 another_protocol_present;
SYS_UWORD16 wait80000clk;
i = 0;
//wait for TS and T0
p->moderx = 0;
p->expected_data= 1;
firstprotocol = 0;
position_of_TC1 = 0;
another_protocol_present = 0;
wait80000clk = 6; // > 24 ms
//wait for first character TS of ATR sequence. It should arrive before 80000sclk
if (returncode = SIM_Waitforchars (p, wait80000clk))
{
return returncode;
}
//wait for T0
p->expected_data++;
if (returncode = SIM_Waitforchars (p, p->etu9600))
{
return returncode;
}
if (((p->rbuf[0] & 0xF0) == 0x30) && (p->rx_index != 0))
{
cP->Inverse = 0;
}
/*-----------------------------------------------------------*/
/* Inverse convention card */
// If first byte is correct for inverse card, return success
else if (((p->rbuf[0] & 0x0F) == 0x03) && (p->rx_index != 0))
{
cP->Inverse = 1;
}
else
{
return (SIM_ERR_CARDREJECT); //Test 27.11.2.4.5
}
countT = 0;
mask = 0x10;
InterfChar = 2;
TDi = 1;
Tx = SIM_Translate_atr_char (p->rbuf[1], cP);
HistChar = Tx & 0x0F; //get K, number of transmitted historical character
while (TDi != 0)
{
while (mask < 0x100) //monitors interface chars
{
if ((Tx & mask) == mask) //monitors if interface character TAx,TBx,TCx,TDc present
{
InterfChar++;
}
//wait for TC1 and save its position
if ((firstprotocol == 0) && ((Tx & 0x40) == mask))
{
position_of_TC1 = InterfChar - 1;
}
mask = mask << 1;
}
p->expected_data = InterfChar; //wait for TAi,TBi,TCi,TDi if present
if (returncode = SIM_Waitforchars (p, p->etu9600))
{
return returncode;
}
//need to monitor if TC1 present and if equal to 0 or 255 on first protocol
if ((firstprotocol == 0) && (position_of_TC1 != 0))
{
T = SIM_Translate_atr_char (p->rbuf[position_of_TC1], cP);
if ((T != 0) && (T != 255)) //test 27.11.1.3
{ //return Error in case of bad TC1 value
return (SIM_ERR_CARDREJECT);
}
}
if ((Tx & 0x80) == 0x80) //TDi byte on first protocol must be 0
{ //get new TD char
Tx = SIM_Translate_atr_char (p->rbuf[InterfChar - 1], cP);
if ((Tx & 0x0F) != 0)
{
if (firstprotocol == 0) //if first protocol received is not T=0, card is rejected
{
return (SIM_ERR_CARDREJECT); //protocol other than T=0
}
else
{ //if an another protocol T != 0 present, need to wait for TCK char
another_protocol_present = 1;
}
}
mask = 0x10;
firstprotocol++; //indicate another protocol T
}
else
{
TDi = 0;
}
}
//add TCK if necessary
p->expected_data = HistChar + InterfChar + another_protocol_present;
if (returncode = SIM_Waitforchars (p, p->etu9600))
{
return returncode;
}
cP->AtrSize = p->rx_index;
if (cP->Inverse) //inverse card
{
// Copy ATR data
for (i=0;i<cP->AtrSize;i++)
{
cP->AtrData[i] = SIM_ByteReverse(p->rbuf[i]);
}
p->c->conf1 = p->conf1
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -