b584601174
技能主文件只留默认路径、成功输出样例、失败索引表与禁令;长尾规程逐字 搬进各技能 reference/,索引键与 reference 小节标题一一对应。 拆分结果(改前 -> 改后,字节): - keil-build 21779 -> 5837,新建 3 篇 reference - task-worktree 24163 -> 11448,新建 4 篇 reference,A-G/C2 节号保号 - draw-diagram 18907 -> 6496,新建 5 篇 reference - mem-budget 13046 -> 8020,新建 4 篇 reference - id-family-semantic-migration 11509 -> 8077,新建 2 篇 reference - stale-doc-count-sweep 8156 -> 6371,新建 1 篇 reference - serial-boot-test 8464 -> 6663,新建 2 篇 reference - board-ui-nav 5350 -> 4364,新建 1 篇 reference 其余 9 个技能补真实成功输出样例(判据串取自本轮实跑或文档原文)。 check_skill_size.py rc=0,17 个主文件无悬空指针。 IEC104 探针入库:ci-artifacts/ 里可复用的三个验收脚本迁入 test/host/iec104_dtu/(纯标准库、argparse、ASCII 输出、exit 0/1/2), 协议逻辑逐字保留,含三处探针自身缺陷修复(GI 漏 3 字节 IOA、I 帧 过滤判反、MEAS 键用 source_addr 而非 IOA)。一次性设计度量按 PROBE_INDEX.md 登记不迁入,运行方法见 PROBE_RUNBOOK.md。
165 lines
6.6 KiB
Python
165 lines
6.6 KiB
Python
"""Board-side IEC 60870-5-104 acceptance probe (read-only, no state changes).
|
||
|
||
Run this when a real DTU board at 192.168.31.100:2404 must be re-accepted after
|
||
a firmware or point-table change. It performs STARTDT, a station general
|
||
interrogation, checks every mapped measurement and DTU signal is reported, then
|
||
proves command refusal and clock confirmation.
|
||
Prerequisites: the board is powered, its 104 listener is reachable, and the
|
||
point table matches iec104_dtu_profile.h. An unreachable board, a non-104
|
||
listener or bad frame sequence exits 2; a missing point or an unexpected
|
||
command reply exits 1.
|
||
"""
|
||
import argparse
|
||
import socket
|
||
import struct
|
||
import sys
|
||
import time
|
||
from pathlib import Path
|
||
|
||
HERE = Path(__file__).resolve().parent
|
||
sys.path.insert(0, str(HERE))
|
||
import wire # noqa: E402
|
||
|
||
HOST = "192.168.31.100"
|
||
PORT = 2404
|
||
|
||
# IOA -> label, from the DTU profile table (iec104_dtu_profile.h).
|
||
# NOTE: s_meas_map[] in iec104_serv.c is keyed by source address (0x9605 etc.),
|
||
# NOT by IOA. The wire carries IOA, so the probe must look up 0x400C.. here.
|
||
MEAS = {
|
||
0x400C: "UA", 0x400D: "UB", 0x400E: "UC", 0x400F: "UN",
|
||
0x4015: "IA", 0x4016: "IB", 0x4017: "IC", 0x4018: "IN",
|
||
0x4007: "freq", 0x4003: "temp",
|
||
}
|
||
YX_SP = 0x0001 # singles start here, 59 points
|
||
YX_DP = 0x1001 # doubles start here, 16 points
|
||
|
||
|
||
def parse_args():
|
||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||
parser.add_argument("--host", default=HOST, help="board IPv4 address")
|
||
parser.add_argument("--port", type=int, default=PORT, help="board IEC104 TCP port")
|
||
return parser.parse_args()
|
||
|
||
|
||
def main(host, port):
|
||
problems = []
|
||
gi_done = False
|
||
trace = []
|
||
t0 = time.monotonic()
|
||
c = wire.Client(host, port, trace, timeout=8)
|
||
print("connected to %s:%d in %.0f ms" % (host, port, (time.monotonic() - t0) * 1000))
|
||
c.start()
|
||
print("STARTDT confirmed (%.0f ms)" % ((time.monotonic() - t0) * 1000))
|
||
|
||
# General interrogation, QOI=20 (station), COT=6 ACT.
|
||
# Type 100 的载荷是 IOA(3) + QOI(1):漏掉 IOA 会让 ASDU 只有 1 字节,
|
||
# vendor IEC60870_Parse_ASDU_Header 判载荷不足 -> FRAME_ERROR -> 断链。
|
||
c.request(wire.asdu(100, wire.u24(0) + bytes((20,)), cot=6))
|
||
end = time.monotonic() + 25
|
||
seen = {}
|
||
while time.monotonic() < end:
|
||
try:
|
||
frame = c.frame(time.monotonic() + 2.0)
|
||
except (TimeoutError, socket.timeout):
|
||
break
|
||
if frame[2] & 1:
|
||
continue # S/U 帧:control 域最低位为 1;只有最低位为 0 才是 I 帧
|
||
data = frame[6:]
|
||
h = wire.header(data)
|
||
if h["type"] == 100 and h["cot"] == 10:
|
||
print("GI activation terminated")
|
||
gi_done = True
|
||
break
|
||
if h["type"] not in (1, 3, 9, 13, 30, 31, 34, 35, 36, 37):
|
||
continue
|
||
try:
|
||
for row in wire.objects(data):
|
||
seen.setdefault(row["ioa"], row)
|
||
except AssertionError as exc:
|
||
print(" decode issue:", exc)
|
||
problems.append("decode issue")
|
||
if not gi_done:
|
||
problems.append("GI not terminated")
|
||
|
||
print("\n=== measurements (mapped by iec104_serv.c s_meas_map) ===")
|
||
for ioa, name in sorted(MEAS.items()):
|
||
row = seen.get(ioa)
|
||
if row is None:
|
||
print(" %-5s IOA 0x%04X : ABSENT" % (name, ioa))
|
||
problems.append("measurement absent %s" % name)
|
||
else:
|
||
iv = "IV" if row.get("quality", 0) & 0x80 else "valid"
|
||
if row["type"] in (13, 36, 112, 206, 207):
|
||
shown = "%.4f" % row["value"]
|
||
else:
|
||
shown = str(row["value"])
|
||
print(" %-5s IOA 0x%04X : type=%d value=%-10s quality=0x%02X %s"
|
||
% (name, ioa, row["type"], shown, row.get("quality", 0), iv))
|
||
|
||
print("\n=== DTU signals 59 SP + 16 DP ===")
|
||
sp_present = sum(1 for i in range(YX_SP, YX_SP + 59) if i in seen)
|
||
dp_present = sum(1 for i in range(YX_DP, YX_DP + 16) if i in seen)
|
||
print(" SP 0x%04X..0x%04X present %d/59" % (YX_SP, YX_SP + 58, sp_present))
|
||
print(" DP 0x%04X..0x%04X present %d/16" % (YX_DP, YX_DP + 15, dp_present))
|
||
if sp_present != 59:
|
||
problems.append("SP present %d/59" % sp_present)
|
||
if dp_present != 16:
|
||
problems.append("DP present %d/16" % dp_present)
|
||
iv_sp = sum(1 for i in range(YX_SP, YX_SP + 59)
|
||
if i in seen and seen[i].get("quality", 0) & 0x80)
|
||
iv_dp = sum(1 for i in range(YX_DP, YX_DP + 16)
|
||
if i in seen and seen[i].get("quality", 0) & 0x80)
|
||
print(" SP carrying IV: %d, DP carrying IV: %d" % (iv_sp, iv_dp))
|
||
|
||
print("\n=== totals ===")
|
||
by_type = {}
|
||
for row in seen.values():
|
||
by_type[row["type"]] = by_type.get(row["type"], 0) + 1
|
||
print(" distinct IOA reported:", len(seen), " by type:", dict(sorted(by_type.items())))
|
||
print(" frames: tx=%d rx=%d" % (sum(1 for x in trace if x["direction"] == "tx"),
|
||
sum(1 for x in trace if x["direction"] == "rx")))
|
||
|
||
# Negative-path probes: commands must be refused, never confirmed.
|
||
print("\n=== command refusal (must not be confirmed) ===")
|
||
payload = wire.u24(0x6001) + bytes((0x01, 0x00))
|
||
c.request(wire.asdu(45, payload, cot=6))
|
||
try:
|
||
data = c.response(45, cot=7, pn=True)
|
||
print(" C_SC_NA_1 0x6001 -> COT 7 P/N=1 refusal (correct)")
|
||
except (TimeoutError, AssertionError) as exc:
|
||
print(" C_SC_NA_1 0x6001 -> UNEXPECTED:", exc)
|
||
problems.append("C_SC refusal unexpected")
|
||
|
||
print("\n=== clock synchronisation (C_CS_NA_1) ===")
|
||
now = time.localtime()
|
||
cp56 = struct.pack("<HBBBBB", now.tm_sec * 1000 + now.tm_min * 0, now.tm_min,
|
||
now.tm_hour, now.tm_mday, now.tm_mon, now.tm_year - 2000)
|
||
# Type 103 的载荷顺序 = IOA(3) + CP56Time2a(7):IOA 在**前**。
|
||
# 写成 CP56 在前会让 vendor 把时标头 3 字节当 IOA 解析,回 COT 47(未知 IOA)。
|
||
c.request(wire.asdu(103, wire.u24(0) + cp56, cot=6))
|
||
try:
|
||
data = c.response(103, cot=7)
|
||
print(" C_CS_NA_1 -> COT 7 confirmed:", data.hex())
|
||
except (TimeoutError, AssertionError) as exc:
|
||
print(" C_CS_NA_1 -> UNEXPECTED:", exc)
|
||
problems.append("C_CS confirmation unexpected")
|
||
|
||
c.close()
|
||
|
||
print("\n=== verdict ===")
|
||
print("FAILURES: %d" % len(problems))
|
||
for item in problems:
|
||
print(" " + item)
|
||
return 1 if problems else 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
parsed = parse_args()
|
||
try:
|
||
rc = main(parsed.host, parsed.port)
|
||
except (OSError, socket.timeout, TimeoutError, AssertionError) as exc:
|
||
print("PROTOCOL_ERROR %s" % type(exc).__name__)
|
||
rc = 2
|
||
raise SystemExit(rc)
|