def main(): nfc = nfc_comm.NFC() readers = nfc.get_readers() print('Found {} readers.'.format(len(readers))) if len(readers) == 0: return 2 reader_status = nfc_comm.ReaderStatus(nfc.get_status_change()[0]) if not reader_status.is_card_present(): print('No card present.') return 2 print('Found card.') cards = nfc_comm.get_cards(nfc) if len(cards) == 0: print('Card is not NTAG203.') return 2 print('Card is NTAG203.') card = cards[0] with nfc_comm.Connection(nfc) as conn: test_data = list(range(0, card.DATA_LEN)) print('Writing sequential bytes...') card.write(conn, test_data) print('Reading bytes...') assert card.read(conn) == test_data print('Test passed.')
def main(): logging.basicConfig(level=logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('--version', default=2, nargs='?', type=int) options = parser.parse_args() nfc = nfc_comm.NFC() readers = nfc.get_readers() if len(readers) == 0: print('Cannot find NFC reader.') return 2 reader_status = nfc_comm.ReaderStatus(nfc.get_status_change()[0]) if not reader_status.is_card_present(): print('No card present.') return 2 print('Found card.') cards = nfc_comm.get_cards(nfc) if len(cards) == 0: print('Card is not NTAG203.') return 2 print('Card is NTAG203.') card = cards[0] with nfc_comm.Connection(nfc) as conn: if options.version == 1: defaults = packet.v1_defaults() else: defaults = packet.v2_defaults(1) pprint(defaults) card.write(conn, packet.encode(defaults))
def rpc_write_tag(fields): LOG.info("rpc_write_tag: %s", fields) data_bytes = packet.encode(fields) LOG.debug("write bytes %s", nfc_comm.fmt_bytes(data_bytes)) with nfc_comm.NFC() as nfc, nfc_comm.Connection(nfc) as conn: cards = nfc_comm.get_cards(nfc) card = cards[0] card.write(conn, data_bytes)
def read_status(): '''Readers and tags come and go. Always return something reasonable.''' with nfc_comm.NFC() as nfc: readers = nfc.get_readers() if len(readers) == 0: return NO_NFC_READER convert_v1_cards_to_v2(nfc) cards = nfc_comm.get_cards(nfc) return {'readers': readers, 'cards': [card.dic for card in cards]}
def main(): nfc = nfc_comm.NFC() readers = nfc.get_readers() print('Found {} readers.'.format(len(readers))) if len(readers) == 0: return 0 print_sep() reader_status = nfc_comm.ReaderStatus(nfc.get_status_change()[0]) if not reader_status.is_card_present(): print('No card present.') return 0 print('Connecting to card...') conn = nfc_comm.Connection(nfc, readers[0]) print('Reading UID...') uid = nfc_comm.check_err(conn.transmit(apdu.get_uid())) print('UID: {}'.format(' '.join([hex2(byte) for byte in uid]))) print_sep() print('Sending NXP GET_VERSION using PASS_THROUGH command...') version_blk = conn.transmit(apdu.get_version()) version_str = ' '.join([hex2(byte) for byte in version_blk]) print('version: {}'.format(version_str)) print_sep() print('Reading blocks until an error is raised...') for block_num in range(0, 64): try: blk = nfc_comm.check_err(conn.transmit(apdu.read_block(block_num))) # pylint: disable=bare-except except: print('Reached end of readable blocks.') break else: blk_str = ' '.join([hex2(byte) for byte in blk]) print('block_num {0:02x}: {1}'.format(block_num, blk_str)) pprint(nfc_comm.NTAG203(conn).dic)
def read_card_with_serial(self, serial): self.nfc = nfc_comm.NFC() readers = self.nfc.get_readers() for reader in readers: if 'SCM Microsystems' in reader: try: with nfc_comm.Connection(self.nfc, reader) as conn: currentSerial = get_attrib( conn.hcard, sc.SCARD_ATTR_VENDOR_IFD_SERIAL_NO) if currentSerial == serial: tag = nfc_comm.maybe_tag(conn) if tag is None: return ('Cartridge not detected.') else: return (str(tag.dic)) except scw.NFCException as nfce: if nfce.err_code == -2146434967: continue elif nfce.err_code == -2146435056: return ('Please re-insert the cartridge') return ('The specified reader was not detected.')