def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) if res < 0: self.globalId = "0" elif res >= 1: uid = None if nt.nti.nai.szUidLen == 4: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(4)]) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() self.globalId = uid.encode("hex") uid = None else: print "no card" uid = None self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: print "no card" self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: print("_poll_loop: res = {}. nt.nti.nai.szUidLen = {}".format( res, nt.nti.nai.szUidLen)) uid = None uidLen = 7 if nt.nti.nai.szUidLen == uidLen: # uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(uidLen)]) uid = bytearray([nt.nti.nai.abtUid[i] for i in range(uidLen)]) print("_poll_loop: uid = {}".format(binascii.hexlify(uid))) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: uid = None if nt.nti.nai.szUidLen == 4: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(4)]) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() # self.write_card(uid, "") self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: print("_poll_loop: res = {}. nt.nti.nai.szUidLen = {}".format(res, nt.nti.nai.szUidLen)) uid = None uidLen = 7 if nt.nti.nai.szUidLen == uidLen: # uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(uidLen)]) uid = bytearray([nt.nti.nai.abtUid[i] for i in range(uidLen)]) print("_poll_loop: uid = {}".format(binascii.hexlify(uid))) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: uid = None if nt.nti.nai.szUidLen >= 1: try: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen)]) except IndexError: raise IndexError("ERROR: index outside the range of nt.nti.nai.abtUid!") if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" GPIO.output(ledBlue, GPIO.HIGH) localtime = time.localtime() timeString = time.strftime("%Y-%m-%d %H:%M", localtime) lcd.clear() lcd.message(timeString + "\nWaiting for Card") nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) #print "RES", res if res < 0: GPIO.output(ledBlue, GPIO.LOW) # lcd.clear() # lcd.message("Resetting Reader") raise IOError("NFC Error whilst polling") elif res >= 1: uid = None if nt.nti.nai.szUidLen == 4: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(4)]) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() #res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, # ctypes.byref(nt)) res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), NFCReader.NUM_OF_POLLS, NFCReader.PERIOD_BW_POLLS, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: uid = None if nt.nti.nai.szUidLen >= 1: try: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen)]) except IndexError: raise IndexError("ERROR: index outside the range of nt.nti.nai.abtUid!") if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() # self.read_card(uid) # Remove the read -- don't need it, and it spews to the screen self._card_uid = uid.encode("hex") self.log("ID: ", uid.encode("hex")) self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: # ASSERT: res == 0 self._card_present = False self._clean_card() self.log("Done _poll_loop, found ID: ", self._card_uid )
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
def poll(self): nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 1, 1, ctypes.byref(nt)) if res < 0: return None elif res == 0: return None uid = None if nt.nti.nai.szUidLen == 4: uid = "".join( [format(nt.nti.nai.abtUid[i], '02x') for i in range(4)]) return uid
def _poll(self): """One iteration of a loop that polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 1, 2, ctypes.byref(nt)) print "RES", res if res < 0: print 'card absent' elif res >= 1: print 'length ',nt.nti.nai.szUidLen self.uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen)]) print self.uid.encode("hex") else: print 'fault'
def init_card_reader_mode(self): """ Prepares NFC reader hardware for interfacing with smart cards """ # Considerable target devices : mobile phone emulating smart card if self.__target is None: self.__target = nfc.nfc_target() if self.__context is None: self.__context = ctypes.pointer(nfc.nfc_context()) # Modulation properties if self.__modulation is None: self.__modulation = nfc.nfc_modulation() self.__modulation.nmt = nfc.NMT_ISO14443A self.__modulation.nbr = nfc.NBR_106
def _poll(self): """One iteration of a loop that polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 1, 2, ctypes.byref(nt)) print "RES", res if res < 0: print 'card absent' elif res >= 1: print 'length ', nt.nti.nai.szUidLen self.uid = "".join([ chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen) ]) print self.uid.encode("hex") else: print 'fault'
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 1, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: uid = None uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(nt.nti.nai.szUidLen)]) if uid: tagid = uid.encode("hex") global members member = members.find_one(tagid=tagid) if member: pp = pprint.PrettyPrinter(indent=4) print pp.pprint(member["expires"]) if member["expires"]: if member["expires"] > datetime.datetime.now(): self.lcd.accessgranted(member['firstname']) elif member["expires"] > datetime.datetime.now() - datetime.timedelta(days=5): self.lcd.accesswarning((member["expires"] - datetime.datetime.now() + datetime.timedelta(days=5)).days) else: self.lcd.accessdenied(tagid) else: self.lcd.accessdenied(tagid) time.sleep(2) self.lcd.idle() else: self.lcd.accessdenied(tagid) time.sleep(2) self.lcd.idle() #if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ # time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): #self._setup_device() # self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: uid = None if nt.nti.nai.szUidLen == 4: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(4)]) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() self.read_card(uid) self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) """ print uid """ #str = hex_dump(self._card_uid) #print "*** UID *** " + str # uidStr = "0x".join("{:02X}".format(ord(c)) for c in uid) # uidProcess = ''.join(('0x',uidStr)) # self.process(uidProcess) #time.sleep(2) else: self._card_present = False self._clean_card()
def init_card_emulation_target(self): """ Prepares NFC reader hardware for card emulation """ if self.__target == None: self.__target = nfc.nfc_target() # Modulation properties modulation = self.__target.nm # access by reference modulation.nmt = nfc.NMT_ISO14443A modulation.nbr = nfc.NBR_UNDEFINED # Target info: card emulation card = nfc.nfc_iso14443a_info() # type: target_info card.abtAtqa[:] = [0x00, 0x04] #atqaSet = (ctypes.c_uint8 * 2).from_buffer(card.abtAtqa) #atqaSet[:] = [0x00, 0x04] # card.abtAtqa[:] = [0x00, 0x04] # also seems to work directly print(card.abtAtqa[:]) # uid = "\x08\x00\xb0\xfe" uid = "\x08\x00\xb0\x0b" for i in range(4): card.abtUid[i] = ord(uid[i]) card.szUidLen = 4 #card.abtAtqa = [0x00, 0x04] card.btSak = 0x20 atsSet = (ctypes.c_uint8 * 4).from_buffer(card.abtAts) atsSet[:] = [0x75, 0x33, 0x92, 0x03] print(card.abtAts[:]) card.szAtsLen = 4 # Assign target_info for card emulation self.__target.nti.nai = card
def _poll_loop(self): """Starts a loop that constantly polls for cards""" nt = nfc.nfc_target() res = nfc.nfc_initiator_poll_target(self.__device, self.__modulations, len(self.__modulations), 10, 2, ctypes.byref(nt)) # print "RES", res if res < 0: raise IOError("NFC Error whilst polling") elif res >= 1: uid = None if nt.nti.nai.szUidLen == 4: uid = "".join([chr(nt.nti.nai.abtUid[i]) for i in range(4)]) if uid: if not ((self._card_uid and self._card_present and uid == self._card_uid) and \ time.mktime(time.gmtime()) <= self._card_last_seen + self.card_timeout): self._setup_device() if stage == 0: self.read_card(uid) #if stage is zero then look for the master #if master checked in, proceed to stage 1 if uid.encode("hex") == 'fd1cfe16': global stage stage += 1 print "Hello Master. Stage Zero Completed." print "_ _ _ _ _ _ _ _ _ _" os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o', 'hdmi', intromagician]) player=True time.sleep(28) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', er1]) player=True GPIO.setup(15,GPIO.OUT)#setting up the servo motor (pin =15 ) m=GPIO.PWM(15,50)#pin=15 frequency=50 heartz m.start(12.5)#starting at 180 degrees position ,basicly its unlocked op=2 #axristi metavliti gia na forcarw to susthma na mpei ston servomixanismo if (op==2): m.ChangeDutyCycle(2.5)#duty cycle = length/period opote gia na stripsei sti thesi 0 diladi na kleidwsei thelei na einai st dc 2.5 time.sleep(4)# o servomixanismos pernei pulses ana 20ms opote dc=0.5/20 * 100 =2.5 , g auto ka ievala kateutheian to 2.5 m.stop else: print "still no master found..." elif stage == 2: self.read_card(uid) #galazio if uid.encode("hex") == '525dc935': print "Good job buddy! Now get the O's and place them in the LED areas..." #turn on LEDs for O's #LED_EXTENDER: turn on first led to indicatre position for letter P. wiringpi.digitalWrite(101,1) wiringpi.digitalWrite(108,1) if isLetterAtPin(31,101) and isLetterAtPin(38,108): print "You found two O's! Congratulations!" print "P O _ _ _ _ _ _ O _" print " " os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', ap2]) player=True time.sleep(17) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', director_vid]) player=True time.sleep(16) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', er3]) player=True print "Stage 3 entered", "Waiting for the NFC..." global stage stage-=1 else: print "still no O's found..." elif stage == 3: self.read_card(uid) #prasino if uid.encode("hex") == '3982c935': print "Awesome! Now get the L's and place them where the LEDs are..." #turn on LEDs for L's #LED_EXTENDER: turn on first led to indicatre position for letter P. wiringpi.digitalWrite(102,1) if isLetterAtPin(33,102): print "You found the L! Congratulations!" print "P O L _ _ _ _ _ O _" os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', ap3]) player=True time.sleep(17) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', sailor_vid]) player=True time.sleep(17) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', er4]) player=True print " " print "Stage 4 entered", "Waiting for the button...!" else: print "still no L found..." elif stage == 5: self.read_card(uid) #kitrino if uid.encode("hex") == '9d4eec23': print "Awesome! Now get the T's and place them where the LEDs are..." #turn on LEDs for T's #LED_EXTENDER: turn on first led to indicatre position for letter P. wiringpi.digitalWrite(104,1) if isLetterAtPin(37, 104): print "You found the T! Congratulations!" print "P O L I T I _ _ O _" os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', ap5]) player=True time.sleep(17) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', typographer_vid]) player=True time.sleep(11) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', er6]) player=True print " " print "Stage 6 entered", "Waiting for the nfc 4...!" else: print "still no T found..." elif stage == 6: self.read_card(uid) #kokkino if uid.encode("hex") == '0d9dc935': print "Awesome! Now get the S's and place them where the LEDs are..." #turn on LEDs for S's #LED_EXTENDER: turn on first led to indicatre position for letter P. wiringpi.digitalWrite(106,1) wiringpi.digitalWrite(109,1) if isLetterAtPin(32,106) and isLetterAtPin(40,109): print "You found the S! Congratulations!" print "P O L I T I S _ O S" os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', ap6]) player=True time.sleep(22) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', ziller_vid]) player=True time.sleep(9) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', er7]) player=True print " " print "Stage 7 entered", "Waiting for the team work...!" global stage stage-=1 else: print "still no S found..." elif stage == 7: global allnfcs if ((allnfcs == 0 ) and (uid.encode("hex") == '525dc935')): allnfcs +=1 print "card1" elif ((allnfcs == 1 ) and (uid.encode("hex") == '3982c935')): allnfcs +=1 print "card2" elif ((allnfcs == 2) and (uid.encode("hex") == '9d4eec23')): allnfcs +=1 print "card3" elif ((allnfcs == 3) and (uid.encode("hex") == '0d9dc935')): allnfcs +=1 print "card4" print "Now you can place the letter M on the LEDs." #turn on leds #LED_EXTENDER: turn on first led to indicatre position for letter P. wiringpi.digitalWrite(107,1) if isLetterAtPin(36,107): print "You found the M! Congratulations!" print "P O L I T I S M O S" os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', ap7]) player=True time.sleep(17) os.system('killall omxplayer.bin') omxc=Popen(['omxplayer', '-o','hdmi', outromagician_vid]) player=True print " " print "OPEN" GPIO.setup(15,GPIO.OUT) p=GPIO.PWM(15,50) p.start(2.5) op=1 if (op==1): p.ChangeDutyCycle(12.5) time.sleep(4) p.stop else: print "Wrong card buddy! Wait for your turn!" self._card_uid = uid self._card_present = True self._card_last_seen = time.mktime(time.gmtime()) else: self._card_present = False self._clean_card()