def scan(self, max_tags=1, encoding='Type A', data=None): # TODO: try max_tags=2 if data is None: data = [] if encoding == 'Type B': data = [0] elif encoding.startswith('FeliCa'): # This is suggested in the datasheet, but doesn't seem to work data = [0x00, 0xff, 0xff, 0x00, 0x00] brty = self.ENCODINGS.index(encoding) resp = self.send(0x4a, [max_tags, brty] + data) r = iter(resp) nbtg = next(r) if not nbtg: raise NoCardException('No cards found', hresult=-1) tags = [] if brty == 0: for i in range(nbtg): tagtype = 0x10 tags.append(self.parse_tag(tagtype, r)) else: raise NotImplementedError() return tags
def reconnect(self, protocol=None, mode=None, disposition=None): """Reconnect to the card. If protocol is not specified, connect with the default connection protocol. If mode is not specified, connect with SCARD_SHARE_SHARED. If disposition is not specified, do a warm reset (SCARD_RESET_CARD)""" CardConnection.reconnect(self, protocol) if self.hcard is None: raise CardConnectionException('Card not connected') pcscprotocol = translateprotocolmask(protocol) if 0 == pcscprotocol: pcscprotocol = self.getProtocol() if mode is None: mode = SCARD_SHARE_SHARED # store the way to dispose the card if disposition is None: disposition = SCARD_RESET_CARD self.disposition = disposition hresult, dwActiveProtocol = SCardReconnect(self.hcard, mode, pcscprotocol, self.disposition) if hresult != 0: self.hcard = None if hresult in (SCARD_W_REMOVED_CARD, SCARD_E_NO_SMARTCARD): raise NoCardException('Unable to reconnect', hresult=hresult) else: raise CardConnectionException( 'Unable to reconnect with protocol: ' + \ dictProtocol[pcscprotocol] + '. ' + \ SCardGetErrorMessage(hresult)) protocol = 0 if dwActiveProtocol == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1: # special case for T0 | T1 # this happen when mode=SCARD_SHARE_DIRECT and no protocol is # then negociated with the card protocol = CardConnection.T0_protocol | CardConnection.T1_protocol else: for p in dictProtocol: if p == dwActiveProtocol: protocol = eval("CardConnection.%s_protocol" % dictProtocol[p]) PCSCCardConnection.setProtocol(self, protocol)
def connect(self, protocol=None, mode=None, disposition=None): """Connect to the card. If protocol is not specified, connect with the default connection protocol. If mode is not specified, connect with SCARD_SHARE_SHARED.""" CardConnection.connect(self, protocol) pcscprotocol = translateprotocolmask(protocol) if 0 == pcscprotocol: pcscprotocol = self.getProtocol() if mode == None: mode = SCARD_SHARE_SHARED # store the way to dispose the card if disposition == None: disposition = SCARD_UNPOWER_CARD self.disposition = disposition hresult, self.hcard, dwActiveProtocol = SCardConnect( self.hcontext, str(self.reader), mode, pcscprotocol) if hresult != 0: self.hcard = None if SCARD_W_REMOVED_CARD == hresult: raise NoCardException( 'Unable to connect: ' + \ SCardGetErrorMessage(hresult)) else: raise CardConnectionException( 'Unable to connect with protocol: ' + \ dictProtocol[pcscprotocol] + '. ' + \ SCardGetErrorMessage(hresult)) protocol = 0 if dwActiveProtocol == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1: # special case for T0 | T1 # this happen when mode=SCARD_SHARE_DIRECT and no protocol is # then negociated with the card protocol = CardConnection.T0_protocol | CardConnection.T1_protocol else: for p in dictProtocol: if p == dwActiveProtocol: protocol = eval("CardConnection.%s_protocol" % dictProtocol[p]) PCSCCardConnection.setProtocol(self, protocol)
def autoscan(self, polls=1, ms=150, types=[0x20, 0x23, 0x4, 0x10, 0x11, 0x12]): if polls is None: polls = 0xff period = ms / 150 resp = self.send(0x60, [polls, period] + types) r = iter(resp) nbtg = next(r) if not nbtg: raise NoCardException('No cards found', hresult=-1) tags = [] for i in range(nbtg): tagtype = next(r) length = next(r) taginfo = [next(r) for i in range(length)] tags.append(self.parse_tag(tagtype, iter(taginfo))) return tags
def connect(self, protocol=None, mode=None, disposition=None): """Connect to the card. If protocol is not specified, connect with the default connection protocol. If mode is not specified, connect with SCARD_SHARE_SHARED.""" CardConnection.connect(self, protocol) pcscprotocol = translateprotocolmask(protocol) if 0 == pcscprotocol: pcscprotocol = self.getProtocol() if mode == None: mode = SCARD_SHARE_SHARED # store the way to dispose the card if disposition == None: disposition = SCARD_UNPOWER_CARD self.disposition = disposition hresult, self.hcard, dwActiveProtocol = SCardConnect( self.hcontext, str(self.reader), mode, pcscprotocol) if hresult != 0: self.hcard = None if SCARD_W_REMOVED_CARD == hresult: raise NoCardException('Unable to connect: ' + SCardGetErrorMessage(hresult)) else: raise CardConnectionException( 'Unable to connect with protocol: ' + dictProtocol[pcscprotocol] + '. ' + SCardGetErrorMessage(hresult)) protocol = 0 for p in dictProtocol: if p == dwActiveProtocol: protocol = eval("CardConnection.%s_protocol" % dictProtocol[p]) PCSCCardConnection.setProtocol(self, protocol)