Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nfc
- import nfc.clf
- import nfc.ndef
- import nfc.tag
- import ndef
- # TODO 3 trys for right nfc tag
- application_uri = "http://marcelochsendorf.com"
- write_try =3
- key_to_write = "1337"
- last_id = ""
- tag_written = 0
- reader_con = False
- clf = nfc.ContactlessFrontend('usb')
- def nfc_connected(tag):
- print("-- CARD SCANNED --")
- global write_try
- global last_id
- global key_to_write
- global application_uri
- if(key_to_write == ""):
- print("-- NO KEY TO WRITE --")
- return
- if(last_id == tag.identifier.encode("hex").upper()):
- print("please scan an other tag")
- return
- #SAFE UUID
- last_id = tag.identifier.encode("hex").upper()
- print last_id
- #TODO ADD TRY
- version = {'Type1Tag': 0x12, 'Type2Tag': 0x12,'Type3Tag': 0x10, 'Type4Tag': 0x30}[tag.type]
- formatted = tag.format(version=version)
- if tag.ndef and formatted:
- print " NDEF"
- print(" readable = %s" % ("no","yes")[tag.ndef.is_readable])
- print(" writeable = %s" % ("no","yes")[tag.ndef.is_writeable])
- print(" capacity = %d byte" % tag.ndef.capacity)
- print(" message = %d byte" % tag.ndef.length)
- obj = {"application":"kaps_data_exporter","key":key_to_write}
- tag.ndef.records = [ndef.SmartposterRecord(str(application_uri),'KAPS'),ndef.TextRecord(str(obj))]
- print "on card:", tag.ndef.message.pretty()
- if(key_to_write in tag.ndef.message.pretty()):
- print("-- KEY WRITTEN TO TAG --")
- key_to_write = ""
- tag_written = 1
- #TODO LOG TO DB
- else:
- if(write_try <= 0):
- key_to_write = ""
- print "nfc write limit exeed"
- tag_written = 2
- else:
- write_try = write_try - 1
- print "sorry, no NDEF or not formattable please try again: " + str(write_try)
- tag_written = 3
- def nfc_write_aes_key_to_nfc(_key):
- print("--- WRITE AES TO NFC - INSERT A NFC TAG ---")
- global reader_con
- if(reader_con is False):
- print("-- NO READER CONNECTED - CALL INIT")
- return
- global key_to_write
- global tag_written
- tag_written = 0
- key_to_write = _key
- #while(tag_written == 0):
- tag = clf.connect(rdwr={'on-connect': nfc_connected})
- return tag_written
- def eject_card():
- print "eject_card"
- # SEND TO CARDDISPENSER
- def nfc_init():
- global reader_con
- #clf = nfc.ContactlessFrontend('usb')
- if(clf.open('usb') is True):
- reader_con = True
- else:
- reader_con = false
- nfc_init()
- nfc_write_aes_key_to_nfc("12354334535")
- #init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement