Created
December 19, 2022 08:37
-
-
Save rena2019/9baa09aa291a1b9f0a7aced3f2fd549e to your computer and use it in GitHub Desktop.
nfc helper script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PC companion to MyNFCTestApp | |
# connect to an Android device that is connected via adb tunnel | |
# | |
# connect vai adb | |
# ------------------------------------------------- | |
# set PATH=D:\tools\platform-tools;%PATH% | |
# # switch to adb wifi connection ----------------- | |
# adb devices | |
# (e.g. 773X12X6M) | |
# adb -s 773X12X6M shell ip -f inet addr show wlan0 | |
# adb -s 773X12X6M tcpip 5555 | |
# adb -s 773X12X6M connect 192.168.178.107:5555 | |
# adb forward tcp:8000 tcp:8000 | |
# ------------------------------------------------- | |
# commands: | |
# apdu, pay, ndef, ... | |
# | |
# 2022-09-30 started | |
# | |
# use in REPL: | |
# from mynfctool import * | |
# send_cmd('apdu 00 A4 04 00 0C D2 76 00 01 35 4B 41 4E 4D 30 31 00 00') | |
# | |
# CLI: | |
# python -c "from mynfctool import *;send_cmd('apdu 00 A4 04 00 0C D2 76 00 01 35 4B 41 4E 4D 30 31 00 00')" | |
import socket | |
sock = None | |
def byte2hex(b): | |
#print('hex:', [hex(i) for i in b]) | |
return ("".join("%02x " % i for i in b)).strip() | |
def hex2byte(data): | |
s = data.replace(' ', '') | |
b = bytearray(int(len(s) / 2)) | |
for i in range(int(len(s) / 2)): | |
b[i] = int(s[2*i:2*i+2], 16) | |
return b | |
def sock_connect(): | |
global sock | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(("127.0.0.1", 8000)) | |
def sock_close(): | |
global sock | |
if not(sock is None): | |
sock.close() | |
sock = None | |
def send_cmd(cmd): | |
global sock | |
if sock is None: | |
sock_connect() | |
try: | |
sock.send((cmd + "\n").encode()) | |
sock.settimeout(10)#sec | |
ret = sock.recv(100) | |
while(not(b"\r\n" in ret)): | |
ret = ret + sock.recv(100) | |
print("<= " + ret.decode("ascii")) | |
return ret | |
except Exception as ex: | |
print("error: " + str(ex)) | |
sock_close() | |
return None | |
if __name__ == "__main__": | |
sock_connect() | |
send_cmd("apdu 00 A4 04 00") | |
sock_close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment