comparison uicc/cardconnect.c @ 130:f691a19f191d

fc-uicc-tool skeleton started
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 00:08:12 +0000
parents
children
comparison
equal deleted inserted replaced
129:2adb802b2a98 130:f691a19f191d
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 "globals.h"
9
10 setup_pcsc_context()
11 {
12 LONG rv;
13
14 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
15 if (rv != SCARD_S_SUCCESS) {
16 fprintf(stderr, "SCardEstablishContext: %s\n",
17 pcsc_stringify_error(rv));
18 exit(1);
19 }
20 return(0);
21 }
22
23 get_reader_name()
24 {
25 LONG rv;
26 DWORD dwReaders;
27
28 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
29 if (rv != SCARD_S_SUCCESS) {
30 fprintf(stderr, "SCardListReaders 1st call: %s\n",
31 pcsc_stringify_error(rv));
32 SCardReleaseContext(hContext);
33 exit(1);
34 }
35 if (dwReaders < 1) {
36 fprintf(stderr,
37 "error: dwReaders returned by SCardListReaders() is less than 1\n");
38 SCardReleaseContext(hContext);
39 exit(1);
40 }
41 reader_name_buf = malloc(dwReaders);
42 if (!reader_name_buf) {
43 perror("malloc for readers list");
44 SCardReleaseContext(hContext);
45 exit(1);
46 }
47 reader_name_buf[0] = '\0';
48 rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders);
49 if (rv != SCARD_S_SUCCESS) {
50 fprintf(stderr, "SCardListReaders 2nd call: %s\n",
51 pcsc_stringify_error(rv));
52 SCardReleaseContext(hContext);
53 exit(1);
54 }
55 if (reader_name_buf[0] == '\0') {
56 fprintf(stderr,
57 "error: list returned by SCardListReaders() begins with a NUL byte\n");
58 SCardReleaseContext(hContext);
59 exit(1);
60 }
61 if (!memchr(reader_name_buf, 0, dwReaders)) {
62 fprintf(stderr,
63 "error: list returned by SCardListReaders() does not contain a NUL byte\n");
64 SCardReleaseContext(hContext);
65 exit(1);
66 }
67 return(0);
68 }
69
70 connect_to_card()
71 {
72 LONG rv;
73 DWORD dwActiveProtocol;
74
75 rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE,
76 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
77 if (rv != SCARD_S_SUCCESS) {
78 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
79 SCardReleaseContext(hContext);
80 exit(1);
81 }
82 return(0);
83 }