Files
STM32F4-Base/test/host/iec104_netmcu/run_netmcu_tests.py
T
wandering1 13ed84a0f0 refactor(iec104): 把 104_port 适配层并入 104,协议栈只剩一个目录
104 从 App/protocol 迁入 App/iec60870 时,协议核心(common/ 104/ examples/)
与我方平台适配层(原 104_port/)仍分成两个平行目录。既然这个库已转为项目自有
代码,适配层没有独立存在的理由——它本来就是对协议的装配与平台后端。

本次只做目录合并,不动任何协议逻辑:

- App/iec60870/104_port/ 的 11 个文件全部移入 App/iec60870/104/
  (iec104_core / iec104_source / iec104_source_ranks / iec104_net_mcu /
  iec104_dtu_table / iec104_comm 的 .c/.h),空目录删除
- Keil 删掉 ../App/iec60870/104_port include 路径;模拟器 CMake 把重复的
  104_port include 行合并进既有的 IEC60870_DIR/104
- 单测运行器里硬拼的 "104_port" 路径段改成 "104"(rename_ref 只认斜杠形态)
- .pre-commit-config.yaml 的 iec104-tcp 触发面去掉 104_port 前缀;coding-standard
  的排除注释改写为「库已转为自有代码,排除项待收编完成后删除」
- 文档与生成物同步:项目架构总览、软件架构与运行时规格、IEC104 上行设计说明、
  产品需求、网络配置说明、App/README、仓根 README,以及引用关系图与架构 SVG

验证(均为实跑):
- Keil UV4 RC=0,0 errors 0 warnings,Code=367464 RO=48492 RW=2288 ZI=173544
- 模拟器构建 OK,编译告警 0,iec104-tcp 13/13 真实 TCP 回环通过
- 受影响单测:iec104_comm 50/50、iec104_serv 50/50、iec104_netmcu 13/13 与
  static 2/2、soe 32/32、iec104_dtu 7 tests OK
- 覆盖率门禁 RC=0,line 8204/9185 (89.3%),49/49 套件
- 全仓 104_port 残留:仅 App/README 里一句「原 104_port 并入 104」的历史说明
2026-11-18 19:17:25 +08:00

118 lines
5.0 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", "iec60870", "104", "iec104_net_mcu.c"),
]
env = dict(os.environ)
env["INCLUDE"] = os.pathsep.join(inc + [
os.path.join(ROOT, "App", "iec60870", "104"),
os.path.join(ROOT, "App", "iec60870", "common"),
os.path.join(ROOT, "App", "iec60870", "104"),
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/iec60870/104/iec104_net_mcu.c + fake net layer; host-only isolation evidence, not full core/TcpServer/hardware",
"backend_sha256": sha256(os.path.join(
ROOT, "App", "iec60870", "104", "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/iec60870/104/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())