def getReaderNames(self): """Returns the list of PCSC readers on which to wait for cards.""" # renew the context in case PC/SC was stopped # this happens on Windows when the last reader is disconnected self.hcontext = PCSCContext().getContext() # get inserted readers hresult, pcscreaders = SCardListReaders(self.hcontext, []) if SCARD_E_SERVICE_STOPPED == hresult: self.hcontext = PCSCContext().renewContext() hresult, pcscreaders = SCardListReaders(self.hcontext, []) if SCARD_E_NO_READERS_AVAILABLE == hresult: return [] if SCARD_S_SUCCESS != hresult: raise ListReadersException(hresult) readers = [] # if no readers asked, use all inserted readers if None == self.readersAsked: readers = pcscreaders # otherwise use only the asked readers that are inserted else: for reader in self.readersAsked: if not isinstance(reader, type("")): reader = str(reader) if reader in pcscreaders: readers = readers + [reader] return readers
def readers(groups=[]): creaders = [] hcontext = PCSCContext().getContext() try: pcsc_readers = __PCSCreaders__(hcontext, groups) except CardServiceStoppedException: hcontext = PCSCContext.renewContext() pcsc_readers = __PCSCreaders__(hcontext, groups) for reader in pcsc_readers: creaders.append(PCSCReader.Factory.create(reader)) return creaders
def readers(groups=[]): creaders = [] hcontext = PCSCContext().getContext() for reader in __PCSCreaders__(hcontext, groups): creaders.append(PCSCReader.Factory.create(reader)) return creaders
def __init__(self, newcardonly=False, readers=None, cardType=None, cardServiceClass=None, timeout=1, msr_active=None): """Construct new PCSCCardRequest. @param newcardonly: if True, request a new card default is False, i.e. accepts cards already inserted @param readers: the list of readers to consider for requesting a card default is to consider all readers @param cardType: the CardType class to wait for; default is AnyCardType, i.e. the request will returns with new or already inserted cards @param cardServiceClass: the specific card service class to create and bind to the card default is to create and bind a PassThruCardService @param timeout: the time in seconds we are ready to wait for connecting to the requested card. default is to wait one second to wait forever, set timeout to None """ AbstractCardRequest.__init__( self, newcardonly, readers, cardType, cardServiceClass, timeout) self.msr_active = True if msr_active == True else False # polling interval in s for SCardGetStatusChange self.pollinginterval = 0.01 if self.msr_active else 0.005 # normal 0.1 # if timeout is None, translate to scard.INFINITE if None == self.timeout: self.timeout = INFINITE # otherwise, from seconds to milliseconds else: self.timeout = int(self.timeout) self.hcontext = PCSCContext().getContext()
def loadPcsc(cards, autoload, loaded, autoconnect): """ try to load the pcsc context, this method must be called before any pcsc action if autoload is disabled """ if loaded.getValue(): return if not autoload.getValue(): raise Exception("pcsc is not loaded and autoload is disabled") # not already called in an import ? try: notice("context loading... please wait") PCSCContext() notice("context loaded") loaded.setValue(True) except EstablishContextException as e: message = str(e) import platform pf = platform.system() if pf == 'Darwin': message += ("\nHINT : connect a reader and use a tag/card with " "it, then retry the command") elif pf == 'Linux': message += ("\nHINT : check if the 'pcscd' daemon is running, " "maybe it has not been started or it crashed") elif pf == 'Windows': message += ("\nHINT : check if the 'scardsvr' service is running, " "maybe it has not been started or it crashed") else: message += "\nHINT : check the os process that manage card reader" raise DefaultPyshellException(message, LIBRARY_ERROR) # start thread to monitor card connection CardManager(cards, autoconnect)
def getReaderNames(self): """Returns the list or PCSC readers on which to wait for cards.""" self.hcontext = PCSCContext().getContext() # get inserted readers hresult, pcscreaders = SCardListReaders(self.hcontext, []) if 0 != hresult and SCARD_E_NO_READERS_AVAILABLE != hresult: raise ListReadersException(hresult) readers = [] # if no readers asked, use all inserted readers if None == self.readersAsked: readers = pcscreaders # otherwise use only the asked readers that are inserted else: for reader in self.readersAsked: if not isinstance(reader, type("")): reader = str(reader) if reader in pcscreaders: readers = readers + [reader] return readers
import sys if (sys.platform == 'darwin'): print "HINT : on macos system, try to execute this script with python2.6" from apdu.apdu import toHexString from apdu.apdu import ApduRaw #TODO #-faire une section critique entre les observeurs et les methods pour proteger : l'envi et la liste de card #-lorsqu'on quitte, se deconnecter? ou ca le fait deja tout seul? ########### TEST CONTEXT ############### try: print "context loading... please wait" PCSCContext() print "context loaded" except EstablishContextException as e: print " " + str(e) pf = platform.system() if pf == 'Darwin': print " HINT : connect a reader and use a tag/card with it, then retry the command" elif pf == 'Linux': print " HINT : check the 'pcscd' daemon, maybe it has not yet started or it crashed" elif pf == 'Windows': print " HINT : check the 'scardsvr' service, maybe it has not yet started or it crashed" else: print " HINT : check the os process that manage card reader" exit()