13ed84a0f0
104 从 App/protocol 迁入 App/iec60870 时,协议核心(common/ 104/ examples/) 与我方平台适配层(原 104_port/)仍分成两个平行目录。既然这个库已转为项目自有 代码,适配层没有独立存在的理由——它本来就是对协议的装配与平台后端。 本次只做目录合并,不动任何协议逻辑: - App/iec60870/104_port/ 的 11 个文件全部移入 App/iec60870/104/ (iec104_core / iec104_source / iec104_source_ranks / iec104_net_mcu / iec104_dtu_table / iec104_comm 的 .c/.h),空目录删除 - Keil 删掉 ../App/iec60870/104_port include 路径;模拟器 CMake 把重复的 104_port include 行合并进既有的 IEC60870_DIR/104 - 单测运行器里硬拼的 "104_port" 路径段改成 "104"(rename_ref 只认斜杠形态) - .pre-commit-config.yaml 的 iec104-tcp 触发面去掉 104_port 前缀;coding-standard 的排除注释改写为「库已转为自有代码,排除项待收编完成后删除」 - 文档与生成物同步:项目架构总览、软件架构与运行时规格、IEC104 上行设计说明、 产品需求、网络配置说明、App/README、仓根 README,以及引用关系图与架构 SVG 验证(均为实跑): - Keil UV4 RC=0,0 errors 0 warnings,Code=367464 RO=48492 RW=2288 ZI=173544 - 模拟器构建 OK,编译告警 0,iec104-tcp 13/13 真实 TCP 回环通过 - 受影响单测:iec104_comm 50/50、iec104_serv 50/50、iec104_netmcu 13/13 与 static 2/2、soe 32/32、iec104_dtu 7 tests OK - 覆盖率门禁 RC=0,line 8204/9185 (89.3%),49/49 套件 - 全仓 104_port 残留:仅 App/README 里一句「原 104_port 并入 104」的历史说明
160 lines
5.9 KiB
C
160 lines
5.9 KiB
C
/*
|
|
* 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;
|
|
}
|