def init():
    global localDB
    global remoteDB
    global nodeType
    global lastAutoUnlockTime
    lastAutoUnlockTime = time.time()
    localDB = database.MemberDatabase(config.localDBHost,config.localDBUser,config.localDBPass,config.localDBDatabase)
    remoteDB = database.MemberDatabase(config.remoteDBHost,config.remoteDBUser,config.remoteDBPass,config.remoteDBDatabase,config.remoteDBPort,config.remoteDBSSL)
    nodeType = localDB.get_node_type()
    init_gpio()
    clearFileCmds()
    subprocess.call(["/usr/local/bin/beep"])
    nfc.init()
Beispiel #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)
Beispiel #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 
Beispiel #4
0
def init():
    global localDB
    global remoteDB
    global nodeType
    global lastAutoUnlockTime
    lastAutoUnlockTime = time.time()
    localDB = database.MemberDatabase(config.localDBHost, config.localDBUser,
                                      config.localDBPass,
                                      config.localDBDatabase)
    remoteDB = database.MemberDatabase(config.remoteDBHost,
                                       config.remoteDBUser,
                                       config.remoteDBPass,
                                       config.remoteDBDatabase,
                                       config.remoteDBPort, config.remoteDBSSL)
    nodeType = localDB.get_node_type()
    init_gpio()
    clearFileCmds()
    subprocess.call(["/usr/local/bin/beep"])
    nfc.init()
Beispiel #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")
Beispiel #6
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")
Beispiel #7
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
Beispiel #8
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
Beispiel #9
0
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