annotate pcsc-test/pcsc-test2.c @ 89:fb75855a74a9

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