merge: 104 协议栈收编为项目自有代码(目录合并 + 裁剪 + 解除第三方钉死)
把 7 个提交合并进主树,主线是「App/protocol 这一层去掉,104 只维护一份」。 - 目录:App/protocol/iec60870 -> App/iec60870,适配层 104_port 并入 104, App/protocol 整层删除 - 裁剪:删 12 个无实现的纯头 + 4 个声明了却无实现的死头,信息对象裁 9 个 用不到的类型,Flash 省约 2 KB - 收编:删 UPSTREAM.sha256,IMPORT.md 改 README.md,许可证口径改为团队自有, coding-standard 排除面从整个 104/ 收窄到只排 business/ 与 IEC104* - 测试:补回 104 适配层与服务层的主机单测,覆盖率门禁从红转绿 合并冲突共 4 处,全是内存数字(ram_baseline.tsv、size_attestation.json、 RAM 台账、RAM 使用分析):两个分支各自构建过,谁的值都不是合并后的真值。 处理方式是先按主树侧收口让树可构建,再重新构建取真值回填 —— 实测合并后 ER_IROM1 = 414,128 B,两边都不等于这个数(主树 416,092 / 分支 413,992)。 验证:Keil 0 errors 0 warnings;模拟器构建 OK;ioboard 131/131、 ui_data 305/305、104 TCP 回环 13/13、覆盖率 89.3% 无 REGRESS; 全量 pre-commit 20 Passed / 0 Failed。
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
"""B6 bench leg: IEC 60870-5-104 master client (GI + COV round-trip).
|
||||
|
||||
Historical bench leg; acceptance criteria: docs/子系统设计/IEC104上行设计说明.md §8.
|
||||
docs/子系统设计/IEC104上行设计说明.md §3/§4. Constants mirror
|
||||
App/protocol/iec104/iec104_asdu.h / iec104_link.h.
|
||||
docs/子系统设计/IEC104上行设计说明.md §3/§4. Constants mirror the old in-house
|
||||
protocol layer (App/protocol/iec104/iec104_asdu.h / iec104_link.h, both deleted
|
||||
2026-11-18 with the skeleton MCU migration); current wire layout is governed by
|
||||
App/iec60870/common/IEC60870_Asdu.h + App/iec60870/104/IEC104_Types.h.
|
||||
|
||||
Flow:
|
||||
1. TCP connect device :2404 (factory default; --port overrides),
|
||||
@@ -64,7 +66,8 @@ import socket
|
||||
import struct
|
||||
import time
|
||||
|
||||
# ---- constants (mirror iec104_asdu.h / iec104_link.h) -----------------------
|
||||
# ---- constants (mirror the old iec104_asdu.h / iec104_link.h; both deleted
|
||||
# 2026-11-18 -- current layout: App/iec60870/common/ + App/iec60870/104/) ------
|
||||
U_STARTDT_ACT, U_STARTDT_CON = 0x07, 0x0B
|
||||
U_TESTFR_ACT, U_TESTFR_CON = 0x43, 0x83
|
||||
|
||||
@@ -76,9 +79,13 @@ T_C_IC_NA_1, T_C_RD_NA_1 = 100, 102
|
||||
COT_BG, COT_SPONT, COT_INIT, COT_REQ = 2, 3, 4, 5
|
||||
COT_ACT, COT_ACTCON, COT_ACTTERM = 6, 7, 8
|
||||
COT_PN = 0x40 # cause byte bit6: negative confirm
|
||||
# NOTE(2026-10-25 m104fix): THIS SLAVE USES THE B4 IN-HOUSE 6-BYTE ASDU HEAD --
|
||||
# T/PN/RBT FLAGS LIVE IN THE CAUSE BYTE (bit7/bit6/bit4), byte[3] IS OA+EVI(bit7).
|
||||
# SEE App/protocol/iec104/iec104_asdu.c iec104_asdu_put_hdr(). The old RX path read
|
||||
# NOTE(2026-10-25 m104fix): THE 6-BYTE ASDU HEAD PUTS T/PN FLAGS IN THE CAUSE BYTE
|
||||
# (bit7/bit6) AND byte[3] IS OA -- NOT COT HI. Current firmware config is sizeOfCOT=2
|
||||
# (App/iec60870/104/IEC104_Types.h IEC104_APP_LAYER_PARAMETERS_DEFAULT), written by
|
||||
# App/iec60870/common/IEC60870_Asdu.c: buffer[2] = cause | PN<<6 | TEST<<7, buffer[3] = OA.
|
||||
# The in-house writer this note was first written against, old
|
||||
# App/protocol/iec104/iec104_asdu.c iec104_asdu_put_hdr(), was deleted on 2026-11-18
|
||||
# with the skeleton MCU migration. The old RX path read
|
||||
# PN from byte[3] (standard 2-byte-COT habit) -> pn was ALWAYS False and a rejected
|
||||
# command logged cot=71 (7|0x40), so actcon_of(type, True) could never match.
|
||||
# Bench proof 2026-10-25 (V5-gap replay): two out-of-range C_SE writes
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* IEC104 [comm] segment host unit test.
|
||||
* 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.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cfg_file.h"
|
||||
|
||||
static int s_total = 0, s_passed = 0, s_failed = 0;
|
||||
#define CHECK(cond, name) do { \
|
||||
s_total++; \
|
||||
if (cond) { s_passed++; printf(" [PASS] %s\n", name); } \
|
||||
else { s_failed++; printf(" [FAIL] %s (line %d)\n", name, __LINE__); } \
|
||||
} while (0)
|
||||
|
||||
/* Config engine fake, controlled by each test case. */
|
||||
static int g_save_result;
|
||||
static int g_register_calls;
|
||||
static int g_save_calls;
|
||||
static cfg_section_reg_t g_last_reg;
|
||||
|
||||
int cfg_file_save(void)
|
||||
{
|
||||
g_save_calls++;
|
||||
return g_save_result;
|
||||
}
|
||||
|
||||
int cfg_file_register(const cfg_section_reg_t *p_reg)
|
||||
{
|
||||
g_register_calls++;
|
||||
if (p_reg != NULL) {
|
||||
g_last_reg = *p_reg;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "iec104_comm.c"
|
||||
|
||||
static void reset_fakes(int save_result)
|
||||
{
|
||||
g_save_result = save_result;
|
||||
g_register_calls = 0;
|
||||
g_save_calls = 0;
|
||||
memset(&g_last_reg, 0, sizeof(g_last_reg));
|
||||
}
|
||||
|
||||
static void tc_init_defaults(void)
|
||||
{
|
||||
uint16_t ca, port;
|
||||
|
||||
reset_fakes(0);
|
||||
CHECK(iec104_comm_ca_set(1234) == 0, "set ca before init");
|
||||
CHECK(iec104_comm_port_set(4000) == 0, "set port before init");
|
||||
CHECK(iec104_comm_init() == 0, "init returns register result");
|
||||
CHECK(g_register_calls == 1, "init registers exactly once");
|
||||
CHECK(strcmp(g_last_reg.name, IEC104_COMM_SEC) == 0, "registered section name");
|
||||
CHECK(g_last_reg.p_data == &s_comm, "registered RAM copy pointer");
|
||||
CHECK(g_last_reg.p_defaults == &s_comm_rom, "registered defaults pointer");
|
||||
CHECK(g_last_reg.data_size == sizeof(iec104_comm_t), "registered data size");
|
||||
CHECK(g_last_reg.p_keys == s_comm_keys, "registered key table");
|
||||
CHECK(g_last_reg.key_count == 2u, "registered key count");
|
||||
CHECK(g_last_reg.p_cb == NULL, "no config callback");
|
||||
CHECK(s_comm_keys[0].type == CFG_VAL_UINT && s_comm_keys[0].len == 2u &&
|
||||
s_comm_keys[0].offset == offsetof(iec104_comm_t, ca), "ca key descriptor");
|
||||
CHECK(s_comm_keys[1].type == CFG_VAL_UINT && s_comm_keys[1].len == 2u &&
|
||||
s_comm_keys[1].offset == offsetof(iec104_comm_t, port), "port key descriptor");
|
||||
ca = iec104_comm_ca();
|
||||
port = iec104_comm_port();
|
||||
CHECK(ca == IEC104_COMM_DEF_CA, "init restores ca default");
|
||||
CHECK(port == IEC104_COMM_DEF_PORT, "init restores port default");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "init clears dirty flag");
|
||||
}
|
||||
|
||||
static void tc_getter_band(void)
|
||||
{
|
||||
reset_fakes(0);
|
||||
(void)iec104_comm_init();
|
||||
|
||||
s_comm.ca = 0u;
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_DEF_CA, "ca below band falls back");
|
||||
s_comm.ca = IEC104_COMM_CA_MAX;
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_CA_MAX, "ca upper bound accepted");
|
||||
s_comm.ca = 42u;
|
||||
CHECK(iec104_comm_ca() == 42u, "ca in-band returned");
|
||||
|
||||
s_comm.port = 0u;
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_DEF_PORT, "port below band falls back");
|
||||
s_comm.port = IEC104_COMM_PORT_MIN - 1u;
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_DEF_PORT, "port 1023 falls back");
|
||||
s_comm.port = IEC104_COMM_PORT_MAX;
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_PORT_MAX, "port upper bound accepted");
|
||||
s_comm.port = 2404u;
|
||||
CHECK(iec104_comm_port() == 2404u, "port in-band returned");
|
||||
}
|
||||
|
||||
static void tc_setters(void)
|
||||
{
|
||||
reset_fakes(0);
|
||||
(void)iec104_comm_init();
|
||||
|
||||
CHECK(iec104_comm_ca_set(IEC104_COMM_CA_MIN) == 0, "ca min accepted");
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_CA_MIN, "ca min stored");
|
||||
CHECK(iec104_comm_ca_set(IEC104_COMM_CA_MAX) == 0, "ca max accepted");
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_CA_MAX, "ca max stored");
|
||||
CHECK(iec104_comm_dirty_peek() == 1u, "legal ca set makes dirty");
|
||||
|
||||
(void)iec104_comm_init();
|
||||
CHECK(iec104_comm_ca_set(0u) == -1, "ca zero rejected");
|
||||
CHECK(iec104_comm_ca() == IEC104_COMM_DEF_CA, "rejected ca leaves copy unchanged");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "rejected ca does not make dirty");
|
||||
|
||||
CHECK(iec104_comm_port_set(IEC104_COMM_PORT_MIN) == 0, "port min accepted");
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_PORT_MIN, "port min stored");
|
||||
CHECK(iec104_comm_port_set(IEC104_COMM_PORT_MAX) == 0, "port max accepted");
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_PORT_MAX, "port max stored");
|
||||
CHECK(iec104_comm_dirty_peek() == 1u, "legal port set makes dirty");
|
||||
|
||||
(void)iec104_comm_init();
|
||||
CHECK(iec104_comm_port_set(IEC104_COMM_PORT_MIN - 1u) == -1, "port 1023 rejected");
|
||||
CHECK(iec104_comm_port() == IEC104_COMM_DEF_PORT, "rejected port leaves copy unchanged");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "rejected port does not make dirty");
|
||||
}
|
||||
|
||||
static void tc_store_save(void)
|
||||
{
|
||||
reset_fakes(0);
|
||||
(void)iec104_comm_init();
|
||||
CHECK(iec104_comm_store_save() == 0, "clean store is a no-op success");
|
||||
CHECK(g_save_calls == 0, "clean store does not call engine");
|
||||
|
||||
CHECK(iec104_comm_ca_set(7u) == 0, "dirty setup");
|
||||
g_save_result = -1;
|
||||
CHECK(iec104_comm_store_save() == -1, "engine failure propagated");
|
||||
CHECK(g_save_calls == 1, "engine save attempted once on failure");
|
||||
CHECK(iec104_comm_dirty_peek() == 1u, "failed save keeps dirty for retry");
|
||||
|
||||
g_save_result = 0;
|
||||
CHECK(iec104_comm_store_save() == 0, "retry save succeeds");
|
||||
CHECK(g_save_calls == 2, "retry calls engine again");
|
||||
CHECK(iec104_comm_dirty_peek() == 0u, "successful save clears dirty");
|
||||
CHECK(iec104_comm_store_save() == 0, "next clean store again no-op");
|
||||
CHECK(g_save_calls == 2, "no extra engine call after clear");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("=== IEC104 [comm] Host Unit Tests ===\n");
|
||||
tc_init_defaults();
|
||||
tc_getter_band();
|
||||
tc_setters();
|
||||
tc_store_save();
|
||||
printf("=== IEC104 Comm Tests: %d/%d PASSED (failed=%d) ===\n",
|
||||
s_passed, s_total, s_failed);
|
||||
return (s_failed == 0) ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Host unit-test runner for the IEC104 [comm] configuration segment."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "test", "host", "common"))
|
||||
|
||||
from unit_test_harness import TestBuild, run_host_unit_test # noqa: E402
|
||||
|
||||
BUILD = TestBuild(
|
||||
name="IEC104 Comm Tests",
|
||||
src=os.path.join(HERE, "iec104_comm_test.c"),
|
||||
include_dirs=[
|
||||
os.path.join(ROOT, "App", "cfg"),
|
||||
os.path.join(ROOT, "App", "iec60870", "104"),
|
||||
HERE,
|
||||
],
|
||||
summary_marker="=== IEC104 Comm Tests:",
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run_host_unit_test(BUILD))
|
||||
@@ -11,7 +11,7 @@
|
||||
## 前置条件
|
||||
|
||||
- Real board: powered, reachable at `192.168.31.100`, firmware IEC104 listener on `2404`,
|
||||
profile table matching `App/protocol/iec60870/examples/iec104_dtu_profile.h`.
|
||||
profile table matching `App/iec60870/examples/iec104_dtu_profile.h`.
|
||||
- Simulator: `simulator/build/Debug/lvgl_sim.exe` built with IEC104 headless CLI,
|
||||
chosen port free, Windows `Get-NetTCPConnection` available for the LAN bind probe.
|
||||
- All three scripts are Python 3 standard library only. They print ASCII only and
|
||||
|
||||
@@ -19,16 +19,45 @@ def main():
|
||||
args = parser.parse_args()
|
||||
output = args.output.resolve()
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
vendor = ROOT / "App/protocol/iec60870"
|
||||
vendor = ROOT / "App/iec60870"
|
||||
original = ROOT / "IEC60870-Slave/test"
|
||||
cl, inc, lib = locate_toolchain()
|
||||
if not cl:
|
||||
raise RuntimeError("MSVC compiler not found")
|
||||
env = dict(os.environ, INCLUDE=os.pathsep.join(inc), LIB=os.pathsep.join(lib))
|
||||
sources = [vendor / line.split(" ", 1)[1] for line in (vendor / "UPSTREAM.sha256").read_text().splitlines() if line.endswith(".c")]
|
||||
# Each fixture includes vendor TcpServer.c and provides the Net implementation.
|
||||
sources = [p for p in sources if p.name not in ("IEC104_TcpServer.c", "IEC104_Net_Windows.c")]
|
||||
assert len(sources) >= 26 and all(p.is_file() for p in sources)
|
||||
# 协议栈源文件清单写死在这里,不再从清单文件推导:App/iec60870 已是项目自有
|
||||
# 代码,增删文件时本次修改要同步这张表,别让测试悄悄少编一个单元。
|
||||
# 每个夹具自带 TcpServer.c 并自己提供 Net 实现,故这两份不在此列。
|
||||
sources = [vendor / rel for rel in (
|
||||
"104/IEC104_AsduQueue.c",
|
||||
"104/IEC104_Codec.c",
|
||||
"104/IEC104_Connection.c",
|
||||
"104/IEC104_Link.c",
|
||||
"104/IEC104_Link_Slave_StartStop.c",
|
||||
"104/IEC104_Receiver.c",
|
||||
"104/IEC104_SendBuffer.c",
|
||||
"104/IEC104_SendManager.c",
|
||||
"104/IEC104_Slave.c",
|
||||
"104/business/IEC104_ClockSynchronization.c",
|
||||
"104/business/IEC104_Command.c",
|
||||
"104/business/IEC104_CounterInterrogation.c",
|
||||
"104/business/IEC104_GeneralInterrogation.c",
|
||||
"104/business/IEC104_Interrogation.c",
|
||||
"104/business/IEC104_Parameter.c",
|
||||
"104/business/IEC104_PeriodicScan.c",
|
||||
"104/business/IEC104_Read.c",
|
||||
"104/business/IEC104_RemoteParameter.c",
|
||||
"104/business/IEC104_Spontaneous.c",
|
||||
"104/business/IEC104_Startup.c",
|
||||
"104/business/IEC104_SystemCommand.c",
|
||||
"common/IEC60870_Asdu.c",
|
||||
"common/IEC60870_Codec.c",
|
||||
"common/IEC60870_FaultEvent.c",
|
||||
"common/IEC60870_InformationObjects.c",
|
||||
"common/IEC60870_Slave.c",
|
||||
"common/IEC60870_Time.c",
|
||||
)]
|
||||
assert len(sources) == 27 and all(p.is_file() for p in sources)
|
||||
include = [vendor / "104", vendor / "104/business", vendor / "common", vendor / "platform", vendor / "examples", original]
|
||||
names = ("iec104_response_transport_test", "iec104_ack_tracking_test", "iec104_idle_transport_test", "iec104_strict_priority_transport_test", "iec104_tcp_event_ring_test", "iec104_tcp_scheduler_test", "iec104_remote_parameter_test")
|
||||
results = []
|
||||
@@ -49,13 +78,13 @@ def main():
|
||||
fixture.write_text(text.replace(old, new), encoding="utf-8")
|
||||
extra = []
|
||||
if name == "source_binding_test":
|
||||
extra = [ROOT / "App/protocol/iec104/iec104_source.c", ROOT / "App/protocol/iec104/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/protocol/iec104/iec104_core.c", ROOT / "App/protocol/iec104/iec104_source.c", ROOT / "App/protocol/iec104/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/protocol/iec104")] + [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}}
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
"""Test the independent acceptance oracle/decoder; not product acceptance."""
|
||||
import struct
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
if str(HERE) not in sys.path:
|
||||
sys.path.insert(0, str(HERE))
|
||||
|
||||
# Pure Python oracle/decoder checks: no C translation unit, so C line coverage does not apply.
|
||||
COVERAGE_SKIP = "纯 Python oracle/解码器测试,无 C 编译单元"
|
||||
|
||||
from oracle import reference
|
||||
from wire import asdu, header, objects, remote_objects, tlv
|
||||
|
||||
@@ -2,20 +2,20 @@
|
||||
|
||||
## 交接说明(最终交付状态)
|
||||
|
||||
- **这组测试证明什么**:针对 `App/protocol/iec104/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/protocol/iec104/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/protocol/iec104/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 负责。
|
||||
|
||||
## 静态源码检查(先于动态用例)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Static acceptance checks for App/protocol/iec104/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", "protocol", "iec104", "iec104_net_mcu.c")
|
||||
SRC = os.path.join(ROOT, "App", "iec60870", "104", "iec104_net_mcu.c")
|
||||
OUT = os.path.join(HERE, "results", "static")
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* IEC104 Net MCU backend isolation contract tests.
|
||||
* Builds the REAL App/protocol/iec104/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>
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"scope": "REAL backend App/protocol/iec104/iec104_net_mcu.c + fake net layer; host-only isolation evidence, not full core/TcpServer/hardware",
|
||||
"backend_sha256": "4d6b8d9e8ed3404372f274d99c16bc87a06fecbee8db0b30b044b24d0562d73d",
|
||||
"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": "e8c913cac2a852173f052ce806a3da101a272e2a4dff8751499fdcf39788deab",
|
||||
"net_socket_fake.h": "d597375d1186b4801b4dc859401b6f3f0c9d1110efda37d0a9e73b810d7a8cd0",
|
||||
"net_socket_fake.c": "8eb9ea5ed195b38e2f4993ac021ad19afa40464ff03de73628d3a795350a631e",
|
||||
"netmcu_test.c": "09c203941c41fbb543580f45192c1604b984daaeb10135548756d4e42e8ebe02",
|
||||
"check_static.py": "8f08695a6c6bffec6047558b5b6d0aff0c7393665853357d1176f61b1aac1132"
|
||||
"MATRIX.md": "67126a0b92a832dd15710c3ce611d111e35f66c94d514d14939fdac7bd0ab314",
|
||||
"net_socket_fake.h": "c7c712256b5ac4b5162fd445319296dd32ada4abc6efa0384e65e2635bba38c9",
|
||||
"net_socket_fake.c": "f86ecd73bae57daad7d5d9506a0a8b7158344ba0e01f0e1d16af0ebdbdac3578",
|
||||
"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/protocol/iec104/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,6 +1,6 @@
|
||||
{
|
||||
"source": "D:\\Code\\DTU Code\\STM32F4-Base\\.agents\\worktrees\\dsh-d2fd6422-281a\\App\\protocol\\iec104\\iec104_net_mcu.c",
|
||||
"source_sha256": "4d6b8d9e8ed3404372f274d99c16bc87a06fecbee8db0b30b044b24d0562d73d",
|
||||
"source": "D:\\Code\\DTU Code\\STM32F4-Base\\.agents\\worktrees\\dsh-d2fd6422-281a\\App\\iec60870\\104\\iec104_net_mcu.c",
|
||||
"source_sha256": "1bcb602946485156ce04b49343ece9525e8ed50f5d5ee0c1d580982e8d77ecd9",
|
||||
"checks": [
|
||||
{
|
||||
"id": "S1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* 模块名称:IEC 60870-5-104 板端 Net 后端(netTask 单 owner) */
|
||||
/*
|
||||
* 模块功能:实现 App/protocol/iec60870/104/IEC104_Net.h 的全部导出 ABI,
|
||||
* 模块功能:实现 App/iec60870/104/IEC104_Net.h 的全部导出 ABI,
|
||||
* 底层走 Drivers/BSP/NET/net_socket.h 的 net_* 接口。
|
||||
* 适用平台:STM32F407ZGTx + CH395F;只在 netTask 上下文中调用(threadless)。
|
||||
* 创建日期:2026-11-17(task-18 板端网络割接)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"source": "D:\\Code\\DTU Code\\STM32F4-Base\\.agents\\worktrees\\dsh-d2fd6422-281a\\test\\host\\iec104_netmcu\\results\\static_mutant\\iec104_net_mcu_BAD.c",
|
||||
"source_sha256": "87bf283ec6b61ac14a0de3ab0f8a0352db4c62f03d4ea4479db36f9e0c546719",
|
||||
"source_sha256": "6ede122b69d5ed0074bcff01c8dd30223a8e5e521f5d78fafdc97791e1d7790e",
|
||||
"checks": [
|
||||
{
|
||||
"id": "S1",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
real_rc=0
|
||||
mutant_rc=1
|
||||
real source: D:\Code\DTU Code\STM32F4-Base\.agents\worktrees\dsh-d2fd6422-281a\App\protocol\iec104\iec104_net_mcu.c
|
||||
real source: D:\Code\DTU Code\STM32F4-Base\.agents\worktrees\dsh-d2fd6422-281a\App\iec60870\104_port\iec104_net_mcu.c
|
||||
mutant: D:\Code\DTU Code\STM32F4-Base\.agents\worktrees\dsh-d2fd6422-281a\test\host\iec104_netmcu\results\static_mutant\iec104_net_mcu_BAD.c
|
||||
|
||||
@@ -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", "protocol", "iec104", "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", "protocol", "iec60870", "104"),
|
||||
os.path.join(ROOT, "App", "protocol", "iec60870", "common"),
|
||||
os.path.join(ROOT, "App", "protocol", "iec104"),
|
||||
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, "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/protocol/iec104/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", "protocol", "iec104", "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/protocol/iec104/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)
|
||||
|
||||
@@ -0,0 +1,389 @@
|
||||
/*
|
||||
* IEC104 service adapter host unit test.
|
||||
* Compiles the real App/task/iec104_serv.c into this TU. All collaborators are
|
||||
* controlled stubs: core lifecycle, source binding, measurement snapshot,
|
||||
* calibration flags, net generation, event flags and task watchdog.
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static int s_total = 0, s_passed = 0, s_failed = 0;
|
||||
#define CHECK(cond, name) do { \
|
||||
s_total++; \
|
||||
if (cond) { s_passed++; printf(" [PASS] %s\n", name); } \
|
||||
else { s_failed++; printf(" [FAIL] %s (line %d)\n", name, __LINE__); } \
|
||||
} while (0)
|
||||
|
||||
#include "cmsis_os2.h"
|
||||
#include "sys_events.h"
|
||||
#include "net_socket.h"
|
||||
#include "cfg_file.h"
|
||||
|
||||
static int g_cfg_register_calls, g_cfg_save_result, g_cfg_save_calls;
|
||||
int cfg_file_register(const cfg_section_reg_t *reg) { (void)reg; g_cfg_register_calls++; return 0; }
|
||||
int cfg_file_save(void) { g_cfg_save_calls++; return g_cfg_save_result; }
|
||||
|
||||
int g_i104_info_count = 0;
|
||||
int g_i104_error_count = 0;
|
||||
|
||||
/* ---------- CMSIS/RTOS fake ---------- */
|
||||
osEventFlagsId_t sys_evtHandle = (osEventFlagsId_t)1;
|
||||
static uint32_t g_tick;
|
||||
static uint32_t g_ready_wait_result;
|
||||
static uint32_t g_ready_wait_options;
|
||||
static uint32_t g_events;
|
||||
|
||||
uint32_t osKernelGetTickCount(void) { return g_tick; }
|
||||
uint32_t osEventFlagsWait(osEventFlagsId_t id, uint32_t flags,
|
||||
uint32_t options, uint32_t timeout)
|
||||
{
|
||||
(void)id; (void)flags; (void)timeout;
|
||||
g_ready_wait_options = options;
|
||||
return g_ready_wait_result;
|
||||
}
|
||||
uint32_t osEventFlagsSet(osEventFlagsId_t id, uint32_t flags)
|
||||
{
|
||||
(void)id;
|
||||
g_events |= flags;
|
||||
return 0u;
|
||||
}
|
||||
|
||||
/* ---------- production headers, then production source ---------- */
|
||||
#include "iec104_comm.c"
|
||||
#include "iec104_core.h"
|
||||
#include "iec104_source.h"
|
||||
#include "iec104_net_mcu.h"
|
||||
#include "meas.h"
|
||||
#include "adc_cal.h"
|
||||
#include "ioboard_di.h"
|
||||
#include "prot_set.h"
|
||||
#include "run_par.h"
|
||||
#include "task_watchdog.h"
|
||||
#include "dbg_log.h"
|
||||
|
||||
/* ---------- collaborator stubs ---------- */
|
||||
static uint32_t g_net_generation;
|
||||
uint32_t net_get_generation(void) { return g_net_generation; }
|
||||
|
||||
static iec104_source_bindings_t g_bound;
|
||||
static int g_core_start_result;
|
||||
static int g_core_poll_result;
|
||||
static iec104_core_status_t g_core_status;
|
||||
static iec104_core_config_t g_last_core_cfg;
|
||||
static int g_core_start_calls, g_core_poll_calls, g_core_stop_calls, g_bind_calls;
|
||||
|
||||
int iec104_core_start(const iec104_core_config_t *cfg)
|
||||
{
|
||||
g_core_start_calls++;
|
||||
if (cfg != NULL) { g_last_core_cfg = *cfg; }
|
||||
return g_core_start_result;
|
||||
}
|
||||
int iec104_core_poll(void) { g_core_poll_calls++; return g_core_poll_result; }
|
||||
int iec104_core_stop(void) { g_core_stop_calls++; return 0; }
|
||||
void iec104_core_get_status(iec104_core_status_t *status)
|
||||
{
|
||||
if (status != NULL) { *status = g_core_status; }
|
||||
}
|
||||
void iec104_source_bind(const iec104_source_bindings_t *bindings)
|
||||
{
|
||||
g_bind_calls++;
|
||||
if (bindings != NULL) { g_bound = *bindings; }
|
||||
}
|
||||
|
||||
static uint32_t g_rx_bytes;
|
||||
void iec104_net_mcu_stats(uint32_t *rx_bytes)
|
||||
{
|
||||
if (rx_bytes != NULL) { *rx_bytes = g_rx_bytes; }
|
||||
}
|
||||
|
||||
static meas_snap_t g_snap;
|
||||
static int g_meas_result;
|
||||
int meas_get(meas_snap_t *out)
|
||||
{
|
||||
if (out != NULL) { *out = g_snap; }
|
||||
return g_meas_result;
|
||||
}
|
||||
static uint32_t g_cal_flags[ADC_SEM_COUNT];
|
||||
uint32_t adc_cal_flags(adc_semantic_t sem)
|
||||
{
|
||||
if ((int)sem < 0 || (int)sem >= (int)ADC_SEM_COUNT) { return 0u; }
|
||||
return g_cal_flags[sem];
|
||||
}
|
||||
|
||||
static uint8_t g_prot_dirty, g_run_dirty;
|
||||
uint8_t prot_set_dirty_peek(void) { return g_prot_dirty; }
|
||||
uint8_t run_par_dirty_peek(void) { return g_run_dirty; }
|
||||
|
||||
static int g_wd_beats;
|
||||
static task_wd_id_t g_last_wd;
|
||||
void task_wd_beat(task_wd_id_t id) { g_wd_beats++; g_last_wd = id; }
|
||||
|
||||
#include "iec104_serv.c"
|
||||
|
||||
static void reset_all(void)
|
||||
{
|
||||
g_tick = 0u;
|
||||
g_ready_wait_result = SYS_EVT_NET_READY;
|
||||
g_ready_wait_options = 0u;
|
||||
g_events = 0u;
|
||||
g_net_generation = 0u;
|
||||
g_core_start_result = 0;
|
||||
g_core_poll_result = 0;
|
||||
memset(&g_core_status, 0, sizeof(g_core_status));
|
||||
memset(&g_last_core_cfg, 0, sizeof(g_last_core_cfg));
|
||||
g_core_start_calls = g_core_poll_calls = g_core_stop_calls = g_bind_calls = 0;
|
||||
g_rx_bytes = 0u;
|
||||
memset(&g_snap, 0, sizeof(g_snap));
|
||||
g_meas_result = 0;
|
||||
memset(g_cal_flags, 0, sizeof(g_cal_flags));
|
||||
g_prot_dirty = g_run_dirty = 0u;
|
||||
g_wd_beats = 0;
|
||||
g_i104_info_count = g_i104_error_count = 0;
|
||||
g_cfg_register_calls = g_cfg_save_calls = 0;
|
||||
g_cfg_save_result = 0;
|
||||
(void)iec104_comm_init();
|
||||
/* reset service static owner state via a fresh start cycle */
|
||||
s_started = 0u; s_gen = 0u; s_retry_tick = 0u; s_dirty_next = 0u;
|
||||
s_last_active = 0u; s_link_run = 0u;
|
||||
memset(&s_stats, 0, sizeof(s_stats));
|
||||
s_last_conn = s_carry_conn = 0u;
|
||||
s_last_apdu = s_carry_apdu = 0u;
|
||||
s_last_txfail = s_carry_txfail = 0u;
|
||||
s_last_rx = s_carry_rx = 0u;
|
||||
g_tick = I104_START_RETRY_MS + 1u;
|
||||
iec104_serv_net_tick();
|
||||
g_tick = 0u;
|
||||
g_core_start_calls = g_core_poll_calls = g_core_stop_calls = g_bind_calls = 0;
|
||||
g_wd_beats = 0;
|
||||
g_events = 0u;
|
||||
g_i104_info_count = g_i104_error_count = 0;
|
||||
}
|
||||
|
||||
static void tc_monotonic(void)
|
||||
{
|
||||
uint32_t last = 100u, carry = 7u;
|
||||
|
||||
/* 正序:不跨回绕,carry 与 last 一起向后走。 */
|
||||
CHECK(s_monotonic(150u, &last, &carry) == 157u, "monotonic normal increment");
|
||||
CHECK(last == 150u && carry == 7u, "monotonic normal state");
|
||||
|
||||
/* 一次真回绕:now < last,把上一段的 last 折进 carry。 */
|
||||
CHECK(s_monotonic(50u, &last, &carry) == 207u, "monotonic reset carries previous");
|
||||
CHECK(last == 50u && carry == 157u, "monotonic reset state");
|
||||
|
||||
/* now == last 不算回绕:时间没有倒退,只是原地等了一拍。
|
||||
* 这一档单独写断言,是因为 `now < last` 误写成 `now <= last` 时
|
||||
* 返回值会多出一整个 last(本例 5 -> 10),而只看"单调增"是看不出来的。 */
|
||||
last = 5u; carry = 0u;
|
||||
CHECK(s_monotonic(5u, &last, &carry) == 5u, "monotonic same tick does not wrap");
|
||||
CHECK(last == 5u && carry == 0u, "monotonic same tick leaves carry alone");
|
||||
|
||||
/* 折进 last 会溢出时,carry 直接钉到 SAT —— 断言 carry 本身而不只是
|
||||
* 返回值:把 `*carry = I104_STAT_SAT` 改成 SAT-1,返回值仍是 SAT
|
||||
* (下一句的饱和分支照样命中),只有 carry 的内部状态能抓出来。 */
|
||||
last = 20u; carry = I104_STAT_SAT - 5u;
|
||||
CHECK(s_monotonic(10u, &last, &carry) == I104_STAT_SAT, "monotonic carry saturation");
|
||||
CHECK(carry == I104_STAT_SAT, "monotonic carry pinned exactly at SAT");
|
||||
|
||||
/* 已经饱和之后再进来,仍然饱和且不再叠加。 */
|
||||
last = 10u; carry = I104_STAT_SAT;
|
||||
CHECK(s_monotonic(15u, &last, &carry) == I104_STAT_SAT, "monotonic already saturated");
|
||||
CHECK(carry == I104_STAT_SAT && last == 15u, "monotonic stays saturated");
|
||||
|
||||
/* 32 位 tick 自然回绕(now 从 0xFFFFFFF0 走到 5)必须被判成回绕并饱和,
|
||||
* 不能算成"倒退了 4G 多"。 */
|
||||
last = 0xFFFFFFF0u; carry = 0u;
|
||||
CHECK(s_monotonic(5u, &last, &carry) == I104_STAT_SAT,
|
||||
"monotonic u32 wrap saturates");
|
||||
|
||||
/* 边界对照组:carry + now 恰好等于 SAT-1 时**不许**提前饱和。
|
||||
* 饱和判据是 `carry > SAT - now`,等号留给正常的加法。
|
||||
* 说明:carry + now 恰等于 SAT 的那一档与饱和分支返回值相同,
|
||||
* 属等价变异,这里不假装能区分,只用 SAT-1 这一档卡住"提前饱和"。 */
|
||||
last = 10u; carry = I104_STAT_SAT - 11u;
|
||||
CHECK(s_monotonic(10u, &last, &carry) == I104_STAT_SAT - 1u,
|
||||
"monotonic sum SAT-1 is not saturated early");
|
||||
}
|
||||
|
||||
static void tc_measure(void)
|
||||
{
|
||||
float value = -1.0f;
|
||||
uint8_t quality = 0xFFu;
|
||||
|
||||
reset_all();
|
||||
memset(&g_snap, 0, sizeof(g_snap));
|
||||
g_snap.yc[ADC_SEM_UB] = 12.5f;
|
||||
g_snap.qds[ADC_SEM_UB] = 0x20u; /* NT only */
|
||||
g_cal_flags[ADC_SEM_UB] = ADC_CAL_F_VALID;
|
||||
CHECK(g_bound.measurement != NULL && g_bound.signal != NULL,
|
||||
"source bindings installed after core start");
|
||||
CHECK(g_bound.measurement(NULL, 0x9606u, &value, &quality) == true,
|
||||
"mapped UB source available");
|
||||
CHECK(value == 12.5f && quality == 0x20u,
|
||||
"UB value and quality copied from snapshot");
|
||||
|
||||
g_cal_flags[ADC_SEM_UB] = 0u;
|
||||
CHECK(g_bound.measurement(NULL, 0x9606u, &value, &quality) == true,
|
||||
"uncalibrated UB still reports snapshot");
|
||||
CHECK(quality == 0xA0u, "uncalibrated UB gets IV bit");
|
||||
|
||||
g_snap.freq_valid = 1u;
|
||||
g_snap.freq_hz = 50.01f;
|
||||
CHECK(g_bound.measurement(NULL, 0x960Du, &value, &quality) == true,
|
||||
"frequency source available");
|
||||
CHECK(value == 50.01f && quality == 0u, "frequency value and clean quality");
|
||||
|
||||
g_snap.freq_valid = 0u;
|
||||
CHECK(g_bound.measurement(NULL, 0x960Du, &value, &quality) == false,
|
||||
"invalid frequency is unavailable");
|
||||
CHECK(g_bound.measurement(NULL, 0x9999u, &value, &quality) == false,
|
||||
"unmapped source is unavailable");
|
||||
CHECK(g_bound.measurement(NULL, 0x9605u, NULL, &quality) == false,
|
||||
"NULL value output rejected");
|
||||
CHECK(g_bound.measurement(NULL, 0x9605u, &value, NULL) == false,
|
||||
"NULL quality output rejected");
|
||||
g_meas_result = -2;
|
||||
CHECK(g_bound.measurement(NULL, 0x9605u, &value, &quality) == false,
|
||||
"no published snapshot is unavailable");
|
||||
g_meas_result = 0;
|
||||
|
||||
CHECK(g_bound.signal(NULL, 0x9141u, &quality, &quality) == false,
|
||||
"signal source always unavailable");
|
||||
}
|
||||
|
||||
static void tc_stats_and_link(void)
|
||||
{
|
||||
iec104_serv_stats_t stats;
|
||||
uint8_t link;
|
||||
|
||||
reset_all();
|
||||
memset(&stats, 0x55, sizeof(stats));
|
||||
iec104_serv_get_stats(&stats);
|
||||
CHECK(stats.conn_up == 0u && stats.apdu_tx == 0u &&
|
||||
stats.tx_fail == 0u && stats.rx_bytes == 0u,
|
||||
"get_stats fills four fields");
|
||||
iec104_serv_get_stats(NULL);
|
||||
CHECK(true, "get_stats NULL is harmless");
|
||||
link = iec104_serv_link_run();
|
||||
CHECK(link == 0u, "link_run starts at zero");
|
||||
|
||||
g_tick = I104_START_RETRY_MS;
|
||||
g_core_status.connections = 3u;
|
||||
g_core_status.written_i_frames = 4u;
|
||||
g_core_status.frame_failures = 5u;
|
||||
g_core_status.active = 1u;
|
||||
g_rx_bytes = 6u;
|
||||
iec104_serv_net_tick();
|
||||
iec104_serv_get_stats(&stats);
|
||||
CHECK(stats.conn_up == 3u && stats.apdu_tx == 4u &&
|
||||
stats.tx_fail == 5u && stats.rx_bytes == 6u,
|
||||
"stats mirror one core poll");
|
||||
CHECK(iec104_serv_link_run() == 1u, "link_run mirrors active");
|
||||
CHECK(s_last_active == 1u, "active edge recorded");
|
||||
|
||||
g_core_status.connections = 0u;
|
||||
g_core_status.written_i_frames = 0u;
|
||||
g_core_status.frame_failures = 0u;
|
||||
g_core_status.active = 0u;
|
||||
g_rx_bytes = 0u;
|
||||
g_tick += 1u;
|
||||
iec104_serv_net_tick();
|
||||
iec104_serv_get_stats(&stats);
|
||||
CHECK(stats.conn_up == 3u && stats.apdu_tx == 4u &&
|
||||
stats.tx_fail == 5u && stats.rx_bytes == 6u,
|
||||
"stats stay monotonic when sources reset");
|
||||
CHECK(iec104_serv_link_run() == 0u, "link_run follows active low");
|
||||
}
|
||||
|
||||
static void tc_tick_control(void)
|
||||
{
|
||||
iec104_serv_stats_t stats;
|
||||
int wd_before;
|
||||
|
||||
reset_all();
|
||||
g_ready_wait_result = osFlagsError;
|
||||
g_tick = 1000u;
|
||||
iec104_serv_net_tick();
|
||||
CHECK(g_wd_beats == 1 && g_last_wd == TASK_WD_IEC104,
|
||||
"watchdog beat precedes NET_READY return");
|
||||
CHECK((g_ready_wait_options & osFlagsNoClear) != 0u,
|
||||
"NET_READY is queried without clearing");
|
||||
CHECK(g_core_start_calls == 0 && g_core_poll_calls == 0,
|
||||
"network not ready: no core calls");
|
||||
|
||||
reset_all();
|
||||
g_ready_wait_result = SYS_EVT_NET_READY;
|
||||
g_tick = I104_START_RETRY_MS;
|
||||
g_core_start_result = -1;
|
||||
s_started = 0u; s_retry_tick = 0u;
|
||||
iec104_serv_net_tick();
|
||||
CHECK(g_core_start_calls == 1 && g_bind_calls == 0,
|
||||
"failed start does not bind sources");
|
||||
g_core_start_result = 0;
|
||||
iec104_serv_net_tick();
|
||||
CHECK(g_core_start_calls == 1, "start retry is throttled");
|
||||
g_tick += I104_START_RETRY_MS;
|
||||
iec104_serv_net_tick();
|
||||
CHECK(g_core_start_calls == 2 && g_bind_calls == 1,
|
||||
"retry after throttle binds sources once");
|
||||
CHECK(strcmp(g_last_core_cfg.bind_address, "0.0.0.0") == 0,
|
||||
"core bind address is wildcard");
|
||||
CHECK(g_last_core_cfg.port == iec104_comm_port() &&
|
||||
g_last_core_cfg.common_address == iec104_comm_ca(),
|
||||
"core config uses live comm getters");
|
||||
CHECK(g_core_poll_calls > 0, "successful start allows polling in later ticks");
|
||||
|
||||
reset_all();
|
||||
g_tick = I104_START_RETRY_MS + 1u;
|
||||
g_net_generation = 9u;
|
||||
iec104_serv_net_tick(); /* generation change: stop + restart_core (throttled) */
|
||||
g_tick += I104_START_RETRY_MS;
|
||||
iec104_serv_net_tick(); /* retry window elapsed: core starts again + rebind */
|
||||
CHECK(g_core_stop_calls >= 1 && g_core_start_calls >= 1,
|
||||
"generation change restarts core without depending on stop status");
|
||||
CHECK(g_bind_calls >= 1, "generation restart rebinds sources");
|
||||
CHECK(g_i104_info_count >= 1, "generation change leaves an info trace");
|
||||
|
||||
reset_all();
|
||||
g_tick = I104_START_RETRY_MS + 1u;
|
||||
iec104_serv_net_tick();
|
||||
g_tick += 1u;
|
||||
g_core_poll_result = -1;
|
||||
wd_before = g_wd_beats;
|
||||
iec104_serv_net_tick();
|
||||
CHECK(g_core_poll_calls == 2 && g_i104_error_count >= 1,
|
||||
"poll failure logs and restarts");
|
||||
iec104_serv_get_stats(&stats);
|
||||
CHECK(stats.conn_up == 0u, "poll failure does not publish stats");
|
||||
|
||||
reset_all();
|
||||
g_tick = I104_START_RETRY_MS;
|
||||
iec104_serv_net_tick();
|
||||
g_prot_dirty = 1u;
|
||||
g_run_dirty = 1u;
|
||||
g_tick = s_dirty_next;
|
||||
iec104_serv_net_tick();
|
||||
CHECK((g_events & SYS_EVT_PROT_SET_SAVE) != 0u,
|
||||
"protection dirty posts save event on deadline");
|
||||
CHECK((g_events & SYS_EVT_RUN_PAR_SAVE) != 0u,
|
||||
"run-param dirty posts save event on deadline");
|
||||
g_events = 0u;
|
||||
g_tick += 1u;
|
||||
iec104_serv_net_tick();
|
||||
CHECK(g_events == 0u, "no duplicate dirty event before next deadline");
|
||||
(void)wd_before;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("=== IEC104 Service Host Unit Tests ===\n");
|
||||
tc_monotonic();
|
||||
tc_measure();
|
||||
tc_stats_and_link();
|
||||
tc_tick_control();
|
||||
printf("=== IEC104 Serv Tests: %d/%d PASSED (failed=%d) ===\n",
|
||||
s_passed, s_total, s_failed);
|
||||
return (s_failed == 0) ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Host unit-test runner for the IEC104 netTask service adapter."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
|
||||
sys.path.insert(0, os.path.join(ROOT, "test", "host", "common"))
|
||||
|
||||
from unit_test_harness import TestBuild, run_host_unit_test # noqa: E402
|
||||
|
||||
BUILD = TestBuild(
|
||||
name="IEC104 Serv Tests",
|
||||
src=os.path.join(HERE, "iec104_serv_test.c"),
|
||||
include_dirs=[
|
||||
os.path.join(HERE, "stub"),
|
||||
os.path.join(ROOT, "App", "task"),
|
||||
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"),
|
||||
os.path.join(ROOT, "App", "iec60870", "platform"),
|
||||
os.path.join(ROOT, "App", "iec60870", "examples"),
|
||||
os.path.join(ROOT, "App", "algo"),
|
||||
os.path.join(ROOT, "App", "sync"),
|
||||
os.path.join(ROOT, "App", "util"),
|
||||
os.path.join(ROOT, "App", "app"),
|
||||
os.path.join(ROOT, "App", "cfg"),
|
||||
HERE,
|
||||
],
|
||||
summary_marker="=== IEC104 Serv Tests:",
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run_host_unit_test(BUILD))
|
||||
@@ -0,0 +1,27 @@
|
||||
/* Host-only stub: CMSIS-RTOS v2 surface used by iec104_serv.c. */
|
||||
#ifndef CMSIS_OS2_H
|
||||
#define CMSIS_OS2_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void *osEventFlagsId_t;
|
||||
|
||||
#define osFlagsWaitAny 0x00000000U
|
||||
#define osFlagsNoClear 0x00000002U
|
||||
#define osFlagsError 0x80000000U
|
||||
#define osOK 0
|
||||
|
||||
uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags,
|
||||
uint32_t options, uint32_t timeout);
|
||||
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags);
|
||||
uint32_t osKernelGetTickCount(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMSIS_OS2_H */
|
||||
@@ -0,0 +1,13 @@
|
||||
/* Host-only stub: logging macros used by iec104_serv.c (counting only). */
|
||||
#ifndef DBG_LOG_H
|
||||
#define DBG_LOG_H
|
||||
|
||||
extern int g_i104_info_count;
|
||||
extern int g_i104_error_count;
|
||||
|
||||
/* Consume all arguments so production call sites with computed locals do not
|
||||
* trigger C4189 under /W4. The comma expression has no side effects. */
|
||||
#define DBG_INFO(...) do { g_i104_info_count++; (void)(__VA_ARGS__); } while (0)
|
||||
#define DBG_ERROR(...) do { g_i104_error_count++; (void)(__VA_ARGS__); } while (0)
|
||||
|
||||
#endif /* DBG_LOG_H */
|
||||
@@ -0,0 +1,13 @@
|
||||
/* Host-only stub: IEC104 [comm] getters used by the service adapter. */
|
||||
#ifndef IEC104_COMM_H
|
||||
#define IEC104_COMM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define IEC104_COMM_DEF_CA 1u
|
||||
#define IEC104_COMM_DEF_PORT 2404u
|
||||
|
||||
uint16_t iec104_comm_ca(void);
|
||||
uint16_t iec104_comm_port(void);
|
||||
|
||||
#endif /* IEC104_COMM_H */
|
||||
@@ -0,0 +1,17 @@
|
||||
/* Host-only stub: net generation accessor used by iec104_serv.c. */
|
||||
#ifndef NET_SOCKET_H
|
||||
#define NET_SOCKET_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32_t net_get_generation(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* NET_SOCKET_H */
|
||||
@@ -0,0 +1,21 @@
|
||||
/* Host-only stub: system event flags and values used by iec104_serv.c. */
|
||||
#ifndef SYS_EVENTS_H
|
||||
#define SYS_EVENTS_H
|
||||
|
||||
#include "cmsis_os2.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern osEventFlagsId_t sys_evtHandle;
|
||||
|
||||
#define SYS_EVT_NET_READY (1u << 0)
|
||||
#define SYS_EVT_PROT_SET_SAVE (1u << 7)
|
||||
#define SYS_EVT_RUN_PAR_SAVE (1u << 8)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SYS_EVENTS_H */
|
||||
@@ -35,7 +35,7 @@ BUILD = TestBuild(
|
||||
src=os.path.join(HERE, "soe_test.c"),
|
||||
include_dirs=[
|
||||
os.path.join(ROOT, "App", "log"),
|
||||
os.path.join(ROOT, "App", "protocol", "iec60870", "common"),
|
||||
os.path.join(ROOT, "App", "iec60870", "common"),
|
||||
HERE,
|
||||
],
|
||||
summary_marker="=== SOE Tests:",
|
||||
|
||||
@@ -352,7 +352,7 @@ static void tc_yx_points(void)
|
||||
* 字面量:测试的职责是独立见证,改点数时必须有人被绊一下——绊到后要同时改:
|
||||
* ui_data.h 的 UI_DATA_YX_N、页面名表 ui_page_yx.c(有编译期数组守卫兜着)、本条。
|
||||
* 历史:割接前 33 同时等于旧产品点表 App/protocol/iec104/iec104_pts.c 的 YX_N
|
||||
* (该文件 2026-11-17 通信骨架割接已删除);现在协议侧是 DTU 标准表
|
||||
* (该文件 2026-11-17 通信骨架割接弃用、2026-11-18 随骨架迁 MCU 删除);现在协议侧是 DTU 标准表
|
||||
* iec104_dtu_table.c 的 SP 59 + DP 16 = 75 个遥信点,**两者语义不同、数值本就不等**,
|
||||
* 写侧 ui_data_feed.c 留了一条一次性 DBG_ERROR 把这个差异显式化,不改显示 */
|
||||
CHECK((uint32_t)UI_DATA_YX_N == 33u, "TC-UIDATA-009d point count is 33 (YX_N witness)");
|
||||
@@ -1317,8 +1317,9 @@ static void tc_maint_forwarding(void)
|
||||
(void)ui_data_maint_oper_note("force reset by panel");
|
||||
CHECK((s_stub_note_calls == 1) && (strcmp(s_stub_note_last, "force reset by panel") == 0),
|
||||
"TC-UIDATA-026i page-side note reaches executor verbatim");
|
||||
/* 面板这条与远端 YK11 那条(App/protocol/iec104/iec104_pts.c 投的
|
||||
* "force reset")必须是不同串,否则审计面再也分不出"主站下发的"还是
|
||||
/* 面板这条与远端 YK11 那条(旧 App/protocol/iec104/iec104_pts.c 投的
|
||||
* "force reset",该文件 2026-11-18 已删除;骨架阶段 104 侧无此记录)
|
||||
* 必须是不同串,否则审计面再也分不出"主站下发的"还是
|
||||
* "现场有人按了面板"。比的是执行侧缓冲里的实际内容,不是两个常量互比 */
|
||||
CHECK(strcmp(s_stub_note_last, "force reset") != 0,
|
||||
"TC-UIDATA-026s posted note differs from the remote YK11 wording");
|
||||
|
||||
Reference in New Issue
Block a user