示例#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 
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
# 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:
示例#9
0
# 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
        # List ISO14443A targets
        res, ant = nfc.initiator_list_passive_targets(pnd, nm, max_target_count)
        if (res >= 0):
            if (verbose or (res > 0)):