Esempio n. 1
0
 def shutdown(self):
     if self.device:
         nfc.close(self.device)
         self.device = None
     if self.context:
         nfc.exit(self.context)
         self.context = None
Esempio n. 2
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)
Esempio n. 3
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 
Esempio n. 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")
Esempio n. 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")
Esempio n. 6
0
def close():
    nfc.close(pnd)
    nfc.exit(context)
Esempio n. 7
0
        if (nfc.writeHash(new_hash[0])):
            # Update database
            remoteDB.update_hash(card_id, new_hash[1])
        else:
            # Set fault bit on card
            remoteDB.update_hash(card_id, card[1], 1)
            ledUpdate("bad hash")
            return False

    # Check for access
    access_status = localDB.check_valid_access(card_id, remoteDB)
    if (access_status != True):
        ledUpdate(access_status)
        return False
    else:
        # Log Success
        #db_log(cardData[0],cardData[1],"Access Granted")
        #log("Access Granted: Card ID=%s User ID=%s" % (card_id,cardData[1]))
        # Open Door
        open_door(unlocked)
        return True


# Main
init()
while (1):
    if poll() is None:
        time.sleep(1)
print "Exited while(1)? We must be done?"
nfc.close()
    return


if __name__ == '__main__':
    # Reset the default Crtl-C behavior
    import signal
    try:
        signal.signal(signal.SIGINT, signal.SIG_DFL)
    except ValueError:
        pass

    print('Version: ', nfc.__version__)

    while (1):
        context = nfc.init()

        if not context:
            print("Unable to init libnfc (malloc)\n");
            sys.exit(1)

        nfcInitListen()
        nfcProtocol()

        nfc.close(pnd)
        nfc.exit(context)
        time.sleep(2)
        print("\nstarting again...\n")

    sys.exit(0)
Esempio n. 9
0
def clean_up():
    nfc.close(pnd)
    nfc.exit(context)
    conn.close()
Esempio n. 10
0
    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)
Esempio n. 11
0
        new_hash = nfc.generateNewHash(card[1])
        if (nfc.writeHash(new_hash[0])):
            # Update database
            remoteDB.update_hash(card_id,new_hash[1])
        else:
            # Set fault bit on card
            remoteDB.update_hash(card_id,card[1],1)
            ledUpdate("bad hash")
            return False
        
    # Check for access
    access_status = localDB.check_valid_access(card_id, remoteDB)
    if (access_status != True):
        ledUpdate(access_status)
        return False
    else:
        # Log Success
        #db_log(cardData[0],cardData[1],"Access Granted")
        #log("Access Granted: Card ID=%s User ID=%s" % (card_id,cardData[1]))
        # Open Door
        open_door(unlocked)
        return True

# Main
init()
while(1):
    if poll() is None:
        time.sleep(1)
print "Exited while(1)? We must be done?"
nfc.close()
Esempio n. 12
0
def close():
  nfc.close(pnd)
  nfc.exit(context)