class NFCScanner(object): """Thread reading data from NFC reader""" def __init__(self, config, msgQueue, ircThread): """Create worker reading UIDs from PN53x reader. """ self.authenticator = UidAuthenticator(config.authDbFilename) self.hmacAuthenticator = None self.desfireAuthenticator = None self.unknownUidTimeoutSecs = config.unknownUidTimeoutSecs self.lockOpenedSecs = config.lockOpenedSecs self.msgQueue = msgQueue self.ircThread = ircThread unlockerClassName = config.unlocker unlockerClass = getattr(unlocker, unlockerClassName) self.unlocker = unlockerClass(config) def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc ) self.desfireAuthenticator = DesfireEd25519Authenthicator( config.authDbFilename, self.nfc, config.desfirePubkey.decode("hex") ) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry e = threading.Event() e.wait(timeout=0.3) except NFCError, e: #this exception happens also when scanUID finds no cards logging.debug("Failed to find RFID cards in reader's field: %s", e) e = threading.Event() e.wait(timeout=0.2) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2) except Exception: logging.exception("Exception in main unlock thread")
class NFCScanner(object): """Thread reading data from NFC reader""" def __init__(self, config, msgQueue, ircThread): """Create worker reading UIDs from PN53x reader. """ self.authenticator = UidAuthenticator(config.authDbFilename) self.hmacAuthenticator = None self.desfireAuthenticator = None self.unknownUidTimeoutSecs = config.unknownUidTimeoutSecs self.lockOpenedSecs = config.lockOpenedSecs self.msgQueue = msgQueue self.ircThread = ircThread unlockerClassName = config.unlocker unlockerClass = getattr(unlocker, unlockerClassName) self.unlocker = unlockerClass(config) def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc) self.desfireAuthenticator = DesfireEd25519Authenthicator( config.authDbFilename, self.nfc, config.desfirePubkey.decode("hex")) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry e = threading.Event() e.wait(timeout=0.3) except NFCError, e: #this exception happens also when scanUID finds no cards logging.debug( "Failed to find RFID cards in reader's field: %s", e) e = threading.Event() e.wait(timeout=0.2) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2) except Exception: logging.exception("Exception in main unlock thread")
print "Now trying to send ISO14443-4 APDUs" try: #nfc.selectPassiveTarget() for apdu in apdus: print "Command APDU:", formatAPDU(apdu) rapdu = nfc.sendAPDU(apdu) print "Response APDU valid: %s, SW %04x, data %s" % ( rapdu.valid(), rapdu.sw(), hexlify(rapdu.data())) except NFCError, e: print "Failed to transmit APDU:", e.what() print "Device is opened:", nfc.opened() print "Closing device" nfc.close() print "Device is opened:", nfc.opened() nfc.unload() except NFCError, e: print "Reading UID failed:", e.what() elif apdu_test == "desfire-ndef4": try: nfc = NFCDevice() uid = nfc.scanUID() print "UID", hexlify(uid) #nfc.close() #nfc.open() ndef = nfc.readDesfireNDEF() print ndef print "Device is opened:", nfc.opened() print "Closing device" nfc.close()
print "Selected test: %s" % apdu_test # select apdus according to test name hex_apdus = tests[apdu_test] apdus = [hex_apdu.replace(" ","").decode("hex") for hex_apdu in hex_apdus] try: nfc = NFCDevice() uid = nfc.scanUID() print "UID", hexlify(uid) #nfc.close() #nfc.open() print "Now trying to send ISO14443-4 APDUs" try: #nfc.selectPassiveTarget() for apdu in apdus: print "Command APDU:", formatAPDU(apdu) rapdu = nfc.sendAPDU(apdu) print "Response APDU valid: %s, SW %04x, data %s" % (rapdu.valid(), rapdu.sw(), hexlify(rapdu.data())) except NFCError, e: print "Failed to transmit APDU:", e.what() print "Device is opened:", nfc.opened() print "Closing device" nfc.close() print "Device is opened:", nfc.opened() nfc.unload() except NFCError, e: print "Reading UID failed:", e.what()