feat(iec104): 在模拟器跑通共享协议核心与标准DTU点表
引入成熟 IEC 60870-5-104 子站的只读子集(App/protocol/iec60870,28 个源文件 按原字节复制,UPSTREAM.sha256 记录来源,兼容补丁保留全部断言与容量),在其上 装配单属主共享核心(Start/Poll/Stop/Get_Status),并把标准 DTU 演示点表全量 接入为唯一真源。 - iec104_core:threadless 推进,poll 零等待,通知/交互/COV 扫描全部有界; 生命周期区分失败启动待重试、重复启动拒绝、断线保固化 RAM。 - iec104_dtu_table / iec104_source:标准 DTU 全点表注册,无数据源的点读 0, 遥控只做模拟状态可读回,不触碰实际出口。 - iec104_sim:仅窗口与独立服务模式起真实 TCP,无头黄金模式保持确定性假统计。 - test/host/iec104_dtu:独立真实 TCP 11 项、传输层 10 项、生命周期与借用负例。 验证:lvgl_sim 真实 TCP 11/11、传输 10/10,UI 像素回归未回退;AC5 单 TU 编译 0 错误 0 诊断。板端尚未切换:完整库闭包 RAM 超 92% 门禁净增预算 25730B, 结论为停止转实施,记录在 IEC104_MCU最小可行性验证.md。旧实现与 board 服务 保持原样,未提交 MDK 工程改动,未烧板。
This commit is contained in:
@@ -9,6 +9,7 @@ set(LVGL_DIR "${PROJECT_ROOT}/Lib/lvgl")
|
||||
set(UI_DIR "${PROJECT_ROOT}/Drivers/BSP/UI")
|
||||
set(NET_DIR "${PROJECT_ROOT}/Drivers/BSP/NET")
|
||||
set(NETCFG_DIR "${PROJECT_ROOT}/Drivers/BSP/NETCFG")
|
||||
set(IEC60870_DIR "${PROJECT_ROOT}/App/protocol/iec60870")
|
||||
set(SDL2_DIR "${CMAKE_CURRENT_SOURCE_DIR}/SDL2/SDL2-2.32.10")
|
||||
|
||||
#=============================================================================
|
||||
@@ -62,6 +63,10 @@ include_directories(
|
||||
${PROJECT_ROOT}/App/algo # prot_set.h(定值模型纯算法,双端共享核心;见 ALGO_SOURCES)
|
||||
${PROJECT_ROOT}/App/util # ioboard_di.h(开入去抖与快照,双端共享核心)
|
||||
${PROJECT_ROOT}/Drivers/BSP # dbg_log.h(使用 simulator 的 dbg_cfg.h 配置)
|
||||
${IEC60870_DIR}/common
|
||||
${IEC60870_DIR}/104
|
||||
${IEC60870_DIR}/104/business
|
||||
${IEC60870_DIR}/examples
|
||||
${SDL2_DIR}/include # SDL2 头文件
|
||||
)
|
||||
|
||||
@@ -77,6 +82,7 @@ set(SDL2_LIB_X64 "${SDL2_DIR}/lib/x64")
|
||||
set(SIM_SOURCES
|
||||
main.c
|
||||
sim/sim_port.c
|
||||
sim/iec104_sim.c
|
||||
sim/sim_latency_stub.c # 探针符号宿主空实现(ui_net_sink.c 引用,见文件头注释)
|
||||
)
|
||||
|
||||
@@ -159,11 +165,42 @@ set(ALGO_SOURCES
|
||||
${PROJECT_ROOT}/App/algo/wave_ring.c
|
||||
)
|
||||
|
||||
# IEC 104 [comm] 配置段(道G task-8):模拟器运行期不跑 104 任务、无消费路径,
|
||||
# 本 TU 仅为双端注册事实一致而编译(iec104 其余 .c 走 scripts 白名单逐个点名,
|
||||
# 白名单是门禁地盘不在这里扩——新配对模块按 new-source-file 口径进显式源列表)。
|
||||
# Portable shared runtime intended for both targets; currently enabled in sim.
|
||||
# Do not link the original import or the example server/main in parallel.
|
||||
set(IEC104_SOURCES
|
||||
${PROJECT_ROOT}/App/protocol/iec104/iec104_comm.c
|
||||
${PROJECT_ROOT}/App/protocol/iec104/iec104_core.c
|
||||
${PROJECT_ROOT}/App/protocol/iec104/iec104_source.c
|
||||
${PROJECT_ROOT}/App/protocol/iec104/iec104_dtu_table.c
|
||||
${IEC60870_DIR}/common/IEC60870_Asdu.c
|
||||
${IEC60870_DIR}/common/IEC60870_Codec.c
|
||||
${IEC60870_DIR}/common/IEC60870_FaultEvent.c
|
||||
${IEC60870_DIR}/common/IEC60870_InformationObjects.c
|
||||
${IEC60870_DIR}/common/IEC60870_Slave.c
|
||||
${IEC60870_DIR}/common/IEC60870_Time.c
|
||||
${IEC60870_DIR}/104/IEC104_AsduQueue.c
|
||||
${IEC60870_DIR}/104/IEC104_Codec.c
|
||||
${IEC60870_DIR}/104/IEC104_Connection.c
|
||||
${IEC60870_DIR}/104/IEC104_Link.c
|
||||
${IEC60870_DIR}/104/IEC104_Link_Slave_StartStop.c
|
||||
${IEC60870_DIR}/104/IEC104_Receiver.c
|
||||
${IEC60870_DIR}/104/IEC104_SendBuffer.c
|
||||
${IEC60870_DIR}/104/IEC104_SendManager.c
|
||||
${IEC60870_DIR}/104/IEC104_Slave.c
|
||||
${IEC60870_DIR}/104/IEC104_TcpServer.c
|
||||
${IEC60870_DIR}/104/IEC104_Net_Windows.c
|
||||
${IEC60870_DIR}/104/business/IEC104_GeneralInterrogation.c
|
||||
${IEC60870_DIR}/104/business/IEC104_CounterInterrogation.c
|
||||
${IEC60870_DIR}/104/business/IEC104_Interrogation.c
|
||||
${IEC60870_DIR}/104/business/IEC104_Command.c
|
||||
${IEC60870_DIR}/104/business/IEC104_ClockSynchronization.c
|
||||
${IEC60870_DIR}/104/business/IEC104_RemoteParameter.c
|
||||
${IEC60870_DIR}/104/business/IEC104_Read.c
|
||||
${IEC60870_DIR}/104/business/IEC104_Parameter.c
|
||||
${IEC60870_DIR}/104/business/IEC104_Startup.c
|
||||
${IEC60870_DIR}/104/business/IEC104_SystemCommand.c
|
||||
${IEC60870_DIR}/104/business/IEC104_Spontaneous.c
|
||||
${IEC60870_DIR}/104/business/IEC104_PeriodicScan.c
|
||||
)
|
||||
|
||||
# 开入去抖与快照(App/util/ioboard_di.c):零平台依赖纯逻辑(只 include <stdint.h>),
|
||||
|
||||
@@ -129,6 +129,29 @@ LVGL 场景(同一份 ui_menu.c)
|
||||
|
||||
---
|
||||
|
||||
### 2.5 IEC104 共享核心显式运行模式
|
||||
|
||||
`--iec104` 路径装配 `App/protocol/iec104/iec104_core.c` 与成熟 Slave/TcpServer/Windows 网络后端,在同一个 `lvgl_sim.exe` 内提供真实 TCP,监听 `0.0.0.0`(全部本机 IPv4 接口,默认端口 2404)。本机可用 `127.0.0.1` 连接;局域网客户端应使用模拟器所在主机的实际 IPv4 地址,能否从远端访问仍取决于网络与 Windows 防火墙规则,本程序不自动修改防火墙。它不表示板端服务已经切换;旧板端协议栈仍保留。
|
||||
|
||||
```powershell
|
||||
.\simulator\build\Debug\lvgl_sim.exe --iec104 --iec104-headless --iec104-port 2404 --iec104-duration 60
|
||||
```
|
||||
|
||||
`--iec104-port 0` 请求宿主分配端口,应读取 READY 输出中的实际端口;有限运行时长由 `--iec104-duration` 指定。该模式与截图、keys、standby、rtlive、cfgtest、logtest 模式互斥,黄金数据不由协议定时推进。
|
||||
|
||||
| 边界 | 说明 |
|
||||
|---|---|
|
||||
| 数据 | 无源观测为 0,保留无效/替代品质;不是正常有效采样。参数保合法类型默认值,不要求参数全零 |
|
||||
| 参数 | 协议 RAM 写后可读但不持久;尚未绑定既有 UI 参数真源。上表“主站读取只能上板”仅指既有真实定值链,不适用于独立协议 RAM 演示 |
|
||||
| 遥控 | 只执行协议模拟动作和反馈,无物理开出(DO);协议反馈尚未映射 UI 遥信 |
|
||||
| 时间 | UI 与协议使用虚拟设备时间接缝,不能修改 Windows 系统钟;超时仍用单调时基 |
|
||||
| 运行报告 | 连接计数有真实观测;其余未接入观测的三项填 0,因 UI 无统计可用性(availability)字段,**这些 0 不证明实际统计为零**,也不代表核心 I 帧确认计数 |
|
||||
| 重连 | TCP 重连保已提交 RAM,取消未完成事务;显式服务重新初始化回默认 |
|
||||
|
||||
用户后续授权的全部 IPv4 监听变更已单独构建并在本机验证:二进制 SHA256 `dde0f0512624518b223564b47ce6bbafab01586532b82ff36fb490288ef012a7`,证据位于 `ci-artifacts/iec104-lan/`。OS 监听地址为 `0.0.0.0`;`127.0.0.1` 与本机确实配置的 `192.168.31.13` 均完成 STARTDT 握手,有限时服务退出后无监听残留。**这是本机连接验证,不证明其他机器可穿过防火墙访问;下述完整业务与黄金证据属于此前里程碑二进制,不冒充新哈希的全套复验。**
|
||||
|
||||
**仅模拟器首个里程碑通过(历史验收快照)**:当时二进制 SHA256 为 `d2285bbc68ce4353c814dbcc455a2083b56e81fc54bf88081402e821c6a92833`(`ci-artifacts/iec104-sim-release.sha256`)。同哈希审查人独立 TCP 复跑 11 项通过,报告 `ci-artifacts/iec104-review/tcp-final/report.json`;菜单 8 状态、内容 76 页、往返池水位及实时数据回归通过,日志 `ci-artifacts/iec104-sim-menu-release.log`、`ci-artifacts/iec104-sim-content-release.log`,未刷新黄金参考。构建、正常 READY/STOP 与端口占用失败证据分别为 `ci-artifacts/iec104-sim-build-release.log`、`ci-artifacts/iec104-sim-smoke-release.log`、`ci-artifacts/iec104-sim-bind-failure.log`;编译输入清单 `ci-artifacts/iec104-sim-release-sources.sha256` 记录 95 个编译 C 文件及 160 个输入文件哈希。**MCU 替换、旧栈删除及真实 UI 参数绑定仍未完成,cppcheck 尚未运行,不等于全部门禁或板端通过。**测试精确集合与矩阵见 `test/host/iec104_dtu/MATRIX.md`;完整边界见 `docs/子系统设计/IEC104上行设计说明.md` §0 和 `docs/测试说明/专项验证/模拟器差距清单.md`。
|
||||
|
||||
## 三、构建与运行
|
||||
|
||||
### 3.1 环境要求
|
||||
|
||||
+76
-1
@@ -71,6 +71,8 @@
|
||||
|
||||
#include "lvgl.h"
|
||||
#include "sim_port.h"
|
||||
#include "iec104_sim.h"
|
||||
#include "iec104_core.h"
|
||||
#include "ui_menu.h"
|
||||
#include "ui_standby.h"
|
||||
#include "ui_content_page.h" /* task-2:--rtlive 直调框架自动刷新判定体 */
|
||||
@@ -142,6 +144,17 @@ static uint32_t sim_map_key_char(char c) {
|
||||
* 返回值:无
|
||||
*/
|
||||
static void sim_time_cb(char *buf, size_t len) {
|
||||
if (iec104_sim_enabled()) {
|
||||
ui_data_clock_t clock;
|
||||
uint64_t seconds = iec104_sim_clock_local_s();
|
||||
if (seconds <= UI_DATA_CLOCK_EPOCH_MAX &&
|
||||
ui_data_clock_from_epoch((uint32_t)seconds, &clock) == 0) {
|
||||
(void)ui_data_clock_format(&clock, buf, (uint32_t)len);
|
||||
} else if (len != 0u) {
|
||||
buf[0] = '\0';
|
||||
}
|
||||
return;
|
||||
}
|
||||
time_t t = time(NULL);
|
||||
struct tm tmv = {0}; /* 显式清零:localtime_s/r 是"输出型"参数,静态分析按未初始化报 */
|
||||
|
||||
@@ -224,7 +237,11 @@ static int sim_maint_clock_set(uint32_t a_epoch) {
|
||||
if ((a_epoch == 0u) || (a_epoch > (uint32_t)UI_DATA_CLOCK_EPOCH_MAX)) {
|
||||
return -1;
|
||||
}
|
||||
s_sim_epoch = a_epoch;
|
||||
if (iec104_sim_enabled()) {
|
||||
if (iec104_sim_clock_set(a_epoch) != 0) { return -1; }
|
||||
} else {
|
||||
s_sim_epoch = a_epoch;
|
||||
}
|
||||
sim_publish_data_frames();
|
||||
return 0;
|
||||
}
|
||||
@@ -1001,6 +1018,18 @@ static void sim_fill_rt_frame(ui_data_rt_t *p_rt, uint32_t n) {
|
||||
p_rt->valid = 1u;
|
||||
p_rt->eng = UI_DATA_ENG_PIN;
|
||||
p_rt->tick_ms = 0u;
|
||||
if (iec104_sim_enabled()) {
|
||||
/* No acquisition is attached: do not publish the golden demonstration ramp. */
|
||||
for (i = 0u; i < UI_DATA_YC_N; ++i) {
|
||||
p_rt->yc[i].qds = UI_DATA_Q_IV;
|
||||
}
|
||||
for (i = 0u; i < UI_DATA_PH_MAX; ++i) {
|
||||
p_rt->ph[i].qds = UI_DATA_Q_IV;
|
||||
}
|
||||
sim_time_cb(p_rt->clock, sizeof(p_rt->clock));
|
||||
p_rt->clock_ok = (p_rt->clock[0] != '\0') ? 1u : 0u;
|
||||
return;
|
||||
}
|
||||
/* 遥测下标 = adc_semantic_t 语义序(UA/UB/UC/UN/IA/IB/IC/IN/TEMP)。
|
||||
* 模拟器不 include adc_input.h,故这里用语义序注释对照,不写符号名 */
|
||||
p_rt->yc[0].val = 0.8123f; /* UA 基波(引脚电压域) */
|
||||
@@ -1097,6 +1126,16 @@ static void sim_publish_rt_live(uint32_t n) {
|
||||
|
||||
sim_fill_rt_frame(&rt, n);
|
||||
(void)ui_data_publish_rt(&rt);
|
||||
if (iec104_sim_enabled()) {
|
||||
ui_data_info_t info;
|
||||
iec104_core_status_t status;
|
||||
if (ui_data_info(&info) == 0) {
|
||||
iec104_core_get_status(&status);
|
||||
info.port_104 = status.bound_port;
|
||||
info.iec104_conn_up = status.connections;
|
||||
(void)ui_data_publish_info(&info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void sim_publish_data_frames(void) {
|
||||
@@ -1137,6 +1176,20 @@ static void sim_publish_data_frames(void) {
|
||||
info.iec104_apdu_tx = 128u;
|
||||
info.iec104_tx_fail = 0u;
|
||||
info.iec104_rx_bytes = 4096u;
|
||||
if (iec104_sim_enabled()) {
|
||||
iec104_core_status_t status;
|
||||
iec104_core_get_status(&status);
|
||||
info.port_104 = status.bound_port;
|
||||
info.boot_ready = 0u;
|
||||
info.boot_count = 0u;
|
||||
info.boot_seq_state = 0u;
|
||||
info.rst_cause = 0u;
|
||||
info.iec104_conn_up = status.connections;
|
||||
/* No all-APDU/byte counters are exposed; I-frame ACKs are not TX APDUs. */
|
||||
info.iec104_apdu_tx = 0u;
|
||||
info.iec104_tx_fail = 0u;
|
||||
info.iec104_rx_bytes = 0u;
|
||||
}
|
||||
(void)ui_data_publish_info(&info);
|
||||
}
|
||||
|
||||
@@ -1727,6 +1780,13 @@ static int run_logtest(void) {
|
||||
* 返回值:0 - 正常退出或自检 PASS;非 0 - 失败 int
|
||||
*/
|
||||
int main(int argc, char **argv) {
|
||||
iec104_sim_options_t protocol_options;
|
||||
if (iec104_sim_parse(argc, argv, &protocol_options) != 0) {
|
||||
return 2;
|
||||
}
|
||||
if (protocol_options.headless) {
|
||||
return iec104_sim_run_headless(&protocol_options);
|
||||
}
|
||||
/* 形态旗标必须先扫(道F task-3):--keys / --standby 分支命中即 return,
|
||||
* 放在它们后面的循环根本走不到 —— 这是旗标看似加了、实际只在无参窗口模式
|
||||
* 生效的那类"看起来做了"的坑,写在这里省一个人重新踩 */
|
||||
@@ -1809,6 +1869,12 @@ int main(int argc, char **argv) {
|
||||
lv_sdl_window_set_title(disp, "STM32F4-Base UI Simulator (160x160 1bpp)");
|
||||
}
|
||||
|
||||
/* Protocol startup is opt-in, after platform setup and before UI snapshots. */
|
||||
if (protocol_options.enabled && iec104_sim_start(&protocol_options) != 0) {
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 配置 RAM 副本:镜像 netTask 上电流程(net_cfg_load → 工厂默认) */
|
||||
(void)net_cfg_load();
|
||||
|
||||
@@ -1880,6 +1946,12 @@ int main(int argc, char **argv) {
|
||||
|
||||
/* 轮询 RemoDispBus 协议(非阻塞收发) */
|
||||
ui_net_sink_poll();
|
||||
if (protocol_options.enabled && iec104_sim_poll() != 0) {
|
||||
fprintf(stderr, "[iec104] POLL_FAILED\n");
|
||||
(void)iec104_sim_stop();
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 执行待处理"保存网口配置"请求(单线程主循环 = 配置引擎属主;
|
||||
* 无请求 = 空操作);"立即应用"回调未注册 → 空操作 */
|
||||
@@ -1921,6 +1993,9 @@ int main(int argc, char **argv) {
|
||||
sleep_ms = (sleep_ms > spent_ms) ? (sleep_ms - spent_ms) : 1u;
|
||||
}
|
||||
|
||||
if (protocol_options.enabled && sleep_ms > 5u) {
|
||||
sleep_ms = 5u;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
Sleep(sleep_ms);
|
||||
#else
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* 模块名称:IEC104 模拟器生命周期适配
|
||||
* 作者:STM32F4-Base 项目组
|
||||
* 说明:共享核心拥有组帧、业务与传输;本模块只装配宿主入口。
|
||||
*/
|
||||
#include "iec104_sim.h"
|
||||
#include "iec104_core.h"
|
||||
#include "iec104_source.h"
|
||||
#include "IEC104_Net.h"
|
||||
#include "IEC60870_Time.h"
|
||||
#include <SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
static int s_started;
|
||||
static int s_exit_registered;
|
||||
|
||||
/* 函数功能:严格解析十进制,不接受符号、空白、后缀或溢出。 */
|
||||
static int parse_number(const char *text, uint32_t maximum, uint32_t *out)
|
||||
{
|
||||
uint32_t value = 0u;
|
||||
if (text == NULL || *text == '\0') { return -1; }
|
||||
while (*text != '\0') {
|
||||
uint32_t digit;
|
||||
if (*text < '0' || *text > '9') { return -1; }
|
||||
digit = (uint32_t)(*text++ - '0');
|
||||
if (value > (maximum - digit) / 10u) { return -1; }
|
||||
value = value * 10u + digit;
|
||||
}
|
||||
*out = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:打印协议模式用法并返回参数错误。 */
|
||||
static int invalid_usage(void)
|
||||
{
|
||||
fprintf(stderr, "[iec104] usage: --iec104 [--iec104-port 0..65535] [--iec104-headless [--iec104-duration 1..86400]]\n");
|
||||
fprintf(stderr, "[iec104] protocol mode cannot be combined with golden/selftest flags\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* 函数功能:在 main 的早退分支前校验协议选项与黄金模式互斥。 */
|
||||
int iec104_sim_parse(int argc, char **argv, iec104_sim_options_t *options)
|
||||
{
|
||||
int i, seen_port = 0, seen_duration = 0, incompatible = 0;
|
||||
memset(options, 0, sizeof(*options));
|
||||
options->port = 2404u;
|
||||
options->duration_s = 60u;
|
||||
for (i = 1; i < argc; ++i) {
|
||||
const char *arg = argv[i];
|
||||
uint32_t value;
|
||||
if (strcmp(arg, "--iec104") == 0) {
|
||||
if (options->enabled) { return invalid_usage(); }
|
||||
options->enabled = 1;
|
||||
} else if (strcmp(arg, "--iec104-headless") == 0) {
|
||||
if (options->headless) { return invalid_usage(); }
|
||||
options->headless = 1;
|
||||
} else if (strcmp(arg, "--iec104-port") == 0) {
|
||||
if (seen_port++ || ++i >= argc || parse_number(argv[i], 65535u, &value)) { return invalid_usage(); }
|
||||
options->port = (uint16_t)value;
|
||||
} else if (strcmp(arg, "--iec104-duration") == 0) {
|
||||
if (seen_duration++ || ++i >= argc || parse_number(argv[i], 86400u, &value) || value == 0u) { return invalid_usage(); }
|
||||
options->duration_s = value;
|
||||
} else if (strncmp(arg, "--iec104", 8u) == 0) {
|
||||
return invalid_usage();
|
||||
} else if (strcmp(arg, "--screenshot") == 0 || strcmp(arg, "--keys") == 0 ||
|
||||
strcmp(arg, "--standby") == 0 || strcmp(arg, "--rtlive") == 0 ||
|
||||
strcmp(arg, "--cfgtest") == 0 || strcmp(arg, "--logtest") == 0 ||
|
||||
strcmp(arg, "--noclock") == 0 || strcmp(arg, "--yt-nopump") == 0 ||
|
||||
strcmp(arg, "--comm-nopump") == 0 || strcmp(arg, "--wave-empty") == 0 ||
|
||||
strcmp(arg, "--wave-bad") == 0) {
|
||||
incompatible = 1;
|
||||
}
|
||||
}
|
||||
if ((!options->enabled && (options->headless || seen_port || seen_duration)) ||
|
||||
(options->enabled && incompatible) || (seen_duration && !options->headless)) { return invalid_usage(); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:兜底 SDL 窗口直接 exit 的有界停服清理。 */
|
||||
static void stop_at_exit(void)
|
||||
{
|
||||
if (s_started && iec104_sim_stop() != 0) {
|
||||
fprintf(stderr, "[iec104] exit cleanup failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* 函数功能:启动共享核心,核心只初始化一次 source,然后种入设备墙钟。 */
|
||||
int iec104_sim_start(const iec104_sim_options_t *options)
|
||||
{
|
||||
iec104_core_config_t config;
|
||||
iec104_core_status_t status;
|
||||
time_t now;
|
||||
if (s_started || options == NULL || !options->enabled) { return -1; }
|
||||
if (!s_exit_registered) {
|
||||
if (atexit(stop_at_exit) != 0) { return -1; }
|
||||
s_exit_registered = 1;
|
||||
}
|
||||
config.bind_address = "0.0.0.0";
|
||||
config.port = options->port;
|
||||
config.common_address = 1u;
|
||||
if (iec104_core_start(&config) != 0) {
|
||||
iec104_core_get_status(&status);
|
||||
fprintf(stderr, "[iec104] START_FAILED error=%d stage=%d native=%d\n",
|
||||
status.last_error, status.last_stage, status.native_error);
|
||||
/* Failed start may retain ownership when platform cleanup fails. */
|
||||
s_started = status.owns_resources ? 1 : 0;
|
||||
(void)iec104_sim_stop();
|
||||
return -1;
|
||||
}
|
||||
s_started = 1;
|
||||
now = time(NULL);
|
||||
if (now != (time_t)-1) {
|
||||
iec104_source_clock_seed((uint64_t)now * 1000u, IEC104_Net_Now_Ms());
|
||||
}
|
||||
iec104_core_get_status(&status);
|
||||
printf("[iec104] READY address=0.0.0.0 port=%u ca=1 mode=%s\n",
|
||||
(unsigned)status.bound_port, options->headless ? "headless" : "window");
|
||||
fflush(stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:执行一次非阻塞协议轮询,无 LVGL 或应用私有 socket。 */
|
||||
int iec104_sim_poll(void)
|
||||
{
|
||||
return s_started ? iec104_core_poll() : -1;
|
||||
}
|
||||
|
||||
/* 函数功能:有界停服并分别报告业务完成、写入与 IEC104 确认。 */
|
||||
int iec104_sim_stop(void)
|
||||
{
|
||||
iec104_core_status_t status;
|
||||
iec104_source_stats_t stats;
|
||||
uint64_t device_ms;
|
||||
if (!s_started) { return 0; }
|
||||
iec104_source_stats(&stats);
|
||||
device_ms = iec104_sim_clock_ms();
|
||||
if (iec104_core_stop() != 0) { return -1; }
|
||||
iec104_core_get_status(&status);
|
||||
s_started = 0;
|
||||
printf("[iec104] STOP connections=%lu rx_i=%lu written_i=%lu acked_i=%lu gi=%lu commands=%lu simulated_actions=%lu physical_do_actions=%lu parameter_commits=%lu clock_sets=%lu device_time_ms=%llu\n",
|
||||
(unsigned long)status.connections, (unsigned long)status.received_i_frames,
|
||||
(unsigned long)status.written_i_frames, (unsigned long)status.acknowledged_i_frames,
|
||||
(unsigned long)status.gi_completed, (unsigned long)status.commands_completed,
|
||||
(unsigned long)stats.simulated_actions, (unsigned long)stats.physical_do_actions,
|
||||
(unsigned long)stats.parameter_commits, (unsigned long)stats.clock_sets,
|
||||
(unsigned long long)device_ms);
|
||||
fflush(stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:查询协议模式是否持有运行核心。 */
|
||||
int iec104_sim_enabled(void)
|
||||
{
|
||||
return s_started;
|
||||
}
|
||||
|
||||
/* 函数功能:读取虚拟设备 UTC 毫秒墙钟。 */
|
||||
uint64_t iec104_sim_clock_ms(void)
|
||||
{
|
||||
return iec104_source_clock_ms(IEC104_Net_Now_Ms());
|
||||
}
|
||||
|
||||
/* 函数功能:按协议库唯一时区常量转换 UI 本地域秒。 */
|
||||
uint64_t iec104_sim_clock_local_s(void)
|
||||
{
|
||||
return iec104_sim_clock_ms() / 1000u +
|
||||
(uint64_t)IEC60870_LOCAL_UTC_OFFSET_MINUTES * 60u;
|
||||
}
|
||||
|
||||
/* 函数功能:将面板本地域秒写回虚拟设备 UTC 墙钟,不改宿主。 */
|
||||
int iec104_sim_clock_set(uint32_t epoch_s)
|
||||
{
|
||||
const uint32_t offset_s = (uint32_t)IEC60870_LOCAL_UTC_OFFSET_MINUTES * 60u;
|
||||
if (epoch_s < offset_s) { return -1; }
|
||||
iec104_source_clock_seed((uint64_t)(epoch_s - offset_s) * 1000u, IEC104_Net_Now_Ms());
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:同可执行程序和核心内有限时服务,黄金路径不进入。 */
|
||||
int iec104_sim_run_headless(const iec104_sim_options_t *options)
|
||||
{
|
||||
uint64_t deadline;
|
||||
int result = 0;
|
||||
if (iec104_sim_start(options) != 0) { return 1; }
|
||||
deadline = IEC104_Net_Now_Ms() + (uint64_t)options->duration_s * 1000u;
|
||||
while (IEC104_Net_Now_Ms() < deadline) {
|
||||
if (iec104_sim_poll() != 0) {
|
||||
fprintf(stderr, "[iec104] POLL_FAILED\n");
|
||||
result = 1;
|
||||
break;
|
||||
}
|
||||
SDL_Delay(5u);
|
||||
}
|
||||
if (iec104_sim_stop() != 0) { result = 1; }
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 模块名称:IEC104 模拟器生命周期接口
|
||||
* 作者:STM32F4-Base 项目组
|
||||
* 说明:同进程协议适配,不调用 LVGL 或物理硬件输出。
|
||||
*/
|
||||
#ifndef IEC104_SIM_H
|
||||
#define IEC104_SIM_H
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint16_t port;
|
||||
uint32_t duration_s;
|
||||
int enabled;
|
||||
int headless;
|
||||
} iec104_sim_options_t;
|
||||
|
||||
/* Parse only the protocol options, rejecting golden/selftest combinations.
|
||||
* Return 0 on success or 2 for invalid command-line usage. */
|
||||
int iec104_sim_parse(int argc, char **argv, iec104_sim_options_t *options);
|
||||
/* One main-thread owner, paired start/stop. 0 success, -1 failure. */
|
||||
int iec104_sim_start(const iec104_sim_options_t *options);
|
||||
int iec104_sim_poll(void);
|
||||
int iec104_sim_stop(void);
|
||||
int iec104_sim_enabled(void);
|
||||
/* Finite service in this executable, without LVGL timers or a second process. */
|
||||
int iec104_sim_run_headless(const iec104_sim_options_t *options);
|
||||
/* Device wall clock only, shared with the protocol source, never the OS clock. */
|
||||
uint64_t iec104_sim_clock_ms(void);
|
||||
uint64_t iec104_sim_clock_local_s(void);
|
||||
int iec104_sim_clock_set(uint32_t epoch_s);
|
||||
#endif
|
||||
Reference in New Issue
Block a user