Example #1
0
 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, e:
         self.log("Exception: " + str(e))
         loop = True  # not str(e).startswith("NFC Error whilst polling")
Example #2
0
 def run(self):
     """Starts the looping thread"""
     self.__context = ctypes.pointer(nfc.nfc_context())
     nfc.nfc_init(ctypes.byref(self.__context))
     loop = True
     try:
         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)
                 self.uid = ''
                 print 'start poll'
                 while True:
                     self._poll()
             finally:
                 nfc.nfc_close(self.__device)
         else:
             print "NFC Waiting for device."
             time.sleep(5)
     except (KeyboardInterrupt, SystemExit):
         loop = False
     except IOError, e:
         print "Exception: " + str(e)
         loop = True
Example #3
0
 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, e:
         self.log("Exception: " + str(e))
         loop = True  # not str(e).startswith("NFC Error whilst polling")
Example #4
0
 def run(self):
     """Starts the looping thread"""
     self.__context = ctypes.pointer(nfc.nfc_context())
     nfc.nfc_init(ctypes.byref(self.__context))
     loop = True
     try:
         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)
                 self.uid = ''
                 print 'start poll'
                 while True:
                     self._poll()
             finally:
                 nfc.nfc_close(self.__device)
         else:
             print "NFC Waiting for device."
             time.sleep(5)
     except (KeyboardInterrupt, SystemExit):
         loop = False
     except IOError, e:
         print "Exception: " + str(e)
         loop = True
Example #5
0
 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)
Example #6
0
    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')
Example #7
0
    def run2(self, no_card_for_seconds=0):
        """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)

                    # If we need to wait until the reader clears, then do this loop
                    if no_card_for_seconds > 0:
                        done_time = datetime.now() + timedelta(seconds=no_card_for_seconds)
                        while (datetime.now()<done_time):
                            self._poll_loop()
                            if self._card_uid is not None:  # Keep resetting while there's a card on the reader
                                done_time = datetime.now() + timedelta(seconds=no_card_for_seconds)
                        print "The NFC reader is now clear."
                    else: # Assert: no_card_for_seconds <= 0, treat negative values as 0
                        # Get a valid RFID
                        while self._card_uid is None:
                            self._poll_loop()

                except (KeyboardInterrupt, SystemExit):
                    loop = False
                    self._clean_card()

                finally:
                    nfc.nfc_close(self.__device)
            else:
                self.log("NFC Waiting for device.")
                time.sleep(self.card_timeout)
        except (KeyboardInterrupt, SystemExit):
            loop = False
            self._clean_card()
        except IOError, e:
            self.log("Exception: " + str(e))
            loop = True  # not str(e).startswith("NFC Error whilst polling")
Example #8
0
    def run(self, wait_for_clear = False, delay_for_clear = 0):
        """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)
                    # Wait until the reader has no card nearby
                    if wait_for_clear:
                        self._poll_loop()
                        start_time = datetime.now()
                        while (datetime.now() - start_time).seconds <= delay_for_clear:
                            self._poll_loop()
                            if self._card_uid is not None:  # Keep resetting while there's a card on the reader
                                start_time = datetime.now()  # Reset the timer
                        print "The NFC reader is now clear."
                        self.log("NFC has cleared for {} seconds.".format(delay_for_clear))
                    while not self._card_uid:  # Wait while there is no card
                        self._poll_loop()
                except (KeyboardInterrupt, SystemExit):
                    loop = False
                    self._clean_card()

                finally:
                    nfc.nfc_close(self.__device)
            else:
                self.log("NFC Waiting for device.")
                time.sleep(self.card_timeout)
        except (KeyboardInterrupt, SystemExit):
            loop = False
            self._clean_card()
        except IOError, e:
            self.log("Exception: " + str(e))
            loop = True  # not str(e).startswith("NFC Error whilst polling")
Example #9
0
 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
Example #10
0
 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
Example #11
0
 def __del__(self):
     nfc.nfc_close(self.__device)
     nfc.nfc_exit(self.__context)
Example #12
0
    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: #IMPORTANT: LOOPS ITSELF INSTANTLY AND FOREVER

                        if stage == 0:
                            self._poll_loop() #IMPORTANT: LOOPS ITSELF EVERY 1 SEC
                            

                        elif stage == 1:
                            print "Proceeding to Stage One. Waiting for Flashlight..."
                            #edw mpainei synexeia kai tha looparei mexri to stage na allaxei xana.

                            while stage==1:
                                print "a ", rc_time(7) #ekthema (flashlight)
                                #print "b ", rc_time(29) #letter
                                if (rc_time(7) < 70):
                                        #LED_EXTENDER: turn on first led to indicatre position for letter P.
                                        wiringpi.digitalWrite(100,1)

                                        print "LIT COMPLETE", "You got a P! Led is on for the first letter! Put it there!"
                                        if isLetterAtPin(29,100):
                                                print "You placed the first letter, P!"
                                                print "P _ _ _ _ _ _ _ _ _"
                                                print " "
                                                print "Stage 2 entered", " Waiting for the NFC..."
                                              
                                                os.system('killall omxplayer.bin')
                                                omxc=Popen(['omxplayer', '-o','hdmi', ap1])
                                                player=True
                                                time.sleep(17)
                                                os.system('killall omxplayer.bin')
                                                omxc=Popen(['omxplayer', '-o','hdmi', journalist_vid])
                                                player=True
                                                time.sleep(12)
                                                os.system('killall omxplayer.bin')
                                                omxc=Popen(['omxplayer', '-o','hdmi', er2])
                                                player=True
                                                

                            #GPIO.cleanup() #TO XALAEI OPOTE DE TO VAZOUME

                        elif stage == 2:
                            self._poll_loop()

                        elif stage == 3:
                            self._poll_loop()

                        elif stage == 4:
                            print "4"
                            
                            GPIO.setup(11,GPIO.IN,pull_up_down=GPIO.PUD_UP)
                           
                            
                            while True:
                                print "ok"
                              
                                input_state = GPIO.input(11)
                                print input_state
                                time.sleep(0.5)
                                if input_state == False :
                            
                                    print "pressed"
                                    #turn on LEDs for I's
                                    #LED_EXTENDER: turn on first led to indicatre position for letter P.
                                    wiringpi.digitalWrite(103,1)
                                    wiringpi.digitalWrite(105,1)
                                    
                                    time.sleep(0.5)

                                    if isLetterAtPin(35, 103) and isLetterAtPin(22,105):
                                        global stage
                                        print "You found two I's! Congratulations!"
                                        print "P O L I _ I _ _ O _"
                                        os.system('killall omxplayer.bin')
                                        omxc=Popen(['omxplayer', '-o','hdmi', ap4])
                                        player=True
                                        time.sleep(17)
                                        os.system('killall omxplayer.bin')
                                        omxc=Popen(['omxplayer', '-o','hdmi', italian_vid])
                                        player=True
                                        time.sleep(17)
                                        os.system('killall omxplayer.bin')
                                        omxc=Popen(['omxplayer', '-o','hdmi', er5])
                                        player=True
                                        
                                        print " "
                                        print "Stage 5 entered", "Waiting for the NFC..."
                                        stage-=1
                                        
                                        break                   

                        elif stage == 5:
                            self._poll_loop()

                        elif stage == 6:
                            self._poll_loop()

                        elif stage == 7:
                            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")
Example #13
0
 def __del__(self):
     if self.dev:
         nfc.nfc_close(self.dev)
     nfc.nfc_exit(self.context)