-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
pcsc_scan.c
973 lines (826 loc) · 21.3 KB
/
pcsc_scan.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*
Scan and print all the PC/SC devices available
Copyright (C) 2001-2024 Ludovic Rousseau <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#ifdef HAVE_SYSEXITS_H
#include <sysexits.h>
#else
#define EX_OK 0 /* successful termination */
#define EX_OSERR 71 /* system error (e.g., can't fork) */
#define EX_USAGE 64 /* command line usage error */
#endif
#include <sys/time.h>
#include <pthread.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#else
#include <winscard.h>
#endif
#define TIMEOUT 3600*1000 /* 1 hour timeout */
#ifndef SCARD_E_NO_READERS_AVAILABLE
#define SCARD_E_NO_READERS_AVAILABLE 0x8010002E
#endif
#ifdef WIN32
const char *pcsc_stringify_error(DWORD rv)
{
static char buffer[1024];
if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
rv,
LANG_USER_DEFAULT,
buffer,
sizeof buffer,
NULL))
{
/* if FormatMessage() failed */
snprintf(buffer, sizeof(buffer), "ERROR: 0x%08lX\n", rv);
}
return buffer;
}
#define MAX_ATR_SIZE 33
#endif
#define print_pcsc_error(fct, rv) \
fprintf(stderr, "%s%s: %s%s\n", red, fct, pcsc_stringify_error(rv), color_end)
#define test_rv(fct, rv, hContext) \
do { \
if (rv != SCARD_S_SUCCESS) \
{ \
print_pcsc_error(fct, rv); \
(void)SCardReleaseContext(hContext); \
return -1; \
} \
} while(0)
static void usage(const char *pname)
{
printf("%s usage:\n\n\t%s [ -h | -V | -n | -r | -c | -s | -t secs | -d | -p]\n\n", pname, pname);
printf("\t\t -h : this help\n");
printf("\t\t -V : print version number\n");
printf("\t\t -n : no ATR analysis\n");
printf("\t\t -r : only lists readers\n");
printf("\t\t -c : only lists cards\n");
printf("\t\t -s : stress mode\n");
printf("\t\t -q : quiet mode\n");
printf("\t\t -v : verbose mode (default)\n");
printf("\t\t -t secs : quit after secs seconds\n");
printf("\t\t -d : debug mode\n");
printf("\t\t -p : force use of PnP mechanism\n");
printf("\n");
}
const char *blue = "";
const char *red = "";
const char *magenta = "";
const char *color_end = "";
const char *cub2 = "";
const char *cub3 = "";
const char *cpl = "";
const char *cnl = "";
time_t start_time;
_Atomic bool Interrupted = false;
typedef struct
{
const char *pname;
bool analyse_atr;
bool stress_card;
bool print_version;
bool verbose;
bool only_list_readers;
bool only_list_cards;
bool debug;
bool pnp;
long maxtime; // in seconds
} options_t;
static options_t Options;
SCARDCONTEXT hContext;
static bool is_member(const char * item, const char * list[])
{
int i = 0;
while (list[i] && 0 != strcmp(item, list[i]))
{
i++;
}
return list[i] != 0;
}
static void initialize_terminal(void)
{
const char *color_terms[] = { "linux", "xterm", "xterm-color",
"xterm-256color", "Eterm", "rxvt", "rxvt-unicode", "cygwin",
"wsvt25", 0};
const char *no_ansi_cursor_terms[] = {"dumb", "emacs", 0};
const char *term = getenv("TERM");
/* do not use color if stdout is redirected */
if (!isatty(fileno(stdout)) || (NULL == term))
{
term = "dumb";
}
if (is_member(term, color_terms))
{
blue = "\033[34m";
red = "\033[31m";
magenta = "\033[35m";
color_end = "\033[0m";
}
if (is_member(term, no_ansi_cursor_terms))
{
cub2 = "\r"; /* use carriage return */
cub3 = "\r";
cpl = "\n"; /* can't do previous line, let's go to the next line. */
cnl = "\n";
}
else
{
cub2 = "\033[2D"; /* move back 2 characters */
cub3 = "\033[3D"; /* move back 3 characters */
cpl = "\033[";
}
}
/* There should be no \033 beyond this line! */
static bool should_exit(void)
{
if (Options.maxtime)
{
time_t t = time(NULL);
if (t - start_time > Options.maxtime)
return true;
}
if (Interrupted)
return true;
return false;
}
typedef enum
{
SpinDisabled = -2,
SpinStopped = -1,
SpinRunning = 0
} SpinState_t;
_Atomic SpinState_t spin_state = SpinStopped;
static void spin_start(void)
{
spin_state = Options.verbose ? SpinRunning : SpinDisabled;
printf("%s", cnl);
}
static void spin_stop(void)
{
spin_state = SpinStopped;
/* clean previous output */
printf("%s %s ", cub2, cub2);
fflush(stdout);
}
static void *spin_update(void *p)
{
char patterns[] = {'-', '\\', '|', '/'};
/* 100 ms wait */
struct timespec wait_time = {.tv_sec = 0, .tv_nsec = 1000*1000*100};
(void)p;
again:
/* wait until spinning starts */
do
{
if (should_exit())
{
if (SpinDisabled == spin_state)
{
SCardCancel(hContext);
pthread_exit(NULL);
}
}
nanosleep(&wait_time, NULL);
} while (spin_state < 0);
do
{
char c = patterns[spin_state];
if (should_exit())
{
SCardCancel(hContext);
pthread_exit(NULL);
}
spin_state++;
if (spin_state >= (int)sizeof patterns)
spin_state = SpinRunning;
printf("%s %c ", cub3, c);
fflush(stdout);
nanosleep(&wait_time, NULL);
} while (spin_state >= SpinRunning);
goto again;
return NULL;
}
static void user_interrupt_signal_handler(int signal)
{
(void)signal;
Interrupted = true;
}
static void initialize_signal_handlers(void)
{
signal(SIGINT, user_interrupt_signal_handler);
}
static void initialize_options(options_t *options, const char *pname)
{
options->pname = pname;
#if defined(ATR_PARSER)
options->analyse_atr = true;
#else
options->analyse_atr = false;
#define ATR_PARSER ""
#endif
options->stress_card = false;
options->print_version = false;
options->verbose = true;
options->only_list_readers = false;
options->only_list_cards = false;
options->debug = false;
options->pnp = false;
options->maxtime = 0;
}
#define OPTIONS "Vhvrcst:qdpn"
static void print_version(void)
{
printf("PC/SC device scanner\n");
printf("V %s (c) 2001-2024, Ludovic Rousseau <[email protected]>\n",
PACKAGE_VERSION);
}
static int parse_options(int argc, char *argv[], options_t *options)
{
const char *pname = argv[0];
int opt;
initialize_options(options, pname);
while ((opt = getopt(argc, argv, OPTIONS)) != EOF)
{
switch (opt)
{
case 'n':
options->analyse_atr = false;
break;
case 'V':
print_version();
exit(EX_OK);
break;
case 'v':
options->verbose = true;
break;
case 'r':
options->only_list_readers = true;
options->verbose = false;
break;
case 'c':
options->only_list_cards = true;
options->verbose = false;
options->analyse_atr = false;
break;
case 's':
options->stress_card = true;
break;
case 't':
options->maxtime = atol(optarg);
break;
case 'q':
options->verbose = false;
break;
case 'p':
options->pnp = true;
break;
case 'h':
usage(pname);
exit(EX_OK);
case 'd':
options->debug = true;
break;
default:
usage(pname);
exit(EX_USAGE);
}
}
if (argc - optind != 0)
{
fprintf(stderr, "%s error: superfluous arguments: %s\n", pname, argv[optind]);
usage(pname);
exit(EX_USAGE);
}
return EX_OK;
}
static LONG stress(SCARDCONTEXT hContext2, const char *readerName)
{
LONG rv, ret_rv = SCARD_S_SUCCESS;
SCARDHANDLE hCard;
DWORD dwActiveProtocol;
const SCARD_IO_REQUEST *pioSendPci;
if (Options.verbose)
{
printf("Stress card in reader: %s\n\n", readerName);
}
else
printf("\n");
rv = SCardConnect(hContext2, readerName, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
if (rv != SCARD_S_SUCCESS)
{
print_pcsc_error("SCardConnect", rv);
return rv;
}
/* Select Master File */
BYTE pbSendBuffer[] = {0, 0xA4, 0, 0, 2, 0x3F, 0};
BYTE pbRecvBuffer[256+2];
DWORD dwSendLength, dwRecvLength;
struct timeval time_start, time_end;
switch (dwActiveProtocol)
{
case SCARD_PROTOCOL_T0:
pioSendPci = SCARD_PCI_T0;
break;
case SCARD_PROTOCOL_T1:
pioSendPci = SCARD_PCI_T1;
break;
case SCARD_PROTOCOL_RAW:
pioSendPci = SCARD_PCI_RAW;
break;
default:
fprintf(stderr, "Unknown protocol\n");
return -1;
}
gettimeofday(&time_start, NULL);
#define COUNT 100
int count;
for (count=0; count<COUNT; count++)
{
printf("%sFAPDU n°: %d\n", cpl, count);
dwSendLength = sizeof(pbSendBuffer);
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, pioSendPci, pbSendBuffer, dwSendLength,
NULL, pbRecvBuffer, &dwRecvLength);
if (rv != SCARD_S_SUCCESS)
{
print_pcsc_error("SCardTransmit", rv);
ret_rv = rv;
break;
}
if (Interrupted)
exit(EX_OSERR);
}
gettimeofday(&time_end, NULL);
struct timeval r;
r.tv_sec = time_end.tv_sec - time_start.tv_sec;
r.tv_usec = time_end.tv_usec - time_start.tv_usec;
if (r.tv_usec < 0)
{
r.tv_sec--;
r.tv_usec += 1000000;
}
long delta = r.tv_sec * 1000000 + r.tv_usec;
printf("Total time: %ld µs\n", delta);
if (0 == count)
count = 1;
printf("%f APDU/s\n", 1000000. / (delta / count));
rv = SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
if (rv != SCARD_S_SUCCESS)
{
print_pcsc_error("SCardDisconnect", rv);
ret_rv = rv;
}
return ret_rv;
}
static void print_readers(const char **readers, int nbReaders)
{
int i = 0;
for (i = 0;i < nbReaders; i++)
{
printf("%s%d: %s%s\n", blue, i, readers[i], color_end);
}
}
static void displayChangedStatus(SCARD_READERSTATE rgReaderStates[], int count)
{
char state_bits[][sizeof "UNAVAILABLE"] = {
"IGNORE", /* 0x0001 */
"CHANGED", /* 0x0002 */
"UNKNOWN", /* 0x0004 */
"UNAVAILABLE", /* 0x0008 */
"EMPTY", /* 0x0010 */
"PRESENT", /* 0x0020 */
"ATRMATCH", /* 0x0040 */
"EXCLUSIVE", /* 0x0080 */
"INUSE", /* 0x0100 */
"MUTE", /* 0x0200 */
"UNPOWERED" /* 0x0400 */
};
printf("\n");
for (int i=0; i<count; i++)
{
SCARD_READERSTATE r = rgReaderStates[i];
printf("%d: %s, %d, 0x%04X -> 0x%04X",
i,
r.szReader,
(int)(r.dwEventState >> 16),
(int)(r.dwCurrentState & 0xFFFF),
(int)(r.dwEventState & 0xFFFF));
for (int b=0; b<11; b++)
{
int v = 1 << b;
if ((r.dwEventState & v) && (r.dwCurrentState & v))
printf(" =%s", state_bits[b]);
if ((r.dwEventState & v) && !(r.dwCurrentState & v))
printf(" %s+%s%s", blue, state_bits[b], color_end);
if (!(r.dwEventState & v) && (r.dwCurrentState & v))
printf(" %s-%s%s", red, state_bits[b], color_end);
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
int current_reader;
#ifdef __APPLE__
uint32_t rv;
#else
LONG rv;
#endif
SCARD_READERSTATE *rgReaderStates_t = NULL;
SCARD_READERSTATE rgReaderStates[1] = { 0, };
DWORD dwReaders = 0, dwReadersOld;
LPSTR mszReaders = NULL;
char *ptr = NULL;
const char **readers = NULL;
int nbReaders, i;
char atr[MAX_ATR_SIZE*3+1]; /* ATR in ASCII */
char atr_command[sizeof(atr)+sizeof(ATR_PARSER)+2+1];
pthread_t spin_pthread;
start_time = time(NULL);
initialize_terminal();
if (0 != parse_options(argc, argv, &Options))
{
exit(EX_USAGE);
}
if (Options.verbose)
{
print_version();
}
initialize_signal_handlers();
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
test_rv("SCardEstablishContext", rv, hContext);
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
rv = SCardGetStatusChange(hContext, 0, rgReaderStates, 1);
if (! Options.pnp && rgReaderStates[0].dwEventState & SCARD_STATE_UNKNOWN)
{
if (Options.verbose)
{
printf("%sPlug'n play reader name not supported. Using polling every %d ms.%s\n", magenta, TIMEOUT, color_end);
}
}
else
{
Options.pnp = true;
if (Options.verbose)
{
printf("%sUsing reader plug'n play mechanism%s\n", magenta, color_end);
}
}
/* start spining thread */
rv = pthread_create(&spin_pthread, NULL, spin_update, NULL);
get_readers:
/* free memory possibly allocated in a previous loop */
if (NULL != readers)
{
free(readers);
readers = NULL;
}
if (NULL != rgReaderStates_t)
{
free(rgReaderStates_t);
rgReaderStates_t = NULL;
}
/* Retrieve the available readers list.
*
* 1. Call with a null buffer to get the number of bytes to allocate
* 2. malloc the necessary storage
* 3. call with the real allocated buffer
*/
if (Options.verbose)
{
printf("%sScanning present readers...%s\n", red, color_end);
}
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (rv != SCARD_E_NO_READERS_AVAILABLE)
test_rv("SCardListReaders", rv, hContext);
dwReadersOld = dwReaders;
/* if non NULL we came back so free first */
if (mszReaders)
{
free(mszReaders);
mszReaders = NULL;
}
mszReaders = malloc(sizeof(char)*dwReaders);
if (mszReaders == NULL)
{
fprintf(stderr, "%s: malloc: not enough memory\n", Options.pname);
exit(EX_OSERR);
}
*mszReaders = '\0';
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
/* Extract readers from the null separated string and get the total
* number of readers */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
ptr += strlen(ptr)+1;
nbReaders++;
}
if (SCARD_E_NO_READERS_AVAILABLE == rv || 0 == nbReaders)
{
if (Options.only_list_cards || Options.only_list_readers)
{
printf("No reader found.\n");
(void)SCardReleaseContext(hContext);
return EX_OK;
}
if (Options.verbose)
{
printf("%sWaiting for the first reader...%s ", red, color_end);
fflush(stdout);
}
if (Options.pnp)
{
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
spin_start();
do
{
rv = SCardGetStatusChange(hContext, TIMEOUT, rgReaderStates, 1);
}
while (SCARD_E_TIMEOUT == rv);
spin_stop();
test_rv("SCardGetStatusChange", rv, hContext);
if (Options.debug)
displayChangedStatus(rgReaderStates, 1);
}
else
{
rv = SCARD_S_SUCCESS;
spin_start();
while ((SCARD_S_SUCCESS == rv) && (dwReaders == dwReadersOld))
{
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (SCARD_E_NO_READERS_AVAILABLE == rv)
rv = SCARD_S_SUCCESS;
sleep(1);
if (should_exit())
{
printf("\n");
exit(EX_OK);
}
}
spin_stop();
}
if (Options.verbose)
{
printf("found one\n");
}
goto get_readers;
}
else
test_rv("SCardListReader", rv, hContext);
/* allocate the readers table */
readers = calloc(nbReaders+1, sizeof(char *));
if (NULL == readers)
{
fprintf(stderr, "%s: Not enough memory for readers table\n", Options.pname);
exit(EX_OSERR);
}
/* fill the readers table */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
readers[nbReaders] = ptr;
ptr += strlen(ptr)+1;
nbReaders++;
}
if (! Options.only_list_cards)
print_readers(readers, nbReaders);
if (Options.only_list_readers)
{
(void)SCardReleaseContext(hContext);
exit(EX_OK);
}
/* allocate the ReaderStates table */
rgReaderStates_t = calloc(nbReaders+1, sizeof(* rgReaderStates_t));
if (NULL == rgReaderStates_t)
{
fprintf(stderr, "%s: Not enough memory for readers states\n", Options.pname);
(void)SCardReleaseContext(hContext);
exit(EX_OSERR);
}
/* Set the initial states to something we do not know
* The loop below will include this state to the dwCurrentState
*/
for (i=0; i<nbReaders; i++)
{
rgReaderStates_t[i].szReader = readers[i];
rgReaderStates_t[i].dwCurrentState = SCARD_STATE_UNAWARE;
rgReaderStates_t[i].cbAtr = sizeof rgReaderStates_t[i].rgbAtr;
}
/* If Plug and Play is supported by the PC/SC layer */
if (Options.pnp)
{
rgReaderStates_t[nbReaders].szReader = "\\\\?PnP?\\Notification";
rgReaderStates_t[nbReaders].dwCurrentState = SCARD_STATE_UNKNOWN;
nbReaders++;
}
#ifdef WIN32
int oldNbReaders;
int oldNbReaders_init = false;
#endif
spin_start();
/* Wait endlessly for all events in the list of readers
* We only stop in case of an error
*/
rv = SCardGetStatusChange(hContext, TIMEOUT, rgReaderStates_t, nbReaders);
spin_stop();
if (Options.debug)
displayChangedStatus(rgReaderStates_t, nbReaders);
while ((rv == SCARD_S_SUCCESS) || (rv == SCARD_E_TIMEOUT))
{
time_t t;
if (Options.pnp)
{
/* check if the number of readers has changed */
#ifdef WIN32
LONG newNbReaders = rgReaderStates_t[nbReaders-1].dwEventState >> 16;
if (! oldNbReaders_init)
{
oldNbReaders = newNbReaders;
oldNbReaders_init = true;
}
if (newNbReaders != oldNbReaders)
#else
if (rgReaderStates_t[nbReaders-1].dwEventState &
SCARD_STATE_CHANGED)
#endif
{
goto get_readers;
}
}
else
{
/* A new reader appeared? */
if ((SCardListReaders(hContext, NULL, NULL, &dwReaders)
== SCARD_S_SUCCESS) && (dwReaders != dwReadersOld))
{
goto get_readers;
}
}
if (rv != SCARD_E_TIMEOUT)
{
/* Timestamp the event as we get notified */
t = time(NULL);
printf("\n%s", ctime(&t));
}
/* Now we have an event, check all the readers in the list to see what
* happened */
for (current_reader=0; current_reader < nbReaders; current_reader++)
{
#if defined(__APPLE__) || defined(WIN32)
if (rgReaderStates_t[current_reader].dwCurrentState ==
rgReaderStates_t[current_reader].dwEventState)
continue;
#endif
/* The new current state is now the old event state */
rgReaderStates_t[current_reader].dwCurrentState =
rgReaderStates_t[current_reader].dwEventState;
if (! (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_CHANGED))
/* If nothing changed then skip to the next reader */
continue;
/* From here we know that the state for the current reader has
* changed because we did not pass through the continue statement
* above.
*/
/* Specify the current reader's number and name */
printf(" Reader %d: %s%s%s\n", current_reader,
magenta, rgReaderStates_t[current_reader].szReader,
color_end);
/* Event number */
printf(" Event number: %s%d%s\n", magenta,
(int)(rgReaderStates_t[current_reader].dwEventState >> 16),
color_end);
/* Dump the full current state */
printf(" Card state: %s", red);
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_IGNORE)
printf("Ignore this reader, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_UNKNOWN)
{
printf("Unknown\n");
goto get_readers;
}
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_UNAVAILABLE)
printf("Status unavailable, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_EMPTY)
printf("Card removed, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_PRESENT)
printf("Card inserted, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_ATRMATCH)
printf("ATR matches card, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_EXCLUSIVE)
printf("Exclusive Mode, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_INUSE)
printf("Shared Mode, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_MUTE)
printf("Unresponsive card, ");
printf("%s\n", color_end);
/* force display */
fflush(stdout);
/* Also dump the ATR if available */
if (rgReaderStates_t[current_reader].cbAtr > 0)
{
printf(" ATR: ");
if (rgReaderStates_t[current_reader].cbAtr)
{
unsigned int j;
for (j=0; j<rgReaderStates_t[current_reader].cbAtr; j++)
sprintf(&atr[j*3], "%02X ",
rgReaderStates_t[current_reader].rgbAtr[j]);
atr[j*3-1] = '\0';
}
else
atr[0] = '\0';
printf("%s%s%s\n", magenta, atr, color_end);
/* force display */
fflush(stdout);
if (Options.analyse_atr)
{
printf("\n");
sprintf(atr_command, ATR_PARSER " '%s'", atr);
if (system(atr_command))
perror(atr_command);
printf("\n");
}
}
LONG state = rgReaderStates_t[current_reader].dwEventState;
if (state & SCARD_STATE_PRESENT
&& !(state & SCARD_STATE_MUTE)
&& Options.stress_card)
{
do
{
rv = stress(hContext, rgReaderStates_t[current_reader].szReader);
} while (SCARD_S_SUCCESS == rv);
rgReaderStates_t[current_reader].dwCurrentState = SCARD_STATE_UNAWARE;
}
} /* for */
if (Options.only_list_cards)
break;
spin_start();
rv = SCardGetStatusChange(hContext, TIMEOUT, rgReaderStates_t,
nbReaders);
spin_stop();
if (Options.debug)
displayChangedStatus(rgReaderStates_t, nbReaders);
} /* while */
/* A reader disappeared */
if (SCARD_E_UNKNOWN_READER == rv)
goto get_readers;
/* If we get out the loop, GetStatusChange() was unsuccessful */
if (rv != SCARD_E_CANCELLED)
test_rv("SCardGetStatusChange", rv, hContext);
else
{
printf("%s", cub3);
fflush(stdout);
pthread_join(spin_pthread, NULL);
}
/* We try to leave things as clean as possible */
rv = SCardReleaseContext(hContext);
test_rv("SCardReleaseContext", rv, hContext);
/* free memory possibly allocated */
if (NULL != readers)
free(readers);
if (NULL != rgReaderStates_t)
free(rgReaderStates_t);
return EX_OK;
}