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