source: branches/monitor-improvement/csctapi/pps.c@ 1194

Last change on this file since 1194 was 1194, checked in by alno, 12 years ago

WebIf:

  • Merging revisions 1192-1193 of trunk
  • whitespaces
File size: 16.1 KB
Line 
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
34/*
35 * Not exported constants definition
36 */
37
38#define PPS_DEFAULT_PROTOCOL 0x00
39
40/*
41 * Not exported macros definition
42 */
43
44#define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
45#define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
46#define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
47
48/*
49 * Not exported funtions declaration
50 */
51
52static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length);
53
54static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply);
55
56static unsigned PPS_GetLength (BYTE * block);
57
58static int PPS_InitICC (PPS * pps);
59
60static int PPS_InitProtocol (PPS * pps, int protocol_selected);
61
62static BYTE PPS_GetPCK (BYTE * block, unsigned length);
63
64/*
65 * Exported functions definition
66 */
67
68PPS * PPS_New (ICC_Async * icc)
69{
70 PPS *pps;
71
72 pps = (PPS *) malloc (sizeof (PPS));
73
74 if (pps != NULL)
75 {
76 pps->icc = icc;
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
87int PPS_Perform (PPS * pps, BYTE * params, unsigned *length)
88{
89 //Performs PPS Exchange on command when *length >0
90 //If unsuccesfull, or when length <= 0
91 //Gets parametes from ATR
92 //If necessary perform PPS session
93 //
94 //Output is pps->params.FI,n,d,t set with correct values
95 //and switched SC-device conform these values to correct baudrate
96 //
97 //We need to store FI instread of F, because SCI_DEV works with FI
98 //and it is easier to overclock then
99 //also from FI -> F is easy, other way around not
100
101 ATR *atr;
102 int ret;
103 bool PPS_success;
104
105 /* Perform PPS Exchange if requested by command */
106 if ((*length) > 0)
107 {
108 ret = PPS_Exchange (pps, params, length);
109
110 /* Get parameters from PPS handsake */
111 if (ret == PPS_OK)
112 {
113 pps->parameters.t = params[1] & 0x0F;
114
115 if (PPS_HAS_PPS1 (params))
116 {
117 pps->parameters.FI = (params[2] >> 4);
118 pps->parameters.d = atr_d_table[(params[2] & 0x0F)];
119 }
120
121/*
122 ret = PPS_InitICC(pps);
123
124 if (ret != PPS_OK)
125 return ret;
126*/
127 }
128/*
129 else
130 {
131 return ret;
132 }
133*/
134 }
135 PPS_success = PPS_OK;
136 int protocol_selected = 0; //stores which TAi,TBi etc. bytes must be used 0 means not set
137 atr = ICC_Async_GetAtr (pps->icc);
138 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
139 {
140 int numprot = atr->pn-1;//number of protocol lines in ATR
141 BYTE tx;
142 cs_debug("ATR reports %i protocol lines:",numprot);
143 int i,point;
144 char txt[50];
145 bool OffersT[3]; //T14 stored as T2
146 for (i = 0; i <= 2; i++)
147 OffersT[i] = FALSE;
148 for (i=1; i<= numprot; i++) {
149 point = 0;
150 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
151 sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
152 point +=7;
153 }
154 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
155 sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
156 point +=7;
157 }
158 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
159 sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
160 point +=7;
161 }
162 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
163 sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
164 point +=7;
165 tx &= 0X0F;
166 sprintf((char *)txt+point,"(T%i)",tx);
167 if (tx == 14)
168 OffersT[2] = TRUE;
169 else
170 OffersT[tx] = TRUE;
171 }
172 else {
173 sprintf((char *)txt+point,"no TD%i means T0",i);
174 OffersT[0] = TRUE;
175 }
176 cs_debug("%s",txt);
177 }
178
179 int numprottype = 0;
180 for (i = 0; i <= 2; i++)
181 if (OffersT[i])
182 numprottype ++;
183 cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
184
185//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.
186 ATR_GetParameter (atr, ATR_PARAMETER_N, &(pps->parameters.n));
187 ATR_GetProtocolType(atr,1,&(pps->parameters.t)); //get protocol from TD1
188 BYTE TA2;
189 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
190 if (SpecificMode) {
191 pps->parameters.t = TA2 & 0x0F;
192 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
193 BYTE TA1;
194 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
195 pps->parameters.FI = TA1 >> 4;
196 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d));
197 }
198 else {
199 pps->parameters.FI = ATR_DEFAULT_FI;
200 pps->parameters.d = ATR_DEFAULT_D;
201 }
202 }
203 else {
204 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
205 pps->parameters.FI = ATR_DEFAULT_FI;
206 pps->parameters.d = ATR_DEFAULT_D;
207 }
208 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
209 }
210 else { //negotiable mode
211
212 bool NeedsPTS = ((pps->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 PTS
213 if (NeedsPTS) {
214 // PTSS PTS0 PTS1 PTS2 PTS3 PCK
215 // PTSS PTS0 PTS1 PCK
216 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
217 req[1]=0x10 | pps->parameters.t; //PTS0 always flags PTS1 to be sent always
218 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
219 req[2] = 0x11; //defaults FI and DI to 1
220 //req[3]=PPS_GetPCK(req,sizeof(req)-1); will be set by PPS_Exchange
221 unsigned int len = sizeof(req);
222 ret = PPS_Exchange (pps, req, &len);
223 if (ret == PPS_OK) {
224 pps->parameters.FI = req[2] >> 4;
225 BYTE DI = req[2] & 0x0F;
226 pps->parameters.d = (double) (atr_d_table[DI]);
227 PPS_success = TRUE;
228 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
229 }
230 else
231 cs_ddump(req,4,"PTS Failure, response:");
232 }
233
234 //FIXME Currently InitICC sets baudrate to 9600 for all T14 cards (=no switching);
235 //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)
236 if (!PPS_success) {//last PPS not succesfull
237 BYTE TA1;
238 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
239 pps->parameters.FI = TA1 >> 4;
240 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d));
241 }
242 else { //do not obey TA1
243 pps->parameters.FI = ATR_DEFAULT_FI;
244 pps->parameters.d = ATR_DEFAULT_D;
245 }
246 ATR_GetProtocolType (atr, 1, &(pps->parameters.t));
247 protocol_selected = 1;
248
249 if (NeedsPTS) {
250 if ((pps->parameters.d == 32) || (pps->parameters.d == 12) || (pps->parameters.d == 20))
251 pps->parameters.d = 0; //behave conform "old" atr_d_table; viaccess cards that fail PTS need this
252 }
253 /////Here all non-ISO behaviour
254 /////End all non-ISO behaviour
255
256 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
257 }
258 }//end negotiable mode
259 }//end length<0
260
261 //make sure no zero values
262 double F = (double) atr_f_table[pps->parameters.FI];
263 if (!F) {
264 pps->parameters.FI = ATR_DEFAULT_FI;
265 cs_log("Warning: F=0 is invalid, forcing FI=%d", pps->parameters.FI);
266 }
267 if (!pps->parameters.d) {
268 pps->parameters.d = ATR_DEFAULT_D;
269 cs_log("Warning: D=0 is invalid, forcing D=%.0f",pps->parameters.d);
270 }
271
272 pps->icc->protocol_type = pps->parameters.t;
273
274#ifdef DEBUG_PROTOCOL
275 printf("PPS: T=%i, F=%.0f, D=%.6f, N=%.0f\n",
276 pps->parameters.t,
277 F,
278 pps->parameters.d,
279 pps->parameters.n);
280#endif
281
282 ret = PPS_InitICC(pps);
283
284 if (ret != PPS_OK)
285 return ret;
286
287 /* Initialize selected protocol with selected parameters */
288 if (pps->parameters.t == 1)
289 ret = PPS_InitProtocol (pps, 3); //FIXME in practice most T1 cards carry timing parameters in TA3, TB3 and TC3
290 else
291 ret = PPS_InitProtocol (pps, 2); //FIXME T0 cards carry timing parameters in TC2
292
293 return ret;
294}
295
296void * PPS_GetProtocol (PPS * pps)
297{
298 return pps->protocol;
299}
300
301PPS_ProtocolParameters *PPS_GetProtocolParameters (PPS * pps)
302{
303 /* User must Remember not to reference this struct after removing PPS */
304 return &(pps->parameters);
305}
306
307void PPS_Delete (PPS * pps)
308{
309 free (pps);
310}
311
312/*
313 * Not exported funtions definition
314 */
315
316static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length)
317{
318 BYTE confirm[PPS_MAX_LENGTH];
319 unsigned len_request, len_confirm;
320 int ret;
321#ifdef DEBUG_PROTOCOL
322 int i;
323#endif
324
325 len_request = PPS_GetLength (params);
326 params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
327
328#ifdef DEBUG_PROTOCOL
329 printf ("PPS: Sending request: ");
330 for (i = 0; i < len_request; i++)
331 printf ("%X ", params[i]);
332 printf ("\n");
333#endif
334
335 cs_debug("PTS: Sending request: %s", cs_hexdump(1, params, len_request));
336
337 /* Send PPS request */
338#ifdef COOL
339 //unsigned char ptsAck[10];
340 //u_int8 ptsLen = len_request;
341 unsigned short int ptsLen = len_request;
342 int Status = cnxt_smc_start_pps(handle, params, confirm, &ptsLen, TRUE);
343 printf ("cnxt_smc_start_pps Status=%i\n", Status);
344 len_confirm = ptsLen;
345#ifdef DEBUG_PROTOCOL
346 printf("COOL: confirm: \n");
347 for (i = 0; i < ptsLen; i++)
348 printf ("%02X", confirm[i]);
349 printf ("\n");
350 fflush(stdout);
351 printf("COOL: req: \n");
352 for (i = 0; i < len_request; i++)
353 printf ("%02X", params[i]);
354 printf ("\n");
355 fflush(stdout);
356#endif
357 if (Status)
358 return PPS_HANDSAKE_ERROR;
359#else
360 if (ICC_Async_Transmit (pps->icc, len_request, params) != ICC_ASYNC_OK)
361 return PPS_ICC_ERROR;
362
363 /* Get PPS confirm */
364 if (ICC_Async_Receive (pps->icc, 2, confirm) != ICC_ASYNC_OK)
365 return PPS_ICC_ERROR;
366
367 len_confirm = PPS_GetLength (confirm);
368
369 if (ICC_Async_Receive (pps->icc, len_confirm - 2, confirm + 2) != ICC_ASYNC_OK)
370 return PPS_ICC_ERROR;
371
372#ifdef DEBUG_PROTOCOL
373 printf ("PPS: Receivig confirm: ");
374 for (i = 0; i < len_confirm; i++)
375 printf ("%X ", confirm[i]);
376 printf ("\n");
377#endif
378
379 cs_debug("PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
380
381 if (!PPS_Match (params, len_request, confirm, len_confirm))
382 ret = PPS_HANDSAKE_ERROR;
383 else
384 ret = PPS_OK;
385#endif
386
387 /* Copy PPS handsake */
388 memcpy (params, confirm, len_confirm);
389 (*length) = len_confirm;
390
391 return ret;
392}
393
394static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm)
395{
396 /* See if the reply differs from request */
397 if ((len_request != len_confirm) || (memcmp (request, confirm, len_request)))
398 {
399 /* See if the card specifies other than default FI and D */
400 //if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2]))
401 return FALSE;
402 }
403
404 return TRUE;
405}
406
407static unsigned PPS_GetLength (BYTE * block)
408{
409 unsigned length = 3;
410
411 if (PPS_HAS_PPS1 (block))
412 length++;
413
414 if (PPS_HAS_PPS2 (block))
415 length++;
416
417 if (PPS_HAS_PPS3 (block))
418 length++;
419
420 return length;
421}
422
423static int PPS_InitICC (PPS * pps)
424{
425#ifdef SCI_DEV
426#include <sys/ioctl.h>
427#include "sci_global.h"
428#include "sci_ioctl.h"
429 if(pps->icc->ifd->io->com==RTYP_SCI)
430 {
431 int n;
432 SCI_PARAMETERS params;
433 //memset(&params,0,sizeof(SCI_PARAMETERS));
434 if (ioctl(pps->icc->ifd->io->fd, IOCTL_GET_PARAMETERS, &params) < 0 )
435 return PPS_ICC_ERROR;
436
437 ATR *atr = ICC_Async_GetAtr (pps->icc);
438
439 params.T = pps->parameters.t;
440 params.fs = atr_fs_table[pps->parameters.FI] / 1000000;
441 double F = (double) atr_f_table[pps->parameters.FI];
442 //for Irdeto T14 cards, do not set ETU
443 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && params.T == 14))
444 params.ETU = F / pps->parameters.d;
445 if (pps->parameters.n == 255) //only for T0 or also for T1?
446 params.EGT = 0;
447 else
448 params.EGT = pps->parameters.n;
449
450 double a;
451 ATR_GetParameter(atr, ATR_PARAMETER_P, &a);
452 params.P=(unsigned char)a;
453 ATR_GetParameter(atr, ATR_PARAMETER_I, &a);
454 params.I=(unsigned char)a;
455
456 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);
457
458 if (ioctl(pps->icc->ifd->io->fd, IOCTL_SET_PARAMETERS, &params)!=0)
459 return PPS_ICC_ERROR;
460
461 }
462#elif COOL
463 if(pps->icc->ifd->io->com==RTYP_SCI) {
464 int mhz = atr_fs_table[pps->parameters.FI] / 10000;
465 if (!Cool_SetBaudrate(mhz))
466 return PPS_ICC_ERROR;
467#ifdef DEBUG_PROTOCOL
468 printf("Coolstream: set clock to %i * 10kHz\n", mhz);
469#endif
470 return PPS_OK;
471 }
472#endif
473 {
474 unsigned long baudrate;
475 double F = (double) atr_f_table[pps->parameters.FI];
476 if (pps->parameters.t == 14)
477 baudrate = 9600;
478 else
479 baudrate = pps->parameters.d * ICC_Async_GetClockRate (pps->icc) / F;
480
481#ifdef DEBUG_PROTOCOL
482 printf ("PPS: Baudrate = %d\n", (int)baudrate);
483#endif
484
485
486 if (ICC_Async_SetBaudrate (pps->icc, baudrate) != ICC_ASYNC_OK)
487 return PPS_ICC_ERROR;
488
489 return PPS_OK;
490 }
491}
492
493static 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)
502 {
503 ret = Protocol_T0_Init ((Protocol_T0 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters), selected_protocol);
504
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, (ICC_Async *) pps->icc, 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, (ICC_Async *) pps->icc, &(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;
557}
558
559static BYTE PPS_GetPCK (BYTE * block, unsigned length)
560{
561 BYTE pck;
562 unsigned i;
563
564 pck = block[0];
565 for (i = 1; i < length; i++)
566 pck ^= block[i];
567
568 return pck;
569}
Note: See TracBrowser for help on using the repository browser.