Exemple #1
0
    def select_card(self):
        """Selects a card after a failed authentication attempt (aborted communications)

           Returns the UID of the card selected
        """
        nt = nfc.nfc_target()
        _ = nfc.nfc_initiator_select_passive_target(self.__device, self.__modulations[0], None, 0, ctypes.byref(nt))
        uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen)])
        return uid
Exemple #2
0
    def select_card(self):
        """Selects a card after a failed authentication attempt (aborted communications)

           Returns the UID of the card selected
        """
        nt = nfc.nfc_target()
        _ = nfc.nfc_initiator_select_passive_target(self.__device, self.__modulations[0], None, 0, ctypes.byref(nt))
        uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen)])
        return uid
Exemple #3
0
    def start_nfc_reader(self):
        connection_loop = True
        try:
            self.log.debug('NFC init')
            nfc.nfc_init(ctypes.byref(self.__context))
            # Select HW reader
            devices_found = nfc.nfc_list_devices(self.__context, ctypes.byref(self.__conn_strings), 1)
            if devices_found != 1:
                raise HWError('No suitable NFC device found. Check your libnfc config and hardware.')

            # NFC abstraction: open
            self.__device = nfc.nfc_open(self.__context, self.__conn_strings[0])
            self.log.debug('NFC_open finished')

            # Start reader as initiator with RF field, but without infinite polling to reduce CPU usage
            result = nfc.nfc_initiator_init(self.__device)
            if result < 0:
                raise HWError('Could not start or configure reader as NFC initiator properly.')
            self.log.debug('Started NFC initiator.')

            while connection_loop:
                self.log.info('Polling for NFC target...')
                result += nfc.nfc_device_set_property_bool(self.__device, nfc.NP_INFINITE_SELECT, False)
                found_card = nfc.nfc_initiator_select_passive_target(self.__device,
                                                                     self.__modulation,
                                                                     None,
                                                                     0,
                                                                     ctypes.byref(self.__target))
                # Wait until target was found
                if found_card > 0:
                    # Got target
                    self.log.debug('Connection established with ISO14443A modulation and baudrate type %d.',
                                   self.__target.nm.nbr)
                    # Send UID to authenticate reader against Android device
                    # -> it will wait until Android's NFC service is ready
                    self.send_uid()
                    # Finish setup step
                    self.running = True
                    connection_loop = False

                else:
                    # No target found
                    self.log.debug('No target found. Sleeping for 1 s.')
                    time.sleep(1.2)

        except (KeyboardInterrupt, SystemExit):
            connection_loop = False
        except HWError as e:
            self.log.error("Hardware exception: " + str(e))
            connection_loop = False
        except IOError as e:
            self.log.error("IOError exception: " + str(e))
            connection_loop = True

        return connection_loop