6 #if defined(__APPLE__) || defined(PCSC)
7 #include <PCSC/winscard.h>
8 #include <PCSC/wintypes.h>
9 #elif defined(__linux__)
10 #include <winpr/crt.h>
11 #include <winpr/smartcard.h>
12 #include <winpr/synch.h>
18 int main(
int argc,
char* argv[])
20 int TestSmartCardStatus(
int argc,
char* argv[])
23 SCARDCONTEXT hContext;
28 DWORD dwActiveProtocol;
37 err = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
39 if (err != SCARD_S_SUCCESS)
41 printf(
"ScardEstablishedContext: 0x%08x\n", err);
45 err = SCardListReaders(hContext,
"SCard$AllReaders", NULL, &cchReaders);
49 printf(
"ScardListReaders: 0x%08x\n", err);
53 mszReaders = calloc(cchReaders,
sizeof(
char));
61 err = SCardListReaders(hContext,
"SCard$AllReaders", mszReaders, &cchReaders);
63 if (err != SCARD_S_SUCCESS)
65 printf(
"ScardListReaders: 0x%08x\n", err);
69 printf(
"Reader: %s\n", mszReaders);
70 err = SCardConnect(hContext, mszReaders, SCARD_SHARE_SHARED,
71 SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
73 if (err != SCARD_S_SUCCESS)
75 printf(
"ScardConnect: 0x%08x\n", err);
81 printf(
"# test 1 - get reader length\n");
82 err = SCardStatus(hCard, NULL, &len, NULL, NULL, NULL, NULL);
83 if (err != SCARD_S_SUCCESS)
85 printf(
"SCardStatus: 0x%08x\n", err);
88 printf(
"reader name length: %u\n", len);
90 printf(
"# test 2 - get reader name value\n");
91 err = SCardStatus(hCard, name, &len, NULL, NULL, NULL, NULL);
92 if (err != SCARD_S_SUCCESS)
94 printf(
"SCardStatus: 0x%08x\n", err);
97 printf(
"Reader name: %s (%ld)\n", name, strlen(name));
99 printf(
"# test 3 - get all values - pre allocated\n");
100 err = SCardStatus(hCard, name, &len, &status, &protocol, atr, &atrlen);
101 if (err != SCARD_S_SUCCESS)
103 printf(
"SCardStatus: 0x%08x\n", err);
106 printf(
"Reader name: %s (%ld/len %u)\n", name, strlen(name), len);
107 printf(
"status: 0x%08X\n", status);
108 printf(
"proto: 0x%08X\n", protocol);
109 printf(
"atrlen: %u\n", atrlen);
111 printf(
"# test 4 - get all values - auto allocate\n");
112 len = atrlen = SCARD_AUTOALLOCATE;
113 err = SCardStatus(hCard, (LPSTR)&aname, &len, &status, &protocol, (LPBYTE)&aatr, &atrlen);
114 if (err != SCARD_S_SUCCESS)
116 printf(
"SCardStatus: 0x%08x\n", err);
119 printf(
"Reader name: %s (%ld/%u)\n", aname, strlen(aname), len);
120 printf(
"status: 0x%08X\n", status);
121 printf(
"proto: 0x%08X\n", protocol);
122 printf(
"atrlen: %u\n", atrlen);
123 SCardFreeMemory(hContext, aname);
124 SCardFreeMemory(hContext, aatr);
126 printf(
"# test 5 - get status and protocol only\n");
127 err = SCardStatus(hCard, NULL, NULL, &status, &protocol, NULL, NULL);
128 if (err != SCARD_S_SUCCESS)
130 printf(
"SCardStatus: 0x%08x\n", err);
133 printf(
"status: 0x%08X\n", status);
134 printf(
"proto: 0x%08X\n", protocol);
136 printf(
"# test 6 - get atr only auto allocated\n");
137 atrlen = SCARD_AUTOALLOCATE;
138 err = SCardStatus(hCard, NULL, NULL, NULL, NULL, (LPBYTE)&aatr, &atrlen);
139 if (err != SCARD_S_SUCCESS)
141 printf(
"SCardStatus: 0x%08x\n", err);
144 printf(
"atrlen: %u\n", atrlen);
145 SCardFreeMemory(hContext, aatr);
147 printf(
"# test 7 - get atr only pre allocated\n");
149 err = SCardStatus(hCard, NULL, NULL, NULL, NULL, atr, &atrlen);
150 if (err != SCARD_S_SUCCESS)
152 printf(
"SCardStatus: 0x%08x\n", err);
155 printf(
"atrlen: %u\n", atrlen);
156 SCardDisconnect(hCard, SCARD_LEAVE_CARD);
157 SCardReleaseContext(hContext);