Files
STM32F4-Base/test/host/iec104_dtu/oracle.py
T
wandering1 7d1db02164 perf(iec104): 参数区按 17 项真实消费者收窄并接上真实定值模型
104 协议侧参数通道此前镜像了厂商点表的全部 430 条参数(697 槽),
但其中 413 条在本设备上没有任何消费者——104 与设备侧真实定值模型
prot_set 之间零调用,主站改定值只落在无人读的 RAM 镜像里,保护功能
毫无感知。本次按"严格按消费者收窄"口径收口:

可见性以 iec104_dtu_protection_mapping() 语义桥表为唯一判据,只放行
三段过流投退/定值/延时(9 项)、低压与过压定值/延时(4 项)、PT 与
CT 一次/二次额定(4 项)。其余参数在位表枚举层即不可见,主站读取
得到 COT=47 + P/N=1 的显式否定确认,不静默丢弃。values[697]→values[53]、
staged[264]→staged[17],并删除生成式 iec104_source_ranks.h。

桥表接真实源:iec104_serv 的 parameter_read/apply 从 NULL 改为直读
prot_set 与整批原子提交,Q10 量化后一次事务落地,持久化仍由
defaultTask 的脏标志闭环负责。

prot_set 因此出现两个运行时写者(netTask 的 104 与 uiTask 的本地整
定),原"写者靠启动时序串行"的约定失效。加入写事务互斥与 seqlock
序号:四个写入口全部进锁,校验与提前返回一律在 enter 之前;序号卡
奇数连续 8 拍即判保护闭锁,避免自检被永久跳过而静默失去防篡改能力。
锁的安装在 defaultTask 的存储 bring-up 阶段,不在 app_main_init——
后者早于 osKernelInitialize,内核启动前禁止任何 RTOS 创建类 API。

实测:RW_IRAM1 117,304→113,720 B(−3,584 B,89.50%→86.76%);
ER_IROM1 413,392→411,668 B(−1,724 B);iec104_source.o 5,656→2,056 B;
prot_set.o 344→352 B(+8 B,写事务序号与锁句柄)。Keil 构建 0 错误
0 警告;主机侧 prot_set 175/175、iec104_dtu 7/7;真实 TCP 验收
13/13;暂存口径门禁 18 项全通过。

已知边界:模拟器不链接 iec104_serv.c,TCP 验收不覆盖 prot_set 绑定,
板端需实机确认主站写入能反映到本地定值页;17 项中 15 项的出厂默认
值与点表字面量不同,主站读到的是真实模型的值;绑定真实源后定值区
切换一律拒绝(单套活跃值无原子整区事务)。
2026-11-19 15:48:21 +08:00

50 lines
2.8 KiB
Python

"""Independent reference extraction, never imports the production DTU model."""
import hashlib
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
EXAMPLES = ROOT / "IEC60870-Slave" / "examples"
def reference():
text = (EXAMPLES / "iec104_dtu_profile.h").read_text(encoding="utf-8-sig")
tables = {}
for name in ("signals", "measurements", "energy", "commands", "parameters"):
body = re.search(r"dtu_" + name + r"\[\]\s*=\s*\{(.*?)\n\};", text, re.S).group(1)
tables[name] = [int(value, 16) for value in re.findall(r"\{0x([0-9A-Fa-f]+)U,", body)]
parameters = {}
for ioa, tag, value, configured in re.findall(
r'\{0x([0-9A-Fa-f]+)U, (\d+)U, (NULL|"[^"]*"), (true|false),', text):
if configured == "true":
parameters[int(ioa, 16)] = (int(tag), value.strip('"'))
assert [len(tables[x]) for x in tables] == [59, 30, 4, 19, 430]
assert len(parameters) == 345
# Accepted consumer subset. This list is the acceptance specification and
# must match the semantic bridge map in iec104_dtu_table.c item for item.
# Every other configured profile row must be refused by the protocol layer.
consumer_ioas = frozenset((
0x8841, 0x8842, 0x8843, 0x8845, 0x8846, 0x8847, 0x8849, 0x884A, 0x884B,
0x8027, 0x8028, 0x8029, 0x802A, 0x8024, 0x8025, 0x8040, 0x8041,
))
parameters = {ioa: row for ioa, row in parameters.items() if ioa in consumer_ioas}
assert len(parameters) == 17
assert set(tables["signals"]) == set(range(1, 0x42)) - set(range(0x2C, 0x32))
assert set(tables["measurements"]) == set(range(0x4001, 0x401F))
gi = {ioa: 1 for ioa in tables["signals"]}
gi.update({ioa: 3 for ioa in range(0x1001, 0x1011)})
gi.update({ioa: 13 for ioa in tables["measurements"]})
for group in range(4):
for index in range(30):
gi[0x8601 + group * 0x20 + index] = 112 if group == 3 else (110 if group == 0 and index >= 4 else 111)
read = {ioa: 30 if typ == 1 else 31 if typ == 3 else typ for ioa, typ in gi.items() if typ < 100}
read.update({ioa: 5 for ioa in range(0x1201, 0x1209)})
read.update({ioa: 7 for ioa in range(0x1301, 0x1309)})
diagnostics = {ioa: 13 for ioa in range(0x9601, 0x962D) if ioa not in range(0x961C, 0x961F)}
diagnostics.update({ioa: 30 for ioa in range(0x9141, 0x919E)})
diagnostics.update({ioa: 30 for ioa in range(0xB100, 0xB110)})
return {"profile": tables, "remote": parameters, "gi": gi, "read": read, "diagnostic_aliases_pending_scope": diagnostics,
"counters": list(range(0x6401, 0x6409)),
"hashes": {name: hashlib.sha256((EXAMPLES / name).read_bytes()).hexdigest()
for name in ("iec104_dtu_profile.h", "iec104_example_points.h", "iec104_slave_example.c")}}