Files
STM32F4-Base/IEC60870-Slave/test/iec104_command_test.c
T
STF bdcc8b6500
CI / L0 静态门禁 (pre-commit) (push) Failing after 7m46s
CI / L0.5 主机侧单测 (MSVC) (push) Successful in 21s
添加遥控、参数装载、文件传输等业务
2026-11-16 22:19:17 -08:00

6318 lines
324 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* IEC104遥控业务回归:通过真实Slave、TCP及SendManager路径验证控制事务。
* 复用iec104_tcp_fixture.h的可控Net、单调时钟和收发缓冲。
* 输入及期望应答由字节手工构造,避免输入与被测编码器共享同一种错误。
* 本文件已间接包含TcpServer.c,构建时不得再链接TcpServer.c或平台Net后端。
* 同一组用例分别运行于TCP容量1/2、独立应答槽容量1/4,输出用例组数与公开结构尺寸。
*/
/*
* 文件内导航(搜索方括号标记;Suite:Command):
* 复用公共fake Net和真实Slave/TCP;command_*为本专项辅助,TEST_HOOKS用于查询/凭证故障注入。
* [COMMON/BASE] 公共基础:依赖、编译选项与断言入口
* [COMMAND/FIXTURE_STATE] 专属夹具状态:服务、站点、事务、模拟动作及窗口
* [COMMAND/HOOKS] 测试钩子:状态查询与应答凭证故障注入
* [COMMON/LIFECYCLE] 专项共用辅助:点配置、服务启停、通知和业务事件消费
* [COMMON/WIRE] 专项共用辅助:独立请求字节、发送、回显及帧计数
* [COMMON/TRANSACTION] 专项共用辅助:动作、选择/执行、返回信息、完成、拥塞与重入
* [COMMAND/BASE] 基础事务用例:配置、报文、完整流程、动作结果及忙匹配
* [COMMAND/DUPLICATE] 决定期间重复请求:非选择重发、期限、校验和身份边界
* [COMMAND/LIFETIME] 事务生命周期:FINISHED、撤销、期限、FIFO、会话和站点
* [COMMAND/EXTENDED_HELPERS] 扩展命令辅助:47–51的值、原文请求与回显
* [COMMAND/EXTENDED] 扩展命令用例:类型配置、流程、匹配、非法值与变长回复
* [COMMAND/TIMED_HELPERS] 带时标辅助:58–64请求及CP56/基准配置
* [COMMAND/QUALIFIERS] QU/QL边界及非法限定词与既有事务隔离
* [COMMAND/TIMED] 带时标用例:完整流程、UTC换算、时间窗、基准与重入
* [COMMAND/POINTS] 点表用例:默认掩码、空表、逐点模式、二分查点及身份
* [COMMAND/RETURNS] 应答收尾用例:持续突发、返回信息与交接寿命
* [COMMAND/ADMISSION] 动作前准入:HIGH余量、旧回复门禁与查询失败
* [COMMAND/COMPLETION] 完成期限:Complete、交接超时、优先级与ENDED回收
* [COMMAND/ACTION_TIME] 动作前时效:等待中过期、拒绝原因及凭证故障
* [COMMAND/ABNORMAL] 异常收尾:失败/超时否定TERM、期限及会话边界
* [COMMAND/RESELECT] 重复选择复归:旧许可失效、清理事件与必要否定的FIFO
* [COMMAND/ADDRESS] 撤销寻址:未知IOA、错误优先级、事务隔离与应答背压
* [ENTRY/MAIN] 测试入口:基础、扩展、时标、点表及收尾分组
* 公共事务区集中动作与重入探针;扩展/时标辅助区包含独立输入表和各自的构造、校验函数。
* 函数定义按主题归类;main保留原调用顺序,RUN/CASE标记细分执行组。
*/
/* ============================================================================
* [COMMON/BASE] 公共基础:依赖、编译选项与断言入口
* ============================================================================ */
#include "iec104_test_config.h"
#include "iec104_tcp_fixture.h"
#include "IEC104_Command.h"
#ifndef IEC104_COMMAND_REPLY_CAPACITY
#define IEC104_COMMAND_REPLY_CAPACITY 4U
#endif
/* ============================================================================
* [COMMAND/FIXTURE_STATE] 专属夹具状态:服务、站点、事务、模拟动作及窗口
* ============================================================================ */
/* 每个用例独占一套服务、业务实例和站点;open/close负责隔离上一轮状态。
* last_input保留修复自引用后的真实Slave事件,供构造发送拥塞或重入校验使用。
* action_result控制模拟动作结果,action_calls和action_request核对至多一次执行及参数。
* window只在窗口背压用例中缩小,close恢复默认值,防止影响下一组测试。 */
static IEC104_Slave_t *command_slave;
static IEC104_Command_t *command;
static IEC104_Station_t command_station;
static IEC104_CommandStatus_t command_status;
static IEC104_SlaveEvent_t command_last_input;
static IEC104_CommandEventResult_t command_last_handled;
static unsigned command_master_ns, command_action_calls, command_checks;
static IEC104_CommandActionResult_t command_action_result;
static IEC104_CommandRequest_t command_action_request;
static uint16_t command_window = 32U;
/* ============================================================================
* [COMMAND/HOOKS] 测试钩子:状态查询与应答凭证故障注入
* ============================================================================ */
#ifdef IEC104_COMMAND_TEST_HOOKS
/* Faults are injected at Command's query boundaries.
* The transport, queue and default Slave implementations remain real. */
static bool command_response_error;
/* 0走真实查询,1使查询返回失败,2让查询成功但队列统计无效。
* 两类故障都应拒绝动作,而不是当作单纯容量不足继续等待。 */
static unsigned command_capacity_fault;
/**
* 功能:只让凭证查询返回故障,已有发送记录保持真实,用来检查故障能否终结事务且保留动作事实。
* 输入参数:slave——被测 Slave 实例(只读);ticket——发送跟踪票据。
* 输出参数:无。
* 返回值:响应票据状态,保留待交接、已交接及失效等区别。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
IEC104_ResponseState_t IEC104_Command_Test_Response_State(const IEC104_Slave_t *slave, uint64_t ticket)
{
return command_response_error ? IEC104_RESPONSE_ERROR : IEC104_Slave_Get_Response_State(slave, ticket);
}
/**
* 功能:区分查询调用失败与队列统计不可用。
* 输入参数:slave——被测 Slave 实例(只读);status——状态快照;error——底层错误码存储。
* 输出参数:status——状态快照;error——底层错误码存储。
* 返回值:真实传输接口或故障注入产生的 TCP 操作结果。
* 说明:两者都不能被当作HIGH还有容量而继续动作。
*/
IEC104_TcpResult_t IEC104_Command_Test_Get_Status(const IEC104_Slave_t *slave,
IEC104_TcpServerStatus_t *status, IEC104_TcpError_t *error)
{
IEC104_TcpResult_t result;
if (command_capacity_fault == 1U) { return IEC104_TCP_INTERNAL_ERROR; }
result = IEC104_Slave_Get_Status(slave, status, error);
if (result == IEC104_TCP_OK && command_capacity_fault == 2U) { status->asdu_queue_status_valid = false; }
return result;
}
#endif
/* These immutable tables belong to the test fixture, not the library defaults.
* Keep all borrowed point storage alive until the corresponding Destroy. */
static const IEC104_CommandPoint_t command_select_points[] = {
{1U, 6001U, UINT16_C(0x1fbf), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{1U, 6002U, UINT16_C(0x1fbf), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{2U, 6001U, UINT16_C(0x1fbf), IEC104_COMMAND_MODE_SELECT_EXECUTE}
};
static const IEC104_CommandPoint_t command_direct_points[] = {
{1U, 6001U, UINT16_C(0x3fff), IEC104_COMMAND_MODE_DIRECT},
{1U, 6002U, UINT16_C(0x3fff), IEC104_COMMAND_MODE_DIRECT},
{2U, 6001U, UINT16_C(0x3fff), IEC104_COMMAND_MODE_DIRECT}
};
/* ============================================================================
* [COMMON/LIFECYCLE] 专项共用辅助:点配置、服务启停、通知和业务事件消费
* ============================================================================ */
/**
* 功能:给配置借出固定寿命的点表。
* 输入参数:config——测试配置对象;mode——用例选择的行为模式。
* 输出参数:config——测试配置对象。
* 返回值:无。
* 说明:模式选择只影响该配置,不修改库的默认配置或静态表。
*/
static void command_config_mode(IEC104_CommandConfig_t *config, IEC104_CommandMode_t mode)
{
config->points = mode == IEC104_COMMAND_MODE_DIRECT ? command_direct_points : command_select_points;
config->point_count = sizeof(command_select_points) / sizeof(command_select_points[0]);
}
/**
* 功能:在真实默认配置上显式接入测试用选择执行点表,其他时限、回复策略保持默认值。
* 输入参数:config——测试配置对象。
* 输出参数:config——测试配置对象。
* 返回值:无。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static void command_config_default(IEC104_CommandConfig_t *config)
{
IEC104_Command_Config_Default(config);
command_config_mode(config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
}
/**
* 功能:消费全部Slave通知并按原FIFO交给Command,不在此消费业务事件或执行应用动作。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:last_handled只记录最近一条I帧的分派结果,后续RECORD/启停通知不得覆盖它。
* 用于验证“先读空通知,再推进动作”的接入约定。
*/
static void command_drain_transport(void)
{
IEC104_SlaveEvent_t event;
IEC104_TcpEventResult_t result;
while ((result = IEC104_Slave_NextEvent(command_slave, &event)) == IEC104_TCP_EVENT_READY) {
IEC104_CommandEventResult_t handled;
if (event.transport.type == IEC104_TCP_EVENT_I_FRAME) {
CHECK(IEC104_Slave_Copy_Event(&command_last_input, &event));
}
handled = IEC104_Command_Handle_Event(command, &event);
CHECK(handled != IEC104_COMMAND_EVENT_ERROR);
if (event.transport.type == IEC104_TCP_EVENT_I_FRAME) { command_last_handled = handled; }
}
CHECK(result == IEC104_TCP_EVENT_EMPTY);
}
/**
* 功能:推进一次真实传输层并读空通知。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:等待参数为0,可控后端不会真实阻塞。
*/
static void command_poll(void)
{
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
command_drain_transport();
}
/**
* 功能:推进一步业务并刷新只读状态。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:等待或已正常终结也必须返回处理成功。
*/
static void command_process(void)
{
CHECK(IEC104_Command_Process(command));
CHECK(IEC104_Command_Get_Status(command, &command_status));
}
/**
* 功能:有界交替驱动传输与业务,使已就绪应答交接、写出及通知消费能够完成。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:不自动Accept、不读取FINISHED,也不伪造主站ACK;仍然等待的事务由各用例显式处理。
*/
static void command_flush(void)
{
unsigned index;
for (index = 0U; index < 16U; ++index) {
command_poll();
command_process();
}
}
/**
* 功能:经可控accept和真实STARTDT建立新会话,核对Command收到激活通知。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:新TCP会话的主站发送序号从0开始;事务及旧事件身份由业务模块自行隔离。
*/
static void command_connect(void)
{
fake.accept_pending = 1U;
command_poll();
push(start_act, sizeof(start_act));
command_poll();
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.active);
command_master_ns = 0U;
}
/**
* 功能:创建并激活测试服务,configuration为NULL时使用默认参数及显式测试点表。
* 输入参数:configuration——可选测试配置(只读)。
* 输出参数:无。
* 返回值:无。
* 说明:链路和本地发送期限刻意放宽,使业务期限测试不会先触发无关的传输超时。
* 查找内部TCP实例仅用于现有夹具的容量背压和故障注入,不替换公开收发入口。
*/
static void command_open(const IEC104_CommandConfig_t *configuration)
{
IEC104_SlaveConfig_t server_configuration;
IEC104_CommandConfig_t defaults;
unsigned slot;
memset(&fake, 0, sizeof(fake));
memset(&command_station, 0, sizeof(command_station));
memset(&command_last_input, 0, sizeof(command_last_input));
fake.now = 1000U;
command_action_calls = 0U;
command_action_result = IEC104_COMMAND_ACTION_STARTED;
#ifdef IEC104_COMMAND_TEST_HOOKS
command_response_error = false;
command_capacity_fault = 0U;
#endif
command_slave = IEC104_Slave_Create();
CHECK(command_slave != NULL);
test_transport_config(&server_configuration);
server_configuration.protocol.link.k = command_window;
server_configuration.protocol.link.t1_ms = 600000U;
server_configuration.protocol.link.t3_ms = 600000U;
server_configuration.protocol.local_send_timeout_ms = 600000U;
CHECK(IEC104_Slave_Init(command_slave, &server_configuration, NULL) == IEC104_TCP_OK);
CHECK(IEC104_Slave_Add_Station(command_slave, &command_station, 1U, &command_action_calls) == IEC104_STATION_OK);
command_config_default(&defaults);
command = IEC104_Command_Create(command_slave, configuration == NULL ? &defaults : configuration);
CHECK(command != NULL);
sut = NULL;
for (slot = 0U; slot < IEC104_TCP_SERVER_CAPACITY; ++slot) {
if (server_pool[slot].in_use) { sut = &server_pool[slot].server; break; }
}
CHECK(sut != NULL);
command_connect();
}
/**
* 功能:读取且核对恰好一个指定类型的业务事件,返回可跨循环保存的纯值副本。
* 输入参数:type——协议类型或期望事件类型。
* 输出参数:无。
* 返回值:已构造或取出的事件值副本。
* 说明:调用后事件才算已交付,后续是否欠应用FINISHED以此为界。
*/
static IEC104_CommandEvent_t command_take(IEC104_CommandEventType_t type)
{
IEC104_CommandEvent_t event;
memset(&event, 0, sizeof(event));
CHECK(IEC104_Command_NextEvent(command, &event) == IEC104_COMMAND_NEXT_READY);
CHECK(event.type == type);
return event;
}
/**
* 功能:核对业务队列确实为空,并验证EMPTY不改写调用方输出存储的接口契约。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void command_empty(void)
{
IEC104_CommandEvent_t event, before;
memset(&event, 0x5a, sizeof(event));
before = event;
CHECK(IEC104_Command_NextEvent(command, &event) == IEC104_COMMAND_NEXT_EMPTY);
CHECK(memcmp(&event, &before, sizeof(event)) == 0);
}
/**
* 功能:按Cancel、读完FINISHED、关闭服务、读空通知、Destroy、移除站点的顺序收尾。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:节点必须已可复用,才能销毁Slave;此处不替应用归还额外Acquire的引用。
*/
static void command_close(void)
{
IEC104_CommandEvent_t event;
IEC104_CommandNextResult_t result;
IEC104_Command_Cancel(command);
while ((result = IEC104_Command_NextEvent(command, &event)) == IEC104_COMMAND_NEXT_READY) {
CHECK(event.type == IEC104_COMMAND_FINISHED);
}
CHECK(result == IEC104_COMMAND_NEXT_EMPTY);
CHECK(IEC104_Slave_Close(command_slave, NULL) == IEC104_TCP_OK);
command_drain_transport();
CHECK(IEC104_Command_Destroy(command));
if (IEC104_Station_Is_Registered(&command_station)) {
CHECK(IEC104_Slave_Remove_Station(command_slave, &command_station) == IEC104_STATION_OK);
}
CHECK(IEC104_Station_Is_Reusable(&command_station));
CHECK(IEC104_Slave_Destroy(command_slave, NULL) == IEC104_TCP_OK);
command = NULL;
command_slave = NULL;
command_window = 32U;
}
/* ============================================================================
* [COMMON/WIRE] 专项共用辅助:独立请求字节、发送、回显及帧计数
* ============================================================================ */
/**
* 功能:将调用方提供的ASDU字节封装成主站I帧,维护主站N(S),不自动确认子站数据。
* 输入参数:payload——原始对象载荷(只读);length——字节长度。
* 输出参数:无。
* 返回值:无。
* 说明:只写入可控Net输入队列;何时接收、交付和推进业务由用例决定。
*/
static void command_queue(const uint8_t *payload, size_t length)
{
uint8_t frame[255] = {0x68};
CHECK(length <= 249U);
frame[1] = (uint8_t)(length + 4U);
frame[2] = (uint8_t)(command_master_ns << 1);
frame[3] = (uint8_t)(command_master_ns >> 7);
++command_master_ns;
memcpy(frame + 6U, payload, length);
push(frame, length + 6U);
}
/**
* 功能:手工生成固定10字节的45/46请求或等长监视对象,用于输入和独立的期望结果。
* 输入参数:bytes——报文字节缓冲区;type——协议类型或期望事件类型;cause——完整传送原因字节;oa——发起者地址;ca——公共地址;ioa——信息对象地址;value——测试对象值或原始位模式。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:无。
* 说明:cause包含完整COT字节(包括TEST/P/N),value保留原SCO/DCO或监视对象字节。
*/
static void command_bytes(uint8_t bytes[10], unsigned type, unsigned cause,
unsigned oa, unsigned ca, unsigned ioa, unsigned value)
{
bytes[0] = (uint8_t)type; bytes[1] = 1U; bytes[2] = (uint8_t)cause; bytes[3] = (uint8_t)oa;
bytes[4] = (uint8_t)ca; bytes[5] = (uint8_t)(ca >> 8);
bytes[6] = (uint8_t)ioa; bytes[7] = (uint8_t)(ioa >> 8); bytes[8] = (uint8_t)(ioa >> 16);
bytes[9] = (uint8_t)value;
}
/**
* 功能:发送一条手工构造的请求并消费传输通知。
* 输入参数:type——协议类型或期望事件类型;cause——完整传送原因字节;oa——发起者地址;ca——公共地址;ioa——信息对象地址;value——测试对象值或原始位模式。
* 输出参数:无。
* 返回值:无。
* 说明:不会自动读取或应答工作事件。
*/
static void command_send(unsigned type, unsigned cause, unsigned oa, unsigned ca, unsigned ioa, unsigned value)
{
uint8_t bytes[10];
command_bytes(bytes, type, cause, oa, ca, ioa, value);
command_queue(bytes, sizeof(bytes));
command_poll();
}
/**
* 功能:扫描已经写出的APDU,返回真实业务ASDU的地址及长度,保持线上原顺序。
* 输入参数:frames——按线上顺序排列的 ASDU 视图数组;lengths——各 ASDU 的长度数组。
* 输出参数:frames——按线上顺序排列的 ASDU 视图数组;lengths——各 ASDU 的长度数组。
* 返回值:本次收集或统计到的有效业务帧数量。
* 说明:忽略S/U帧和制造拥塞的Type200填充帧;输入必须已写成完整帧。
* 输出指针借用fake.output,各用例应在缓冲内容改变前完成断言。
*/
static unsigned command_collect(const uint8_t **frames, size_t *lengths)
{
size_t offset = 0U;
unsigned count = 0U;
while (offset < fake.sent_bytes) {
const uint8_t *frame = fake.output + offset;
size_t length;
CHECK(fake.sent_bytes - offset >= 6U && frame[0] == 0x68U);
length = (size_t)frame[1] + 2U;
CHECK(length >= 6U && offset + length <= fake.sent_bytes);
if ((frame[2] & 1U) == 0U && frame[6] != 200U) {
CHECK(count < 64U);
frames[count] = frame + 6U;
lengths[count++] = length - 6U;
}
offset += length;
}
return count;
}
/**
* 功能:核对第index条业务输出的完整原文。
* 输入参数:index——待访问或核对的条目下标;type——协议类型或期望事件类型;cause——完整传送原因字节;oa——发起者地址;ca——公共地址;ioa——信息对象地址;value——测试对象值或原始位模式。
* 输出参数:无。
* 返回值:无。
* 说明:同时检查长度、OA/CA/IOA、原命令值及COT。
* 期望原文由手工字节构造,避免只检查状态计数而遗漏回显或帧序错误。
*/
static void command_reply_at(unsigned index, unsigned type, unsigned cause,
unsigned oa, unsigned ca, unsigned ioa, unsigned value)
{
const uint8_t *frames[64];
size_t lengths[64];
uint8_t expected[10];
CHECK(command_collect(frames, lengths) > index);
command_bytes(expected, type, cause, oa, ca, ioa, value);
CHECK(lengths[index] == sizeof(expected));
CHECK(memcmp(frames[index], expected, sizeof(expected)) == 0);
}
/**
* 功能:返回已写出的业务I帧数量,过滤规则与command_reply_at一致。
* 输入参数:无。
* 输出参数:无。
* 返回值:本次收集或统计到的有效业务帧数量。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static unsigned command_frame_count(void)
{
const uint8_t *frames[64]; size_t lengths[64];
return command_collect(frames, lengths);
}
/* ============================================================================
* [COMMON/TRANSACTION] 专项共用辅助:动作、选择/执行、返回信息、完成、拥塞与重入
* ============================================================================ */
/**
* 功能:模拟短时应用动作:记录调用次数与模块实际交付的请求,返回用例预设结果。
* 输入参数:context——业务回调的测试上下文;request——当前业务请求(只读)。
* 输出参数:无。
* 返回值:业务动作结果,保留应用成功、业务拒绝及未知对象等协议区别。
* 说明:不发布数据、不推进网络,因此重复调用可直接识别为事务执行错误。
*/
static IEC104_CommandActionResult_t command_action(void *context, const IEC104_CommandRequest_t *request)
{
CHECK(context == &command_action_calls);
++command_action_calls;
command_action_request = *request;
return command_action_result;
}
/**
* 功能:受理并接受一次选择,推进其确认后返回原SELECT事件。
* 输入参数:type——协议类型或期望事件类型;value——测试对象值或原始位模式。
* 输出参数:无。
* 返回值:已构造或取出的事件值副本。
* 说明:固定使用CA=1、IOA=6001、OA=0x51,方便后续匹配与逐字段变异测试。
*/
static IEC104_CommandEvent_t command_select(unsigned type, unsigned value)
{
IEC104_CommandEvent_t event;
command_send(type, 6U, 0x51U, 1U, 6001U, value | 0x80U);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
return event;
}
/**
* 功能:接受一次执行并返回其事件。
* 输入参数:type——协议类型或期望事件类型;value——测试对象值或原始位模式。
* 输出参数:无。
* 返回值:已构造或取出的事件值副本。
* 说明:刻意不等待确认交接,支持结果先于确认的测试。
*/
static IEC104_CommandEvent_t command_execute(unsigned type, unsigned value)
{
IEC104_CommandEvent_t event;
command_send(type, 6U, 0x51U, 1U, 6001U, value);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
return event;
}
/**
* 功能:构造CA=1、监视IOA=1001、COT=11的返回信息视图。
* 输入参数:object——信息对象或 ASDU 存储;bytes——报文字节缓冲区;type——协议类型或期望事件类型;value——测试对象值或原始位模式。
* 输出参数:object——构造、解析或追加完成的对象/ASDU;bytes——写入或读出的数据字节。
* 返回值:无。
* 说明:监视IOA与遥控IOA不同。
* object借用bytes,调用方在提交返回WAIT期间必须保留并维护该缓冲。
*/
static void command_return(IEC60870_Asdu_t *object, uint8_t bytes[10], unsigned type, unsigned value)
{
command_bytes(bytes, type, 11U, 0x51U, 1U, 1001U, value);
CHECK(IEC60870_Asdu_Create_From_Buffer(object,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, 10U) == IEC60870_ASDU_OK);
}
/**
* 功能:报告给定设备结果、关闭返回信息阶段并读出FINISHED,核对成功/失败终结原因。
* 输入参数:event——业务或传输事件(只读);result——用例预设或期望的处理结果。
* 输出参数:无。
* 返回值:已构造或取出的事件值副本。
* 说明:只适用于需要的返回信息已经提交完毕、且无需额外ACK才能推进的用例。
*/
static IEC104_CommandEvent_t command_done(const IEC104_CommandEvent_t *event, IEC104_CommandResult_t result)
{
IEC104_CommandEvent_t finished;
command_flush();
CHECK(IEC104_Command_Report_Result(command, event, result) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, event) == IEC104_COMMAND_REPLY_OK);
command_flush();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (result == IEC104_COMMAND_RESULT_SUCCEEDED ?
IEC104_COMMAND_FINISH_COMPLETED : IEC104_COMMAND_FINISH_EXECUTION_FAILED));
command_empty();
return finished;
}
/**
* 功能:阻塞实际Net写入,再填满当前帧与HIGH容量,直到公开发送接口真实返回WAIT。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:业务层没有伪造的提交结果;解除send_blocked后由原发送管理器恢复推进。
*/
static void command_congest(void)
{
uint8_t bytes[249] = {200, 0, 3, 0, 1, 0};
IEC60870_Asdu_t filler;
unsigned index;
fake.send_blocked = 1U;
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
for (index = 0U; index < 32U; ++index) {
if (!IEC60870_MasterConnection_Send_ASDU(&command_last_input.connection, &filler)) {
CHECK(IEC104_Slave_Get_Last_Submit_Result(&command_last_input.connection) == IEC104_TCP_SUBMIT_WAIT);
return;
}
}
CHECK(0);
}
/* 供动作回调使用的已交付事件,专门验证重入时不能再次消费或销毁当前事务。 */
static IEC104_CommandEvent_t command_reentry_event;
/**
* 功能:回调中只读查询应成功,推进、消费、拒绝、销毁及重复动作应被拒绝。
* 输入参数:context——业务回调的测试上下文;request——当前业务请求(只读)。
* 输出参数:无。
* 返回值:业务动作结果,保留应用成功、业务拒绝及未知对象等协议区别。
* 说明:Cancel为无效果操作;返回后原动作仍只执行一次,并可正常报告结果。
*/
static IEC104_CommandActionResult_t command_reentry_action(void *context, const IEC104_CommandRequest_t *request)
{
IEC104_CommandTransactionInfo_t info;
IEC104_CommandEvent_t ignored;
IEC104_CommandStatus_t status;
CHECK(IEC104_Command_Get_Status(command, &status));
CHECK(IEC104_Command_Get_Transaction(command, &command_reentry_event, &info));
CHECK(!IEC104_Command_Destroy(command));
CHECK(!IEC104_Command_Process(command));
CHECK(IEC104_Command_NextEvent(command, &ignored) == IEC104_COMMAND_NEXT_ERROR);
CHECK(IEC104_Command_Accept(command, &command_reentry_event, command_action, context) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Reject(command, &command_reentry_event, IEC104_COMMAND_REJECT_NEGATIVE) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Handle_Event(command, &command_last_input) == IEC104_COMMAND_EVENT_ERROR);
IEC104_Command_Cancel(command);
return command_action(context, request);
}
/* ============================================================================
* [COMMAND/BASE] 基础事务用例:配置、报文、完整流程、动作结果及忙匹配
* ============================================================================ */
/**
* 功能:验证以下场景与约束:配置与实例契约:默认值、类型位边界、非法配置、重复绑定及NULL参数。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:先销毁合法空实例再逐项尝试非法配置,确保拒绝不是被已有实例掩盖。
*/
static void test_command_configuration(void)
{
IEC104_CommandConfig_t config, invalid;
IEC104_CommandStatus_t status;
unsigned index;
IEC104_Command_Config_Default(&config);
CHECK(config.type_mask == UINT16_C(0x3fff));
CHECK(config.points == NULL && config.point_count == 0U);
CHECK(config.select_timeout_ms == 30000U && config.decision_timeout_ms == 15000U);
CHECK(config.execute_timeout_ms == 60000U);
CHECK(config.complete_timeout_ms == 30000U && config.handover_timeout_ms == 60000U);
CHECK(IEC104_COMMAND_TYPE_BIT(44U) == 0U && IEC104_COMMAND_TYPE_BIT(52U) == 0U);
CHECK(IEC104_COMMAND_TYPE_BIT(58U) == 128U && IEC104_COMMAND_TYPE_BIT(64U) == 8192U);
CHECK(IEC104_COMMAND_TYPE_BIT(65U) == 0U);
CHECK(IEC104_Command_Create(NULL, &config) == NULL);
CHECK(!IEC104_Command_Destroy(NULL));
CHECK(!IEC104_Command_Get_Status(NULL, &status));
command_open(NULL);
CHECK(IEC104_Command_Create(command_slave, &config) == NULL);
CHECK(IEC104_Command_Destroy(command));
for (index = 0U; index < 5U; ++index) {
invalid = config;
switch (index) {
case 0U: invalid.type_mask = 0U; break;
case 1U: invalid.type_mask |= UINT16_C(0x4000); break;
case 2U: invalid.select_timeout_ms = 0U; break;
case 3U: invalid.point_count = 1U; break;
default: invalid.points = command_select_points; break;
}
CHECK(IEC104_Command_Create(command_slave, &invalid) == NULL);
}
CHECK(IEC104_Command_Create(command_slave, NULL) == NULL);
command = IEC104_Command_Create(command_slave, &config);
CHECK(command != NULL);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:接收校验顺序:先COT、再CA、再IOA、最后对象内容。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:TEST/P/N随否定回显保留。
* 无唯一完整对象时静默丢弃,SQ和尾字节异常则规范成单对象10字节否定。
* 另验证未支持或被type_mask禁用的类型返回IGNORED,不误交付工作事件。
*/
static void test_command_wire_validation(void)
{
static const struct { unsigned type, cot, ca, ioa, value, cause; } cases[] = {
{45, 5, 65535, 0, 2, 45}, {45, 8, 65535, 0, 2, 46},
{45, 6, 2, 0, 2, 46}, {45, 6, 1, 0, 2, 47},
{45, 6, 1, 6001, 2, 7}, {46, 6, 1, 6001, 0, 7},
{46, 6, 1, 6001, 3, 7}, {46, 8, 1, 6001, 3, 9},
{45, 0x46, 1, 6001, 1, 7}, {45, 0x86, 1, 6001, 1, 0x87}
};
unsigned index;
for (index = 0U; index < sizeof(cases) / sizeof(cases[0]); ++index) {
command_open(NULL);
command_send(cases[index].type, cases[index].cot, 0x61U, cases[index].ca,
cases[index].ioa, cases[index].value);
command_flush();
CHECK(command_frame_count() == 1U);
command_reply_at(0U, cases[index].type, cases[index].cause | 0x40U,
0x61U, cases[index].ca, cases[index].ioa, cases[index].value);
CHECK(command_status.rejected == 1U && command_action_calls == 0U);
command_empty();
command_close();
}
for (index = 0U; index < 5U; ++index) {
uint8_t bytes[14];
size_t length = 10U;
command_open(NULL);
command_bytes(bytes, 45U, 6U, 0x62U, 1U, 6001U, 0x81U);
switch (index) {
case 0U: bytes[1] = 0U; break;
case 1U: bytes[1] = 2U; length = 14U; memset(bytes + 10U, 0, 4U); break;
case 2U: length = 9U; break;
case 3U: bytes[1] = 0x81U; break;
default: bytes[10] = 0x55U; length = 11U; break;
}
command_queue(bytes, length); command_poll(); command_flush();
CHECK(command_frame_count() == (index < 3U ? 0U : 1U));
if (index >= 3U) { command_reply_at(0U, 45U, 0x47U, 0x62U, 1U, 6001U, 0x81U); }
command_empty(); command_close();
}
{
IEC104_CommandConfig_t config;
uint8_t timed[17] = {0};
command_config_default(&config);
config.type_mask =
IEC104_COMMAND_TYPE_BIT(45U) | IEC104_COMMAND_TYPE_BIT(46U);
command_open(&config);
command_send(47U, 6U, 0U, 1U, 6001U, 1U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
command_bytes(timed, 58U, 6U, 0U, 1U, 6001U, 1U);
timed[14] = 1U; timed[15] = 1U; timed[16] = 26U;
command_queue(timed, sizeof(timed)); command_poll();
}
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
command_empty(); command_close();
{
IEC104_CommandConfig_t config;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(46U);
command_open(&config);
command_send(45U, 6U, 0U, 1U, 6001U, 0x81U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
command_empty();
command_send(46U, 6U, 0U, 1U, 6001U, 0x82U);
CHECK(command_take(IEC104_COMMAND_SELECT).request.type_id == 46U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:单/双命令的完整选择执行链:分别提交0、1、2帧返回信息,逐字节检查帧序。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:同时核对选择与执行属于同一事务但使用不同事件令牌、结果冻结、Complete一次性、
* 旧SELECT只能查询事务不能报告执行结果,以及槽回收后旧令牌失效。
*/
static void test_command_complete_flows(void)
{
unsigned type, count;
for (type = 45U; type <= 46U; ++type) {
for (count = 0U; count <= 2U; ++count) {
IEC104_CommandEvent_t selection, execution, finished;
IEC104_CommandTransactionInfo_t info;
IEC60870_Asdu_t data;
uint8_t bytes[10];
unsigned index, value = type == 45U ? 1U : 2U;
command_open(NULL);
selection = command_select(type, value | 4U);
CHECK(selection.request.state == value && selection.request.qualifier == 1U);
CHECK(selection.request.is_select && !selection.request.selected);
execution = command_execute(type, value | 4U);
CHECK(execution.request.selected && !execution.request.is_select);
CHECK(execution.transaction_id == selection.transaction_id);
CHECK(execution.event_serial != selection.event_serial);
command_flush();
CHECK(IEC104_Command_Get_Transaction(command, &selection, &info) && info.active);
CHECK(info.phase == IEC104_COMMAND_PHASE_EXECUTING && info.confirm_submitted);
CHECK(IEC104_Command_Report_Result(command, &selection, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Complete(command, &execution) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Report_Result(command, &execution, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Report_Result(command, &execution, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_ERROR);
for (index = 0U; index < count; ++index) {
command_return(&data, bytes, type == 45U ? 1U : 3U, value);
CHECK(IEC104_Command_Submit_Return_Info(command, &execution, &data) == IEC104_COMMAND_REPLY_OK);
command_poll();
}
CHECK(IEC104_Command_Complete(command, &execution) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &execution) == IEC104_COMMAND_REPLY_ERROR);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &execution, &data) == IEC104_COMMAND_REPLY_ERROR);
command_flush();
CHECK(command_frame_count() == count + 3U);
command_reply_at(0U, type, 7U, 0x51U, 1U, 6001U, value | 0x84U);
command_reply_at(1U, type, 7U, 0x51U, 1U, 6001U, value | 4U);
for (index = 0U; index < count; ++index) {
command_reply_at(index + 2U, type == 45U ? 1U : 3U, 11U, 0x51U, 1U, 1001U, value);
}
command_reply_at(count + 2U, type, 10U, 0x51U, 1U, 6001U, value | 4U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETED && finished.select_effect && finished.execute_effect);
CHECK(finished.request.receive_serial == execution.request.receive_serial);
CHECK(command_action_calls == 2U);
CHECK(!IEC104_Command_Get_Transaction(command, &execution, &info));
CHECK(IEC104_Command_Complete(command, &execution) == IEC104_COMMAND_REPLY_STALE);
command_empty(); command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:直接执行采用固定收尾:成功Complete回肯定TERM,明确失败Complete回否定TERM。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:线上的P/N与FINISHED中的已知执行结果一致,不再提供关闭终止帧的配置。
*/
static void test_command_result_policies(void)
{
unsigned success;
for (success = 0U; success < 2U; ++success) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config);
command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
event = command_execute(45U, 1U);
CHECK(!event.request.selected);
finished = command_done(&event, success ? IEC104_COMMAND_RESULT_SUCCEEDED : IEC104_COMMAND_RESULT_FAILED);
CHECK(finished.execute_effect && !finished.select_effect);
CHECK(command_frame_count() == 2U && command_action_calls == 1U);
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 45U, success ? 10U : 0x4aU, 0x51U, 1U, 6001U, 1U);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.negative_submitted == (success ? 0U : 1U));
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:选择与执行各覆盖NOT_STARTED、STARTED、FAILED,检查确认、原因及副作用标志。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:Accept的OK只代表事件消费;再次Accept不能重复动作。无返校动作的NULL选择
* 可以生效,但不得错误标记为曾产生物理选择副作用。
*/
static void test_command_action_outcomes(void)
{
unsigned selected, outcome;
for (selected = 0U; selected < 2U; ++selected) {
for (outcome = 0U; outcome < 3U; ++outcome) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config);
if (!selected) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, 6001U, selected ? 0x81U : 1U);
event = command_take(selected ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
command_action_result = (IEC104_CommandActionResult_t)outcome;
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_action_calls == 1U);
command_flush();
command_reply_at(0U, 45U, outcome == IEC104_COMMAND_ACTION_STARTED ? 7U : 0x47U,
0x51U, 1U, 6001U, selected ? 0x81U : 1U);
if (outcome == IEC104_COMMAND_ACTION_STARTED) {
IEC104_Command_Cancel(command);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_CANCELLED);
} else {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (outcome == IEC104_COMMAND_ACTION_FAILED ?
IEC104_COMMAND_FINISH_ACTION_FAILED : IEC104_COMMAND_FINISH_REJECTED));
}
CHECK(finished.select_effect == (selected && outcome != IEC104_COMMAND_ACTION_NOT_STARTED));
CHECK(finished.execute_effect == (!selected && outcome != IEC104_COMMAND_ACTION_NOT_STARTED));
command_close();
}
}
{
IEC104_CommandEvent_t event, finished;
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush(); IEC104_Command_Cancel(command);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(!finished.select_effect && command_action_calls == 0U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:事件身份和拒绝接口:分别篡改实例、事务、事件序号和类型,旧事件不得执行动作。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:Reject只能消费一次;ENDED仍可查询,槽回收后查询失败且输出不变,新事务也不能
* 重新接纳上一事务的令牌。
*/
static void test_command_rejection_and_stale(void)
{
IEC104_CommandEvent_t event, stale, finished;
IEC104_CommandTransactionInfo_t info, before;
unsigned index;
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
for (index = 0U; index < 4U; ++index) {
stale = event;
switch (index) {
case 0U: ++stale.instance_id; break;
case 1U: ++stale.transaction_id; break;
case 2U: ++stale.event_serial; break;
default: stale.type = IEC104_COMMAND_EXECUTE; break;
}
CHECK(IEC104_Command_Accept(command, &stale, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
}
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Reject(command, &event, (IEC104_CommandRejectCause_t)0) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Reject(command, &event, IEC104_COMMAND_REJECT_UNKNOWN_IOA) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Reject(command, &event, IEC104_COMMAND_REJECT_NEGATIVE) == IEC104_COMMAND_REPLY_STALE);
command_flush(); command_reply_at(0U, 45U, 0x6fU, 0x51U, 1U, 6001U, 0x81U);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && !info.active);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_REJECTED && command_action_calls == 0U);
memset(&info, 0x5a, sizeof(info)); before = info;
CHECK(!IEC104_Command_Get_Transaction(command, &event, &info));
CHECK(memcmp(&before, &info, sizeof(info)) == 0);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:选择已确认后逐项改变执行类型、状态、QU、OA,必须取消原选择并回显新请求。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:其他点的新选择只得到忙否定,原选择仍可正常执行;默认禁止无选择直接执行。
*/
static void test_command_matching_and_busy(void)
{
unsigned change;
for (change = 0U; change < 4U; ++change) {
IEC104_CommandEvent_t selected, finished;
unsigned type = change == 0U ? 46U : 45U;
unsigned value = change == 0U ? 2U : (change == 1U ? 0U : (change == 2U ? 5U : 1U));
command_open(NULL); selected = command_select(45U, 1U);
command_send(type, 6U, change == 3U ? 0x52U : 0x51U, 1U, 6001U, value);
command_flush();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH);
CHECK(finished.request.receive_serial == selected.request.receive_serial && finished.request.type_id == 45U);
command_reply_at(1U, type, 0x47U, change == 3U ? 0x52U : 0x51U, 1U, 6001U, value);
CHECK(command_action_calls == 1U); command_close();
}
{
IEC104_CommandEvent_t event;
command_open(NULL); (void)command_select(45U, 1U);
command_send(45U, 6U, 0x51U, 1U, 6002U, 0x81U); command_flush();
CHECK(command_status.busy && command_status.busy_rejected == 1U);
command_reply_at(1U, 45U, 0x47U, 0x51U, 1U, 6002U, 0x81U);
event = command_execute(45U, 1U); (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_close();
}
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U); command_flush();
CHECK(!command_status.busy); command_reply_at(0U, 45U, 0x47U, 0x51U, 1U, 6001U, 1U);
command_empty(); command_close();
++command_checks;
}
/* ============================================================================
* [COMMAND/DUPLICATE] 决定期间重复请求:非选择重发、期限、校验和身份边界
* ============================================================================ */
/**
* 功能:验证以下场景与约束:决定窗口内不触发重复选择复归的同OA激活仍静默:覆盖选择待决定、直接执行、选择后执行,
* 以及工作事件未取和已取两种情况。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:未取分支把原请求与重发放在同一批输入;
* 已取分支制造真实发送背压。SELECT_WAIT只注入执行;EXECUTE_WAIT同时注入选择和执行。
* 原工作身份、动作参数及决定期限保持不变,最终只出现原请求自己的确认。
*/
static void test_command_pending_same_point(void)
{
unsigned type, phase, taken;
for (type = 45U; type <= 46U; ++type) {
for (phase = 0U; phase < 3U; ++phase) {
for (taken = 0U; taken < 2U; ++taken) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t info;
uint8_t bytes[10];
unsigned state = type == 45U ? 1U : 2U;
unsigned select_bit = phase == 0U ? 0x80U : 0U;
unsigned prior = phase == 2U ? 1U : 0U;
uint64_t started, original_receive;
command_config_default(&config);
config.decision_timeout_ms = 100U;
if (phase == 1U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
if (phase == 2U) { (void)command_select(type, state); }
started = fake.now;
command_bytes(bytes, type, 6U, 0x51U, 1U, 6001U, state | select_bit);
command_queue(bytes, sizeof(bytes));
if (taken != 0U) {
command_poll();
event = command_take(phase == 0U ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
command_congest();
}
bytes[9] &= 0x7fU;
command_queue(bytes, sizeof(bytes)); command_flush();
original_receive = command_last_input.transport.data.i_frame.receive_serial - 1U;
fake.now += 5U;
command_send(type, 6U, 0x51U, 1U, 6001U, state | (select_bit ^ 0x80U));
command_send(type, 6U, 0x51U, 1U, 6001U, type == 45U ? 0U : 1U);
command_send(type, 6U, 0x51U, 1U, 6001U, state | 4U);
command_send(type == 45U ? 46U : 45U, 6U, 0x51U, 1U, 6001U,
type == 45U ? 2U : 1U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_HANDLED);
command_flush();
CHECK(command_status.accepted == prior + 1U && command_status.dropped_requests == 5U);
CHECK(command_status.rejected == 0U && command_status.busy_rejected == 0U &&
command_status.overloads == 0U && command_status.negative_submitted == 0U);
CHECK(command_status.pending_replies == 0U && command_status.pending_events == (taken == 0U ? 1U : 0U));
CHECK(command_status.timer_active && command_status.timeout_remaining_ms == 95U);
CHECK(command_frame_count() == prior && command_action_calls == prior);
if (taken == 0U) { event = command_take(phase == 0U ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE); }
CHECK(event.request.type_id == type && event.request.originator_address == 0x51U &&
event.request.state == state && event.request.qualifier == 0U &&
event.request.is_select == (phase == 0U) && event.request.selected == (phase == 2U));
CHECK(event.request.accepted_ms == started && event.request.receive_serial == original_receive);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.active &&
info.phase == (phase == 0U ? IEC104_COMMAND_PHASE_SELECT_WAIT : IEC104_COMMAND_PHASE_EXECUTE_WAIT));
command_empty();
fake.send_blocked = 0U; command_flush();
CHECK(command_frame_count() == prior);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
CHECK(command_action_calls == prior + 1U && command_frame_count() == prior + 1U);
CHECK(command_action_request.receive_serial == original_receive &&
command_action_request.state == state && command_action_request.qualifier == 0U);
command_reply_at(prior, type, 7U, 0x51U, 1U, 6001U, state | select_bit);
if (phase != 0U) { (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED); }
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:不触发重复选择的异OA同点激活必须否定回显,不能吞成原请求的重发。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:覆盖SELECT_WAIT、直接执行及选择后执行的EXECUTE_WAIT,以及真实HIGH背压下的WAIT重试。
* 原工作事件留到拒绝之后才取,验证新请求不能替换事件、动作参数或决定期限;同OA仍静默。
*/
static void test_command_pending_other_originator(void)
{
unsigned phase, blocked;
for (phase = 0U; phase < 3U; ++phase) {
for (blocked = 0U; blocked < 2U; ++blocked) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t info;
const unsigned prior = phase == 2U ? 1U : 0U;
const unsigned value = phase == 0U ? 0x81U : 1U;
const unsigned other_value = phase == 0U ? 6U : 0x86U;
uint64_t started, original_receive;
command_config_default(&config);
config.decision_timeout_ms = 100U;
if (phase == 1U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
if (phase == 2U) { (void)command_select(45U, 1U); }
started = fake.now;
command_send(45U, 6U, 0x51U, 1U, 6001U, value);
original_receive = command_last_input.transport.data.i_frame.receive_serial;
if (blocked != 0U) { command_congest(); }
fake.now += 5U;
/* 同时改变Type、OA和命令字段;否定必须逐字节对应这个新请求。 */
command_send(46U, 6U, 0x52U, 1U, 6001U, other_value);
command_send(45U, 6U, 0x51U, 1U, 6001U, value & 0x7fU);
command_flush();
CHECK(command_status.accepted == prior + 1U && command_status.rejected == 1U &&
command_status.busy_rejected == 1U && command_status.dropped_requests == 1U);
CHECK(command_status.overloads == 0U && command_status.pending_events == 1U &&
command_status.timer_active && command_status.timeout_remaining_ms == 95U);
CHECK(command_status.pending_replies == blocked &&
command_status.negative_submitted == (blocked == 0U ? 1U : 0U));
CHECK(command_frame_count() == prior + (blocked == 0U ? 1U : 0U) && command_action_calls == prior);
if (blocked != 0U) { CHECK(command_status.waits != 0U); }
event = command_take(phase == 0U ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
CHECK(event.request.type_id == 45U && event.request.originator_address == 0x51U &&
event.request.state == 1U && event.request.qualifier == 0U &&
event.request.is_select == (phase == 0U) && event.request.selected == (phase == 2U));
CHECK(event.request.accepted_ms == started && event.request.receive_serial == original_receive);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.active &&
info.phase == (phase == 0U ? IEC104_COMMAND_PHASE_SELECT_WAIT : IEC104_COMMAND_PHASE_EXECUTE_WAIT));
command_empty();
fake.send_blocked = 0U;
command_flush();
CHECK(command_frame_count() == prior + 1U && command_status.pending_replies == 0U &&
command_status.negative_submitted == 1U && command_status.timeout_remaining_ms == 95U);
command_reply_at(prior, 46U, 0x47U, 0x52U, 1U, 6001U, other_value);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_flush();
CHECK(command_action_calls == prior + 1U && command_frame_count() == prior + 2U &&
command_action_request.originator_address == 0x51U && command_action_request.receive_serial == original_receive);
command_reply_at(prior + 1U, 45U, 7U, 0x51U, 1U, 6001U, value);
if (phase != 0U) { (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED); }
command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:不同OA的同点执行忙否定复用独立短回复池:真实WAIT时填满,再来的只计丢新。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:满池既不能覆盖已排队否定,也不能覆盖原工作事件;解除背压后按各请求OA FIFO回显,
* 原SELECT/EXECUTE依旧能Accept一次,决定期限不因拒绝或过载续期。
*/
static void test_command_pending_other_originator_overload(void)
{
unsigned execute, index;
for (execute = 0U; execute < 2U; ++execute) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
const unsigned value = execute != 0U ? 1U : 0x81U;
uint64_t original_receive;
command_config_default(&config);
config.decision_timeout_ms = 100U;
if (execute != 0U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, 6001U, value);
original_receive = command_last_input.transport.data.i_frame.receive_serial;
command_congest();
fake.now += 5U;
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY + 2U; ++index) {
command_send(46U, 6U, 0x60U + index, 1U, 6001U, 2U | ((index & 3U) << 2));
}
command_flush();
CHECK(command_status.pending_replies == IEC104_COMMAND_REPLY_CAPACITY && command_status.waits != 0U);
CHECK(command_status.rejected == IEC104_COMMAND_REPLY_CAPACITY + 2U &&
command_status.busy_rejected == IEC104_COMMAND_REPLY_CAPACITY + 2U &&
command_status.overloads == 2U && command_status.dropped_requests == 2U);
CHECK(command_status.accepted == 1U && command_status.pending_events == 1U &&
command_status.timeout_remaining_ms == 95U && command_action_calls == 0U && command_frame_count() == 0U);
event = command_take(execute != 0U ? IEC104_COMMAND_EXECUTE : IEC104_COMMAND_SELECT);
CHECK(event.request.originator_address == 0x51U && event.request.type_id == 45U &&
event.request.receive_serial == original_receive && event.request.state == 1U && event.request.qualifier == 0U);
command_empty();
fake.send_blocked = 0U;
command_flush();
CHECK(command_status.pending_replies == 0U && command_status.timeout_remaining_ms == 95U &&
command_frame_count() == IEC104_COMMAND_REPLY_CAPACITY);
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY; ++index) {
command_reply_at(index, 46U, 0x47U, 0x60U + index, 1U, 6001U, 2U | ((index & 3U) << 2));
}
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_flush();
CHECK(command_action_calls == 1U && command_frame_count() == IEC104_COMMAND_REPLY_CAPACITY + 1U);
command_reply_at(IEC104_COMMAND_REPLY_CAPACITY, 45U, 7U, 0x51U, 1U, 6001U, value);
if (execute != 0U) { (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED); }
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:同OA的执行重发不能续期,也不能替换超时确认的回显来源。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:决定到期后只拒绝原请求;
* FINISHED仍按事务历史配对,选择曾交付时未读EXECUTE超时也须通知清理。
*/
static void test_command_pending_repeat_deadline(void)
{
unsigned phase, taken;
for (phase = 0U; phase < 3U; ++phase) {
for (taken = 0U; taken < 2U; ++taken) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
unsigned prior = phase == 2U ? 1U : 0U;
unsigned value = phase == 0U ? 0x82U : 2U;
uint64_t original_receive;
command_config_default(&config); config.decision_timeout_ms = 10U;
if (phase == 1U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
if (phase == 2U) { (void)command_select(46U, 2U); }
command_send(46U, 6U, 0x51U, 1U, 6001U, value);
original_receive = command_last_input.transport.data.i_frame.receive_serial;
if (taken != 0U) { event = command_take(phase == 0U ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE); }
fake.now += 9U; command_send(45U, 6U, 0x51U, 1U, 6001U, 5U); command_process();
CHECK(command_status.dropped_requests == 1U && command_status.timeout_remaining_ms == 1U);
CHECK(command_frame_count() == prior);
++fake.now; command_flush();
CHECK(command_status.decision_timeouts == 1U && command_action_calls == prior);
CHECK(command_frame_count() == prior + 1U);
command_reply_at(prior, 46U, 0x47U, 0x51U, 1U, 6001U, value);
if (taken != 0U || phase == 2U) {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DECISION_TIMEOUT &&
finished.request.receive_serial == original_receive && finished.request.type_id == 46U);
}
if (taken != 0U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
}
command_empty(); command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:静默策略只作用于通过校验的同OA同点同T的COT=6:禁用类型仍IGNORED,错误请求仍答错,
* TEST与其他IOA/站点均忙拒绝,合法真实撤销仍能终结原待决定事务且回显撤销请求。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_pending_repeat_boundaries(void)
{
static const unsigned invalid[][3] = {
{3U, 0x81U, 0x6dU}, {0x86U, 0x81U, 0xc7U},
{0x46U, 0x81U, 0x47U}, {6U, 0x83U, 0x47U}
};
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC104_Station_t other = {0};
unsigned index;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(45U);
command_open(&config);
CHECK(IEC104_Slave_Add_Station(command_slave, &other, 2U, &other) == IEC104_STATION_OK);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U); event = command_take(IEC104_COMMAND_SELECT);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0U);
command_send(46U, 6U, 0x51U, 1U, 6001U, 0x82U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
for (index = 0U; index < sizeof(invalid) / sizeof(invalid[0]); ++index) {
command_send(45U, invalid[index][0], 0x51U, 1U, 6001U, invalid[index][1]); command_flush();
command_reply_at(index, 45U, invalid[index][2], 0x51U, 1U, 6001U, invalid[index][1]);
}
command_send(45U, 6U, 0x51U, 1U, 6002U, 0x81U); command_flush();
command_reply_at(index++, 45U, 0x47U, 0x51U, 1U, 6002U, 0x81U);
command_send(45U, 6U, 0x51U, 2U, 6001U, 0x81U); command_flush();
command_reply_at(index++, 45U, 0x47U, 0x51U, 2U, 6001U, 0x81U);
CHECK(command_status.dropped_requests == 1U && command_status.busy_rejected == 3U &&
command_status.rejected == 6U && command_status.overloads == 0U && command_action_calls == 0U);
command_send(45U, 8U, 0x51U, 1U, 6001U, 0U); command_flush();
command_reply_at(index++, 45U, 9U, 0x51U, 1U, 6001U, 0U);
CHECK(command_frame_count() == index && command_status.dropped_requests == 1U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DEACTIVATED &&
finished.request.receive_serial == event.request.receive_serial);
CHECK(IEC104_Slave_Remove_Station(command_slave, &other) == IEC104_STATION_OK);
command_empty(); command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:同点依据原注册身份及会话。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:删除后未知CA仍回46,同CA替代节点仍属其他点并忙拒绝。
* STOP/断线终结待决定事务后,相同原文可建立新事务;此前被丢弃的请求不得留下回复或动作。
*/
static void test_command_pending_repeat_lifetime(void)
{
IEC104_CommandEvent_t event, next, finished;
IEC104_Station_t replacement = {0};
unsigned mode;
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Slave_Remove_Station(command_slave, &command_station) == IEC104_STATION_OK);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U); command_flush();
command_reply_at(0U, 45U, 0x6eU, 0x51U, 1U, 6001U, 0x81U);
CHECK(IEC104_Slave_Add_Station(command_slave, &replacement, 1U, &replacement) == IEC104_STATION_OK);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U); command_flush();
command_reply_at(1U, 45U, 0x47U, 0x51U, 1U, 6001U, 0x81U);
CHECK(command_status.dropped_requests == 0U && command_status.busy_rejected == 1U);
IEC104_Command_Cancel(command); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_CANCELLED &&
finished.request.station_id == event.request.station_id && IEC104_Station_Is_Reusable(&command_station));
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U); next = command_take(IEC104_COMMAND_SELECT);
CHECK(next.request.station_id != event.request.station_id && next.transaction_id != event.transaction_id);
CHECK(IEC104_Command_Accept(command, &next, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush(); command_reply_at(2U, 45U, 7U, 0x51U, 1U, 6001U, 0x81U);
CHECK(command_action_calls == 1U && command_frame_count() == 3U);
CHECK(IEC104_Slave_Remove_Station(command_slave, &replacement) == IEC104_STATION_OK);
command_close(); CHECK(IEC104_Station_Is_Reusable(&replacement));
for (mode = 0U; mode < 2U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_SlaveEvent_t old_input;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); command_send(46U, 6U, 0x51U, 1U, 6001U, 2U);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Slave_Copy_Event(&old_input, &command_last_input));
command_send(46U, 6U, 0x51U, 1U, 6001U, 2U);
if (mode == 0U) { push(stop_act, sizeof(stop_act)); }
else { fake.recv_eof = 1U; }
command_poll(); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (mode == 0U ? IEC104_COMMAND_FINISH_STOPPED : IEC104_COMMAND_FINISH_DISCONNECTED));
CHECK(command_action_calls == 0U && command_frame_count() == 0U);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
if (mode == 0U) { push(start_act, sizeof(start_act)); command_poll(); }
else {
fake.recv_eof = 0U; command_connect();
CHECK(IEC104_Command_Handle_Event(command, &old_input) == IEC104_COMMAND_EVENT_IGNORED);
}
command_send(46U, 6U, 0x51U, 1U, 6001U, 2U); next = command_take(IEC104_COMMAND_EXECUTE);
CHECK(next.transaction_id != event.transaction_id);
CHECK((next.request.generation == event.request.generation) == (mode == 0U));
CHECK(IEC104_Command_Accept(command, &next, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&next, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && command_frame_count() == 2U && command_status.dropped_requests == 1U);
command_reply_at(0U, 46U, 7U, 0x51U, 1U, 6001U, 2U);
command_empty(); command_close();
}
++command_checks;
}
/* ============================================================================
* [COMMAND/LIFETIME] 事务生命周期:FINISHED、撤销、期限、FIFO、会话和站点
* ============================================================================ */
/**
* 功能:验证以下场景与约束:FINISHED配对按事务历史判断:未读SELECT、未读直接EXECUTE不欠终结通知。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:SELECT曾交付后,即使匹配的EXECUTE尚未读,撤销也必须恰好交付一次FINISHED。
* 撤销不要求命令值相同,DEACT_CON应回显撤销请求自身的值。
*/
static void test_command_finished_pairing(void)
{
unsigned mode;
for (mode = 0U; mode < 4U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config);
if (mode == 1U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
if (mode >= 2U) {
event = command_select(45U, 1U);
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
} else {
command_send(45U, 6U, 0x51U, 1U, 6001U, mode == 0U ? 0x81U : 1U);
}
if (mode == 3U) { event = command_take(IEC104_COMMAND_EXECUTE); }
command_send(45U, 8U, 0x51U, 1U, 6001U, 0U); command_flush();
if (mode < 2U) { command_empty(); }
else {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DEACTIVATED);
CHECK(finished.transaction_id == event.transaction_id && finished.select_effect);
command_empty();
}
command_reply_at(mode >= 2U ? 1U : 0U, 45U, 9U, 0x51U, 1U, 6001U, 0U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:HIGH不足时选择动作尚未执行。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:同OA同点重复激活继续按决定阶段规则静默丢弃。
* 发送恢复后再Accept,确认接手后匹配执行应成功。
*/
static void test_command_unsubmitted_selection(void)
{
IEC104_CommandEvent_t selected, executed;
IEC104_CommandTransactionInfo_t info;
command_open(NULL);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
selected = command_take(IEC104_COMMAND_SELECT); command_congest();
CHECK(IEC104_Command_Accept(command, &selected, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
command_process();
CHECK(IEC104_Command_Get_Transaction(command, &selected, &info) && !info.confirm_submitted);
CHECK(command_action_calls == 0U && command_status.pending_replies == 0U);
/* 尚未执行选择;同OA同点不同类型激活也不能抢先产生否定确认。 */
command_send(46U, 6U, 0x51U, 1U, 6001U, 2U);
CHECK(IEC104_Command_Get_Transaction(command, &selected, &info) && info.active);
CHECK(info.phase == IEC104_COMMAND_PHASE_SELECT_WAIT);
fake.send_blocked = 0U; command_flush();
CHECK(command_status.mismatches == 0U && command_status.busy_rejected == 0U);
CHECK(command_status.dropped_requests == 1U && command_status.confirm_room_waits == 1U);
CHECK(IEC104_Command_Accept(command, &selected, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 0x81U);
CHECK(command_frame_count() == 1U);
executed = command_execute(45U, 1U);
(void)command_done(&executed, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 2U);
command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:应用结束与槽释放分离:先读取撤销FINISHED,但DEACT_CON仍被HIGH背压阻塞。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:事务必须继续占槽,Destroy失败,新选择忙拒绝;确认交接后才允许释放。
*/
static void test_command_deactivation_wait(void)
{
IEC104_CommandEvent_t event, finished;
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT); command_congest();
command_send(45U, 8U, 0x51U, 1U, 6001U, 0U); command_process();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DEACTIVATED);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.busy && command_status.pending_replies != 0U);
CHECK(!IEC104_Command_Destroy(command));
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_send(45U, 6U, 0x52U, 1U, 6002U, 0x81U);
fake.send_blocked = 0U; command_flush();
CHECK(!command_status.busy);
command_reply_at(0U, 45U, 9U, 0x51U, 1U, 6001U, 0U);
command_reply_at(1U, 45U, 0x47U, 0x52U, 1U, 6002U, 0x81U);
command_empty(); command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:决定、选择、执行三种期限分别验证到期前、恰好到期和单调时间倒退。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:不先调用Process,而由Accept、Handle_Event、Report_Result或NextEvent触发检查,
* 防止过期状态被新的操作复活;关闭持续时长限制仍不能掩盖时钟倒退。
*/
static void test_command_deadlines(void)
{
unsigned phase, boundary;
for (phase = 0U; phase < 3U; ++phase) {
for (boundary = 0U; boundary < 3U; ++boundary) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint64_t started;
command_config_default(&config);
config.decision_timeout_ms = config.select_timeout_ms = config.execute_timeout_ms = 10U;
command_open(&config);
if (phase == 0U) {
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
} else {
event = command_select(45U, 1U);
if (phase == 2U) { event = command_execute(45U, 1U); command_flush(); }
}
started = fake.now;
if (phase == 1U) {
uint8_t bytes[10];
command_bytes(bytes, 45U, 6U, 0x51U, 1U, 6001U, 1U);
command_queue(bytes, sizeof(bytes));
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
}
/* 传输接收时保持正常时间,再改变业务受理时刻,隔离链路计时的影响。 */
fake.now = boundary == 2U ? started - 1U : started + 9U + boundary;
if (phase == 0U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) ==
(boundary == 0U ? IEC104_COMMAND_REPLY_OK : IEC104_COMMAND_REPLY_STALE));
} else if (phase == 1U) {
command_drain_transport();
} else {
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) ==
(boundary == 0U ? IEC104_COMMAND_REPLY_OK : IEC104_COMMAND_REPLY_STALE));
}
if (boundary != 0U) {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (phase == 0U ? IEC104_COMMAND_FINISH_DECISION_TIMEOUT :
(phase == 1U ? IEC104_COMMAND_FINISH_SELECT_TIMEOUT : IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT)));
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.clock_errors == (boundary == 2U ? 1U : 0U));
} else if (phase == 1U) {
event = command_take(IEC104_COMMAND_EXECUTE); CHECK(event.request.selected);
}
fake.now = started + 10U;
command_close();
}
}
/* 期限配置0仅关闭持续时长限制;单调时钟倒退仍须终结并累计clock_errors。 */
for (phase = 0U; phase < 2U; ++phase) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config);
command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.decision_timeout_ms = config.execute_timeout_ms = 0U;
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
if (phase != 0U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
--fake.now;
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.timer_active && command_status.need_process);
if (phase == 0U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_action_calls == 0U);
} else {
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
}
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (phase == 0U ? IEC104_COMMAND_FINISH_DECISION_TIMEOUT : IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT));
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.clock_errors == 1U);
++fake.now;
command_close();
}
/* SELECT已经交付,即使后续EXECUTE尚未读取,决定超时仍欠应用一次FINISHED。 */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
command_config_default(&config); config.decision_timeout_ms = 10U;
command_open(&config); (void)command_select(45U, 1U);
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U); fake.now += 10U;
event = command_take(IEC104_COMMAND_FINISHED);
CHECK(event.reason == IEC104_COMMAND_FINISH_DECISION_TIMEOUT);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:确认已接手但k窗口未打开时仍可先报告结果,停止执行计时。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:返回信息同样可先入HIGH,随后逐个ACK推进确认、返回信息、终止。
*/
static void test_command_result_before_confirm(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC104_CommandTransactionInfo_t info;
IEC60870_Asdu_t data;
uint8_t bytes[10];
uint8_t filler[] = {200U, 0U, 3U, 0U, 1U, 0U};
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); config.execute_timeout_ms = 10U;
command_window = 1U; command_open(&config);
CHECK(IEC60870_Asdu_Create_From_Buffer(&data, IEC104_Slave_Get_Application_Layer_Parameters(command_slave),
filler, sizeof(filler)) == IEC60870_ASDU_OK);
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &data) == IEC104_TCP_SUBMIT_OK);
command_poll(); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_process();
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.confirm_submitted);
CHECK(command_frame_count() == 0U);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
fake.now += 100U; command_process();
CHECK(command_status.execute_timeouts == 0U && command_action_calls == 1U);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
push_ack(1U); command_poll(); command_flush();
push_ack(2U); command_poll(); command_flush();
push_ack(3U); command_poll();
command_flush(); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETED && command_frame_count() == 3U);
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
command_reply_at(2U, 45U, 10U, 0x51U, 1U, 6001U, 1U);
command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:全局FIFO与独立应答满池:连续忙请求填满固定槽,多出的新请求丢弃并计数。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:只排空传输缓冲而不推进Command时,Submit_Return_Info仍须等待模块内旧应答,
* 证明返回信息不能绕过业务FIFO;解除后按原请求OA顺序回包。
*/
static void test_command_fifo_and_overload(void)
{
IEC104_CommandEvent_t event;
IEC60870_Asdu_t data;
uint8_t bytes[10];
unsigned index;
command_open(NULL); (void)command_select(45U, 1U); event = command_execute(45U, 1U); command_flush();
command_congest();
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY + 2U; ++index) {
command_send(45U, 6U, 0x60U + index, 1U, 6002U, 0x81U);
}
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.pending_replies == IEC104_COMMAND_REPLY_CAPACITY);
CHECK(command_status.overloads == 2U && command_status.dropped_requests == 2U);
fake.send_blocked = 0U;
/* 只推进TCP,保留Command队首;底层空闲不能成为返回信息插队的理由。 */
for (index = 0U; index < 16U; ++index) { command_poll(); }
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_WAIT);
command_flush();
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
command_poll(); (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY; ++index) {
command_reply_at(index + 2U, 45U, 0x47U, 0x60U + index, 1U, 6002U, 0x81U);
}
command_reply_at(IEC104_COMMAND_REPLY_CAPACITY + 2U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:返回信息必须完整验证类型、COT、P/N、TEST、CA、对象品质和精确长度。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:错误ASDU不得接手;合法的COT=3及与遥控点不同的监视IOA应可提交。
*/
static void test_command_return_validation(void)
{
IEC104_CommandEvent_t event;
IEC60870_Asdu_t object;
uint8_t bytes[11];
unsigned index;
command_open(NULL); (void)command_select(45U, 1U); event = command_execute(45U, 1U); command_flush();
for (index = 0U; index < 7U; ++index) {
size_t length = 10U;
command_bytes(bytes, 1U, 11U, 0x51U, 1U, 1001U, 1U);
switch (index) {
case 0U: bytes[0] = 45U; break;
case 1U: bytes[2] = 5U; break;
case 2U: bytes[2] |= 0x40U; break;
case 3U: bytes[2] |= 0x80U; break;
case 4U: bytes[4] = 2U; break;
case 5U: bytes[9] = 2U; break;
default: bytes[10] = 1U; length = 11U; break;
}
CHECK(IEC60870_Asdu_Create_From_Buffer(&object,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, length) == IEC60870_ASDU_OK);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &object) == IEC104_COMMAND_REPLY_ERROR);
}
command_bytes(bytes, 1U, 3U, 0x51U, 1U, 9876U, 0U);
CHECK(IEC60870_Asdu_Create_From_Buffer(&object,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, 10U) == IEC60870_ASDU_OK);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &object) == IEC104_COMMAND_REPLY_OK);
command_poll(); (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_reply_at(2U, 1U, 3U, 0x51U, 1U, 9876U, 0U);
command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:STOP/START通知优先于同一时刻的决定超时:未读通知时Accept只返回WAIT且不动作。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:按FIFO消费后,旧事务原因保持STOPPED,重新激活也不能复活旧事件。
*/
static void test_command_stop_notifications(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config); config.decision_timeout_ms = 10U;
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
fake.now += 10U;
push(stop_act, sizeof(stop_act)); push(start_act, sizeof(start_act));
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
command_process(); CHECK(command_status.events_pending && command_status.decision_timeouts == 0U);
command_drain_transport();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_STOPPED && command_action_calls == 0U);
command_empty(); CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.active);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:执行中断线不回滚动作,但必须交付DISCONNECTED并拒绝迟到结果。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:重连后的同点新事务具有新连接代次与事务号,旧令牌不能作用于新输出。
*/
static void test_command_disconnect_and_reuse(void)
{
IEC104_CommandEvent_t event, next, finished;
IEC104_CommandConfig_t config;
uint64_t generation;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); event = command_execute(45U, 1U); command_flush();
generation = event.request.generation;
fake.recv_eof = 1U; command_poll();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DISCONNECTED && finished.execute_effect);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
command_empty(); fake.recv_eof = 0U; command_connect();
next = command_execute(45U, 1U);
CHECK(next.request.generation != generation && next.transaction_id != event.transaction_id);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
(void)command_done(&next, IEC104_COMMAND_RESULT_SUCCEEDED); command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:原站点删除、同CA重新注册时,已受理事务仍持原节点,不能匹配替代站点的执行。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:应用在工作事件阶段额外Acquire,保证FINISHED出队并释放模块引用之后仍能清理;
* 最后一次应用Release才允许旧节点复用。
*/
static void test_command_station_lifetime(void)
{
IEC104_CommandEvent_t event, finished;
IEC104_Station_t replacement = {0};
uint64_t original;
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT); original = event.request.station_id;
CHECK(IEC104_Slave_Acquire_Station(command_slave, &command_station) == IEC104_STATION_OK);
CHECK(IEC104_Slave_Remove_Station(command_slave, &command_station) == IEC104_STATION_OK);
CHECK(!IEC104_Station_Is_Reusable(&command_station));
CHECK(IEC104_Slave_Add_Station(command_slave, &replacement, 1U, &replacement) == IEC104_STATION_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush(); CHECK(command_action_request.station_id == original);
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U); command_flush();
CHECK(command_action_calls == 1U && command_status.busy);
command_reply_at(1U, 45U, 0x47U, 0x51U, 1U, 6001U, 1U);
IEC104_Command_Cancel(command); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_CANCELLED && finished.request.station_id == original);
CHECK(IEC104_Slave_Get_Station_By_Id(command_slave, original) == &command_station);
CHECK(IEC104_Slave_Release_Station(command_slave, &command_station) == IEC104_STATION_OK);
CHECK(IEC104_Station_Is_Reusable(&command_station));
CHECK(IEC104_Slave_Remove_Station(command_slave, &replacement) == IEC104_STATION_OK);
command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:EXECUTE必须提供动作。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:回调重入受测入口不会改变当前事务或触发第二次动作。
*/
static void test_command_reentry(void)
{
IEC104_CommandConfig_t config;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
command_reentry_event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Accept(command, &command_reentry_event, NULL, NULL) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Accept(command, &command_reentry_event, command_reentry_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(command_action_calls == 1U);
(void)command_done(&command_reentry_event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_close(); ++command_checks;
}
/**
* 功能:验证以下场景与约束:将k缩为1,逐帧给出真实主站ACK:选择确认、执行确认、返回信息各占一个窗口。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:HIGH有空间即可接手返回信息,Complete可关闭阶段但TERM仍等待窗口,
* 最后交接TERM后才交付完成通知,随后按统一生命周期释放资源。
*/
static void test_command_window_and_cancel(void)
{
IEC104_CommandEvent_t event;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_window = 1U; command_open(NULL); (void)command_select(45U, 1U);
event = command_execute(45U, 1U); command_process();
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process(); command_empty(); CHECK(command_frame_count() == 1U);
push_ack(1U); command_poll(); command_flush();
command_empty(); CHECK(command_frame_count() == 2U);
push_ack(2U); command_poll(); command_flush();
command_flush(); CHECK(command_frame_count() == 3U);
command_empty();
push_ack(3U); command_poll(); command_flush();
CHECK(command_take(IEC104_COMMAND_FINISHED).reason == IEC104_COMMAND_FINISH_COMPLETED);
CHECK(command_frame_count() == 4U); command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:TCP容量至少为2时创建第二个独立Slave,验证实例池可分配且事件归属不串服务。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:外来Slave通知被拒绝,外来工作事件不能触发动作;容量1构建仍保留其余测试。
*/
static void test_command_two_services(void)
{
#if IEC104_TCP_SERVER_CAPACITY >= 2
IEC104_Slave_t *other;
IEC104_Command_t *other_command;
IEC104_CommandConfig_t config;
IEC104_SlaveConfig_t service_config;
IEC104_CommandEvent_t event;
command_open(NULL);
other = IEC104_Slave_Create(); CHECK(other != NULL);
test_transport_config(&service_config);
CHECK(IEC104_Slave_Init(other, &service_config, NULL) == IEC104_TCP_OK);
command_config_default(&config);
other_command = IEC104_Command_Create(other, &config); CHECK(other_command != NULL);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Handle_Event(other_command, &command_last_input) == IEC104_COMMAND_EVENT_ERROR);
CHECK(IEC104_Command_Accept(other_command, &event, command_action, &command_action_calls) != IEC104_COMMAND_REPLY_OK);
CHECK(command_action_calls == 0U);
CHECK(IEC104_Command_Destroy(other_command));
CHECK(IEC104_Slave_Close(other, NULL) == IEC104_TCP_OK);
CHECK(IEC104_Slave_Destroy(other, NULL) == IEC104_TCP_OK);
command_close();
#endif
++command_checks;
}
/**
* 功能:对照NORMAL覆盖行为:先把一条返回信息入NORMAL,再以填充事件将其覆盖。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:返回信息立即由HIGH接手,不等待NORMAL排空;其原文不被覆盖。
*/
static void test_command_normal_overwrite(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC60870_Asdu_t object, filler;
uint8_t bytes[10], fill_bytes[249] = {200, 0, 3, 0, 1, 0};
unsigned index;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); event = command_execute(45U, 1U); command_flush();
command_return(&object, bytes, 1U, 0U);
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &object) == IEC104_TCP_SUBMIT_OK);
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), fill_bytes, sizeof(fill_bytes)) == IEC60870_ASDU_OK);
for (index = 0U; index < IEC104_TCP_NORMAL_CAPACITY / sizeof(fill_bytes) + 4U; ++index) {
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &filler) == IEC104_TCP_SUBMIT_OK);
}
command_return(&object, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &object) == IEC104_COMMAND_REPLY_OK);
CHECK(command_frame_count() == 2U);
command_flush();
/* NORMAL中的第一条返回信息(值0)已被覆盖;HIGH返回值1仍完整保留。 */
CHECK(command_frame_count() == 2U);
command_poll(); (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_frame_count() == 3U);
command_reply_at(1U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
command_close(); ++command_checks;
}
/**
* 功能:临时拒绝启停通知准入,让真实STOP停留在传输层,供提交入口再次处理。
* 输入参数:context——业务回调的测试上下文。
* 输出参数:无。
* 返回值:固定返回 0,拒绝本次启停通知准入。
* 说明:接口保留参数仍按回调签名传入,未使用的参数不参与本辅助函数的处理。
*/
static int command_hold_transfer(void *context) { (void)context; return 0; }
/**
* 功能:利用原传输观察者的背压入口保留STOP,随后恢复观察者。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:此时服务仍显示已启动且通知队列为空,下一次提交必须亲自处理STOP竞态。
*/
static void command_retain_stop(void)
{
IEC104_ConnectionTransferObserver_t observer = sut->connection.transfer_observer;
IEC104_TcpServerStatus_t status;
sut->connection.transfer_observer.can_accept = command_hold_transfer;
push(stop_act, sizeof(stop_act));
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
sut->connection.transfer_observer = observer;
CHECK(sut->rx_blocked && !sut->connection.receive_batch_pending);
CHECK(IEC104_Slave_Get_Status(command_slave, &status, NULL) == IEC104_TCP_OK);
CHECK(status.pending_event_count == 0U && status.connection.data_transfer_started);
}
/**
* 功能:先真实交付启停通知,再报告观察者失败,构造“已有STOP通知但结果为FATAL”。
* 输入参数:context——业务回调的测试上下文;state——状态值。
* 输出参数:无。
* 返回值:固定返回 0,在已交付通知后向传输层报告观察者失败。
* 说明:用于区分可以让出的正常STOP与必须立即终结的传输故障。
*/
static int command_fail_after_stop(void *context, IEC104_DataTransferState_t state)
{
CHECK(Notify_Transfer_State(context, state));
return 0;
}
/**
* 功能:验证以下场景与约束:容量和会话预检通过后,在动作之后的唯一确认提交入口内才受理STOP。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:正常STOP应等待通知再以STOPPED收尾;注入FATAL则立即记SEND_ERROR,不能降为WAIT。
* 两种路径都保留副作用事实,并拒绝迟到Report_Result。
*/
static void test_command_stop_during_submit(void)
{
unsigned fatal;
for (fatal = 0U; fatal < 2U; ++fatal) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
command_retain_stop();
if (fatal) { sut->connection.transfer_observer.notify = command_fail_after_stop; }
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Get_Status(command, &command_status));
if (fatal) {
/* 即使已有STOP通知,FATAL仍必须保留发送错误语义,不能当成普通让出。 */
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_SEND_ERROR);
sut->connection.transfer_observer.notify = Notify_Transfer_State;
command_drain_transport();
} else {
CHECK(command_status.events_pending && command_status.busy && !command_status.need_process);
command_drain_transport(); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_STOPPED);
}
CHECK(finished.execute_effect && command_action_calls == 1U);
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.pending_replies == 0U);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:无活动事务但仍有独立错误应答时,实例也不能销毁。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:Cancel必须清理该应答池;不会因不存在工作事件而凭空产生FINISHED。
*/
static void test_command_independent_reply_lifetime(void)
{
command_open(NULL);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
command_congest();
/* 取消尚未交付的事务,再制造独立错误应答,隔离应答池自身的生命周期。 */
IEC104_Command_Cancel(command);
command_send(45U, 6U, 0x61U, 2U, 6001U, 0x81U);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.busy && command_status.pending_replies == 1U);
CHECK(!IEC104_Command_Destroy(command));
IEC104_Command_Cancel(command);
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.pending_replies == 0U);
command_empty(); command_close(); ++command_checks;
}
/* ============================================================================
* [COMMAND/EXTENDED_HELPERS] 扩展命令辅助:47–51的值、原文请求与回显
* ============================================================================ */
/* 新增类型输入仍用独立的手工字节,不调用被测公共编码器生成期望结果。
* qualifier_offset为255表示没有限定词;状态类的状态位位于限定词中。 */
static const struct {
unsigned type;
size_t payload_length;
unsigned qualifier_offset;
uint8_t payload[5];
} command_value_cases[] = {
{47U, 1U, 0U, {0x0dU, 0U, 0U, 0U, 0U}},
{48U, 3U, 2U, {0xc7U, 0xcfU, 0U, 0U, 0U}},
{49U, 3U, 2U, {0U, 0x80U, 0U, 0U, 0U}},
{50U, 5U, 4U, {0U, 0U, 0xa0U, 0xbfU, 0U}},
{51U, 4U, 255U, {0xd5U, 0xaaU, 0x80U, 0xffU, 0U}}
};
/**
* 功能:拼接手工 ASDU 公共头与给定原始值载荷,返回生成报文的长度。
* 输入参数:bytes——报文字节缓冲区;type——协议类型或期望事件类型;cause——完整传送原因字节;oa——发起者地址;ca——公共地址;ioa——信息对象地址;payload——原始对象载荷(只读);payload_length——载荷字节数。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:生成报文或结果数据的实际字节长度。
* 说明:输出缓冲由用例提供至少32字节;当前控制族值载荷最多12字节。
* 只拼接手工公共头和调用者给定的原始载荷,供基础/时标命令共用。
*/
static size_t command_payload_bytes(uint8_t *bytes, unsigned type, unsigned cause,
unsigned oa, unsigned ca, unsigned ioa, const uint8_t *payload, size_t payload_length)
{
CHECK(payload != NULL && payload_length > 0U && payload_length <= 12U);
command_bytes(bytes, type, cause, oa, ca, ioa, 0U);
memcpy(bytes + 9U, payload, payload_length);
return 9U + payload_length;
}
/**
* 功能:接收一条任意长度的手工ASDU并分派通知,容许用例送入畸形长度以检查校验顺序。
* 输入参数:bytes——报文字节缓冲区(只读);length——字节长度。
* 输出参数:无。
* 返回值:无。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static void command_raw_send(const uint8_t *bytes, size_t length)
{
command_queue(bytes, length);
command_poll();
}
/**
* 功能:请求可含SQ或尾部错误,length由用例显式指定期望规范回复长度。
* 输入参数:index——待访问或核对的条目下标;request——当前业务请求(只读);length——字节长度;cause——完整传送原因字节。
* 输出参数:无。
* 返回值:无。
* 说明:完整比较保证没有截断浮点/时标,也没有携带上一条长回复的尾部。
*/
static void command_raw_reply_at(unsigned index, const uint8_t *request, size_t length, unsigned cause)
{
const uint8_t *frames[64];
size_t lengths[64];
uint8_t expected[32];
CHECK(length <= sizeof(expected));
CHECK(command_collect(frames, lengths) > index);
memcpy(expected, request, length);
expected[1] = 1U;
expected[2] = (uint8_t)cause;
CHECK(lengths[index] == length);
CHECK(memcmp(frames[index], expected, length) == 0);
}
/**
* 功能:对照固定输入值表检查应用事件的解码值及限定词。
* 输入参数:request——当前业务请求(只读);type——协议类型或期望事件类型。
* 输出参数:无。
* 返回值:无。
* 说明:比特串族没有S/E字段,必须保持直接执行。
*/
static void command_check_value(const IEC104_CommandRequest_t *request, unsigned type)
{
CHECK(request->type_id == type);
switch (type) {
case 47U: case 58U: case 60U: CHECK(request->state == 1U && request->qualifier == 3U); break;
case 59U: CHECK(request->state == 2U && request->qualifier == 3U); break;
case 48U: case 61U: CHECK(request->value.normalized == -12345 && request->qualifier == 0U); break;
case 49U: case 62U: CHECK(request->value.scaled == -32768 && request->qualifier == 0U); break;
case 50U: case 63U: CHECK(request->value.short_float == -1.25f && request->qualifier == 0U); break;
default:
CHECK((type == 51U || type == 64U) && request->value.bitstring == UINT32_C(0xff80aad5));
CHECK(request->qualifier == 0U && !request->is_select);
break;
}
}
/* ============================================================================
* [COMMAND/EXTENDED] 扩展命令用例:类型配置、流程、匹配、非法值与变长回复
* ============================================================================ */
/**
* 功能:验证以下场景与约束:可配置的类型与默认启用集合分开验证。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:无S/E的比特串不能要求先选择。
*/
static void test_command_extended_configuration(void)
{
static const IEC104_CommandPoint_t invalid = {1U, 6001U,
IEC104_COMMAND_TYPE_BIT(51U), IEC104_COMMAND_MODE_SELECT_EXECUTE};
IEC104_CommandConfig_t config;
unsigned type;
command_open(NULL);
CHECK(IEC104_Command_Destroy(command));
for (type = 47U; type <= 51U; ++type) {
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(type);
command_config_mode(&config, type == 51U ? IEC104_COMMAND_MODE_DIRECT : IEC104_COMMAND_MODE_SELECT_EXECUTE);
command = IEC104_Command_Create(command_slave, &config);
CHECK(command != NULL && IEC104_Command_Destroy(command));
}
config.points = &invalid;
config.point_count = 1U;
CHECK(IEC104_Command_Create(command_slave, &config) == NULL);
command_config_default(&config);
CHECK(config.type_mask == UINT16_C(0x3fff));
command = IEC104_Command_Create(command_slave, &config);
CHECK(command != NULL);
command_close();
++command_checks;
}
/**
* 功能:逐类型完成选择/执行/结果/终止。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:51直接执行,且首个值字节bit7=1不能误判选择。
* 应用改写事件副本的数值后,动作和FINISHED仍须取自模块保存的原请求。
*/
static void test_command_extended_flows(void)
{
size_t index;
for (index = 0U; index < sizeof(command_value_cases) / sizeof(command_value_cases[0]); ++index) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32], selected[32], payload[5];
size_t length;
unsigned type = command_value_cases[index].type;
unsigned preceding = type == 51U ? 0U : 1U;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(type);
command_config_mode(&config, type == 51U ? IEC104_COMMAND_MODE_DIRECT : IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&config);
memcpy(payload, command_value_cases[index].payload, sizeof(payload));
length = command_payload_bytes(bytes, type, 6U, 0x51U, 1U, 6001U,
payload, command_value_cases[index].payload_length);
if (type != 51U) {
memcpy(selected, bytes, length);
selected[9U + command_value_cases[index].qualifier_offset] |= 0x80U;
command_raw_send(selected, length);
event = command_take(IEC104_COMMAND_SELECT);
command_check_value(&event.request, type);
CHECK(event.request.is_select && !event.request.selected);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_raw_reply_at(0U, selected, length, 7U);
}
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_EXECUTE);
command_check_value(&event.request, type);
CHECK(!event.request.is_select && event.request.selected == (type != 51U));
event.request.value.bitstring = 0U;
event.request.state = 0U;
event.request.qualifier = 0U;
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(command_action_calls == 1U);
command_check_value(&command_action_request, type);
finished = command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_check_value(&finished.request, type);
command_raw_reply_at(preceding, bytes, length, 7U);
command_raw_reply_at(preceding + 1U, bytes, length, 10U);
CHECK(command_frame_count() == preceding + 2U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:改变值或合法QU必须结束原选择,否定回显本次执行。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:非法QL只独立拒绝。
* 短浮点的+0和-0作为不同线上位模式处理,避免改成浮点数值相等比较。
*/
static void test_command_extended_matching(void)
{
size_t index;
unsigned variant;
for (index = 0U; index < 4U; ++index) {
for (variant = 0U; variant < 2U; ++variant) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32];
size_t length;
unsigned type = command_value_cases[index].type;
unsigned qualifier = 9U + command_value_cases[index].qualifier_offset;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(type);
command_open(&config);
length = command_payload_bytes(executed, type, 6U, 0x51U, 1U, 6001U,
command_value_cases[index].payload, command_value_cases[index].payload_length);
memcpy(selected, executed, length);
selected[qualifier] |= 0x80U;
command_raw_send(selected, length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
if (variant == 0U) { executed[9] ^= type == 47U ? 3U : 1U; }
else { executed[qualifier] ^= type == 47U ? 4U : 1U; }
command_raw_send(executed, length);
command_flush();
if (variant != 0U && type != 47U) {
IEC104_CommandTransactionInfo_t info;
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.active &&
info.phase == IEC104_COMMAND_PHASE_SELECTED);
} else {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH);
command_check_value(&finished.request, type);
}
CHECK(command_action_calls == 0U);
command_raw_reply_at(1U, executed, length, 0x47U);
command_empty();
command_close();
}
}
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32];
static const uint8_t plus_zero[] = {0U, 0U, 0U, 0U, 0x80U};
static const uint8_t minus_zero[] = {0U, 0U, 0U, 0x80U, 0U};
size_t length;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(50U);
command_open(&config);
length = command_payload_bytes(selected, 50U, 6U, 0x51U, 1U, 6001U, plus_zero, sizeof(plus_zero));
command_raw_send(selected, length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
CHECK(command_payload_bytes(executed, 50U, 6U, 0x51U, 1U, 6001U, minus_zero, sizeof(minus_zero)) == length);
command_raw_send(executed, length);
command_flush();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH && command_action_calls == 0U);
command_raw_reply_at(1U, executed, length, 0x47U);
command_empty();
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:RCS保留状态和浮点NaN/Inf必须否定,完整非法值原样回显,不经合法对象重编码。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_extended_invalid_values(void)
{
static const uint8_t values[][5] = {
{0U, 0U, 0U, 0U, 0U}, {3U, 0U, 0U, 0U, 0U},
{0U, 0U, 0x80U, 0x7fU, 0x55U}, {0U, 0U, 0x80U, 0xffU, 0x55U},
{0U, 0U, 0xc0U, 0x7fU, 0x55U}, {1U, 0U, 0x80U, 0x7fU, 0x55U}
};
size_t index;
for (index = 0U; index < sizeof(values) / sizeof(values[0]); ++index) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
unsigned type = index < 2U ? 47U : 50U;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(type);
command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
length = command_payload_bytes(bytes, type, 6U, 0x61U, 1U, 6001U, values[index], index < 2U ? 1U : 5U);
command_raw_send(bytes, length);
command_flush();
CHECK(command_frame_count() == 1U && command_action_calls == 0U);
command_raw_reply_at(0U, bytes, length, 0x47U);
CHECK(command_status.rejected == 1U && !command_status.busy);
command_empty();
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:每种新增长度都检查COT/CA/IOA优先级、SQ/尾部规范回复和缺失完整对象时丢弃。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:TEST/P/N只改变应答原因及标志,不得改变原命令值或启动动作。
*/
static void test_command_extended_wire_validation(void)
{
size_t index;
unsigned variant;
for (index = 0U; index < sizeof(command_value_cases) / sizeof(command_value_cases[0]); ++index) {
for (variant = 0U; variant < 10U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length, received;
unsigned expected = 0x47U;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(command_value_cases[index].type);
command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
length = command_payload_bytes(bytes, command_value_cases[index].type, 6U, 0x62U, 1U, 6001U,
command_value_cases[index].payload, command_value_cases[index].payload_length);
received = length;
switch (variant) {
case 0U: bytes[2] = 5U; bytes[4] = 2U; bytes[6] = bytes[7] = bytes[8] = 0U; expected = 0x6dU; break;
case 1U: bytes[4] = 2U; bytes[6] = bytes[7] = bytes[8] = 0U; expected = 0x6eU; break;
case 2U: bytes[6] = bytes[7] = bytes[8] = 0U; expected = 0x6fU; break;
case 3U: bytes[1] = 0x81U; break;
case 4U: bytes[received++] = 0xa5U; break;
case 5U: bytes[2] |= 0x40U; break;
case 6U: bytes[2] |= 0x80U; expected = 0x87U; break;
case 7U: --received; expected = 0U; break;
case 8U: bytes[1] = 0U; expected = 0U; break;
default:
memcpy(bytes + length, bytes + 6U, length - 6U);
received += length - 6U;
bytes[1] = 2U;
expected = 0U;
break;
}
command_raw_send(bytes, received);
command_flush();
CHECK(command_frame_count() == (expected == 0U ? 0U : (variant == 6U ? 2U : 1U)));
if (expected != 0U) { command_raw_reply_at(0U, bytes, length, expected); }
if (variant == 6U) { command_raw_reply_at(1U, bytes, length, 0x8aU); }
CHECK(command_action_calls == 0U && !command_status.busy);
command_empty();
command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:长请求被另一类型同点执行终结时,回复长度属于新请求,FINISHED仍属于原选择。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:等待发送期间请求、事务回复及独立回复池同时持有不同长度的原文。
*/
static void test_command_extended_reply_lengths(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32], short_reply[32];
static const uint8_t selected_payload[] = {0U, 0U, 0xa0U, 0xbfU, 0x80U};
static const uint8_t executed_payload[] = {0U, 0x80U, 0U};
static const uint8_t short_payload[] = {0x81U};
size_t selected_length, executed_length, short_length;
command_config_default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(45U) | IEC104_COMMAND_TYPE_BIT(49U) | IEC104_COMMAND_TYPE_BIT(50U);
command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&config);
selected_length = command_payload_bytes(selected, 50U, 6U, 0x51U, 1U, 6001U,
selected_payload, sizeof(selected_payload));
command_raw_send(selected, selected_length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_congest();
short_length = command_payload_bytes(short_reply, 45U, 6U, 0x61U, 1U, 6002U,
short_payload, sizeof(short_payload));
command_raw_send(short_reply, short_length);
executed_length = command_payload_bytes(executed, 49U, 6U, 0x51U, 1U, 6001U,
executed_payload, sizeof(executed_payload));
command_raw_send(executed, executed_length);
command_process();
CHECK(command_status.pending_replies == 2U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH);
CHECK(finished.request.type_id == 50U && finished.request.value.short_float == -1.25f);
CHECK(command_action_calls == 0U);
fake.send_blocked = 0U;
command_flush();
command_raw_reply_at(0U, selected, selected_length, 7U);
command_raw_reply_at(1U, short_reply, short_length, 0x47U);
command_raw_reply_at(2U, executed, executed_length, 0x47U);
CHECK(command_frame_count() == 3U && !command_status.busy);
command_empty();
command_close();
++command_checks;
}
/* ============================================================================
* [COMMAND/TIMED_HELPERS] 带时标辅助:58–64请求及CP56/基准配置
* ============================================================================ */
/* UTC 2026-01-01 00:00:00.000;日期和期望纪元毫秒独立固定,不用被测转换器算期望。 */
static const uint64_t command_epoch_ms = UINT64_C(1767225600000);
static const uint8_t command_epoch_time[7] = {0U, 0U, 0U, 0U, 1U, 1U, 26U};
static const struct {
unsigned type;
size_t payload_length;
unsigned qualifier_offset;
uint8_t payload[5];
} command_timed_cases[] = {
{58U, 1U, 0U, {0x0dU, 0U, 0U, 0U, 0U}},
{59U, 1U, 0U, {0x0eU, 0U, 0U, 0U, 0U}},
{60U, 1U, 0U, {0x0dU, 0U, 0U, 0U, 0U}},
{61U, 3U, 2U, {0xc7U, 0xcfU, 0U, 0U, 0U}},
{62U, 3U, 2U, {0U, 0x80U, 0U, 0U, 0U}},
{63U, 5U, 4U, {0U, 0U, 0xa0U, 0xbfU, 0U}},
{64U, 4U, 255U, {0xd5U, 0xaaU, 0x80U, 0xffU, 0U}}
};
/**
* 功能:值载荷后原样附加7字节CP56。
* 输入参数:bytes——报文字节缓冲区;type——协议类型或期望事件类型;cause——完整传送原因字节;oa——发起者地址;ca——公共地址;ioa——信息对象地址;value——测试对象值或原始位模式(只读);value_length——对象值字节数;time_bytes——原始 CP56Time2a 七字节(只读)。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:生成报文或结果数据的实际字节长度。
* 说明:不校验时间内容,以便非法月份、IV和边界时间仍可送入待测模块。
*/
static size_t command_timed_bytes(uint8_t *bytes, unsigned type, unsigned cause,
unsigned oa, unsigned ca, unsigned ioa, const uint8_t *value, size_t value_length,
const uint8_t time_bytes[7])
{
uint8_t payload[12];
CHECK(value_length >= 1U && value_length <= 5U);
memcpy(payload, value, value_length);
memcpy(payload + value_length, time_bytes, 7U);
return command_payload_bytes(bytes, type, cause, oa, ca, ioa, payload, value_length + 7U);
}
/**
* 功能:只开放本例目标类型,按UTC解释手工时标。
* 输入参数:config——测试配置对象;type——协议类型或期望事件类型;maximum——最大允许时间偏差(毫秒)。
* 输出参数:config——测试配置对象。
* 返回值:无。
* 说明:maximum=0用于明确关闭时效检查的对照路径。
*/
static void command_timed_config(IEC104_CommandConfig_t *config, unsigned type, uint32_t maximum)
{
IEC104_Command_Config_Default(config);
config->type_mask = IEC104_COMMAND_TYPE_BIT(type);
command_config_mode(config, IEC104_COMMAND_MODE_DIRECT);
config->time_tag_max_delay_ms = maximum;
config->master_utc_offset_minutes = 0;
}
/**
* 功能:验证应用可用48或49拒绝普通及带时标命令的选择、执行事件,并完整回显请求。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:非法原因不能消费事件;有效拒绝只产生一次结束通知,不执行动作,也不计作自动时间校验失败。
*/
static void test_command_application_reject_causes(void)
{
static const IEC104_CommandRejectCause_t causes[] = {
IEC104_COMMAND_REJECT_INTERLOCK, IEC104_COMMAND_REJECT_TIME
};
unsigned cause_index, timed, selecting;
CHECK(IEC104_COMMAND_REJECT_INTERLOCK == 3 && IEC104_COMMAND_REJECT_TIME == 4);
for (cause_index = 0U; cause_index < 2U; ++cause_index) {
for (timed = 0U; timed < 2U; ++timed) {
for (selecting = 0U; selecting < 2U; ++selecting) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32], payload = selecting ? 0x81U : 1U;
unsigned type = timed ? 58U : 45U;
size_t length;
command_timed_config(&config, type, 1000U);
command_config_mode(&config, selecting ? IEC104_COMMAND_MODE_SELECT_EXECUTE : IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = timed ? command_timed_bytes(bytes, type, 6U, 0x6aU, 1U, 6001U,
&payload, 1U, command_epoch_time) : command_payload_bytes(bytes, type, 6U,
0x6aU, 1U, 6001U, &payload, 1U);
command_raw_send(bytes, length);
event = command_take(selecting ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Reject(command, &event, (IEC104_CommandRejectCause_t)5) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Reject(command, &event, causes[cause_index]) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Reject(command, &event, causes[cause_index]) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_flush();
command_raw_reply_at(0U, bytes, length, cause_index == 0U ? 0x70U : 0x71U);
CHECK(command_frame_count() == 1U && command_action_calls == 0U);
CHECK(command_status.app_rejected == 1U && command_status.negative_submitted == 1U);
CHECK(command_status.time_rejected == 0U && command_status.time_expired == 0U);
CHECK(command_status.action_time_rejected == 0U && command_status.action_time_expired == 0U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_REJECTED &&
finished.transaction_id == event.transaction_id);
CHECK(!finished.select_effect && !finished.execute_effect && finished.request.has_time == (timed != 0U));
if (timed) { CHECK(finished.request.time_utc_ms == command_epoch_ms); }
command_empty(); command_flush();
CHECK(!command_status.busy && command_frame_count() == 1U);
command_close();
}
}
}
++command_checks;
}
/* ============================================================================
* [COMMAND/QUALIFIERS] QU/QL边界及非法限定词与既有事务隔离
* ============================================================================ */
/**
* 功能:限定词专项独立构造合法状态/数值和CP56,只改变QU或QL。
* 输入参数:bytes——报文字节缓冲区;type——协议类型或期望事件类型;cause——完整传送原因字节;qualifier——原始限定词;selecting——是否构造选择阶段。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:生成报文或结果数据的实际字节长度。
* 说明:51/64没有该字段,已由完整比特串流程覆盖;不能把其数据高位拿来作限定词。
*/
static size_t command_qualified_bytes(uint8_t *bytes, unsigned type, unsigned cause,
unsigned qualifier, bool selecting)
{
uint8_t payload[5] = {0U};
unsigned base_type = type >= 58U ? type - 13U : type;
size_t payload_length = base_type <= 47U ? 1U : (base_type <= 49U ? 3U : 5U);
if (base_type <= 47U) { payload[0] = (uint8_t)(1U | (qualifier << 2)); }
else { payload[payload_length - 1U] = (uint8_t)qualifier; }
if (selecting) { payload[payload_length - 1U] |= 0x80U; }
return type >= 58U ? command_timed_bytes(bytes, type, cause, 0x51U, 1U, 6001U,
payload, payload_length, command_epoch_time) : command_payload_bytes(bytes, type, cause,
0x51U, 1U, 6001U, payload, payload_length);
}
/**
* 功能:验证以下场景与约束:十二种有QU/QL类型均从真实接收路径验证:QU=0..3可交应用、4..31逐值拒绝,
* 含此前报告的9..15。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:QL只接受0,所列预定义/保留/私有范围值均否定。
* 合法激活真正调用一次action,非法值无业务事件;已知点撤销无匹配事务回否定9,
* 完整原文(含限定词)回显。公共编解码允许完整位域,不等于业务支持所有值。
*/
static void test_command_qualifier_boundaries(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 58U, 59U, 60U, 61U, 62U, 63U};
static const unsigned ql[] = {0U, 1U, 17U, 37U, 64U, 127U};
size_t type_index, value_index;
unsigned cause;
for (type_index = 0U; type_index < sizeof(types) / sizeof(types[0]); ++type_index) {
unsigned type = types[type_index];
bool status_command = type <= 47U || (type >= 58U && type <= 60U);
size_t count = status_command ? 32U : sizeof(ql) / sizeof(ql[0]);
for (value_index = 0U; value_index < count; ++value_index) {
unsigned qualifier = status_command ? (unsigned)value_index : ql[value_index];
for (cause = 6U; cause <= 8U; cause += 2U) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
bool legal = status_command ? qualifier <= 3U : qualifier == 0U;
command_timed_config(&config, type, 0U);
command_open(&config);
length = command_qualified_bytes(bytes, type, cause, qualifier, false);
command_raw_send(bytes, length);
if (legal && cause == 6U) {
IEC104_CommandEvent_t event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.qualifier == qualifier);
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && command_status.rejected == 0U);
command_raw_reply_at(0U, bytes, length, 7U);
command_raw_reply_at(1U, bytes, length, 10U);
} else {
command_flush();
CHECK(command_status.rejected == 1U && command_action_calls == 0U && !command_status.busy);
CHECK(command_frame_count() == 1U);
command_raw_reply_at(0U, bytes, length, cause == 6U ? 0x47U : 0x49U);
}
command_empty();
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:非法限定词不能被决定期同OA重发规则吞掉,也不能终结原选择或撤销原事务。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:每个类型覆盖SELECT_WAIT、SELECTED及直接EXECUTE_WAIT;激活和撤销均遍历S/E=0、1。
* 后续原请求仍能正常执行一次,证明工作事件、原文、站点身份没有被坏请求覆盖。
*/
static void test_command_qualifier_preserves_transaction(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 58U, 59U, 60U, 61U, 62U, 63U};
size_t type_index;
unsigned phase, cause, variant;
for (type_index = 0U; type_index < sizeof(types) / sizeof(types[0]); ++type_index) {
unsigned type = types[type_index];
unsigned invalid = type <= 47U || (type >= 58U && type <= 60U) ? 4U : 1U;
for (phase = 0U; phase < 3U; ++phase) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t info;
uint8_t original[32], invalid_bytes[32];
size_t length;
unsigned replies = 0U;
command_timed_config(&config, type, 0U);
if (phase != 2U) { command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE); }
command_open(&config);
length = command_qualified_bytes(original, type, 6U, 0U, phase != 2U);
command_raw_send(original, length);
event = command_take(phase == 2U ? IEC104_COMMAND_EXECUTE : IEC104_COMMAND_SELECT);
if (phase == 1U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
++replies;
}
for (variant = 0U; variant < 4U; ++variant) {
cause = variant < 2U ? 6U : 8U;
CHECK(command_qualified_bytes(invalid_bytes, type, cause, invalid, (variant & 1U) != 0U) == length);
command_raw_send(invalid_bytes, length);
command_flush();
command_raw_reply_at(replies++, invalid_bytes, length, cause == 6U ? 0x47U : 0x49U);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.active);
CHECK(info.phase == (phase == 0U ? IEC104_COMMAND_PHASE_SELECT_WAIT :
(phase == 1U ? IEC104_COMMAND_PHASE_SELECTED : IEC104_COMMAND_PHASE_EXECUTE_WAIT)));
CHECK(command_action_calls == 0U);
command_empty();
}
CHECK(command_status.rejected == 4U && command_status.dropped_requests == 0U);
if (phase == 0U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
if (phase != 2U) {
CHECK(command_qualified_bytes(original, type, 6U, 0U, false) == length);
command_raw_send(original, length);
event = command_take(IEC104_COMMAND_EXECUTE);
}
CHECK(event.request.qualifier == 0U);
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && command_action_request.qualifier == 0U);
command_empty();
command_close();
}
}
++command_checks;
}
/**
* 功能:在普通重入探针之前尝试设置/清除时间基准,核对只读查询仍有效且基准不变。
* 输入参数:context——业务回调的测试上下文;request——当前业务请求(只读)。
* 输出参数:无。
* 返回值:业务动作结果,保留应用成功、业务拒绝及未知对象等协议区别。
* 说明:该替身只检查回调保护;执行时效及回调返回后的后续请求由用例单独断言。
*/
static IEC104_CommandActionResult_t command_time_reentry_action(void *context,
const IEC104_CommandRequest_t *request)
{
IEC104_CommandStatus_t status;
CHECK(!IEC104_Command_Set_Time_Base(command, 0U, fake.now));
IEC104_Command_Clear_Time_Base(command);
CHECK(IEC104_Command_Get_Status(command, &status) && status.time_base_valid);
return command_reentry_action(context, request);
}
/* ============================================================================
* [COMMAND/TIMED] 带时标用例:完整流程、UTC换算、时间窗、基准与重入
* ============================================================================ */
/**
* 功能:验证以下场景与约束:七类时标命令分别验证完整规范长度和字段。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:选择/执行时标不同仍匹配。
* max=0不依赖基准;执行使用的值和时标不能被应用修改的事件副本替换。
*/
static void test_command_timed_flows(void)
{
size_t index;
for (index = 0U; index < sizeof(command_timed_cases) / sizeof(command_timed_cases[0]); ++index) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32], time_bytes[7];
size_t length;
unsigned type = command_timed_cases[index].type;
unsigned preceding = type == 64U ? 0U : 1U;
command_timed_config(&config, type, 0U);
if (type != 64U) { command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE); }
command_open(&config);
length = command_timed_bytes(selected, type, 6U, 0x51U, 1U, 6001U,
command_timed_cases[index].payload, command_timed_cases[index].payload_length, command_epoch_time);
CHECK(length == (type <= 60U ? 17U : (type <= 62U ? 19U : (type == 63U ? 21U : 20U))));
if (type != 64U) {
selected[9U + command_timed_cases[index].qualifier_offset] |= 0x80U;
command_raw_send(selected, length);
event = command_take(IEC104_COMMAND_SELECT);
command_check_value(&event.request, type);
CHECK(event.request.has_time && event.request.time_utc_ms == command_epoch_ms);
CHECK(event.request.is_select && !event.request.selected);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_raw_reply_at(0U, selected, length, 7U);
}
memcpy(time_bytes, command_epoch_time, sizeof(time_bytes));
time_bytes[0] = 0xd2U; time_bytes[1] = 4U;
CHECK(command_timed_bytes(executed, type, 6U, 0x51U, 1U, 6001U,
command_timed_cases[index].payload, command_timed_cases[index].payload_length, time_bytes) == length);
command_raw_send(executed, length);
event = command_take(IEC104_COMMAND_EXECUTE);
command_check_value(&event.request, type);
CHECK(event.request.has_time && event.request.time_utc_ms == command_epoch_ms + 1234U);
CHECK(event.request.time.time.second == 1U && event.request.time.time.ms == 234U);
CHECK(event.request.selected == (type != 64U) && !event.request.is_select);
event.request.value.bitstring = 0U;
event.request.state = 0U;
event.request.time_utc_ms = 0U;
memset(&event.request.time, 0, sizeof(event.request.time));
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_check_value(&command_action_request, type);
CHECK(command_action_calls == 1U && command_action_request.time_utc_ms == command_epoch_ms + 1234U);
finished = command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_check_value(&finished.request, type);
CHECK(finished.request.has_time && finished.request.time_utc_ms == command_epoch_ms + 1234U);
command_raw_reply_at(preceding, executed, length, 7U);
command_raw_reply_at(preceding + 1U, executed, length, 10U);
CHECK(command_frame_count() == preceding + 2U);
command_close();
}
/* 相同语义值不能跨类型执行:49->48、45->58;最长命令还单独验证OA不匹配。 */
for (index = 0U; index < 3U; ++index) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32];
static const uint8_t scaled[] = {0U, 0x80U, 0U};
static const uint8_t single[] = {1U};
static const uint8_t real_value[] = {0U, 0U, 0xa0U, 0xbfU, 0U};
unsigned select_type = index == 0U ? 49U : (index == 1U ? 45U : 63U);
unsigned execute_type = index == 0U ? 48U : (index == 1U ? 58U : 63U);
size_t selected_length, executed_length;
command_timed_config(&config, select_type, 0U);
config.type_mask |= IEC104_COMMAND_TYPE_BIT(execute_type);
command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&config);
if (index == 0U) {
selected_length = command_payload_bytes(selected, select_type, 6U, 0x51U, 1U, 6001U,
scaled, sizeof(scaled));
selected[11] |= 0x80U;
executed_length = command_payload_bytes(executed, execute_type, 6U, 0x51U, 1U, 6001U,
scaled, sizeof(scaled));
} else if (index == 1U) {
selected_length = command_payload_bytes(selected, select_type, 6U, 0x51U, 1U, 6001U,
single, sizeof(single));
selected[9] |= 0x80U;
executed_length = command_timed_bytes(executed, execute_type, 6U, 0x51U, 1U, 6001U,
single, sizeof(single), command_epoch_time);
} else {
selected_length = command_timed_bytes(selected, select_type, 6U, 0x51U, 1U, 6001U,
real_value, sizeof(real_value), command_epoch_time);
selected[13] |= 0x80U;
executed_length = command_timed_bytes(executed, execute_type, 6U, 0x52U, 1U, 6001U,
real_value, sizeof(real_value), command_epoch_time);
}
command_raw_send(selected, selected_length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_raw_send(executed, executed_length);
command_flush();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH);
CHECK(finished.request.type_id == select_type && finished.request.originator_address == 0x51U);
CHECK(command_action_calls == 0U && command_frame_count() == 2U);
command_raw_reply_at(0U, selected, selected_length, 7U);
command_raw_reply_at(1U, executed, executed_length, 0x47U);
command_empty();
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:原始时标带SU时只扣一次夏令时,并使用显式主站标准时区。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:保留毫秒和原始SU。
*/
static void test_command_time_conversion(void)
{
static const struct { int offset; uint8_t bytes[7]; unsigned summer; } cases[] = {
{0, {0xd2U, 4U, 0U, 0U, 1U, 1U, 26U}, 0U},
{480, {0xd2U, 4U, 0U, 8U, 1U, 1U, 26U}, 0U},
{480, {0xd2U, 4U, 0U, 0x89U, 1U, 1U, 26U}, 1U},
{0, {0xd2U, 4U, 0U, 0x81U, 1U, 1U, 26U}, 1U},
{-330, {0xd2U, 4U, 30U, 18U, 31U, 12U, 25U}, 0U}
};
static const uint8_t value[] = {1U};
size_t index;
for (index = 0U; index < sizeof(cases) / sizeof(cases[0]); ++index) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
uint8_t bytes[32];
size_t length;
command_timed_config(&config, 58U, 0U);
config.master_utc_offset_minutes = (int16_t)cases[index].offset;
command_open(&config);
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U, value, sizeof(value), cases[index].bytes);
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.has_time && event.request.time_utc_ms == command_epoch_ms + 1234U);
CHECK(event.request.time.time.ms == 234U && event.request.time.is_summer_time == cases[index].summer);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_raw_reply_at(0U, bytes, length, 7U);
command_raw_reply_at(1U, bytes, length, 10U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:接收端忽略辅助星期与GEN,不放宽真实日历、IV和10s动作时效。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:四种输入分别为无星期、错误星期、GEN、错误星期加GEN;肯定/否定确认均回显原文。
* TEST仅模拟CON/TERM;真实动作在Accept前再次到达10s边界时仍须拒绝。
*/
static void test_command_timed_auxiliary_fields(void)
{
size_t index;
unsigned variant, mode;
for (index = 0U; index < sizeof(command_timed_cases) / sizeof(command_timed_cases[0]); ++index) {
for (variant = 0U; variant < 4U; ++variant) {
for (mode = 0U; mode < 6U; ++mode) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], time_bytes[7];
size_t length;
command_timed_config(&config, command_timed_cases[index].type, 10000U);
CHECK(config.negative_on_expired && config.decision_timeout_ms == 15000U);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command,
command_epoch_ms + (mode == 2U ? 10000U : 0U), fake.now));
memcpy(time_bytes, command_epoch_time, sizeof(time_bytes));
if ((variant & 1U) != 0U) { time_bytes[4] |= 0x20U; } /* 周四故意填写周一。 */
if ((variant & 2U) != 0U) { time_bytes[2] |= 0x40U; } /* RES1/GEN不是保留位。 */
if (mode == 4U) { time_bytes[2] |= 0x80U; }
if (mode == 5U) {
time_bytes[4] = (uint8_t)((time_bytes[4] & 0xe0U) | 30U);
time_bytes[5] = 2U; /* 2026-02-30:不能被辅助字段兼容掩盖。 */
}
length = command_timed_bytes(bytes, command_timed_cases[index].type,
mode == 1U ? 0x86U : 6U, 0x61U, 1U, 6001U,
command_timed_cases[index].payload, command_timed_cases[index].payload_length, time_bytes);
command_raw_send(bytes, length);
if (mode == 0U || mode == 3U) {
IEC104_CommandEvent_t event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.time_utc_ms == command_epoch_ms && event.request.time.time.weekday == 4U);
CHECK(event.request.time.has_weekday == ((variant & 1U) != 0U));
if (mode == 3U) {
fake.now += 10000U;
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_take(IEC104_COMMAND_FINISHED).reason == IEC104_COMMAND_FINISH_TIME_EXPIRED);
} else {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
}
}
command_flush(); command_empty();
CHECK(command_action_calls == (mode == 0U ? 1U : 0U));
CHECK(command_frame_count() == (mode <= 1U ? 2U : 1U));
command_raw_reply_at(0U, bytes, length, mode == 0U ? 7U : mode == 1U ? 0x87U : 0x71U);
if (mode <= 1U) { command_raw_reply_at(1U, bytes, length, mode == 0U ? 10U : 0x8aU); }
CHECK(command_status.time_expired == (mode == 2U ? 1U : 0U));
CHECK(command_status.action_time_expired == (mode == 3U ? 1U : 0U));
CHECK(command_status.time_rejected == (mode >= 4U ? 1U : 0U));
CHECK(!command_status.busy && command_status.pending_events == 0U);
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:关闭新鲜度检查仍拒绝IV、真正保留位、非法日历和UTC越界,且保留非法原文用于否定回显。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_timed_invalid_values(void)
{
size_t index;
unsigned variant;
for (index = 0U; index < sizeof(command_timed_cases) / sizeof(command_timed_cases[0]); ++index) {
for (variant = 0U; variant < 10U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], time_bytes[7];
size_t length;
command_timed_config(&config, command_timed_cases[index].type, 0U);
memcpy(time_bytes, command_epoch_time, sizeof(time_bytes));
switch (variant) {
case 0U: time_bytes[2] = 0x80U; break;
case 1U: time_bytes[3] = 0x40U; break;
case 2U: time_bytes[3] = 0x20U; break;
case 3U: time_bytes[5] = 0x11U; break;
case 4U: time_bytes[6] |= 0x80U; break;
case 5U: time_bytes[0] = 0x60U; time_bytes[1] = 0xeaU; break;
case 6U: time_bytes[4] = 30U; time_bytes[5] = 2U; break;
case 7U: time_bytes[5] = 0x81U; break;
case 8U: time_bytes[6] = 100U; break;
default:
time_bytes[0] = 0x5fU; time_bytes[1] = 0xeaU;
time_bytes[2] = 59U; time_bytes[3] = 23U;
time_bytes[4] = 31U; time_bytes[5] = 12U; time_bytes[6] = 99U;
config.master_utc_offset_minutes = -1;
break;
}
command_open(&config);
length = command_timed_bytes(bytes, command_timed_cases[index].type, 6U, 0x61U, 1U, 6001U,
command_timed_cases[index].payload, command_timed_cases[index].payload_length, time_bytes);
command_raw_send(bytes, length);
command_flush();
CHECK(command_frame_count() == 1U && command_action_calls == 0U);
command_raw_reply_at(0U, bytes, length, 0x71U);
CHECK(command_status.rejected == 1U && !command_status.busy);
CHECK(command_status.time_rejected == 1U);
command_empty();
command_close();
}
}
/* 时标类型也必须复用SCO/DCS/RCS及浮点值校验,而非只验证CP56。 */
for (variant = 0U; variant < 6U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], payload[5] = {0U, 0U, 0x80U, 0x7fU, 0U};
unsigned type = variant < 3U ? 58U + variant : 63U;
size_t payload_length = type == 63U ? 5U : 1U;
size_t length;
if (variant == 0U) { payload[0] = 2U; }
else if (variant == 1U) { payload[0] = 0U; }
else if (variant == 2U) { payload[0] = 3U; }
else if (variant == 4U) { payload[3] = 0xffU; }
else if (variant == 5U) { payload[2] = 0xc0U; }
command_timed_config(&config, type, 0U);
command_open(&config);
length = command_timed_bytes(bytes, type, 6U, 0x61U, 1U, 6001U, payload, payload_length, command_epoch_time);
command_raw_send(bytes, length);
command_flush();
command_raw_reply_at(0U, bytes, length, 0x47U);
CHECK(command_frame_count() == 1U && command_action_calls == 0U && !command_status.busy);
command_empty();
command_close();
}
++command_checks;
}
/**
* 功能:验证七类时标命令的坏CP56返回49,混合命令值、限定词或报文结构错误仍优先返回7/9。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:交叉真实/TEST和激活/撤销,保留原始时标回显;时间拒绝只计一次,较早的命令错误不计入时间拒绝。
*/
static void test_command_timed_error_precedence(void)
{
size_t index;
unsigned test, deactivation, variant;
for (index = 0U; index < sizeof(command_timed_cases) / sizeof(command_timed_cases[0]); ++index) {
unsigned type = command_timed_cases[index].type;
for (test = 0U; test < 2U; ++test) {
for (deactivation = 0U; deactivation < 2U; ++deactivation) {
for (variant = 0U; variant < 7U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], time_bytes[7], payload[5];
unsigned flag = test ? 0x80U : 0U;
unsigned cause = deactivation ? 8U : 6U;
unsigned expected = variant < 2U ? 0x71U : (deactivation ? 0x49U : 0x47U);
size_t length, received;
if (variant == 2U && (type == 61U || type == 62U || type == 64U)) { continue; }
if (variant == 3U && type == 64U) { continue; }
command_timed_config(&config, type, 1000U);
command_open(&config);
memcpy(payload, command_timed_cases[index].payload, sizeof(payload));
memcpy(time_bytes, command_epoch_time, sizeof(time_bytes));
if (variant == 0U) { time_bytes[3] |= 0x20U; }
else { time_bytes[4] = 30U; time_bytes[5] = 2U; }
if (variant == 2U) {
if (type == 58U) { payload[0] |= 0x02U; }
else if (type == 59U || type == 60U) { payload[0] &= 0xfcU; }
else { payload[0] = payload[1] = 0U; payload[2] = 0xc0U; payload[3] = 0x7fU; }
} else if (variant == 3U) {
unsigned offset = command_timed_cases[index].qualifier_offset;
payload[offset] = type <= 60U ? (uint8_t)((payload[offset] & 0x03U) | 0x10U) : 1U;
}
length = command_timed_bytes(bytes, type, cause | flag, 0x6bU, 1U, 6001U,
payload, command_timed_cases[index].payload_length, time_bytes);
received = length;
if (variant == 4U) { bytes[2] |= 0x40U; }
else if (variant == 5U) { bytes[1] |= 0x80U; }
else if (variant == 6U) { bytes[received++] = 0x5aU; }
command_raw_send(bytes, received); command_flush(); command_empty();
command_raw_reply_at(0U, bytes, length, expected | flag);
CHECK(command_frame_count() == 1U && command_action_calls == 0U && !command_status.busy);
CHECK(command_status.rejected == 1U && command_status.time_rejected == (variant < 2U ? 1U : 0U));
CHECK(command_status.time_expired == 0U && command_status.app_rejected == 0U);
command_close();
}
}
}
}
++command_checks;
}
/**
* 功能:验证普通撤销可忽略IV、时效及基准,TEST撤销须校验;坏CP56编码对两者均回49且保留选择。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:每项先建立选择,拒绝后用新鲜时标成功撤销,确认时间错误不会误清许可或启动动作。
*/
static void test_command_deactivation_time_rules(void)
{
unsigned test, mode;
for (test = 0U; test < 2U; ++test) {
for (mode = 0U; mode < 5U; ++mode) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], payload = 0x81U;
unsigned flag = test ? 0x80U : 0U, replies = 0U;
bool rejected = test != 0U || mode == 4U;
size_t length;
command_timed_config(&config, 58U, 1000U);
command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
config.negative_on_expired = false;
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_timed_bytes(bytes, 58U, 6U | flag, 0x6cU, 1U, 6001U,
&payload, 1U, command_epoch_time);
command_raw_send(bytes, length);
if (!test) {
IEC104_CommandEvent_t event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
}
command_flush(); command_empty();
command_raw_reply_at(replies++, bytes, length, 7U | flag);
bytes[2] = (uint8_t)(8U | flag); bytes[9] = 1U;
if (mode == 0U) { bytes[length - 5U] |= 0x80U; }
else if (mode == 1U) { fake.now += 1000U; }
else if (mode == 2U) { IEC104_Command_Clear_Time_Base(command); }
else if (mode == 3U) { CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms - 1000U, fake.now)); }
else { bytes[length - 4U] |= 0x20U; }
command_raw_send(bytes, length); command_flush();
command_raw_reply_at(replies++, bytes, length, (rejected ? 0x71U : 9U) | flag);
/* 真实撤销成功仍等待应用读取FINISHED,因此槽此时也必须保持占用。 */
CHECK(command_status.busy && command_action_calls == 0U);
CHECK(command_status.pending_events == (rejected ? 0U : 1U));
CHECK(command_status.rejected == (rejected ? 1U : 0U));
CHECK(command_status.time_expired == (test && mode == 1U ? 1U : 0U));
CHECK(command_status.time_rejected == (rejected && mode != 1U ? 1U : 0U));
if (rejected) {
command_empty();
memcpy(bytes + length - 7U, command_epoch_time, sizeof(command_epoch_time));
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
command_raw_send(bytes, length); command_flush();
command_raw_reply_at(replies++, bytes, length, 9U | flag);
}
if (!test) { CHECK(command_take(IEC104_COMMAND_FINISHED).reason == IEC104_COMMAND_FINISH_DEACTIVATED); }
command_empty(); command_process();
CHECK(command_frame_count() == replies && !command_status.busy && command_action_calls == 0U);
command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:过去和未来采用同一严格窗口:差值等于上限即不接受。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:过去显式覆盖否定/静默,未来总是否定。
*/
static void test_command_time_boundaries(void)
{
unsigned future, negative, boundary;
static const uint8_t value[] = {1U};
for (future = 0U; future < 2U; ++future) {
for (negative = 0U; negative < 2U; ++negative) {
for (boundary = 0U; boundary < 3U; ++boundary) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
uint64_t difference = 999U + boundary;
command_timed_config(&config, 58U, 1000U);
config.negative_on_expired = negative != 0U;
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command,
future ? command_epoch_ms - difference : command_epoch_ms + difference, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
value, sizeof(value), command_epoch_time);
command_raw_send(bytes, length);
command_flush();
CHECK(command_status.time_base_valid && command_status.dropped_requests == 0U);
if (boundary == 0U) {
IEC104_CommandEvent_t event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.time_utc_ms == command_epoch_ms);
CHECK(command_status.time_expired == 0U && command_status.time_rejected == 0U);
CHECK(command_status.rejected == 0U && command_frame_count() == 0U);
} else {
CHECK(command_status.time_expired == (future ? 0U : 1U));
CHECK(command_status.time_rejected == (future ? 1U : 0U));
CHECK(command_status.rejected == (future || negative ? 1U : 0U));
CHECK(command_frame_count() == (future || negative ? 1U : 0U));
if (future || negative) { command_raw_reply_at(0U, bytes, length, 0x71U); }
command_empty();
}
CHECK(command_action_calls == 0U);
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:不覆盖默认窗口、时区或回复策略:58–64收到时恰好过期,须各有且仅有一条否定。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:58–63分别覆盖选择和直接执行;64没有S/E,只有执行。过期请求不得建立事务、
* 产生业务事件或调用动作;重复Process和传输推进也不得再回复或留下零超时唤醒。
*/
static void test_command_default_received_expiry(void)
{
static const uint8_t local_time[7] = {0U, 0U, 0U, 8U, 1U, 1U, 26U};
size_t index;
unsigned selecting;
for (index = 0U; index < sizeof(command_timed_cases) / sizeof(command_timed_cases[0]); ++index) {
unsigned type = command_timed_cases[index].type;
for (selecting = 0U; selecting < (type == 64U ? 1U : 2U); ++selecting) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
command_config_default(&config);
CHECK(config.negative_on_expired && config.time_tag_max_delay_ms == 10000U);
CHECK(config.master_utc_offset_minutes == 480 && config.decision_timeout_ms == 15000U);
command_config_mode(&config, selecting ? IEC104_COMMAND_MODE_SELECT_EXECUTE :
IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms + 10000U, fake.now));
length = command_timed_bytes(bytes, type, 6U, 0x51U, 1U, 6001U,
command_timed_cases[index].payload, command_timed_cases[index].payload_length, local_time);
if (selecting) { bytes[9U + command_timed_cases[index].qualifier_offset] |= 0x80U; }
command_raw_send(bytes, length);
command_flush();
command_empty();
command_process();
command_flush();
CHECK(command_frame_count() == 1U && command_action_calls == 0U);
command_raw_reply_at(0U, bytes, length, 0x71U);
CHECK(command_status.time_expired == 1U && command_status.time_rejected == 0U);
CHECK(command_status.rejected == 1U && command_status.negative_submitted == 1U);
CHECK(command_status.accepted == 0U && command_status.selected == 0U && command_status.executed == 0U);
CHECK(command_status.action_time_expired == 0U && command_status.decision_timeouts == 0U);
CHECK(!command_status.busy && !command_status.timer_active && !command_status.need_process);
CHECK(command_status.pending_events == 0U && command_status.pending_replies == 0U);
command_empty();
command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:基准缺失、单调时钟倒退和推算UTC越界都明确否定。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:不能通过无符号回绕变成新鲜请求。
*/
static void test_command_time_base_errors(void)
{
unsigned variant;
static const uint8_t value[] = {1U};
const uint64_t maximum = (uint64_t)IEC60870_TIME_MAX_SECONDS * 1000U + 999U;
for (variant = 0U; variant < 4U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
command_timed_config(&config, 58U, 1000U);
command_open(&config);
if (variant == 1U) {
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
} else if (variant == 2U) {
CHECK(IEC104_Command_Set_Time_Base(command, maximum, fake.now));
++fake.now;
} else if (variant == 3U) {
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
IEC104_Command_Clear_Time_Base(command);
}
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
value, sizeof(value), command_epoch_time);
if (variant == 1U) {
/* 在网络已收帧、业务尚未消费通知之间注入回退,避免传输层先处理同一个时钟故障。 */
command_queue(bytes, length);
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
--fake.now;
command_drain_transport();
++fake.now;
} else { command_raw_send(bytes, length); }
command_flush();
if (variant != 1U) { command_raw_reply_at(0U, bytes, length, 0x71U); }
CHECK(command_status.time_rejected == 1U && command_status.time_expired == 0U);
CHECK(command_status.rejected == 1U && command_status.dropped_requests == 0U);
CHECK(command_status.clock_errors == (variant == 1U ? 1U : 0U));
CHECK(command_status.time_base_valid == (variant == 1U || variant == 2U));
/* 回退同样会被发送路径的Net期限检查发现并收尾,不能要求故障连接仍成功写出否定。 */
CHECK(command_action_calls == 0U && command_frame_count() == (variant == 1U ? 0U : 1U));
command_empty();
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:Invalid Set preserves the base。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:accepted timestamps stay immutable while
* Accept checks their freshness against the current base.
*/
static void test_command_time_base_api(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32];
size_t length;
static const uint8_t value[] = {1U};
const uint64_t maximum = (uint64_t)IEC60870_TIME_MAX_SECONDS * 1000U + 999U;
command_timed_config(&config, 58U, 1000U);
command_open(&config);
CHECK(!IEC104_Command_Set_Time_Base(NULL, command_epoch_ms, fake.now));
IEC104_Command_Clear_Time_Base(NULL);
CHECK(IEC104_Command_Set_Time_Base(command, 0U, fake.now));
CHECK(IEC104_Command_Set_Time_Base(command, maximum, fake.now));
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms - 100U, fake.now - 100U));
CHECK(!IEC104_Command_Set_Time_Base(command, maximum + 1U, fake.now));
CHECK(!IEC104_Command_Set_Time_Base(command, 0U, fake.now + 1U));
CHECK(!IEC104_Command_Set_Time_Base(command, UINT64_MAX, fake.now));
CHECK(!IEC104_Command_Set_Time_Base(command, 0U, UINT64_MAX));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U, value, sizeof(value), command_epoch_time);
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.time_utc_ms == command_epoch_ms);
IEC104_Command_Clear_Time_Base(command);
CHECK(IEC104_Command_Get_Status(command, &command_status) && !command_status.time_base_valid);
CHECK(IEC104_Command_Set_Time_Base(command, 0U, fake.now));
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_action_calls == 0U);
command_flush();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_TIME_REJECTED);
CHECK(finished.request.time_utc_ms == command_epoch_ms);
CHECK(command_status.time_rejected == 0U && command_status.time_expired == 0U);
CHECK(command_status.action_time_rejected == 1U);
command_raw_reply_at(0U, bytes, length, 0x71U);
CHECK(command_frame_count() == 1U);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:STOP和断线只结束连接事务,可信时间基准继续有效。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:停用及离线时仍可显式更新。
*/
static void test_command_time_base_lifecycle(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
uint8_t bytes[32];
size_t length;
static const uint8_t value[] = {1U};
command_timed_config(&config, 58U, 1000U);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
push(stop_act, sizeof(stop_act));
command_flush();
CHECK(!command_status.active && command_status.time_base_valid);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
push(start_act, sizeof(start_act));
command_flush();
CHECK(command_status.active && command_status.time_base_valid);
fake.recv_eof = 1U;
command_flush();
CHECK(!command_status.active && command_status.time_base_valid);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
fake.recv_eof = 0U;
command_connect();
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U, value, sizeof(value), command_epoch_time);
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.time_utc_ms == command_epoch_ms);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_status.time_base_valid);
IEC104_Command_Clear_Time_Base(command);
CHECK(IEC104_Command_Get_Status(command, &command_status) && !command_status.time_base_valid);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:公共类型支持、时区配置边界和无S/E选择限制均在Create时校验。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_time_configuration(void)
{
static const IEC104_CommandPoint_t invalid = {1U, 6001U,
IEC104_COMMAND_TYPE_BIT(64U), IEC104_COMMAND_MODE_SELECT_EXECUTE};
IEC104_CommandConfig_t config;
unsigned type;
command_open(NULL);
CHECK(IEC104_Command_Destroy(command));
for (type = 58U; type <= 64U; ++type) {
command_timed_config(&config, type, 1000U);
config.master_utc_offset_minutes = type % 2U == 0U ? 1439 : -1439;
command = IEC104_Command_Create(command_slave, &config);
CHECK(command != NULL && IEC104_Command_Destroy(command));
}
config.points = &invalid;
config.point_count = 1U;
CHECK(IEC104_Command_Create(command_slave, &config) == NULL);
command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.master_utc_offset_minutes = 1440;
CHECK(IEC104_Command_Create(command_slave, &config) == NULL);
config.master_utc_offset_minutes = -1440;
CHECK(IEC104_Command_Create(command_slave, &config) == NULL);
command_config_default(&config);
CHECK(config.type_mask == UINT16_C(0x3fff));
command = IEC104_Command_Create(command_slave, &config);
CHECK(command != NULL);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:决定期执行重发先于时效。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:重复SELECT必须先校验时效,过期请求不能清除选择。
* CP56编码错误仍回49,其他点过期先于busy,真实撤销不要求新鲜时标或基准。
* 本组显式选静默策略,利用没有新增回复区分过期丢弃与普通忙拒绝。
*/
static void test_command_time_ordering(void)
{
unsigned selecting;
for (selecting = 0U; selecting < 2U; ++selecting) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32], changed[32], payload[1];
size_t length;
command_timed_config(&config, 58U, 1000U);
config.negative_on_expired = false;
if (selecting) { command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE); }
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
payload[0] = selecting ? 0x81U : 1U;
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
payload, sizeof(payload), command_epoch_time);
command_raw_send(bytes, length);
event = command_take(selecting ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms + 1000U, fake.now));
memcpy(changed, bytes, length);
command_raw_send(changed, length);
command_flush();
CHECK(command_status.dropped_requests == (selecting ? 0U : 1U) && command_status.time_expired == selecting);
CHECK(command_status.time_rejected == 0U && command_frame_count() == 0U);
changed[13] |= 0x20U; /* 小时RES2才是格式错误;分钟RES1/GEN接收时允许为1。 */
command_raw_send(changed, length);
command_flush();
command_raw_reply_at(0U, changed, length, 0x71U);
CHECK(command_status.rejected == 1U && command_status.dropped_requests == (selecting ? 0U : 1U));
memcpy(changed, bytes, length);
changed[6] += 1U;
command_raw_send(changed, length);
command_flush();
CHECK(command_status.time_expired == selecting + 1U && command_status.busy_rejected == 0U);
CHECK(command_status.rejected == 1U && command_frame_count() == 1U);
IEC104_Command_Clear_Time_Base(command);
memcpy(changed, bytes, length);
changed[2] = 8U;
changed[9] = 0U;
changed[length - 1U] = 25U;
command_raw_send(changed, length);
command_flush();
command_raw_reply_at(1U, changed, length, 9U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DEACTIVATED);
CHECK(finished.request.receive_serial == event.request.receive_serial);
CHECK(finished.request.time_utc_ms == command_epoch_ms);
CHECK(command_status.time_rejected == 1U && command_action_calls == 0U);
command_empty();
command_close();
}
/* 过期执行不破坏原选择;分别验证恢复基准后执行,以及最终到选择保持期限终结。 */
for (selecting = 0U; selecting < 2U; ++selecting) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t info;
uint8_t bytes[32];
static const uint8_t payload[] = {0x81U};
size_t length;
command_timed_config(&config, 58U, 1000U);
config.negative_on_expired = false;
command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
payload, sizeof(payload), command_epoch_time);
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms + 1000U, fake.now));
bytes[9] = 1U;
command_raw_send(bytes, length);
command_flush();
command_empty();
CHECK(IEC104_Command_Get_Transaction(command, &event, &info));
CHECK(info.active && info.phase == IEC104_COMMAND_PHASE_SELECTED);
CHECK(command_status.time_expired == 1U && command_frame_count() == 1U);
if (selecting == 0U) {
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && command_status.time_expired == 1U);
} else {
fake.now += config.select_timeout_ms;
command_flush();
event = command_take(IEC104_COMMAND_FINISHED);
CHECK(event.reason == IEC104_COMMAND_FINISH_SELECT_TIMEOUT);
CHECK(command_status.select_timeouts == 1U && command_status.time_expired == 1U);
CHECK(command_action_calls == 0U && command_frame_count() == 1U);
command_empty();
}
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:时基修改同样受动作回调保护。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:回调返回后再收请求,证明旧基准没有被替换或清除。
*/
static void test_command_time_base_reentry(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
uint8_t bytes[32];
static const uint8_t payload[] = {1U};
size_t length;
command_timed_config(&config, 58U, 1000U);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
payload, sizeof(payload), command_epoch_time);
command_raw_send(bytes, length);
command_reentry_event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Accept(command, &command_reentry_event, command_time_reentry_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&command_reentry_event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_raw_send(bytes, length);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.time_utc_ms == command_epoch_ms);
CHECK(command_status.time_rejected == 0U && command_action_calls == 1U);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:完整21字节请求、17字节事务回复及独立短回复在WAIT期间各自保留长度。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:独立槽先保存最长非法请求,再复用为短请求,不能截断时标或带出旧尾部。
*/
static void test_command_timed_reply_lengths(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32], rejected[32], short_reply[32];
static const uint8_t selected_payload[] = {0U, 0U, 0xa0U, 0xbfU, 0x80U};
static const uint8_t executed_payload[] = {1U};
static const uint8_t short_payload[] = {0x81U};
size_t selected_length, executed_length, short_length;
command_timed_config(&config, 63U, 0U);
config.type_mask |= IEC104_COMMAND_TYPE_BIT(45U) | IEC104_COMMAND_TYPE_BIT(58U);
command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&config);
selected_length = command_timed_bytes(selected, 63U, 6U, 0x51U, 1U, 6001U,
selected_payload, sizeof(selected_payload), command_epoch_time);
command_raw_send(selected, selected_length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_congest();
memcpy(rejected, selected, selected_length);
rejected[3] = 0x62U;
rejected[4] = 2U;
command_raw_send(rejected, selected_length);
executed_length = command_timed_bytes(executed, 58U, 6U, 0x51U, 1U, 6001U,
executed_payload, sizeof(executed_payload), command_epoch_time);
command_raw_send(executed, executed_length);
command_process();
CHECK(command_status.pending_replies == 2U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH);
CHECK(finished.request.type_id == 63U && finished.request.value.short_float == -1.25f);
CHECK(finished.request.time_utc_ms == command_epoch_ms);
fake.send_blocked = 0U;
command_flush();
command_raw_reply_at(0U, selected, selected_length, 7U);
command_raw_reply_at(1U, rejected, selected_length, 0x6eU);
command_raw_reply_at(2U, executed, executed_length, 0x47U);
CHECK(command_frame_count() == 3U && !command_status.busy);
command_congest();
short_length = command_payload_bytes(short_reply, 45U, 6U, 0x63U, 2U, 6001U,
short_payload, sizeof(short_payload));
command_raw_send(short_reply, short_length);
command_process();
CHECK(command_status.pending_replies == 1U);
fake.send_blocked = 0U;
command_flush();
command_raw_reply_at(3U, short_reply, short_length, 0x6eU);
CHECK(command_frame_count() == 4U && command_action_calls == 0U);
command_empty();
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:所有时标长度独立验证SQ/尾部规范化及截断/多对象丢弃。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:地址错误优先于基准缺失。
*/
static void test_command_timed_wire_validation(void)
{
size_t index;
unsigned variant;
for (index = 0U; index < sizeof(command_timed_cases) / sizeof(command_timed_cases[0]); ++index) {
for (variant = 0U; variant < 8U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[64];
size_t length, received;
unsigned expected = 0x47U;
command_timed_config(&config, command_timed_cases[index].type, 1000U);
command_open(&config);
length = command_timed_bytes(bytes, command_timed_cases[index].type, 6U, 0x61U, 1U, 6001U,
command_timed_cases[index].payload, command_timed_cases[index].payload_length, command_epoch_time);
received = length;
switch (variant) {
case 0U: bytes[1] = 0x81U; break;
case 1U: bytes[received++] = 0xa5U; break;
case 2U: --received; expected = 0U; break;
case 3U: bytes[1] = 0U; expected = 0U; break;
case 4U:
memcpy(bytes + length, bytes + 6U, length - 6U);
received += length - 6U;
bytes[1] = 2U;
expected = 0U;
break;
case 5U: bytes[2] = 5U; bytes[4] = 2U; expected = 0x6dU; break;
case 6U: bytes[4] = 2U; bytes[6] = bytes[7] = bytes[8] = 0U; expected = 0x6eU; break;
default: bytes[6] = bytes[7] = bytes[8] = 0U; expected = 0x6fU; break;
}
command_raw_send(bytes, received);
command_flush();
CHECK(command_frame_count() == (expected == 0U ? 0U : 1U));
if (expected != 0U) { command_raw_reply_at(0U, bytes, length, expected); }
CHECK(command_status.time_rejected == 0U && command_status.time_expired == 0U);
CHECK(command_action_calls == 0U && !command_status.busy);
command_empty();
command_close();
}
}
++command_checks;
}
/* ============================================================================
* [COMMAND/POINTS] 点表用例:默认掩码、空表、逐点模式、二分查点及身份
* ============================================================================ */
/**
* 功能:验证以下场景与约束:保持默认全局类型掩码,用显式测试点表逐一接入14类命令。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:点表令51/64直接执行,其余先选择;时标类无基准时拒绝,有基准后完成事务。
*/
static void test_command_default_all_types(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 51U,
58U, 59U, 60U, 61U, 62U, 63U, 64U};
static const uint8_t local_time[7] = {0U, 0U, 0U, 8U, 1U, 1U, 26U};
IEC104_CommandConfig_t defaults;
size_t index;
IEC104_Command_Config_Default(&defaults);
CHECK(defaults.type_mask == UINT16_C(0x3fff));
CHECK(defaults.points == NULL && defaults.point_count == 0U);
CHECK(defaults.time_tag_max_delay_ms == 10000U && defaults.master_utc_offset_minutes == 480);
CHECK(defaults.negative_on_expired);
for (index = 0U; index < sizeof(types) / sizeof(types[0]); ++index) {
unsigned type = types[index];
unsigned qualifier, reply_index = 0U;
bool timed = type >= 58U;
bool direct = type == 51U || type == 64U;
IEC104_CommandEvent_t event, finished;
uint8_t selected[32], executed[32], simple[1];
const uint8_t *payload;
size_t payload_length, length;
if (type <= 46U) {
simple[0] = type == 45U ? 1U : 2U;
payload = simple;
payload_length = sizeof(simple);
qualifier = 0U;
} else if (!timed) {
payload = command_value_cases[type - 47U].payload;
payload_length = command_value_cases[type - 47U].payload_length;
qualifier = command_value_cases[type - 47U].qualifier_offset;
} else {
payload = command_timed_cases[type - 58U].payload;
payload_length = command_timed_cases[type - 58U].payload_length;
qualifier = command_timed_cases[type - 58U].qualifier_offset;
}
command_config_mode(&defaults, direct ? IEC104_COMMAND_MODE_DIRECT : IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&defaults);
length = timed ? command_timed_bytes(executed, type, 6U, 0x51U, 1U, 6001U,
payload, payload_length, local_time) : command_payload_bytes(executed, type, 6U,
0x51U, 1U, 6001U, payload, payload_length);
memcpy(selected, executed, length);
if (!direct) { selected[9U + qualifier] |= 0x80U; }
if (timed) {
command_raw_send(selected, length);
command_flush();
CHECK(command_status.time_rejected == 1U && command_status.accepted == 0U);
CHECK(command_action_calls == 0U && !command_status.busy);
command_raw_reply_at(reply_index++, selected, length, 0x71U);
command_empty();
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
}
if (!direct) {
command_raw_send(executed, length);
command_flush();
CHECK(command_status.accepted == 0U && command_action_calls == 0U);
command_raw_reply_at(reply_index++, executed, length, 0x47U);
command_empty();
command_raw_send(selected, length);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(event.request.type_id == type && event.request.is_select);
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_raw_reply_at(reply_index++, selected, length, 7U);
}
command_raw_send(executed, length);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.type_id == type && !event.request.is_select);
CHECK(event.request.selected == !direct && event.request.has_time == timed);
if (type > 46U) { command_check_value(&event.request, type); }
else { CHECK(event.request.state == (type == 45U ? 1U : 2U)); }
if (timed) { CHECK(event.request.time_utc_ms == command_epoch_ms); }
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
finished = command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && finished.request.type_id == type);
command_raw_reply_at(reply_index++, executed, length, 7U);
command_raw_reply_at(reply_index++, executed, length, 10U);
CHECK(command_frame_count() == reply_index);
command_close();
}
command_open(NULL);
command_send(105U, 6U, 0U, 1U, 0U, 1U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED && command_frame_count() == 0U);
command_empty();
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:A raw default configuration has no points: all 14 enabled, valid types reach
* the unknown-IOA reply without publishing work or FINISHED.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_point_default_empty(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 51U,
58U, 59U, 60U, 61U, 62U, 63U, 64U};
static const uint8_t local_time[7] = {0U, 0U, 0U, 8U, 1U, 1U, 26U};
size_t index;
for (index = 0U; index < sizeof(types) / sizeof(types[0]); ++index) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], simple[1];
const uint8_t *payload;
size_t payload_length, length;
unsigned type = types[index];
IEC104_Command_Config_Default(&config);
CHECK(config.type_mask == UINT16_C(0x3fff));
CHECK(config.points == NULL && config.point_count == 0U);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
if (type <= 46U) {
simple[0] = type == 45U ? 1U : 2U;
payload = simple; payload_length = sizeof(simple);
} else if (type < 58U) {
payload = command_value_cases[type - 47U].payload;
payload_length = command_value_cases[type - 47U].payload_length;
} else {
payload = command_timed_cases[type - 58U].payload;
payload_length = command_timed_cases[type - 58U].payload_length;
}
length = type >= 58U ? command_timed_bytes(bytes, type, 6U, 0x51U, 1U, 6001U,
payload, payload_length, local_time) : command_payload_bytes(bytes, type, 6U,
0x51U, 1U, 6001U, payload, payload_length);
command_raw_send(bytes, length); command_flush();
CHECK(command_last_handled == IEC104_COMMAND_EVENT_HANDLED);
CHECK(command_status.rejected == 1U && command_status.accepted == 0U);
CHECK(!command_status.busy && command_action_calls == 0U);
CHECK(command_frame_count() == 1U);
command_raw_reply_at(0U, bytes, length, 0x6fU);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:Validate the entire borrowed table before binding, including entries whose
* type bits are currently disabled globally. Mutations occur only unbound.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_point_configuration(void)
{
static const IEC104_CommandPoint_t valid[] = {
{1U, 1U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{1U, UINT32_C(0xffffff), IEC104_COMMAND_TYPE_BIT(51U), IEC104_COMMAND_MODE_DIRECT},
{65534U, UINT32_C(0xffffff), IEC104_COMMAND_TYPE_BIT(64U), IEC104_COMMAND_MODE_DIRECT}
};
IEC104_CommandConfig_t config;
IEC104_CommandPoint_t points[3];
unsigned variant;
command_open(NULL);
CHECK(IEC104_Command_Destroy(command));
for (variant = 0U; variant < 17U; ++variant) {
IEC104_Command_Config_Default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(45U);
memcpy(points, valid, sizeof(points));
config.points = points; config.point_count = 3U;
switch (variant) {
case 0U: points[0].common_address = 0U; break;
case 1U: points[2].common_address = 65535U; break;
case 2U: points[0].ioa = 0U; break;
case 3U: points[2].ioa = UINT32_C(0x1000000); break;
case 4U: points[0].type_mask = 0U; break;
case 5U: points[0].type_mask |= UINT16_C(0x4000); break;
case 6U: points[2].type_mask |= UINT16_C(0x8000); break;
case 7U: points[0].mode = (IEC104_CommandMode_t)0; break;
case 8U: points[2].mode = (IEC104_CommandMode_t)3; break;
case 9U: points[1].ioa = points[0].ioa; break;
case 10U: points[0].ioa = 2U; points[1].ioa = 1U; break;
case 11U: points[0].common_address = 2U; break;
case 12U: points[1].mode = IEC104_COMMAND_MODE_SELECT_EXECUTE; break;
case 13U: points[2].mode = IEC104_COMMAND_MODE_SELECT_EXECUTE; break;
case 14U: config.points = NULL; break;
case 15U: config.point_count = 0U; break;
default: config.point_count = SIZE_MAX; break;
}
CHECK(IEC104_Command_Create(command_slave, &config) == NULL);
}
IEC104_Command_Config_Default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(45U);
config.points = valid; config.point_count = sizeof(valid) / sizeof(valid[0]);
command = IEC104_Command_Create(command_slave, &config);
CHECK(command != NULL);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:The same type can have different modes at two IOAs or at equal IOAs in two
* stations. Policy rejections never borrow a transaction or call application.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_point_modes(void)
{
static const IEC104_CommandPoint_t points[] = {
{1U, 6001U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_DIRECT},
{1U, 6002U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{1U, 6003U, IEC104_COMMAND_TYPE_BIT(46U), IEC104_COMMAND_MODE_DIRECT},
{2U, 6001U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{2U, 6002U, IEC104_COMMAND_TYPE_BIT(46U), IEC104_COMMAND_MODE_DIRECT}
};
static const struct { unsigned ca, ioa, value, event, cause; } cases[] = {
{1U, 6001U, 0x81U, 0U, 0x47U}, {1U, 6001U, 1U, IEC104_COMMAND_EXECUTE, 7U},
{1U, 6002U, 1U, 0U, 0x47U}, {1U, 6002U, 0x81U, IEC104_COMMAND_SELECT, 7U},
{2U, 6001U, 1U, 0U, 0x47U}, {2U, 6001U, 0x81U, IEC104_COMMAND_SELECT, 7U},
{1U, 6003U, 1U, 0U, 0x47U}, {2U, 6002U, 0x81U, 0U, 0x47U},
{1U, 6004U, 1U, 0U, 0x6fU}, {2U, 6003U, 0x81U, 0U, 0x6fU}
};
size_t index;
for (index = 0U; index < sizeof(cases) / sizeof(cases[0]); ++index) {
IEC104_CommandConfig_t config;
IEC104_Station_t other = {0};
IEC104_CommandEvent_t event;
IEC104_Command_Config_Default(&config);
config.points = points; config.point_count = sizeof(points) / sizeof(points[0]);
command_open(&config);
CHECK(IEC104_Slave_Add_Station(command_slave, &other, 2U, &other) == IEC104_STATION_OK);
/* Config is copied; the original const table remains alive and unchanged. */
config.points = NULL; config.point_count = 0U; config.type_mask = 0U;
command_send(45U, 6U, 0x51U, cases[index].ca, cases[index].ioa, cases[index].value);
if (cases[index].event != 0U) {
event = command_take((IEC104_CommandEventType_t)cases[index].event);
CHECK(event.request.common_address == cases[index].ca && event.request.ioa == cases[index].ioa);
CHECK(!event.request.selected);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
if (event.type == IEC104_COMMAND_EXECUTE) { (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED); }
else {
command_flush(); IEC104_Command_Cancel(command);
CHECK(command_take(IEC104_COMMAND_FINISHED).reason == IEC104_COMMAND_FINISH_CANCELLED);
}
CHECK(command_action_calls == 1U);
} else {
command_flush(); command_empty();
CHECK(command_status.rejected == 1U && command_status.accepted == 0U);
CHECK(!command_status.busy && command_status.busy_rejected == 0U && command_action_calls == 0U);
CHECK(command_frame_count() == 1U);
IEC104_Command_Cancel(command); command_empty();
}
command_reply_at(0U, 45U, cases[index].cause, 0x51U, cases[index].ca,
cases[index].ioa, cases[index].value);
CHECK(IEC104_Slave_Remove_Station(command_slave, &other) == IEC104_STATION_OK);
command_empty(); command_close();
CHECK(IEC104_Station_Is_Reusable(&other));
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:A point may name globally disabled types, but global disable still produces
* IGNORED for activation/deactivation. An enabled type absent from a known
* point produces negative 7/9。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:it is a point policy, not an unknown type.
*/
static void test_command_point_type_intersection(void)
{
static const IEC104_CommandPoint_t points[] = {
{1U, 6001U, IEC104_COMMAND_TYPE_BIT(45U) | IEC104_COMMAND_TYPE_BIT(46U), IEC104_COMMAND_MODE_DIRECT},
{1U, 6002U, IEC104_COMMAND_TYPE_BIT(46U), IEC104_COMMAND_MODE_DIRECT}
};
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
unsigned ioa;
IEC104_Command_Config_Default(&config);
config.type_mask = IEC104_COMMAND_TYPE_BIT(45U);
config.points = points; config.point_count = sizeof(points) / sizeof(points[0]);
command_open(&config);
for (ioa = 6001U; ioa <= 6003U; ++ioa) {
command_send(46U, 6U, 0x51U, 1U, ioa, 2U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
command_send(46U, 8U, 0x51U, 1U, ioa, 2U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
}
command_flush(); command_empty();
CHECK(command_status.rejected == 0U && command_frame_count() == 0U);
command_send(45U, 6U, 0x51U, 1U, 6002U, 1U); command_flush();
command_reply_at(0U, 45U, 0x47U, 0x51U, 1U, 6002U, 1U);
command_empty(); CHECK(command_action_calls == 0U && !command_status.busy);
command_send(45U, 8U, 0x51U, 1U, 6002U, 1U); command_flush();
command_reply_at(1U, 45U, 0x49U, 0x51U, 1U, 6002U, 1U);
command_empty(); CHECK(command_action_calls == 0U && !command_status.busy);
event = command_execute(45U, 1U);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_status.accepted == 1U && command_status.rejected == 2U);
CHECK(command_action_calls == 1U && command_frame_count() == 4U);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:Pending same-point activation and selected-command matching precede point
* policy。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:policy itself precedes busy. Cancellation still uses transaction identity.
*/
static void test_command_point_ordering(void)
{
static const IEC104_CommandPoint_t points[] = {
{1U, 6001U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{1U, 6002U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_DIRECT}
};
unsigned phase;
for (phase = 0U; phase < 3U; ++phase) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC104_CommandTransactionInfo_t info;
unsigned ioa = phase == 2U ? 6002U : 6001U;
IEC104_Command_Config_Default(&config);
config.points = points; config.point_count = sizeof(points) / sizeof(points[0]);
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, ioa, phase == 2U ? 1U : 0x81U);
event = command_take(phase == 2U ? IEC104_COMMAND_EXECUTE : IEC104_COMMAND_SELECT);
if (phase == 1U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
/* Type 46 is globally enabled but absent from this point. */
command_send(46U, 6U, 0x51U, 1U, ioa, 2U); command_flush();
if (phase == 1U) {
command_reply_at(1U, 46U, 0x47U, 0x51U, 1U, ioa, 2U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_MISMATCH);
CHECK(finished.transaction_id == event.transaction_id && command_status.mismatches == 1U);
CHECK(command_status.dropped_requests == 0U && command_frame_count() == 2U);
} else {
CHECK(command_status.dropped_requests == 1U && command_status.rejected == 0U);
CHECK(command_frame_count() == 0U);
if (phase == 2U) {
command_send(45U, 6U, 0x51U, 1U, ioa, 0x81U); command_flush();
CHECK(command_status.dropped_requests == 2U && command_status.rejected == 0U);
}
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.active);
command_send(45U, 6U, 0x51U, 1U, 6003U, 1U); command_flush();
command_reply_at(0U, 45U, 0x6fU, 0x51U, 1U, 6003U, 1U);
CHECK(command_status.busy_rejected == 0U);
command_send(46U, 6U, 0x51U, 1U, phase == 2U ? 6001U : 6002U, 2U); command_flush();
command_reply_at(1U, 46U, 0x47U, 0x51U, 1U, phase == 2U ? 6001U : 6002U, 2U);
CHECK(command_status.busy_rejected == 0U);
command_send(45U, 6U, 0x51U, 1U, phase == 2U ? 6001U : 6002U, phase == 2U ? 0x81U : 1U);
command_flush();
CHECK(command_status.busy_rejected == 1U && command_status.rejected == 3U);
command_send(45U, 8U, 0x51U, 1U, ioa, 0U); command_flush();
command_reply_at(3U, 45U, 9U, 0x51U, 1U, ioa, 0U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DEACTIVATED);
CHECK(finished.transaction_id == event.transaction_id && command_frame_count() == 4U);
}
CHECK(command_action_calls == 0U);
command_empty(); command_close();
}
{
IEC104_CommandConfig_t config;
uint8_t bytes[32];
static const uint8_t payload[1] = {1U};
size_t length;
command_timed_config(&config, 58U, 1000U);
/* 静默过期先于点表拒绝:无新增回复证明不会落入未知IOA/类型拒绝。 */
config.negative_on_expired = false;
config.points = points; config.point_count = sizeof(points) / sizeof(points[0]);
command_open(&config);
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6003U,
payload, sizeof(payload), command_epoch_time);
command_raw_send(bytes, length); command_flush();
command_raw_reply_at(0U, bytes, length, 0x71U);
CHECK(command_status.time_rejected == 1U);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms + 1000U, fake.now));
command_raw_send(bytes, length); command_flush();
CHECK(command_status.time_expired == 1U && command_frame_count() == 1U);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
command_raw_send(bytes, length); command_flush();
command_raw_reply_at(1U, bytes, length, 0x6fU);
bytes[6] -= 2U;
command_raw_send(bytes, length); command_flush();
command_raw_reply_at(2U, bytes, length, 0x47U);
CHECK(command_status.rejected == 3U && command_status.time_rejected == 1U);
CHECK(command_status.accepted == 0U && !command_status.busy && command_action_calls == 0U);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:Binary lookup must distinguish both CA and all 24 IOA bits at first, middle,
* last and missing keys. The table may precede station registration.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_point_lookup_boundaries(void)
{
static const IEC104_CommandPoint_t points[] = {
{1U, 1U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_DIRECT},
{1U, 6001U, IEC104_COMMAND_TYPE_BIT(46U), IEC104_COMMAND_MODE_DIRECT},
{1U, UINT32_C(0xffffff), IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_DIRECT},
{65534U, 1U, IEC104_COMMAND_TYPE_BIT(46U), IEC104_COMMAND_MODE_DIRECT},
{65534U, UINT32_C(0xffffff), IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_DIRECT}
};
static const struct { unsigned ca, ioa, type, cause; } cases[] = {
{1U, 1U, 45U, 7U}, {1U, 6001U, 46U, 7U}, {1U, 0xffffffU, 45U, 7U},
{65534U, 1U, 46U, 7U}, {65534U, 0xffffffU, 45U, 7U},
{1U, 2U, 45U, 0x6fU}, {1U, 6000U, 45U, 0x6fU},
{65534U, 0xfffffeU, 45U, 0x6fU}, {65534U, 6001U, 45U, 0x6fU},
{65533U, 1U, 45U, 0x6eU}
};
size_t index;
for (index = 0U; index < sizeof(cases) / sizeof(cases[0]); ++index) {
IEC104_CommandConfig_t config;
IEC104_Station_t other = {0};
unsigned value = cases[index].type == 45U ? 1U : 2U;
IEC104_Command_Config_Default(&config);
config.points = points; config.point_count = sizeof(points) / sizeof(points[0]);
command_open(&config);
CHECK(IEC104_Slave_Add_Station(command_slave, &other, 65534U, &other) == IEC104_STATION_OK);
command_send(cases[index].type, 6U, 0x51U, cases[index].ca, cases[index].ioa, value);
if (cases[index].cause == 7U) {
IEC104_CommandEvent_t event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(event.request.ioa == cases[index].ioa && event.request.common_address == cases[index].ca);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U);
} else {
command_flush(); command_empty();
CHECK(command_action_calls == 0U && command_status.accepted == 0U && !command_status.busy);
}
command_reply_at(0U, cases[index].type, cases[index].cause, 0x51U,
cases[index].ca, cases[index].ioa, value);
CHECK(IEC104_Slave_Remove_Station(command_slave, &other) == IEC104_STATION_OK);
command_close();
CHECK(IEC104_Station_Is_Reusable(&other));
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:Ending a selection cannot transfer its permission to another IOA or a new
* station registration. The immutable table remains usable after every end.
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void test_command_point_lifetime(void)
{
static const IEC104_CommandPoint_t points[] = {
{1U, 6001U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{1U, 6002U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_DIRECT}
};
unsigned ending;
for (ending = 0U; ending < 3U; ++ending) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t selected, event, finished;
IEC104_Station_t replacement = {0};
unsigned reply_index = 1U;
IEC104_Command_Config_Default(&config);
config.points = points; config.point_count = sizeof(points) / sizeof(points[0]);
config.select_timeout_ms = 10U;
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
selected = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &selected, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
if (ending == 0U) { fake.now += 10U; command_flush(); }
else if (ending == 1U) { push(stop_act, sizeof(stop_act)); command_poll(); }
else {
CHECK(IEC104_Slave_Remove_Station(command_slave, &command_station) == IEC104_STATION_OK);
CHECK(IEC104_Slave_Add_Station(command_slave, &replacement, 1U, &replacement) == IEC104_STATION_OK);
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U); command_flush();
command_reply_at(reply_index++, 45U, 0x47U, 0x51U, 1U, 6001U, 1U);
CHECK(command_status.mismatches == 0U && command_status.busy_rejected == 1U);
IEC104_Command_Cancel(command);
}
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (ending == 0U ? IEC104_COMMAND_FINISH_SELECT_TIMEOUT :
(ending == 1U ? IEC104_COMMAND_FINISH_STOPPED : IEC104_COMMAND_FINISH_CANCELLED)));
CHECK(finished.request.station_id == selected.request.station_id);
CHECK(IEC104_Command_Accept(command, &selected, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
if (ending == 1U) { push(start_act, sizeof(start_act)); command_poll(); }
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U); command_flush();
command_reply_at(reply_index++, 45U, 0x47U, 0x51U, 1U, 6001U, 1U);
command_empty(); CHECK(command_action_calls == 0U && !command_status.busy);
command_send(45U, 6U, 0x51U, 1U, 6002U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(!event.request.selected && event.transaction_id != selected.transaction_id);
CHECK((event.request.station_id == selected.request.station_id) == (ending != 2U));
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
command_reply_at(reply_index++, 45U, 7U, 0x51U, 1U, 6002U, 1U);
command_reply_at(reply_index++, 45U, 10U, 0x51U, 1U, 6002U, 1U);
CHECK(command_frame_count() == reply_index && command_action_calls == 1U);
if (ending == 2U) {
CHECK(IEC104_Station_Is_Reusable(&command_station));
CHECK(IEC104_Slave_Remove_Station(command_slave, &replacement) == IEC104_STATION_OK);
}
command_close();
CHECK(IEC104_Station_Is_Reusable(&replacement));
}
++command_checks;
}
/* ============================================================================
* [COMMAND/RETURNS] 应答收尾用例:持续突发、返回信息与交接寿命
* ============================================================================ */
/**
* 功能:验证以下场景与约束:用k=1及一个未ACK填充帧把事务回复留在HIGH,交叉覆盖有/无返校及成功/失败终止。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:FINISHED以事务最后一帧被HANDED为界;仅Send_Response接手OK还不能提前完成。
*/
static void test_command_response_tail(void)
{
unsigned has_return, success;
for (has_return = 0U; has_return <= 1U; ++has_return) {
for (success = 0U; success <= 1U; ++success) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
uint8_t filler[] = {200U, 0U, 3U, 0U, 1U, 0U};
unsigned handed, replies = 2U + has_return;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_window = 1U; command_open(&config);
CHECK(IEC60870_Asdu_Create_From_Buffer(&data,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), filler, sizeof(filler)) == IEC60870_ASDU_OK);
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &data) == IEC104_TCP_SUBMIT_OK);
command_poll();
event = command_execute(45U, 1U);
CHECK(IEC104_Command_Report_Result(command, &event,
success ? IEC104_COMMAND_RESULT_SUCCEEDED : IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_OK);
if (has_return != 0U) {
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
}
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process(); command_empty();
CHECK(command_status.busy && command_frame_count() == 0U);
for (handed = 1U; handed <= replies; ++handed) {
push_ack((uint16_t)handed); command_poll(); command_flush();
CHECK(command_frame_count() == handed);
if (handed < replies) { command_empty(); CHECK(command_status.busy); }
}
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (success ? IEC104_COMMAND_FINISH_COMPLETED : IEC104_COMMAND_FINISH_EXECUTION_FAILED));
CHECK(!finished.return_info_undelivered);
command_reply_at(replies - 1U, 45U, success ? 10U : 0x4aU, 0x51U, 1U, 6001U, 1U);
CHECK(command_action_calls == 1U && command_status.return_info_undelivered == 0U);
command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:保持NORMAL始终有积压,k=1下每次ACK只腾出一个发送窗口。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:确认、返校和终止仍须经HIGH依次推进;检查结束时NORMAL尚有待发帧,
* 排除“先排空突发才能完成遥控”的旧屏障行为。
*/
static void test_command_continuous_events(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC104_AsduQueueStatus_t queues;
IEC60870_Asdu_t filler, data;
uint8_t filler_bytes[] = {200U, 0U, 3U, 0U, 1U, 0U};
uint8_t data_bytes[10];
unsigned index;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_window = 1U; command_open(&config);
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave),
filler_bytes, sizeof(filler_bytes)) == IEC60870_ASDU_OK);
for (index = 0U; index < 12U; ++index) {
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &filler) == IEC104_TCP_SUBMIT_OK);
}
event = command_execute(45U, 1U);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
command_return(&data, data_bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process(); command_empty();
for (index = 1U; index <= 4U && command_frame_count() < 3U; ++index) {
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &filler) == IEC104_TCP_SUBMIT_OK);
push_ack((uint16_t)index); command_poll(); command_flush();
}
CHECK(command_frame_count() == 3U);
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
command_reply_at(2U, 45U, 10U, 0x51U, 1U, 6001U, 1U);
CHECK(IEC104_AsduQueue_Get_Status(&sut->queue, &queues) == IEC104_ASDU_QUEUE_OK);
CHECK(queues.entry_count[IEC104_ASDU_PRIORITY_NORMAL] >
queues.retained_unacked_count[IEC104_ASDU_PRIORITY_NORMAL]);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETED && !finished.return_info_undelivered);
command_close();
++command_checks;
}
/**
* 功能:验证以下场景与约束:异常终结时冻结返校交付快照,不能等应用读取FINISHED才重新查询。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:六种模式覆盖STOP、EOF、Cancel、Close、仅TERM未交付,以及两条返校只交付首条。
* Cancel后已接手的返校仍可能发出,但不能追改已冻结的undelivered结果。
*/
static void test_command_return_lifetime(void)
{
unsigned mode;
for (mode = 0U; mode < 6U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
bool undelivered = mode != 4U;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_window = 1U; command_open(&config);
event = command_execute(45U, 1U);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
if (mode == 5U) {
command_return(&data, bytes, 1U, 0U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
}
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process(); command_empty();
if (mode >= 4U) {
push_ack(1U); command_poll(); command_flush();
CHECK(command_frame_count() == 2U); command_empty();
}
if (mode == 1U) { fake.recv_eof = 1U; command_poll(); fake.recv_eof = 0U; }
else if (mode == 2U) {
IEC104_Command_Cancel(command);
/* Finish freezes PENDING before the old HIGH frame later hands off. */
push_ack(1U); command_poll(); command_flush();
CHECK(command_frame_count() == 2U);
} else if (mode == 3U) {
CHECK(IEC104_Slave_Close(command_slave, NULL) == IEC104_TCP_OK);
command_drain_transport();
} else { push(stop_act, sizeof(stop_act)); command_poll(); }
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.return_info_undelivered == (undelivered ? 1U : 0U));
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (mode == 2U ? IEC104_COMMAND_FINISH_CANCELLED :
(mode == 1U || mode == 3U ? IEC104_COMMAND_FINISH_DISCONNECTED : IEC104_COMMAND_FINISH_STOPPED)));
CHECK(finished.return_info_undelivered == undelivered);
CHECK(command_action_calls == 1U);
command_close();
}
/* A normal completion is already frozen before Close retires all tickets. */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); event = command_execute(45U, 1U);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_flush();
CHECK(IEC104_Slave_Close(command_slave, NULL) == IEC104_TCP_OK);
command_drain_transport();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETED && !finished.return_info_undelivered);
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.return_info_undelivered == 0U);
command_close();
}
++command_checks;
}
/* ============================================================================
* [COMMAND/ADMISSION] 动作前准入:HIGH余量、旧回复门禁与查询失败
* ============================================================================ */
/**
* 功能:先用最大填充帧占住当前发送,再按HIGH头开销计算第二帧长度,留下精确room字节。
* 输入参数:room——需保留的 HIGH 空间(字节)。
* 输出参数:无。
* 返回值:无。
* 说明:用真实队列构造边界,不覆盖状态快照;11/12字节分别不足/刚够45类确认。
*/
static void command_leave_high_room(size_t room)
{
uint8_t bytes[249] = {200U, 0U, 3U, 0U, 1U, 0U};
IEC60870_Asdu_t filler;
IEC104_TcpServerStatus_t status;
size_t length = IEC104_TCP_HIGH_CAPACITY - room - IEC104_ASDU_HIGH_ENTRY_HEADER_LEN;
CHECK(length >= 6U && length <= sizeof(bytes));
fake.send_blocked = 1U;
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
CHECK(IEC60870_MasterConnection_Send_ASDU(&command_last_input.connection, &filler));
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, length) == IEC60870_ASDU_OK);
CHECK(IEC60870_MasterConnection_Send_ASDU(&command_last_input.connection, &filler));
CHECK(IEC104_Slave_Get_Status(command_slave, &status, NULL) == IEC104_TCP_OK);
CHECK(status.asdu_queue_status_valid && status.high_queue.free_bytes == room);
}
/**
* 功能:验证以下场景与约束:在执行、有动作选择、纯逻辑选择三种模式下比较HIGH少1字节与刚好够用。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:预检WAIT不能调用动作,也不能消耗事件;恢复后同一事件可重试,但原决策期限不续期。
* confirm_room_waits只计准入等待,不应混入真正提交返回WAIT的waits。
*/
static void test_command_accept_capacity(void)
{
unsigned mode, short_room;
for (mode = 0U; mode < 3U; ++mode) {
for (short_room = 0U; short_room < 2U; ++short_room) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t info;
command_config_default(&config);
if (mode == 0U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, 6001U, mode == 0U ? 1U : 0x81U);
event = command_take(mode == 0U ? IEC104_COMMAND_EXECUTE : IEC104_COMMAND_SELECT);
command_leave_high_room(12U - short_room);
CHECK(IEC104_Command_Accept(command, &event, mode == 2U ? NULL : command_action,
&command_action_calls) == (short_room ? IEC104_COMMAND_REPLY_WAIT : IEC104_COMMAND_REPLY_OK));
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.confirm_room_waits == short_room && command_status.waits == 0U);
CHECK(command_status.pending_replies == 0U);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info));
CHECK(info.confirm_submitted == (short_room == 0U));
CHECK(command_action_calls == ((short_room == 0U && mode != 2U) ? 1U : 0U));
fake.send_blocked = 0U; command_poll(); command_flush();
if (short_room) {
CHECK(IEC104_Command_Accept(command, &event, mode == 2U ? NULL : command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(command_action_calls == (mode != 2U ? 1U : 0U));
}
command_flush();
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, mode == 0U ? 1U : 0x81U);
CHECK(command_frame_count() == 1U);
command_close();
}
}
/* Capacity refusal does not postpone the decision timeout. Its negative
* reply is only arranged and may subsequently be dropped by cleanup. */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.decision_timeout_ms = 10U; config.handover_timeout_ms = 10U;
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE); command_congest();
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
fake.now += 10U;
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DECISION_TIMEOUT && !finished.execute_effect);
CHECK(command_action_calls == 0U);
fake.now += 10U; command_empty();
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.busy && command_status.dropped_replies == 1U);
CHECK(command_status.confirm_room_waits == 1U && command_status.decision_timeouts == 1U);
CHECK(command_frame_count() == 0U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:即使HIGH已腾出空间,Accept也不代替Process发送本模块的旧回复,且必须先消费
* 新到达的Slave通知。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:其他点和不同OA同点的忙否定均须先于原执行确认,检查这两道门禁。
*/
static void test_command_accept_old_reply_gate(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
uint8_t other[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE); command_congest();
command_send(45U, 6U, 0x52U, 1U, 6002U, 1U);
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.pending_replies == 1U);
fake.send_blocked = 0U; command_poll();
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
CHECK(command_action_calls == 0U);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.pending_replies == 1U && command_status.confirm_room_waits == 0U);
command_process(); command_poll();
command_bytes(other, 45U, 6U, 0x53U, 1U, 6001U, 1U);
command_queue(other, sizeof(other));
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
CHECK(command_action_calls == 0U);
command_drain_transport();
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
command_reply_at(0U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U);
command_reply_at(1U, 45U, 0x47U, 0x53U, 1U, 6001U, 1U);
command_reply_at(2U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
CHECK(command_action_calls == 1U && command_frame_count() == 3U);
command_close(); ++command_checks;
}
#ifdef IEC104_COMMAND_TEST_HOOKS
/**
* 功能:验证以下场景与约束:服务查询失败和统计无效均终结为SERVICE_ERROR,动作次数为0。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:不能误计容量等待,也不能生成本来需要动作后才能发送的肯定确认。
*/
static void test_command_capacity_query_faults(void)
{
unsigned fault;
for (fault = 1U; fault <= 2U; ++fault) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
command_capacity_fault = fault;
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_ERROR);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_SERVICE_ERROR && !finished.execute_effect);
CHECK(command_action_calls == 0U && command_frame_count() == 0U);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.active && !command_status.busy);
CHECK(command_status.confirm_room_waits == 0U && command_status.waits == 0U);
CHECK(command_status.cancelled == 1U && command_status.dropped_replies == 0U);
command_capacity_fault = 0U; command_close();
}
++command_checks;
}
#endif
/* ============================================================================
* [COMMAND/COMPLETION] 完成期限:Complete、交接超时、优先级与ENDED回收
* ============================================================================ */
/**
* 功能:验证以下场景与约束:设备结果已报告,但应用未及时Complete:恰到期限即结束,不补发ACT_TERM。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:重复Report_Result不得续期;已执行的effect标志仍为真,超时不代表设备动作回滚。
* 另验证禁用期限和时钟倒退,以及返校晚交付不能改写超时快照。
*/
static void test_command_complete_timeout(void)
{
unsigned mode;
for (mode = 0U; mode < 4U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint64_t started;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.complete_timeout_ms = mode == 3U ? 0U : 10U;
command_open(&config); event = command_execute(45U, 1U); command_flush();
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
started = fake.now;
fake.now += 9U; command_empty();
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.timer_active == (mode != 3U));
CHECK(command_status.timeout_remaining_ms == (mode == 3U ? 0U : 1U));
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_ERROR);
if (mode == 3U) {
fake.now = started - 1U; command_empty();
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.clock_errors == 0U);
fake.now = started + 100000U;
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_flush(); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETED);
CHECK(command_status.complete_timeouts == 0U);
} else {
fake.now = mode == 2U ? started - 1U : started + 10U + mode;
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_STALE);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETE_TIMEOUT && finished.execute_effect);
CHECK(!finished.select_effect && !finished.return_info_undelivered);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.complete_timeouts == 1U && command_status.handover_timeouts == 0U);
CHECK(command_status.clock_errors == (mode == 2U ? 1U : 0U));
CHECK(command_status.completed == 0U && command_status.execution_failed == 0U);
CHECK(command_status.execute_timeouts == 0U && command_status.dropped_replies == 0U);
CHECK(!command_status.busy && !command_status.timer_active);
CHECK(command_frame_count() == 1U);
}
command_close();
}
/* Complete timeout freezes an accepted but unhanded return too. Later
* delivery must not rewrite FINISHED or undo its conservative counter. */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.complete_timeout_ms = 10U;
command_window = 1U; command_open(&config);
event = command_execute(45U, 1U); command_flush();
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
fake.now += 10U;
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETE_TIMEOUT && finished.return_info_undelivered);
CHECK(finished.execute_effect && !finished.select_effect);
push_ack(1U); command_poll(); command_flush();
command_reply_at(1U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
CHECK(command_frame_count() == 2U && command_status.return_info_undelivered == 1U);
CHECK(command_status.complete_timeouts == 1U && command_status.completed == 0U);
CHECK(command_status.dropped_replies == 0U);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:Complete之后等待交接超时:只可丢弃仍由模块保存的TERM。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:已交给HIGH的返校/TERM归传输层所有,可能在超时通知之后才发出;
* dropped_replies仅在本地TERM被清理时增加,不能把所有未HANDED帧都算丢弃。
*/
static void test_command_handover_timeout(void)
{
unsigned mode;
for (mode = 0U; mode < 4U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
uint64_t started;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.handover_timeout_ms = 10U;
if (mode != 0U) { command_window = 1U; }
command_open(&config); event = command_execute(45U, 1U); command_flush();
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
if (mode == 2U) {
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
}
if (mode == 0U) { command_congest(); }
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
started = fake.now;
command_process(); command_empty();
fake.now += 9U; command_process();
CHECK(command_status.timeout_remaining_ms == 1U && command_status.timer_active);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_ERROR);
fake.now = mode == 3U ? started - 1U : started + 10U;
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_HANDOVER_TIMEOUT && finished.execute_effect);
CHECK(finished.return_info_undelivered == (mode == 2U));
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.handover_timeouts == 1U && command_status.complete_timeouts == 0U);
CHECK(command_status.completed == 0U && command_status.clock_errors == (mode == 3U ? 1U : 0U));
CHECK(command_status.dropped_replies == (mode == 0U ? 1U : 0U));
CHECK(!command_status.busy && !command_status.timer_active && command_status.pending_replies == 0U);
fake.now = started + 11U;
if (mode == 0U) { fake.send_blocked = 0U; command_flush(); CHECK(command_frame_count() == 1U); }
else {
push_ack(1U); command_poll(); command_flush();
if (mode == 2U) { push_ack(2U); command_poll(); command_flush(); }
CHECK(command_frame_count() == (mode == 2U ? 3U : 2U));
command_reply_at(mode == 2U ? 2U : 1U, 45U, 10U, 0x51U, 1U, 6001U, 1U);
}
CHECK(command_status.return_info_undelivered == (mode == 2U ? 1U : 0U));
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:本地待提交TERM也是事务尾部,不能因为当前凭证已交付就提前FINISHED。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:真正尾帧已HANDED后再推进时钟,多个入口均应优先识别正常完成/执行失败,
* 而不是因观察得晚误报交接超时。
*/
static void test_command_completion_precedence(void)
{
unsigned trigger;
for (trigger = 0U; trigger < 5U; ++trigger) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.handover_timeout_ms = trigger == 0U ? 0U : 10U;
command_window = 1U; command_open(&config);
event = command_execute(45U, 1U); command_flush();
CHECK(IEC104_Command_Report_Result(command, &event,
trigger == 4U ? IEC104_COMMAND_RESULT_FAILED : IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_empty();
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_ERROR);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.busy && command_status.pending_replies == 1U && command_status.completed == 0U);
command_process(); command_empty();
if (trigger == 0U) {
uint64_t previous = fake.now;
--fake.now; command_empty();
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.timer_active && command_status.clock_errors == 0U);
fake.now = previous;
}
push_ack(1U); command_poll();
fake.now += 100U;
if (trigger == 1U) { CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_STALE); }
else if (trigger == 2U) {
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
} else if (trigger == 3U) {
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_STALE);
}
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (trigger == 4U ? IEC104_COMMAND_FINISH_EXECUTION_FAILED : IEC104_COMMAND_FINISH_COMPLETED));
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.completed == (trigger == 4U ? 0U : 1U));
CHECK(command_status.execution_failed == (trigger == 4U ? 1U : 0U) && command_status.handover_timeouts == 0U);
CHECK(command_status.dropped_replies == 0U && !command_status.busy);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:拒绝已使事务进入ENDED,但本地否定回复仍可能堵在队列里。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:覆盖FINISHED已读/未读、另有回复、未读Slave通知、禁用期限和时钟倒退;
* 到期清理只回收本事务回复,不重复发FINISHED,也不清掉另一条独立回复。
*/
static void test_command_ended_cleanup(void)
{
unsigned mode;
for (mode = 0U; mode < 6U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint64_t started;
uint8_t request[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.handover_timeout_ms = mode == 4U ? 0U : 10U;
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE); command_congest();
CHECK(IEC104_Command_Reject(command, &event, IEC104_COMMAND_REJECT_NEGATIVE) == IEC104_COMMAND_REPLY_OK);
started = fake.now;
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.pending_replies == 1U && command_status.timer_active == (mode != 4U));
if (mode == 1U || mode == 2U || mode == 4U) {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_REJECTED && !finished.execute_effect);
}
if (mode == 2U) {
command_send(45U, 6U, 0x52U, 1U, 6002U, 1U);
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.pending_replies == 2U);
}
fake.now = started + 5U; command_process();
CHECK(command_status.timeout_remaining_ms == (mode == 4U ? 0U : 5U));
if (mode == 3U) {
command_bytes(request, 45U, 6U, 0x53U, 1U, 6001U, 1U);
command_queue(request, sizeof(request));
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
CHECK(IEC104_Command_Get_Status(command, &command_status) && command_status.events_pending);
}
if (mode == 4U) {
fake.now = started - 1U; command_empty();
fake.now = started + 1000U; command_empty();
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.busy && command_status.pending_replies == 1U);
CHECK(command_status.dropped_replies == 0U && command_status.clock_errors == 0U);
fake.send_blocked = 0U; command_flush();
CHECK(!command_status.busy && !command_status.timer_active);
command_reply_at(0U, 45U, 0x47U, 0x51U, 1U, 6001U, 1U);
} else {
fake.now = mode == 5U ? started - 1U : started + 10U;
if (mode == 0U || mode == 3U || mode == 5U) {
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_REJECTED && !finished.execute_effect);
} else { command_empty(); }
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.busy && !command_status.timer_active);
CHECK(command_status.pending_replies == (mode == 2U ? 1U : 0U));
CHECK(command_status.dropped_replies == 1U && command_status.app_rejected == 1U);
CHECK(command_status.clock_errors == (mode == 5U ? 1U : 0U));
CHECK(command_status.handover_timeouts == 0U && command_status.complete_timeouts == 0U);
CHECK(command_status.dropped_requests == 0U);
if (mode == 0U || mode == 1U || mode == 5U) { CHECK(!command_status.need_process); }
if (mode == 2U) {
fake.send_blocked = 0U; command_flush();
command_reply_at(0U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U);
CHECK(command_frame_count() == 1U && command_status.pending_replies == 0U);
}
}
fake.now = started + 1001U;
command_close();
}
/* An unread FINISHED alone must not keep the expired timer alive. */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.handover_timeout_ms = 10U;
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
CHECK(IEC104_Command_Reject(command, &event, IEC104_COMMAND_REJECT_NEGATIVE) == IEC104_COMMAND_REPLY_OK);
command_flush(); fake.now += 100U;
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.busy && command_status.pending_events == 1U);
CHECK(command_status.pending_replies == 0U && !command_status.timer_active && !command_status.need_process);
CHECK(command_take(IEC104_COMMAND_FINISHED).reason == IEC104_COMMAND_FINISH_REJECTED);
command_close();
}
++command_checks;
}
/* ============================================================================
* [COMMAND/ACTION_TIME] 动作前时效:等待中过期、拒绝原因及凭证故障
* ============================================================================ */
/**
* 功能:验证以下场景与约束:带时标请求受理时有效不代表动作时仍有效。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:选择/执行、是否否定应答、是否先遇
* 容量WAIT做交叉组合。过期后重试必须STALE且动作次数为0,计入动作前时效计数,
* 不计成请求到达时的拒绝或设备执行超时。
*/
static void test_command_action_time_expiry(void)
{
unsigned selecting, negative, capacity_wait;
for (selecting = 0U; selecting < 2U; ++selecting) {
for (negative = 0U; negative < 2U; ++negative) {
for (capacity_wait = 0U; capacity_wait < 2U; ++capacity_wait) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32], payload = selecting ? 0x81U : 1U;
size_t length;
command_timed_config(&config, 58U, 10U);
if (selecting) { command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE); }
config.negative_on_expired = negative != 0U;
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
&payload, 1U, command_epoch_time);
command_raw_send(bytes, length);
event = command_take(selecting ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
if (capacity_wait) {
command_congest();
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
}
fake.now += 10U;
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_action_calls == 0U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_TIME_EXPIRED);
CHECK(!finished.select_effect && !finished.execute_effect && !finished.return_info_undelivered);
CHECK(finished.request.time_utc_ms == command_epoch_ms);
fake.send_blocked = 0U; command_flush();
CHECK(command_status.action_time_expired == 1U && command_status.action_time_rejected == 0U);
CHECK(command_status.time_expired == 0U && command_status.time_rejected == 0U);
CHECK(command_status.rejected == 0U && command_status.dropped_requests == 0U);
CHECK(command_status.completed == 0U && command_status.confirm_room_waits == capacity_wait);
CHECK(command_status.negative_submitted == negative && command_frame_count() == negative);
if (negative) { command_raw_reply_at(0U, bytes, length, 0x71U); }
CHECK(!command_status.busy);
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:使用真实默认10s时标窗/15s决定期限、UTC+8及过期否定策略,不覆盖回复配置。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:初始年龄0/2s分别只剩10/8s:边界前1ms仍可接受,恰好到界必须拒绝。
* Status报告阶段期限,Process不因时标到界自动终结;Accept仍须独立复查。
* 核对否定原文、唯一FINISHED及重复Accept,防止把查询余量误当成执行许可。
*/
static void test_command_default_time_window(void)
{
static const uint8_t local_time[7] = {0U, 0U, 0U, 8U, 1U, 1U, 26U};
unsigned selecting, aged, expired;
for (selecting = 0U; selecting < 2U; ++selecting) {
for (aged = 0U; aged < 2U; ++aged) {
for (expired = 0U; expired < 2U; ++expired) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32], payload = selecting ? 0x81U : 1U;
uint32_t age = aged ? 2000U : 0U;
uint32_t waited = 10000U - age - (expired ? 0U : 1U);
size_t length;
command_config_default(&config);
CHECK(config.time_tag_max_delay_ms == 10000U && config.decision_timeout_ms == 15000U);
CHECK(config.master_utc_offset_minutes == 480 && config.negative_on_expired);
command_config_mode(&config, selecting ? IEC104_COMMAND_MODE_SELECT_EXECUTE :
IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms + age, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
&payload, 1U, local_time);
command_raw_send(bytes, length);
event = command_take(selecting ? IEC104_COMMAND_SELECT : IEC104_COMMAND_EXECUTE);
CHECK(event.request.time_utc_ms == command_epoch_ms);
fake.now += waited;
command_process();
CHECK(command_status.busy && command_status.timer_active && !command_status.need_process);
CHECK(command_status.timeout_remaining_ms == 15000U - waited);
CHECK(command_status.action_time_expired == 0U && command_status.decision_timeouts == 0U);
if (expired) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_action_calls == 0U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_TIME_EXPIRED);
CHECK(!finished.select_effect && !finished.execute_effect && !finished.return_info_undelivered);
CHECK(finished.transaction_id == event.transaction_id &&
finished.request.time_utc_ms == command_epoch_ms);
command_flush();
command_empty();
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_process();
CHECK(command_action_calls == 0U && command_frame_count() == 1U);
command_raw_reply_at(0U, bytes, length, 0x71U);
CHECK(command_status.action_time_expired == 1U && command_status.action_time_rejected == 0U);
CHECK(command_status.time_expired == 0U && command_status.time_rejected == 0U);
CHECK(command_status.decision_timeouts == 0U && command_status.negative_submitted == 1U);
CHECK(!command_status.busy && !command_status.timer_active && !command_status.need_process);
CHECK(command_status.pending_events == 0U && command_status.pending_replies == 0U);
} else {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
command_flush();
CHECK(command_action_calls == 1U && command_frame_count() == 1U);
command_raw_reply_at(0U, bytes, length, 7U);
CHECK(command_status.action_time_expired == 0U && command_status.negative_submitted == 0U);
if (!selecting) { (void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED); }
}
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:受理后清除/改变UTC基准仍须重新验证动作时效。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:单调决策时钟倒退另归决策超时。
* 模式5显式禁用时效检查,模式6由应用主动拒绝;二者不能误计为时基拒绝。
* 后半段还验证:旧回复门禁解除后,才对将要执行的动作检查已经过期的时标。
*/
static void test_command_action_time_rejections(void)
{
unsigned mode;
for (mode = 0U; mode < 7U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint8_t bytes[32], payload = 1U;
size_t length;
uint64_t started;
const uint64_t maximum = (uint64_t)IEC60870_TIME_MAX_SECONDS * 1000U + 999U;
command_timed_config(&config, 58U, mode == 5U ? 0U : 100U);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
&payload, 1U, command_epoch_time);
command_raw_send(bytes, length); event = command_take(IEC104_COMMAND_EXECUTE);
started = fake.now;
switch (mode) {
case 1U:
fake.now += 2U;
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms + 2U, fake.now));
--fake.now;
break;
case 2U:
CHECK(IEC104_Command_Set_Time_Base(command, maximum, fake.now));
++fake.now;
break;
case 3U: CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms - 100U, fake.now)); break;
case 4U: --fake.now; break;
default: IEC104_Command_Clear_Time_Base(command); break;
}
if (mode == 5U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
CHECK(command_action_calls == 1U);
finished = command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_COMPLETED);
} else if (mode == 6U) {
CHECK(IEC104_Command_Reject(command, &event, IEC104_COMMAND_REJECT_NEGATIVE) == IEC104_COMMAND_REPLY_OK);
command_flush(); finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_REJECTED && command_action_calls == 0U);
} else {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (mode == 4U ? IEC104_COMMAND_FINISH_DECISION_TIMEOUT : IEC104_COMMAND_FINISH_TIME_REJECTED));
CHECK(command_action_calls == 0U && !finished.execute_effect);
}
fake.now = started + 10U; command_flush();
CHECK(command_status.action_time_rejected == (mode < 4U ? 1U : 0U));
CHECK(command_status.action_time_expired == 0U && command_status.time_rejected == 0U && command_status.time_expired == 0U);
CHECK(command_status.clock_errors == (mode == 1U || mode == 4U ? 1U : 0U));
CHECK(command_status.decision_timeouts == (mode == 4U ? 1U : 0U));
if (mode < 4U || mode == 6U) { command_raw_reply_at(0U, bytes, length, mode < 4U ? 0x71U : 0x47U); }
command_close();
}
/* A pending old reply gates before the time guard. Freshness is checked
* as soon as that predecessor has been submitted, not on blocked retries. */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
uint8_t bytes[32], payload = 1U;
size_t length;
command_timed_config(&config, 58U, 10U); command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_timed_bytes(bytes, 58U, 6U, 0x51U, 1U, 6001U,
&payload, 1U, command_epoch_time);
command_raw_send(bytes, length); event = command_take(IEC104_COMMAND_EXECUTE); command_congest();
++bytes[6]; bytes[3] = 0x52U;
command_raw_send(bytes, length);
fake.now += 10U;
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_WAIT);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.action_time_expired == 0U && command_status.confirm_room_waits == 0U);
fake.send_blocked = 0U; command_flush();
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_take(IEC104_COMMAND_FINISHED).reason == IEC104_COMMAND_FINISH_TIME_EXPIRED);
CHECK(command_action_calls == 0U); command_close();
}
++command_checks;
}
#ifdef IEC104_COMMAND_TEST_HOOKS
/**
* 功能:验证以下场景与约束:强制尾部凭证查询失败,覆盖请求分派、结果报告、返校提交和NextEvent入口。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:都须以SERVICE_ERROR结束已执行事务并保留execute_effect;新到达请求不得被
* 接受或额外生成回复,避免查询故障被当作旧事务已正常交付。
*/
static void test_command_response_ticket_faults(void)
{
unsigned entry;
for (entry = 0U; entry < 4U; ++entry) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.handover_timeout_ms = 0U;
command_window = 1U; command_open(&config);
event = command_execute(45U, 1U); command_flush();
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process(); command_empty();
command_response_error = true;
if (entry == 0U) {
IEC104_SlaveEvent_t input;
IEC104_TcpEventResult_t next;
unsigned seen = 0U;
command_bytes(bytes, 45U, 6U, 0x52U, 1U, 6002U, 1U);
command_queue(bytes, sizeof(bytes));
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
while ((next = IEC104_Slave_NextEvent(command_slave, &input)) == IEC104_TCP_EVENT_READY) {
IEC104_CommandEventResult_t handled = IEC104_Command_Handle_Event(command, &input);
if (input.transport.type == IEC104_TCP_EVENT_I_FRAME) {
CHECK(handled == IEC104_COMMAND_EVENT_ERROR); ++seen;
} else { CHECK(handled != IEC104_COMMAND_EVENT_ERROR); }
}
CHECK(next == IEC104_TCP_EVENT_EMPTY && seen == 1U);
} else if (entry == 1U) {
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_ERROR);
} else if (entry == 2U) {
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_ERROR);
}
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_SERVICE_ERROR && finished.execute_effect);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(!command_status.active && !command_status.busy);
CHECK(command_status.accepted == 1U && command_status.submitted == 2U);
CHECK(command_status.pending_replies == 0U && command_status.rejected == 0U);
CHECK(command_status.cancelled == 1U && command_status.handover_timeouts == 0U);
CHECK(command_action_calls == 1U && command_frame_count() == 1U);
command_response_error = false;
command_empty(); command_close();
}
++command_checks;
}
#endif
/* ============================================================================
* [COMMAND/ABNORMAL] 异常收尾:失败/超时否定TERM、期限及会话边界
* ============================================================================ */
/**
* 功能:验证以下场景与约束:NOT_STARTED/动作FAILED没有进入已启动流程,仍只回否定ACT_CON。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:固定失败TERM规则不能把拒绝启动改写为执行后终止,也不能重做设备动作。
*/
static void test_command_action_failure_boundary(void)
{
unsigned outcome;
for (outcome = 0U; outcome < 2U; ++outcome) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config);
command_action_result = outcome ? IEC104_COMMAND_ACTION_FAILED : IEC104_COMMAND_ACTION_NOT_STARTED;
event = command_execute(45U, 1U); command_flush();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (outcome ? IEC104_COMMAND_FINISH_ACTION_FAILED : IEC104_COMMAND_FINISH_REJECTED));
CHECK(finished.execute_effect == (outcome != 0U) && command_action_calls == 1U);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_frame_count() == 1U);
command_reply_at(0U, 45U, 0x47U, 0x51U, 1U, 6001U, 1U);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:明确失败的TERM与成功路径共用HIGH/FIFO:确认、返回信息、旧忙回复、否定TERM。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:满HIGH时不重做动作,不空转,也不提前交付已知结果的FINISHED。
* negative_submitted最终为2:一条是新命令的忙否定ACT_CON,一条是原事务的失败TERM。
*/
static void test_command_negative_term_wait(void)
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
command_open(&config); event = command_execute(45U, 1U); command_flush();
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
command_flush(); command_leave_high_room(0U);
command_send(45U, 6U, 0x52U, 1U, 6002U, 1U);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_OK);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process(); command_empty();
CHECK(command_status.pending_replies == 2U && !command_status.need_process);
CHECK(command_status.busy && command_status.execution_failed == 0U);
CHECK(command_frame_count() == 2U && command_action_calls == 1U);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_ERROR);
fake.send_blocked = 0U; command_flush();
CHECK(command_frame_count() == 4U);
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
command_reply_at(2U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U);
command_reply_at(3U, 45U, 0x4aU, 0x51U, 1U, 6001U, 1U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_EXECUTION_FAILED && finished.execute_effect);
CHECK(command_status.negative_submitted == 2U && command_status.return_info_submitted == 1U);
CHECK(command_action_calls == 1U);
command_empty(); command_close(); ++command_checks;
}
/**
* 功能:所有会推进期限的公开入口必须执行相同超时转换。
* 输入参数:entry——公开入口选择号;event——业务或传输事件(只读);finished——终结事件存储。
* 输出参数:finished——NextEvent 分支取出的 FINISHED 事件。
* 返回值:true 表示 NextEvent 分支已取走 FINISHED;false 表示由调用方继续读取终结事件。
* 说明:entry依次覆盖Process、NextEvent、Report_Result、Complete、Submit_Return_Info和I帧Handle_Event。
* 返回true只表示NextEvent已取走FINISHED,其余入口由调用方统一读取,避免误测成重复通知。
* 新I帧触发到期后,原槽仍欠FINISHED/TERM,新的命令只能获得忙否定而不能执行。
*/
static bool command_timeout_entry(unsigned entry, const IEC104_CommandEvent_t *event,
IEC104_CommandEvent_t *finished)
{
IEC60870_Asdu_t data;
uint8_t bytes[10];
if (entry == 0U) { CHECK(IEC104_Command_Process(command)); }
else if (entry == 1U) {
CHECK(IEC104_Command_NextEvent(command, finished) == IEC104_COMMAND_NEXT_READY);
return true;
} else if (entry == 2U) {
CHECK(IEC104_Command_Report_Result(command, event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_STALE);
} else if (entry == 3U) { CHECK(IEC104_Command_Complete(command, event) == IEC104_COMMAND_REPLY_STALE); }
else if (entry == 4U) {
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, event, &data) == IEC104_COMMAND_REPLY_STALE);
} else {
command_send(45U, 6U, 0x52U, 1U, 6002U, 1U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_HANDLED);
}
return false;
}
/**
* 功能:验证以下场景与约束:9ms未到期,10ms固定产生否定TERM,但结果仍未知。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:先读FINISHED再解堵,
* 检查ENDED事务仍保存TERM、连接保持激活,迟到结果不能覆盖原因或重复动作。
* info.active=false表示应用事务结束,status.active=true表示会话仍激活;两者不矛盾。
* 未知结果只计execute_timeouts,不计completed/execution_failed;背压解除前也不计negative_submitted。
*/
static void test_command_timeout_term_entries(void)
{
unsigned entry;
for (entry = 0U; entry < 6U; ++entry) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished, next_event;
IEC104_CommandTransactionInfo_t info;
IEC104_TcpServerStatus_t transport;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.execute_timeout_ms = 10U;
command_open(&config); event = command_execute(45U, 1U); command_flush();
command_leave_high_room(0U);
fake.now += 9U; command_process(); command_empty();
CHECK(command_status.timeout_remaining_ms == 1U && command_status.active);
++fake.now;
if (!command_timeout_entry(entry, &event, &finished)) { finished = command_take(IEC104_COMMAND_FINISHED); }
CHECK(finished.type == IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT && finished.execute_effect);
CHECK(finished.transaction_id == event.transaction_id && !finished.select_effect);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info));
CHECK(!info.active && info.phase == IEC104_COMMAND_PHASE_ENDED);
CHECK(!info.result_reported && !info.completed && !info.term_submitted);
command_process(); command_empty();
CHECK(command_status.active && command_status.busy && !command_status.need_process);
CHECK(command_status.accepted == 1U && command_status.execute_timeouts == 1U);
CHECK(command_status.completed == 0U && command_status.execution_failed == 0U);
CHECK(command_status.pending_replies == (entry == 5U ? 2U : 1U) && command_status.pending_events == 0U);
CHECK(command_status.negative_submitted == 0U && command_action_calls == 1U);
CHECK(IEC104_Slave_Get_Status(command_slave, &transport, NULL) == IEC104_TCP_OK);
CHECK(transport.listening && transport.connected && transport.generation == event.request.generation);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_STALE);
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_frame_count() == 1U);
fake.send_blocked = 0U; command_flush(); command_empty();
CHECK(command_frame_count() == (entry == 5U ? 3U : 2U));
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 45U, 0x4aU, 0x51U, 1U, 6001U, 1U);
if (entry == 5U) { command_reply_at(2U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U); }
CHECK(!command_status.busy && !command_status.timer_active && !command_status.need_process);
CHECK(command_status.active && command_status.execute_timeouts == 1U);
next_event = command_execute(45U, 1U);
CHECK(next_event.request.generation == event.request.generation);
(void)command_done(&next_event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 2U && command_status.execute_timeouts == 1U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:STOP/断线通知先于到期处理:不能给已停止的会话另造TERM。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:重新连接后旧事件仍STALE,新命令不受旧超时期限或返回结果影响。
*/
static void test_command_timeout_term_lifetime(void)
{
unsigned stop;
for (stop = 0U; stop < 2U; ++stop) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished, next_event;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.execute_timeout_ms = 10U;
command_open(&config); event = command_execute(45U, 1U); command_flush();
fake.now += 10U;
if (stop) { push(stop_act, sizeof(stop_act)); }
else { fake.recv_eof = 1U; }
CHECK(IEC104_Slave_Poll(command_slave, 0U, NULL) == IEC104_TCP_OK);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_SUCCEEDED) == IEC104_COMMAND_REPLY_WAIT);
command_drain_transport(); fake.recv_eof = 0U;
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (stop ? IEC104_COMMAND_FINISH_STOPPED : IEC104_COMMAND_FINISH_DISCONNECTED));
CHECK(finished.execute_effect && command_frame_count() == 1U);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.execute_timeouts == 0U && !command_status.active);
if (stop) { fake.recv_eof = 1U; command_poll(); fake.recv_eof = 0U; }
command_connect();
CHECK(command_status.generation != event.request.generation);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_STALE);
fake.sent_bytes = 0U; next_event = command_execute(45U, 1U);
(void)command_done(&next_event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_frame_count() == 2U && command_action_calls == 2U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:固定超时TERM仅适用于未取得执行结果:Complete/Handover仍按原寿命清理。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:期限0只禁用正向等待限制,时钟回退仍按执行超时收尾;返回信息未交接标志首次结束即冻结。
*/
static void test_command_timeout_term_boundaries(void)
{
unsigned mode;
for (mode = 0U; mode < 4U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
uint64_t started;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.execute_timeout_ms = 0U;
config.complete_timeout_ms = 10U; config.handover_timeout_ms = 10U;
command_open(&config); event = command_execute(45U, 1U); command_flush();
started = fake.now;
if (mode < 2U) {
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_OK);
if (mode == 1U) {
command_leave_high_room(0U);
CHECK(IEC104_Command_Complete(command, &event) == IEC104_COMMAND_REPLY_OK);
command_process();
}
fake.now += 10U; finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == (mode ? IEC104_COMMAND_FINISH_HANDOVER_TIMEOUT : IEC104_COMMAND_FINISH_COMPLETE_TIMEOUT));
CHECK(finished.execute_effect);
fake.send_blocked = 0U; command_flush();
CHECK(command_status.active && command_status.execute_timeouts == 0U);
CHECK(command_frame_count() == 1U);
} else if (mode == 2U) {
fake.now += 100000U; command_process(); command_empty();
CHECK(command_status.active && command_status.busy && !command_status.timer_active);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_frame_count() == 2U);
} else {
fake.now = started - 1U; finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT && finished.execute_effect);
CHECK(IEC104_Command_Get_Status(command, &command_status));
CHECK(command_status.clock_errors == 1U && command_status.execute_timeouts == 1U);
CHECK(command_status.execution_failed == 0U && command_frame_count() == 1U);
fake.now = started; command_flush(); command_empty();
CHECK(command_frame_count() == 2U && command_status.active);
command_reply_at(1U, 45U, 0x4aU, 0x51U, 1U, 6001U, 1U);
}
command_close();
}
/* k=1让返回信息留在HIGH;FINISHED先报告保守未交接事实,随后ACK可继续发返回信息和TERM。 */
for (mode = 0U; mode < 2U; ++mode) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC60870_Asdu_t data;
uint8_t bytes[10];
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.execute_timeout_ms = 10U;
if (mode == 1U) { command_window = 1U; }
command_open(&config); event = command_execute(45U, 1U); command_flush();
command_return(&data, bytes, 1U, 1U);
CHECK(IEC104_Command_Submit_Return_Info(command, &event, &data) == IEC104_COMMAND_REPLY_OK);
command_flush(); fake.now += 10U; command_process();
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT && finished.execute_effect);
CHECK(finished.return_info_undelivered == (mode != 0U));
if (mode) {
push_ack(1U); command_poll(); command_flush();
push_ack(2U); command_poll(); command_flush();
} else { command_flush(); }
command_empty();
CHECK(command_status.return_info_undelivered == mode);
CHECK(command_status.execute_timeouts == 1U && command_status.execution_failed == 0U);
CHECK(command_status.active && !command_status.busy && !command_status.need_process);
CHECK(command_frame_count() == 3U);
command_reply_at(1U, 1U, 11U, 0x51U, 1U, 1001U, 1U);
command_reply_at(2U, 45U, 0x4aU, 0x51U, 1U, 6001U, 1U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:肯定ACT_CON不得因执行到期被否定或删除。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:超时TERM排在它和旧忙回复之后。
* FINISHED先读后,残留回复仍有寿命;到期只清本事务,STOP/EOF清会话,均不重复FINISHED。
* ending=0正常解堵,1等待ENDED回复清理期限,2为STOP,3为EOF。
* ending=1仅丢弃原事务尚未交接的TERM并计一次dropped_replies,独立的旧忙回复继续发送。
*/
static void test_command_timeout_term_pending(void)
{
unsigned ending;
for (ending = 0U; ending < 4U; ++ending) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.execute_timeout_ms = 10U; config.handover_timeout_ms = 10U;
command_open(&config); event = command_execute(45U, 1U);
command_leave_high_room(0U);
command_send(45U, 6U, 0x52U, 1U, 6002U, 1U);
fake.now += 10U; finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT && finished.execute_effect);
CHECK(IEC104_Command_Report_Result(command, &event, IEC104_COMMAND_RESULT_FAILED) == IEC104_COMMAND_REPLY_STALE);
command_process(); command_empty();
CHECK(command_status.pending_replies == 2U && command_status.busy && !command_status.need_process);
CHECK(command_frame_count() == 1U && command_action_calls == 1U);
if (ending == 1U) {
fake.now += 10U; command_empty(); command_process();
CHECK(!command_status.busy && command_status.pending_replies == 1U);
CHECK(command_status.dropped_replies == 1U && command_status.active);
} else if (ending >= 2U) {
if (ending == 2U) { push(stop_act, sizeof(stop_act)); }
else { fake.recv_eof = 1U; }
command_poll(); fake.recv_eof = 0U;
command_empty();
}
/* EOF可等待已编号的填充帧写完才交付DISCONNECTED;放行传输后再核对会话收尾。 */
fake.send_blocked = 0U; command_flush(); command_empty();
CHECK(command_status.execute_timeouts == 1U && command_status.execution_failed == 0U);
if (ending == 0U) {
CHECK(command_frame_count() == 3U);
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U);
command_reply_at(2U, 45U, 0x4aU, 0x51U, 1U, 6001U, 1U);
} else if (ending == 1U) {
CHECK(command_frame_count() == 2U);
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U);
} else {
CHECK(command_frame_count() == 1U);
CHECK(!command_status.busy && !command_status.active && command_status.pending_replies == 0U);
}
CHECK(command_action_calls == 1U && !command_status.timer_active && !command_status.need_process);
command_close();
}
/* 先占满k窗口,使肯定确认和旧忙回复仍留HIGH、一个字节都未写出。
* confirm_submitted只证明发送层已受理原确认,不代表它已编号、写出或得到主站ACK。
* 执行超时先交付FINISHED,随后逐个ACK推进原确认、旧回复、否定TERM,顺序不变。
* 第一次ACK释放占窗的填充帧,此后才轮到原确认;不能把执行超时误写成撤销已受理确认。 */
{
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event, finished;
IEC104_CommandTransactionInfo_t info;
IEC60870_Asdu_t filler;
uint8_t bytes[] = {200U, 0U, 3U, 0U, 1U, 0U};
unsigned acknowledged;
command_config_default(&config); command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT);
config.execute_timeout_ms = 10U;
command_window = 1U; command_open(&config);
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &filler) == IEC104_TCP_SUBMIT_OK);
command_poll(); event = command_execute(45U, 1U);
command_send(45U, 6U, 0x52U, 1U, 6002U, 1U);
fake.now += 10U; finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_EXECUTE_TIMEOUT && finished.execute_effect);
CHECK(command_frame_count() == 0U);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info));
CHECK(info.confirm_submitted && !info.term_submitted && !info.result_reported);
for (acknowledged = 1U; acknowledged <= 3U; ++acknowledged) {
push_ack((uint16_t)acknowledged); command_poll(); command_flush();
CHECK(command_frame_count() == acknowledged);
}
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 1U);
command_reply_at(1U, 45U, 0x47U, 0x52U, 1U, 6002U, 1U);
command_reply_at(2U, 45U, 0x4aU, 0x51U, 1U, 6001U, 1U);
CHECK(command_status.execute_timeouts == 1U && command_status.active && command_action_calls == 1U);
command_empty(); command_close();
}
++command_checks;
}
/* ============================================================================
* [COMMAND/RESELECT] 重复选择复归:旧许可失效、清理事件与必要否定的FIFO
* ============================================================================ */
/**
* 功能:验证以下场景与约束:十二种有S/E类型统一复归。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:覆盖SELECT未领取、已领取、已确认以及TEST模拟选择。
* 同OA和异OA均不得让第二条选择接管旧许可;否定回显第二条,FINISHED仍携带第一条。
* 旧执行必须重新经历选择,且应用只对曾领取的工作事件收到一次FINISHED。
*/
static void test_command_duplicate_select_reset(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 58U, 59U, 60U, 61U, 62U, 63U};
size_t index;
unsigned phase, other_oa;
for (index = 0U; index < sizeof(types) / sizeof(types[0]); ++index) {
for (phase = 0U; phase < 4U; ++phase) {
for (other_oa = 0U; other_oa < 2U; ++other_oa) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event = {0}, finished, next;
IEC104_CommandTransactionInfo_t info;
uint8_t original[32], repeated[32], execute[32];
size_t length;
unsigned test = phase == 3U ? 0x80U : 0U;
unsigned prior = phase >= 2U ? 1U : 0U;
command_timed_config(&config, types[index], 1000U);
command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_qualified_bytes(original, types[index], 6U | test, 0U, true);
command_raw_send(original, length);
if (phase == 1U || phase == 2U) { event = command_take(IEC104_COMMAND_SELECT); }
if (phase == 2U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
}
command_flush();
memcpy(repeated, original, length);
repeated[3] = (uint8_t)(0x51U + other_oa);
command_raw_send(repeated, length); command_flush();
command_raw_reply_at(prior, repeated, length, 0x47U | test);
CHECK(command_frame_count() == prior + 1U && command_status.rejected == 1U);
CHECK(command_status.busy_rejected == 0U && command_status.dropped_requests == 0U);
CHECK(command_status.accepted == 1U && command_action_calls == (phase == 2U ? 1U : 0U));
if (phase == 1U || phase == 2U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Reject(command, &event,
IEC104_COMMAND_REJECT_NEGATIVE) == IEC104_COMMAND_REPLY_STALE);
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && !info.active &&
info.phase == IEC104_COMMAND_PHASE_ENDED);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DUPLICATE_SELECT &&
finished.transaction_id == event.transaction_id &&
finished.request.receive_serial == event.request.receive_serial &&
finished.request.originator_address == 0x51U);
CHECK(finished.select_effect == (phase == 2U) && !finished.execute_effect);
}
command_empty(); command_process();
CHECK(!command_status.busy && command_status.pending_replies == 0U);
CHECK(command_qualified_bytes(execute, types[index], 6U | test, 0U, false) == length);
command_raw_send(execute, length); command_flush(); command_empty();
command_raw_reply_at(prior + 1U, execute, length, 0x47U | test);
CHECK(!command_status.busy);
command_raw_send(original, length);
if (phase == 3U) { command_flush(); command_empty(); }
else {
next = command_take(IEC104_COMMAND_SELECT);
CHECK(next.transaction_id != event.transaction_id);
CHECK(IEC104_Command_Accept(command, &next, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
command_raw_reply_at(prior + 2U, original, length, 7U | test);
CHECK(command_status.busy && command_status.accepted == 2U);
if (phase == 2U && other_oa != 0U) {
command_raw_send(execute, length);
next = command_take(IEC104_COMMAND_EXECUTE);
CHECK(next.transaction_id != event.transaction_id && next.request.selected);
CHECK(IEC104_Command_Accept(command, &next, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&next, IEC104_COMMAND_RESULT_SUCCEEDED);
command_raw_reply_at(prior + 3U, execute, length, 7U);
command_raw_reply_at(prior + 4U, execute, length, 10U);
CHECK(command_action_calls == 2U);
}
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:重复选择不要求命令值、合法QU或Type相同。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:该点允许的另一种选择同样结束旧许可。
* 已确认时保留原确认,第二条只得到否定,且从不产生第二个SELECT工作事件。
*/
static void test_command_duplicate_select_fields(void)
{
static const unsigned changed[][2] = {{45U, 0x80U}, {45U, 0x85U}, {46U, 0x82U}};
unsigned accepted, variant;
for (accepted = 0U; accepted < 2U; ++accepted) {
for (variant = 0U; variant < sizeof(changed) / sizeof(changed[0]); ++variant) {
IEC104_CommandEvent_t event, finished;
command_open(NULL); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
if (accepted != 0U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
command_send(changed[variant][0], 6U, 0x51U, 1U, 6001U, changed[variant][1]);
command_flush();
command_reply_at(accepted, changed[variant][0], 0x47U, 0x51U, 1U, 6001U, changed[variant][1]);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DUPLICATE_SELECT &&
finished.request.type_id == 45U && finished.request.state == 1U &&
finished.request.qualifier == 0U && finished.transaction_id == event.transaction_id);
CHECK(command_action_calls == accepted && command_frame_count() == accepted + 1U);
command_empty(); command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:独立短回复满池也不能阻止复归或丢失必要否定。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:更早的异点忙回复按FIFO保留。
* 覆盖SELECT_WAIT、已交接CON以及TEST未交接CON;TEST旧肯定确认必须撤掉。
* 真实Accept有动作前容量准入,使用TEST验证共用SELECTED状态下的模块持有CON。
* FINISHED先消费仍不能过早释放持有否定的事务槽,发送恢复后才完成回收。
*/
static void test_command_duplicate_select_backpressure(void)
{
unsigned phase, index;
for (phase = 0U; phase < 3U; ++phase) {
IEC104_CommandEvent_t event = {0}, finished;
IEC104_CommandTransactionInfo_t info;
unsigned test = phase == 2U ? 0x80U : 0U;
unsigned prior = phase == 1U ? 1U : 0U;
command_open(NULL);
if (phase == 2U) {
command_send(44U, 6U, 0x51U, 1U, 6001U, 1U);
command_congest();
}
command_send(45U, 6U | test, 0x51U, 1U, 6001U, 0x81U);
if (phase != 2U) {
event = command_take(IEC104_COMMAND_SELECT);
if (phase == 1U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) &&
info.phase == IEC104_COMMAND_PHASE_SELECTED && info.confirm_submitted);
}
command_congest();
}
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY; ++index) {
command_send(45U, 6U | test, 0x60U + index, 1U, 6002U, 0x81U);
}
command_process();
CHECK(command_status.pending_replies == IEC104_COMMAND_REPLY_CAPACITY + (phase >= 2U ? 1U : 0U));
command_send(46U, 6U | test, 0x52U, 1U, 6001U, 0x86U);
command_process();
CHECK(command_status.busy && command_status.pending_replies == IEC104_COMMAND_REPLY_CAPACITY + 1U);
CHECK(command_status.rejected == IEC104_COMMAND_REPLY_CAPACITY + 1U &&
command_status.busy_rejected == IEC104_COMMAND_REPLY_CAPACITY &&
command_status.overloads == 0U && command_status.dropped_requests == 0U);
CHECK(command_frame_count() == prior);
if (phase != 2U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DUPLICATE_SELECT &&
finished.select_effect == (phase != 0U) && !finished.execute_effect);
}
command_empty(); command_process();
CHECK(command_status.busy && !IEC104_Command_Destroy(command));
fake.send_blocked = 0U; command_flush(); command_empty();
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY; ++index) {
command_reply_at(prior + index, 45U, 0x47U | test, 0x60U + index, 1U, 6002U, 0x81U);
}
command_reply_at(prior + index, 46U, 0x47U | test, 0x52U, 1U, 6001U, 0x86U);
CHECK(command_frame_count() == prior + index + 1U && !command_status.busy);
CHECK(command_status.pending_replies == 0U && command_status.negative_submitted == index + 1U);
CHECK(command_action_calls == (phase == 1U ? 1U : 0U));
command_close();
}
/* 已入HIGH的真实CON即使尚未编号也不再归Command所有。先填满k窗口,
* 复归后FINISHED立即可取;逐个ACK必须先写出旧肯定CON,再写第二条的否定CON。 */
{
IEC104_CommandEvent_t event, finished;
IEC104_CommandTransactionInfo_t info;
IEC60870_Asdu_t filler;
uint8_t bytes[] = {200U, 0U, 3U, 0U, 1U, 0U};
command_window = 1U; command_open(NULL);
CHECK(IEC60870_Asdu_Create_From_Buffer(&filler,
IEC104_Slave_Get_Application_Layer_Parameters(command_slave), bytes, sizeof(bytes)) == IEC60870_ASDU_OK);
CHECK(IEC104_Slave_Enqueue_Event(command_slave, &filler) == IEC104_TCP_SUBMIT_OK);
command_poll(); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush();
CHECK(IEC104_Command_Get_Transaction(command, &event, &info) && info.confirm_submitted);
CHECK(command_frame_count() == 0U);
command_send(45U, 6U, 0x52U, 1U, 6001U, 0x81U);
finished = command_take(IEC104_COMMAND_FINISHED);
CHECK(finished.reason == IEC104_COMMAND_FINISH_DUPLICATE_SELECT && finished.select_effect);
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_STALE);
CHECK(command_frame_count() == 0U);
for (index = 1U; index <= 2U; ++index) {
push_ack((uint16_t)index); command_poll(); command_flush();
CHECK(command_frame_count() == index);
}
command_reply_at(0U, 45U, 7U, 0x51U, 1U, 6001U, 0x81U);
command_reply_at(1U, 45U, 0x47U, 0x52U, 1U, 6001U, 0x81U);
CHECK(command_action_calls == 1U && !command_status.busy);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:全局认识但点表禁用的选择、跨T选择和异点选择都不属于原事务的有效重复选择。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:原工作身份保持有效;EXECUTE_WAIT/EXECUTING中的选择也不能取消已接纳的执行。
*/
static void test_command_duplicate_select_isolation(void)
{
static const IEC104_CommandPoint_t points[] = {
{1U, 6001U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE},
{1U, 6002U, IEC104_COMMAND_TYPE_BIT(45U), IEC104_COMMAND_MODE_SELECT_EXECUTE}
};
unsigned phase;
for (phase = 0U; phase < 4U; ++phase) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t before, after;
unsigned prior = 0U;
command_config_default(&config); config.points = points;
config.point_count = sizeof(points) / sizeof(points[0]);
command_open(&config); command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U);
event = command_take(IEC104_COMMAND_SELECT);
if (phase >= 1U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush(); ++prior;
}
if (phase >= 2U) {
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
}
if (phase == 3U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush(); ++prior;
}
CHECK(IEC104_Command_Get_Transaction(command, &event, &before));
if (phase <= 1U) {
command_send(46U, 6U, 0x51U, 1U, 6001U, 0x82U); command_flush();
command_reply_at(prior++, 46U, 0x47U, 0x51U, 1U, 6001U, 0x82U);
} else {
command_send(45U, 6U, 0x52U, 1U, 6001U, 0x81U); command_flush();
command_reply_at(prior++, 45U, 0x47U, 0x52U, 1U, 6001U, 0x81U);
command_send(45U, 6U, 0x51U, 1U, 6001U, 0x81U); command_flush();
if (phase == 3U) { command_reply_at(prior++, 45U, 0x47U, 0x51U, 1U, 6001U, 0x81U); }
else { CHECK(command_status.dropped_requests == 1U); }
}
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 0x81U); command_flush();
command_reply_at(prior++, 45U, 0xc7U, 0x51U, 1U, 6001U, 0x81U);
command_send(45U, 6U, 0x51U, 1U, 6002U, 0x81U); command_flush();
command_reply_at(prior++, 45U, 0x47U, 0x51U, 1U, 6002U, 0x81U);
command_empty();
CHECK(IEC104_Command_Get_Transaction(command, &event, &after) && after.active &&
after.phase == before.phase && after.confirm_submitted == before.confirm_submitted);
CHECK(command_frame_count() == prior);
if (phase == 0U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
if (phase < 2U) {
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
}
if (phase != 3U) {
CHECK(IEC104_Command_Accept(command, &event, command_action,
&command_action_calls) == IEC104_COMMAND_REPLY_OK);
}
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U);
command_close();
}
++command_checks;
}
/* ============================================================================
* [COMMAND/TEST_BIT] T位模拟:原文确认、SBO状态和真实动作隔离
* ============================================================================ */
/**
* 功能:构造十四类合法模拟请求。
* 输入参数:bytes——报文字节缓冲区;type——协议类型或期望事件类型;cause——完整传送原因字节;selecting——是否构造选择阶段。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:生成报文或结果数据的实际字节长度。
* 说明:51/64没有S/E和限定词,保持完整32位值。
*/
static size_t command_test_bytes(uint8_t *bytes, unsigned type, unsigned cause, bool selecting)
{
static const uint8_t bits[] = {0xd5U, 0xaaU, 0x80U, 0xffU};
if (type == 51U || type == 64U) {
return type == 64U ? command_timed_bytes(bytes, type, cause | 0x80U, 0x51U, 1U, 6001U,
bits, sizeof(bits), command_epoch_time) : command_payload_bytes(bytes, type, cause | 0x80U,
0x51U, 1U, 6001U, bits, sizeof(bits));
}
return command_qualified_bytes(bytes, type, cause | 0x80U, 0U, selecting);
}
/**
* 功能:验证以下场景与约束:所有类型的DIRECT模拟只发送肯定CON/TERM,不生成供应用决定或执行的事件。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:十二种SBO类型先选择、再执行或撤销;每帧保留T/OA/值/限定词/原时标。
* 选择和执行统计属于真实动作,模拟只计协议accepted/completed及发送数。
*/
static void test_command_test_bit_flows(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 51U,
58U, 59U, 60U, 61U, 62U, 63U, 64U};
size_t index;
unsigned mode;
for (index = 0U; index < sizeof(types) / sizeof(types[0]); ++index) {
for (mode = 0U; mode < (types[index] == 51U || types[index] == 64U ? 1U : 3U); ++mode) {
IEC104_CommandConfig_t config;
uint8_t bytes[32], selected[32];
size_t length;
unsigned replies = 0U;
command_timed_config(&config, types[index], 1000U);
if (mode != 0U) { command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE); }
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_test_bytes(bytes, types[index], 6U, false);
if (mode != 0U) {
/* 无选择的测试执行同样否定;不能因为T位直接跳过SBO。 */
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(replies++, bytes, length, 0xc7U);
CHECK(!command_status.busy && command_status.accepted == 0U);
CHECK(command_test_bytes(selected, types[index], 6U, true) == length);
command_raw_send(selected, length); command_flush(); command_empty();
command_raw_reply_at(replies++, selected, length, 0x87U);
CHECK(command_status.busy && command_status.timer_active && command_status.pending_events == 0U);
CHECK(command_status.timeout_remaining_ms == config.select_timeout_ms);
}
if (mode == 2U) { bytes[2] = 0x88U; }
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(replies++, bytes, length, mode == 2U ? 0x89U : 0x87U);
if (mode != 2U) { command_raw_reply_at(replies++, bytes, length, 0x8aU); }
CHECK(command_frame_count() == replies && command_action_calls == 0U);
CHECK(command_status.selected == 0U && command_status.executed == 0U);
CHECK(command_status.completed == (mode == 2U ? 0U : 1U));
CHECK(command_status.return_info_submitted == 0U && !command_status.busy);
CHECK(!command_status.timer_active && !command_status.need_process && command_status.pending_events == 0U);
command_close();
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:TEST激活/撤销不能命中同点真实SELECT_WAIT、SELECTED、EXECUTE_WAIT或EXECUTING。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:特别覆盖同OA:T必须先隔离,不能被同点静默重发分支吞掉,更不能清除真实选择。
* 原应用事件和阶段不变,之后真实动作仍只执行一次。
*/
static void test_command_test_bit_preserves_real(void)
{
unsigned phase, oa, cause;
for (phase = 0U; phase < 4U; ++phase) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t before, after;
unsigned replies = 0U;
command_config_default(&config);
if (phase >= 2U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
command_send(45U, 6U, 0x51U, 1U, 6001U, phase >= 2U ? 1U : 0x81U);
event = command_take(phase >= 2U ? IEC104_COMMAND_EXECUTE : IEC104_COMMAND_SELECT);
if (phase == 1U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush(); ++replies;
}
if (phase == 3U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
command_flush(); ++replies;
}
CHECK(IEC104_Command_Get_Transaction(command, &event, &before));
for (oa = 0x51U; oa <= 0x52U; ++oa) {
for (cause = 6U; cause <= 8U; cause += 2U) {
command_send(45U, cause | 0x80U, oa, 1U, 6001U, 1U);
command_flush(); command_empty();
command_reply_at(replies++, 45U, cause == 6U ? 0xc7U : 0xc9U, oa, 1U, 6001U, 1U);
CHECK(IEC104_Command_Get_Transaction(command, &event, &after));
CHECK(after.active && after.phase == before.phase && after.confirm_submitted == before.confirm_submitted);
CHECK(command_action_calls == (phase == 3U ? 1U : 0U) && command_status.dropped_requests == 0U);
}
}
if (phase == 0U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
if (phase < 2U) {
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
}
if (phase != 3U) {
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
}
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && command_status.executed == 1U);
command_empty(); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:模拟选择不能被真实激活/撤销消费,也不能被其他OA的测试执行/撤销取消。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:非法QU必须在匹配前独立否定;同OA的值失配则按原SBO规则结束该模拟选择。
* 模拟选择占唯一槽;结束后可以立即开始正常真实业务。
*/
static void test_command_test_bit_identity(void)
{
unsigned mismatch;
for (mismatch = 0U; mismatch < 2U; ++mismatch) {
IEC104_CommandEvent_t event;
unsigned replies = 0U, variant;
command_open(NULL);
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 0x81U); command_flush(); command_empty();
command_reply_at(replies++, 45U, 0x87U, 0x51U, 1U, 6001U, 0x81U);
for (variant = 0U; variant < 6U; ++variant) {
unsigned cause = (variant & 1U) ? 8U : 6U;
unsigned oa = variant < 2U || variant >= 4U ? 0x51U : 0x52U;
unsigned test = variant < 2U ? 0U : 0x80U;
unsigned value = variant >= 4U ? 0x11U : 1U;
command_send(45U, cause | test, oa, 1U, 6001U, value); command_flush(); command_empty();
command_reply_at(replies++, 45U, (cause == 6U ? 0x47U : 0x49U) | test, oa, 1U, 6001U, value);
CHECK(command_status.busy && command_status.pending_events == 0U && command_action_calls == 0U);
}
command_send(45U, mismatch ? 0x86U : 0x88U, 0x51U, 1U, 6001U, mismatch ? 0U : 1U);
command_flush(); command_empty();
command_reply_at(replies++, 45U, mismatch ? 0xc7U : 0x89U, 0x51U, 1U, 6001U, mismatch ? 0U : 1U);
CHECK(!command_status.busy && command_frame_count() == replies);
CHECK(command_status.mismatches == mismatch && command_status.selected == 0U && command_status.executed == 0U);
(void)command_select(45U, 1U);
event = command_execute(45U, 1U);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 2U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:T位不能绕过十二种限定词白名单、可信时间基准、IV或时差边界。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:negative_on_expired=false只作用于真实命令,测试过期必须给否定确认。
* 测试撤销的坏时标保持原模拟选择,随后新鲜撤销仍可成功。
*/
static void test_command_test_bit_validation(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 58U, 59U, 60U, 61U, 62U, 63U};
size_t index;
unsigned mode;
for (index = 0U; index < sizeof(types) / sizeof(types[0]); ++index) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
unsigned type = types[index];
size_t length;
command_timed_config(&config, type, 0U); command_open(&config);
length = command_qualified_bytes(bytes, type, 0x86U,
type <= 47U || (type >= 58U && type <= 60U) ? 4U : 1U, false);
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(0U, bytes, length, 0xc7U);
CHECK(command_frame_count() == 1U && command_action_calls == 0U && !command_status.busy);
command_close();
}
for (mode = 0U; mode < 5U; ++mode) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
unsigned replies = 0U;
command_timed_config(&config, 58U, 1000U); config.negative_on_expired = false;
if (mode == 4U) { command_config_mode(&config, IEC104_COMMAND_MODE_SELECT_EXECUTE); }
command_open(&config);
if (mode != 0U) { CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now)); }
length = command_test_bytes(bytes, 58U, 6U, mode == 4U);
if (mode == 4U) {
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(replies++, bytes, length, 0x87U);
bytes[2] = 0x88U;
}
if (mode == 1U) { bytes[length - 5U] |= 0x80U; }
else if (mode == 2U || mode == 4U) { fake.now += 1000U; }
else if (mode == 3U) { CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms - 1000U, fake.now)); }
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(replies++, bytes, length, 0xf1U);
CHECK(command_action_calls == 0U && command_status.accepted == (mode == 4U ? 1U : 0U));
CHECK(command_status.busy == (mode == 4U));
if (mode == 4U) {
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(replies++, bytes, length, 0x89U);
CHECK(!command_status.busy);
}
CHECK(command_frame_count() == replies); command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:模拟确认也遵守HIGH背压、选择确认先于执行、CON先于TERM和交接期限。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:等待发送不生成工作事件,不要求无网络进展的忙轮询;解堵后不重复模拟事务。
*/
static void test_command_test_bit_backpressure(void)
{
unsigned selecting;
for (selecting = 0U; selecting < 2U; ++selecting) {
IEC104_CommandConfig_t config;
command_config_default(&config);
if (!selecting) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
/* 未支持类型只取得本连接纯值身份,不产生应答/事务,再用该身份填满HIGH。 */
command_send(44U, 6U, 0x51U, 1U, 6001U, 1U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
command_leave_high_room(0U);
command_send(45U, 0x86U, 0x51U, 1U, 6001U, selecting ? 0x81U : 1U);
command_process(); command_empty();
CHECK(command_status.busy && command_status.pending_replies == (selecting ? 1U : 2U));
CHECK(!command_status.need_process && command_status.timer_active && command_action_calls == 0U);
if (selecting) {
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 1U);
command_process(); command_empty();
CHECK(command_status.busy && command_status.pending_replies == 2U);
}
fake.send_blocked = 0U; command_flush(); command_empty();
command_reply_at(0U, 45U, 0x87U, 0x51U, 1U, 6001U, selecting ? 0x81U : 1U);
command_reply_at(1U, 45U, selecting ? 0xc7U : 0x8aU, 0x51U, 1U, 6001U, 1U);
if (selecting) {
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 1U); command_flush(); command_empty();
command_reply_at(2U, 45U, 0x87U, 0x51U, 1U, 6001U, 1U);
command_reply_at(3U, 45U, 0x8aU, 0x51U, 1U, 6001U, 1U);
}
CHECK(command_frame_count() == (selecting ? 4U : 2U));
CHECK(!command_status.busy && !command_status.need_process && command_action_calls == 0U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:模拟选择沿用选择期限及Cancel/STOP/断线清理,但没有应用资源,所以始终无FINISHED。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:释放站点引用和唯一槽后,下一个真实选择仍能照常交应用;旧测试执行不能复活旧选择。
*/
static void test_command_test_bit_lifetime(void)
{
unsigned ending;
for (ending = 0U; ending < 4U; ++ending) {
IEC104_CommandConfig_t config;
command_config_default(&config); config.select_timeout_ms = 10U;
command_open(&config);
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 0x81U); command_flush(); command_empty();
if (ending == 0U) { fake.now += 10U; command_process(); }
else if (ending == 1U) { IEC104_Command_Cancel(command); }
else if (ending == 2U) { push(stop_act, sizeof(stop_act)); command_poll(); }
else { fake.recv_eof = 1U; command_poll(); fake.recv_eof = 0U; }
command_flush(); command_empty();
CHECK(!command_status.busy && !command_status.timer_active && !command_status.need_process);
CHECK(command_action_calls == 0U && command_frame_count() == 1U);
if (ending == 2U) { push(start_act, sizeof(start_act)); command_poll(); }
else if (ending == 3U) { command_connect(); }
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 1U); command_flush(); command_empty();
command_reply_at(1U, 45U, 0xc7U, 0x51U, 1U, 6001U, 1U);
CHECK(!command_status.busy && command_action_calls == 0U);
(void)command_select(45U, 1U);
CHECK(command_action_calls == 1U);
command_close();
}
++command_checks;
}
/* ============================================================================
* [COMMAND/ADDRESS] 撤销寻址:未知IOA、错误优先级、事务隔离与应答背压
* ============================================================================ */
/**
* 功能:验证以下场景与约束:十四种命令、真实/TEST均区分未知地址47与已知点无任务的否定9。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:低端0、相邻缺项、24位最大地址独立回显,防止只检查零地址或截成16位。
* 即使点表为空,已注册CA的未知地址也不能被描述成“没有可撤销事务”。
*/
static void test_command_deactivation_addresses(void)
{
static const unsigned types[] = {45U, 46U, 47U, 48U, 49U, 50U, 51U,
58U, 59U, 60U, 61U, 62U, 63U, 64U};
static const unsigned addresses[] = {0U, 6000U, 6001U, 6002U, 6003U, 0xffffffU};
size_t type_index, address_index;
unsigned test;
for (type_index = 0U; type_index < sizeof(types) / sizeof(types[0]); ++type_index) {
for (test = 0U; test <= 1U; ++test) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
size_t length;
command_timed_config(&config, types[type_index], 1000U);
command_open(&config);
CHECK(IEC104_Command_Set_Time_Base(command, command_epoch_ms, fake.now));
length = command_test_bytes(bytes, types[type_index], 8U, false);
bytes[2] = (uint8_t)(8U | (test ? 0x80U : 0U));
bytes[3] = 0x73U;
for (address_index = 0U; address_index < sizeof(addresses) / sizeof(addresses[0]); ++address_index) {
unsigned ioa = addresses[address_index];
unsigned reply = (ioa == 6001U || ioa == 6002U ? 0x49U : 0x6fU) | (test ? 0x80U : 0U);
bytes[6] = (uint8_t)ioa; bytes[7] = (uint8_t)(ioa >> 8); bytes[8] = (uint8_t)(ioa >> 16);
command_raw_send(bytes, length); command_flush(); command_empty();
CHECK(command_last_handled == IEC104_COMMAND_EVENT_HANDLED);
command_raw_reply_at((unsigned)address_index, bytes, length, reply);
CHECK(!command_status.busy && command_action_calls == 0U && command_status.accepted == 0U);
CHECK(command_status.rejected == address_index + 1U && command_status.busy_rejected == 0U);
CHECK(command_status.deactivated == 0U && command_status.pending_events == 0U);
}
CHECK(command_frame_count() == sizeof(addresses) / sizeof(addresses[0]));
command_close();
}
}
for (test = 0U; test <= 1U; ++test) {
IEC104_CommandConfig_t config;
IEC104_Command_Config_Default(&config);
command_open(&config);
command_send(45U, 8U | (test ? 0x80U : 0U), 0x73U, 1U, 6001U, 1U);
command_flush(); command_empty();
command_reply_at(0U, 45U, 0x6fU | (test ? 0x80U : 0U), 0x73U, 1U, 6001U, 1U);
CHECK(!command_status.busy && command_action_calls == 0U && command_status.accepted == 0U);
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:仅用包含完整单对象的报文检查错误优先级:未知COT45、未知CA46、未知IOA47
* 先于限定词/SQ拒绝。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:已知IOA的相同坏字段仍为否定9,不把已知点误报未知。
* 不支持的全局类型依旧交给外层44分派,覆盖见point_type_intersection。
*/
static void test_command_deactivation_address_precedence(void)
{
static const unsigned types[] = {45U, 49U, 58U, 62U};
size_t type_index;
unsigned test, variant;
for (type_index = 0U; type_index < sizeof(types) / sizeof(types[0]); ++type_index) {
for (test = 0U; test <= 1U; ++test) {
for (variant = 0U; variant < 6U; ++variant) {
IEC104_CommandConfig_t config;
uint8_t bytes[32];
unsigned type = types[type_index], flag = test ? 0x80U : 0U;
unsigned reply = variant == 1U ? 46U : (variant == 2U ? 45U :
(variant == 3U || variant == 4U ? 9U : 47U));
size_t length;
command_timed_config(&config, type, 1000U);
command_open(&config);
length = command_qualified_bytes(bytes, type, 8U | flag,
variant < 4U ? (type == 45U || type == 58U ? 9U : 64U) : 0U, false);
if (variant != 3U && variant != 4U) { bytes[6] += 2U; }
if (variant == 1U || variant == 2U) { bytes[4] = 2U; }
if (variant == 2U) { bytes[2] = (uint8_t)(5U | flag); }
if (variant >= 4U) { bytes[1] |= 0x80U; }
/* 无时间基准不会掩盖更早的寻址/格式错误,也不会触发实际动作。 */
command_raw_send(bytes, length); command_flush(); command_empty();
command_raw_reply_at(0U, bytes, length, reply | 0x40U | flag);
CHECK(command_frame_count() == 1U && command_status.rejected == 1U);
CHECK(command_status.time_rejected == 0U && !command_status.busy && command_action_calls == 0U);
command_close();
}
}
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:未知地址撤销是独立错误回复:不能终结真实SELECT_WAIT、SELECTED、EXECUTE_WAIT
* 或测试SELECTED,也不能改写原应用事件/续期。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:随后原事务仍可执行或正常撤销。
* 同时发送真实和TEST撤销,确认错误回复镜像T而不依赖旧事务的T位。
*/
static void test_command_deactivation_address_isolation(void)
{
unsigned phase, test;
for (phase = 0U; phase < 4U; ++phase) {
IEC104_CommandConfig_t config;
IEC104_CommandEvent_t event;
IEC104_CommandTransactionInfo_t before, after;
unsigned replies = 0U;
command_config_default(&config);
config.decision_timeout_ms = 100U; config.select_timeout_ms = 100U;
if (phase == 2U) { command_config_mode(&config, IEC104_COMMAND_MODE_DIRECT); }
command_open(&config);
command_send(45U, phase == 3U ? 0x86U : 6U, 0x51U, 1U, 6001U, phase == 2U ? 1U : 0x81U);
if (phase != 3U) {
event = command_take(phase == 2U ? IEC104_COMMAND_EXECUTE : IEC104_COMMAND_SELECT);
if (phase == 1U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
}
}
command_flush(); command_empty();
if (phase != 3U) { CHECK(IEC104_Command_Get_Transaction(command, &event, &before)); }
replies = phase == 1U || phase == 3U ? 1U : 0U;
CHECK(command_status.timer_active && command_status.timeout_remaining_ms == 100U);
fake.now += 5U;
for (test = 0U; test <= 1U; ++test) {
unsigned flag = test ? 0x80U : 0U;
command_send(45U, 8U | flag, 0x51U, 1U, 6003U, 1U); command_flush(); command_empty();
command_reply_at(replies++, 45U, 0x6fU | flag, 0x51U, 1U, 6003U, 1U);
command_send(45U, 8U | flag, 0x51U, 1U, 6002U, 1U); command_flush(); command_empty();
command_reply_at(replies++, 45U, 0x49U | flag, 0x51U, 1U, 6002U, 1U);
CHECK(command_status.busy && command_status.accepted == 1U && command_action_calls == 0U);
CHECK(command_status.timer_active && command_status.timeout_remaining_ms == 95U);
CHECK(command_status.deactivated == 0U && command_status.pending_events == 0U);
if (phase != 3U) {
CHECK(IEC104_Command_Get_Transaction(command, &event, &after));
CHECK(after.active && after.phase == before.phase && after.confirm_submitted == before.confirm_submitted);
}
}
CHECK(command_frame_count() == replies && command_status.rejected == 4U);
if (phase == 3U) {
command_send(45U, 0x88U, 0x51U, 1U, 6001U, 1U); command_flush(); command_empty();
command_reply_at(replies++, 45U, 0x89U, 0x51U, 1U, 6001U, 1U);
CHECK(!command_status.busy && command_action_calls == 0U);
} else {
if (phase == 0U) {
CHECK(IEC104_Command_Accept(command, &event, NULL, NULL) == IEC104_COMMAND_REPLY_OK);
command_flush();
}
if (phase != 2U) {
command_send(45U, 6U, 0x51U, 1U, 6001U, 1U);
event = command_take(IEC104_COMMAND_EXECUTE);
}
CHECK(IEC104_Command_Accept(command, &event, command_action, &command_action_calls) == IEC104_COMMAND_REPLY_OK);
(void)command_done(&event, IEC104_COMMAND_RESULT_SUCCEEDED);
CHECK(command_action_calls == 1U && command_status.executed == 1U);
command_empty();
}
command_close();
}
++command_checks;
}
/**
* 功能:验证以下场景与约束:未知IOA的47沿用短回复FIFO和满槽丢新规则。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:HIGH完全阻塞时,原测试选择确认
* 必须先保留,后来错误回复不能改写它;解堵后按生成顺序发送,原选择仍可撤销。
* 该用例在1/4个短回复槽下运行,覆盖WAIT、实际容量边界及每帧原T/OA/IOA。
*/
static void test_command_deactivation_address_backpressure(void)
{
unsigned index;
command_open(NULL);
command_send(44U, 6U, 0x51U, 1U, 6001U, 1U);
CHECK(command_last_handled == IEC104_COMMAND_EVENT_IGNORED);
command_leave_high_room(0U);
command_send(45U, 0x86U, 0x51U, 1U, 6001U, 0x81U);
command_process(); command_empty();
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY + 1U; ++index) {
command_send(45U, 8U | ((index & 1U) ? 0x80U : 0U), 0x60U + index, 1U, 6003U + index, 1U);
command_process(); command_empty();
}
CHECK(command_frame_count() == 0U && command_status.busy && command_status.accepted == 1U);
CHECK(command_status.pending_replies == IEC104_COMMAND_REPLY_CAPACITY + 1U);
CHECK(command_status.overloads == 1U && command_status.dropped_requests == 1U && command_action_calls == 0U);
CHECK(command_status.rejected == IEC104_COMMAND_REPLY_CAPACITY + 1U && !command_status.need_process);
fake.send_blocked = 0U; command_flush(); command_empty();
command_reply_at(0U, 45U, 0x87U, 0x51U, 1U, 6001U, 0x81U);
for (index = 0U; index < IEC104_COMMAND_REPLY_CAPACITY; ++index) {
command_reply_at(index + 1U, 45U, 0x6fU | ((index & 1U) ? 0x80U : 0U),
0x60U + index, 1U, 6003U + index, 1U);
}
CHECK(command_frame_count() == IEC104_COMMAND_REPLY_CAPACITY + 1U && command_status.busy);
CHECK(command_status.pending_replies == 0U && command_status.pending_events == 0U);
command_send(45U, 0x88U, 0x51U, 1U, 6001U, 1U); command_flush(); command_empty();
command_reply_at(IEC104_COMMAND_REPLY_CAPACITY + 1U, 45U, 0x89U, 0x51U, 1U, 6001U, 1U);
CHECK(!command_status.busy && command_status.deactivated == 1U && command_action_calls == 0U);
command_close();
++command_checks;
}
/* ============================================================================
* [ENTRY/MAIN] 测试入口:基础、扩展、时标、点表及收尾分组
* ============================================================================ */
/**
* 功能:执行契约测试。
* 输入参数:无。
* 输出参数:无。
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
* 说明:各组内部的类型、动作结果、期限及策略组合分别断言。
* 尺寸输出用于记录当前宿主ABI的公开结构布局,不当作MCU内存预算。
*/
int main(void)
{
/* [RUN/BASE] 基础配置、完整事务及动作结果 */
test_command_configuration();
test_command_wire_validation();
test_command_complete_flows();
test_command_result_policies();
test_command_action_outcomes();
test_command_rejection_and_stale();
test_command_application_reject_causes();
test_command_matching_and_busy();
/* [RUN/DUPLICATE] 决定期间重复请求 */
test_command_pending_same_point();
test_command_pending_other_originator();
test_command_pending_other_originator_overload();
test_command_pending_repeat_deadline();
test_command_pending_repeat_boundaries();
test_command_pending_repeat_lifetime();
test_command_duplicate_select_reset();
test_command_duplicate_select_fields();
test_command_duplicate_select_backpressure();
test_command_duplicate_select_isolation();
/* [RUN/LIFETIME] 终结、会话、引用与FIFO */
test_command_finished_pairing();
test_command_unsubmitted_selection();
test_command_deactivation_wait();
test_command_deadlines();
test_command_result_before_confirm();
test_command_fifo_and_overload();
test_command_return_validation();
test_command_stop_notifications();
test_command_disconnect_and_reuse();
test_command_station_lifetime();
test_command_reentry();
test_command_window_and_cancel();
test_command_two_services();
test_command_normal_overwrite();
test_command_stop_during_submit();
test_command_independent_reply_lifetime();
/* [RUN/EXTENDED] 47–51扩展命令 */
test_command_extended_configuration();
test_command_extended_flows();
test_command_extended_matching();
test_command_extended_invalid_values();
test_command_extended_wire_validation();
test_command_extended_reply_lengths();
/* [RUN/QUALIFIERS] QU/QL边界和既有事务隔离 */
test_command_qualifier_boundaries();
test_command_qualifier_preserves_transaction();
/* [RUN/TIMED] 58–64时标命令与时间基准 */
test_command_timed_flows();
test_command_time_conversion();
test_command_timed_auxiliary_fields();
test_command_timed_invalid_values();
test_command_timed_error_precedence();
test_command_deactivation_time_rules();
test_command_time_boundaries();
test_command_default_received_expiry();
test_command_time_base_errors();
test_command_time_base_api();
test_command_time_base_lifecycle();
test_command_time_configuration();
test_command_time_ordering();
test_command_time_base_reentry();
test_command_timed_reply_lengths();
test_command_timed_wire_validation();
/* [RUN/POINTS] 类型掩码及逐点配置 */
test_command_default_all_types();
test_command_point_default_empty();
test_command_point_configuration();
test_command_point_modes();
test_command_point_type_intersection();
test_command_point_ordering();
test_command_point_lookup_boundaries();
test_command_point_lifetime();
/* [RUN/RETURNS] 返回信息与应答尾部 */
test_command_response_tail();
test_command_continuous_events();
test_command_return_lifetime();
/* [RUN/ADMISSION] 动作前容量与旧回复门禁 */
test_command_accept_capacity();
test_command_accept_old_reply_gate();
#ifdef IEC104_COMMAND_TEST_HOOKS
test_command_capacity_query_faults();
#endif
/* [RUN/COMPLETION] 完成、交接及ENDED清理期限 */
test_command_complete_timeout();
test_command_handover_timeout();
test_command_completion_precedence();
test_command_ended_cleanup();
/* [RUN/ACTION_TIME] 动作前时效与凭证故障 */
test_command_action_time_expiry();
test_command_default_time_window();
test_command_action_time_rejections();
#ifdef IEC104_COMMAND_TEST_HOOKS
test_command_response_ticket_faults();
#endif
/* [RUN/ABNORMAL] 固定失败/超时TERM及其FIFO、会话和清理边界 */
test_command_action_failure_boundary();
test_command_negative_term_wait();
test_command_timeout_term_entries();
test_command_timeout_term_lifetime();
test_command_timeout_term_boundaries();
test_command_timeout_term_pending();
/* [RUN/TEST_BIT] 无动作模拟确认及T/OA、真实事务隔离 */
test_command_test_bit_flows();
test_command_test_bit_preserves_real();
test_command_test_bit_identity();
test_command_test_bit_validation();
test_command_test_bit_backpressure();
test_command_test_bit_lifetime();
/* [RUN/ADDRESS] 未知撤销地址与业务拒绝的原因码、身份及背压边界 */
test_command_deactivation_addresses();
test_command_deactivation_address_precedence();
test_command_deactivation_address_isolation();
test_command_deactivation_address_backpressure();
printf("Command tests passed: groups=%u tcp=%u replies=%u request=%zu event=%zu status=%zu\n",
command_checks, (unsigned)IEC104_TCP_SERVER_CAPACITY, (unsigned)IEC104_COMMAND_REPLY_CAPACITY,
sizeof(IEC104_CommandRequest_t), sizeof(IEC104_CommandEvent_t), sizeof(IEC104_CommandStatus_t));
return 0;
}