A plugin-based core engine for DSL-style NFC testing automation.
Project description
nfcscript
NFC 测试脚本 DSL 执行环境,基于 nfctester 插件化架构。
安装
uv sync
使用
nfcscript - 脚本执行
nfcscript test_card.py
常用参数
# 指定串口
nfcscript test_card.py -p COM21
# 指定读卡器类型
nfcscript test_card.py -r acr122u
# 开启追踪层
nfcscript test_card.py --trace-driver --trace-protocol
# 设置日志级别
nfcscript test_card.py --trace-level DEBUG
# 组合使用
nfcscript test_card.py -p COM21 -r pn532 --trace-driver --trace-protocol --trace-level DEBUG
nfc-cli - 交互式命令行
# 列出所有可用卡片类型
nfc-cli --list-cards
# 连接卡片并进入交互模式
nfc-cli -c ntag224 -p COM21
# 指定读卡器类型
nfc-cli -c mifare_classic -p COM21 -r pn532
# 通过环境变量自动发现外部卡片
export NFC_CARD_PATH="../scripts/lib"
nfc-cli -c sm7 -p COM20
# 或手动指定
nfc-cli -c sm7 -p COM20 -i ../scripts/lib
交互命令
进入交互模式后,可以输入命令操作卡片:
> help # 显示可用命令
> auth 7 FFFFFFFFFFFF # 认证块 7
> read 10 # 读取页 10
> write 20 AABBCCDD... # 写入页 20
> quit # 退出
支持的值格式:
- 十六进制:
0xFF,0xAABBCCDD - 十进制:
7,255 - 字节串:
b'\xff\x00'
导入外部卡片
使用 -i 参数导入外部卡片模块,支持单个 .py 文件或包含 __init__.py 的目录:
# 导入单个文件
nfc-cli -c my_card -p COM20 -i /path/to/my_card.py
# 导入目录(需要有 __init__.py)
nfc-cli -c sm7 -p COM20 -i /path/to/lib
外部卡片模块需要使用 @CardRegistry.register("name") 装饰器注册。
示例
# NTAG224 认证并读取
nfc-cli -c ntag224 -p COM21
> auth 1 AABBCCDD112233445566778899001122
> read 4
# Mifare Classic 操作
nfc-cli -c mifare_classic -p COM21
> authenticate 7 FFFFFFFFFFFF
> read_block 8
> write_block 9 AABBCCDD112233445566778899001122
脚本示例
from nfc import *
# 连接读卡器 (默认使用环境变量 NFC_PORT 或 COM20)
connect()
# 开启追踪
trace.set_layer("DRIVER", True)
trace.set_layer("PROTOCOL", True)
# 寻卡(自动切换协议解析器)
card_info = active()
ASSERT_IS_NOT_NONE(card_info, "未扫描到卡片")
print(f"UID: {' '.join(f'{b:02X}' for b in card_info.uid)}")
# 创建卡片实例
from nfctester.registry import CardRegistry
card = CardRegistry.create("mifare_classic", reader=get_reader())
# 关闭连接
close()
上下文管理器
from nfc import *
with session() as reader:
card_info = reader.active()
print(card_info.uid)
# 自动断开连接
模块说明
| 模块 | 说明 |
|---|---|
reader.py |
读卡器连接与通信 |
trace.py |
日志追踪控制 |
assertions.py |
测试断言 |
bits.py |
位操作工具 |
hex_util.py |
十六进制工具 |
checksum.py |
校验工具 |
delay.py |
延时工具 |
API 参考
reader.py
| 函数 | 说明 |
|---|---|
connect(port, reader_type) |
连接读卡器 |
get_reader() |
获取当前读卡器实例 |
active(low_layer, ignore_error, reqa_cmd) |
寻卡,返回 CardInfo (uid/atq/sak),自动切换协议解析器 |
transceive(data, tx_crc, rx_crc) |
底层帧交互 |
transceive_bits(data, last_tx_bits, tx_crc, rx_crc) |
位控制帧交互 |
reqa() |
ISO14443-A REQA |
wupa() |
ISO14443-A WUPA |
halt() |
ISO14443-A HALT |
select(cl_level, uid) |
ISO14443-A SELECT |
anticoll(cl_level, nvb, uid_prefix) |
ISO14443-A ANTICOLL |
field_on() / field_off() |
RF 场控制 |
session(port, reader_type) |
上下文管理器 |
close() |
断开连接 |
trace.py
| 函数 | 说明 |
|---|---|
trace.set_layer(layer, enable) |
开启/关闭追踪层 |
trace.set_level(level) |
设置日志级别 |
trace.set_parse(level) |
设置解析级别 (0=关闭, 1=hex+摘要) |
trace.add_sink(fn) |
注册结构化 trace 事件回调 |
trace.remove_sink(fn) |
移除已注册的回调 |
trace.info(msg) / trace.error(msg) / trace.warning(msg) / trace.success(msg) / trace.debug(msg) |
输出日志 |
assertions.py
| 函数 | 说明 |
|---|---|
ASSERT_EQUAL(expected, actual, msg) |
相等断言 |
ASSERT_LEN(data, expected_len, msg) |
长度断言 |
ASSERT_IS_NOT_NONE(value, msg) |
非空断言 |
ASSERT_IS_NONE(value, msg) |
空值断言 |
bits.py
| 函数 | 说明 |
|---|---|
BITS_UPDATE(val, mask, data) |
Read-Modify-Write |
BITS_SET(val, mask) |
置位 |
BITS_RESET(val, mask) |
清位 |
hex_util.py
| 函数 | 说明 |
|---|---|
PARSE_HEX(text) |
解析 hex 字符串 |
FORMAT_HEX(data, last_bits) |
格式化为可视化 hex |
checksum.py
| 函数 | 说明 |
|---|---|
GET_BCC(data) |
计算异或校验 |
delay.py
| 函数 | 说明 |
|---|---|
DELAY_MS(ms) |
毫秒延时 |
环境变量
优先级: CLI 参数 > 内层 .env > 外层 .env > 系统环境变量 (必须配置,否则报错)
| 变量 | 说明 | 默认值 |
|---|---|---|
NFC_PORT |
串口号 | - |
NFC_READER |
读卡器类型 | - |
NFC_TRACE_LEVEL |
日志级别 | INFO |
NFC_TRACE_DRIVER |
开启 DRIVER 层追踪 | false |
NFC_TRACE_PROTOCOL |
开启 PROTOCOL 层追踪 | false |
NFC_CARD_PATH |
外部卡片模块搜索路径 (分号分隔) | - |
支持多层 .env 加载:从脚本目录向上搜索所有 .env 文件,按从外到内的顺序依次加载(内层覆盖外层同名变量)。
例如,脚本位于 /project/tests/run.py,沿途有 /project/.env 和 /project/tests/.env,加载顺序为:
/project/.env(基础配置)/project/tests/.env(覆盖基础配置中的同名变量)
在项目根目录或脚本所在目录创建 .env 文件进行持久化配置:
NFC_PORT=COM20
NFC_READER=pn532
NFC_TRACE_LEVEL=DEBUG
NFC_TRACE_DRIVER=true
NFC_TRACE_PROTOCOL=true
AI 编写脚本
本目录下 SKILL.md 提供了 DSL 的完整 API 参考和 few-shot 示例,可直接用于 AI Agent 生成 NFC 脚本。
使用方式:在 AI 对话中引用该文件,例如:
@nfcscript/SKILL.md
Agent 会基于 SKILL.md 中的 API 签名、few-shot 示例和约定生成正确的脚本代码。
开发
# 运行测试
uv run pytest
# 运行特定测试
uv run pytest tests/test_xxx.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nfcscript-0.0.13.tar.gz.
File metadata
- Download URL: nfcscript-0.0.13.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c73bf6b49f2810f9b60b576bbd00bae390137672990d525f95a3e96af82444f2
|
|
| MD5 |
40f8472d577c685a3de101892a25878a
|
|
| BLAKE2b-256 |
38ab7e29940a8efc168a8de014e88813ed307aba9874af4b2f0f74662def81fd
|
Provenance
The following attestation bundles were made for nfcscript-0.0.13.tar.gz:
Publisher:
publish-pypi.yml on CRThu/nfcscript
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nfcscript-0.0.13.tar.gz -
Subject digest:
c73bf6b49f2810f9b60b576bbd00bae390137672990d525f95a3e96af82444f2 - Sigstore transparency entry: 2123974251
- Sigstore integration time:
-
Permalink:
CRThu/nfcscript@cb76eab12605d2625870032a9f79df07752ea61f -
Branch / Tag:
refs/tags/v0.0.13 - Owner: https://github.com/CRThu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@cb76eab12605d2625870032a9f79df07752ea61f -
Trigger Event:
push
-
Statement type:
File details
Details for the file nfcscript-0.0.13-py3-none-any.whl.
File metadata
- Download URL: nfcscript-0.0.13-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a827b9ba2139f3b6bb762f732d170982e9d8d221f82881e455d1ce235ef1e571
|
|
| MD5 |
2e83aa70eb7170e67cbe74f6b85c062f
|
|
| BLAKE2b-256 |
617df75c08e3487bad414926100179f01eaf831979eb931f6c6130703af9bce2
|
Provenance
The following attestation bundles were made for nfcscript-0.0.13-py3-none-any.whl:
Publisher:
publish-pypi.yml on CRThu/nfcscript
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nfcscript-0.0.13-py3-none-any.whl -
Subject digest:
a827b9ba2139f3b6bb762f732d170982e9d8d221f82881e455d1ce235ef1e571 - Sigstore transparency entry: 2123974280
- Sigstore integration time:
-
Permalink:
CRThu/nfcscript@cb76eab12605d2625870032a9f79df07752ea61f -
Branch / Tag:
refs/tags/v0.0.13 - Owner: https://github.com/CRThu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@cb76eab12605d2625870032a9f79df07752ea61f -
Trigger Event:
push
-
Statement type: