FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
TestSmartCardStatus.c
1// compile against PCSC gcc -o scardtest TestSmartCardStatus.c -DPCSC=1 -I /usr/include/PCSC
2// -lpcsclite
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
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>
13#else
14#include <winscard.h>
15#endif
16
17#if defined(PCSC)
18int main(int argc, char* argv[])
19#else
20int TestSmartCardStatus(int argc, char* argv[])
21#endif
22{
23 SCARDCONTEXT hContext;
24 LPSTR mszReaders;
25 DWORD cchReaders = 0;
26 DWORD err;
27 SCARDHANDLE hCard;
28 DWORD dwActiveProtocol;
29 char name[100];
30 char* aname = NULL;
31 char* aatr = NULL;
32 DWORD len;
33 BYTE atr[32];
34 DWORD atrlen = 32;
35 DWORD status = 0;
36 DWORD protocol = 0;
37 err = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
38
39 if (err != SCARD_S_SUCCESS)
40 {
41 printf("ScardEstablishedContext: 0x%08x\n", err);
42 return -1;
43 }
44
45 err = SCardListReaders(hContext, "SCard$AllReaders", NULL, &cchReaders);
46
47 if (err != 0)
48 {
49 printf("ScardListReaders: 0x%08x\n", err);
50 return -1;
51 }
52
53 mszReaders = calloc(cchReaders, sizeof(char));
54
55 if (!mszReaders)
56 {
57 printf("calloc\n");
58 return -1;
59 }
60
61 err = SCardListReaders(hContext, "SCard$AllReaders", mszReaders, &cchReaders);
62
63 if (err != SCARD_S_SUCCESS)
64 {
65 printf("ScardListReaders: 0x%08x\n", err);
66 return -1;
67 }
68
69 printf("Reader: %s\n", mszReaders);
70 err = SCardConnect(hContext, mszReaders, SCARD_SHARE_SHARED,
71 SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
72
73 if (err != SCARD_S_SUCCESS)
74 {
75 printf("ScardConnect: 0x%08x\n", err);
76 return -1;
77 }
78
79 free(mszReaders);
80
81 printf("# test 1 - get reader length\n");
82 err = SCardStatus(hCard, NULL, &len, NULL, NULL, NULL, NULL);
83 if (err != SCARD_S_SUCCESS)
84 {
85 printf("SCardStatus: 0x%08x\n", err);
86 return -1;
87 }
88 printf("reader name length: %u\n", len);
89
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)
93 {
94 printf("SCardStatus: 0x%08x\n", err);
95 return -1;
96 }
97 printf("Reader name: %s (%ld)\n", name, strlen(name));
98
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)
102 {
103 printf("SCardStatus: 0x%08x\n", err);
104 return -1;
105 }
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);
110
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)
115 {
116 printf("SCardStatus: 0x%08x\n", err);
117 return -1;
118 }
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);
125
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)
129 {
130 printf("SCardStatus: 0x%08x\n", err);
131 return -1;
132 }
133 printf("status: 0x%08X\n", status);
134 printf("proto: 0x%08X\n", protocol);
135
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)
140 {
141 printf("SCardStatus: 0x%08x\n", err);
142 return -1;
143 }
144 printf("atrlen: %u\n", atrlen);
145 SCardFreeMemory(hContext, aatr);
146
147 printf("# test 7 - get atr only pre allocated\n");
148 atrlen = 32;
149 err = SCardStatus(hCard, NULL, NULL, NULL, NULL, atr, &atrlen);
150 if (err != SCARD_S_SUCCESS)
151 {
152 printf("SCardStatus: 0x%08x\n", err);
153 return -1;
154 }
155 printf("atrlen: %u\n", atrlen);
156 SCardDisconnect(hCard, SCARD_LEAVE_CARD);
157 SCardReleaseContext(hContext);
158
159 return 0;
160}