Files
STM32F4-Base/IEC60870-Slave/test/iec104_strict_priority_transport_test.c
T
wandering1 90cc9a7d99 feat(iec104): 把 104 骨架完整迁到 MCU 板端并在台架跑通总召
把 IEC104 从模拟器端整体搬到板端:协议栈原先由独立 iec104Task 驱动,
现在改为 netTask 单 owner 的 threadless 驱动,退役独立任务。

驱动与后端:
- 新增 iec104_net_mcu.c/.h,实现 IEC104_Net.h 的 11 个 ABI 函数,
  用槽表加单调句柄映射 net_socket 的单 fd 模型;CLIENT 拆除只走
  net_drop_conn_locked,绝不 net_close 监听 fd。
- net_socket 增 5 个 owner-only 接口(net_accept_locked、
  net_drop_conn_locked、net_pending_bytes、net_send_try、
  net_mono_now_ms)。
- iec104_serv.c 重写为 iec104_serv_net_tick(),在 netTask 1ms 主循环
  里驱动 core;无 osDelay/阻塞调用,喂狗是第一句。
- Src/freertos.c 删除 iec104Task 三件套,对应栈水位探针表项同步删除。

内存:目标板 192KB SRAM 装不下 vendor 默认容量档,按实测裁剪到
skeleton 档(TCP 缓冲、业务池、SOE 256->128、值槽密集化),
RW_IRAM1 118,408 B / 90.3%,留 2,178 B 余量。

台架修复(只在真硬件暴露):
- SNTP s_ctx.fd 零初始化后为 0,而 net fd 0 是合法槽位下标,
  首次代次变化时误 net_close(0) 关掉 ftpTask 的监听槽,导致 104
  连接被 FTP 的 220 banner 抢走;改为显式 .fd = -1。
- Accept 幂等:单 fd 模型下 vendor 每轮重试 Accept 会拿到同一 fd,
  后端改为会话在役时返 WOULD_BLOCK,Close 不再清槽。

验证:
- Keil 全量重建 0 error 0 warning,uvprojx 零抖动。
- 模拟器真实 TCP 13/13、transport 10/10。
- netmcu 后端单测 13/13(新增 N12)。
- 台架 serial-boot-test required 全中;板端 2404 总召 129 帧收到
  59 遥信(53 有源、全 IV)+ 16 双点 + 30 遥测(PT1 三相非零、
  UN/频率/温度按未标定置 IV)、遥控选择执行全否定、校时 COT 7。
- 期间顺带修正 UI 漂移自检文案、.serialtest required 陈旧项、
  coding-standard 对 vendor 与外部 ABI 名字的排除口径。
2026-11-18 10:29:18 +08:00

601 lines
29 KiB
C

/* Strict business scheduling over real TCP/Connection/Manager with controlled Net I/O. */
#define IEC104_TCP_HIGH_CAPACITY 320U
#define IEC104_TCP_NORMAL_CAPACITY 512U
#include "iec104_test_config.h"
#include "iec104_tcp_fixture.h"
#define SOE_CAP IEC104_ASDU_SOE_MAX_CAPACITY
#define SOE_OVER (IEC104_ASDU_SOE_MAX_CAPACITY + 1U)
static unsigned strict_groups;
/**
* 功能:建立启用严格业务优先级的伪网络连接并完成STARTDT,清空建立阶段通知。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void strict_begin(void)
{
IEC104_TcpServerConfig_t config;
memset(&fake, 0, sizeof(fake)); fake.now = 1000U; fake.accept_pending = 1U;
sut = IEC104_TcpServer_Create(); CHECK(sut != NULL);
test_transport_config(&config);
config.strict_business_priority = 1U;
config.protocol.link.k = IEC104_TCP_RECORD_CAPACITY;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
poll_once(); CHECK(drain() == 1U);
push(start_act, sizeof(start_act)); poll_once(); CHECK(drain() == 1U);
CHECK(sut->queue.strict_business_priority == 1U);
}
/**
* 功能:在测试ASDU模板中写入可识别标签,保持指定总长度以覆盖不同帧容量。
* 输入参数:bytes——报文字节缓冲区;length——字节长度;tag——区分帧顺序的测试标签。
* 输出参数:bytes——写入或读出的数据字节。
* 返回值:无。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
static void payload(uint8_t *bytes, uint16_t length, uint8_t tag)
{
memset(bytes, tag, length); memcpy(bytes, asdu, sizeof(asdu)); bytes[6] = tag;
}
/**
* 功能:按指定业务等级提交带标签的HIGH应答,断言接收成功并返回有效跟踪票据。
* 输入参数:rank——业务优先级;tag——区分帧顺序的测试标签;length——字节长度。
* 输出参数:无。
* 返回值:本次成功提交的 HIGH 响应跟踪票据。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static uint64_t response(unsigned rank, uint8_t tag, uint16_t length)
{
uint8_t bytes[249]; uint64_t ticket = UINT64_MAX;
payload(bytes, length, tag);
CHECK(IEC104_TcpServer_Send_Response_Tracked_Priority(sut, sut->generation, bytes, length,
&ticket, (IEC104_BusinessPriority_t)rank) == IEC104_TCP_SUBMIT_OK);
CHECK(ticket != UINT64_MAX && ticket != 0U);
return ticket;
}
/**
* 功能:按指定业务等级排入带标签的NORMAL事件,断言入队成功供后续排序与ACK测试使用。
* 输入参数:rank——业务优先级;tag——区分帧顺序的测试标签;length——字节长度。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void event(unsigned rank, uint8_t tag, uint16_t length)
{
uint8_t bytes[249]; payload(bytes, length, tag);
CHECK(IEC104_TcpServer_Enqueue_Event_Priority(sut, bytes, length,
(IEC104_BusinessPriority_t)rank) == IEC104_TCP_SUBMIT_OK);
}
/**
* 功能:遍历伪网络输出中的完整APDU,核对I帧序号连续并收集标签。
* 输入参数:tags——发送标签数组;capacity——调用方提供的缓冲容量。
* 输出参数:tags——发送标签数组。
* 返回值:本次收集或统计到的有效业务帧数量。
* 说明:控制帧不计入结果。
*/
static size_t output_tags(uint8_t *tags, size_t capacity)
{
size_t offset = 0U, count = 0U;
while (offset < fake.sent_bytes) {
size_t length;
CHECK(fake.sent_bytes - offset >= 2U);
length = (size_t)fake.output[offset + 1U] + 2U;
CHECK(length >= IEC104_APCI_LEN && length <= fake.sent_bytes - offset);
if ((fake.output[offset + 2U] & 1U) == 0U) {
unsigned ns = (unsigned)(fake.output[offset + 2U] >> 1) | ((unsigned)fake.output[offset + 3U] << 7);
CHECK(count < capacity && length >= IEC104_APCI_LEN + sizeof(asdu));
CHECK(ns == count);
tags[count++] = fake.output[offset + IEC104_APCI_LEN + 6U];
}
offset += length;
}
return count;
}
/**
* 功能:执行固定轮数的轮询及事件消费,使发送推进保持有界而不依赖真实时间等待。
* 输入参数:rounds——推进轮数。
* 输出参数:无。
* 返回值:无。
* 说明:接口保留参数仍按回调签名传入,未使用的参数不参与本辅助函数的处理。
*/
static void emit(unsigned rounds)
{
unsigned i;
for (i = 0U; i < rounds; ++i) { poll_once(); (void)drain(); }
}
/**
* 功能:混合HIGH应答和NORMAL事件验证12级实际发送顺序,并确认ACK后记录和正文均释放。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void all_ranks_mixed_lifetimes(void)
{
uint64_t tickets[13] = {0U};
uint8_t tags[32]; unsigned rank;
IEC104_TcpServerStatus_t status;
strict_begin();
for (rank = 12U; rank != 0U; --rank) {
if (rank == 4U || rank == 6U || rank == 8U) { event(rank, (uint8_t)rank, 10U); }
else { tickets[rank] = response(rank, (uint8_t)rank, 10U); }
}
CHECK(fake.sent_bytes == sizeof(start_act));
emit(12U);
CHECK(output_tags(tags, sizeof(tags)) == 12U);
for (rank = 1U; rank <= 12U; ++rank) {
CHECK(tags[rank - 1U] == rank);
if (tickets[rank] != 0U) { CHECK(IEC104_TcpServer_Get_Response_State(sut, tickets[rank]) == IEC104_RESPONSE_HANDED); }
}
push_ack(12U); emit(4U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.normal_queue.stored_asdu_count == 0U && status.normal_queue.used_bytes == 0U);
CHECK(sut->connection.send_manager.record_count == 0U);
end(); ++strict_groups;
}
/**
* 功能:同级HIGH/NORMAL交替入队仍按共同准入顺序发送,不能按生命周期队列重新分组。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void same_rank_cross_lifetime_fifo(void)
{
uint8_t tags[8];
strict_begin();
event(6U, 1U, 10U);
(void)response(6U, 2U, 10U);
event(6U, 3U, 10U);
(void)response(6U, 4U, 10U);
emit(4U);
CHECK(output_tags(tags, sizeof(tags)) == 4U);
CHECK(tags[0] == 1U && tags[1] == 2U && tags[2] == 3U && tags[3] == 4U);
push_ack(4U); emit(2U);
CHECK(sut->queue.priority_queues[IEC104_ASDU_PRIORITY_NORMAL].entry_count == 0U);
end(); ++strict_groups;
}
/**
* 功能:低优先级帧已部分写出时不得被新高优先级帧抢占,帧身份和完整字节顺序保持不变。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void partial_frame_is_not_preempted(void)
{
uint8_t tags[8]; uint64_t low, high;
unsigned budget = 1U;
IEC104_SendBufferHeadInfo_t current, after;
strict_begin();
low = response(12U, 12U, 10U);
fake.send_limit = 3U;
CHECK(Flush_Output(sut, &budget, NULL));
CHECK(budget == 0U && fake.sent_bytes == sizeof(start_act) + 3U);
CHECK(IEC104_SendBuffer_Peek_Send_Head(&sut->connection.send_buffer, &current) == IEC104_SEND_BUFFER_OK);
high = response(3U, 3U, 10U);
CHECK(IEC104_SendBuffer_Peek_Send_Head(&sut->connection.send_buffer, &after) == IEC104_SEND_BUFFER_OK);
CHECK(after.frame_id.frame_serial == current.frame_id.frame_serial && after.frame_id.connection_generation == current.frame_id.connection_generation);
CHECK(IEC104_TcpServer_Get_Response_State(sut, low) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, high) == IEC104_RESPONSE_PENDING);
fake.send_limit = 0U; emit(3U);
CHECK(output_tags(tags, sizeof(tags)) == 2U && tags[0] == 12U && tags[1] == 3U);
push_ack(2U); emit(2U);
end(); ++strict_groups;
}
/**
* 功能:正文覆盖、启动门和异序优先级同时存在时,按发送身份ACK而非入队顺序清理保留记录。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void retained_ack_and_snapshot(void)
{
uint8_t tags[8];
IEC104_TcpServerStatus_t status;
IEC104_AsduQueueStatus_t queue_status;
strict_begin();
event(8U, 8U, 180U); event(4U, 4U, 180U);
emit(2U);
CHECK(output_tags(tags, sizeof(tags)) == 2U && tags[0] == 4U && tags[1] == 8U);
CHECK(IEC104_AsduQueue_Set_Startup_Pending(&sut->queue, sut->generation, 1U) == IEC104_ASDU_QUEUE_OK);
event(6U, 6U, 249U);
CHECK(IEC104_AsduQueue_Get_Status(&sut->queue, &queue_status) == IEC104_ASDU_QUEUE_OK);
CHECK(queue_status.overwritten_unacked_count == 1U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.normal_queue.stored_asdu_count == 2U && status.normal_queue.pending_asdu_count == 1U);
/* N(R)=1 acknowledges rank4, whose insertion order was later than the overwritten rank8. */
push_ack(1U); emit(2U);
CHECK(IEC104_AsduQueue_Get_Status(&sut->queue, &queue_status) == IEC104_ASDU_QUEUE_OK);
CHECK(queue_status.overwritten_unacked_count == 1U && queue_status.entry_count[IEC104_ASDU_PRIORITY_NORMAL] == 1U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.normal_queue.used_bytes == 249U + 2U * IEC104_ASDU_NORMAL_ENTRY_HEADER_LEN);
push_ack(2U); emit(2U);
CHECK(IEC104_AsduQueue_Get_Status(&sut->queue, &queue_status) == IEC104_ASDU_QUEUE_OK && queue_status.overwritten_unacked_count == 0U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
emit(2U);
CHECK(output_tags(tags, sizeof(tags)) == 3U && tags[2] == 6U);
push_ack(3U); emit(2U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK && status.normal_queue.used_bytes == 0U);
end(); ++strict_groups;
}
/**
* 功能:HIGH拥塞时高优先级应答仍可直接提交,ACK观察保持有效并记录收到确认的时间。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void full_high_direct_ack_watch(void)
{
uint8_t bytes[10], tags[8]; uint64_t ticket, stamp;
unsigned i;
strict_begin();
(void)response(12U, 12U, 249U);
for (i = 0U; i < 2U; ++i) {
uint8_t small[6] = {1U, 0U, 3U, 0U, 1U, 0U};
CHECK(IEC104_TcpServer_Send_Response_Priority(sut, sut->generation, small, sizeof(small),
IEC104_BUSINESS_PRIORITY_COUNTER_INTERROGATION) == IEC104_TCP_SUBMIT_OK);
}
CHECK(IEC104_TcpServer_Begin_Business_Cycle(sut) == IEC104_TCP_SUBMIT_OK);
CHECK(IEC104_TcpServer_Set_Business_Priority(sut, IEC104_BUSINESS_PRIORITY_COMMAND) == IEC104_TCP_SUBMIT_OK);
payload(bytes, sizeof(bytes), 3U); fake.send_blocked = 1U;
CHECK(IEC104_TcpServer_Send_Response_Acked_Priority(sut, sut->generation, bytes, sizeof(bytes),
&ticket, IEC104_BUSINESS_PRIORITY_COMMAND) == IEC104_TCP_SUBMIT_OK && ticket != 0U);
CHECK(IEC104_TcpServer_Get_Response_State(sut, ticket) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Ack_State(sut, sut->generation, ticket, &stamp) == IEC104_TCP_ACK_SENT);
CHECK(IEC104_TcpServer_End_Business_Cycle(sut) == IEC104_TCP_SUBMIT_OK);
fake.send_blocked = 0U; poll_once(); (void)drain();
CHECK(output_tags(tags, sizeof(tags)) == 1U && tags[0] == 3U);
fake.now = 1100U; push_ack(1U); poll_once(); (void)drain();
CHECK(IEC104_TcpServer_Get_Ack_State(sut, sut->generation, ticket, &stamp) == IEC104_TCP_ACK_ACKED && stamp == 1100U);
IEC104_TcpServer_Release_Ack(sut, sut->generation, ticket);
end(); ++strict_groups;
}
/**
* 功能:未消费业务通知暂缓低优先级发送且不忙轮询,通知处理后高优先级应答先发。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void notification_blocks_without_spin(void)
{
IEC104_TcpWaitInfo_t wait;
uint8_t tags[8];
strict_begin();
(void)response(12U, 12U, 10U);
push_i(0U, 0U, 16U); poll_once();
CHECK(sut->event_count != 0U && output_tags(tags, sizeof(tags)) == 0U);
/* A retained readable edge may require one bounded continuation to observe WOULD_BLOCK. */
CHECK(IEC104_TcpServer_Prepare(sut, &wait, NULL) == IEC104_TCP_OK);
poll_once();
CHECK(IEC104_TcpServer_Prepare(sut, &wait, NULL) == IEC104_TCP_OK && wait.need_process == 0U);
CHECK(output_tags(tags, sizeof(tags)) == 0U);
CHECK(drain() == 1U);
(void)response(3U, 3U, 10U);
emit(2U);
CHECK(output_tags(tags, sizeof(tags)) == 2U && tags[0] == 3U && tags[1] == 12U);
end(); ++strict_groups;
}
/**
* 功能:关闭再初始化后旧票据均失效,新票据严格递增,避免旧查询误指向新连接应答。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
*/
static void close_init_preserves_ticket_identity(void)
{
IEC104_TcpServerConfig_t config;
uint64_t first, pending, newer;
strict_begin();
first = response(3U, 3U, 10U);
pending = response(12U, 12U, 10U);
emit(1U);
CHECK(IEC104_TcpServer_Get_Response_State(sut, first) == IEC104_RESPONSE_HANDED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, pending) == IEC104_RESPONSE_PENDING);
config = sut->config;
CHECK(IEC104_TcpServer_Close(sut, NULL) == IEC104_TCP_OK); (void)drain();
CHECK(IEC104_TcpServer_Get_Response_State(sut, pending) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
fake.accept_pending = 1U; poll_once(); CHECK(drain() == 1U);
push(start_act, sizeof(start_act)); poll_once(); CHECK(drain() == 1U);
newer = response(3U, 33U, 10U);
CHECK(newer > first && newer > pending);
CHECK(IEC104_TcpServer_Get_Response_State(sut, first) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, pending) == IEC104_RESPONSE_CANCELLED);
CHECK(IEC104_TcpServer_Get_Response_State(sut, newer) == IEC104_RESPONSE_PENDING);
end(); ++strict_groups;
}
/**
* 功能:建立指定调度模式且启用启动门禁的SOE传输测试会话。
* 输入参数:strict——0使用兼容调度,1使用严格业务优先级。
* 输出参数:无。
* 返回值:无。
* 说明:连接完成STARTDT后仍保留NORMAL门禁,便于在首次发送前检查完整缓存。
*/
static void soe_begin(unsigned strict)
{
IEC104_TcpServerConfig_t config;
memset(&fake, 0, sizeof(fake)); fake.now = 1000U; fake.accept_pending = 1U;
sut = IEC104_TcpServer_Create(); CHECK(sut != NULL);
test_transport_config(&config);
config.strict_business_priority = (uint8_t)strict;
config.startup_event_gate = 1U;
config.protocol.link.k = IEC104_TCP_RECORD_CAPACITY;
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
poll_once(); CHECK(drain() == 1U);
push(start_act, sizeof(start_act)); poll_once(); CHECK(drain() == 1U);
}
/**
* 功能:独立构造单对象Type30/31原文,以IOA保存事件编号供逐字节回显检查。
* 输入参数:id——事件编号,须在16位非零范围;ca——公共地址。
* 输出参数:bytes——写入17字节完整ASDU。
* 返回值:无。
* 说明:采用固定合法CP56和状态1,奇偶编号交替覆盖单点、双点两种类型。
*/
static void soe_bytes(uint8_t bytes[17], unsigned id, unsigned ca)
{
static const uint8_t sample[17] = {30U, 1U, 3U, 0U, 1U, 0U, 1U, 0U, 0U, 1U,
0U, 0U, 0U, 0U, 1U, 1U, 26U};
memcpy(bytes, sample, sizeof(sample));
bytes[0] = (uint8_t)((id & 1U) != 0U ? 30U : 31U);
bytes[4] = (uint8_t)ca; bytes[5] = (uint8_t)(ca >> 8);
bytes[6] = (uint8_t)id; bytes[7] = (uint8_t)(id >> 8);
}
/**
* 功能:将一条固定时间的遥信存入SOE区并核对同步接手结果。
* 输入参数:id——事件编号;ca——公共地址;deferred——是否等待应用决定。
* 输出参数:ticket——可选票据输出,deferred时必须有效。
* 返回值:无。
* 说明:全部事件使用同一业务等级,使保留顺序与覆盖边界可独立核对。
*/
static void soe_enqueue(unsigned id, unsigned ca, bool deferred, IEC104_SoeTicket_t *ticket)
{
uint8_t bytes[17]; soe_bytes(bytes, id, ca);
CHECK(IEC104_TcpServer_Enqueue_Soe(sut, bytes, sizeof(bytes),
IEC104_BUSINESS_PRIORITY_SEQUENCE_EVENT, deferred, ticket) == IEC104_TCP_SUBMIT_OK);
}
/**
* 功能:解析实际发送的SOE帧并核对原文,返回其事件编号序列。
* 输入参数:capacity——ids可容纳的编号数。
* 输出参数:ids——按实际发送顺序写入SOE事件编号。
* 返回值:已完整发送的SOE帧数。
* 说明:跳过控制帧及其他业务,原文比较包含类型、CA、品质和完整CP56。
*/
static size_t output_soe_ids(unsigned *ids, size_t capacity)
{
size_t offset = 0U, count = 0U;
while (offset < fake.sent_bytes) {
const uint8_t *frame = fake.output + offset;
size_t length;
CHECK(fake.sent_bytes - offset >= 2U);
length = (size_t)frame[1] + 2U;
CHECK(length >= IEC104_APCI_LEN && length <= fake.sent_bytes - offset);
if ((frame[2] & 1U) == 0U &&
(frame[IEC104_APCI_LEN] == 30U || frame[IEC104_APCI_LEN] == 31U)) {
uint8_t expected[17];
const uint8_t *data = frame + IEC104_APCI_LEN;
unsigned id, ca;
CHECK(length == IEC104_APCI_LEN + sizeof(expected) && count < capacity);
id = (unsigned)data[6] | ((unsigned)data[7] << 8);
ca = (unsigned)data[4] | ((unsigned)data[5] << 8);
soe_bytes(expected, id, ca);
CHECK(memcmp(data, expected, sizeof(expected)) == 0);
ids[count++] = id;
}
offset += length;
}
return count;
}
/**
* 功能:逐轮发送并按实际线上I帧数提供累计ACK,直到两类NORMAL原文和记录均释放。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:有界推进2048轮,给严格模式每帧发送及ACK通知各留推进轮次;N(R)来自实际线上帧数。
*/
static void soe_flush_all(void)
{
uint8_t tags[512];
unsigned round, acknowledged = 0U;
for (round = 0U; round < 2048U; ++round) {
IEC104_TcpServerStatus_t status;
size_t sent;
emit(1U); sent = output_tags(tags, sizeof(tags));
if (sent > acknowledged) { acknowledged = (unsigned)sent; push_ack(acknowledged); }
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
if (status.normal_queue.stored_asdu_count == 0U &&
sut->connection.send_manager.record_count == 0U) { return; }
}
{
IEC104_TcpServerStatus_t status;
IEC104_AsduHeadInfo_t head = {0};
IEC104_AsduQueueResult_t peek;
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
fprintf(stderr, "SOE drain stalled: mode=%u sent=%u stored=%lu pending=%lu soe=%u records=%u\n",
(unsigned)sut->config.strict_business_priority, acknowledged,
(unsigned long)status.normal_queue.stored_asdu_count,
(unsigned long)status.normal_queue.pending_asdu_count,
(unsigned)status.soe_queue.stored_count,
(unsigned)sut->connection.send_manager.record_count);
peek = IEC104_AsduQueue_Peek_Unsubmitted(&sut->queue, IEC104_ASDU_PRIORITY_NORMAL, &head);
fprintf(stderr, "SOE drain gates: peek=%d rank=%u serial=%llu pending=%u blocked=%u used=%u startup=%u eof=%u\n",
(int)peek, (unsigned)head.business_priority, (unsigned long long)head.entry_id.entry_serial,
(unsigned)sut->connection.send_manager.pending_business_priority,
(unsigned)sut->connection.send_manager.new_i_frame_blocked,
(unsigned)sut->connection.send_manager.business_frame_used,
(unsigned)sut->queue.startup_pending, (unsigned)sut->queue.normal_send_limited);
fprintf(stderr, "SOE drain TCP: events=%u rx_event=%u cycle=%u network_used=%u process=%u rx_blocked=%u rx_end=%lu input=%lu/%lu\n",
(unsigned)sut->event_count, (unsigned)sut->rx_event_pending,
(unsigned)sut->business_cycle_active, (unsigned)sut->connection.send_manager.network_frame_used,
(unsigned)sut->process_pending, (unsigned)sut->rx_blocked, (unsigned long)sut->rx_end,
(unsigned long)fake.offset, (unsigned long)fake.length);
}
CHECK(0);
}
/**
* 功能:在兼容和严格模式验证256条完整保留、第257条覆盖最早事件及普通NORMAL挤占隔离。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:使用仅512字节的普通环持续制造覆盖,SOE仍按完整256条序列发送并由真实ACK释放。
*/
static void soe_capacity_isolation(void)
{
unsigned strict, id, ids[SOE_CAP];
for (strict = 0U; strict < 2U; ++strict) {
IEC104_TcpServerStatus_t status;
soe_begin(strict);
for (id = 1U; id <= SOE_CAP; ++id) { soe_enqueue(id, 1U, false, NULL); }
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.capacity == SOE_CAP && status.soe_queue.stored_count == SOE_CAP);
CHECK(status.soe_queue.dropped_pending_count == 0U);
for (id = 1U; id <= 64U; ++id) { event(8U, (uint8_t)id, 120U); }
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == SOE_CAP && status.soe_queue.dropped_pending_count == 0U);
soe_enqueue(SOE_OVER, 1U, false, NULL);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == SOE_CAP && status.soe_queue.dropped_pending_count == 1U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
soe_flush_all();
CHECK(output_soe_ids(ids, SOE_CAP) == SOE_CAP);
for (id = 0U; id < SOE_CAP; ++id) { CHECK(ids[id] == id + 2U); }
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == 0U && status.soe_queue.retired_count == 0U);
end();
}
++strict_groups;
}
/**
* 功能:验证已发送SOE被覆盖后的旧ACK只释放退休身份,随后部分ACK准确释放对应新事件。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:两种调度模式均核对全部257条实际线上原文,防止槽复用后旧ACK误删新数据。
*/
static void soe_overwritten_ack_identity(void)
{
unsigned strict, id, ids[SOE_OVER];
for (strict = 0U; strict < 2U; ++strict) {
IEC104_TcpServerStatus_t status;
soe_begin(strict); soe_enqueue(1U, 1U, false, NULL);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
emit(1U); CHECK(output_soe_ids(ids, SOE_OVER) == 1U && ids[0] == 1U);
CHECK(IEC104_AsduQueue_Set_Startup_Pending(&sut->queue, sut->generation, 1U) == IEC104_ASDU_QUEUE_OK);
for (id = 2U; id <= SOE_OVER; ++id) { soe_enqueue(id, 1U, false, NULL); }
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == SOE_CAP && status.soe_queue.retired_count == 1U);
CHECK(status.soe_queue.overwritten_unacked_count == 1U);
push_ack(1U); emit(2U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == SOE_CAP && status.soe_queue.retired_count == 0U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
emit(1U); CHECK(output_soe_ids(ids, SOE_OVER) >= 2U && ids[1] == 2U);
CHECK(IEC104_AsduQueue_Set_Startup_Pending(&sut->queue, sut->generation, 1U) == IEC104_ASDU_QUEUE_OK);
push_ack(2U); emit(2U);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == (SOE_CAP - 1U) && status.soe_queue.retired_count == 0U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
soe_flush_all(); CHECK(output_soe_ids(ids, SOE_OVER) == SOE_OVER);
for (id = 0U; id < SOE_OVER; ++id) { CHECK(ids[id] == id + 1U); }
end();
}
++strict_groups;
}
/**
* 功能:验证SOE占位跨重连保留、启动门禁、QRP2底层筛选覆盖两区以及服务重建票据隔离。
* 输入参数:无。
* 输出参数:无。
* 返回值:无。
* 说明:直接调用QRP2所用Count/Discard接口,真实SystemCommand和GI业务交接由示例专项验证。
*/
static void soe_reconnect_deferred_and_clear(void)
{
unsigned strict, ids[8];
for (strict = 0U; strict < 2U; ++strict) {
IEC104_SoeTicket_t first, cleared, current;
IEC104_TcpServerConfig_t config;
IEC104_TcpServerStatus_t status;
uint8_t bytes[17], types[32] = {0U};
uint32_t count = 0U;
soe_begin(strict); soe_enqueue(10U, 1U, true, &first);
soe_enqueue(11U, 1U, false, NULL);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
emit(3U); CHECK(output_soe_ids(ids, 8U) == 0U);
soe_bytes(bytes, 13U, 1U);
CHECK(IEC104_TcpServer_Enqueue_Event_Priority(sut, bytes, sizeof(bytes),
IEC104_BUSINESS_PRIORITY_SEQUENCE_EVENT) == IEC104_TCP_SUBMIT_OK);
soe_enqueue(12U, 2U, false, NULL);
CHECK(IEC104_TcpServer_Close_Connection(sut, sut->generation, NULL) == IEC104_TCP_OK);
(void)drain(); fake.sent_bytes = 0U; fake.accept_pending = 1U;
poll_once(); CHECK(drain() == 1U);
push(start_act, sizeof(start_act)); poll_once(); CHECK(drain() == 1U);
CHECK(IEC104_TcpServer_Resolve_Soe(sut, &first, true) == IEC104_TCP_OK);
emit(3U); CHECK(output_soe_ids(ids, 8U) == 0U);
soe_enqueue(14U, 1U, true, &cleared);
types[30U / 8U] |= (uint8_t)(1U << (30U % 8U));
types[31U / 8U] |= (uint8_t)(1U << (31U % 8U));
CHECK(IEC104_TcpServer_Count_Pending_Normal(sut, 1U, types, &count, NULL) == IEC104_TCP_OK && count == 4U);
CHECK(IEC104_TcpServer_Discard_Pending_Normal(sut, 1U, types, &count, NULL) == IEC104_TCP_OK && count == 4U);
CHECK(IEC104_TcpServer_Resolve_Soe(sut, &cleared, true) == IEC104_TCP_OK);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.normal_queue.stored_asdu_count == 1U && status.soe_queue.stored_count == 1U);
CHECK(status.soe_queue.deferred_count == 0U);
CHECK(IEC104_TcpServer_Release_Startup(sut, sut->generation, sut->activation_id) == IEC104_TCP_SUBMIT_OK);
soe_flush_all(); CHECK(output_soe_ids(ids, 8U) == 1U && ids[0] == 12U);
config = sut->config;
CHECK(IEC104_TcpServer_Close(sut, NULL) == IEC104_TCP_OK); (void)drain();
CHECK(IEC104_TcpServer_Init(sut, &config, NULL) == IEC104_TCP_OK);
soe_enqueue(20U, 1U, true, &current);
CHECK(IEC104_TcpServer_Resolve_Soe(sut, &cleared, true) != IEC104_TCP_OK);
CHECK(IEC104_TcpServer_Get_Status(sut, &status, NULL) == IEC104_TCP_OK);
CHECK(status.soe_queue.stored_count == 1U && status.soe_queue.deferred_count == 1U);
CHECK(IEC104_TcpServer_Resolve_Soe(sut, &current, false) == IEC104_TCP_OK);
end();
}
++strict_groups;
}
/**
* 功能:执行真实传输层与伪网络组合下的优先级及SOE保留场景,打印全部通过的组数。
* 输入参数:无。
* 输出参数:无。
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
*/
int main(void)
{
all_ranks_mixed_lifetimes(); same_rank_cross_lifetime_fifo(); partial_frame_is_not_preempted();
retained_ack_and_snapshot(); full_high_direct_ack_watch();
notification_blocks_without_spin(); close_init_preserves_ticket_identity();
soe_capacity_isolation(); soe_overwritten_ack_identity(); soe_reconnect_deferred_and_clear();
printf("strict priority transport: %u groups passed\n", strict_groups);
return 0;
}