1 | /*
|
---|
2 | pps.c
|
---|
3 | Protocol Parameters Selection
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "pps.h"
|
---|
26 | #include "atr.h"
|
---|
27 | #include "protocol_t0.h"
|
---|
28 | #include "protocol_t1.h"
|
---|
29 | #include <stdlib.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include "ifd.h"
|
---|
33 | #include "../globals.h"
|
---|
34 |
|
---|
35 | /*
|
---|
36 | * Not exported constants definition
|
---|
37 | */
|
---|
38 |
|
---|
39 | #define PPS_DEFAULT_PROTOCOL 0x00
|
---|
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
|
---|
49 | /*
|
---|
50 | * Not exported macros definition
|
---|
51 | */
|
---|
52 |
|
---|
53 | #define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
|
---|
54 | #define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
|
---|
55 | #define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Not exported funtions declaration
|
---|
59 | */
|
---|
60 |
|
---|
61 | static int PPS_Exchange (BYTE * params, unsigned *length);
|
---|
62 |
|
---|
63 | static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply);
|
---|
64 |
|
---|
65 | static unsigned PPS_GetLength (BYTE * block);
|
---|
66 |
|
---|
67 | static int PPS_InitICC ();
|
---|
68 |
|
---|
69 | static BYTE PPS_GetPCK (BYTE * block, unsigned length);
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Exported functions definition
|
---|
73 | */
|
---|
74 |
|
---|
75 | void PPS_New ()
|
---|
76 | {
|
---|
77 | protocol = NULL;
|
---|
78 | parameters.t = PPS_DEFAULT_PROTOCOL;
|
---|
79 | parameters.FI = ATR_DEFAULT_FI;
|
---|
80 | parameters.d = ATR_DEFAULT_D;
|
---|
81 | parameters.n = ATR_DEFAULT_N;
|
---|
82 | }
|
---|
83 |
|
---|
84 | int PPS_Perform (BYTE * params, unsigned *length)
|
---|
85 | {
|
---|
86 | //Performs PPS Exchange on command when *length >0
|
---|
87 | //If unsuccesfull, or when length <= 0
|
---|
88 | //Gets parametes from ATR
|
---|
89 | //If necessary perform PPS session
|
---|
90 | //
|
---|
91 | //Output is pps->params.FI,n,d,t set with correct values
|
---|
92 | //and switched SC-device conform these values to correct baudrate
|
---|
93 | //
|
---|
94 | //We need to store FI instread of F, because SCI_DEV works with FI
|
---|
95 | //and it is easier to overclock then
|
---|
96 | //also from FI -> F is easy, other way around not
|
---|
97 |
|
---|
98 | int ret;
|
---|
99 | bool PPS_success;
|
---|
100 |
|
---|
101 | /* Perform PPS Exchange if requested by command */
|
---|
102 | if ((*length) > 0)
|
---|
103 | {
|
---|
104 | ret = PPS_Exchange (params, length);
|
---|
105 |
|
---|
106 | /* Get parameters from PPS handsake */
|
---|
107 | if (ret == PPS_OK)
|
---|
108 | {
|
---|
109 | parameters.t = params[1] & 0x0F;
|
---|
110 |
|
---|
111 | if (PPS_HAS_PPS1 (params))
|
---|
112 | {
|
---|
113 | parameters.FI = (params[2] >> 4);
|
---|
114 | parameters.d = atr_d_table[(params[2] & 0x0F)];
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*
|
---|
118 | ret = PPS_InitICC();
|
---|
119 |
|
---|
120 | if (ret != PPS_OK)
|
---|
121 | return ret;
|
---|
122 | */
|
---|
123 | }
|
---|
124 | /*
|
---|
125 | else
|
---|
126 | {
|
---|
127 | return ret;
|
---|
128 | }
|
---|
129 | */
|
---|
130 | }
|
---|
131 | PPS_success = PPS_OK;
|
---|
132 | int protocol_selected = 0; //stores which TAi,TBi etc. bytes must be used 0 means not set
|
---|
133 | if ((*length) <= 0 || !PPS_success) // If not by command, or PPS Exchange by command failed: Try PPS Exchange by ATR or Get parameters from ATR
|
---|
134 | {
|
---|
135 | int numprot = atr->pn;
|
---|
136 | //if there is a trailing TD, this number is one too high
|
---|
137 | BYTE tx;
|
---|
138 | if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
|
---|
139 | if ((tx & 0xF0) == 0)
|
---|
140 | numprot--;
|
---|
141 | cs_debug("ATR reports %i protocol lines:",numprot);
|
---|
142 | int i,point;
|
---|
143 | char txt[50];
|
---|
144 | bool OffersT[3]; //T14 stored as T2
|
---|
145 | for (i = 0; i <= 2; i++)
|
---|
146 | OffersT[i] = FALSE;
|
---|
147 | for (i=1; i<= numprot; i++) {
|
---|
148 | point = 0;
|
---|
149 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
|
---|
150 | sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
|
---|
151 | point +=7;
|
---|
152 | }
|
---|
153 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
|
---|
154 | sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
|
---|
155 | point +=7;
|
---|
156 | }
|
---|
157 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
|
---|
158 | sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
|
---|
159 | point +=7;
|
---|
160 | }
|
---|
161 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
|
---|
162 | sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
|
---|
163 | point +=7;
|
---|
164 | tx &= 0X0F;
|
---|
165 | sprintf((char *)txt+point,"(T%i)",tx);
|
---|
166 | if (tx == 14)
|
---|
167 | OffersT[2] = TRUE;
|
---|
168 | else
|
---|
169 | OffersT[tx] = TRUE;
|
---|
170 | }
|
---|
171 | else {
|
---|
172 | sprintf((char *)txt+point,"no TD%i means T0",i);
|
---|
173 | OffersT[0] = TRUE;
|
---|
174 | }
|
---|
175 | cs_debug("%s",txt);
|
---|
176 | }
|
---|
177 |
|
---|
178 | int numprottype = 0;
|
---|
179 | for (i = 0; i <= 2; i++)
|
---|
180 | if (OffersT[i])
|
---|
181 | numprottype ++;
|
---|
182 | cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
|
---|
183 |
|
---|
184 | //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.
|
---|
185 | ATR_GetParameter (atr, ATR_PARAMETER_N, &(parameters.n));
|
---|
186 | ATR_GetProtocolType(atr,1,&(parameters.t)); //get protocol from TD1
|
---|
187 | BYTE TA2;
|
---|
188 | bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
|
---|
189 | if (SpecificMode) {
|
---|
190 | parameters.t = TA2 & 0x0F;
|
---|
191 | if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
|
---|
192 | BYTE TA1;
|
---|
193 | if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
|
---|
194 | parameters.FI = TA1 >> 4;
|
---|
195 | ATR_GetParameter (atr, ATR_PARAMETER_D, &(parameters.d));
|
---|
196 | }
|
---|
197 | else {
|
---|
198 | parameters.FI = ATR_DEFAULT_FI;
|
---|
199 | parameters.d = ATR_DEFAULT_D;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | else {
|
---|
203 | cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
|
---|
204 | parameters.FI = ATR_DEFAULT_FI;
|
---|
205 | parameters.d = ATR_DEFAULT_D;
|
---|
206 | }
|
---|
207 | 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);
|
---|
208 | }
|
---|
209 | else { //negotiable mode
|
---|
210 |
|
---|
211 | 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
|
---|
212 | if (NeedsPTS) {
|
---|
213 | // PTSS PTS0 PTS1 PTS2 PTS3 PCK
|
---|
214 | // PTSS PTS0 PTS1 PCK
|
---|
215 | BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
|
---|
216 | req[1]=0x10 | parameters.t; //PTS0 always flags PTS1 to be sent always
|
---|
217 | if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
|
---|
218 | req[2] = 0x11; //defaults FI and DI to 1
|
---|
219 | //req[3]=PPS_GetPCK(req,sizeof(req)-1); will be set by PPS_Exchange
|
---|
220 | unsigned int len = sizeof(req);
|
---|
221 | ret = PPS_Exchange (req, &len);
|
---|
222 | if (ret == PPS_OK) {
|
---|
223 | parameters.FI = req[2] >> 4;
|
---|
224 | BYTE DI = req[2] & 0x0F;
|
---|
225 | parameters.d = (double) (atr_d_table[DI]);
|
---|
226 | PPS_success = TRUE;
|
---|
227 | 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);
|
---|
228 | }
|
---|
229 | else
|
---|
230 | cs_ddump(req,4,"PTS Failure, response:");
|
---|
231 | }
|
---|
232 |
|
---|
233 | //FIXME Currently InitICC sets baudrate to 9600 for all T14 cards (=no switching);
|
---|
234 | //When for SCI, T14 protocol, TA1 is obeyed, this goes OK for mosts devices, but somehow on DM7025 Sky S02 card goes wrong when setting ETU (ok on DM800/DM8000)
|
---|
235 | if (!PPS_success) {//last PPS not succesfull
|
---|
236 | BYTE TA1;
|
---|
237 | if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
|
---|
238 | parameters.FI = TA1 >> 4;
|
---|
239 | ATR_GetParameter (atr, ATR_PARAMETER_D, &(parameters.d));
|
---|
240 | }
|
---|
241 | else { //do not obey TA1
|
---|
242 | parameters.FI = ATR_DEFAULT_FI;
|
---|
243 | parameters.d = ATR_DEFAULT_D;
|
---|
244 | }
|
---|
245 | ATR_GetProtocolType (atr, 1, &(parameters.t));
|
---|
246 | protocol_selected = 1;
|
---|
247 |
|
---|
248 | if (NeedsPTS) {
|
---|
249 | if ((parameters.d == 32) || (parameters.d == 12) || (parameters.d == 20))
|
---|
250 | parameters.d = 0; //behave conform "old" atr_d_table; viaccess cards that fail PTS need this
|
---|
251 | }
|
---|
252 | /////Here all non-ISO behaviour
|
---|
253 | /////End all non-ISO behaviour
|
---|
254 |
|
---|
255 | 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);
|
---|
256 | }
|
---|
257 | }//end negotiable mode
|
---|
258 | }//end length<0
|
---|
259 |
|
---|
260 | //make sure no zero values
|
---|
261 | double F = (double) atr_f_table[parameters.FI];
|
---|
262 | if (!F) {
|
---|
263 | parameters.FI = ATR_DEFAULT_FI;
|
---|
264 | cs_log("Warning: F=0 is invalid, forcing FI=%d", parameters.FI);
|
---|
265 | }
|
---|
266 | if (!parameters.d) {
|
---|
267 | parameters.d = ATR_DEFAULT_D;
|
---|
268 | cs_log("Warning: D=0 is invalid, forcing D=%.0f",parameters.d);
|
---|
269 | }
|
---|
270 |
|
---|
271 | protocol_type = parameters.t;
|
---|
272 | #ifdef DEBUG_PROTOCOL
|
---|
273 | printf("PPS: T=%i, F=%.0f, D=%.6f, N=%.0f\n",
|
---|
274 | parameters.t,
|
---|
275 | F,
|
---|
276 | parameters.d,
|
---|
277 | parameters.n);
|
---|
278 | #endif
|
---|
279 |
|
---|
280 | /* Initialize selected protocol with selected parameters */
|
---|
281 | // if (PPS_InitProtocol () != PPS_OK)
|
---|
282 | // return PPS_ICC_ERROR;
|
---|
283 |
|
---|
284 | return PPS_InitICC();
|
---|
285 | }
|
---|
286 |
|
---|
287 | PPS_ProtocolParameters *PPS_GetProtocolParameters ()
|
---|
288 | {
|
---|
289 | /* User must Remember not to reference this struct after removing PPS */
|
---|
290 | return &(parameters);
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Not exported funtions definition
|
---|
295 | */
|
---|
296 |
|
---|
297 | static int PPS_Exchange (BYTE * params, unsigned *length)
|
---|
298 | {
|
---|
299 | BYTE confirm[PPS_MAX_LENGTH];
|
---|
300 | unsigned len_request, len_confirm;
|
---|
301 | int ret;
|
---|
302 | #ifdef DEBUG_PROTOCOL
|
---|
303 | int i;
|
---|
304 | #endif
|
---|
305 |
|
---|
306 | len_request = PPS_GetLength (params);
|
---|
307 | params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
|
---|
308 |
|
---|
309 | #ifdef DEBUG_PROTOCOL
|
---|
310 | printf ("PPS: Sending request: ");
|
---|
311 | for (i = 0; i < len_request; i++)
|
---|
312 | printf ("%X ", params[i]);
|
---|
313 | printf ("\n");
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | cs_debug("PTS: Sending request: %s", cs_hexdump(1, params, len_request));
|
---|
317 |
|
---|
318 | /* Send PPS request */
|
---|
319 | #ifdef COOL
|
---|
320 | //unsigned char ptsAck[10];
|
---|
321 | //u_int8 ptsLen = len_request;
|
---|
322 | unsigned short int ptsLen = len_request;
|
---|
323 | int Status = cnxt_smc_start_pps(handle, params, confirm, &ptsLen, TRUE);
|
---|
324 | printf ("cnxt_smc_start_pps Status=%i\n", Status);
|
---|
325 | len_confirm = ptsLen;
|
---|
326 | #ifdef DEBUG_PROTOCOL
|
---|
327 | printf("COOL: confirm: \n");
|
---|
328 | for (i = 0; i < ptsLen; i++)
|
---|
329 | printf ("%02X", confirm[i]);
|
---|
330 | printf ("\n");
|
---|
331 | fflush(stdout);
|
---|
332 | printf("COOL: req: \n");
|
---|
333 | for (i = 0; i < len_request; i++)
|
---|
334 | printf ("%02X", params[i]);
|
---|
335 | printf ("\n");
|
---|
336 | fflush(stdout);
|
---|
337 | #endif
|
---|
338 | if (Status)
|
---|
339 | return PPS_HANDSAKE_ERROR;
|
---|
340 | #else
|
---|
341 | if (ICC_Async_Transmit (len_request, params) != ICC_ASYNC_OK)
|
---|
342 | return PPS_ICC_ERROR;
|
---|
343 |
|
---|
344 | /* Get PPS confirm */
|
---|
345 | if (ICC_Async_Receive (2, confirm) != ICC_ASYNC_OK)
|
---|
346 | return PPS_ICC_ERROR;
|
---|
347 |
|
---|
348 | len_confirm = PPS_GetLength (confirm);
|
---|
349 |
|
---|
350 | if (ICC_Async_Receive (len_confirm - 2, confirm + 2) != ICC_ASYNC_OK)
|
---|
351 | return PPS_ICC_ERROR;
|
---|
352 |
|
---|
353 | #ifdef DEBUG_PROTOCOL
|
---|
354 | printf ("PPS: Receivig confirm: ");
|
---|
355 | for (i = 0; i < len_confirm; i++)
|
---|
356 | printf ("%X ", confirm[i]);
|
---|
357 | printf ("\n");
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | cs_debug("PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
|
---|
361 |
|
---|
362 | if (!PPS_Match (params, len_request, confirm, len_confirm))
|
---|
363 | ret = PPS_HANDSAKE_ERROR;
|
---|
364 | else
|
---|
365 | ret = PPS_OK;
|
---|
366 | #endif
|
---|
367 |
|
---|
368 | /* Copy PPS handsake */
|
---|
369 | memcpy (params, confirm, len_confirm);
|
---|
370 | (*length) = len_confirm;
|
---|
371 |
|
---|
372 | return ret;
|
---|
373 | }
|
---|
374 |
|
---|
375 | static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm)
|
---|
376 | {
|
---|
377 | /* See if the reply differs from request */
|
---|
378 | if ((len_request != len_confirm) || (memcmp (request, confirm, len_request)))
|
---|
379 | {
|
---|
380 | /* See if the card specifies other than default FI and D */
|
---|
381 | //if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2]))
|
---|
382 | return FALSE;
|
---|
383 | }
|
---|
384 |
|
---|
385 | return TRUE;
|
---|
386 | }
|
---|
387 |
|
---|
388 | static unsigned PPS_GetLength (BYTE * block)
|
---|
389 | {
|
---|
390 | unsigned length = 3;
|
---|
391 |
|
---|
392 | if (PPS_HAS_PPS1 (block))
|
---|
393 | length++;
|
---|
394 |
|
---|
395 | if (PPS_HAS_PPS2 (block))
|
---|
396 | length++;
|
---|
397 |
|
---|
398 | if (PPS_HAS_PPS3 (block))
|
---|
399 | length++;
|
---|
400 |
|
---|
401 | return length;
|
---|
402 | }
|
---|
403 |
|
---|
404 | unsigned int ETU_to_ms(unsigned long WWT)
|
---|
405 | {
|
---|
406 | #define CHAR_LEN 10L //character length in ETU, perhaps should be 9 when parity = none?
|
---|
407 | if (WWT > CHAR_LEN)
|
---|
408 | WWT -= CHAR_LEN;
|
---|
409 | else
|
---|
410 | WWT = 0;
|
---|
411 | double work_etu = 1000 / (double)reader[ridx].baudrate;//FIXME sometimes work_etu should be used, sometimes initial etu
|
---|
412 | return (unsigned int) WWT * work_etu;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | static int PPS_InitICC ()
|
---|
417 | {
|
---|
418 | unsigned long WWT, BWT, CWT, BGT, edc, EGT, CGT;
|
---|
419 |
|
---|
420 | if (parameters.n == 255) //Extra Guard Time
|
---|
421 | EGT = 0;
|
---|
422 | else
|
---|
423 | EGT = parameters.n;
|
---|
424 | unsigned int GT = EGT + 12; //Guard Time in ETU
|
---|
425 | unsigned long gt_ms = ETU_to_ms(GT);
|
---|
426 |
|
---|
427 | switch (parameters.t) {
|
---|
428 | case ATR_PROTOCOL_TYPE_T0:
|
---|
429 | case ATR_PROTOCOL_TYPE_T14:
|
---|
430 | {
|
---|
431 | BYTE wi;
|
---|
432 | /* Integer value WI = TC2, by default 10 */
|
---|
433 | #ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS
|
---|
434 | if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
|
---|
435 | #endif
|
---|
436 | wi = PROTOCOL_T0_DEFAULT_WI;
|
---|
437 |
|
---|
438 | // WWT = 960 * WI * (Fi / f) * 1000 milliseconds
|
---|
439 | WWT = (unsigned long) 960 * wi; //in ETU
|
---|
440 | if (parameters.t == 14)
|
---|
441 | WWT >>= 1; //is this correct?
|
---|
442 |
|
---|
443 | if (reader[ridx].typ != R_INTERNAL) {
|
---|
444 | unsigned long wwt_ms = ETU_to_ms(WWT);
|
---|
445 |
|
---|
446 | icc_timings.block_timeout = wwt_ms;
|
---|
447 | icc_timings.char_timeout = wwt_ms;
|
---|
448 | icc_timings.block_delay = gt_ms;
|
---|
449 | icc_timings.char_delay = gt_ms;
|
---|
450 | cs_debug("Setting timings: block_timeout=%u ms, char_timeout=%u ms, block_delay=%u ms, char_delay=%u ms",icc_timings.block_timeout, icc_timings.char_timeout, icc_timings.block_delay, icc_timings.char_delay);
|
---|
451 | }
|
---|
452 | #ifdef DEBUG_PROTOCOL
|
---|
453 | printf ("Protocol: T=%i: WWT=%d, Clockrate=%lu\n", params->t, (int)(wwt),ICC_Async_GetClockRate());
|
---|
454 | #endif
|
---|
455 | }
|
---|
456 | break;
|
---|
457 | case ATR_PROTOCOL_TYPE_T1:
|
---|
458 | {
|
---|
459 | BYTE ta, tb, tc, cwi, bwi;
|
---|
460 |
|
---|
461 | // Set IFSC
|
---|
462 | if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND)
|
---|
463 | ifsc = PROTOCOL_T1_DEFAULT_IFSC;
|
---|
464 | else if ((ta != 0x00) && (ta != 0xFF))
|
---|
465 | ifsc = ta;
|
---|
466 | else
|
---|
467 | ifsc = PROTOCOL_T1_DEFAULT_IFSC;
|
---|
468 |
|
---|
469 | // Towitoko does not allow IFSC > 251 //FIXME not sure whether this limitation still exists
|
---|
470 | ifsc = MIN (ifsc, PROTOCOL_T1_MAX_IFSC);
|
---|
471 |
|
---|
472 | // Set IFSD
|
---|
473 | ifsd = PROTOCOL_T1_DEFAULT_IFSD;
|
---|
474 |
|
---|
475 | #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
|
---|
476 | // Calculate CWI and BWI
|
---|
477 | if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND)
|
---|
478 | {
|
---|
479 | #endif
|
---|
480 | cwi = PROTOCOL_T1_DEFAULT_CWI;
|
---|
481 | bwi = PROTOCOL_T1_DEFAULT_BWI;
|
---|
482 | #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
|
---|
483 | }
|
---|
484 | else
|
---|
485 | {
|
---|
486 | cwi = tb & 0x0F;
|
---|
487 | bwi = tb >> 4;
|
---|
488 | }
|
---|
489 | #endif
|
---|
490 |
|
---|
491 | // Set CWT = (2^CWI + 11) work etu
|
---|
492 | CWT = (unsigned short) (((1<<cwi) + 11)); // in ETU
|
---|
493 |
|
---|
494 | // Set BWT = (2^BWI * 960 + 11) work etu
|
---|
495 | BWT = (unsigned short) (((1<<bwi) * 960 + 11)); //in ETU
|
---|
496 |
|
---|
497 | // Set BGT = 22 * work etu
|
---|
498 | BGT = 22L; //in ETU
|
---|
499 |
|
---|
500 | if (parameters.n == 255)
|
---|
501 | CGT = 11L; //in ETU
|
---|
502 | else
|
---|
503 | CGT = GT;
|
---|
504 |
|
---|
505 | // Set the error detection code type
|
---|
506 | if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
|
---|
507 | edc = PROTOCOL_T1_EDC_LRC;
|
---|
508 | else
|
---|
509 | edc = tc & 0x01;
|
---|
510 |
|
---|
511 | // Set initial send sequence (NS)
|
---|
512 | ns = 1;
|
---|
513 |
|
---|
514 | cs_debug ("Protocol: T=1: IFSC=%d, IFSD=%d, CWT=%d etu, BWT=%d etu, BGT=%d etu, EDC=%s\n", ifsc, ifsd, CWT, BWT, BGT, (edc == PROTOCOL_T1_EDC_LRC) ? "LRC" : "CRC");
|
---|
515 |
|
---|
516 | if (reader[ridx].typ != R_INTERNAL) {
|
---|
517 | icc_timings.block_timeout = ETU_to_ms(BWT);
|
---|
518 | icc_timings.char_timeout = ETU_to_ms(CWT);
|
---|
519 | icc_timings.block_delay = ETU_to_ms(BGT);
|
---|
520 | icc_timings.char_delay = ETU_to_ms(CGT);
|
---|
521 | cs_debug("Setting timings: block_timeout=%u ms, char_timeout=%u ms, block_delay=%u ms, char_delay=%u ms",icc_timings.block_timeout, icc_timings.char_timeout, icc_timings.block_delay, icc_timings.char_delay);
|
---|
522 | }
|
---|
523 | }
|
---|
524 | break;
|
---|
525 | default:
|
---|
526 | protocol = NULL;
|
---|
527 | return PPS_PROTOCOL_ERROR;
|
---|
528 | break;
|
---|
529 | }
|
---|
530 |
|
---|
531 | #ifdef SCI_DEV
|
---|
532 | #include <sys/ioctl.h>
|
---|
533 | #include "sci_global.h"
|
---|
534 | #include "sci_ioctl.h"
|
---|
535 | if(reader[ridx].typ == R_INTERNAL)
|
---|
536 | {
|
---|
537 | int n;
|
---|
538 | SCI_PARAMETERS params;
|
---|
539 | //memset(¶ms,0,sizeof(SCI_PARAMETERS));
|
---|
540 | if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, ¶ms) < 0 )
|
---|
541 | return PPS_ICC_ERROR;
|
---|
542 |
|
---|
543 | params.T = parameters.t;
|
---|
544 | params.fs = atr_fs_table[parameters.FI] / 1000000;
|
---|
545 | double F = (double) atr_f_table[parameters.FI];
|
---|
546 | //for Irdeto T14 cards, do not set ETU
|
---|
547 | if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && params.T == 14))
|
---|
548 | params.ETU = F / parameters.d;
|
---|
549 | params.EGT = EGT;
|
---|
550 | params.WWT = WWT;
|
---|
551 | params.BWT = BWT;
|
---|
552 | params.CWT = CWT;
|
---|
553 |
|
---|
554 | double a;
|
---|
555 | ATR_GetParameter(atr, ATR_PARAMETER_P, &a);
|
---|
556 | params.P=(unsigned char)a;
|
---|
557 | ATR_GetParameter(atr, ATR_PARAMETER_I, &a);
|
---|
558 | params.I=(unsigned char)a;
|
---|
559 |
|
---|
560 | cs_debug("Setting T=%d fs=%lu mhz ETU=%d WWT=%d CWT=%d BWT=%d EGT=%d clock=%d check=%d P=%d I=%d U=%d", (int)params.T, params.fs, (int)params.ETU, (int)params.WWT, (int)params.CWT, (int)params.BWT, (int)params.EGT, (int)params.clock_stop_polarity, (int)params.check, (int)params.P, (int)params.I, (int)params.U);
|
---|
561 |
|
---|
562 | if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, ¶ms)!=0)
|
---|
563 | return PPS_ICC_ERROR;
|
---|
564 |
|
---|
565 | }
|
---|
566 | #elif COOL
|
---|
567 | if(reader[ridx].typ == R_INTERNAL) {
|
---|
568 | int mhz = atr_fs_table[parameters.FI] / 10000;
|
---|
569 | if (!Cool_SetBaudrate(mhz))
|
---|
570 | return PPS_ICC_ERROR;
|
---|
571 | #ifdef DEBUG_PROTOCOL
|
---|
572 | printf("Coolstream: set clock to %i * 10kHz\n", mhz);
|
---|
573 | #endif
|
---|
574 | return PPS_OK;
|
---|
575 | }
|
---|
576 | #endif
|
---|
577 | {
|
---|
578 | unsigned long baudrate;
|
---|
579 | double F = (double) atr_f_table[parameters.FI];
|
---|
580 | if (parameters.t == 14)
|
---|
581 | baudrate = 9600;
|
---|
582 | else
|
---|
583 | baudrate = parameters.d * ICC_Async_GetClockRate () / F;
|
---|
584 |
|
---|
585 | #ifdef DEBUG_PROTOCOL
|
---|
586 | printf ("PPS: Baudrate = %d\n", (int)baudrate);
|
---|
587 | #endif
|
---|
588 |
|
---|
589 |
|
---|
590 | if (ICC_Async_SetBaudrate (baudrate) != ICC_ASYNC_OK)
|
---|
591 | return PPS_ICC_ERROR;
|
---|
592 |
|
---|
593 | return PPS_OK;
|
---|
594 | }
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | static BYTE PPS_GetPCK (BYTE * block, unsigned length)
|
---|
599 | {
|
---|
600 | BYTE pck;
|
---|
601 | unsigned i;
|
---|
602 |
|
---|
603 | pck = block[0];
|
---|
604 | for (i = 1; i < length; i++)
|
---|
605 | pck ^= block[i];
|
---|
606 |
|
---|
607 | return pck;
|
---|
608 | }
|
---|