Changeset 1290 for branches/smartreader/csctapi/protocol_t1.c
- Timestamp:
- 01/19/10 16:48:50 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/smartreader/csctapi/protocol_t1.c
r1280 r1290 31 31 32 32 /* 33 * Not exported constants definition34 */35 #define PROTOCOL_T1_DEFAULT_IFSC 3236 #define PROTOCOL_T1_DEFAULT_IFSD 3237 #define PROTOCOL_T1_MAX_IFSC 251 /* Cannot send > 255 buffer */38 #define PROTOCOL_T1_DEFAULT_CWI 1339 #define PROTOCOL_T1_DEFAULT_BWI 440 #define PROTOCOL_T1_EDC_LRC 041 #define PROTOCOL_T1_EDC_CRC 142 43 /*44 33 * Not exported functions declaration 45 34 */ 46 35 47 static void48 Protocol_T1_Clear (Protocol_T1 * t1);49 50 36 static int 51 37 Protocol_T1_SendBlock (T1_Block * block); 52 38 53 39 static int 54 Protocol_T1_ReceiveBlock ( Protocol_T1 * t1,T1_Block ** block);40 Protocol_T1_ReceiveBlock (T1_Block ** block); 55 41 56 42 static int … … 61 47 */ 62 48 63 Protocol_T1 *64 Protocol_T1_New (void)65 {66 Protocol_T1 *t1;67 68 t1 = (Protocol_T1 *) malloc (sizeof (Protocol_T1));69 70 if (t1 != NULL)71 Protocol_T1_Clear (t1);72 73 return t1;74 }75 76 49 int 77 Protocol_T1_Init (Protocol_T1 * t1, int selected_protocol) 78 { 79 BYTE ta, tb, tc, cwi, bwi; 80 unsigned long baudrate; 81 double work_etu; 82 int i; 83 84 /* Set IFSC */ 85 if (ATR_GetInterfaceByte (atr, selected_protocol, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND) 86 t1->ifsc = PROTOCOL_T1_DEFAULT_IFSC; 87 else if ((ta != 0x00) && (ta != 0xFF)) 88 t1->ifsc = ta; 89 else 90 t1->ifsc = PROTOCOL_T1_DEFAULT_IFSC; 91 92 /* Towitoko does not allow IFSC > 251 */ 93 t1->ifsc = MIN (t1->ifsc, PROTOCOL_T1_MAX_IFSC); 94 95 /* Set IFSD */ 96 t1->ifsd = PROTOCOL_T1_DEFAULT_IFSD; 97 98 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS 99 /* Calculate CWI and BWI */ 100 if (ATR_GetInterfaceByte (atr, selected_protocol, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND) 101 { 102 #endif 103 cwi = PROTOCOL_T1_DEFAULT_CWI; 104 bwi = PROTOCOL_T1_DEFAULT_BWI; 105 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS 106 } 107 else 108 { 109 cwi = tb & 0x0F; 110 bwi = (tb & 0xF0) >> 4; 111 } 112 #endif 113 114 /* Work etu = (1000 / baudrate) milliseconds */ 115 ICC_Async_GetBaudrate (&baudrate); 116 work_etu = 1000 / (double)baudrate; 117 118 /* Set CWT = (2^CWI + 11) work etu */ 119 t1->cwt = 1; 120 121 for (i = 0; i < cwi ; i++) 122 t1->cwt *= 2; 123 124 t1->cwt = (unsigned short) ((t1->cwt + 11) * work_etu); 125 126 /* Set BWT = (2^BWI * 960 + 11) work etu */ 127 t1->bwt = 1; 128 for (i = 0; i < bwi; i++) 129 t1->bwt *= 2; 130 131 t1->bwt = (unsigned short) ((t1->bwt * 960 + 11) * work_etu); 132 133 /* Set BGT = 22 * work etu */ 134 t1->bgt = (unsigned short) (22 * work_etu); 135 136 /* Set the error detection code type */ 137 if (ATR_GetInterfaceByte (atr, selected_protocol, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND) 138 t1->edc = PROTOCOL_T1_EDC_LRC; 139 else 140 t1->edc = tc & 0x01; 141 142 /* Set initial send sequence (NS) */ 143 t1->ns = 1; 144 145 /* Set timings */ 146 icc_timings.block_timeout = t1->bwt; 147 icc_timings.char_timeout = t1->cwt; 148 icc_timings.block_delay = t1->bgt; 149 ICC_Async_SetTimings (); 150 151 #ifdef DEBUG_PROTOCOL 152 printf ("Protocol: T=1: IFSC=%d, IFSD=%d, CWT=%d, BWT=%d, BGT=%d, EDC=%s\n", 153 t1->ifsc, t1->ifsd, t1->cwt, t1->bwt, t1->bgt, 154 (t1->edc == PROTOCOL_T1_EDC_LRC) ? "LRC" : "CRC"); 155 #endif 156 157 return PROTOCOL_T1_OK; 158 } 159 160 int 161 Protocol_T1_Command (Protocol_T1 * t1, APDU_Cmd * cmd, APDU_Rsp ** rsp) 50 Protocol_T1_Command (APDU_Cmd * cmd, APDU_Rsp ** rsp) 162 51 { 163 52 T1_Block *block; … … 183 72 184 73 /* Receive a block */ 185 ret = Protocol_T1_ReceiveBlock ( t1,&block);74 ret = Protocol_T1_ReceiveBlock (&block); 186 75 187 76 if (ret == PROTOCOL_T1_OK) … … 194 83 /* Update IFSD value */ 195 84 inf = (*T1_Block_GetInf (block)); 196 t1->ifsd = inf;85 ifsd = inf; 197 86 #ifdef DEBUG_PROTOCOL 198 87 printf ("Protocol: Received block S(IFS response, %d)\n", inf); … … 206 95 /* Calculate the number of bytes to send */ 207 96 counter = 0; 208 bytes = MIN (APDU_Cmd_RawLen (cmd), t1->ifsc);97 bytes = MIN (APDU_Cmd_RawLen (cmd), ifsc); 209 98 210 99 /* See if chaining is needed */ 211 more = (APDU_Cmd_RawLen (cmd) > t1->ifsc);100 more = (APDU_Cmd_RawLen (cmd) > ifsc); 212 101 213 102 /* Increment ns */ 214 t1->ns = (t1->ns + 1) %2;103 ns = (ns + 1) %2; 215 104 216 105 /* Create an I-Block */ 217 block = T1_Block_NewIBlock (bytes, APDU_Cmd_Raw (cmd), t1->ns, more);218 219 #ifdef DEBUG_PROTOCOL 220 printf ("Sending block I(%d,%d)\n", t1->ns, more);106 block = T1_Block_NewIBlock (bytes, APDU_Cmd_Raw (cmd), ns, more); 107 108 #ifdef DEBUG_PROTOCOL 109 printf ("Sending block I(%d,%d)\n", ns, more); 221 110 #endif 222 111 … … 230 119 { 231 120 /* Receive a block */ 232 ret = Protocol_T1_ReceiveBlock ( t1,&block);121 ret = Protocol_T1_ReceiveBlock (&block); 233 122 234 123 if (ret == PROTOCOL_T1_OK) … … 246 135 247 136 /* Increment ns */ 248 t1->ns = (t1->ns + 1) % 2;137 ns = (ns + 1) % 2; 249 138 250 139 /* Calculate the number of bytes to send */ 251 140 counter += bytes; 252 bytes = MIN (APDU_Cmd_RawLen (cmd) - counter, t1->ifsc);141 bytes = MIN (APDU_Cmd_RawLen (cmd) - counter, ifsc); 253 142 254 143 /* See if chaining is needed */ 255 more = (APDU_Cmd_RawLen (cmd) - counter > t1->ifsc);144 more = (APDU_Cmd_RawLen (cmd) - counter > ifsc); 256 145 257 146 /* Create an I-Block */ 258 147 block = 259 148 T1_Block_NewIBlock (bytes, APDU_Cmd_Raw (cmd) + counter, 260 t1->ns, more);261 #ifdef DEBUG_PROTOCOL 262 printf ("Protocol: Sending block I(%d,%d)\n", t1->ns, more);149 ns, more); 150 #ifdef DEBUG_PROTOCOL 151 printf ("Protocol: Sending block I(%d,%d)\n", ns, more); 263 152 #endif 264 153 /* Send a block */ … … 293 182 { 294 183 if (wtx > 1) 295 Protocol_T1_UpdateBWT (wtx * ( t1->bwt));184 Protocol_T1_UpdateBWT (wtx * (bwt)); 296 185 297 186 /* Receive a block */ 298 ret = Protocol_T1_ReceiveBlock ( t1,&block);187 ret = Protocol_T1_ReceiveBlock (&block); 299 188 300 189 if (wtx > 1) 301 190 { 302 Protocol_T1_UpdateBWT ( t1->bwt);191 Protocol_T1_UpdateBWT (bwt); 303 192 wtx = 0; 304 193 } … … 383 272 } 384 273 385 int386 Protocol_T1_Close (Protocol_T1 * t1)387 {388 Protocol_T1_Clear (t1);389 390 return PROTOCOL_T1_OK;391 }392 393 void394 Protocol_T1_Delete (Protocol_T1 * t1)395 {396 free (t1);397 }398 399 274 /* 400 275 * Not exported functions definition … … 425 300 426 301 static int 427 Protocol_T1_ReceiveBlock ( Protocol_T1 * t1,T1_Block ** block)302 Protocol_T1_ReceiveBlock (T1_Block ** block) 428 303 { 429 304 BYTE buffer[T1_BLOCK_MAX_SIZE]; … … 442 317 { 443 318 /* Set timings to read the remaining block */ 444 Protocol_T1_UpdateBWT ( t1->cwt);319 Protocol_T1_UpdateBWT (cwt); 445 320 446 321 /* Receive remaining bytes */ … … 459 334 460 335 /* Restore timings */ 461 Protocol_T1_UpdateBWT ( t1->bwt);336 Protocol_T1_UpdateBWT (bwt); 462 337 } 463 338 else … … 469 344 470 345 return ret; 471 }472 473 static void474 Protocol_T1_Clear (Protocol_T1 * t1)475 {476 t1->ifsc = 0;477 t1->ifsd = 0;478 t1->bgt = 0;479 t1->bwt = 0;480 t1->cwt = 0;481 t1->edc = 0;482 t1->ns = 0;483 346 } 484 347
Note:
See TracChangeset
for help on using the changeset viewer.