def addObserver(self, observer): """Add an observer.""" Observable.addObserver(self, observer) # If self.startOnDemand is True, the reader monitoring # thread only runs when there are observers. if self.startOnDemand: if 0 < self.countObservers(): if not self.rmthread: self.rmthread = ReaderMonitoringThread( self, self.readerProc, self.period) # start reader monitoring thread in another thread to # avoid a deadlock; addObserver and notifyObservers called # in the ReaderMonitoringThread run() method are # synchronized try: # Python 3.x import _thread _thread.start_new_thread(self.rmthread.start, ()) except: # Python 2.x import thread thread.start_new_thread(self.rmthread.start, ()) else: observer.update(self, (self.rmthread.readers, []))
def addObserver(self, observer): """Add an observer.""" Observable.addObserver(self, observer) # If self.startOnDemand is True, the reader monitoring # thread only runs when there are observers. if self.startOnDemand: if 0 < self.countObservers(): if not self.rmthread: self.rmthread = ReaderMonitoringThread(self, self.readerProc, self.period) # start reader monitoring thread in another thread to # avoid a deadlock; addObserver and notifyObservers called # in the ReaderMonitoringThread run() method are # synchronized try: # Python 3.x import _thread _thread.start_new_thread(self.rmthread.start, ()) except: # Python 2.x import thread thread.start_new_thread(self.rmthread.start, ()) else: observer.update(self, (self.rmthread.readers, []))
def deleteObserver(self, observer): """Remove an observer.""" Observable.deleteObserver(self, observer) # If self.startOnDemand is True, the reader monitoring # thread is stopped when there are no more observers. if self.startOnDemand: if 0 == self.countObservers(): self.rmthread.stop() del self.rmthread self.rmthread = None
def connect(self, protocol=None, mode=None, disposition=None): """Connect to card. protocol: a bit mask of the protocols to use, from CardConnection.T0_protocol, CardConnection.T1_protocol, CardConnection.RAW_protocol, CardConnection.T15_protocol mode: passed as-is to the PC/SC layer """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('connect'))
def __init__(self, reader): """Construct a new card connection. readerName: name of the reader in which the smartcard to connect to is located. """ Observable.__init__(self) self.reader = reader self.errorcheckingchain = None self.defaultprotocol = CardConnection.T0_protocol | CardConnection.T1_protocol
def deleteObserver(self, observer): """Remove an observer. We delete the CardMonitoringThread reference when there are no more observers. """ Observable.deleteObserver(self, observer) if _START_ON_DEMAND_: if self.countObservers() == 0: if self.rmthread != None: self.rmthread = None
def __init__(self, startOnDemand=True, readerProc=smartcard.System.readers, period=1): self.__dict__ = self.__shared_state Observable.__init__(self) self.startOnDemand = startOnDemand self.readerProc = readerProc self.period = period if self.startOnDemand: self.rmthread = None else: self.rmthread = ReaderMonitoringThread(self, self.readerProc, self.period) self.rmthread.start()
def addObserver(self, observer): """Add an observer. We only start the card monitoring thread when there are observers. """ Observable.addObserver(self, observer) if _START_ON_DEMAND_: if self.countObservers() > 0 and self.rmthread is None: self.rmthread = CardMonitoringThread(self) else: observer.update(self, (self.rmthread.cards, []))
def addObserver(self, observer): """Add an observer. We only start the card monitoring thread when there are observers. """ Observable.addObserver(self, observer) if _START_ON_DEMAND_: if self.countObservers() > 0 and self.rmthread == None: self.rmthread = CardMonitoringThread(self) else: observer.update(self, (self.rmthread.cards, []))
def getAttrib(self, attribId): """return the requested attribute @param attribId: attribute id like SCARD_ATTR_VENDOR_NAME """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('attrib', [attribId])) data = self.doGetAttrib(attribId) if None != self.errorcheckingchain: self.errorcheckingchain[0](data) return data
def transmit(self, bytes, protocol=None): """Transmit an apdu. Internally calls doTransmit() class method and notify observers upon command/response APDU events. Subclasses must override the doTransmit() class method. bytes: list of bytes to transmit protocol: the transmission protocol, from CardConnection.T0_protocol, CardConnection.T1_protocol, or CardConnection.RAW_protocol """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent( 'command', [bytes, protocol])) data, sw1, sw2 = self.doTransmit(bytes, protocol) Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent( 'response', [data, sw1, sw2])) if None != self.errorcheckingchain: self.errorcheckingchain[0](data, sw1, sw2) return data, sw1, sw2
def reconnect(self, protocol=None, mode=None, disposition=None): """Reconnect to card. @param protocol: a bit mask of the protocols to use, from L{CardConnection.T0_protocol}, L{CardConnection.T1_protocol}, L{CardConnection.RAW_protocol}, L{CardConnection.T15_protocol} @param mode: SCARD_SHARE_SHARED (default), SCARD_SHARE_EXCLUSIVE or SCARD_SHARE_DIRECT @param disposition: SCARD_LEAVE_CARD, SCARD_RESET_CARD (default), SCARD_UNPOWER_CARD or SCARD_EJECT_CARD """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('reconnect'))
def transmit(self, bytes, protocol=None): """Transmit an apdu. Internally calls doTransmit() class method and notify observers upon command/response APDU events. Subclasses must override the doTransmit() class method. @param bytes: list of bytes to transmit @param protocol: the transmission protocol, from CardConnection.T0_protocol, CardConnection.T1_protocol, or CardConnection.RAW_protocol """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent( 'command', [bytes, protocol])) data, sw1, sw2 = self.doTransmit(bytes, protocol) Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent( 'response', [data, sw1, sw2])) if self.errorcheckingchain is not None: self.errorcheckingchain[0](data, sw1, sw2) return data, sw1, sw2
def getAttrib(self, attribId): """return the requested attribute attribId: attribute id like SCARD_ATTR_VENDOR_NAME """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent( 'attrib', [attribId])) data = self.doGetAttrib(attribId) if None != self.errorcheckingchain: self.errorcheckingchain[0](data) return data
def connect(self, protocol=None, mode=None, disposition=None): """Connect to card. protocol: a bit mask of the protocols to use, from CardConnection.T0_protocol, CardConnection.T1_protocol, CardConnection.RAW_protocol, CardConnection.T15_protocol mode: SCARD_SHARE_SHARED (default), SCARD_SHARE_EXCLUSIVE or SCARD_SHARE_DIRECT disposition: SCARD_LEAVE_CARD (default), SCARD_RESET_CARD, SCARD_UNPOWER_CARD or SCARD_EJECT_CARD """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('connect'))
def control(self, controlCode, bytes=[]): """Send a control command and buffer. Internally calls doControl() class method and notify observers upon command/response events. Subclasses must override the doControl() class method. controlCode: command code bytes: list of bytes to transmit """ Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('command', [controlCode, bytes])) data = self.doControl(controlCode, bytes) Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('response', data)) if None != self.errorcheckingchain: self.errorcheckingchain[0](data) return data
def deleteObserver(self, observer): """Remove a CardConnection observer.""" Observable.deleteObserver(self, observer)
def addObserver(self, observer): """Add a CardConnection observer.""" Observable.addObserver(self, observer)
def disconnect(self): """Disconnect from card.""" Observable.setChanged(self) Observable.notifyObservers(self, CardConnectionEvent('disconnect'))
def __init__(self): Observable.__init__(self) if _START_ON_DEMAND_: self.rmthread = None else: self.rmthread = CardMonitoringThread(self)