1/*
2 * MUSCLE SmartCard Development ( http://www.linuxnet.com )
3 *
4 * Copyright (C) 1999-2002
5 *  David Corcoran <corcoran@linuxnet.com>
6 * Copyright (C) 1999-2008
7 *  Ludovic Rousseau <ludovic.rousseau@free.fr>
8 *
9 * This file is dual licenced:
10 * - BSD-like, see the COPYING file
11 * - GNU Lesser General Licence 2.1 or (at your option) any later version.
12 *
13 * $Id: error.c 123 2010-03-27 10:50:42Z ludovic.rousseau@gmail.com $
14 */
15
16/**
17 * @file
18 * @brief This handles pcsc_stringify_error()
19 */
20
21#include <stdio.h>
22#include <sys/types.h>
23
24#include "pcscexport.h"
25#include "pcsclite.h"
26#include "string.h"
27
28/**
29 * @brief This function return a human readable text for the given PC/SC error
30 * code.
31 *
32 * @ingroup API
33 * @param[in] pcscError Error code to be translated to text.
34 *
35 * @return Text representing the error code passed.
36 *
37 * @code
38 * SCARDCONTEXT hContext;
39 * LONG rv;
40 * rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
41 * if (rv != SCARD_S_SUCCESS)
42 *     printf("SCardReleaseContext: %s (0x%lX)\n",
43 *         pcsc_stringify_error(rv), rv);
44 * @endcode
45 */
46PCSC_API char* pcsc_stringify_error(int32_t pcscError)
47{
48	static char strError[75];
49
50	switch (pcscError)
51	{
52	case SCARD_S_SUCCESS:
53		(void)strlcpy(strError, "Command successful.", sizeof(strError));
54		break;
55	case SCARD_E_CANCELLED:
56		(void)strlcpy(strError, "Command cancelled.", sizeof(strError));
57		break;
58	case SCARD_E_CANT_DISPOSE:
59		(void)strlcpy(strError, "Cannot dispose handle.", sizeof(strError));
60		break;
61	case SCARD_E_INSUFFICIENT_BUFFER:
62		(void)strlcpy(strError, "Insufficient buffer.", sizeof(strError));
63		break;
64	case SCARD_E_INVALID_ATR:
65		(void)strlcpy(strError, "Invalid ATR.", sizeof(strError));
66		break;
67	case SCARD_E_INVALID_HANDLE:
68		(void)strlcpy(strError, "Invalid handle.", sizeof(strError));
69		break;
70	case SCARD_E_INVALID_PARAMETER:
71		(void)strlcpy(strError, "Invalid parameter given.", sizeof(strError));
72		break;
73	case SCARD_E_INVALID_TARGET:
74		(void)strlcpy(strError, "Invalid target given.", sizeof(strError));
75		break;
76	case SCARD_E_INVALID_VALUE:
77		(void)strlcpy(strError, "Invalid value given.", sizeof(strError));
78		break;
79	case SCARD_E_NO_MEMORY:
80		(void)strlcpy(strError, "Not enough memory.", sizeof(strError));
81		break;
82	case SCARD_F_COMM_ERROR:
83		(void)strlcpy(strError, "RPC transport error.", sizeof(strError));
84		break;
85	case SCARD_F_INTERNAL_ERROR:
86		(void)strlcpy(strError, "Internal error.", sizeof(strError));
87		break;
88	case SCARD_F_UNKNOWN_ERROR:
89		(void)strlcpy(strError, "Unknown error.", sizeof(strError));
90		break;
91	case SCARD_F_WAITED_TOO_LONG:
92		(void)strlcpy(strError, "Waited too long.", sizeof(strError));
93		break;
94	case SCARD_E_UNKNOWN_READER:
95		(void)strlcpy(strError, "Unknown reader specified.", sizeof(strError));
96		break;
97	case SCARD_E_TIMEOUT:
98		(void)strlcpy(strError, "Command timeout.", sizeof(strError));
99		break;
100	case SCARD_E_SHARING_VIOLATION:
101		(void)strlcpy(strError, "Sharing violation.", sizeof(strError));
102		break;
103	case SCARD_E_NO_SMARTCARD:
104		(void)strlcpy(strError, "No smart card inserted.", sizeof(strError));
105		break;
106	case SCARD_E_UNKNOWN_CARD:
107		(void)strlcpy(strError, "Unknown card.", sizeof(strError));
108		break;
109	case SCARD_E_PROTO_MISMATCH:
110		(void)strlcpy(strError, "Card protocol mismatch.", sizeof(strError));
111		break;
112	case SCARD_E_NOT_READY:
113		(void)strlcpy(strError, "Subsystem not ready.", sizeof(strError));
114		break;
115	case SCARD_E_SYSTEM_CANCELLED:
116		(void)strlcpy(strError, "System cancelled.", sizeof(strError));
117		break;
118	case SCARD_E_NOT_TRANSACTED:
119		(void)strlcpy(strError, "Transaction failed.", sizeof(strError));
120		break;
121	case SCARD_E_READER_UNAVAILABLE:
122		(void)strlcpy(strError, "Reader is unavailable.", sizeof(strError));
123		break;
124	case SCARD_W_UNSUPPORTED_CARD:
125		(void)strlcpy(strError, "Card is not supported.", sizeof(strError));
126		break;
127	case SCARD_W_UNRESPONSIVE_CARD:
128		(void)strlcpy(strError, "Card is unresponsive.", sizeof(strError));
129		break;
130	case SCARD_W_UNPOWERED_CARD:
131		(void)strlcpy(strError, "Card is unpowered.", sizeof(strError));
132		break;
133	case SCARD_W_RESET_CARD:
134		(void)strlcpy(strError, "Card was reset.", sizeof(strError));
135		break;
136	case SCARD_W_REMOVED_CARD:
137		(void)strlcpy(strError, "Card was removed.", sizeof(strError));
138		break;
139	case SCARD_W_INSERTED_CARD:
140		(void)strlcpy(strError, "Card was inserted.", sizeof(strError));
141		break;
142	case SCARD_E_UNSUPPORTED_FEATURE:
143		(void)strlcpy(strError, "Feature not supported.", sizeof(strError));
144		break;
145	case SCARD_E_PCI_TOO_SMALL:
146		(void)strlcpy(strError, "PCI struct too small.", sizeof(strError));
147		break;
148	case SCARD_E_READER_UNSUPPORTED:
149		(void)strlcpy(strError, "Reader is unsupported.", sizeof(strError));
150		break;
151	case SCARD_E_DUPLICATE_READER:
152		(void)strlcpy(strError, "Reader already exists.", sizeof(strError));
153		break;
154	case SCARD_E_CARD_UNSUPPORTED:
155		(void)strlcpy(strError, "Card is unsupported.", sizeof(strError));
156		break;
157	case SCARD_E_NO_SERVICE:
158		(void)strlcpy(strError, "Service not available.", sizeof(strError));
159		break;
160	case SCARD_E_SERVICE_STOPPED:
161		(void)strlcpy(strError, "Service was stopped.", sizeof(strError));
162		break;
163	case SCARD_E_NO_READERS_AVAILABLE:
164		(void)strlcpy(strError, "Cannot find a smart card reader.", sizeof(strError));
165		break;
166	default:
167		(void)snprintf(strError, sizeof(strError)-1, "Unkown error: 0x%08X",
168			pcscError);
169	};
170
171	/* add a null byte */
172	strError[sizeof(strError)-1] = '\0';
173
174	return strError;
175}
176
177