-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
ifdwrapper.c
757 lines (643 loc) · 18.4 KB
/
ifdwrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*
* MUSCLE SmartCard Development ( https://www.linuxnet.com )
*
* Copyright (C) 1999-2004
* David Corcoran <[email protected]>
* Copyright (C) 2003-2004
* Damien Sauveron <[email protected]>
* Copyright (C) 2002-2010
* Ludovic Rousseau <[email protected]>
*
* $Id$
*/
/**
* @file
* @brief This wraps the dynamic ifdhandler functions.
*/
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include "config.h"
#include "misc.h"
#include "pcscd.h"
#include "ifdhandler.h"
#include "debuglog.h"
#include "readerfactory.h"
#include "ifdwrapper.h"
#include "atrhandler.h"
#include "dyn_generic.h"
#include "sys_generic.h"
#include "utils.h"
#ifdef PCSCLITE_STATIC_DRIVER
/* check that either IFDHANDLERv1, IFDHANDLERv2 or IFDHANDLERv3 is
* defined */
#if ! (defined(IFDHANDLERv1) || defined(IFDHANDLERv2) || defined(IFDHANDLERv3))
#error IFDHANDLER version not defined
#endif
#endif
/**
* Set the protocol type selection (PTS).
* This function sets the appropriate protocol to be used on the card.
*/
LONG IFDSetPTS(READER_CONTEXT * rContext, DWORD dwProtocol, UCHAR ucFlags,
UCHAR ucPTS1, UCHAR ucPTS2, UCHAR ucPTS3)
{
RESPONSECODE rv = IFD_SUCCESS;
UCHAR ucValue[1];
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFD_set_protocol_parameters) (DWORD, UCHAR, UCHAR,
UCHAR, UCHAR) = NULL;
RESPONSECODE(*IFDH_set_protocol_parameters) (DWORD, DWORD, UCHAR,
UCHAR, UCHAR, UCHAR) = NULL;
if (rContext->version == IFD_HVERSION_1_0)
{
IFD_set_protocol_parameters = (RESPONSECODE(*)(DWORD, UCHAR, UCHAR,
UCHAR, UCHAR)) rContext->psFunctions.psFunctions_v1.pvfSetProtocolParameters;
if (NULL == IFD_set_protocol_parameters)
return SCARD_E_UNSUPPORTED_FEATURE;
}
else
{
IFDH_set_protocol_parameters = (RESPONSECODE(*)(DWORD, DWORD, UCHAR,
UCHAR, UCHAR, UCHAR))
rContext->psFunctions.psFunctions_v2.pvfSetProtocolParameters;
if (NULL == IFDH_set_protocol_parameters)
return SCARD_E_UNSUPPORTED_FEATURE;
}
#endif
/*
* Locking is done in winscard.c SCardConnect() and SCardReconnect()
*
* This avoids to renegotiate the protocol and confuse the card
* Error returned by CCID driver is: CCID_Receive Procedure byte conflict
*/
ucValue[0] = rContext->slot;
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
{
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = (*IFD_set_protocol_parameters) (dwProtocol,
ucFlags, ucPTS1, ucPTS2, ucPTS3);
}
else
{
rv = (*IFDH_set_protocol_parameters) (rContext->slot,
dwProtocol, ucFlags, ucPTS1, ucPTS2, ucPTS3);
}
#else
#ifdef IFDHANDLERv1
{
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = IFD_Set_Protocol_Parameters(dwProtocol, ucFlags, ucPTS1,
ucPTS2, ucPTS3);
}
#else
rv = IFDHSetProtocolParameters(rContext->slot, dwProtocol, ucFlags,
ucPTS1, ucPTS2, ucPTS3);
#endif
#endif
return rv;
}
/**
* Open a communication channel to the IFD.
*/
LONG IFDOpenIFD(READER_CONTEXT * rContext)
{
RESPONSECODE rv = 0;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IO_create_channel) (DWORD) = NULL;
RESPONSECODE(*IFDH_create_channel) (DWORD, DWORD) = NULL;
RESPONSECODE(*IFDH_create_channel_by_name) (DWORD, LPSTR) = NULL;
if (rContext->version == IFD_HVERSION_1_0)
IO_create_channel =
rContext->psFunctions.psFunctions_v1.pvfCreateChannel;
else
if (rContext->version == IFD_HVERSION_2_0)
IFDH_create_channel =
rContext->psFunctions.psFunctions_v2.pvfCreateChannel;
else
{
IFDH_create_channel =
rContext->psFunctions.psFunctions_v3.pvfCreateChannel;
IFDH_create_channel_by_name =
rContext->psFunctions.psFunctions_v3.pvfCreateChannelByName;
}
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
{
rv = (*IO_create_channel) (rContext->port);
} else if (rContext->version == IFD_HVERSION_2_0)
{
rv = (*IFDH_create_channel) (rContext->slot, rContext->port);
} else
{
/* use device name only if defined */
if (rContext->lpcDevice[0] != '\0')
rv = (*IFDH_create_channel_by_name) (rContext->slot, rContext->lpcDevice);
else
rv = (*IFDH_create_channel) (rContext->slot, rContext->port);
}
#else
#ifdef IFDHANDLERv1
rv = IO_Create_Channel(rContext->port);
#elif defined(IFDHANDLERv2)
rv = IFDHCreateChannel(rContext->slot, rContext->port);
#else
{
/* Use device name only if defined */
if (rContext->lpcDevice[0] != '\0')
rv = IFDHCreateChannelByName(rContext->slot, rContext->lpcDevice);
else
rv = IFDHCreateChannel(rContext->slot, rContext->port);
}
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
return rv;
}
/**
* Close a communication channel to the IFD.
*/
LONG IFDCloseIFD(READER_CONTEXT * rContext)
{
RESPONSECODE rv = IFD_SUCCESS;
int repeat;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IO_close_channel) (void) = NULL;
RESPONSECODE(*IFDH_close_channel) (DWORD) = NULL;
if (rContext->version == IFD_HVERSION_1_0)
IO_close_channel = rContext->psFunctions.psFunctions_v1.pvfCloseChannel;
else
IFDH_close_channel = rContext->psFunctions.psFunctions_v2.pvfCloseChannel;
#endif
/* TRY TO LOCK THIS CODE REGION */
repeat = 5;
again:
rv = pthread_mutex_trylock(rContext->mMutex);
if (EBUSY == rv)
{
Log1(PCSC_LOG_ERROR, "Locking failed");
repeat--;
if (repeat)
{
(void)SYS_USleep(100*1000); /* 100 ms */
goto again;
}
}
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
rv = (*IO_close_channel) ();
else
rv = (*IFDH_close_channel) (rContext->slot);
#else
#ifdef IFDHANDLERv1
rv = IO_Close_Channel();
#else
rv = IFDHCloseChannel(rContext->slot);
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
return rv;
}
/**
* Set capabilities in the reader.
*/
LONG IFDSetCapabilities(READER_CONTEXT * rContext, DWORD dwTag,
DWORD dwLength, PUCHAR pucValue)
{
RESPONSECODE rv = IFD_SUCCESS;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFD_set_capabilities) (DWORD, PUCHAR) = NULL;
RESPONSECODE(*IFDH_set_capabilities) (DWORD, DWORD, DWORD, PUCHAR) = NULL;
if (rContext->version == IFD_HVERSION_1_0)
IFD_set_capabilities = rContext->psFunctions.psFunctions_v1.pvfSetCapabilities;
else
IFDH_set_capabilities = rContext->psFunctions.psFunctions_v2.pvfSetCapabilities;
#endif
/*
* Let the calling function lock this otherwise a deadlock will
* result
*/
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
rv = (*IFD_set_capabilities) (dwTag, pucValue);
else
rv = (*IFDH_set_capabilities) (rContext->slot, dwTag,
dwLength, pucValue);
#else
#ifdef IFDHANDLERv1
rv = IFD_Set_Capabilities(dwTag, pucValue);
#else
rv = IFDHSetCapabilities(rContext->slot, dwTag, dwLength, pucValue);
#endif
#endif
return rv;
}
/**
* Get's capabilities in the reader.
* Other functions int this file will call
* the driver directly to not cause a deadlock.
*/
LONG IFDGetCapabilities(READER_CONTEXT * rContext, DWORD dwTag,
PDWORD pdwLength, PUCHAR pucValue)
{
RESPONSECODE rv = IFD_SUCCESS;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFD_get_capabilities) (DWORD, /*@out@*/ PUCHAR) = NULL;
RESPONSECODE(*IFDH_get_capabilities) (DWORD, DWORD, PDWORD, /*@out@*/ PUCHAR) = NULL;
if (rContext->version == IFD_HVERSION_1_0)
IFD_get_capabilities =
rContext->psFunctions.psFunctions_v1.pvfGetCapabilities;
else
IFDH_get_capabilities =
rContext->psFunctions.psFunctions_v2.pvfGetCapabilities;
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
rv = (*IFD_get_capabilities) (dwTag, pucValue);
else
rv = (*IFDH_get_capabilities) (rContext->slot, dwTag,
pdwLength, pucValue);
#else
#ifdef IFDHANDLERv1
rv = IFD_Get_Capabilities(dwTag, pucValue);
#else
rv = IFDHGetCapabilities(rContext->slot, dwTag, pdwLength, pucValue);
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
return rv;
}
/**
* Power up/down or reset's an ICC located in the IFD.
*/
LONG IFDPowerICC(READER_CONTEXT * rContext, DWORD dwAction,
PUCHAR pucAtr, PDWORD pdwAtrLen)
{
RESPONSECODE rv;
#ifndef PCSCLITE_STATIC_DRIVER
short ret;
SMARTCARD_EXTENSION sSmartCard;
#endif
DWORD dwStatus;
UCHAR ucValue[1];
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFD_power_icc) (DWORD) = NULL;
RESPONSECODE(*IFDH_power_icc) (DWORD, DWORD, PUCHAR, PDWORD) = NULL;
#endif
/*
* Zero out everything
*/
rv = IFD_SUCCESS;
dwStatus = 0;
ucValue[0] = 0;
/*
* Check that the card is inserted first
*/
(void)IFDStatusICC(rContext, &dwStatus, pucAtr, pdwAtrLen);
if (dwStatus & SCARD_ABSENT)
return SCARD_W_REMOVED_CARD;
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
IFD_power_icc = rContext->psFunctions.psFunctions_v1.pvfPowerICC;
else
IFDH_power_icc = rContext->psFunctions.psFunctions_v2.pvfPowerICC;
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
{
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = (*IFD_power_icc) (dwAction);
}
else
{
rv = (*IFDH_power_icc) (rContext->slot, dwAction,
pucAtr, pdwAtrLen);
ret = ATRDecodeAtr(&sSmartCard, pucAtr, *pdwAtrLen);
}
#else
#ifdef IFDHANDLERv1
{
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = IFD_Power_ICC(dwAction);
}
#else
rv = IFDHPowerICC(rContext->slot, dwAction, pucAtr, pdwAtrLen);
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
/* use clean values in case of error */
if (rv != IFD_SUCCESS)
{
*pdwAtrLen = 0;
pucAtr[0] = '\0';
if (rv == IFD_NO_SUCH_DEVICE)
{
(void)SendHotplugSignal();
return SCARD_E_READER_UNAVAILABLE;
}
return SCARD_E_NOT_TRANSACTED;
}
/*
* Get the ATR and it's length
*/
if (rContext->version == IFD_HVERSION_1_0)
(void)IFDStatusICC(rContext, &dwStatus, pucAtr, pdwAtrLen);
return rv;
}
/**
* Provide statistical information about the IFD and ICC including insertions,
* atr, powering status/etc.
*/
LONG IFDStatusICC(READER_CONTEXT * rContext, PDWORD pdwStatus,
PUCHAR pucAtr, PDWORD pdwAtrLen)
{
RESPONSECODE rv = IFD_SUCCESS;
DWORD dwTag = 0, dwCardStatus = 0;
SMARTCARD_EXTENSION sSmartCard;
UCHAR ucValue[1] = "\x00";
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFD_is_icc_present) (void) = NULL;
RESPONSECODE(*IFDH_icc_presence) (DWORD) = NULL;
RESPONSECODE(*IFD_get_capabilities) (DWORD, /*@out@*/ PUCHAR) = NULL;
if (rContext->version == IFD_HVERSION_1_0)
{
IFD_is_icc_present =
rContext->psFunctions.psFunctions_v1.pvfICCPresence;
IFD_get_capabilities =
rContext->psFunctions.psFunctions_v1.pvfGetCapabilities;
}
else
IFDH_icc_presence = rContext->psFunctions.psFunctions_v2.pvfICCPresence;
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
{
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = (*IFD_is_icc_present) ();
}
else
rv = (*IFDH_icc_presence) (rContext->slot);
#else
#ifdef IFDHANDLERv1
{
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = IFD_Is_ICC_Present();
}
#else
rv = IFDHICCPresence(rContext->slot);
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
if (rv == IFD_SUCCESS || rv == IFD_ICC_PRESENT)
dwCardStatus |= SCARD_PRESENT;
else
if (rv == IFD_ICC_NOT_PRESENT)
dwCardStatus |= SCARD_ABSENT;
else
{
Log2(PCSC_LOG_ERROR, "Card not transacted: %ld", rv);
*pdwStatus = SCARD_UNKNOWN;
if (rv == IFD_NO_SUCH_DEVICE)
{
(void)SendHotplugSignal();
return SCARD_E_READER_UNAVAILABLE;
}
return SCARD_E_NOT_TRANSACTED;
}
/*
* Now lets get the ATR and process it if IFD Handler version 1.0.
* IFD Handler version 2.0 does this immediately after reset/power up
* to conserve resources
*/
if (rContext->version == IFD_HVERSION_1_0)
{
if (rv == IFD_SUCCESS || rv == IFD_ICC_PRESENT)
{
short ret;
dwTag = TAG_IFD_ATR;
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
#ifndef PCSCLITE_STATIC_DRIVER
rv = (*IFD_get_capabilities) (dwTag, pucAtr);
#else
#ifdef IFDHANDLERv1
rv = IFD_Get_Capabilities(dwTag, pucAtr);
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
/*
* FIX :: This is a temporary way to return the correct size
* of the ATR since most of the drivers return MAX_ATR_SIZE
*/
ret = ATRDecodeAtr(&sSmartCard, pucAtr, MAX_ATR_SIZE);
/*
* Might be a memory card without an ATR
*/
if (ret == 0)
*pdwAtrLen = 0;
else
*pdwAtrLen = sSmartCard.ATR.Length;
}
else
{
/*
* No card is inserted - Atr length is 0
*/
*pdwAtrLen = 0;
}
/*
* End of FIX
*/
}
*pdwStatus = dwCardStatus;
return SCARD_S_SUCCESS;
}
/*
* Function: IFDControl Purpose : This function provides a means for
* toggling a specific action on the reader such as swallow, eject,
* biometric.
*/
/*
* Valid only for IFDHandler version 2.0
*/
LONG IFDControl_v2(READER_CONTEXT * rContext, PUCHAR TxBuffer,
DWORD TxLength, PUCHAR RxBuffer, PDWORD RxLength)
{
RESPONSECODE rv = IFD_SUCCESS;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFDH_control_v2) (DWORD, PUCHAR, DWORD, /*@out@*/ PUCHAR,
PDWORD);
#endif
if (rContext->version != IFD_HVERSION_2_0)
return SCARD_E_UNSUPPORTED_FEATURE;
#ifndef PCSCLITE_STATIC_DRIVER
IFDH_control_v2 = rContext->psFunctions.psFunctions_v2.pvfControl;
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
rv = (*IFDH_control_v2) (rContext->slot, TxBuffer, TxLength,
RxBuffer, RxLength);
#elif defined(IFDHANDLERv2)
rv = IFDHControl(rContext->slot, TxBuffer, TxLength,
RxBuffer, RxLength);
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
if (rv == IFD_SUCCESS)
return SCARD_S_SUCCESS;
else
{
Log2(PCSC_LOG_ERROR, "Card not transacted: %ld", rv);
LogXxd(PCSC_LOG_DEBUG, "TxBuffer ", TxBuffer, TxLength);
LogXxd(PCSC_LOG_DEBUG, "RxBuffer ", RxBuffer, *RxLength);
return SCARD_E_NOT_TRANSACTED;
}
}
/**
* Provide a means for toggling a specific action on the reader such as
* swallow, eject, biometric.
*/
/*
* Valid only for IFDHandler version 3.0 and up
*/
LONG IFDControl(READER_CONTEXT * rContext, DWORD ControlCode,
LPCVOID TxBuffer, DWORD TxLength, LPVOID RxBuffer, DWORD RxLength,
LPDWORD BytesReturned)
{
RESPONSECODE rv = IFD_SUCCESS;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFDH_control) (DWORD, DWORD, LPCVOID, DWORD, LPVOID, DWORD, LPDWORD);
#endif
if (rContext->version < IFD_HVERSION_3_0)
return SCARD_E_UNSUPPORTED_FEATURE;
#ifndef PCSCLITE_STATIC_DRIVER
IFDH_control = rContext->psFunctions.psFunctions_v3.pvfControl;
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
rv = (*IFDH_control) (rContext->slot, ControlCode, TxBuffer,
TxLength, RxBuffer, RxLength, BytesReturned);
#elif defined(IFDHANDLERv3)
rv = IFDHControl(rContext->slot, ControlCode, TxBuffer,
TxLength, RxBuffer, RxLength, BytesReturned);
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
if (rv == IFD_SUCCESS)
return SCARD_S_SUCCESS;
else
{
Log2(PCSC_LOG_ERROR, "Card not transacted: %ld", rv);
Log3(PCSC_LOG_DEBUG, "ControlCode: 0x%.8LX BytesReturned: %ld",
ControlCode, *BytesReturned);
LogXxd(PCSC_LOG_DEBUG, "TxBuffer ", TxBuffer, TxLength);
LogXxd(PCSC_LOG_DEBUG, "RxBuffer ", RxBuffer, *BytesReturned);
if (rv == IFD_NO_SUCH_DEVICE)
{
(void)SendHotplugSignal();
return SCARD_E_READER_UNAVAILABLE;
}
if ((IFD_ERROR_NOT_SUPPORTED == rv) || (IFD_NOT_SUPPORTED == rv))
return SCARD_E_UNSUPPORTED_FEATURE;
if (IFD_ERROR_INSUFFICIENT_BUFFER ==rv)
return SCARD_E_INSUFFICIENT_BUFFER;
return SCARD_E_NOT_TRANSACTED;
}
}
/**
* Transmit an APDU to the ICC.
*/
LONG IFDTransmit(READER_CONTEXT * rContext, SCARD_IO_HEADER pioTxPci,
PUCHAR pucTxBuffer, DWORD dwTxLength, PUCHAR pucRxBuffer,
PDWORD pdwRxLength, PSCARD_IO_HEADER pioRxPci)
{
RESPONSECODE rv = IFD_SUCCESS;
#ifndef PCSCLITE_STATIC_DRIVER
RESPONSECODE(*IFD_transmit_to_icc) (SCARD_IO_HEADER, PUCHAR, DWORD,
/*@out@*/ PUCHAR, PDWORD, PSCARD_IO_HEADER) = NULL;
RESPONSECODE(*IFDH_transmit_to_icc) (DWORD, SCARD_IO_HEADER, PUCHAR,
DWORD, /*@out@*/ PUCHAR, PDWORD, PSCARD_IO_HEADER) = NULL;
#endif
/* log the APDU */
DebugLogCategory(DEBUG_CATEGORY_APDU, pucTxBuffer, dwTxLength);
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
IFD_transmit_to_icc =
rContext->psFunctions.psFunctions_v1.pvfTransmitToICC;
else
IFDH_transmit_to_icc =
rContext->psFunctions.psFunctions_v2.pvfTransmitToICC;
#endif
/* LOCK THIS CODE REGION */
(void)pthread_mutex_lock(rContext->mMutex);
#ifndef PCSCLITE_STATIC_DRIVER
if (rContext->version == IFD_HVERSION_1_0)
{
UCHAR ucValue[1];
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = (*IFD_transmit_to_icc) (pioTxPci, (LPBYTE) pucTxBuffer,
dwTxLength, pucRxBuffer, pdwRxLength, pioRxPci);
}
else
rv = (*IFDH_transmit_to_icc) (rContext->slot, pioTxPci,
(LPBYTE) pucTxBuffer, dwTxLength,
pucRxBuffer, pdwRxLength, pioRxPci);
#else
#ifdef IFDHANDLERv1
{
UCHAR ucValue[1];
ucValue[0] = rContext->slot;
(void)IFDSetCapabilities(rContext, TAG_IFD_SLOTNUM, 1, ucValue);
rv = IFD_Transmit_to_ICC(pioTxPci, (LPBYTE) pucTxBuffer,
dwTxLength, pucRxBuffer, pdwRxLength, pioRxPci);
}
#else
rv = IFDHTransmitToICC(rContext->slot, pioTxPci,
(LPBYTE) pucTxBuffer, dwTxLength,
pucRxBuffer, pdwRxLength, pioRxPci);
#endif
#endif
/* END OF LOCKED REGION */
(void)pthread_mutex_unlock(rContext->mMutex);
/* log the returned status word */
DebugLogCategory(DEBUG_CATEGORY_SW, pucRxBuffer, *pdwRxLength);
if (rv == IFD_SUCCESS)
return SCARD_S_SUCCESS;
else
{
Log2(PCSC_LOG_ERROR, "Card not transacted: %ld", rv);
if (rv == IFD_NO_SUCH_DEVICE)
{
(void)SendHotplugSignal();
return SCARD_E_READER_UNAVAILABLE;
}
return SCARD_E_NOT_TRANSACTED;
}
}