<commit_before>#! /usr/bin/env python

from smartcard.System import readers
from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
from smartcard.util import toBytes, toHexString
import logging

# Initialize logging.
logging.basicConfig(filename='nfc.log', level=logging.INFO)

# Define the APDUs used in this script
CMD_SERIAL_ID = toBytes("80 14 00 00 08")
CMD_CARD_ID = toBytes("80 14 04 00 06")
CMD_NFC_SCAN = toBytes("ff 00 00 00 06 D4 60 01 01 00 20")
GET_RESPONSE = toBytes("FF C0 00 00 FF")

debug = False

# get all the available readers
readers = readers()
print "Available readers:", readers

reader = readers[0]
print "Using:", reader

connection = reader.createConnection()
connection.connect()

if debug:
    observer = ConsoleCardConnectionObserver()
    connection.addObserver(observer)


# Get the reader's serial id
data, sw1, sw2 = connection.transmit(CMD_SERIAL_ID)
serialId = toHexString(data)
print "Reader's serial id: {}".format(serialId)
logging.info("Reader serial id: %s", serialId)
logging.info("APDU Command result: %02X %02X", sw1, sw2)


# Get the reader's card id
data, sw1, sw2 = connection.transmit(CMD_CARD_ID)
cardId = toHexString(data)
print "Reader's card id: {}".format(cardId)
logging.info("Reader card id: %s", cardId)
logging.info("APDU Command result: %02X %02X", sw1, sw2)


old_tag = []
while True:
    data, sw1, sw2 = connection.transmit(CMD_NFC_SCAN)
    if debug:
        print data
        print "Command: %02X %02X" % (sw1, sw2)

    if sw2 == 5:
        old_tag = []
        continue

    GET_RESPONSE[4] = sw2
    tag, sw1, sw2 = connection.transmit(GET_RESPONSE)
    if debug:
        print tag
        print "Response: %02X %02X" % (sw1, sw2)

    if tag != old_tag:
        old_tag = tag
        print toHexString(tag)<commit_msg>Added multi reader support<commit_after>#! /usr/bin/env python

from smartcard.System import readers
from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
from smartcard.util import toBytes, toHexString
import logging

# Initialize logging.
logging.basicConfig(filename='nfc.log', level=logging.INFO)

# Define the APDUs used in this script
CMD_SERIAL_ID = toBytes("80 14 00 00 08")
CMD_CARD_ID = toBytes("80 14 04 00 06")
CMD_NFC_SCAN = toBytes("ff 00 00 00 06 D4 60 01 01 00 20")
GET_RESPONSE = toBytes("FF C0 00 00 FF")

debug = False

# get all the available readers
readers = readers()
print "Available readers:", readers

connections = list()

for reader in readers:
    print "Initializing reader:", reader

    connection = reader.createConnection()
    connection.connect()
    connections.append(connection)

    if debug:
        observer = ConsoleCardConnectionObserver()
        connection.addObserver(observer)

for connection in connections:
    print connection
    # Get the reader's serial id
    data, sw1, sw2 = connection.transmit(CMD_SERIAL_ID)
    serialId = toHexString(data)
    print "Reader's serial id: {}".format(serialId)
    logging.info("Reader serial id: %s", serialId)
    logging.info("APDU Command result: %02X %02X", sw1, sw2)

    # Get the reader's card id
    data, sw1, sw2 = connection.transmit(CMD_CARD_ID)
    cardId = toHexString(data)
    print "Reader's card id: {}".format(cardId)
    logging.info("Reader card id: %s", cardId)
    logging.info("APDU Command result: %02X %02X", sw1, sw2)

print connections

old_tag = []
while True:
    for connection in connections:
        data, sw1, sw2 = connection.transmit(CMD_NFC_SCAN)
        if debug:
            print data
            print "Command: %02X %02X" % (sw1, sw2)

        if sw2 == 5:
            old_tag = []
            continue

        GET_RESPONSE[4] = sw2
        tag, sw1, sw2 = connection.transmit(GET_RESPONSE)
        if debug:
            print tag
            print "Response: %02X %02X" % (sw1, sw2)

        if tag != old_tag:
            old_tag = tag
            print toHexString(tag)
{'fraction_non_alphanumeric': 0.10421962379257753, 'fraction_numerical': 0.04092526690391459, 'mean_word_length': 3.4920091324200913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1471415', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1261, 'n_words': 392}