stringify_error now returns a string instead of printing the error

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@741 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2012-04-10 22:15:57 +00:00
parent ca85cd8983
commit cc23508082
4 changed files with 190 additions and 192 deletions

View File

@@ -1 +0,0 @@
../../ccid/src/pcscutil.c

126
pcsc-relay/src/pcscutil.c Normal file
View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2010 Frank Morgner
*
* This file is part of ccid.
*
* ccid is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* ccid is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* ccid. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include "pcscutil.h"
#pragma comment(lib, "winscard.lib")
LONG
pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocols,
LPSCARDCONTEXT phContext, LPSTR *readers, LPSCARDHANDLE phCard,
LPDWORD pdwActiveProtocol)
{
LONG r;
DWORD ctl, readerslen;
char *reader;
size_t l, i;
r = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, phContext);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to PC/SC Service\n");
goto err;
}
readerslen = SCARD_AUTOALLOCATE;
r = SCardListReaders(*phContext, NULL, (LPTSTR) readers, &readerslen);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not get readers\n");
goto err;
}
for (reader = *readers, i = 0; readerslen > 0;
l = strlen(reader) + 1, readerslen -= l, reader += l, i++) {
if (i == readernum)
break;
}
if (readerslen <= 0) {
fprintf(stderr, "Could not find reader number %u\n", readernum);
r = SCARD_E_UNKNOWN_READER;
goto err;
}
r = SCardConnect(*phContext, reader, dwShareMode, dwPreferredProtocols,
phCard, pdwActiveProtocol);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to %s\n", reader);
goto err;
}
printf("Connected to %s\n", reader);
err:
return r;
}
void
pcsc_disconnect(SCARDCONTEXT hContext, SCARDHANDLE hCard, LPSTR readers)
{
SCardDisconnect(hCard, SCARD_LEAVE_CARD);
SCardFreeMemory(hContext, readers);
SCardReleaseContext(hContext);
}
LONG
pcsc_transmit(DWORD dwActiveProtocol, SCARDHANDLE hCard,
LPCBYTE pbSendBuffer, DWORD cbSendLength,
LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
{
LONG r;
SCARD_IO_REQUEST ioRecvPci;
switch (dwActiveProtocol) {
case SCARD_PROTOCOL_T0:
r = SCardTransmit(hCard, SCARD_PCI_T0, pbSendBuffer, cbSendLength,
&ioRecvPci, pbRecvBuffer, pcbRecvLength);
break;
case SCARD_PROTOCOL_T1:
r = SCardTransmit(hCard, SCARD_PCI_T1, pbSendBuffer, cbSendLength,
&ioRecvPci, pbRecvBuffer, pcbRecvLength);
break;
default:
fprintf(stderr, "Could not transmit with unknown protocol %u\n",
(unsigned int) dwActiveProtocol);
r = SCARD_E_PROTO_MISMATCH;
break;
}
return r;
}
void
printb(const char *label, unsigned char *buf, size_t len)
{
size_t i = 0;
printf("%s", label);
while (i < len) {
printf("%02X", buf[i]);
i++;
if (i%20)
printf(" ");
else if (i != len)
printf("\n");
}
printf("\n");
}

View File

@@ -1 +0,0 @@
../../ccid/src/pcscutil.h

64
pcsc-relay/src/pcscutil.h Normal file
View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2010-2012 Frank Morgner <morgner@informatik.hu-berlin.de>.
*
* This file is part of ccid.
*
* ccid is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* ccid is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* ccid. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _PCSCUTIL_H
#define _PCSCUTIL_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <winscard.h>
#ifdef HAVE_PCSCLITE_H
#include <pcsclite.h>
#define stringify_error(r) pcsc_stringify_error(r)
#else
#define stringify_error(r) "PC/SC error code %u\n", (unsigned int) r
#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1)
#endif
#ifdef HAVE_READER_H
#include <reader.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
LONG
pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocol,
SCARDCONTEXT *hContext, LPSTR *readers, LPSCARDHANDLE phCard,
LPDWORD pdwActiveProtocol);
void
pcsc_disconnect(SCARDCONTEXT hContext, SCARDHANDLE hCard, LPSTR readers);
LONG
pcsc_transmit(DWORD dwActiveProtocol, SCARDHANDLE hCard,
LPCBYTE pbSendBuffer, DWORD cbSendLength,
LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength);
void
printb(const char *label, unsigned char *buf, size_t len);
#ifdef __cplusplus
}
#endif
#endif