예제 #1
0
def nfc_open(connstring=_connstring):
    """Open the PN532 for reading.

    Use this like so:
    with reader.nfc_open() as device:
        do_stuff_with_device(device)
    
    Automatically takes care of closing the connection for you.

    Fun fact: you can (and should) do this with regular file open() as well. 
    """
    ctx = nfc.init()
    if ctx is None:
        raise RuntimeError('Couldn\'t init libnfc')

    device = nfc.open(ctx, connstring)
    if device is None:
        raise NFCError('Couldn\'t open NFC device', {'connstring': connstring})

    try:
        if nfc.initiator_init(device) < 0:
            raise NFCError('Couldn\'t init NFC device',
                           {'connstring': connstring})

        yield device
    finally:
        # cleanup...
        nfc.close(device)
        nfc.exit(ctx)
예제 #2
0
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 
예제 #3
0
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))
예제 #4
0
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")
예제 #5
0
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")
예제 #6
0
    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
예제 #7
0
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()
예제 #8
0
파일: main.py 프로젝트: AssuRFID/assurpid
def signal_handler(signal, frame):
    clean_up()
    sys.exit(0)

sleep_time = 2

# 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
예제 #9
0
        exit()

abtTx = b'Hello Mars!'

context = nfc.init()
if context is None:
    print("Unable to init libnfc")
    exit()

MAX_DEVICE_COUNT = 2

connstrings = nfc.list_devices(context, MAX_DEVICE_COUNT)
szDeviceFound = len(connstrings)

if szDeviceFound == 1:
    pnd = nfc.open(context, connstrings[0])
elif szDeviceFound > 1:
    pnd = nfc.open(context, connstrings[1])
else:
    print("No device found.")
    nfc.exit(context)
    exit()

nt = nfc.target()
nt.nm.nmt = nfc.NMT_DEP
nt.nm.nbr = nfc.NBR_UNDEFINED
nt.nti.ndi.abtNFCID3 = bytearray([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00])
nt.nti.ndi.szGB = 4
nt.nti.ndi.abtGB = bytearray([0x12, 0x34, 0x56, 0x78])
nt.nti.ndi.ndm = nfc.NDM_UNDEFINED
nt.nti.ndi.btDID = 0x00
예제 #10
0

context = nfc.init()

# Display libnfc version
print("%s uses libnfc %s" %( sys.argv[0], nfc.__version__))

# TODO
connstrings = nfc.list_devices(context, max_device_count)
szDeviceFound = len(connstrings)

if szDeviceFound == 0:
    print("No NFC device found.")

for i in range(szDeviceFound):
    pnd = nfc.open(context, connstrings[i]);
    if pnd is None:
        continue

    if(nfc.initiator_init(pnd)<0):
        nfc.perror(pnd, "nfc_initiator_init")
        nfc.close(pnd)
        nfc.exit(context)
        exit()

    print("NFC reader:", nfc.device_get_name(pnd), "opened")   

    nm = nfc.modulation()
    if mask & 0x1:
        nm.nmt = nfc.NMT_ISO14443A
        nm.nbr = nfc.NBR_106
예제 #11
0
import requests

is_linux = (system() == "Linux")
yes_confirm_options = ["yes", "y"]

if is_linux:
    """
    Determine if the device this is running on is a linux device and if so, attempt to import necessary nfc modules
    and prepare for later use of the reader.
    """
    import nfc

    context = nfc.init()
    reader = nfc.list_devices(context, 2)[0]

    listener = nfc.open(context, reader)
    modulation = nfc.modulation()

    modulation.nmt = nfc.NMT_ISO14443A
    modulation.nbr = nfc.NBR_106


def parse_nfc_uid(device):
    """
    Parses from the string returned from nfc.str_nfc_target and just returns the uid.
    :param device: An object of the card class from nfc's implementation. Get this from initiator_list_passive_targets
    :return: The uid of the given object.
    """
    num, string = nfc.str_nfc_target(device, False)
    str_list = string.split("\n")
    uid = str_list[2].strip().replace("  ", " ")
예제 #12
0

abtTx = b'Hello Mars!'

context = nfc.init()
if context is None:
    print("Unable to init libnfc")
    exit()

MAX_DEVICE_COUNT = 2

connstrings = nfc.list_devices(context, MAX_DEVICE_COUNT)
szDeviceFound = len(connstrings)

if szDeviceFound == 1:
    pnd = nfc.open(context, connstrings[0])
elif szDeviceFound > 1:
    pnd = nfc.open(context, connstrings[1])
else:
    print("No device found.")
    nfc.exit(context)
    exit()

nt = nfc.target()
nt.nm.nmt = nfc.NMT_DEP
nt.nm.nbr = nfc.NBR_UNDEFINED
nt.nti.ndi.abtNFCID3 = bytearray(
    [0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00])
nt.nti.ndi.szGB = 4
nt.nti.ndi.abtGB = bytearray([0x12, 0x34, 0x56, 0x78])
nt.nti.ndi.ndm = nfc.NDM_UNDEFINED