def get_tag(): context = nfc.init() pnd = nfc.open(context) if pnd is None: print('ERROR: Unable to open NFC device.') exit() if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initiator_init") print('ERROR: Unable to init NFC device.') exit() # Declare modulations nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 nt = nfc.target() # Wait for tag ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) # Convert raw uid to hex, then trim to length (2*nt.nti.nai.szUidLen) tag = ''.join(format(x, '02x') for x in nt.nti.nai.abtUid)[:2*nt.nti.nai.szUidLen] nfc.close(pnd) nfc.exit(context) # Return UID return tag
def nfcInitListen(): """ Open nfc device and set opened NFC device to initiator mode: """ global pnd, context pnd = nfc.open(context) if pnd is None: print('ERROR: Unable to open NFC device.') sys.exit(1) if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initiator_init") print('ERROR: Unable to init NFC device.') sys.exit(1) print('NFC reader: %s opened' % nfc.device_get_name(pnd))
def init_reader(callback): context = nfc.init() devs = nfc.list_devices(context, 16) if devs: for dev in devs: pnd = nfc.open(context, dev) if pnd: if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initator_init") nfc.close(pnd) else: reader = NFCReader(context, pnd, callback) reader.daemon = True reader.start() else: print("failed to find a NFC device")
def open_device(self): # return if device is already initialized if self.device: return # make sure everything is shut down before initalizing self.shutdown() self.context = nfc.init() print("{} uses libnfc {}".format(sys.argv[0], nfc.__version__)) connstrings = nfc.list_devices(self.context, max_device_count) szDeviceFound = len(connstrings) if szDeviceFound == 0: raise Exception("No NFC reader found!") for i in range(szDeviceFound): self.device = nfc.open(self.context, connstrings[i]) if self.device is None: continue if(nfc.initiator_init(self.device)<0): nfc.perror(self.device, "nfc_initiator_init") raise Exception("Init of device failed!") print("NFC reader:", nfc.device_get_name(self.device), "opened") break
def try_read(): if nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt) > -1: return read_sector(7) @atexit.register def close(): nfc.close(pnd) nfc.exit(context) accessBits = compute_access_bits(c1, c2, c3) context = nfc.init() pnd = nfc.open(context) if pnd is None: raise Exception('ERROR: Unable to open NFC device.') if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initiator_init") raise Exception('ERROR: Unable to init NFC device.') print('NFC reader: %s opened' % nfc.device_get_name(pnd)) nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 nt = nfc.target()
# Ensure ^C doesn't corrupt/leave anything broken. signal.signal(signal.SIGINT, signal_handler) conn = sqlite3.connect('/var/db/assurpid/assurpid.db') output('opened database successfully') output('libnfc version: ' + nfc.__version__) context = nfc.init() pnd = nfc.open(context) if pnd is None: output('ERROR: Unable to detect RFID sensor (run this as root).') exit() if nfc.initiator_init(pnd) < 0: nfc.perror(pnd, "nfc_initiator_init") output('ERROR: Unable to init RFID sensor.') exit() output('NFC reader: ' + nfc.device_get_name(pnd) + 'opened') nmMifare = nfc.modulation() nmMifare.nmt = nfc.NMT_ISO14443A nmMifare.nbr = nfc.NBR_106 nt = nfc.target() admin_switch = False while True: if not admin_switch:
print("Unable to open NFC device.") nfc.exit(context) exit() print("NFC device: %s opened" % nfc.device_get_name(pnd)) #signal(SIGINT, stop_dep_communication); print("NFC device will now act as: ") #print_nfc_target(&nt, false) print("Waiting for initiator request...") abtRx = nfc.target_init(pnd, nt, MAX_FRAME_LEN, 0) szRx = len(abtRx) if szRx < 0: nfc.perror(pnd, "nfc_target_init") nfc.close(pnd) nfc.exit(context) exit() print("Initiator request received. Waiting for data...\n") abtRx = nfc.target_receive_bytes(pnd, MAX_FRAME_LEN, 0) szRx = len(abtRx) if szRx < 0: nfc.perror(pnd, "nfc_target_receive_bytes") nfc.close(pnd) nfc.exit(context) exit() #abtRx[szRx] = '\0'
print('Received bits: ', end='') nfc.print_hex(pbtRx, szRx) # Succesful transfer return True, pbtRx context = nfc.init() pnd = nfc.open(context) if pnd is None: print('ERROR: Unable to open NFC device.') nfc.exit(context) exit() if (nfc.initiator_init(pnd) < 0): nfc.perror(pnd, "nfc_initiator_init") nfc.close(pnd) nfc.exit(context) exit() if (nfc.device_set_property_bool(pnd, nfc.NP_HANDLE_CRC, False) < 0): nfc.perror(pnd, "nfc_device_set_property_bool") nfc.close(pnd) nfc.exit(context) exit() if (nfc.device_set_property_bool(pnd, nfc.NP_EASY_FRAMING, False) < 0): nfc.perror(pnd, "nfc_device_set_property_bool") nfc.close(pnd) nfc.exit(context) exit()
nfc.print_hex(pbtRx, szRx) # Succesful transfer return True, pbtRx context = nfc.init() pnd = nfc.open(context) if pnd is None: print('ERROR: Unable to open NFC device.') nfc.exit(context) exit() if(nfc.initiator_init(pnd)<0): nfc.perror(pnd, "nfc_initiator_init") nfc.close(pnd) nfc.exit(context) exit() if (nfc.device_set_property_bool(pnd, nfc.NP_HANDLE_CRC, False) < 0): nfc.perror(pnd, "nfc_device_set_property_bool") nfc.close(pnd) nfc.exit(context) exit() if (nfc.device_set_property_bool(pnd, nfc.NP_EASY_FRAMING, False) < 0): nfc.perror(pnd, "nfc_device_set_property_bool") nfc.close(pnd) nfc.exit(context) exit()
print("Unable to open NFC device.") nfc.exit(context) exit() print("NFC device: %s opened" % nfc.device_get_name(pnd)) #signal(SIGINT, stop_dep_communication); print("NFC device will now act as: ") #print_nfc_target(&nt, false) print("Waiting for initiator request...") abtRx = nfc.target_init(pnd, nt, MAX_FRAME_LEN, 0) szRx = len(abtRx) if szRx < 0: nfc.perror(pnd, "nfc_target_init") nfc.close(pnd) nfc.exit(context) exit() print("Initiator request received. Waiting for data...\n") abtRx = nfc.target_receive_bytes(pnd, MAX_FRAME_LEN, 0) szRx = len(abtRx) if szRx < 0: nfc.perror(pnd, "nfc_target_receive_bytes") nfc.close(pnd) nfc.exit(context) exit() #abtRx[szRx] = '\0' print("Received: %s\n", abtRx)