~ubuntu-branches/ubuntu/jaunty/pcsc-lite/jaunty-security

« back to all changes in this revision

Viewing changes to doc/example/pcsc_demo.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2004-06-13 21:45:56 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040613214556-zio7hrzkz9wwtffx
Tags: 1.2.9-beta2-2
* debian/rules: add -lpthread to LDFLAGS so that pthread_* symbols are
  included in the library (problem only seen on mips and mipsel).
  Closes: #253629
* debian/control: make libpcsclite-dev and libpcsclite1 at Priority:
  optional so that other packages at Priority: optional can use them.
  Closes: #249374

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Sample program to use PC/SC API.
 
3
 *
 
4
 * MUSCLE SmartCard Development ( http://www.linuxnet.com )
 
5
 *
 
6
 * Copyright (C) 2003-2004
 
7
 *  Ludovic Rousseau <ludovic.rousseau@free.fr>
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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
 
22
 *
 
23
 * $Id: pcsc_demo.c,v 1.1.1.4 2004/05/11 13:44:18 rousseau Exp $
 
24
 */
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <time.h>
 
29
#include <unistd.h>
 
30
#include <string.h>
 
31
#include <wintypes.h>
 
32
#include <winscard.h>
 
33
 
 
34
#ifndef TRUE
 
35
#define TRUE 1
 
36
#define FALSE 0
 
37
#endif
 
38
 
 
39
/* PCSC error message pretty print */
 
40
#define PCSC_ERROR(rv, text) \
 
41
if (rv != SCARD_S_SUCCESS) \
 
42
{ \
 
43
        printf(text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
 
44
        goto end; \
 
45
} \
 
46
else \
 
47
{ \
 
48
        printf(text ": OK\n\n"); \
 
49
}
 
50
 
 
51
int main(int argc, char *argv[])
 
52
{
 
53
        LONG rv;
 
54
        SCARDCONTEXT hContext;
 
55
        DWORD dwReaders;
 
56
        LPSTR mszReaders;
 
57
        char *ptr, **readers = NULL;
 
58
        int nbReaders;
 
59
        SCARDHANDLE hCard;
 
60
        DWORD dwActiveProtocol, dwReaderLen, dwState, dwProt, dwAtrLen;
 
61
        BYTE pbAtr[MAX_ATR_SIZE] = "";
 
62
        BYTE pbReader[MAX_READERNAME] = "";
 
63
        int reader_nb;
 
64
        int i;
 
65
 
 
66
        printf("PC/SC sample code\n");
 
67
        printf("V 1.1 2003-2004, Ludovic Rousseau <ludovic.rousseau@free.fr>\n");
 
68
 
 
69
        rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 
70
        if (rv != SCARD_S_SUCCESS)
 
71
        {
 
72
                printf("SCardEstablishContext: Cannot Connect to Resource Manager %lX\n", rv);
 
73
                return 1;
 
74
        }
 
75
 
 
76
        /* Retrieve the available readers list.
 
77
         *
 
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
 
81
         */
 
82
        rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
 
83
        if (rv != SCARD_S_SUCCESS)
 
84
        {
 
85
                printf("SCardListReader: %lX\n", rv);
 
86
        }
 
87
 
 
88
        mszReaders = malloc(sizeof(char)*dwReaders);
 
89
        if (mszReaders == NULL)
 
90
        {
 
91
                printf("malloc: not enough memory\n");
 
92
                goto end;
 
93
        }
 
94
 
 
95
        rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
 
96
        if (rv != SCARD_S_SUCCESS)
 
97
                printf("SCardListReader: %lX\n", rv);
 
98
 
 
99
        /* Extract readers from the null separated string and get the total
 
100
         * number of readers */
 
101
        nbReaders = 0;
 
102
        ptr = mszReaders;
 
103
        while (*ptr != '\0')
 
104
        {
 
105
                ptr += strlen(ptr)+1;
 
106
                nbReaders++;
 
107
        }
 
108
 
 
109
        if (nbReaders == 0)
 
110
        {
 
111
                printf("No reader found\n");
 
112
                goto end;
 
113
        }
 
114
 
 
115
        /* allocate the readers table */
 
116
        readers = calloc(nbReaders, sizeof(char *));
 
117
        if (NULL == readers)
 
118
        {
 
119
                printf("Not enough memory for readers[]\n");
 
120
                goto end;
 
121
        }
 
122
 
 
123
        /* fill the readers table */
 
124
        nbReaders = 0;
 
125
        ptr = mszReaders;
 
126
        while (*ptr != '\0')
 
127
        {
 
128
                printf("%d: %s\n", nbReaders, ptr);
 
129
                readers[nbReaders] = ptr;
 
130
                ptr += strlen(ptr)+1;
 
131
                nbReaders++;
 
132
        }
 
133
 
 
134
        if (argc > 1)
 
135
        {
 
136
                reader_nb = atoi(argv[1]);
 
137
                if (reader_nb < 0 || reader_nb >= nbReaders)
 
138
                {
 
139
                        printf("Wrong reader index: %d\n", reader_nb);
 
140
                        goto end;
 
141
                }
 
142
        }
 
143
        else
 
144
                reader_nb = 0;
 
145
 
 
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")
 
152
 
 
153
        /* get card status */
 
154
        dwAtrLen = sizeof(pbAtr);
 
155
        dwReaderLen = sizeof(pbReader);
 
156
        rv = SCardStatus(hCard, /*NULL*/ pbReader, &dwReaderLen, &dwState, &dwProt,
 
157
                pbAtr, &dwAtrLen);
 
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]);
 
164
        printf("\n");
 
165
        PCSC_ERROR(rv, "SCardStatus")
 
166
 
 
167
        /* card reconnect */
 
168
        rv = SCardReconnect(hCard, SCARD_SHARE_EXCLUSIVE,
 
169
                SCARD_PROTOCOL_ANY, SCARD_UNPOWER_CARD, &dwActiveProtocol);
 
170
        PCSC_ERROR(rv, "SCardReconnect")
 
171
 
 
172
        /* get card status */
 
173
        dwAtrLen = sizeof(pbAtr);
 
174
        dwReaderLen = sizeof(pbReader);
 
175
        rv = SCardStatus(hCard, /*NULL*/ pbReader, &dwReaderLen, &dwState, &dwProt,
 
176
                pbAtr, &dwAtrLen);
 
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]);
 
183
        printf("\n");
 
184
        PCSC_ERROR(rv, "SCardStatus")
 
185
 
 
186
        /* card disconnect */
 
187
        rv = SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
 
188
        PCSC_ERROR(rv, "SCardDisconnect")
 
189
 
 
190
end:
 
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),
 
195
                        rv);
 
196
 
 
197
        /* Free allocated resources */
 
198
        SCardUnload();
 
199
 
 
200
        /* free allocated memory */
 
201
        free(mszReaders);
 
202
        free(readers);
 
203
 
 
204
        return 0;
 
205
} /* main */
 
206