|
|
|
@ -10,9 +10,35 @@ |
|
|
|
|
#ifndef PCSC_HXX |
|
|
|
|
#define PCSC_HXX |
|
|
|
|
|
|
|
|
|
#include <PCSC/wintypes.h> |
|
|
|
|
#include <PCSC/pcsclite.h> |
|
|
|
|
#include <PCSC/winscard.h> |
|
|
|
|
#ifdef WIN32 |
|
|
|
|
#include "WinSCard.h" |
|
|
|
|
#ifndef MAX_ATR_SIZE |
|
|
|
|
#define MAX_ATR_SIZE 33 |
|
|
|
|
#endif |
|
|
|
|
namespace pcsc { |
|
|
|
|
//! stupid windows needs std::wstring
|
|
|
|
|
std::wstring strconv(std::string s) { |
|
|
|
|
return std::wstring(s.begin(), s.end()); |
|
|
|
|
} |
|
|
|
|
std::string strconv(std::wstring s) { |
|
|
|
|
return std::string(s.begin(), s.end()); |
|
|
|
|
} |
|
|
|
|
typedef wchar_t char_t; |
|
|
|
|
typedef std::wstring string; |
|
|
|
|
} |
|
|
|
|
#else |
|
|
|
|
#include <PCSC/pcsclite.h> |
|
|
|
|
#include <PCSC/wintypes.h> |
|
|
|
|
#include <PCSC/pcsclite.h> |
|
|
|
|
#include <PCSC/winscard.h> |
|
|
|
|
namespace pcsc { |
|
|
|
|
const std::string& strconv(const std::string& s) { |
|
|
|
|
return s; |
|
|
|
|
} |
|
|
|
|
typedef char char_t; |
|
|
|
|
typedef std::string string; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp> |
|
|
|
|
#include <string> |
|
|
|
@ -169,9 +195,9 @@ namespace pcsc { |
|
|
|
|
|
|
|
|
|
//! Get reader status.
|
|
|
|
|
Status status() { |
|
|
|
|
unsigned long dummy(0); |
|
|
|
|
unsigned long s; |
|
|
|
|
unsigned long len(MAX_ATR_SIZE); |
|
|
|
|
DWORD dummy(0); |
|
|
|
|
DWORD s; |
|
|
|
|
DWORD len(MAX_ATR_SIZE); |
|
|
|
|
unsigned char a[len]; |
|
|
|
|
check(SCardStatus(_id, 0, &dummy, &s, &_protocol, a, &len)); |
|
|
|
|
return Status(s, std::string((char*)a, len)); |
|
|
|
@ -179,7 +205,7 @@ namespace pcsc { |
|
|
|
|
|
|
|
|
|
//! Transmit data to reader.
|
|
|
|
|
std::string transmit(std::string in) { |
|
|
|
|
unsigned long len(1024); // arbitrary
|
|
|
|
|
DWORD len(1024); // arbitrary
|
|
|
|
|
unsigned char buff[len]; |
|
|
|
|
SCARD_IO_REQUEST rPci; |
|
|
|
|
check(SCardTransmit(_id, pci(), |
|
|
|
@ -217,7 +243,7 @@ namespace pcsc { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*! @throw not_implemented if _protocol is unknown. */ |
|
|
|
|
SCARD_IO_REQUEST* pci() { |
|
|
|
|
const SCARD_IO_REQUEST* pci() { |
|
|
|
|
switch(_protocol) { |
|
|
|
|
case SCARD_PROTOCOL_T0: return SCARD_PCI_T0; |
|
|
|
|
case SCARD_PROTOCOL_T1: return SCARD_PCI_T1; |
|
|
|
@ -240,7 +266,7 @@ namespace pcsc { |
|
|
|
|
//! Establishes a connection to the given named cardreader
|
|
|
|
|
Reader(const std::string& nm, Connection& c): |
|
|
|
|
name(nm), _connection(c) { |
|
|
|
|
check(SCardConnect(_connection._id, name.c_str(), |
|
|
|
|
check(SCardConnect(_connection._id, strconv(name).c_str(), |
|
|
|
|
SCARD_SHARE_SHARED, |
|
|
|
|
SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1, |
|
|
|
|
&_id, &_protocol)); |
|
|
|
@ -256,9 +282,9 @@ namespace pcsc { |
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
Connection& _connection; |
|
|
|
|
long _id; |
|
|
|
|
SCARDHANDLE _id; |
|
|
|
|
long _state; |
|
|
|
|
unsigned long _protocol; |
|
|
|
|
DWORD _protocol; |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
//------------------------------------------------------------------Reader
|
|
|
|
@ -319,14 +345,15 @@ namespace pcsc { |
|
|
|
|
Strings scan(const Strings& groups = Strings()) { |
|
|
|
|
Strings res; |
|
|
|
|
std::string grp(join(groups)); |
|
|
|
|
unsigned long num(0); |
|
|
|
|
if (!check(SCardListReaders(_id, grp.size()?grp.data():0, 0, &num))) |
|
|
|
|
DWORD num(0); |
|
|
|
|
if (!check(SCardListReaders(_id, grp.size()?strconv(grp).data():0, 0, |
|
|
|
|
&num))) |
|
|
|
|
return res; |
|
|
|
|
std::auto_ptr<char> nm(new char[num]); |
|
|
|
|
if (!check(SCardListReaders(_id, grp.size()?grp.data():0, |
|
|
|
|
std::auto_ptr<char_t> nm(new char_t[num]); |
|
|
|
|
if (!check(SCardListReaders(_id, grp.size()?strconv(grp).data():0, |
|
|
|
|
nm.get(), &num))) |
|
|
|
|
return res; |
|
|
|
|
return res = split(std::string(nm.get(), num-1)); |
|
|
|
|
return res = split(strconv(string(nm.get(), num-1))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//! Get a reader, open a connection if not already open.
|
|
|
|
@ -352,7 +379,13 @@ namespace pcsc { |
|
|
|
|
|
|
|
|
|
//! Get the describing text of the last error
|
|
|
|
|
std::string error() const { |
|
|
|
|
#ifdef WIN32 |
|
|
|
|
std::stringstream ss; |
|
|
|
|
ss<<"PCSC state: "<<_state; |
|
|
|
|
return ss.str(); |
|
|
|
|
#else |
|
|
|
|
return pcsc_stringify_error(_state); |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//................................................................methods
|
|
|
|
@ -396,7 +429,7 @@ namespace pcsc { |
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
bool _exc; |
|
|
|
|
long _id; |
|
|
|
|
SCARDCONTEXT _id; |
|
|
|
|
long _state; |
|
|
|
|
std::map<std::string, boost::shared_ptr<Reader> > _reader; |
|
|
|
|
|
|
|
|
|