comparison libcommon/cardconnect.c @ 30:84d1c31d0fad

first round of refactoring for selection among multiple readers
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 17:09:01 +0000
parents f7145c77b7fb
children b37fcb235848
comparison
equal deleted inserted replaced
29:d368051576d0 30:84d1c31d0fad
5 #include <pcsclite.h> 5 #include <pcsclite.h>
6 #include <winscard.h> 6 #include <winscard.h>
7 7
8 SCARDCONTEXT hContext; 8 SCARDCONTEXT hContext;
9 SCARDHANDLE hCard; 9 SCARDHANDLE hCard;
10 char *reader_name_buf; 10 char *reader_list, *selected_reader;
11 11
12 setup_pcsc_context() 12 setup_pcsc_context()
13 { 13 {
14 LONG rv; 14 LONG rv;
15 15
20 exit(1); 20 exit(1);
21 } 21 }
22 return(0); 22 return(0);
23 } 23 }
24 24
25 get_reader_name() 25 get_reader_list()
26 { 26 {
27 LONG rv; 27 LONG rv;
28 DWORD dwReaders; 28 DWORD dwReaders;
29 29
30 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders); 30 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
38 fprintf(stderr, 38 fprintf(stderr,
39 "error: dwReaders returned by SCardListReaders() is less than 1\n"); 39 "error: dwReaders returned by SCardListReaders() is less than 1\n");
40 SCardReleaseContext(hContext); 40 SCardReleaseContext(hContext);
41 exit(1); 41 exit(1);
42 } 42 }
43 reader_name_buf = malloc(dwReaders); 43 reader_list = malloc(dwReaders);
44 if (!reader_name_buf) { 44 if (!reader_list) {
45 perror("malloc for readers list"); 45 perror("malloc for readers list");
46 SCardReleaseContext(hContext); 46 SCardReleaseContext(hContext);
47 exit(1); 47 exit(1);
48 } 48 }
49 reader_name_buf[0] = '\0'; 49 reader_list[0] = '\0';
50 rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders); 50 rv = SCardListReaders(hContext, NULL, reader_list, &dwReaders);
51 if (rv != SCARD_S_SUCCESS) { 51 if (rv != SCARD_S_SUCCESS) {
52 fprintf(stderr, "SCardListReaders 2nd call: %s\n", 52 fprintf(stderr, "SCardListReaders 2nd call: %s\n",
53 pcsc_stringify_error(rv)); 53 pcsc_stringify_error(rv));
54 SCardReleaseContext(hContext); 54 SCardReleaseContext(hContext);
55 exit(1); 55 exit(1);
56 } 56 }
57 if (reader_name_buf[0] == '\0') { 57 return(0);
58 fprintf(stderr, 58 }
59 "error: list returned by SCardListReaders() begins with a NUL byte\n"); 59
60 SCardReleaseContext(hContext); 60 select_reader_num(select_num)
61 exit(1); 61 unsigned select_num;
62 {
63 char *cp;
64 unsigned num;
65
66 for (cp = reader_list, num = 0; *cp; num++) {
67 if (num == select_num) {
68 selected_reader = cp;
69 return(0);
70 }
71 cp += strlen(cp) + 1;
62 } 72 }
63 if (!memchr(reader_name_buf, 0, dwReaders)) { 73 fprintf(stderr,
64 fprintf(stderr, 74 "error: requested reader #%u, but only %u readers found\n",
65 "error: list returned by SCardListReaders() does not contain a NUL byte\n"); 75 select_num, num);
66 SCardReleaseContext(hContext); 76 exit(1);
67 exit(1);
68 }
69 return(0);
70 } 77 }
71 78
72 connect_to_card() 79 connect_to_card()
73 { 80 {
74 LONG rv; 81 LONG rv;
75 DWORD dwActiveProtocol; 82 DWORD dwActiveProtocol;
76 83
77 rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE, 84 rv = SCardConnect(hContext, selected_reader, SCARD_SHARE_EXCLUSIVE,
78 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol); 85 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
79 if (rv != SCARD_S_SUCCESS) { 86 if (rv != SCARD_S_SUCCESS) {
80 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv)); 87 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
81 SCardReleaseContext(hContext); 88 SCardReleaseContext(hContext);
82 exit(1); 89 exit(1);