FreeCalypso > hg > fc-pcsc-tools
comparison libcommon/cardconnect.c @ 0:f7145c77b7fb
starting libcommon: factored out of fc-simtool
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 11 Feb 2021 22:28:45 +0000 |
parents | |
children | 84d1c31d0fad |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f7145c77b7fb |
---|---|
1 #include <string.h> | |
2 #include <strings.h> | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <pcsclite.h> | |
6 #include <winscard.h> | |
7 | |
8 SCARDCONTEXT hContext; | |
9 SCARDHANDLE hCard; | |
10 char *reader_name_buf; | |
11 | |
12 setup_pcsc_context() | |
13 { | |
14 LONG rv; | |
15 | |
16 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); | |
17 if (rv != SCARD_S_SUCCESS) { | |
18 fprintf(stderr, "SCardEstablishContext: %s\n", | |
19 pcsc_stringify_error(rv)); | |
20 exit(1); | |
21 } | |
22 return(0); | |
23 } | |
24 | |
25 get_reader_name() | |
26 { | |
27 LONG rv; | |
28 DWORD dwReaders; | |
29 | |
30 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders); | |
31 if (rv != SCARD_S_SUCCESS) { | |
32 fprintf(stderr, "SCardListReaders 1st call: %s\n", | |
33 pcsc_stringify_error(rv)); | |
34 SCardReleaseContext(hContext); | |
35 exit(1); | |
36 } | |
37 if (dwReaders < 1) { | |
38 fprintf(stderr, | |
39 "error: dwReaders returned by SCardListReaders() is less than 1\n"); | |
40 SCardReleaseContext(hContext); | |
41 exit(1); | |
42 } | |
43 reader_name_buf = malloc(dwReaders); | |
44 if (!reader_name_buf) { | |
45 perror("malloc for readers list"); | |
46 SCardReleaseContext(hContext); | |
47 exit(1); | |
48 } | |
49 reader_name_buf[0] = '\0'; | |
50 rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders); | |
51 if (rv != SCARD_S_SUCCESS) { | |
52 fprintf(stderr, "SCardListReaders 2nd call: %s\n", | |
53 pcsc_stringify_error(rv)); | |
54 SCardReleaseContext(hContext); | |
55 exit(1); | |
56 } | |
57 if (reader_name_buf[0] == '\0') { | |
58 fprintf(stderr, | |
59 "error: list returned by SCardListReaders() begins with a NUL byte\n"); | |
60 SCardReleaseContext(hContext); | |
61 exit(1); | |
62 } | |
63 if (!memchr(reader_name_buf, 0, dwReaders)) { | |
64 fprintf(stderr, | |
65 "error: list returned by SCardListReaders() does not contain a NUL byte\n"); | |
66 SCardReleaseContext(hContext); | |
67 exit(1); | |
68 } | |
69 return(0); | |
70 } | |
71 | |
72 connect_to_card() | |
73 { | |
74 LONG rv; | |
75 DWORD dwActiveProtocol; | |
76 | |
77 rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE, | |
78 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol); | |
79 if (rv != SCARD_S_SUCCESS) { | |
80 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv)); | |
81 SCardReleaseContext(hContext); | |
82 exit(1); | |
83 } | |
84 return(0); | |
85 } |