def run(self): """Starts the looping thread""" self.__context = ctypes.pointer(nfc.nfc_context()) nfc.nfc_init(ctypes.byref(self.__context)) loop = True try: self._clean_card() conn_strings = (nfc.nfc_connstring * 10)() devices_found = nfc.nfc_list_devices(self.__context, conn_strings, 10) if devices_found >= 1: self.__device = nfc.nfc_open(self.__context, conn_strings[0]) try: _ = nfc.nfc_initiator_init(self.__device) while True: self._poll_loop() finally: nfc.nfc_close(self.__device) else: self.log("NFC Waiting for device.") time.sleep(5) except (KeyboardInterrupt, SystemExit): loop = False except IOError as e: self.log("Exception: " + str(e)) loop = True # not str(e).startswith("NFC Error whilst polling") # except Exception, e: # loop = True # print "[!]", str(e) finally: nfc.nfc_exit(self.__context) self.log("NFC Clean shutdown called") return loop
def shutdown_nfc_reader(self, delay_time=0): if self.running: self.running = False try: nfc.nfc_close(self.__device) nfc.nfc_exit(self.__context) finally: self.log.info('NFC clean shutdown. Delay some time until restart.') time.sleep(delay_time)
def run(self): """ Connects with the NFC reader and starts messaging with NFC devices """ self.__context = ctypes.pointer(nfc.nfc_context()) self.log.debug('NFC init') nfc.nfc_init(ctypes.byref(self.__context)) conn_strings = (nfc.nfc_connstring * 1)() connection_loop = True while connection_loop: try: devices_found = nfc.nfc_list_devices(self.__context, ctypes.byref(conn_strings), 1) if devices_found != 1: raise HWError('No NFC device found. Check your libnfc config and hardware.') self.log.debug('NFC open') # NFC abstraction: open self.__device = nfc.nfc_open(self.__context, conn_strings[0]) self.log.debug('NFC_open finished') # self.log.debug(self.__device.last_error) while connection_loop: # Reset all state variables self.log.info('Wait for NFC initiator for 5s') # NFC abstraction: target_init connection_res = nfc.nfc_target_init(self.__device, ctypes.byref(self.__target), ctypes.pointer(self.__rx_msg), self.ISO7816_SHORT_APDU_MAX_LEN, 5000) if connection_res >= 0: self.log.debug(self.__rx_msg[:connection_res]) # Initialize state machine and start message exchange state machine stm = CardEmulation(self.q_data_in, self.q_data_out) self._message_loop(stm) elif connection_res != nfc.NFC_ETIMEOUT: self.log.warning('nfc_target_init: got error %i', connection_res) 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 finally: nfc.nfc_close(self.__device) nfc.nfc_exit(self.__context) self.log.info('NFC clean shutdown')
def run(self): """Starts the looping thread""" # import ipdb; ipdb.set_trace() # break 119 # break 240 # break 208 self.__context = ctypes.pointer(nfc.nfc_context()) nfc.nfc_init(ctypes.byref(self.__context)) loop = True try: self._clean_card() conn_strings = (nfc.nfc_connstring * 10)() devices_found = nfc.nfc_list_devices( self.__context, conn_strings, 10) if devices_found >= 1: self.__device = nfc.nfc_open(self.__context, conn_strings[0]) try: _ = nfc.nfc_initiator_init(self.__device) while True: self._poll_loop() finally: nfc.nfc_close(self.__device) else: self.log("NFC Waiting for device.") time.sleep(5) except (KeyboardInterrupt, SystemExit): loop = False except IOError as e: self.log("Exception: " + str(e)) loop = True # not str(e).startswith("NFC Error whilst polling") # except Exception, e: # loop = True # print "[!]", str(e) finally: nfc.nfc_exit(self.__context) self.log("NFC Clean shutdown called") return loop
def __del__(self): nfc.nfc_close(self.__device) nfc.nfc_exit(self.__context)
self._poll_loop() finally: nfc.nfc_close(self.__device) else: self.log("NFC Waiting for device.") time.sleep(5) except (KeyboardInterrupt, SystemExit): loop = False except IOError, e: self.log("Exception: " + str(e)) loop = True # not str(e).startswith("NFC Error whilst polling") # except Exception, e: # loop = True # print "[!]", str(e) finally: nfc.nfc_exit(self.__context) self.log("NFC Clean shutdown called") return loop @staticmethod def _sanitize(bytesin): """Returns guaranteed ascii text from the input bytes""" return "".join([x if 0x7f > ord(x) > 0x1f else '.' for x in bytesin]) @staticmethod def _hashsanitize(bytesin): """Returns guaranteed hexadecimal digits from the input bytes""" return "".join( [x if x.lower() in 'abcdef0123456789' else '' for x in bytesin]) def _poll_loop(self):
self._poll_loop() finally: nfc.nfc_close(self.__device) else: self.log("NFC Waiting for device.") time.sleep(5) except (KeyboardInterrupt, SystemExit): loop = False except IOError, e: self.log("Exception: " + str(e)) loop = True # not str(e).startswith("NFC Error whilst polling") # except Exception, e: # loop = True # print "[!]", str(e) finally: nfc.nfc_exit(self.__context) self.log("NFC Clean shutdown called") return loop @staticmethod def _sanitize(bytesin): """Returns guaranteed ascii text from the input bytes""" return "".join([x if 0x7f > ord(x) > 0x1f else '.' for x in bytesin]) @staticmethod def _hashsanitize(bytesin): """Returns guaranteed hexadecimal digits from the input bytes""" return "".join([x if x.lower() in 'abcdef0123456789' else '' for x in bytesin]) def _poll_loop(self): """Starts a loop that constantly polls for cards"""
def __del__(self): if self.dev: nfc.nfc_close(self.dev) nfc.nfc_exit(self.context)