Package smartcard :: Package scard :: Module scard
[hide private]
[frames] | no frames]

Module scard

source code


The smartcard.scard module is a simple wrapper on top of the C language
PCSC SCardXXX API.

The smartcard.scard module is the lower layer of the pyscard
framework that provides a higher level interface.

You should avoid using the smartcard.scard package directly, and use the
pyscard directly because:

 - smartcard.scard being a C wrapper, the code tends to look like C code
   written in python syntax

 - the smartcard package provides higher level abstractions (e.g.
   CardType, CardConnection), and makes programming easier since it is
   totally written in Python

You can still use the smartcard.scard package if you want to write your
own framework, or if you want to perform quick-and-dirty port of C
language programs using SCardXXX calls, or if there are features of
SCardXXX API that you want to use and that are not available in the
pyscard library.

Introduction

The smartcard.scard module is a Python wrapper around PCSC smart card base
services.  On Windows, the wrapper is performed around the smart card base
components winscard library.  On linux and OS X, the wrapper is performed
around the PCSC-lite library.


The smartcard.scard module provides mapping for the following API functions,
depending on the Operating System:

=============================== ======= =======
Function                        Windows  Linux 
                                         OS X  
=============================== ======= =======
GetOpenCardName                                
SCardAddReaderToGroup              Y           
SCardBeginTransaction              Y       Y   
SCardCancel                        Y       Y   
SCardConnect                       Y       Y   
SCardControl                       Y       Y   
SCardDisconnect                    Y       Y   
SCardEndTransaction                Y       Y   
SCardEstablishConteYt              Y       Y   
SCardForgetCardType                Y           
SCardForgetReader                  Y           
SCardForgetReaderGroup             Y           
SCardFreeMemory                                
SCardGetAttrib                     Y       Y   
SCardGetCardTypeProviderName       Y           
SCardGetErrorMessage               Y           
SCardGetProviderId                             
SCardGetStatusChange               Y       Y   
SCardIntroduceCardType             Y           
SCardIntroduceReader               Y           
SCardIntroduceReaderGroup          Y           
SCardIsValidConteYt                Y       Y   
SCardListCards                     Y           
SCardListInterfaces                Y           
SCardListReaderGroups              Y       Y   
SCardListReaders                   Y       Y   
SCardLocateCards                   Y           
SCardReconnect                     Y       Y   
SCardReleaseConteYt                Y       Y   
SCardRemoveReaderFromGroup         Y           
SCardSetAttrib                     Y       Y   
SCardSetCartTypeProviderName                   
SCardStatus                        Y       Y   
SCardTransmit                      Y       Y   
SCardUIDlgSelectCard                           
=============================== ======= =======

Comments, bug reports, improvements welcome.

-------------------------------------------------------------------------------
Copyright 2001-2012 gemalto
@Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
@Author: Ludovic Rousseau, mailto:ludovic.rousseau@free.fr

This file is part of pyscard.

pyscard is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.

pyscard 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Functions [hide private]
 
_swig_setattr_nondynamic(self, class_type, name, value, static=1) source code
 
_swig_setattr(self, class_type, name, value) source code
 
_swig_getattr(self, class_type, name) source code
 
_swig_repr(self) source code
SCARDRETCODE
SCardIsValidContext(hcontext)
Parameters ---------- hcontext: context handle return from SCardEstablishContext()
source code
SCARDRETCODE
SCardGetAttrib(hcard, dwAttrId)
Parameters ---------- hcard: card handle return from SCardConnect() dwAttrId: value of attribute to get
source code
 
SCardSetAttrib(hcard, dwAttrId, ATTRIBUTESIN)
SCardSetAttrib( hcard, dwAttrId, BYTELIST * ATTRIBUTESIN) -> SCARDRETCODE
source code
 
SCardControl(hcard, dwControlCode, inbuffer)
SCardControl( hcard, dwControlCode, byte[] inbuffer) -> SCARDRETCODE
source code
SCARDRETCODE
SCardBeginTransaction(hcard)
Parameters ---------- hcard: card handle return from SCardConnect()
source code
SCARDRETCODE
SCardCancel(hcontext)
Parameters ---------- hcontext: context handle return from SCardEstablishContext()
source code
SCARDRETCODE
SCardConnect(hcontext, readername, dwShareMode, dwPreferredProtocols)
Parameters ---------- hcontext: context handle return from SCardEstablishContext() readername: card reader name dwShareMode: share mode dwPreferredProtocols: preferred protocols
source code
SCARDRETCODE
SCardDisconnect(hcard, dwDisposition)
Parameters ---------- hcard: card handle return from SCardConnect() dwDisposition: card disposition on return
source code
SCARDRETCODE
SCardEndTransaction(hcard, dwDisposition)
Parameters ---------- hcard: card handle return from SCardConnect() dwDisposition: card disposition on return
source code
SCARDRETCODE
SCardEstablishContext(dwScope)
Parameters ---------- dwScope: context scope
source code
 
SCardGetStatusChange(hcontext, dwTimeout, readerstatelist)
SCardGetStatusChange( hcontext, dwTimeout, tuple[] readerstatelist) -> SCARDRETCODE
source code
 
SCardListReaders(hcontext, readergroups)
SCardListReaders( hcontext, [] readergroups) -> SCARDRETCODE
source code
SCARDRETCODE
SCardListReaderGroups(hcontext)
Parameters ---------- hcontext: context handle return from SCardEstablishContext()
source code
SCARDRETCODE
SCardReconnect(hcard, dwShareMode, dwPreferredProtocols, dwInitialization)
Parameters ---------- hcard: card handle return from SCardConnect() dwShareMode: share mode dwPreferredProtocols: preferred protocols dwInitialization: the type of initialization that should be performed on the card
source code
SCARDRETCODE
SCardReleaseContext(hcontext)
Parameters...
source code
SCARDRETCODE
SCardStatus(hcard)
Parameters ---------- hcard: card handle return from SCardConnect()
source code
 
SCardTransmit(hcard, pioSendPci, apducommand)
SCardTransmit( hcard, unsigned long pioSendPci, byte[] apducommand) -> SCARDRETCODE
source code
 
SCARD_CTL_CODE(code)
SCARD_CTL_CODE(long code) -> long
source code
 
SCardGetErrorMessage(lErrCode)
SCardGetErrorMessage(long lErrCode) -> ERRORSTRING *
source code
Variables [hide private]
  _newclass = 1
  SCARD_SCOPE_USER = 0
  SCARD_SCOPE_TERMINAL = 1
  SCARD_SCOPE_SYSTEM = 2
  SCARD_SHARE_SHARED = 2
  SCARD_SHARE_EXCLUSIVE = 1
  SCARD_SHARE_DIRECT = 3
  SCARD_LEAVE_CARD = 0
  SCARD_RESET_CARD = 1
  SCARD_UNPOWER_CARD = 2
  SCARD_EJECT_CARD = 3
  SCARD_STATE_UNAWARE = 0
  SCARD_STATE_IGNORE = 1
  SCARD_STATE_CHANGED = 2
  SCARD_STATE_UNKNOWN = 4
  SCARD_STATE_UNAVAILABLE = 8
  SCARD_STATE_EMPTY = 16
  SCARD_STATE_PRESENT = 32
  SCARD_STATE_ATRMATCH = 64
  SCARD_STATE_EXCLUSIVE = 128
  SCARD_STATE_INUSE = 256
  SCARD_STATE_MUTE = 512
  SCARD_STATE_UNPOWERED = 1024
  SCARD_PROTOCOL_UNSET = 0
  SCARD_PROTOCOL_T0 = 1
  SCARD_PROTOCOL_T1 = 2
  SCARD_PROTOCOL_RAW = 4
  SCARD_PROTOCOL_T15 = 8
  SCARD_PROTOCOL_ANY = 3
  SCARD_PROTOCOL_UNDEFINED = 0
  SCARD_PROTOCOL_OPTIMAL = 0
  SCARD_PROTOCOL_Tx = 3
  SCARD_PROTOCOL_DEFAULT = 3
  SCARD_PCI_T0 = 1
  SCARD_PCI_T1 = 2
  SCARD_PCI_RAW = 4
  SCARD_ATTR_VENDOR_NAME = 65792
  SCARD_ATTR_VENDOR_IFD_TYPE = 65793
  SCARD_ATTR_VENDOR_IFD_VERSION = 65794
  SCARD_ATTR_VENDOR_IFD_SERIAL_NO = 65795
  SCARD_ATTR_CHANNEL_ID = 131344
  SCARD_ATTR_ASYNC_PROTOCOL_TYPES = 196896
  SCARD_ATTR_DEFAULT_CLK = 196897
  SCARD_ATTR_MAX_CLK = 196898
  SCARD_ATTR_DEFAULT_DATA_RATE = 196899
  SCARD_ATTR_MAX_DATA_RATE = 196900
  SCARD_ATTR_MAX_IFSD = 196901
  SCARD_ATTR_SYNC_PROTOCOL_TYPES = 196902
  SCARD_ATTR_POWER_MGMT_SUPPORT = 262449
  SCARD_ATTR_USER_TO_CARD_AUTH_DEVICE = 328000
  SCARD_ATTR_USER_AUTH_INPUT_DEVICE = 328002
  SCARD_ATTR_CHARACTERISTICS = 393552
  SCARD_ATTR_CURRENT_PROTOCOL_TYPE = 524801
  SCARD_ATTR_CURRENT_CLK = 524802
  SCARD_ATTR_CURRENT_F = 524803
  SCARD_ATTR_CURRENT_D = 524804
  SCARD_ATTR_CURRENT_N = 524805
  SCARD_ATTR_CURRENT_W = 524806
  SCARD_ATTR_CURRENT_IFSC = 524807
  SCARD_ATTR_CURRENT_IFSD = 524808
  SCARD_ATTR_CURRENT_BWT = 524809
  SCARD_ATTR_CURRENT_CWT = 524810
  SCARD_ATTR_CURRENT_EBC_ENCODING = 524811
  SCARD_ATTR_EXTENDED_BWT = 524812
  SCARD_ATTR_ICC_PRESENCE = 590592
  SCARD_ATTR_ICC_INTERFACE_STATUS = 590593
  SCARD_ATTR_CURRENT_IO_STATE = 590594
  SCARD_ATTR_ATR_STRING = 590595
  SCARD_ATTR_ICC_TYPE_PER_ATR = 590596
  SCARD_ATTR_ESC_RESET = 499712
  SCARD_ATTR_ESC_CANCEL = 499715
  SCARD_ATTR_ESC_AUTHREQUEST = 499717
  SCARD_ATTR_MAXINPUT = 499719
  SCARD_ATTR_DEVICE_UNIT = 2147418113
  SCARD_ATTR_DEVICE_IN_USE = 2147418114
  SCARD_ATTR_DEVICE_FRIENDLY_NAME_A = 2147418115
  SCARD_ATTR_DEVICE_SYSTEM_NAME_A = 2147418116
  SCARD_ATTR_DEVICE_FRIENDLY_NAME_W = 2147418117
  SCARD_ATTR_DEVICE_SYSTEM_NAME_W = 2147418118
  SCARD_ATTR_SUPRESS_T1_IFS_REQUEST = 2147418119
  SCARD_ATTR_DEVICE_FRIENDLY_NAME = 2147418115
  SCARD_ATTR_DEVICE_SYSTEM_NAME = 2147418116
  SCARD_S_SUCCESS = 0
  SCARD_F_INTERNAL_ERROR = 2148532225
  SCARD_E_CANCELLED = 2148532226
  SCARD_E_INVALID_HANDLE = 2148532227
  SCARD_E_INVALID_PARAMETER = 2148532228
  SCARD_E_INVALID_TARGET = 2148532229
  SCARD_E_NO_MEMORY = 2148532230
  SCARD_F_WAITED_TOO_LONG = 2148532231
  SCARD_E_INSUFFICIENT_BUFFER = 2148532232
  SCARD_E_UNKNOWN_READER = 2148532233
  SCARD_E_TIMEOUT = 2148532234
  SCARD_E_SHARING_VIOLATION = 2148532235
  SCARD_E_NO_SMARTCARD = 2148532236
  SCARD_E_UNKNOWN_CARD = 2148532237
  SCARD_E_CANT_DISPOSE = 2148532238
  SCARD_E_PROTO_MISMATCH = 2148532239
  SCARD_E_NOT_READY = 2148532240
  SCARD_E_INVALID_VALUE = 2148532241
  SCARD_E_SYSTEM_CANCELLED = 2148532242
  SCARD_F_COMM_ERROR = 2148532243
  SCARD_F_UNKNOWN_ERROR = 2148532244
  SCARD_E_INVALID_ATR = 2148532245
  SCARD_E_NOT_TRANSACTED = 2148532246
  SCARD_E_READER_UNAVAILABLE = 2148532247
  SCARD_E_PCI_TOO_SMALL = 2148532249
  SCARD_E_READER_UNSUPPORTED = 2148532250
  SCARD_E_DUPLICATE_READER = 2148532251
  SCARD_E_CARD_UNSUPPORTED = 2148532252
  SCARD_E_NO_SERVICE = 2148532253
  SCARD_E_SERVICE_STOPPED = 2148532254
  SCARD_E_NO_READERS_AVAILABLE = 2148532270
  SCARD_E_UNSUPPORTED_FEATURE = 2148532255
  SCARD_W_UNSUPPORTED_CARD = 2148532325
  SCARD_W_UNRESPONSIVE_CARD = 2148532326
  SCARD_W_UNPOWERED_CARD = 2148532327
  SCARD_W_RESET_CARD = 2148532328
  SCARD_W_REMOVED_CARD = 2148532329
  SCARD_W_SECURITY_VIOLATION = 2148532330
  SCARD_W_WRONG_CHV = 2148532331
  SCARD_W_CHV_BLOCKED = 2148532332
  SCARD_W_EOF = 2148532333
  SCARD_W_CANCELLED_BY_USER = 2148532334
  SCARD_W_CARD_NOT_AUTHENTICATED = 2148532335
  SCARD_E_UNEXPECTED = 2148532255
  SCARD_E_ICC_INSTALLATION = 2148532256
  SCARD_E_ICC_CREATEORDER = 2148532257
  SCARD_E_DIR_NOT_FOUND = 2148532259
  SCARD_E_FILE_NOT_FOUND = 2148532260
  SCARD_E_NO_DIR = 2148532261
  SCARD_E_NO_FILE = 2148532262
  SCARD_E_NO_ACCESS = 2148532263
  SCARD_E_WRITE_TOO_MANY = 2148532264
  SCARD_E_BAD_SEEK = 2148532265
  SCARD_E_INVALID_CHV = 2148532266
  SCARD_E_UNKNOWN_RES_MNG = 2148532267
  SCARD_E_NO_SUCH_CERTIFICATE = 2148532268
  SCARD_E_CERTIFICATE_UNAVAILABLE = 2148532269
  SCARD_E_COMM_DATA_LOST = 2148532271
  SCARD_E_NO_KEY_CONTAINER = 2148532272
  SCARD_E_SERVER_TOO_BUSY = 2148532273
  INVALID_HANDLE = 2148532227
  SCARD_P_SHUTDOWN = 2148532248
  INFINITE = 2147483647
  resourceManager = 'pcsclite'
  resourceManagerSubType = 'pcsclite-linux'
  __package__ = 'smartcard.scard'
Function Details [hide private]

SCardIsValidContext(hcontext)

source code 
Parameters
----------
hcontext: context handle return from SCardEstablishContext()



This function determines whether a smart card context handle is still
valid.  After a smart card context handle has been set by
SCardEstablishContext(), it may become not valid if the resource manager
service has been shut down.

from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult = SCardIsValidContext(hcontext)
if hresult != SCARD_S_SUCCESS:
    raise error, 'Invalid context: ' + SCardGetErrorMessage(hresult)
...

Returns: SCARDRETCODE

SCardGetAttrib(hcard, dwAttrId)

source code 
Parameters
----------
hcard: card handle return from SCardConnect()
dwAttrId: value of attribute to get




This function get an attribute from the IFD Handler.

The possible attributes are:

======================================== ======= =======
Attribute                                Windows  PSCS  
                                                  lite
======================================== ======= =======
SCARD_ATTR_ASYNC_PROTOCOL_TYPES                     Y   
SCARD_ATTR_ATR_STRING                       Y       Y   
SCARD_ATTR_CHANNEL_ID                       Y       Y   
SCARD_ATTR_CHARACTERISTICS                  Y       Y   
SCARD_ATTR_CURRENT_BWT                      Y       Y   
SCARD_ATTR_CURRENT_CLK                      Y       Y   
SCARD_ATTR_CURRENT_CWT                      Y       Y   
SCARD_ATTR_CURRENT_D                        Y       Y   
SCARD_ATTR_CURRENT_EBC_ENCODING             Y       Y   
SCARD_ATTR_CURRENT_F                        Y       Y   
SCARD_ATTR_CURRENT_IFSC                     Y       Y   
SCARD_ATTR_CURRENT_IFSD                     Y       Y   
SCARD_ATTR_CURRENT_IO_STATE                 Y       Y   
SCARD_ATTR_CURRENT_N                        Y       Y   
SCARD_ATTR_CURRENT_PROTOCOL_TYPE            Y       Y   
SCARD_ATTR_CURRENT_W                        Y       Y   
SCARD_ATTR_DEFAULT_CLK                      Y       Y   
SCARD_ATTR_DEFAULT_DATA_RATE                Y       Y   
SCARD_ATTR_DEVICE_FRIENDLY_NAME_A           Y       Y   
SCARD_ATTR_DEVICE_FRIENDLY_NAME_W           Y       Y   
SCARD_ATTR_DEVICE_IN_USE                    Y       Y   
SCARD_ATTR_DEVICE_SYSTEM_NAME_A             Y       Y   
SCARD_ATTR_DEVICE_SYSTEM_NAME_W             Y       Y   
SCARD_ATTR_DEVICE_UNIT                      Y       Y   
SCARD_ATTR_ESC_AUTHREQUEST                  Y       Y   
SCARD_ATTR_ESC_CANCEL                       Y       Y   
SCARD_ATTR_ESC_RESET                        Y       Y   
SCARD_ATTR_EXTENDED_BWT                     Y       Y   
SCARD_ATTR_ICC_INTERFACE_STATUS             Y       Y   
SCARD_ATTR_ICC_PRESENCE                     Y       Y   
SCARD_ATTR_ICC_TYPE_PER_ATR                 Y       Y   
SCARD_ATTR_MAXINPUT                         Y       Y   
SCARD_ATTR_MAX_CLK                          Y       Y   
SCARD_ATTR_MAX_DATA_RATE                    Y       Y   
SCARD_ATTR_MAX_IFSD                         Y       Y   
SCARD_ATTR_POWER_MGMT_SUPPORT               Y       Y   
SCARD_ATTR_SUPRESS_T1_IFS_REQUEST           Y       Y   
SCARD_ATTR_SYNC_PROTOCOL_TYPES                      Y   
SCARD_ATTR_USER_AUTH_INPUT_DEVICE           Y       Y   
SCARD_ATTR_USER_TO_CARD_AUTH_DEVICE         Y       Y   
SCARD_ATTR_VENDOR_IFD_SERIAL_NO             Y       Y   
SCARD_ATTR_VENDOR_IFD_TYPE                  Y       Y   
SCARD_ATTR_VENDOR_IFD_VERSION               Y       Y   
SCARD_ATTR_VENDOR_NAME                      Y       Y   
======================================== ======= =======

Not all the dwAttrId values listed above may be implemented in the IFD
Handler you are using.  And some dwAttrId values not listed here may be
implemented.


from smartcard.scard import *
... establish context and connect to card ...
hresult, attrib = SCardGetAttrib(hcard, SCARD_ATTR_ATR_STRING)
if hresult == SCARD_S_SUCCESS:
    for j in attrib:
         print '0x%.2X' % attrib,
...

Returns: SCARDRETCODE

SCardSetAttrib(hcard, dwAttrId, ATTRIBUTESIN)

source code 

SCardSetAttrib( hcard,  dwAttrId, BYTELIST * ATTRIBUTESIN) -> SCARDRETCODE

Parameters
----------
hcard: card handle return from SCardConnect()
dwAttrId: value of attribute to get
ATTRIBUTESIN: BYTELIST *




This function sets an attribute from the IFD Handler. Not all
attributes are supported by all readers nor can they be set at all
times.

The possible attributes are:

======================================== ======= =======
Attribute                                Windows  PSCS  
                                                  lite
======================================== ======= =======
SCARD_ATTR_ASYNC_PROTOCOL_TYPES                     Y   
SCARD_ATTR_ATR_STRING                       Y       Y   
SCARD_ATTR_CHANNEL_ID                       Y       Y   
SCARD_ATTR_CHARACTERISTICS                  Y       Y   
SCARD_ATTR_CURRENT_BWT                      Y       Y   
SCARD_ATTR_CURRENT_CLK                      Y       Y   
SCARD_ATTR_CURRENT_CWT                      Y       Y   
SCARD_ATTR_CURRENT_D                        Y       Y   
SCARD_ATTR_CURRENT_EBC_ENCODING             Y       Y   
SCARD_ATTR_CURRENT_F                        Y       Y   
SCARD_ATTR_CURRENT_IFSC                     Y       Y   
SCARD_ATTR_CURRENT_IFSD                     Y       Y   
SCARD_ATTR_CURRENT_IO_STATE                 Y       Y   
SCARD_ATTR_CURRENT_N                        Y       Y   
SCARD_ATTR_CURRENT_PROTOCOL_TYPE            Y       Y   
SCARD_ATTR_CURRENT_W                        Y       Y   
SCARD_ATTR_DEFAULT_CLK                      Y       Y   
SCARD_ATTR_DEFAULT_DATA_RATE                Y       Y   
SCARD_ATTR_DEVICE_FRIENDLY_NAME_A           Y       Y   
SCARD_ATTR_DEVICE_FRIENDLY_NAME_W           Y       Y   
SCARD_ATTR_DEVICE_IN_USE                    Y       Y   
SCARD_ATTR_DEVICE_SYSTEM_NAME_A             Y       Y   
SCARD_ATTR_DEVICE_SYSTEM_NAME_W             Y       Y   
SCARD_ATTR_DEVICE_UNIT                      Y       Y   
SCARD_ATTR_ESC_AUTHREQUEST                  Y       Y   
SCARD_ATTR_ESC_CANCEL                       Y       Y   
SCARD_ATTR_ESC_RESET                        Y       Y   
SCARD_ATTR_EXTENDED_BWT                     Y       Y   
SCARD_ATTR_ICC_INTERFACE_STATUS             Y       Y   
SCARD_ATTR_ICC_PRESENCE                     Y       Y   
SCARD_ATTR_ICC_TYPE_PER_ATR                 Y       Y   
SCARD_ATTR_MAXINPUT                         Y       Y   
SCARD_ATTR_MAX_CLK                          Y       Y   
SCARD_ATTR_MAX_DATA_RATE                    Y       Y   
SCARD_ATTR_MAX_IFSD                         Y       Y   
SCARD_ATTR_POWER_MGMT_SUPPORT               Y       Y   
SCARD_ATTR_SUPRESS_T1_IFS_REQUEST           Y       Y   
SCARD_ATTR_SYNC_PROTOCOL_TYPES                      Y   
SCARD_ATTR_USER_AUTH_INPUT_DEVICE           Y       Y   
SCARD_ATTR_USER_TO_CARD_AUTH_DEVICE         Y       Y   
SCARD_ATTR_VENDOR_IFD_SERIAL_NO             Y       Y   
SCARD_ATTR_VENDOR_IFD_TYPE                  Y       Y   
SCARD_ATTR_VENDOR_IFD_VERSION               Y       Y   
SCARD_ATTR_VENDOR_NAME                      Y       Y   
======================================== ======= =======

Not all the dwAttrId values listed above may be implemented in the IFD
Handler you are using.  And some dwAttrId values not listed here may be
implemented.


from smartcard.scard import *
... establish context and connect to card ...
hresult, attrib = SCardSetAttrib(hcard, SCARD_ATTR_VENDOR_NAME, ['G', 'e', 'm', 'a', 'l', 't', 'o'])
if hresult != SCARD_S_SUCCESS:
     print 'Failed to set attribute'
...

SCardControl(hcard, dwControlCode, inbuffer)

source code 

SCardControl( hcard,  dwControlCode, byte[] inbuffer) -> SCARDRETCODE

Parameters
----------
hcard: card handle return from SCardConnect()
dwControlCode: the control code to send
inbuffer: list of bytes to send with the control code



This function sends a control command to the reader connected to by
SCardConnect().  It returns a result and the control response.


from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, hcard, dwActiveProtocol = SCardConnect(
     hcontext, 'SchlumbergerSema Reflex USB v.2 0', SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0)
CMD = [0x12, 0x34]
hresult, response = SCardControl(hcard, 42, CMD)
if hresult != SCARD_S_SUCCESS:
    raise error, 'Failed to control: ' + SCardGetErrorMessage(hresult)

SCardBeginTransaction(hcard)

source code 
Parameters
----------
hcard: card handle return from SCardConnect()



This function establishes a temporary exclusive access mode for doing a
series of commands or transaction.  You might want to use this when you
are selecting a few files and then writing a large file so you can make
sure that another application will not change the current file.  If
another application has a lock on this reader or this application is in
SCARD_SHARE_EXCLUSIVE there will be no action taken.

from smartcard.scard import *
... establish context ...
hresult, hcard, dwActiveProtocol = SCardConnect(
    hcontext, 'SchlumbergerSema Reflex USB v.2 0', SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0)
if hresult!=SCARD_S_SUCCESS:
    raise error, 'unable to connect: ' + SCardGetErrorMessage(hresult)
hresult = SCardBeginTransaction(hcard)
if hresult != SCARD_S_SUCCESS:
    raise error, 'failed to begin transaction: ' + SCardGetErrorMessage(hresult)
...

Returns: SCARDRETCODE

SCardCancel(hcontext)

source code 
Parameters
----------
hcontext: context handle return from SCardEstablishContext()



This function cancels all pending blocking requests on the
ScardGetStatusChange() function.

from smartcard.scard import *
... establish context ...
hresult = SCardCancel(hcard)
if hresult != SCARD_S_SUCCESS:
    raise error, 'failed to cancel pending actions: ' + SCardGetErrorMessage(hresult)
...

Returns: SCARDRETCODE

SCardConnect(hcontext, readername, dwShareMode, dwPreferredProtocols)

source code 
Parameters
----------
hcontext: context handle return from SCardEstablishContext()
readername: card reader name
dwShareMode: share mode
dwPreferredProtocols: preferred protocols



This function establishes a connection to the friendly name of the reader
specified in szReader.  The first connection will power up and perform a
reset on the card.

Value of dwShareMode    Meaning
SCARD_SHARE_SHARED      This application will allow others to share the reader
SCARD_SHARE_EXCLUSIVE   This application will NOT allow others to share the reader
SCARD_SHARE_DIRECT      Direct control of the reader, even without a card

SCARD_SHARE_DIRECT can be used before using SCardControl() to send control
commands to the reader even if a card is not present in the reader.

Value of dwPreferredProtocols   Meaning
SCARD_PROTOCOL_T0               Use the T=0 protocol
SCARD_PROTOCOL_T1               Use the T=1 protocol
SCARD_PROTOCOL_RAW              Use with memory type cards

from smartcard.scard import *
... establish context ...
hresult, readers = SCardListReaders(hcontext, 'NULL')
if hresult != SCARD_S_SUCCESS:
    raise error, 'Failed to list readers:: ' + SCardGetErrorMessage(hresult)
hresult, hcard, dwActiveProtocol = SCardConnect(
    hcontext, readers[0], SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0)
if hresult != SCARD_S_SUCCESS:
    raise error, 'unable to connect: ' + SCardGetErrorMessage(hresult)
...

Returns: SCARDRETCODE

SCardDisconnect(hcard, dwDisposition)

source code 
Parameters
----------
hcard: card handle return from SCardConnect()
dwDisposition: card disposition on return



This function terminates a connection to the connection made through
SCardConnect.  disposition can have the following values:

Value of disposition    Meaning
SCARD_LEAVE_CARD        Do nothing
SCARD_RESET_CARD        Reset the card (warm reset)
SCARD_UNPOWER_CARD      Unpower the card (cold reset)
SCARD_EJECT_CARD        Eject the card

from smartcard.scard import *
... establish context and connect to card ...
hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD)
if hresult != SCARD_S_SUCCESS:
    raise error, 'failed to disconnect: ' + SCardGetErrorMessage(hresult)
...

Returns: SCARDRETCODE

SCardEndTransaction(hcard, dwDisposition)

source code 
Parameters
----------
hcard: card handle return from SCardConnect()
dwDisposition: card disposition on return




This function ends a previously begun transaction.  The calling
application must be the owner of the previously begun transaction or an
error will occur.  disposition can have the following values: The
disposition action is not currently used in this release.

Value of disposition    Meaning
SCARD_LEAVE_CARD        Do nothing
SCARD_RESET_CARD        Reset the card
SCARD_UNPOWER_CARD      Unpower the card
SCARD_EJECT_CARD        Eject the card

from smartcard.scard import *
... establish context, connect to card, begin transaction ...
hresult = SCardEndTransaction(hcard, SCARD_LEAVE_CARD)
if hresult != SCARD_S_SUCCESS:
    raise error, 'failed to end transaction: ' + SCardGetErrorMessage(hresult)

Returns: SCARDRETCODE

SCardEstablishContext(dwScope)

source code 
Parameters
----------
dwScope: context scope



This function creates a communication context to the PC/SC Resource
Manager.  This must be the first function called in a PC/SC application.

Value of dwScope        Meaning
SCARD_SCOPE_USER        Operations performed within the scope of the User
SCARD_SCOPE_TERMINAL    Not used
SCARD_SCOPE_GLOBAL      Not used
SCARD_SCOPE_SYSTEM      Operations performed within the scope of the system


from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
if hresult != SCARD_S_SUCCESS:
    raise error, 'Failed to establish context: ' + SCardGetErrorMessage(hresult)

Returns: SCARDRETCODE

SCardGetStatusChange(hcontext, dwTimeout, readerstatelist)

source code 

SCardGetStatusChange( hcontext,  dwTimeout, tuple[] readerstatelist) -> SCARDRETCODE

Parameters
----------
hcontext: context handle return from SCardEstablishContext()
dwTimeout: timeout value, INFINITE for infinite time-out
readerstatelist: in input/output, a list of reader state tuple (readername, state, atr)




This function receives a structure or list of tuples containing reader
states. A READERSTATE hast three fields (readername, state, atr).
It then blocks for a change in state to occur on any of the OR'd
values contained in the current state for a maximum blocking time of
dwTimeout or forever if INFINITE is used.  The new event state will be
contained in state.  A status change might be a card insertion or
removal event, a change in ATR, etc.

Value of state              Meaning
SCARD_STATE_UNAWARE         The application is unaware of the current state, and would like to know. The use of this value results in an immediate return from state transition monitoring services. This is represented by all bits set to zero
SCARD_STATE_IGNORE          This reader should be ignored
SCARD_STATE_CHANGED         There is a difference between the state believed by the application, and the state known by the resource manager. When this bit is set, the application may assume a significant state change has occurred on this reader
SCARD_STATE_UNKNOWN         The given reader name is not recognized by the resource manager. If this bit is set, then SCARD_STATE_CHANGED and SCARD_STATE_IGNORE will also be set
SCARD_STATE_UNAVAILABLE     The actual state of this reader is not available. If this bit is set, then all the following bits are clear
SCARD_STATE_EMPTY           There is no card in the reader. If this bit is set, all the following bits will be clear
SCARD_STATE_PRESENT         There is a card in the reader
SCARD_STATE_ATRMATCH        There is a card in the reader with an ATR matching one of the target cards. If this bit is set, SCARD_STATE_PRESENT will also be set. This bit is only returned on the SCardLocateCards function
SCARD_STATE_EXCLUSIVE       The card in the reader is allocated for exclusive use by another application. If this bit is set, SCARD_STATE_PRESENT will also be set
SCARD_STATE_INUSE           The card in the reader is in use by one or more other applications, but may be connected to in shared mode. If this bit is set, SCARD_STATE_PRESENT will also be set
SCARD_STATE_MUTE            There is an unresponsive card in the reader


from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, readers = SCardListReaders(hcontext, [])
readerstates = []
cards = [ 'Schlumberger Cryptoflex 4k', 'Schlumberger Cryptoflex 8k', 'Schlumberger Cryptoflex 8k v2' ]
for i in xrange(len(readers)):
    readerstates += [ (readers[i], SCARD_STATE_UNAWARE) ]
hresult, newstates = SCardLocateCards(hcontext, cards, readerstates)
print '----- Please insert or remove a card ------------'
hresult, newstates = SCardGetStatusChange(hcontext, INFINITE, newstates)
for i in newstates
     reader, eventstate, atr = i
    if eventstate & SCARD_STATE_ATRMATCH:
        print '     Card found'
    if eventstate & SCARD_STATE_EMPTY:
        print '     Reader empty'

SCardListReaders(hcontext, readergroups)

source code 

SCardListReaders( hcontext, [] readergroups) -> SCARDRETCODE

Parameters
----------
hcontext: context handle return from SCardEstablishContext()
readergroups: a list of reader groups to search for readers



This function returns a list of currently available readers on the system.
A list of group can be provided in input to list readers in a given
group only.

from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, readers = SCardListReaders(hcontext, [])
if hresult != SCARD_S_SUCCESS:
    raise error, 'Failed to list readers: ' + SCardGetErrorMessage(hresult)
print 'PCSC Readers: ', readers
hresult, readers = SCardListReaders(hcontext, ['SCard$T1ProtocolReaders', 'SCard$MyOwnGroup']
...

SCardListReaderGroups(hcontext)

source code 
Parameters
----------
hcontext: context handle return from SCardEstablishContext()



This function returns a list of currently available reader groups on the
system.

from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, readerGroups = SCardListReaderGroups(hcontext)
if hresult != SCARD_S_SUCCESS:
    raise error, 'Unable to list reader groups: ' + SCardGetErrorMessage(hresult)
print 'PCSC Reader groups: ', readerGroups

Returns: SCARDRETCODE

SCardReconnect(hcard, dwShareMode, dwPreferredProtocols, dwInitialization)

source code 
Parameters
----------
hcard: card handle return from SCardConnect()
dwShareMode: share mode
dwPreferredProtocols: preferred protocols
dwInitialization: the type of initialization that should be performed on the card




This function reestablishes a connection to a reader that was previously
connected to using SCardConnect().  In a multi application environment it
is possible for an application to reset the card in shared mode.  When
this occurs any other application trying to access certain commands will
be returned the value SCARD_W_RESET_CARD.  When this occurs
SCardReconnect() must be called in order to acknowledge that the card was
reset and allow it to change it's state accordingly.

Value of dwShareMode    Meaning
SCARD_SHARE_SHARED      This application will allow others to share the reader
SCARD_SHARE_EXCLUSIVE   This application will NOT allow others to share the reader

Value of dwPreferredProtocols   Meaning
SCARD_PROTOCOL_T0               Use the T=0 protocol
SCARD_PROTOCOL_T1               Use the T=1 protocol
SCARD_PROTOCOL_RAW              Use with memory type cards

dwPreferredProtocols is a bit mask of acceptable protocols for the connection. You can use (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) if you do not have a preferred protocol.

Value of dwInitialization   Meaning
SCARD_LEAVE_CARD            Do nothing
SCARD_RESET_CARD            Reset the card (warm reset)
SCARD_UNPOWER_CARD          Unpower the card (cold reset)
SCARD_EJECT_CARD            Eject the card


from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, hcard, dwActiveProtocol = SCardConnect(
    hcontext, 'SchlumbergerSema Reflex USB v.2 0', SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0)
hresult, activeProtocol = SCardReconnect(hcard, SCARD_SHARE_EXCLUSIVE,
    SCARD_PROTOCOL_T0, SCARD_RESET_CARD)
...

Returns: SCARDRETCODE

SCardReleaseContext(hcontext)

source code 
Parameters
----------
hcontext: context handle return from SCardEstablishContext()

Returns: SCARDRETCODE

SCardStatus(hcard)

source code 
Parameters
----------
hcard: card handle return from SCardConnect()



This function returns the current status of the reader connected to by
hcard.  The reader friendly name is returned, as well as the state,
protocol and ATR.  The state is a DWORD possibly OR'd with the following
values:

Value of pdwState   Meaning
SCARD_ABSENT        There is no card in the reader
SCARD_PRESENT       There is a card in the reader, but it has not been moved into position for use
SCARD_SWALLOWED     There is a card in the reader in position for use. The card is not powered
SCARD_POWERED       Power is being provided to the card, but the reader driver is unaware of the mode of the card
SCARD_NEGOTIABLE    The card has been reset and is awaiting PTS negotiation
SCARD_SPECIFIC      The card has been reset and specific communication protocols have been established

Value of pdwProtocol    Meaning
SCARD_PROTOCOL_T0       Use the T=0 protocol
SCARD_PROTOCOL_T1       Use the T=1 protocol


from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, hcard, dwActiveProtocol = SCardConnect(
         hcontext, 'SchlumbergerSema Reflex USB v.2 0', SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0)
hresult, reader, state, protocol, atr = SCardStatus(hcard)
if hresult != SCARD_S_SUCCESS:
    raise error, 'failed to get status: ' + SCardGetErrorMessage(hresult)
print 'Reader: ', reader
print 'State: ', state
print 'Protocol: ', protocol
print 'ATR: ',
for i in xrange(len(atr)):
    print '0x%.2X' % i,
print 
...

Returns: SCARDRETCODE

SCardTransmit(hcard, pioSendPci, apducommand)

source code 

SCardTransmit( hcard, unsigned long pioSendPci, byte[] apducommand) -> SCARDRETCODE

Parameters
----------
hcard: card handle return from SCardConnect()
pioSendPci: unsigned long
apducommand: list of APDU bytes to transmit



This function sends an APDU to the smart card contained in the reader
connected to by SCardConnect().
It returns a result and the card APDU response.

Value of pioSendPci     Meaning
SCARD_PCI_T0            Pre-defined T=0 PCI structure
SCARD_PCI_T1            Pre-defined T=1 PCI structure


from smartcard.scard import *
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
hresult, hcard, dwActiveProtocol = SCardConnect(
     hcontext, 'SchlumbergerSema Reflex USB v.2 0', SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0)
SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02]
DF_TELECOM = [0x7F, 0x10]
hresult, response = SCardTransmit(hcard, SCARD_PCI_T0, SELECT + DF_TELECOM)
if hresult != SCARD_S_SUCCESS:
    raise error, 'Failed to transmit: ' + SCardGetErrorMessage(hresult)

SCARD_CTL_CODE(code)

source code 

SCARD_CTL_CODE(long code) -> long

Parameters
----------
code: long



This function returns the value of a control code

from smartcard.scard import *
...
CM_IOCTL_GET_FEATURE_REQUEST = SCARD_CTL_CODE(3400)
...

SCardGetErrorMessage(lErrCode)

source code 

SCardGetErrorMessage(long lErrCode) -> ERRORSTRING *

Parameters
----------
lErrCode: long



This function return a human readable text for the given PC/SC error code.

from smartcard.scard import *
...
hresult, response = SCardTransmit(hcard, SCARD_PCI_T0, SELECT + DF_TELECOM)
if hresult != SCARD_S_SUCCESS:
    raise error, 'Failed to transmit: ' + SCardGetErrorMessage(hresult)
...