Changeset 1291 for branches/monitor-improvement/csctapi/pps.c
- Timestamp:
- 01/19/10 19:59:12 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/monitor-improvement/csctapi/pps.c
r1282 r1291 38 38 39 39 #define PPS_DEFAULT_PROTOCOL 0x00 40 40 #define PROTOCOL_T0_DEFAULT_WI 10 41 42 #define PROTOCOL_T1_DEFAULT_IFSC 32 43 #define PROTOCOL_T1_DEFAULT_IFSD 32 44 #define PROTOCOL_T1_MAX_IFSC 251 /* Cannot send > 255 buffer */ 45 #define PROTOCOL_T1_DEFAULT_CWI 13 46 #define PROTOCOL_T1_DEFAULT_BWI 4 47 #define PROTOCOL_T1_EDC_LRC 0 48 #define PROTOCOL_T1_EDC_CRC 1 41 49 /* 42 50 * Not exported macros definition … … 57 65 static unsigned PPS_GetLength (BYTE * block); 58 66 59 static int PPS_InitICC ( PPS * pps);60 61 static int PPS_InitProtocol ( PPS * pps, int protocol_selected);67 static int PPS_InitICC (); 68 69 static int PPS_InitProtocol (); 62 70 63 71 static BYTE PPS_GetPCK (BYTE * block, unsigned length); … … 67 75 */ 68 76 69 PPS * PPS_New () 70 { 71 PPS *pps; 72 73 pps = (PPS *) malloc (sizeof (PPS)); 74 75 if (pps != NULL) 76 { 77 pps->protocol = NULL; 78 pps->parameters.t = PPS_DEFAULT_PROTOCOL; 79 pps->parameters.FI = ATR_DEFAULT_FI; 80 pps->parameters.d = ATR_DEFAULT_D; 81 pps->parameters.n = ATR_DEFAULT_N; 82 } 83 84 return pps; 85 } 86 87 int PPS_Perform (PPS * pps, BYTE * params, unsigned *length) 77 void PPS_New () 78 { 79 protocol = NULL; 80 parameters.t = PPS_DEFAULT_PROTOCOL; 81 parameters.FI = ATR_DEFAULT_FI; 82 parameters.d = ATR_DEFAULT_D; 83 parameters.n = ATR_DEFAULT_N; 84 } 85 86 int PPS_Perform (BYTE * params, unsigned *length) 88 87 { 89 88 //Performs PPS Exchange on command when *length >0 … … 110 109 if (ret == PPS_OK) 111 110 { 112 p ps->parameters.t = params[1] & 0x0F;111 parameters.t = params[1] & 0x0F; 113 112 114 113 if (PPS_HAS_PPS1 (params)) 115 114 { 116 p ps->parameters.FI = (params[2] >> 4);117 p ps->parameters.d = atr_d_table[(params[2] & 0x0F)];115 parameters.FI = (params[2] >> 4); 116 parameters.d = atr_d_table[(params[2] & 0x0F)]; 118 117 } 119 118 120 119 /* 121 ret = PPS_InitICC( pps);120 ret = PPS_InitICC(); 122 121 123 122 if (ret != PPS_OK) … … 186 185 187 186 //If more than one protocol type and/or TA1 parameter values other than the default values and/or N equeal to 255 is/are indicated in the answer to reset, the card shall know unambiguously, after having sent the answer to reset, which protocol type or/and transmission parameter values (FI, D, N) will be used. Consequently a selection of the protocol type and/or the transmission parameters values shall be specified. 188 ATR_GetParameter (atr, ATR_PARAMETER_N, &(p ps->parameters.n));189 ATR_GetProtocolType(atr,1,&(p ps->parameters.t)); //get protocol from TD1187 ATR_GetParameter (atr, ATR_PARAMETER_N, &(parameters.n)); 188 ATR_GetProtocolType(atr,1,&(parameters.t)); //get protocol from TD1 190 189 BYTE TA2; 191 190 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode 192 191 if (SpecificMode) { 193 p ps->parameters.t = TA2 & 0x0F;192 parameters.t = TA2 & 0x0F; 194 193 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters 195 194 BYTE TA1; 196 195 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) { 197 p ps->parameters.FI = TA1 >> 4;198 ATR_GetParameter (atr, ATR_PARAMETER_D, &(p ps->parameters.d));196 parameters.FI = TA1 >> 4; 197 ATR_GetParameter (atr, ATR_PARAMETER_D, &(parameters.d)); 199 198 } 200 199 else { 201 p ps->parameters.FI = ATR_DEFAULT_FI;202 p ps->parameters.d = ATR_DEFAULT_D;200 parameters.FI = ATR_DEFAULT_FI; 201 parameters.d = ATR_DEFAULT_D; 203 202 } 204 203 } 205 204 else { 206 205 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values"); 207 p ps->parameters.FI = ATR_DEFAULT_FI;208 p ps->parameters.d = ATR_DEFAULT_D;209 } 210 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", p ps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);206 parameters.FI = ATR_DEFAULT_FI; 207 parameters.d = ATR_DEFAULT_D; 208 } 209 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", parameters.t, (double) atr_f_table[parameters.FI], parameters.d, parameters.n); 211 210 } 212 211 else { //negotiable mode 213 212 214 bool NeedsPTS = ((p ps->parameters.t != 14) && (numprottype > 1 || (atr->ib[0][ATR_INTERFACE_BYTE_TA].present == TRUE && atr->ib[0][ATR_INTERFACE_BYTE_TA].value != 0x11) || pps->parameters.n == 255)); //needs PTS according to ISO 7816 , SCI gets stuck on our PTS213 bool NeedsPTS = ((parameters.t != 14) && (numprottype > 1 || (atr->ib[0][ATR_INTERFACE_BYTE_TA].present == TRUE && atr->ib[0][ATR_INTERFACE_BYTE_TA].value != 0x11) || parameters.n == 255)); //needs PTS according to ISO 7816 , SCI gets stuck on our PTS 215 214 if (NeedsPTS) { 216 215 // PTSS PTS0 PTS1 PTS2 PTS3 PCK 217 216 // PTSS PTS0 PTS1 PCK 218 217 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes 219 req[1]=0x10 | p ps->parameters.t; //PTS0 always flags PTS1 to be sent always218 req[1]=0x10 | parameters.t; //PTS0 always flags PTS1 to be sent always 220 219 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1 221 220 req[2] = 0x11; //defaults FI and DI to 1 … … 224 223 ret = PPS_Exchange (req, &len); 225 224 if (ret == PPS_OK) { 226 p ps->parameters.FI = req[2] >> 4;225 parameters.FI = req[2] >> 4; 227 226 BYTE DI = req[2] & 0x0F; 228 p ps->parameters.d = (double) (atr_d_table[DI]);227 parameters.d = (double) (atr_d_table[DI]); 229 228 PPS_success = TRUE; 230 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", p ps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);229 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", parameters.t, (double) atr_f_table[parameters.FI], parameters.d, parameters.n); 231 230 } 232 231 else … … 239 238 BYTE TA1; 240 239 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) { 241 p ps->parameters.FI = TA1 >> 4;242 ATR_GetParameter (atr, ATR_PARAMETER_D, &(p ps->parameters.d));240 parameters.FI = TA1 >> 4; 241 ATR_GetParameter (atr, ATR_PARAMETER_D, &(parameters.d)); 243 242 } 244 243 else { //do not obey TA1 245 p ps->parameters.FI = ATR_DEFAULT_FI;246 p ps->parameters.d = ATR_DEFAULT_D;244 parameters.FI = ATR_DEFAULT_FI; 245 parameters.d = ATR_DEFAULT_D; 247 246 } 248 ATR_GetProtocolType (atr, 1, &(p ps->parameters.t));247 ATR_GetProtocolType (atr, 1, &(parameters.t)); 249 248 protocol_selected = 1; 250 249 251 250 if (NeedsPTS) { 252 if ((p ps->parameters.d == 32) || (pps->parameters.d == 12) || (pps->parameters.d == 20))253 p ps->parameters.d = 0; //behave conform "old" atr_d_table; viaccess cards that fail PTS need this251 if ((parameters.d == 32) || (parameters.d == 12) || (parameters.d == 20)) 252 parameters.d = 0; //behave conform "old" atr_d_table; viaccess cards that fail PTS need this 254 253 } 255 254 /////Here all non-ISO behaviour 256 255 /////End all non-ISO behaviour 257 256 258 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", p ps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);257 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", parameters.t, (double) atr_f_table[parameters.FI], parameters.d, parameters.n); 259 258 } 260 259 }//end negotiable mode … … 262 261 263 262 //make sure no zero values 264 double F = (double) atr_f_table[p ps->parameters.FI];263 double F = (double) atr_f_table[parameters.FI]; 265 264 if (!F) { 266 p ps->parameters.FI = ATR_DEFAULT_FI;267 cs_log("Warning: F=0 is invalid, forcing FI=%d", p ps->parameters.FI);268 } 269 if (!p ps->parameters.d) {270 p ps->parameters.d = ATR_DEFAULT_D;271 cs_log("Warning: D=0 is invalid, forcing D=%.0f",p ps->parameters.d);272 } 273 274 protocol_type = p ps->parameters.t;265 parameters.FI = ATR_DEFAULT_FI; 266 cs_log("Warning: F=0 is invalid, forcing FI=%d", parameters.FI); 267 } 268 if (!parameters.d) { 269 parameters.d = ATR_DEFAULT_D; 270 cs_log("Warning: D=0 is invalid, forcing D=%.0f",parameters.d); 271 } 272 273 protocol_type = parameters.t; 275 274 276 275 #ifdef DEBUG_PROTOCOL 277 276 printf("PPS: T=%i, F=%.0f, D=%.6f, N=%.0f\n", 278 p ps->parameters.t,277 parameters.t, 279 278 F, 280 p ps->parameters.d,281 p ps->parameters.n);282 #endif 283 284 ret = PPS_InitICC( pps);279 parameters.d, 280 parameters.n); 281 #endif 282 283 ret = PPS_InitICC(); 285 284 286 285 if (ret != PPS_OK) … … 288 287 289 288 /* Initialize selected protocol with selected parameters */ 290 if (pps->parameters.t == 1) 291 ret = PPS_InitProtocol (pps, 3); //FIXME in practice most T1 cards carry timing parameters in TA3, TB3 and TC3 292 else 293 ret = PPS_InitProtocol (pps, 2); //FIXME T0 cards carry timing parameters in TC2 294 295 return ret; 296 } 297 298 void * PPS_GetProtocol (PPS * pps) 299 { 300 return pps->protocol; 301 } 302 303 PPS_ProtocolParameters *PPS_GetProtocolParameters (PPS * pps) 289 return PPS_InitProtocol (); 290 } 291 292 PPS_ProtocolParameters *PPS_GetProtocolParameters () 304 293 { 305 294 /* User must Remember not to reference this struct after removing PPS */ 306 return &(pps->parameters); 307 } 308 309 void PPS_Delete (PPS * pps) 310 { 311 free (pps); 295 return &(parameters); 312 296 } 313 297 … … 423 407 } 424 408 425 static int PPS_InitICC ( PPS * pps)409 static int PPS_InitICC () 426 410 { 427 411 #ifdef SCI_DEV … … 437 421 return PPS_ICC_ERROR; 438 422 439 params.T = p ps->parameters.t;440 params.fs = atr_fs_table[p ps->parameters.FI] / 1000000;441 double F = (double) atr_f_table[p ps->parameters.FI];423 params.T = parameters.t; 424 params.fs = atr_fs_table[parameters.FI] / 1000000; 425 double F = (double) atr_f_table[parameters.FI]; 442 426 //for Irdeto T14 cards, do not set ETU 443 427 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && params.T == 14)) 444 params.ETU = F / p ps->parameters.d;445 if (p ps->parameters.n == 255) //only for T0 or also for T1?428 params.ETU = F / parameters.d; 429 if (parameters.n == 255) //only for T0 or also for T1? 446 430 params.EGT = 0; 447 431 else 448 params.EGT = p ps->parameters.n;432 params.EGT = parameters.n; 449 433 450 434 double a; … … 462 446 #elif COOL 463 447 if(reader[ridx].typ == R_INTERNAL) { 464 int mhz = atr_fs_table[p ps->parameters.FI] / 10000;448 int mhz = atr_fs_table[parameters.FI] / 10000; 465 449 if (!Cool_SetBaudrate(mhz)) 466 450 return PPS_ICC_ERROR; … … 473 457 { 474 458 unsigned long baudrate; 475 double F = (double) atr_f_table[p ps->parameters.FI];476 if (p ps->parameters.t == 14)459 double F = (double) atr_f_table[parameters.FI]; 460 if (parameters.t == 14) 477 461 baudrate = 9600; 478 462 else 479 baudrate = p ps->parameters.d * ICC_Async_GetClockRate () / F;463 baudrate = parameters.d * ICC_Async_GetClockRate () / F; 480 464 481 465 #ifdef DEBUG_PROTOCOL … … 491 475 } 492 476 493 static int PPS_InitProtocol (PPS * pps, int selected_protocol) 494 { 495 int ret; 496 497 if (pps->parameters.t == ATR_PROTOCOL_TYPE_T0) 498 { 499 pps->protocol = Protocol_T0_New (); 500 501 if ((pps->protocol) != NULL) 477 int Protocol_T1_Init () 478 { 479 BYTE ta, tb, tc, cwi, bwi; 480 unsigned long baudrate; 481 double work_etu; 482 483 // Set IFSC 484 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND) 485 ifsc = PROTOCOL_T1_DEFAULT_IFSC; 486 else if ((ta != 0x00) && (ta != 0xFF)) 487 ifsc = ta; 488 else 489 ifsc = PROTOCOL_T1_DEFAULT_IFSC; 490 491 // Towitoko does not allow IFSC > 251 //FIXME not sure whether this limitation still exists 492 ifsc = MIN (ifsc, PROTOCOL_T1_MAX_IFSC); 493 494 // Set IFSD 495 ifsd = PROTOCOL_T1_DEFAULT_IFSD; 496 497 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS 498 // Calculate CWI and BWI 499 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND) 502 500 { 503 ret = Protocol_T0_Init ((Protocol_T0 *) pps->protocol, &(pps->parameters), selected_protocol); 501 #endif 502 cwi = PROTOCOL_T1_DEFAULT_CWI; 503 bwi = PROTOCOL_T1_DEFAULT_BWI; 504 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS 505 } 506 else 507 { 508 cwi = tb & 0x0F; 509 bwi = tb >> 4; 510 } 511 #endif 512 513 // Work etu = (1000 / baudrate) milliseconds 514 ICC_Async_GetBaudrate (&baudrate); 515 work_etu = 1000 / (double)baudrate; 516 517 // Set CWT = (2^CWI + 11) work etu 518 cwt = (unsigned short) (((1<<cwi) + 11) * work_etu); 519 520 // Set BWT = (2^BWI * 960 + 11) work etu 521 bwt = (unsigned short) (((1<<bwi) * 960 + 11) * work_etu); 522 523 // Set BGT = 22 * work etu 524 bgt = (unsigned short) (22 * work_etu); 525 526 // Set the error detection code type 527 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND) 528 edc = PROTOCOL_T1_EDC_LRC; 529 else 530 edc = tc & 0x01; 531 532 // Set initial send sequence (NS) 533 ns = 1; 534 535 // Set timings 536 icc_timings.block_timeout = bwt; 537 icc_timings.char_timeout = cwt; 538 icc_timings.block_delay = bgt; 539 ICC_Async_SetTimings (); 540 541 #ifdef DEBUG_PROTOCOL 542 printf ("Protocol: T=1: IFSC=%d, IFSD=%d, CWT=%d, BWT=%d, BGT=%d, EDC=%s\n", 543 ifsc, ifsd, cwt, bwt, t1->bgt, 544 (edc == PROTOCOL_T1_EDC_LRC) ? "LRC" : "CRC"); 545 #endif 546 547 return PROTOCOL_T1_OK; 548 } 549 550 static int PPS_InitProtocol () 551 { 552 switch (parameters.t) { 553 case ATR_PROTOCOL_TYPE_T0: 554 case ATR_PROTOCOL_TYPE_T14: 555 { 556 BYTE wi; 557 /* Integer value WI = TC2, by default 10 */ 558 #ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS 559 if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK) 560 #endif 561 wi = PROTOCOL_T0_DEFAULT_WI; 562 563 /* WWT = 960 * WI * (Fi / f) * 1000 milliseconds */ 564 double F = (double) atr_f_table[parameters.FI]; 565 unsigned long wwt = (long unsigned int) (960 * wi * (F / ICC_Async_GetClockRate ()) * 1000); 566 if (parameters.t == 14) 567 wwt >>= 1; //is this correct? 504 568 505 if (ret != PROTOCOL_T0_OK) 506 { 507 Protocol_T0_Delete ((Protocol_T0 *) pps->protocol); 508 pps->protocol = NULL; 509 return PPS_PROTOCOL_ERROR; 510 } 511 512 return PPS_OK; 513 } 514 } 515 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T1) 516 { 517 pps->protocol = Protocol_T1_New (); 518 519 if (pps->protocol != NULL) 520 { 521 ret = Protocol_T1_Init ((Protocol_T1 *) pps->protocol, selected_protocol); 522 523 if (ret != PROTOCOL_T1_OK) 524 { 525 Protocol_T1_Delete ((Protocol_T1 *) pps->protocol); 526 pps->protocol = NULL; 527 return PPS_PROTOCOL_ERROR; 528 } 529 530 return PPS_OK; 531 } 532 } 533 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T14) 534 { 535 pps->protocol = Protocol_T14_New (); 536 537 if ((pps->protocol) != NULL) 538 { 539 ret = Protocol_T14_Init ((Protocol_T14 *) pps->protocol, &(pps->parameters), selected_protocol); 540 541 if (ret != PROTOCOL_T14_OK) 542 { 543 Protocol_T14_Delete ((Protocol_T14 *) pps->protocol); 544 pps->protocol = NULL; 545 return PPS_PROTOCOL_ERROR; 546 } 547 548 return PPS_OK; 549 } 550 } 551 else 552 { 553 pps->protocol = NULL; 554 } 555 556 return PPS_PROTOCOL_ERROR; 569 /* Set timings */ 570 icc_timings.block_timeout = wwt; 571 icc_timings.char_timeout = wwt; 572 ICC_Async_SetTimings (); 573 #ifdef DEBUG_PROTOCOL 574 printf ("Protocol: T=%i: WWT=%d, Clockrate=%lu\n", params->t, (int)(wwt),ICC_Async_GetClockRate()); 575 #endif 576 } 577 break; 578 case ATR_PROTOCOL_TYPE_T1: 579 Protocol_T1_Init ();//always returns ok 580 break; 581 default: 582 protocol = NULL; 583 return PPS_PROTOCOL_ERROR; 584 break; 585 } 586 return PPS_OK; 557 587 } 558 588
Note:
See TracChangeset
for help on using the changeset viewer.