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」的历史说明
This commit is contained in:
2026-11-18 19:17:25 +08:00
parent d8d8fbe081
commit 13ed84a0f0
53 changed files with 6019 additions and 5579 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
/*
* IEC104 [comm] segment host unit test.
* Builds the real App/iec60870/104_port/iec104_comm.c into this translation unit
* Builds the real App/iec60870/104/iec104_comm.c into this translation unit
* and fakes only the config engine boundary (cfg_file_register/cfg_file_save)
* against the real App/cfg/cfg_file.h interface.
*/
+1 -1
View File
@@ -15,7 +15,7 @@ BUILD = TestBuild(
src=os.path.join(HERE, "iec104_comm_test.c"),
include_dirs=[
os.path.join(ROOT, "App", "cfg"),
os.path.join(ROOT, "App", "iec60870", "104_port"),
os.path.join(ROOT, "App", "iec60870", "104"),
HERE,
],
summary_marker="=== IEC104 Comm Tests:",
+3 -3
View File
@@ -49,13 +49,13 @@ def main():
fixture.write_text(text.replace(old, new), encoding="utf-8")
extra = []
if name == "source_binding_test":
extra = [ROOT / "App/iec60870/104_port/iec104_source.c", ROOT / "App/iec60870/104_port/iec104_dtu_table.c", vendor / "104/IEC104_Net_Windows.c", vendor / "104/IEC104_TcpServer.c"]
extra = [ROOT / "App/iec60870/104/iec104_source.c", ROOT / "App/iec60870/104/iec104_dtu_table.c", vendor / "104/IEC104_Net_Windows.c", vendor / "104/IEC104_TcpServer.c"]
if name == "core_lifecycle_test":
extra = [ROOT / "App/iec60870/104_port/iec104_core.c", ROOT / "App/iec60870/104_port/iec104_source.c", ROOT / "App/iec60870/104_port/iec104_dtu_table.c"]
extra = [ROOT / "App/iec60870/104/iec104_core.c", ROOT / "App/iec60870/104/iec104_source.c", ROOT / "App/iec60870/104/iec104_dtu_table.c"]
flags = ["/nologo", "/std:c11", "/O2", "/W3", "/WX", "/utf-8", "/D_CRT_SECURE_NO_WARNINGS"]
if name == "iec104_remote_parameter_test":
flags += ["/DIEC104_REMOTE_PARAMETER_TEST_HOOKS", "/DIEC104_TCP_HIGH_CAPACITY=251"]
command = [cl] + flags + ["/I" + str(p) for p in include] + ["/I" + str(ROOT / "App/iec60870/104_port")] + [str(fixture)] + [str(p) for p in sources + extra] + ["/Fe:" + str(target / (name + ".exe"))] + (["/link", "ws2_32.lib"] if extra else [])
command = [cl] + flags + ["/I" + str(p) for p in include] + ["/I" + str(ROOT / "App/iec60870/104")] + [str(fixture)] + [str(p) for p in sources + extra] + ["/Fe:" + str(target / (name + ".exe"))] + (["/link", "ws2_32.lib"] if extra else [])
build = subprocess.run(command, cwd=target, env=env, capture_output=True)
(target / "build.log").write_text(decode(build.stdout) + decode(build.stderr), encoding="utf-8")
row = {"name": name, "build_rc": build.returncode, "command": command, "fixture_sha256": hashlib.sha256(fixture.read_bytes()).hexdigest(), "adaptation": adaptation, "extra_sources_sha256": {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in extra}}
+3 -3
View File
@@ -2,20 +2,20 @@
## 交接说明(最终交付状态)
- **这组测试证明什么**:针对 `App/iec60870/104_port/iec104_net_mcu.c`(被测 SHA256 `08ca22b9d61ec86181973ab97d7fab68ec8f0ecbf944bfd758e1546a80dc8b35`)的真实后端,在 fake net 层上证明 IEC104 Net ABI 契约:同 fd 语义下 Close 不毁监听、Pending_Input 精确/降级有界续收、Send 短写且不借用 buffer、Now_Ms 跨 32 位 tick 回绕单调、网络代次与句柄失效、断链重连、非 owner 入口拒绝、两句柄模型下代次重建后的监听存活(armed==1)与无 ABA、以及单 fd 会话的 Accept 一次性语义(已有 CLIENT/CLIENT_EOF 槽时 WOULD_BLOCK、零芯片事务、Close 后释放;旧语义"re-Accept 再发新句柄"已实测导致板端拆活连接,2026-11-18 台架)。最终实测:文本级静态 2/2、静态 checker 变异自测 `real_rc=0 mutant_rc=1`、动态 12/12(`real backend passed 12/12`)。
- **这组测试证明什么**:针对 `App/iec60870/104/iec104_net_mcu.c`(被测 SHA256 `08ca22b9d61ec86181973ab97d7fab68ec8f0ecbf944bfd758e1546a80dc8b35`)的真实后端,在 fake net 层上证明 IEC104 Net ABI 契约:同 fd 语义下 Close 不毁监听、Pending_Input 精确/降级有界续收、Send 短写且不借用 buffer、Now_Ms 跨 32 位 tick 回绕单调、网络代次与句柄失效、断链重连、非 owner 入口拒绝、两句柄模型下代次重建后的监听存活(armed==1)与无 ABA、以及单 fd 会话的 Accept 一次性语义(已有 CLIENT/CLIENT_EOF 槽时 WOULD_BLOCK、零芯片事务、Close 后释放;旧语义"re-Accept 再发新句柄"已实测导致板端拆活连接,2026-11-18 台架)。最终实测:文本级静态 2/2、静态 checker 变异自测 `real_rc=0 mutant_rc=1`、动态 12/12(`real backend passed 12/12`)。
- **这组测试不证明什么**:fake net 层只是按产品头语义实现,不是真实 CH395F 驱动或板端电路;**CH395F 板端行为未验**(同 fd 语义待板端确认);**core / TcpServer 集成未验**(完整 STARTDT 建链为 NOT_RUN);netTask 队列/临界区与板端时序未验;MCU WCET / 性能未测;真实后端编译期坏变体未做,历史 TEST DOUBLE B1~B7 仅证明 harness 有效性;S1/S2 是文本级静态检查,不覆盖宏展开或函数指针间接调用。禁止据此写“板端已验证”。
- **一条命令复现**(仓库根执行):`python test/host/iec104_netmcu/check_static_selftest.py && python test/host/iec104_netmcu/check_static.py && python test/host/iec104_netmcu/run_netmcu_tests.py && git diff --check -- test/host/iec104_netmcu`
- **产物在哪里**:本目录 `test/host/iec104_netmcu/`;最终动态证据 `results/real/{build.log,run.log,report.json}`,静态证据 `results/static/report.json`,静态 checker 变异自测 `results/static_mutant/{report.json,selftest.txt,iec104_net_mcu_BAD.c}`,历史 TEST DOUBLE 证据 `results/report.json` 与 `results/{good,B1..B7}/`。所有文件未提交、未改产品/供应商源码;第二步 serv 重写后待命做 `iec104_serv_net_tick` 针对性核查。
## 状态与边界
- 被测对象:`App/iec60870/104_port/iec104_net_mcu.c`(真实板端 Net 后端,net-cutover 所有)对 `IEC104_Net.h` 的实现。
- 被测对象:`App/iec60870/104/iec104_net_mcu.c`(真实板端 Net 后端,net-cutover 所有)对 `IEC104_Net.h` 的实现。
- 测试替身:`net_socket_fake.{h,c}` 按产品 `Drivers/BSP/NET/net_socket.h` 声明模拟 `net_*` owner 出口(包括 `net_send_try` / `net_pending_bytes` / `net_mono_now_ms` / `net_accept_locked` / `net_drop_conn_locked` / `net_ctx_is_owner_task` 等),并受控注入单连接同 fd、短写 / WOULD_BLOCK / BUSY / RETRY_LATER、EOF / RST、网络代次变化、句柄非复用、芯片接收队列长度与 32 位 tick 回绕。绝不调用真实 CH395F 或 Windows socket。
- **CH395F 同 fd 语义(fake 已如实建模,板端仍待验证)**:`net_accept` / `net_accept_locked` 返回**监听 socket 自身**(`net_socket_fake.c:629-643` 的 `return sockfd;`),并为同一 fake fd 置 `established=1`;`net_close` 对监听 fd 会**真实销毁监听**并递增 `listen_closed_attempts`(`net_socket_fake.c:760-777`),而 `net_drop_conn` 只拆会话、保留监听(`net_socket_fake.c:779-797`)。因此 fake 不是 Winsock 式独立 fd 语义,N1/N8 断言的“Close 不毁监听”确实在同 fd 规则下判定;但这只是 fake 忠实模拟,**CH395F 实际行为仍待板端验证**。
- 本目录不进 `MDK-ARM/*.uvprojx`,不碰产品构建;只在主机侧用 MSVC 编译运行。
- 证据边界:这是 **真实后端 + fake net 层** 的 Net ABI 隔离证据,不等于完整 `iec104_core` / `TcpServer` 链路,不等于板端 CH395F / netTask 时序,也不是 MCU WCET。完整 STARTDT 建链流程仍为 NOT_RUN。
- Lead 三条口径:① `Now_Ms` = N-2(owner 周期采样),只测回绕后单调,不测“不调用也不漏记”;② `Pending_Input` 精确:RX=7 报 7、RX=0 报 0,不谎报 1/0;③ `Send` 返回后不借用 buffer:每次用不同源 buffer,返回即覆写,再断言 fake 内核中字节未被破坏。
- 已知平台偏离:CH395F 无 `setsockopt`,`App/iec60870/104_port/iec104_net_mcu.h` 注释已入档;恒无 Nagle 视作平台事实,**不以 NODELAY 作为 OK 条件**。plan.md 的入档由 net-cutover 负责。
- 已知平台偏离:CH395F 无 `setsockopt`,`App/iec60870/104/iec104_net_mcu.h` 注释已入档;恒无 Nagle 视作平台事实,**不以 NODELAY 作为 OK 条件**。plan.md 的入档由 net-cutover 负责。
## 静态源码检查(先于动态用例)
+2 -2
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Static acceptance checks for App/iec60870/104_port/iec104_net_mcu.c
"""Static acceptance checks for App/iec60870/104/iec104_net_mcu.c
S1: IEC104_Net_Close CLIENT/CLIENT_EOF branch must not contain net_close_sock.
S2: IEC104_Net_Recv rc==0 EOF branch must not call close/drop on the session.
@@ -13,7 +13,7 @@ import re
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
SRC = os.path.join(ROOT, "App", "iec60870", "104_port", "iec104_net_mcu.c")
SRC = os.path.join(ROOT, "App", "iec60870", "104", "iec104_net_mcu.c")
OUT = os.path.join(HERE, "results", "static")
+1 -1
View File
@@ -1,6 +1,6 @@
/*
* IEC104 Net MCU backend isolation contract tests.
* Builds the REAL App/iec60870/104_port/iec104_net_mcu.c against a fake net layer.
* Builds the REAL App/iec60870/104/iec104_net_mcu.c against a fake net layer.
* This is host-only evidence for the Net ABI, not full core/TcpServer or hardware.
*/
#include <stdio.h>
+5 -5
View File
@@ -1,12 +1,12 @@
{
"scope": "REAL backend App/iec60870/104_port/iec104_net_mcu.c + fake net layer; host-only isolation evidence, not full core/TcpServer/hardware",
"scope": "REAL backend App/iec60870/104/iec104_net_mcu.c + fake net layer; host-only isolation evidence, not full core/TcpServer/hardware",
"backend_sha256": "1bcb602946485156ce04b49343ece9525e8ed50f5d5ee0c1d580982e8d77ecd9",
"sources": {
"MATRIX.md": "b17c25dabfc4ca17ec8befddafd420edcceb1bac69a65538c4f06b85c78d48eb",
"MATRIX.md": "67126a0b92a832dd15710c3ce611d111e35f66c94d514d14939fdac7bd0ab314",
"net_socket_fake.h": "c7c712256b5ac4b5162fd445319296dd32ada4abc6efa0384e65e2635bba38c9",
"net_socket_fake.c": "f86ecd73bae57daad7d5d9506a0a8b7158344ba0e01f0e1d16af0ebdbdac3578",
"netmcu_test.c": "65aa96163acf0ea38369f905027b8a3c8a9811bcf99aa5b4c8b874f21ea48ac1",
"check_static.py": "cd071b0a0d804f0e4a9123278a87ed413a4d2d6f1989dfc505689b1e49fdf115"
"netmcu_test.c": "a0b2b3b7719e7e355376ffd49755ebb116bcd072722075c0728c7ba37b2cacb4",
"check_static.py": "21fdcf1f67c959232b7dcdcdf4ec1c86b3ac75d632aabef480303487f7ab5448"
},
"case_count": 13,
"case_count_basis": "13 named cases; N2a/N2b are two cases in one N2 test function",
@@ -39,6 +39,6 @@
"MCU timing/WCET"
],
"known_deviations": [
"NODELAY: App/iec60870/104_port/iec104_net_mcu.h comment; CH395F has no setsockopt, treated as always no-Nagle and NOT an OK condition"
"NODELAY: App/iec60870/104/iec104_net_mcu.h comment; CH395F has no setsockopt, treated as always no-Nagle and NOT an OK condition"
]
}
@@ -1,5 +1,5 @@
{
"source": "D:\\Code\\DTU Code\\STM32F4-Base\\.agents\\worktrees\\dsh-d2fd6422-281a\\App\\iec60870\\104_port\\iec104_net_mcu.c",
"source": "D:\\Code\\DTU Code\\STM32F4-Base\\.agents\\worktrees\\dsh-d2fd6422-281a\\App\\iec60870\\104\\iec104_net_mcu.c",
"source_sha256": "1bcb602946485156ce04b49343ece9525e8ed50f5d5ee0c1d580982e8d77ecd9",
"checks": [
{
+5 -5
View File
@@ -37,13 +37,13 @@ def run_real(cl, inc, lib):
sources = [
os.path.join(HERE, "netmcu_test.c"),
os.path.join(HERE, "net_socket_fake.c"),
os.path.join(ROOT, "App", "iec60870", "104_port", "iec104_net_mcu.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_port"),
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"),
@@ -90,9 +90,9 @@ def main():
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_port/iec104_net_mcu.c + fake net layer; host-only isolation evidence, not full core/TcpServer/hardware",
"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_port", "iec104_net_mcu.c")),
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"]},
@@ -102,7 +102,7 @@ def main():
"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_port/iec104_net_mcu.h comment; CH395F has no setsockopt, treated as always no-Nagle and NOT an OK condition"],
"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)
+1 -1
View File
@@ -16,7 +16,7 @@ BUILD = TestBuild(
include_dirs=[
os.path.join(HERE, "stub"),
os.path.join(ROOT, "App", "task"),
os.path.join(ROOT, "App", "iec60870", "104_port"),
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, "App", "iec60870", "104", "business"),