Files
STM32F4-Base/test/host/iec104_netmcu/check_static.py
T
wandering1 c0cef2fca0 refactor(app): 去掉 App/protocol 层,104 迁入 App/iec60870/104_port
IEC 60870 是标准族(含 104/101),原 App/protocol/ 把它拆成
iec60870/(vendor 核心)+ iec104/(我方适配)两棵同级树,多一层且
看不出「104 只维护一个」。现按标准族收口:

  App/protocol/iec60870        -> App/iec60870
  App/protocol/iec104          -> App/iec60870/104_port
  App/protocol/{README.md,.gitkeep}  删除
  App/protocol/iec104/iec104_asdu.h  删除(零 include / 零注册的孤儿)

vendor 子树(104/ common/ examples/)逐字节未改:91 个 R100 重命名、
git diff --cached -M 后 0 insertions / 0 deletions,另按字节直比 89/89
一致;UPSTREAM.sha256 差异集仍等于既有 17 项受控差异,非篡改。
适配层只改注释里的路径字样,无逻辑行。

门禁配套:
- coding-standard 的 exclude 不能写祖先 App/iec60870/——rename_ref 机械
  改出的祖先形态会把 2,191 行适配层一起豁免掉。改为三条精确前缀
  common/ 104/ examples/,适配层 11/11 回到受检;vendor 直调脚本仍报
  88 处违规而钩子 Skipped,证明确实由 exclude 生效而非空跑。
- check_registration 的 KNOWN_GAPS 键与理由文本同步到新路径。

验证:Keil 0 errors 0 warnings(Code=367464 RO-data=48492
RW-data=2288 ZI-data=173544),构建日志与 .d 依赖证明 5 个适配 .o
真被编入;模拟器 0 警告并跑通 iec104-tcp 13/13 真实 TCP 回环;
pre-commit 25 Passed / 0 Failed;doc-links 30 处问题全在
IEC60870-Slave/ 供应商快照,我方文档零新增。

顺带删掉仓根误入库且零引用的 _all_hook.txt 与 _scope.txt。
2026-11-18 14:15:10 +08:00

115 lines
4.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Static acceptance checks for App/iec60870/104_port/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.
Both checks use brace matching from the exact branch header, not line guesses.
"""
import hashlib
import json
import os
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")
OUT = os.path.join(HERE, "results", "static")
def sha256(path):
h = hashlib.sha256()
with open(path, "rb") as fh:
h.update(fh.read())
return h.hexdigest()
def brace_span(text, brace_pos):
assert text[brace_pos] == "{", "not a brace"
depth = 0
i = brace_pos
while i < len(text):
c = text[i]
if c == "{":
depth += 1
elif c == "}":
depth -= 1
if depth == 0:
return brace_pos, i + 1
i += 1
raise ValueError("unbalanced brace")
def func_body(text, name):
m = re.search(r"\b" + re.escape(name) + r"\s*\([^;{]*\)\s*\{", text)
if m is None:
raise ValueError("function not found: " + name)
start = text.index("{", m.start())
return brace_span(text, start)
def branch_body(text, span, needle):
a, b = span
pos = text.find(needle, a, b)
if pos < 0:
raise ValueError("branch not found: " + needle)
brace = text.index("{", pos)
assert brace < b
return brace_span(text, brace)
def main(src_path=None, report_path=None):
src_path = src_path or SRC
report_path = report_path or OUT
os.makedirs(report_path, exist_ok=True)
with open(src_path, "r", encoding="utf-8") as fh:
text = fh.read()
checks = []
close_span = func_body(text, "IEC104_Net_Close")
client_branch = branch_body(text, close_span,
"if (slot->role == SLOT_CLIENT || slot->role == SLOT_CLIENT_EOF)")
body = text[client_branch[0]:client_branch[1]]
checks.append({
"id": "S1",
"description": "Close CLIENT/CLIENT_EOF branch contains no net_close_sock",
"passed": "net_close_sock" not in body,
"source_lines": [text.count("\n", 0, client_branch[0]) + 1,
text.count("\n", 0, client_branch[1]) + 1],
"evidence": "\n".join(text[client_branch[0]:client_branch[1]].splitlines()[:3]),
})
recv_span = func_body(text, "IEC104_Net_Recv")
eof_branch = branch_body(text, recv_span, "if (rc == 0)")
eof_body = text[eof_branch[0]:eof_branch[1]]
forbidden = [tok for tok in ["net_close_sock", "net_drop_conn_locked",
"net_close(", "net_drop_conn("] if tok in eof_body]
checks.append({
"id": "S2",
"description": "Recv EOF branch only changes role; no close/drop call",
"passed": not forbidden,
"source_lines": [text.count("\n", 0, eof_branch[0]) + 1,
text.count("\n", 0, eof_branch[1]) + 1],
"evidence": eof_body.strip(),
})
report = {
"source": src_path,
"source_sha256": sha256(src_path),
"checks": checks,
"passed": sum(1 for c in checks if c["passed"]),
"total": len(checks),
}
with open(os.path.join(report_path, "report.json"), "w", encoding="utf-8") as fh:
json.dump(report, fh, ensure_ascii=False, indent=2)
print("static source sha256={}".format(report["source_sha256"]))
for c in checks:
print("{}: {} {}-{} passed={}".format(c["id"], c["description"],
c["source_lines"][0], c["source_lines"][1],
c["passed"]))
if not c["passed"]:
print(" evidence: {}".format(c["evidence"].replace("\n", " | ")))
print("static passed {}/{}".format(report["passed"], report["total"]))
return 0 if report["passed"] == report["total"] else 1
if __name__ == "__main__":
raise SystemExit(main())