Files
STM32F4-Base/test/host/iec104_netmcu/run_netmcu_tests.py
T
wandering1 90cc9a7d99 feat(iec104): 把 104 骨架完整迁到 MCU 板端并在台架跑通总召
把 IEC104 从模拟器端整体搬到板端:协议栈原先由独立 iec104Task 驱动,
现在改为 netTask 单 owner 的 threadless 驱动,退役独立任务。

驱动与后端:
- 新增 iec104_net_mcu.c/.h,实现 IEC104_Net.h 的 11 个 ABI 函数,
  用槽表加单调句柄映射 net_socket 的单 fd 模型;CLIENT 拆除只走
  net_drop_conn_locked,绝不 net_close 监听 fd。
- net_socket 增 5 个 owner-only 接口(net_accept_locked、
  net_drop_conn_locked、net_pending_bytes、net_send_try、
  net_mono_now_ms)。
- iec104_serv.c 重写为 iec104_serv_net_tick(),在 netTask 1ms 主循环
  里驱动 core;无 osDelay/阻塞调用,喂狗是第一句。
- Src/freertos.c 删除 iec104Task 三件套,对应栈水位探针表项同步删除。

内存:目标板 192KB SRAM 装不下 vendor 默认容量档,按实测裁剪到
skeleton 档(TCP 缓冲、业务池、SOE 256->128、值槽密集化),
RW_IRAM1 118,408 B / 90.3%,留 2,178 B 余量。

台架修复(只在真硬件暴露):
- SNTP s_ctx.fd 零初始化后为 0,而 net fd 0 是合法槽位下标,
  首次代次变化时误 net_close(0) 关掉 ftpTask 的监听槽,导致 104
  连接被 FTP 的 220 banner 抢走;改为显式 .fd = -1。
- Accept 幂等:单 fd 模型下 vendor 每轮重试 Accept 会拿到同一 fd,
  后端改为会话在役时返 WOULD_BLOCK,Close 不再清槽。

验证:
- Keil 全量重建 0 error 0 warning,uvprojx 零抖动。
- 模拟器真实 TCP 13/13、transport 10/10。
- netmcu 后端单测 13/13(新增 N12)。
- 台架 serial-boot-test required 全中;板端 2404 总召 129 帧收到
  59 遥信(53 有源、全 IV)+ 16 双点 + 30 遥测(PT1 三相非零、
  UN/频率/温度按未标定置 IV)、遥控选择执行全否定、校时 COT 7。
- 期间顺带修正 UI 漂移自检文案、.serialtest required 陈旧项、
  coding-standard 对 vendor 与外部 ABI 名字的排除口径。
2026-11-18 10:29:18 +08:00

118 lines
5.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Build and run the REAL IEC104 Net MCU backend against the fake net layer.
Host-only isolation evidence for the IEC104_Net ABI (N1..N9). This is not a
full core/TcpServer run and not hardware evidence. The TEST DOUBLE B1..B7
evidence stays in results_stub/report.json.
"""
import hashlib
import json
import os
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
sys.path.insert(0, os.path.join(ROOT, "test", "host", "common"))
from unit_test_harness import locate_toolchain # noqa: E402
OUT = os.path.join(HERE, "results")
def sha256(path):
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(65536), b""):
h.update(chunk)
return h.hexdigest()
def run_real(cl, inc, lib):
work = os.path.join(OUT, "real")
os.makedirs(work, exist_ok=True)
objdir = os.path.join(work, "obj")
os.makedirs(objdir, exist_ok=True)
exe = os.path.join(work, "netmcu_test.exe")
sources = [
os.path.join(HERE, "netmcu_test.c"),
os.path.join(HERE, "net_socket_fake.c"),
os.path.join(ROOT, "App", "protocol", "iec104", "iec104_net_mcu.c"),
]
env = dict(os.environ)
env["INCLUDE"] = os.pathsep.join(inc + [
os.path.join(ROOT, "App", "protocol", "iec60870", "104"),
os.path.join(ROOT, "App", "protocol", "iec60870", "common"),
os.path.join(ROOT, "App", "protocol", "iec104"),
os.path.join(ROOT, "Drivers", "BSP", "NET"),
os.path.join(HERE, "stub"),
os.path.join(ROOT, "test", "host", "net_socket", "stub"),
HERE,
])
env["LIB"] = os.pathsep.join(lib)
env["PATH"] = os.path.dirname(cl) + os.pathsep + env.get("PATH", "")
flags = ["/nologo", "/std:c11", "/W3", "/WX", "/utf-8"]
objs = []
build_log = os.path.join(work, "build.log")
with open(build_log, "wb") as log:
for src in sources:
obj = os.path.join(objdir, os.path.basename(src) + ".obj")
p = subprocess.run([cl] + flags + ["/Fo" + obj, "/c", src],
env=env, cwd=HERE, capture_output=True)
log.write((p.stdout + p.stderr).decode("utf-8", "replace").encode("utf-8"))
if p.returncode != 0:
return {"build_rc": p.returncode, "run_rc": None, "note": "compile error"}
objs.append(obj)
p = subprocess.run([cl, "/nologo", "/Fe" + exe] + objs,
env=env, cwd=HERE, capture_output=True)
log.write((p.stdout + p.stderr).decode("utf-8", "replace").encode("utf-8"))
if p.returncode != 0:
return {"build_rc": p.returncode, "run_rc": None, "note": "link error"}
r = subprocess.run([exe], env=env, cwd=work, capture_output=True)
with open(os.path.join(work, "run.log"), "wb") as f:
f.write(r.stdout + r.stderr)
text = (r.stdout + r.stderr).decode("utf-8", "replace")
fail_lines = [ln for ln in text.splitlines() if ln.strip().startswith("FAIL ")]
return {"build_rc": 0, "run_rc": r.returncode,
"failures": len(fail_lines),
"failed_cases": sorted(set(
ln.split()[1].rstrip(":") for ln in fail_lines if len(ln.split()) > 1)),
"note": ""}
def main():
os.makedirs(OUT, exist_ok=True)
cl, inc, lib = locate_toolchain()
if not cl:
print("[ENV] MSVC toolchain not found")
return 2
res = run_real(cl, inc, lib)
print("real: build_rc={} run_rc={} note={} failed_cases={}".format(
res["build_rc"], res["run_rc"], res["note"], res.get("failed_cases", [])))
report = {
"scope": "REAL backend App/protocol/iec104/iec104_net_mcu.c + fake net layer; host-only isolation evidence, not full core/TcpServer/hardware",
"backend_sha256": sha256(os.path.join(
ROOT, "App", "protocol", "iec104", "iec104_net_mcu.c")),
"sources": {f: sha256(os.path.join(HERE, f)) for f in [
"MATRIX.md", "net_socket_fake.h", "net_socket_fake.c",
"netmcu_test.c", "check_static.py"]},
"case_count": 13,
"case_count_basis": "13 named cases; N2a/N2b are two cases in one N2 test function",
"case_names": ["N1", "N2a", "N2b", "N3", "N4", "N5", "N6", "N7", "N8", "N9", "N10", "N11", "N12"],
"result": res,
"bad_variants": "NOT_RUN for real backend; TEST DOUBLE B1..B7 evidence retained in results_stub/report.json",
"not_run": ["full IEC104 STARTDT over core/TcpServer", "board CH395F", "MCU timing/WCET"],
"known_deviations": ["NODELAY: App/protocol/iec104/iec104_net_mcu.h comment; CH395F has no setsockopt, treated as always no-Nagle and NOT an OK condition"],
}
with open(os.path.join(OUT, "report.json"), "w", encoding="utf-8") as f:
json.dump(report, f, indent=2, ensure_ascii=False)
if res["build_rc"] != 0 or res["run_rc"] is None:
return 1
print("real backend passed {}/{}".format(
13 - len(res.get("failed_cases", [])), 13))
return 0 if res["run_rc"] == 0 else 1
if __name__ == "__main__":
sys.exit(main())