annotate libcommon/cardconnect.c @ 148:db43bc57ecf7

fc-simtool bfsearch: better reporting of EF structure
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 25 Feb 2021 07:00:51 +0000
parents 360d07419357
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <string.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <strings.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <stdio.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <stdlib.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <pcsclite.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <winscard.h>
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 SCARDCONTEXT hContext;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 SCARDHANDLE hCard;
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
10 char *reader_list, *selected_reader;
33
8a4f3d00d997 more refactoring of (select reader by number) logic
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
11 unsigned select_reader_num;
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 setup_pcsc_context()
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 LONG rv;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 fprintf(stderr, "SCardEstablishContext: %s\n",
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 return(0);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
26 get_reader_list()
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 LONG rv;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 DWORD dwReaders;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 fprintf(stderr, "SCardListReaders 1st call: %s\n",
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (dwReaders < 1) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 fprintf(stderr,
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 "error: dwReaders returned by SCardListReaders() is less than 1\n");
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
44 reader_list = malloc(dwReaders);
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
45 if (!reader_list) {
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 perror("malloc for readers list");
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
50 reader_list[0] = '\0';
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
51 rv = SCardListReaders(hContext, NULL, reader_list, &dwReaders);
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 fprintf(stderr, "SCardListReaders 2nd call: %s\n",
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
58 return(0);
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
59 }
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
60
33
8a4f3d00d997 more refactoring of (select reader by number) logic
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
61 select_reader_by_num()
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
62 {
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
63 char *cp;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
64 unsigned num;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
65
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
66 for (cp = reader_list, num = 0; *cp; num++) {
33
8a4f3d00d997 more refactoring of (select reader by number) logic
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
67 if (num == select_reader_num) {
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
68 selected_reader = cp;
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
69 return(0);
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
70 }
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
71 cp += strlen(cp) + 1;
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
73 fprintf(stderr,
35
360d07419357 better error message for non-existent reader request
Mychaela Falconia <falcon@freecalypso.org>
parents: 33
diff changeset
74 "error: requested reader #%u, but only %u reader(s) found\n",
33
8a4f3d00d997 more refactoring of (select reader by number) logic
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
75 select_reader_num, num);
32
b37fcb235848 recent libcommon/cardconnect.c change: missed
Mychaela Falconia <falcon@freecalypso.org>
parents: 30
diff changeset
76 SCardReleaseContext(hContext);
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
77 exit(1);
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 connect_to_card()
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 LONG rv;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 DWORD dwActiveProtocol;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
30
84d1c31d0fad first round of refactoring for selection among multiple readers
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
85 rv = SCardConnect(hContext, selected_reader, SCARD_SHARE_EXCLUSIVE,
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (rv != SCARD_S_SUCCESS) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 SCardReleaseContext(hContext);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 exit(1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 return(0);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }