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:
@@ -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