Skip to content

Instantly share code, notes, and snippets.

@rena2019
Last active June 16, 2020 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rena2019/92c11c48718b6e055eed9809f4f79521 to your computer and use it in GitHub Desktop.
Save rena2019/92c11c48718b6e055eed9809f4f79521 to your computer and use it in GitHub Desktop.
import nfc
import struct
import sys
import ndef
# NDEF hex dump
#record = ndef.TextRecord("Hallo Welt", "de")
#print(b''.join(ndef.message_encoder([record])).hex())
def HEX(s):
return bytearray.fromhex(s)
# test data taken from https://github.com/nfcpy/nfcpy/blob/master/tests/test_tag_tt3.py
ndef_data_3 = HEX(
"10 01 01 00 05 00 00 00 00 00 01 00 00 10 00 28"
"d1 02 0b 53 70 d1 01 07 55 03 61 62 2e 63 6f 6d"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
"0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
)
#default NDEF record
record = ndef.TextRecord("Hallo Welt", "de")
ndef_data_area = bytearray(64 * 16)
if len(sys.argv) > 1:
#command line arg => 'en' text record
record = ndef.TextRecord(sys.argv[1], "en")
def set_ndef_data(record=ndef.TextRecord("Hallo Welt", "de")):
global ndef_data_area
print("set_ndef_data")
ndef_data_area[0] = 0x10 # NDEF mapping version '1.0'
ndef_data_area[1] = 3 # Number of blocks that may be read at once
ndef_data_area[2] = 3 # Number of blocks that may be written at once
ndef_data_area[4] = 3 # Number of blocks available for NDEF data
ndef_data_area[10] = 1 # NDEF read and write operations are allowed
ndef_data_area[13] = 0 # length?
ndef_data_area[14:16] = struct.pack('>H', sum(ndef_data_area[0:14])) # Checksum
#ndef_data_area = ndef_data_3
#ndef text
blob = b''.join(ndef.message_encoder([record]))
ndef_data_area[16:16+len(blob)] = blob
ndef_data_area[13] = len(blob) # length?
ndef_data_area[14:16] = struct.pack('>H', sum(ndef_data_area[0:14])) # Checksum
set_ndef_data(record)
#print(b' '.join(ndef_data_area).hex())
def ndef_read(block_number, rb, re):
print("ndef_read: ", block_number, rb, re)
if block_number < len(ndef_data_area) / 16:
first, last = block_number*16, (block_number+1)*16
block_data = ndef_data_area[first:last]
return block_data
def ndef_write(block_number, block_data, wb, we):
print("ndef_write")
global ndef_data_area
if block_number < len(ndef_data_area) / 16:
first, last = block_number*16, (block_number+1)*16
ndef_data_area[first:last] = block_data
return True
def on_startup(target):
print("on_startup")
idm, pmm, sys = '03FEFFE011223344', '01E0000000FFFF00', '12FC'
target.sensf_res = bytearray.fromhex('01' + idm + pmm + sys)
target.brty = "212F"
return target
def on_connect(tag):
print("tag activated")
tag.add_service(0x0009, ndef_read, ndef_write)
tag.add_service(0x000B, ndef_read, lambda: False)
return True
def emulate():
with nfc.ContactlessFrontend('usb') as clf:
print("with clf")
#with nfc.ContactlessFrontend('tty:USB0:pn532') as clf:
#print("while")
while clf.connect(card={'on-startup': on_startup, 'on-connect': on_connect}):
print("tag released")
return
import PySimpleGUI as sg
layout = [ [sg.Text('Text Record'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Cancel')] ]
# Create the Window
window = sg.Window('NDEF Text Message Emulation', layout) #,return_keyboard_events=True
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
#print("events,values")
if event in (None, 'Cancel'): # if user closes window or clicks cancel
break
print('You entered ', values[0])
set_ndef_data(ndef.TextRecord(values[0], "de"))
emulate()
window.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment