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 名字的排除口径。
This commit is contained in:
2026-11-18 10:29:18 +08:00
parent 2e9272cc8a
commit 90cc9a7d99
125 changed files with 7463 additions and 5335 deletions
+2 -2
View File
@@ -19,7 +19,7 @@ Read-only reference: `IEC60870-Slave/examples/iec104_dtu_profile.h`, `iec104_exa
Measured status: `ci-artifacts/iec104-tests/tcp-final/report.json` records 11/11 PASS on the frozen `lvgl_sim.exe` SHA256 `d2285bbc68ce4353c814dbcc455a2083b56e81fc54bf88081402e821c6a92833`. Exact GI225 includes parameter value/QPM checks; CI8 uses original default Type206; Read121 checks zero/IV or SB plus CP56 legality; remote345 compares independent original typed defaults. The run includes area changes, preset/read unchanged/commit/read changed/cancel, disconnected stage invalidation, committed RAM retained across TCP reconnect, UInt16777217 precision, unknown/unconfigured addresses, malformed typed values, APDU255, split/coalesced streams, causal SBO state/feedback and rejected-command invariance, full device time and progression, unchanged host clock, and 30-second monotonic SBO expiry. Normal pre-START TESTFR and illegal pre-START I-frame close are distinct assertions. Source/transport/core probes record 10/10 PASS in `transport-final/report.json`; these are separately linked migrated production sources, not old-library binaries.
Remaining unverified: malformed GI CA/IOA/QOI combinations; inbound APDU overlimit close; wrong TLV declared length on product TCP; every one of the 65 command entries and default-disabled extension permissions; nonzero measurement deadband reference stability; Spontaneous negative time_kind; injected Close/Destroy/GetStatus cleanup failures (no supported fixture injection); atomic platform apply rejection after validation; mapped-UShort callback overflow/fraction (no reachable mapped UShort entry). TCP tests terminate the process, so normal finite-deadline shutdown and UI are separately owned simulator evidence. The 11-case TCP suite is not an assertion that all commands/features were exhaustively tested. Diagnostic aliases150 are deliberately excluded by Lead scope decision and must return unknown rather than fabricated zero success.
Remaining unverified: malformed GI CA/IOA/QOI combinations; inbound APDU overlimit close; wrong TLV declared length on product TCP; every one of the 65 command entries and default-disabled extension permissions; nonzero measurement deadband reference stability; Spontaneous negative time_kind; injected Close/Destroy/GetStatus cleanup failures (no supported fixture injection); atomic platform apply rejection after validation; mapped-UShort callback overflow/fraction (no reachable mapped UShort entry). Type 110/111/112 LOAD is now covered (`test_standard_parameter_load_110_111_112`: accept per type, KPA mismatch, type mismatch, unknown IOA/CA/COT, per-group range, NaN, TEST bit, single-GI readback, restore) and Type 113 activation is covered (`test_standard_parameter_activation_113`: QPA=3 activate/deactivate, QPA=1/2 refusal while their config extensions stay off, IOA!=0 refusal, TEST bit, COT 45/46). What remains unverified there: QPA=1/QPA=2 positive paths (require `IEC104_PARAMETER_EXT_QPA_LOADED`/`_OBJECT` to be enabled, which this build does not do), the two-phase PREPARE/COMMIT apply-on-ACK flow, and periodic-scan output after QPA=3 activation. TCP tests terminate the process, so normal finite-deadline shutdown and UI are separately owned simulator evidence. The TCP suite (11 cases in the frozen snapshot above, 13 as of 2026-11-18) is not an assertion that all commands/features were exhaustively tested. Diagnostic aliases150 are deliberately excluded by Lead scope decision and must return unknown rather than fabricated zero success.
Initialization contract frozen by Lead after data/core review: observations are zero with IV; step/bit simulation is zero with SB. Parameters are configuration, not sampled observations: preserve original legal typed defaults and read-only mappings. This is the chosen integration contract, not a quotation of the original user wording. Committed values survive ordinary TCP reconnect in RAM; reconnect discards selections/stages; explicit service initialization restores defaults; no persistence is claimed.
@@ -58,7 +58,7 @@ python -B test/host/iec104_dtu/run_transport_test.py --only vendor_boundary --ou
TCP requires Python standard library and the final same-UI simulator binary; use a distinct `--port` when tests run concurrently. Transport/probes require MSVC x64 and Windows SDK, discovered by existing `test/host/common/unit_test_harness.py::locate_toolchain` (standard VS installation directories, no `vcvars` or `vswhere` invocation). Explicit `DTU_MSVC_CL` and `DTU_WINDOWS_KITS_ROOT` overrides obey that shared harness contract. Compiler path, command, source SHA256 and per-fixture build/run logs are retained. `/W3 /WX /std:c11 /utf-8` are used without warning suppression. Idle upstream fixture has a checked size_t-to-uint32 conversion in its generated artifact copy; report preserves upstream and adapted hashes. Original library and old host tests are not edited. Preserved legacy runners were actually executed: IEC104179/179 and IEC104 SERV37/37 PASS, combined command exit0; summary `ci-artifacts/iec104-tests/legacy-regression.txt` records existing compiler warnings, so this is not a zero-warning legacy-build claim.
The self-test runner only tests the independent decoder/oracle (7 tests), not product acceptance. Existing total host discovery sees `run_unit_test.py`; real TCP and MSVC transport runners are explicit acceptance entrypoints, NOT yet registered as automatic CI coverage gates. New firmware registration/Keil build and existing host coverage gates are separately owned; this file does not claim CI/coverage/dual-target registration passed. No old tests were deleted to hide regressions.
The self-test runner only tests the independent decoder/oracle (7 tests), not product acceptance. Existing total host discovery sees `run_unit_test.py`. As of 2026-11-18 the real TCP runner IS registered as an automatic coverage gate: `.pre-commit-config.yaml` declares hook `iec104-tcp` (`stages: [manual]`, entry = the repo's gate launcher `gate_python.py` followed by `check_iec104_tcp.py`, `pass_filenames: false`), which builds `lvgl_sim.exe` incrementally, runs `run_tcp_test.py`, and writes `ci-artifacts/iec104-tests/tcp-gate/{report.json,wire.json,objects.json,simulator.log}`. Verified by a genuine run: `pre-commit run --all-files --hook-stage manual iec104-tcp` -> `Passed`, wall clock 16.8 s, report recorded 11/11 PASS at that time; as of 2026-11-18 the suite is 13 cases (`test_standard_parameter_load_110_111_112`, `test_standard_parameter_activation_113` added) and a full run gives 13/13, canonical evidence `ci-artifacts/iec104-tests/s5/report.json`. Three limits are stated rather than hidden: (1) it is a manual-stage hook, so `git commit` does not run it - the local sweep is `scripts/ci_local.py` and the remote CI's manual stage; (2) the remote CI (`.gitea/workflows/ci.yml`) does NOT build the simulator, so it deliberately omits this hook from its `$hooks` list - the gate only ever runs on a developer machine that has `simulator/build/Debug/lvgl_sim.exe`; (3) the hook builds through `build_sim.bat`, i.e. VENDOR DEFAULT capacities (RX 2048 / HIGH 1024 / NORMAL 4096 / RECORD 32 / EVENT 68), so it does NOT exercise the P6a tier the firmware actually ships (RX 256 / HIGH 288 / NORMAL 272 / RECORD 4 / EVENT 11); the P6a evidence is a separate build via `ci-artifacts/iec104-cutover/build_sim_p6a.ps1` plus the same runner, and the two must not be quoted for each other. The MSVC transport runner remains an explicit acceptance entrypoint only (recorded in `transport-final/report.json`), not a hook. New firmware registration/Keil build and existing host coverage gates are separately owned; this file does not claim CI/coverage/dual-target registration passed. No old tests were deleted to hide regressions.
Oracle corrections retained with failures: QPM is `{1,3,4,2}` from original enum, not group+1; original CI defaults to Type206, not15; pre-START I-frame is FATAL close per original Link; wrong-IOA EXEC does not clear a prior selection, so reselecting without cancellation invokes duplicate-select rejection. The genuine product Read Type1/3 compatibility defect was separately repaired to Type30/31 CP56 before the final binary. Earlier failing snapshots remain in artifacts.
+299 -58
View File
@@ -5,10 +5,12 @@ import hashlib
import json
import math
from pathlib import Path
import re
import socket
import struct
import subprocess
import tempfile
import threading
import time
import traceback
@@ -241,52 +243,61 @@ class Acceptance:
def position(self):
return tuple(self.read(ioa)["value"] for ioa in (0x1001, 0x0011, 0x0028))
def execute_with_feedback(self, value):
before = self.position()
self.command(value, select=True)
assert self.position() == before, "selection acted"
start = len(self.client.asdus)
self.command(value)
self.client.receive(lambda d: header(d)["type"] == 46 and header(d)["cot"] == 10)
frames = self.client.asdus[start:]
rows = [r for d in frames if header(d)["type"] in (1, 3, 30, 31) for r in objects(d)]
self.decoded.extend(rows)
expected = (value, int(value == 2), int(value == 1))
assert before != expected, "test must request a real state transition"
assert self.position() == expected
for ioa, result in zip((0x1001, 0x0011, 0x0028), expected):
assert any(r["ioa"] == ioa and r["value"] == result and r["quality"] == 32 for r in rows), ("missing simulated feedback", ioa, rows)
def denied(self, value, select=False, ioa=0x6002):
"""A refused command is answered COT 7 with P/N=1 and leaves the state alone."""
data = self.command(value, select=select, ioa=ioa, pn=True)
assert header(data)["cot"] == 7, ("refusal must echo COT 7", data.hex())
return data
def test_sbo_order_feedback_and_reconnect(self):
def expect_no_completion(self, seconds=0.3):
"""A refused command must not be followed by ACT_TERM (COT 10) nor return info (COT 11)."""
start = len(self.client.asdus)
deadline = time.monotonic() + seconds
try:
while time.monotonic() < deadline:
self.client.frame(deadline)
except (socket.timeout, TimeoutError):
pass
for data in self.client.asdus[start:]:
h = header(data)
assert not (h["type"] == 46 and h["cot"] == 10), ("unexpected ACT_TERM", data.hex())
assert h["cot"] != 11, ("unexpected return info", data.hex())
def test_sbo_order_and_refusal_semantics(self):
"""Every command is refused in the skeleton build: COT 7 + P/N=1, state untouched.
The DTU command table still declares SELECT_EXECUTE points and the vendor module
still enforces SBO ordering and address checking; those paths are exercised here
through the refusal branch (COT 46 for a wrong common address, P/N=1 everywhere),
but no command may ever reach COT 10 (ACT_TERM) or COT 11 (return info).
"""
initial = self.position()
self.command(2, pn=True)
self.denied(2)
assert self.position() == initial
self.command(2, select=True)
self.command(1, pn=True)
self.denied(2, select=True)
assert self.position() == initial
self.command(2, select=True)
self.command(2, ioa=0x6004, pn=True)
self.denied(1)
assert self.position() == initial
self.connect() # Wrong-IOA EXEC does not cancel the original selection.
self.execute_with_feedback(1 if initial[0] == 2 else 2)
after = self.position()
self.command(after[0], pn=True)
assert self.position() == after
opposite = 1 if after[0] == 2 else 2
self.command(opposite, select=True)
self.connect()
self.command(opposite, pn=True)
assert self.position() == after, "old connection selection survived"
self.client.request(asdu(46, u24(0x6002) + bytes((opposite | 128,)), ca=2))
self.connect() # Reconnect must not turn a refused command into an accepted one.
self.denied(1, select=True)
self.denied(1)
assert self.position() == initial
# A wrong common address is still rejected by the command module as UNKNOWN_IOA.
self.client.request(asdu(46, u24(0x6002) + bytes((2 | 128,)), ca=2))
reply, _ = self.client.receive(lambda d: header(d)["type"] == 46)
assert header(reply)["cot"] == 46 and header(reply)["pn"]
self.command(opposite, pn=True)
assert self.position() == after, "wrong CA acted or selected"
self.execute_with_feedback(opposite)
# Direct-execute step and setpoint points take the same refusal path.
self.denied(1, ioa=0x6101)
self.denied(1, ioa=0x6301)
assert self.position() == initial
self.expect_no_completion()
self.denied(2)
self.expect_no_completion()
def test_clock_is_device_only_and_sbo_is_monotonic(self):
def test_clock_is_device_only_and_no_command_completes(self):
host_delta = time.time() - time.monotonic()
self.command(2, select=True)
initial = self.position()
self.denied(2, select=True)
# Year2035 is deliberately far from the host clock; no OS clock API is used.
stamp = struct.pack("<HBBBBB", 12345, 22, 11, 17, 6, 35)
self.client.request(asdu(103, u24(0) + stamp))
@@ -302,13 +313,229 @@ class Acceptance:
later = cp56(self.client.response(103, 5)[-7:])
assert 0.1 <= (datetime.datetime.fromisoformat(later["iso"]) - datetime.datetime.fromisoformat(actual_time["iso"])).total_seconds() < 2
assert abs((time.time() - time.monotonic()) - host_delta) < 1.0, "host wall clock changed"
# A far-future device clock must not prematurely expire the selection.
self.command(2)
self.client.receive(lambda d: header(d)["type"] == 46 and header(d)["cot"] == 10)
self.command(1, select=True)
# A far-future device clock must not expire anything into a completion.
self.denied(2)
self.denied(1, select=True)
time.sleep(self.args.sbo_timeout + 0.3)
self.command(1, pn=True)
assert self.read(0x1001)["value"] == 2, "expired command changed state"
self.denied(1)
self.expect_no_completion()
assert self.position() == initial, "refused command changed state"
# ---- Type 110/111/112/113 standard parameter path -------------------------
#
# This is the IEC104_Parameter module, NOT IEC104_RemoteParameter (Types
# 200..203, covered above). The two are separate vendor modules with
# different wire layouts and different acceptance rules:
# 110 normalized value = IOA(3) + int16(2) + QPM(1)
# 111 scaled value = IOA(3) + int16(2) + QPM(1)
# 112 short float = IOA(3) + float32(4) + QPM(1)
# 113 activation = IOA(3) + QPA(1)
# QPM's low six bits select the parameter kind (KPA), which must match the
# point table row; that is what makes "wrong KPA" a per-row rejection rather
# than a global one.
def standard_params(self):
"""GI snapshot restricted to standard parameter objects (type >= 100).
Cost note (measured 2026-11-18, must be respected when editing this test):
one full GI here is ~126 I-frames / ~3.9 s wall clock, because the 120
standard parameter rows cannot be coalesced - they span types 110/111/112
and each ASDU carries exactly one object. Call this only as often as the
assertion genuinely needs; every extra call costs ~4 s in the manual gate.
"""
self.client.request(asdu(100, u24(0) + b"\x14"))
self.client.response(100, 7)
rows = []
while True:
data, _ = self.client.receive(lambda d: header(d)["cot"] in (20, 10))
if header(data)["type"] == 100:
assert header(data)["cot"] == 10
break
rows.extend(objects(data))
self.decoded.extend(rows)
return {row["ioa"]: row for row in rows if row["type"] >= 100}
def param_load(self, type_id, ioa, value, kpa, test=False, cot=6):
payload = struct.pack("<f", value) if type_id == 112 else struct.pack("<h", value)
self.client.request(asdu(type_id, u24(ioa) + payload + bytes((kpa,)),
cot=cot | (0x80 if test else 0)))
data, _ = self.client.receive(lambda d: header(d)["type"] == type_id)
return data, header(data)
def param_activate(self, ioa, qpa, test=False, cot=6):
self.client.request(asdu(113, u24(ioa) + bytes((qpa,)), cot=cot | (0x80 if test else 0)))
data, _ = self.client.receive(lambda d: header(d)["type"] == 113)
return data, header(data)
def test_standard_parameter_load_110_111_112(self):
# Point table geometry (iec104_dtu_table.c / iec104_source.c):
# group 0 (0x8601..): kind THRESHOLD(1); index>=4 is type 110, else 111
# group 1 (0x8621..): kind LOW_LIMIT(3), type 111
# group 2 (0x8641..): kind HIGH_LIMIT(4), type 111
# group 3 (0x8661..): kind SMOOTHING(2), type 112
before = self.standard_params()
assert before[0x8601]["type"] == 111 and before[0x8601]["value"] == 200
assert before[0x8605]["type"] == 110 and before[0x8605]["value"] == 655
assert before[0x8661]["type"] == 112 and before[0x8661]["value"] == 0.0
# --- accepted LOADs, one per type -------------------------------------
data, h = self.param_load(110, 0x8605, 700, 1)
assert h["cot"] == 7 and not h["pn"], ("110 LOAD must be accepted", data.hex())
assert objects(data)[0]["value"] == 700, "positive CON must carry the stored value"
data, h = self.param_load(111, 0x8621, -9999, 3)
assert h["cot"] == 7 and not h["pn"], ("111 LOAD must be accepted", data.hex())
data, h = self.param_load(112, 0x8661, 12.5, 2)
assert h["cot"] == 7 and not h["pn"], ("112 LOAD must be accepted", data.hex())
# --- rejection: unknown IOA / unknown CA / unknown COT ----------------
# These use the standard error causes and echo the request bytes verbatim,
# because no point row matched so no running value could be frozen.
data, h = self.param_load(111, 0x9999, -5, 1)
assert h["cot"] == 47 and h["pn"], ("unknown IOA -> COT 47", data.hex())
data, h = self.param_load(111, 0x8601, -5, 1, cot=3)
assert h["cot"] == 45 and h["pn"], ("unknown COT -> COT 45", data.hex())
# --- rejection: KPA (kind) must match the row -------------------------
data, h = self.param_load(111, 0x8601, -5, 3)
assert h["cot"] == 7 and h["pn"], ("kind mismatch must be a negative CON", data.hex())
assert objects(data)[0]["value"] == -5, \
"address matched but kind did not, so no running value was frozen: reply echoes the request"
# --- rejection: type id must match the row ----------------------------
data, h = self.param_load(110, 0x8601, -5, 1)
assert h["cot"] == 7 and h["pn"], ("type mismatch must be a negative CON", data.hex())
assert objects(data)[0]["value"] == -5, "type mismatch likewise echoes the request"
# --- rejection: range/content per group -------------------------------
# group 0 requires a non-negative value, so -1 is refused on a 110 row.
# NOTE the contrast with the two cases above: the row DID match here, so
# the module froze the running value and the negative CON carries 700
# (the current value), not the refused request value. Getting this
# backwards is the easiest way to write a test that passes for the wrong
# reason, so both shapes are asserted explicitly.
data, h = self.param_load(110, 0x8605, -1, 1)
assert h["cot"] == 7 and h["pn"], ("negative on unsigned-style row must be refused", data.hex())
assert objects(data)[0]["value"] == 700, \
"row matched so the running value is frozen: negative CON carries the CURRENT value, not the request"
data, h = self.param_load(111, 0x8621, -10001, 3) # below LOW_LIMIT's own floor
assert h["cot"] == 7 and h["pn"], "below low limit must be refused"
data, h = self.param_load(111, 0x8641, 10001, 4) # above HIGH_LIMIT's own ceiling
assert h["cot"] == 7 and h["pn"], "above high limit must be refused"
data, h = self.param_load(112, 0x8661, 61.0, 2) # smoothing is 0..60 s
assert h["cot"] == 7 and h["pn"], "float above 60 must be refused"
data, h = self.param_load(112, 0x8661, float("nan"), 2)
assert h["cot"] == 7 and h["pn"], "NaN must be refused before it can poison the store"
# --- TEST bit: accepted, but must not mutate --------------------------
data, h = self.param_load(111, 0x8621, -100, 3, test=True)
assert h["cot"] == 7 and not h["pn"], ("TEST LOAD must be accepted", data.hex())
# --- ONE readback verifies everything above at once -------------------
# Single GI, deliberately: each call costs ~4 s (see standard_params).
# It must show the three accepted values, prove the six refusals and the
# TEST request moved nothing, and prove an untouched row stayed put.
after = self.standard_params()
assert after[0x8605]["value"] == 700, "110 LOAD did not reach the value store"
assert after[0x8621]["value"] == -9999, "111 LOAD did not reach the value store, or a refusal/TEST leaked"
assert after[0x8661]["value"] == 12.5, "112 LOAD did not reach the value store, or a refusal leaked"
assert after[0x8601]["value"] == 200, "untouched row must not move"
assert after[0x8641]["value"] == 10000, "high-limit row was mutated by a refusal"
assert after[0x8622]["value"] == -10000, "TEST LOAD mutated a sibling row in its group"
# --- restore so the run is repeatable ---------------------------------
for type_id, ioa, value, kpa in ((110, 0x8605, 655, 1), (111, 0x8621, -10000, 3),
(112, 0x8661, 0.0, 2)):
data, h = self.param_load(type_id, ioa, value, kpa)
assert h["cot"] == 7 and not h["pn"], ("restore failed", data.hex())
# A second GI is the only way to prove the restore actually landed; it is
# worth the ~4 s because a silent restore failure would poison every later
# GI-based test in this suite.
assert self.standard_params() == before, "restore did not return the exact initial snapshot"
def test_standard_parameter_activation_113(self):
# QPA selects the activation flavour. Only QPA=3 (cyclic transmission) is
# enabled by default; QPA=1/2 are gated behind config extensions that this
# build does not turn on, so they must be refused rather than silently
# accepted. This distinction is the point of the test.
data, h = self.param_activate(0, 3)
assert h["cot"] == 7 and not h["pn"], ("QPA=3 activate must be accepted", data.hex())
data, h = self.param_activate(0, 3, cot=8)
assert h["cot"] == 9 and not h["pn"], ("QPA=3 deactivate must be confirmed with COT 9", data.hex())
# A non-zero IOA with QPA=3 reaches the application, which refuses it
# because periodic transmission is addressed by IOA 0 only.
data, h = self.param_activate(0x1234, 3)
assert h["cot"] == 7 and h["pn"], ("QPA=3 with IOA!=0 must be refused", data.hex())
for qpa in (1, 2):
data, h = self.param_activate(0, qpa)
assert h["cot"] == 7 and h["pn"], \
("QPA=%d must be refused: its config extension is off" % qpa, data.hex())
# TEST bit is accepted and must not alter the schedule.
data, h = self.param_activate(0, 3, test=True)
assert h["cot"] == 7 and not h["pn"], ("TEST activate must be accepted", data.hex())
# Error causes.
data, h = self.param_activate(0, 3, cot=3)
assert h["cot"] == 45 and h["pn"], ("unknown COT -> COT 45", data.hex())
self.client.request(asdu(113, u24(0) + bytes((3,)), ca=9))
data, _ = self.client.receive(lambda d: header(d)["type"] == 113)
h = header(data)
assert h["cot"] == 46 and h["pn"], ("unknown CA -> COT 46", data.hex())
# Leave the periodic scan off so it cannot perturb anything after us.
data, h = self.param_activate(0, 3, cot=8)
assert h["cot"] == 9 and not h["pn"], ("final deactivate failed", data.hex())
READY_PATTERN = re.compile(r"^\[iec104\] READY address=(?P<address>\S+) port=(?P<port>\d+) ca=(?P<ca>\d+) mode=(?P<mode>\S+)\s*$")
class ReadyWatcher:
"""转发模拟器 stdout 到日志文件,并从中取回 READY 行里的实际监听端口。
为什么不靠轮询端口探活(2026-11-18 改):
1) 探活的每一次 connect 都会被固件当成一次真实接入,白白吃掉一次 accept,
让 STOP 摘要里的 connections 多算一次,也让单连接模型的会话序列多一段;
2) 端口固定时探活"成功"只说明有人在这个号上监听,并行构建或端口被别的进程
占住时会把真正的失败伪装成通过;
3) READY 行里的 port 是固件 bind 成功后回报的 bound_port,--iec104-port 0 时
这正是唯一能拿到实际端口的途径(simulator/README.md 明确要求读它)。
日志必须保留:它带着 START/READY/STOP 摘要,是人工复核的证据面。
"""
def __init__(self, log):
self.log = log
self.ready = threading.Event()
self.port = None
self.ca = None
self.address = None
self.mode = None
self.error = None
self._thread = threading.Thread(target=self._pump, name="sim-stdout", daemon=True)
def start(self, stream):
self._stream = stream
self._thread.start()
def _pump(self):
try:
for line in self._stream:
self.log.write(line)
self.log.flush()
match = READY_PATTERN.match(line)
if match and not self.ready.is_set():
self.port = int(match.group("port"))
self.ca = int(match.group("ca"))
self.address = match.group("address")
self.mode = match.group("mode")
self.ready.set()
except Exception as exc: # 读管道失败也要让主线程拿到结论
self.error = exc
self.ready.set()
def join(self, timeout=5.0):
self._thread.join(timeout)
def main():
@@ -316,13 +543,18 @@ def main():
parser.add_argument("--exe", type=Path)
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=24040)
parser.add_argument("--sbo-timeout", type=float, default=30)
# 这里是"等一个不可能发生的 vendor 超时"的窗口,不是真实超时值。
# 所有命令都被否定确认(COT 7 + P/N=1),库里根本不会进入选择/执行事务,
# 因此 select(30s)/decision(15s)/execute(60s) 三个计时器都不会被启动。
# 保留 2s 只是给"迟到的 ACT_TERM"留一个可观测窗口;若将来命令被受理,
# 必须调回 30 或更大,否则这条用例会在超时真正到期前就宣告通过(假绿)。
parser.add_argument("--sbo-timeout", type=float, default=2)
parser.add_argument("--output", type=Path, default=ROOT / "ci-artifacts" / "iec104-tests")
parser.add_argument("--only", default="")
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
suite = Acceptance(args)
process, log = None, None
process, log, watcher = None, None, None
binary = {}
with tempfile.TemporaryDirectory(prefix="iec104-dtu-") as cwd:
try:
@@ -330,19 +562,22 @@ def main():
exe = args.exe.resolve()
binary = {"path": str(exe), "sha256": hashlib.sha256(exe.read_bytes()).hexdigest()}
log = (args.output / "simulator.log").open("w", encoding="utf-8")
process = subprocess.Popen([str(exe), "--iec104", "--iec104-headless", "--iec104-port", str(args.port), "--iec104-duration", "180"], cwd=cwd, stdout=log, stderr=subprocess.STDOUT)
deadline = time.monotonic() + 15
while True:
if process.poll() is not None:
raise RuntimeError("simulator exited before listen: " + str(process.returncode))
try:
with socket.create_connection((args.host, args.port), 0.2):
break
except OSError:
if time.monotonic() >= deadline:
raise TimeoutError("simulator did not listen")
time.sleep(0.05)
names = ["test_link_start_stop_testfr_and_prestart", "test_gi_exact_set_and_zero_observations", "test_counter_exact_set_zero_and_invalid", "test_read_all_runtime_addresses", "test_remote_complete_typed_snapshot", "test_area_switch_and_readback", "test_typed_preset_commit_cancel_readback", "test_unknown_and_bad_typed_requests", "test_maximum_apdu_and_coalesced_frames", "test_sbo_order_feedback_and_reconnect", "test_clock_is_device_only_and_sbo_is_monotonic"]
process = subprocess.Popen(
[str(exe), "--iec104", "--iec104-headless", "--iec104-port", str(args.port), "--iec104-duration", "180"],
cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace", bufsize=1)
watcher = ReadyWatcher(log)
watcher.start(process.stdout)
if not watcher.ready.wait(15.0):
raise TimeoutError("simulator did not print READY within 15s")
if watcher.error is not None:
raise RuntimeError("simulator stdout plumbing failed: " + str(watcher.error))
if watcher.port is None:
raise RuntimeError("simulator exited before listen: " + str(process.poll()))
# 用固件回报的 bound_port 连接,而不是我们请求的那个号:--iec104-port 0 时
# 请求号无意义,且这样连等于断言"READY 里说的端口真的在听"。
args.port = watcher.port
binary["ready"] = {"address": watcher.address, "port": watcher.port, "ca": watcher.ca, "mode": watcher.mode}
names = ["test_link_start_stop_testfr_and_prestart", "test_gi_exact_set_and_zero_observations", "test_counter_exact_set_zero_and_invalid", "test_read_all_runtime_addresses", "test_remote_complete_typed_snapshot", "test_area_switch_and_readback", "test_typed_preset_commit_cancel_readback", "test_unknown_and_bad_typed_requests", "test_standard_parameter_load_110_111_112", "test_standard_parameter_activation_113", "test_maximum_apdu_and_coalesced_frames", "test_sbo_order_and_refusal_semantics", "test_clock_is_device_only_and_no_command_completes"]
for name in names:
if not args.only or args.only in name:
suite.run(name, getattr(suite, name))
@@ -350,6 +585,12 @@ def main():
if process and process.poll() is None:
process.terminate()
process.wait(timeout=10)
# 先收干 stdout 再关日志:进程被 terminate 后管道里可能还有最后几行
# (含 STOP 摘要),丢了就等于丢掉唯一的人工复核证据。
if watcher:
watcher.join(5.0)
if process and process.stdout:
process.stdout.close()
if log:
log.close()
report = {"created": datetime.datetime.now(datetime.timezone.utc).isoformat(), "binary": binary, "reference": suite.oracle, "results": suite.results,