FreeCalypso > hg > freecalypso-hwlab
annotate simtool/cardconnect.c @ 145:14dee03e9675
fc-uicc-tool: low-level write commands ported over
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 06 Feb 2021 02:17:51 +0000 |
parents | b57cf64ece29 |
children |
rev | line source |
---|---|
85
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <string.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <strings.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <stdio.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <stdlib.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <pcsclite.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <winscard.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include "globals.h" |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 setup_pcsc_context() |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 LONG rv; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 if (rv != SCARD_S_SUCCESS) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 fprintf(stderr, "SCardEstablishContext: %s\n", |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 pcsc_stringify_error(rv)); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 return(0); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 get_reader_name() |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 LONG rv; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 DWORD dwReaders; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (rv != SCARD_S_SUCCESS) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 fprintf(stderr, "SCardListReaders 1st call: %s\n", |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 pcsc_stringify_error(rv)); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 if (dwReaders < 1) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 "error: dwReaders returned by SCardListReaders() is less than 1\n"); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 reader_name_buf = malloc(dwReaders); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (!reader_name_buf) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 perror("malloc for readers list"); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 reader_name_buf[0] = '\0'; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 if (rv != SCARD_S_SUCCESS) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 fprintf(stderr, "SCardListReaders 2nd call: %s\n", |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 pcsc_stringify_error(rv)); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (reader_name_buf[0] == '\0') { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 "error: list returned by SCardListReaders() begins with a NUL byte\n"); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (!memchr(reader_name_buf, 0, dwReaders)) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 "error: list returned by SCardListReaders() does not contain a NUL byte\n"); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 return(0); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 connect_to_card() |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 LONG rv; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 DWORD dwActiveProtocol; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (rv != SCARD_S_SUCCESS) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv)); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 SCardReleaseContext(hContext); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 exit(1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 return(0); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |