From ac7f52211389398caade8ed56a2f6d89ce6f8ce4 Mon Sep 17 00:00:00 2001 From: champignoom Date: Mon, 11 Aug 2025 16:08:24 +0800 Subject: [PATCH 1/9] Changed|Added(scripts/termux-nfc): support all NFC APIs --- scripts/termux-nfc.in | 157 ++++++++++++++++++++++++++++++++---------- 1 file changed, 121 insertions(+), 36 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index b1b26c1..b65a484 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -3,45 +3,130 @@ set -e -u show_usage() { - echo "Usage: termux-nfc [-r [short|full]] [-w] [-t [text for TAG] " - echo " read/write data from/to NDEF tag " - echo " -r, read tag " - echo " short, read short information from tag " - echo " full, read full information from tag " - echo " -w, write information on tag " - echo " -t, text for tag" - exit 0 -} + echo "Usage: termux-nfc subcommands... args..." + echo "" + echo "Wrappers for all Android NFC APIs, except for get(), which is merged with connect() (see Example below)." + echo "" + echo "Byte array is supplied and returned in hexadecimal format, for example: 01234567DEADBEEF". + echo "" + echo "Return values or exceptions are in JSON format." + echo "" + echo "During the NFC connection, the auxiliary NFC activity should be kept in the foreground, possibly as picture-in-picture." + echo "" + echo "Subcommands:" + echo " quit" + echo " ---" + echo " TagTechnology close" + echo " TagTechnology isConnected" + echo " TagTechnology getTag" + echo " ---" + echo " NfcA connect" + echo " NfcA getAtqa" + echo " NfcA getMaxTransceiveLength" + echo " NfcA getSak" + echo " NfcA getTimeout" + echo " NfcA setTimeout " + echo " NfcA transceive " + echo " ---" + echo " NfcB connect" + echo " NfcB getApplicationData" + echo " NfcB getMaxTransceiveLength" + echo " NfcB getProtocolInfo" + echo " NfcB transceive " + echo " ---" + echo " NfcF connect" + echo " NfcF getSystemCode" + echo " NfcF getManufacturer" + echo " NfcF getMaxTransceiveLength" + echo " NfcF getSystemCode" + echo " NfcF getTimeout" + echo " NfcF setTimeout " + echo " NfcF transceive " + echo " ---" + echo " NfcV connect" + echo " NfcV getDsfId" + echo " NfcV getMaxTransceiveLength" + echo " NfcV getResponseFlags" + echo " NfcV transceive " + echo " ---" + echo " IsoDep connect" + echo " IsoDep getHiLayerResponse" + echo " IsoDep getHistoricalBytes" + echo " IsoDep getMaxTransceiveLength" + echo " IsoDep getTimeout" + echo " IsoDep isExtendedLengthApduSupported" + echo " IsoDep setTimeout " + echo " IsoDep transceive " + echo " ---" + echo " Ndef canMakeReadOnly" + echo " Ndef connect" + echo " Ndef getCachedNdefMessage" + echo " Ndef getMaxSize" + echo " Ndef getNdefMessage" + echo " Ndef getType" + echo " Ndef isWritable" + echo " Ndef makeReadOnly" + echo " Ndef writeNdefMessage " + echo " ---" + echo " MifareClassic authenticateSectorWithKeyA " + echo " MifareClassic authenticateSectorWithKeyB " + echo " MifareClassic blockToSector " + echo " MifareClassic connect" + echo " MifareClassic decrement " + echo " MifareClassic getBlockCount" + echo " MifareClassic getBlockCountInSector " + echo " MifareClassic getMaxTransceiveLength" + echo " MifareClassic getSectorCount" + echo " MifareClassic getSize" + echo " MifareClassic getTimeout" + echo " MifareClassic getType" + echo " MifareClassic increment " + echo " MifareClassic readBlock " + echo " MifareClassic restore " + echo " MifareClassic sectorToBlock " # Duplicate if blockToSector is preferred, pick one + echo " MifareClassic setTimeout " + echo " MifareClassic transceive " + echo " MifareClassic transfer " + echo " MifareClassic writeBlock " + echo " ---" + echo " MifareUltralight connect" + echo " MifareUltralight getMaxTransceiveLength" + echo " MifareUltralight getTimeout" + echo " MifareUltralight getType" + echo " MifareUltralight readPages " + echo " MifareUltralight setTimeout " + echo " MifareUltralight transceive " + echo " MifareUltralight writePage " + echo " ---" + echo " NfcBarcode connect" + echo " NfcBarcode getBarcode" # Assuming this is a method for NfcBarcode + echo " NfcBarcode getType" + echo " ---" + echo " NdefFormatable connect" + echo " NdefFormatable format " + echo " NdefFormatable formatReadOnly " + echo "" + echo "Example:" + echo " termux-nfc IsoDep connect # Discover the current tag that you believe to support IsoDep and connect to it" + echo " termux-nfc IsoDep transceive 01020304 # Transceive data with the connected IsoDep tag" + echo " termux-nfc IsoDep transceive deadbeef # Keep the state and transceive more data" + echo " termux-nfc TagTechnology close # Close the current tag connection and clean the state" + echo " termux-nfc quit # Exit the auxiliary NFC activity" -ARG_R="" -OPT_R="" -ARG_W="" -OPT_T="" -ARG_T="" -PARAM="" + exit 0 +} -if [ $# -eq 0 ];then show_usage;fi +if [ $# -eq 0 ] || + { [ $# -eq 1 ] && { [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ]; }; } +then + show_usage + exit 1 +fi -while getopts ":r:t:w" opt -do - case "$opt" in - r) ARG_R="--es mode read"; OPT_R="--es param $OPTARG"; ;; - w) ARG_W="--es mode write";; - t) ARG_T="--es param text --es value"; OPT_T="$OPTARG"; ;; - ?) echo "Error: unknown parameters: $opt $OPTARG;";show_usage; ;; - esac +api_args=() +for ((i=1; i<=$#; ++i)); do + args+=("--es" "arg$i" "${!i}") done - -shift $((OPTIND-1)) - -if [ "$#" != 0 ]; then echo "Error: too many parameters!"; show_usage;fi -if [ -n "$ARG_R" ]; then if [ -n "$ARG_W" ]; then echo "Error: Incompatible parameters! \"-r and \"-n";show_usage;fi;fi - - - -if [ -n "$ARG_R" ]; then set -- "$@" $ARG_R $OPT_R;fi -if [ -n "$ARG_W" ]; then set -- "$@" $ARG_W;fi -if [ -n "$ARG_T" ]; then set -- "$@" $ARG_T "$OPT_T";fi -@TERMUX_PREFIX@/libexec/termux-api Nfc "$@" +@TERMUX_PREFIX@/libexec/termux-api Nfc "${args[@]}" From 544fa2df33d07aa9c406f1bd915b94bc9c4da271 Mon Sep 17 00:00:00 2001 From: champignoom Date: Thu, 14 Aug 2025 16:33:37 +0800 Subject: [PATCH 2/9] adapt to json version --- scripts/termux-nfc.in | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index b65a484..0f4686e 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -3,19 +3,15 @@ set -e -u show_usage() { - echo "Usage: termux-nfc subcommands... args..." + echo "Usage: termux-nfc -" echo "" - echo "Wrappers for all Android NFC APIs, except for get(), which is merged with connect() (see Example below)." + echo "Reads from stdin line by line, parse each line as JSON object specifying an NFC API, and execute it." echo "" - echo "Byte array is supplied and returned in hexadecimal format, for example: 01234567DEADBEEF". - echo "" - echo "Return values or exceptions are in JSON format." + echo 'Format: {"class": "", "method": "", "args": [ARG1, ARG2, ...]}' echo "" - echo "During the NFC connection, the auxiliary NFC activity should be kept in the foreground, possibly as picture-in-picture." + echo "Byte array is supplied and returned in hexadecimal format, for example: 01234567DEADBEEF". echo "" - echo "Subcommands:" - echo " quit" - echo " ---" + echo "Class/Method/Args:" echo " TagTechnology close" echo " TagTechnology isConnected" echo " TagTechnology getTag" @@ -107,12 +103,10 @@ show_usage() { echo " NdefFormatable formatReadOnly " echo "" echo "Example:" - echo " termux-nfc IsoDep connect # Discover the current tag that you believe to support IsoDep and connect to it" - echo " termux-nfc IsoDep transceive 01020304 # Transceive data with the connected IsoDep tag" - echo " termux-nfc IsoDep transceive deadbeef # Keep the state and transceive more data" - echo " termux-nfc TagTechnology close # Close the current tag connection and clean the state" - echo " termux-nfc quit # Exit the auxiliary NFC activity" - + echo ' {"class":"IsoDep", "method": "connect", "args": []}' + echo ' {"class":"IsoDep", "method": "transceive", "args": ["01020304"]}' + echo ' {"class":"IsoDep", "method": "transceive", "args": ["deadbeef"]}' + echo ' {"class":"TagTechnology", "method": "close", "args": []}' exit 0 } @@ -124,9 +118,4 @@ then exit 1 fi -api_args=() -for ((i=1; i<=$#; ++i)); do - args+=("--es" "arg$i" "${!i}") -done - -@TERMUX_PREFIX@/libexec/termux-api Nfc "${args[@]}" +@TERMUX_PREFIX@/libexec/termux-api Nfc \ No newline at end of file From 60da058e0b71a06804ce6d1bb0bb2c6c6410830e Mon Sep 17 00:00:00 2001 From: champignoom Date: Fri, 15 Aug 2025 01:51:43 +0800 Subject: [PATCH 3/9] adapt to op code --- scripts/termux-nfc.in | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index 0f4686e..c7db2ca 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -7,7 +7,8 @@ show_usage() { echo "" echo "Reads from stdin line by line, parse each line as JSON object specifying an NFC API, and execute it." echo "" - echo 'Format: {"class": "", "method": "", "args": [ARG1, ARG2, ...]}' + echo "Tag discovery: {"op": "discoverTag"}" + echo 'API Format: {"op": "api", "class": "", "method": "", "args": [ARG1, ARG2, ...]}' echo "" echo "Byte array is supplied and returned in hexadecimal format, for example: 01234567DEADBEEF". echo "" @@ -103,10 +104,15 @@ show_usage() { echo " NdefFormatable formatReadOnly " echo "" echo "Example:" - echo ' {"class":"IsoDep", "method": "connect", "args": []}' - echo ' {"class":"IsoDep", "method": "transceive", "args": ["01020304"]}' - echo ' {"class":"IsoDep", "method": "transceive", "args": ["deadbeef"]}' - echo ' {"class":"TagTechnology", "method": "close", "args": []}' + echo ' {"op": "discoverTag"}' + echo ' {"op": "api", "class":"IsoDep", "method": "connect", "args": []}' + echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": ["01020304"]}' + echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": ["deadbeef"]}' + echo ' {"op": "api", "class":"TagTechnology", "method": "close", "args": []}' + echo ' {"op": "api", "class":"MifareClassic", "method": "connect", "args": []}' + echo ' {"op": "api", "class":"MifareClassic", "method": "authenticateSectorWithKeyA", "args": [0, "FFFFFFFFFFFFFFFF"]}' + echo ' {"op": "api", "class":"MifareClassic", "method": "readBlock", "args": [0]}' + echo ' {"op": "api", "class":"TagTechnology", "method": "close", "args": []}' exit 0 } From 8581acb14693acae5c80acd7a44b762e9ac0c939 Mon Sep 17 00:00:00 2001 From: champignoom Date: Fri, 15 Aug 2025 01:55:02 +0800 Subject: [PATCH 4/9] save --- scripts/termux-nfc.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index c7db2ca..d39a48a 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -5,10 +5,10 @@ set -e -u show_usage() { echo "Usage: termux-nfc -" echo "" - echo "Reads from stdin line by line, parse each line as JSON object specifying an NFC API, and execute it." + echo "Reads from stdin line by line, parse each line as JSON object specifying an NFC operation, and execute it." echo "" echo "Tag discovery: {"op": "discoverTag"}" - echo 'API Format: {"op": "api", "class": "", "method": "", "args": [ARG1, ARG2, ...]}' + echo 'API: {"op": "api", "class": "", "method": "", "args": [ARG1, ARG2, ...]}' echo "" echo "Byte array is supplied and returned in hexadecimal format, for example: 01234567DEADBEEF". echo "" From 6b9604585d6cf32116a54e20d7d85d4e2f1bbe7b Mon Sep 17 00:00:00 2001 From: champignoom Date: Fri, 15 Aug 2025 01:59:28 +0800 Subject: [PATCH 5/9] save --- scripts/termux-nfc.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index d39a48a..0a0733d 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -80,7 +80,7 @@ show_usage() { echo " MifareClassic increment " echo " MifareClassic readBlock " echo " MifareClassic restore " - echo " MifareClassic sectorToBlock " # Duplicate if blockToSector is preferred, pick one + echo " MifareClassic sectorToBlock " echo " MifareClassic setTimeout " echo " MifareClassic transceive " echo " MifareClassic transfer " @@ -96,7 +96,7 @@ show_usage() { echo " MifareUltralight writePage " echo " ---" echo " NfcBarcode connect" - echo " NfcBarcode getBarcode" # Assuming this is a method for NfcBarcode + echo " NfcBarcode getBarcode" echo " NfcBarcode getType" echo " ---" echo " NdefFormatable connect" From 0f8a0feb706d4e4bc6816ff72eeece542bc64bd2 Mon Sep 17 00:00:00 2001 From: champignoom Date: Fri, 15 Aug 2025 02:02:11 +0800 Subject: [PATCH 6/9] save --- scripts/termux-nfc.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index 0a0733d..6f7cc3f 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -106,8 +106,7 @@ show_usage() { echo "Example:" echo ' {"op": "discoverTag"}' echo ' {"op": "api", "class":"IsoDep", "method": "connect", "args": []}' - echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": ["01020304"]}' - echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": ["deadbeef"]}' + echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": ["00A40000023F00"]}' echo ' {"op": "api", "class":"TagTechnology", "method": "close", "args": []}' echo ' {"op": "api", "class":"MifareClassic", "method": "connect", "args": []}' echo ' {"op": "api", "class":"MifareClassic", "method": "authenticateSectorWithKeyA", "args": [0, "FFFFFFFFFFFFFFFF"]}' From d65dc37253b4ae369fb38a3e512026a712977417 Mon Sep 17 00:00:00 2001 From: champignoom Date: Fri, 15 Aug 2025 03:03:00 +0800 Subject: [PATCH 7/9] save --- scripts/termux-nfc.in | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index 6f7cc3f..35fe2db 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -65,13 +65,13 @@ show_usage() { echo " Ndef makeReadOnly" echo " Ndef writeNdefMessage " echo " ---" - echo " MifareClassic authenticateSectorWithKeyA " - echo " MifareClassic authenticateSectorWithKeyB " + echo " MifareClassic authenticateSectorWithKeyA " + echo " MifareClassic authenticateSectorWithKeyB " echo " MifareClassic blockToSector " echo " MifareClassic connect" echo " MifareClassic decrement " echo " MifareClassic getBlockCount" - echo " MifareClassic getBlockCountInSector " + echo " MifareClassic getBlockCountInSector " echo " MifareClassic getMaxTransceiveLength" echo " MifareClassic getSectorCount" echo " MifareClassic getSize" @@ -80,7 +80,7 @@ show_usage() { echo " MifareClassic increment " echo " MifareClassic readBlock " echo " MifareClassic restore " - echo " MifareClassic sectorToBlock " + echo " MifareClassic sectorToBlock " echo " MifareClassic setTimeout " echo " MifareClassic transceive " echo " MifareClassic transfer " @@ -117,7 +117,7 @@ show_usage() { } if [ $# -eq 0 ] || - { [ $# -eq 1 ] && { [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ]; }; } + { [ $# -eq 1 ] && { [ "$1" != "-" ]; }; } then show_usage exit 1 From b5519d6a5f22a94d481738e2f940c99a21ac87e1 Mon Sep 17 00:00:00 2001 From: champignoom Date: Fri, 15 Aug 2025 03:05:12 +0800 Subject: [PATCH 8/9] save --- scripts/termux-nfc.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index 35fe2db..9c0bdc9 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -116,8 +116,7 @@ show_usage() { exit 0 } -if [ $# -eq 0 ] || - { [ $# -eq 1 ] && { [ "$1" != "-" ]; }; } +if [ $# -ne 1 ] || [ "$1" != "-" ]; then show_usage exit 1 From d7ecbbb0d6a3f605d3e37f11e64f9c649473b686 Mon Sep 17 00:00:00 2001 From: champignoom Date: Wed, 20 Aug 2025 02:25:29 +0800 Subject: [PATCH 9/9] save --- scripts/termux-nfc.in | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/termux-nfc.in b/scripts/termux-nfc.in index 9c0bdc9..c608d6e 100644 --- a/scripts/termux-nfc.in +++ b/scripts/termux-nfc.in @@ -106,10 +106,10 @@ show_usage() { echo "Example:" echo ' {"op": "discoverTag"}' echo ' {"op": "api", "class":"IsoDep", "method": "connect", "args": []}' - echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": ["00A40000023F00"]}' + echo ' {"op": "api", "class":"IsoDep", "method": "transceive", "args": [{"format": "hex", "value": "00A40000023F00"}]}' echo ' {"op": "api", "class":"TagTechnology", "method": "close", "args": []}' echo ' {"op": "api", "class":"MifareClassic", "method": "connect", "args": []}' - echo ' {"op": "api", "class":"MifareClassic", "method": "authenticateSectorWithKeyA", "args": [0, "FFFFFFFFFFFFFFFF"]}' + echo ' {"op": "api", "class":"MifareClassic", "method": "authenticateSectorWithKeyA", "args": [0, {"format": "hex", "value": "FFFFFFFFFFFFFFFF"}]}' echo ' {"op": "api", "class":"MifareClassic", "method": "readBlock", "args": [0]}' echo ' {"op": "api", "class":"TagTechnology", "method": "close", "args": []}' @@ -122,4 +122,4 @@ then exit 1 fi -@TERMUX_PREFIX@/libexec/termux-api Nfc \ No newline at end of file +@TERMUX_PREFIX@/libexec/termux-api Nfc