comparison pcsc-test/pcsc-test2.c @ 84:1d7d8615d628

pcsc-test: some experiments to get pcsc-lite working
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 23 Jan 2021 17:21:08 +0000
parents
children
comparison
equal deleted inserted replaced
83:d7a1e7a6d6ba 84:1d7d8615d628
1 #include <sys/types.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <pcsclite.h>
7 #include <winscard.h>
8 #include <reader.h>
9
10 #define MAX_ATR_BYTES 33
11
12 SCARDCONTEXT hContext;
13 SCARDHANDLE hCard;
14 char *reader_name_buf;
15
16 setup_pcsc_context()
17 {
18 LONG rv;
19
20 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
21 if (rv != SCARD_S_SUCCESS) {
22 fprintf(stderr, "SCardEstablishContext: %s\n",
23 pcsc_stringify_error(rv));
24 exit(1);
25 }
26 return(0);
27 }
28
29 get_reader_name()
30 {
31 LONG rv;
32 DWORD dwReaders;
33
34 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
35 if (rv != SCARD_S_SUCCESS) {
36 fprintf(stderr, "SCardListReaders 1st call: %s\n",
37 pcsc_stringify_error(rv));
38 SCardReleaseContext(hContext);
39 exit(1);
40 }
41 if (dwReaders < 1) {
42 fprintf(stderr,
43 "error: dwReaders returned by SCardListReaders() is less than 1\n");
44 SCardReleaseContext(hContext);
45 exit(1);
46 }
47 reader_name_buf = malloc(dwReaders);
48 if (!reader_name_buf) {
49 perror("malloc for readers list");
50 SCardReleaseContext(hContext);
51 exit(1);
52 }
53 reader_name_buf[0] = '\0';
54 rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders);
55 if (rv != SCARD_S_SUCCESS) {
56 fprintf(stderr, "SCardListReaders 2nd call: %s\n",
57 pcsc_stringify_error(rv));
58 SCardReleaseContext(hContext);
59 exit(1);
60 }
61 if (reader_name_buf[0] == '\0') {
62 fprintf(stderr,
63 "error: list returned by SCardListReaders() begins with a NUL byte\n");
64 SCardReleaseContext(hContext);
65 exit(1);
66 }
67 if (!memchr(reader_name_buf, 0, dwReaders)) {
68 fprintf(stderr,
69 "error: list returned by SCardListReaders() does not contain a NUL byte\n");
70 SCardReleaseContext(hContext);
71 exit(1);
72 }
73 return(0);
74 }
75
76 connect_to_card()
77 {
78 LONG rv;
79 DWORD dwActiveProtocol;
80
81 rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE,
82 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
83 if (rv != SCARD_S_SUCCESS) {
84 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
85 SCardReleaseContext(hContext);
86 exit(1);
87 }
88 return(0);
89 }
90
91 retrieve_atr()
92 {
93 u_char atrbuf[MAX_ATR_BYTES];
94 LONG rv;
95 DWORD dwAttrLen;
96 unsigned n;
97
98 dwAttrLen = MAX_ATR_BYTES;
99 rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, atrbuf, &dwAttrLen);
100 if (rv != SCARD_S_SUCCESS) {
101 fprintf(stderr, "SCardGetAttrib for ATR: %s\n",
102 pcsc_stringify_error(rv));
103 return(-1);
104 }
105 printf("ATR:");
106 for (n = 0; n < dwAttrLen; n++)
107 printf(" %02X", atrbuf[n]);
108 putchar('\n');
109 return(0);
110 }
111
112 main(argc, argv)
113 char **argv;
114 {
115 setup_pcsc_context();
116 get_reader_name();
117 printf("Card reader name: %s\n", reader_name_buf);
118 connect_to_card();
119 retrieve_atr();
120 SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
121 SCardReleaseContext(hContext);
122 exit(0);
123 }