2
* Sample program to use PC/SC API.
4
* MUSCLE SmartCard Development ( http://www.linuxnet.com )
6
* Copyright (C) 2003-2004
7
* Ludovic Rousseau <ludovic.rousseau@free.fr>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* $Id: pcsc_demo.c,v 1.1.1.4 2004/05/11 13:44:18 rousseau Exp $
39
/* PCSC error message pretty print */
40
#define PCSC_ERROR(rv, text) \
41
if (rv != SCARD_S_SUCCESS) \
43
printf(text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
48
printf(text ": OK\n\n"); \
51
int main(int argc, char *argv[])
54
SCARDCONTEXT hContext;
57
char *ptr, **readers = NULL;
60
DWORD dwActiveProtocol, dwReaderLen, dwState, dwProt, dwAtrLen;
61
BYTE pbAtr[MAX_ATR_SIZE] = "";
62
BYTE pbReader[MAX_READERNAME] = "";
66
printf("PC/SC sample code\n");
67
printf("V 1.1 2003-2004, Ludovic Rousseau <ludovic.rousseau@free.fr>\n");
69
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
70
if (rv != SCARD_S_SUCCESS)
72
printf("SCardEstablishContext: Cannot Connect to Resource Manager %lX\n", rv);
76
/* Retrieve the available readers list.
78
* 1. Call with a null buffer to get the number of bytes to allocate
79
* 2. malloc the necessary storage
80
* 3. call with the real allocated buffer
82
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
83
if (rv != SCARD_S_SUCCESS)
85
printf("SCardListReader: %lX\n", rv);
88
mszReaders = malloc(sizeof(char)*dwReaders);
89
if (mszReaders == NULL)
91
printf("malloc: not enough memory\n");
95
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
96
if (rv != SCARD_S_SUCCESS)
97
printf("SCardListReader: %lX\n", rv);
99
/* Extract readers from the null separated string and get the total
100
* number of readers */
105
ptr += strlen(ptr)+1;
111
printf("No reader found\n");
115
/* allocate the readers table */
116
readers = calloc(nbReaders, sizeof(char *));
119
printf("Not enough memory for readers[]\n");
123
/* fill the readers table */
128
printf("%d: %s\n", nbReaders, ptr);
129
readers[nbReaders] = ptr;
130
ptr += strlen(ptr)+1;
136
reader_nb = atoi(argv[1]);
137
if (reader_nb < 0 || reader_nb >= nbReaders)
139
printf("Wrong reader index: %d\n", reader_nb);
146
/* connect to a card */
147
dwActiveProtocol = -1;
148
rv = SCardConnect(hContext, readers[reader_nb], SCARD_SHARE_EXCLUSIVE,
149
SCARD_PROTOCOL_ANY, &hCard, &dwActiveProtocol);
150
printf(" Protocol: %ld\n", dwActiveProtocol);
151
PCSC_ERROR(rv, "SCardConnect")
153
/* get card status */
154
dwAtrLen = sizeof(pbAtr);
155
dwReaderLen = sizeof(pbReader);
156
rv = SCardStatus(hCard, /*NULL*/ pbReader, &dwReaderLen, &dwState, &dwProt,
158
printf(" Reader: %s (length %ld bytes)\n", pbReader, dwReaderLen);
159
printf(" State: 0x%lX\n", dwState);
160
printf(" Prot: %ld\n", dwProt);
161
printf(" ATR (length %ld bytes):", dwAtrLen);
162
for (i=0; i<dwAtrLen; i++)
163
printf(" %02X", pbAtr[i]);
165
PCSC_ERROR(rv, "SCardStatus")
168
rv = SCardReconnect(hCard, SCARD_SHARE_EXCLUSIVE,
169
SCARD_PROTOCOL_ANY, SCARD_UNPOWER_CARD, &dwActiveProtocol);
170
PCSC_ERROR(rv, "SCardReconnect")
172
/* get card status */
173
dwAtrLen = sizeof(pbAtr);
174
dwReaderLen = sizeof(pbReader);
175
rv = SCardStatus(hCard, /*NULL*/ pbReader, &dwReaderLen, &dwState, &dwProt,
177
printf(" Reader: %s (length %ld bytes)\n", pbReader, dwReaderLen);
178
printf(" State: 0x%lX\n", dwState);
179
printf(" Prot: %ld\n", dwProt);
180
printf(" ATR (length %ld bytes):", dwAtrLen);
181
for (i=0; i<dwAtrLen; i++)
182
printf(" %02X", pbAtr[i]);
184
PCSC_ERROR(rv, "SCardStatus")
186
/* card disconnect */
187
rv = SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
188
PCSC_ERROR(rv, "SCardDisconnect")
191
/* We try to leave things as clean as possible */
192
rv = SCardReleaseContext(hContext);
193
if (rv != SCARD_S_SUCCESS)
194
printf("SCardReleaseContext: %s (0x%lX)\n", pcsc_stringify_error(rv),
197
/* Free allocated resources */
200
/* free allocated memory */