682 lines
32 KiB
C
682 lines
32 KiB
C
/* Type42业务专项:真实Slave/NORMAL/发送管理器配合可控Net。
|
||
* 本单元包含TcpServer和Spontaneous实现,链接时须排除它们的独立源文件。
|
||
* 独立手工字节及固定偏移断言是线上格式的主要依据,末尾另用Parse/Getter核对读取接口。
|
||
* 不启动真实主站或系统socket;除指定的入队/查询故障外,缓存、选帧、重连和ACK走真实协议代码。
|
||
* 文件内导航:
|
||
* [FAULT/HOOKS] 可控失败和调用观察,仅替换本单元包含的发布实现。
|
||
* [FAULT/FIXTURE] 服务、站点、冻结报告、原始I帧及发布辅助。
|
||
* [FAULT/LIFECYCLE] WAIT、校时、原数组复用、启动门禁和重连重发。
|
||
* [FAULT/INPUT] 业务约束、类型变体和统计单位。
|
||
* [FAULT/ERRORS] 服务查询/交接错误与统计失败的区别。
|
||
* [FAULT/QUEUE] 等级排序、NORMAL容量覆盖及精确249字节边界。
|
||
* [ENTRY] 每种编译容量均运行兼容与严格业务调度两种配置。 */
|
||
#include "iec104_test_config.h"
|
||
#include "iec104_tcp_fixture.h"
|
||
#include "IEC104_Spontaneous.h"
|
||
#include <math.h>
|
||
|
||
/* [FAULT/HOOKS] *_at为从1开始的调用序号,0表示不注入;每次fe_open清零。
|
||
* fe_last观察最近一次交接尝试,包含被注入WAIT/ERROR拒绝的原文,不代表成功入队或已写出。 */
|
||
static unsigned fe_enqueues, fe_fail_at, fe_queries, fe_query_fail_at, fe_query_invalid_at;
|
||
static unsigned fe_now_reads;
|
||
static IEC104_TcpSubmitResult_t fe_failure;
|
||
static IEC104_TcpResult_t fe_query_failure;
|
||
static IEC104_BusinessPriority_t fe_last_priority;
|
||
static uint8_t fe_last[IEC104_MAX_ASDU_LEN];
|
||
static uint16_t fe_last_length;
|
||
|
||
/**
|
||
* 功能:在指定的单次发布入队调用注入故障,其余调用保留真实队列行为。
|
||
* 输入参数:slave——被测 Slave 实例;value——ASDU 容器;priority——存储或业务优先级。
|
||
* 输出参数:无。
|
||
* 返回值:发送提交结果,区分接收成功、WAIT、ERROR 与 FATAL。
|
||
* 说明:注入只截住指定的一次调用;其他调用仍由真实Slave校验并复制进NORMAL。
|
||
* 先记录尝试原文,便于把WAIT前后两次编码与同一手工向量比较;队列事实另查fe_queue。
|
||
*/
|
||
static IEC104_TcpSubmitResult_t FaultTest_Enqueue(IEC104_Slave_t *slave,
|
||
IEC60870_Asdu_t *value, IEC104_BusinessPriority_t priority)
|
||
{
|
||
IEC104_TcpSubmitResult_t result;
|
||
++fe_enqueues;
|
||
fe_last_priority = priority;
|
||
CHECK(value->length <= sizeof(fe_last));
|
||
memcpy(fe_last, value->buffer, value->length);
|
||
fe_last_length = value->length;
|
||
if (fe_enqueues == fe_fail_at) { return fe_failure; }
|
||
result = IEC104_Slave_Enqueue_Event_Priority(slave, value, priority);
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:为故障事件发布模块注入状态查询错误,或返回真实传输状态。
|
||
* 输入参数:slave——被测 Slave 实例(只读);status——状态快照;error——底层错误码存储。
|
||
* 输出参数:status——状态快照;error——底层错误码存储。
|
||
* 返回值:真实传输接口或故障注入产生的 TCP 操作结果。
|
||
* 说明:查询失败不提供可用输出;另可在真实查询成功后标记队列快照无效。
|
||
* 通过序号区分发布前准入和发布后覆盖统计,避免把诊断失败误报成报告未发布。
|
||
*/
|
||
static IEC104_TcpResult_t FaultTest_GetStatus(const IEC104_Slave_t *slave,
|
||
IEC104_TcpServerStatus_t *status, IEC104_TcpError_t *error)
|
||
{
|
||
IEC104_TcpResult_t result;
|
||
++fe_queries;
|
||
if (fe_queries == fe_query_fail_at) { return fe_query_failure; }
|
||
result = IEC104_Slave_Get_Status(slave, status, error);
|
||
if (result == IEC104_TCP_OK && fe_queries == fe_query_invalid_at) {
|
||
status->asdu_queue_status_valid = 0U;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:只统计发布模块自身取时,传输层仍使用公共fake时钟。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:本次采样的时钟毫秒值。
|
||
* 说明:此计数不能解释为全协议栈未读时钟。
|
||
*/
|
||
static uint64_t FaultTest_Now(void) { ++fe_now_reads; return fake.now; }
|
||
|
||
/* 替换范围止于包含的Spontaneous实现,之后恢复名称;上面的钩子和后面的测试调用真实Slave。 */
|
||
#define IEC104_Slave_Get_Status FaultTest_GetStatus
|
||
#define IEC104_Slave_Enqueue_Event_Priority FaultTest_Enqueue
|
||
#define IEC104_Net_Now_Ms FaultTest_Now
|
||
#include "../104/business/IEC104_Spontaneous.c"
|
||
#undef IEC104_Slave_Get_Status
|
||
#undef IEC104_Slave_Enqueue_Event_Priority
|
||
#undef IEC104_Net_Now_Ms
|
||
|
||
/* [FAULT/FIXTURE] 一个真实服务和注册站;数组容量覆盖最大帧与超长输入,属于测试夹具。
|
||
* fe_activation只保存来源身份和启动轮次,不复制含自引用ASDU的事件对象。 */
|
||
static IEC104_Slave_t *fe_slave;
|
||
static IEC104_Spontaneous_t *fe_publisher;
|
||
static IEC104_Station_t fe_station;
|
||
static IEC104_SlaveEventIdentity_t fe_activation;
|
||
static uint64_t fe_activation_id;
|
||
static IEC60870_FaultEventSignal_t fe_signals[24];
|
||
static IEC60870_FaultEventMeasurement_t fe_measurements[40];
|
||
|
||
/* 1遥信、8浮点遥测:6字节公共头+2+11+2+8*7=77字节,没有外层IOA或遥测QDS。
|
||
* 偏移6/7为N/type;8..10为遥信地址0x010203,11为SIQ,12..18为10:20:30.123的CP56。
|
||
* 偏移19/20为M/type,21起每项7字节,第二项float为-2.0以检查符号和大小端。
|
||
* 该向量直接描述故障事件扩展格式,不由被测编码器生成;加APCI后83字节,APDU长度字段为81。 */
|
||
static const uint8_t fe_expected[] = {
|
||
42,1,3,0x56,0x34,0x12,
|
||
1,30, 3,2,1,1, 0xab,0x75,0x14,0x0a,0x0c,0x0b,0x1a,
|
||
8,13,
|
||
1,0x40,0, 0,0,0x80,0x3f,
|
||
2,0x40,0, 0,0,0,0xc0,
|
||
3,0x40,0, 0,0,0x40,0x40,
|
||
4,0x40,0, 0,0,0x80,0x40,
|
||
5,0x40,0, 0,0,0xa0,0x40,
|
||
6,0x40,0, 0,0,0xc0,0x40,
|
||
7,0x40,0, 0,0,0xe0,0x40,
|
||
8,0x40,0, 0,0,0,0x41
|
||
};
|
||
|
||
/**
|
||
* 功能:读空通知防止应用侧背压,保存最新ACTIVATED的合法身份供显式开放启动门禁。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:此辅助不模拟初始化/总召成功,也不会自行释放门禁或确认I帧。
|
||
*/
|
||
static void fe_drain(void)
|
||
{
|
||
IEC104_SlaveEvent_t event;
|
||
IEC104_TcpEventResult_t result;
|
||
while ((result = IEC104_Slave_NextEvent(fe_slave, &event)) == IEC104_TCP_EVENT_READY) {
|
||
if (event.transport.type == IEC104_TCP_EVENT_ACTIVATED) {
|
||
CHECK(IEC104_Slave_Get_Event_Identity(fe_slave, &event, &fe_activation));
|
||
fe_activation_id = event.transport.activation_id;
|
||
}
|
||
}
|
||
CHECK(result == IEC104_TCP_EVENT_EMPTY);
|
||
}
|
||
|
||
/**
|
||
* 功能:按严格优先级和初始化门禁选项建立故障事件发布测试夹具。
|
||
* 输入参数:strict——是否启用严格优先级;gate——初始化发送门禁选择。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:strict/gate是运行时选项;延长期限只为隔离本用例的快照与队列断言,避免推进时钟触发超时。
|
||
* 扫描实际服务池取得fixture所需的sut,不能把Slave句柄强转为TcpServer。
|
||
*/
|
||
static void fe_initialize(unsigned strict, unsigned gate)
|
||
{
|
||
IEC104_SlaveConfig_t config;
|
||
test_transport_config(&config);
|
||
config.strict_business_priority = (uint8_t)strict;
|
||
config.startup_event_gate = (uint8_t)gate;
|
||
config.protocol.link.k = IEC104_TCP_RECORD_CAPACITY;
|
||
config.protocol.link.t1_ms = 600000U;
|
||
config.protocol.link.t3_ms = 600000U;
|
||
config.protocol.local_send_timeout_ms = 600000U;
|
||
CHECK(IEC104_Slave_Init(fe_slave, &config, NULL) == IEC104_TCP_OK);
|
||
sut = NULL;
|
||
for (unsigned i = 0U; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||
if (server_pool[i].in_use) { sut = &server_pool[i].server; break; }
|
||
}
|
||
CHECK(sut != NULL);
|
||
}
|
||
|
||
/**
|
||
* 功能:initialized=false保留“句柄和站点存在、服务尚未监听”的状态供准入校验。
|
||
* 输入参数:strict——是否启用严格优先级;gate——初始化发送门禁选择;initialized——是否完成初始化交接。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:默认不建立主站连接。
|
||
* 调用前须完成上例fe_close,不能靠清零夹具掩盖站点引用或静态实例泄漏。
|
||
*/
|
||
static void fe_open(unsigned strict, unsigned gate, bool initialized)
|
||
{
|
||
IEC104_SpontaneousConfig_t config;
|
||
memset(&fake, 0, sizeof(fake));
|
||
memset(&fe_station, 0, sizeof(fe_station));
|
||
memset(&fe_activation, 0, sizeof(fe_activation));
|
||
fake.now = 1000U;
|
||
fe_activation_id = 0U;
|
||
fe_enqueues = fe_fail_at = fe_queries = fe_query_fail_at = fe_query_invalid_at = fe_now_reads = 0U;
|
||
fe_last_length = 0U;
|
||
fe_failure = IEC104_TCP_SUBMIT_WAIT;
|
||
fe_query_failure = IEC104_TCP_INTERNAL_ERROR;
|
||
fe_slave = IEC104_Slave_Create();
|
||
CHECK(fe_slave != NULL);
|
||
CHECK(IEC104_Slave_Add_Station(fe_slave, &fe_station, 0x1234U, NULL) == IEC104_STATION_OK);
|
||
test_spontaneous_config(&config);
|
||
config.originator_address = 0x56U;
|
||
fe_publisher = IEC104_Spontaneous_Create(fe_slave, &config);
|
||
CHECK(fe_publisher != NULL);
|
||
if (initialized) { fe_initialize(strict, gate); }
|
||
}
|
||
|
||
/**
|
||
* 功能:Close后先读空收尾通知,再销毁发布实例、移除站点及销毁Slave,验证各级生命周期能够回收。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
||
*/
|
||
static void fe_close(void)
|
||
{
|
||
CHECK(IEC104_Slave_Close(fe_slave, NULL) == IEC104_TCP_OK);
|
||
fe_drain();
|
||
CHECK(IEC104_Spontaneous_Destroy(fe_publisher));
|
||
CHECK(IEC104_Slave_Remove_Station(fe_slave, &fe_station) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Station_Is_Reusable(&fe_station));
|
||
CHECK(IEC104_Slave_Destroy(fe_slave, NULL) == IEC104_TCP_OK);
|
||
fe_slave = NULL;
|
||
fe_publisher = NULL;
|
||
sut = NULL;
|
||
}
|
||
|
||
/**
|
||
* 功能:每次重建固定故障日历和8项必需测量,使用不同IOA,额外数组项用于扩展及容量用例。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:已构造或取出的事件值副本。
|
||
* 说明:返回报告借用全局数组;再次调用本函数会替换其内容,测试仅在明确允许复用时如此操作。
|
||
* 点号0x4001起只是夹具约定,不是库自动分配的Ua..I0地址。
|
||
*/
|
||
static IEC104_SpontaneousFaultEvent_t fe_report(void)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
memset(&report, 0, sizeof(report));
|
||
memset(fe_signals, 0, sizeof(fe_signals));
|
||
memset(fe_measurements, 0, sizeof(fe_measurements));
|
||
report.common_address = 0x1234U;
|
||
report.station_id = IEC104_Station_Get_Id(&fe_station);
|
||
IEC60870_FaultEvent_Init(&report.event);
|
||
CHECK(report.event.signal_type == 30U && report.event.measurement_type == 13U);
|
||
report.event.signals = fe_signals;
|
||
report.event.signal_count = 1U;
|
||
report.event.measurements = fe_measurements;
|
||
report.event.measurement_count = 8U;
|
||
for (unsigned i = 0U; i < sizeof(fe_signals) / sizeof(fe_signals[0]); ++i) {
|
||
fe_signals[i].ioa = 0x010203U + i;
|
||
fe_signals[i].value = 1U;
|
||
fe_signals[i].timestamp.time.year = 2026U;
|
||
fe_signals[i].timestamp.time.month = 11U;
|
||
fe_signals[i].timestamp.time.day = 12U;
|
||
fe_signals[i].timestamp.time.hour = 10U;
|
||
fe_signals[i].timestamp.time.minute = 20U;
|
||
fe_signals[i].timestamp.time.second = 30U;
|
||
fe_signals[i].timestamp.time.ms = 123U;
|
||
}
|
||
for (unsigned i = 0U; i < sizeof(fe_measurements) / sizeof(fe_measurements[0]); ++i) {
|
||
fe_measurements[i].ioa = 0x4001U + i;
|
||
fe_measurements[i].value.short_float = (float)(i + 1U);
|
||
}
|
||
fe_measurements[1].value.short_float = -2.0f;
|
||
return report;
|
||
}
|
||
|
||
/**
|
||
* 功能:直接读真实队列状态,绕过发布实现内的查询钩子,不消耗故障注入序号。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:按值返回的当前状态或信息快照。
|
||
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
||
*/
|
||
static IEC104_TcpServerStatus_t fe_queue(void)
|
||
{
|
||
IEC104_TcpServerStatus_t status;
|
||
CHECK(IEC104_Slave_Get_Status(fe_slave, &status, NULL) == IEC104_TCP_OK);
|
||
return status;
|
||
}
|
||
|
||
/**
|
||
* 功能:单轮零等待推进并读空通知,不自动变更fake时钟、释放门禁或发送ACK。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
||
*/
|
||
static void fe_poll(void)
|
||
{
|
||
CHECK(IEC104_Slave_Poll(fe_slave, 0U, NULL) == IEC104_TCP_OK);
|
||
fe_drain();
|
||
}
|
||
|
||
/**
|
||
* 功能:建立TCP并完成STARTDT。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:数据传输激活不代表启动业务已完成,gate开启时NORMAL仍须等待。
|
||
*/
|
||
static void fe_connect(void)
|
||
{
|
||
fake.accept_pending = 1U;
|
||
fe_poll();
|
||
push(start_act, sizeof(start_act));
|
||
for (unsigned i = 0U; i < 8U; ++i) { fe_poll(); }
|
||
CHECK(sut->connection.link_state.dt_state == IEC104_DT_STARTED);
|
||
}
|
||
|
||
/**
|
||
* 功能:从fake Net真正接手的APDU中跳过S/U帧,借出第wanted个I帧的ASDU,不读取入队钩子的副本。
|
||
* 输入参数:wanted——目标 I 帧序号;length——字节长度。
|
||
* 输出参数:length——本次数据、文件或 ASDU 的实际长度。
|
||
* 返回值:借用模拟网络输出的目标 ASDU 起点;输出缓冲改变后不可继续使用。
|
||
* 说明:返回指针前有6字节APCI,故wire[-5]为APDU长度字段;原文所有权仍属fake.output。
|
||
*/
|
||
static const uint8_t *fe_wire(unsigned wanted, size_t *length)
|
||
{
|
||
unsigned found = 0U;
|
||
size_t offset = 0U;
|
||
while (offset < fake.sent_bytes) {
|
||
const uint8_t *frame = fake.output + offset;
|
||
size_t count;
|
||
CHECK(fake.sent_bytes - offset >= 6U && frame[0] == 0x68U);
|
||
count = (size_t)frame[1] + 2U;
|
||
CHECK(count >= 6U && count <= fake.sent_bytes - offset);
|
||
if ((frame[2] & 1U) == 0U && found++ == wanted) {
|
||
*length = count - 6U;
|
||
return frame + 6U;
|
||
}
|
||
offset += count;
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* 功能:执行完整故障事件发布并核对本次发布成功。
|
||
* 输入参数:report——故障事件报告(只读)。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:仅用于整份发布预期成功的用例;published=1按报告计数,等级6检查与当前调度模式无关。
|
||
*/
|
||
static void fe_publish(const IEC104_SpontaneousFaultEvent_t *report)
|
||
{
|
||
IEC104_SpontaneousPublishInfo_t info;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, report, &info) ==
|
||
IEC104_SPONTANEOUS_PUBLISH_OK);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_NONE && info.published == 1U && info.failed_index == SIZE_MAX);
|
||
CHECK(fe_last_priority == IEC104_BUSINESS_PRIORITY_FAULT_EVENT);
|
||
}
|
||
|
||
/**
|
||
* 功能:INPUT的原子性指“没有尝试入队、没有改变队列”,不是仅检查最终错误码。
|
||
* 输入参数:report——故障事件报告(只读)。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:单报告没有普通变化数组下标,failed_index应始终为SIZE_MAX。
|
||
*/
|
||
static void fe_expect_input(const IEC104_SpontaneousFaultEvent_t *report)
|
||
{
|
||
IEC104_SpontaneousPublishInfo_t info;
|
||
IEC104_TcpServerStatus_t before = fe_queue(), after;
|
||
unsigned enqueues = fe_enqueues;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, report, &info) ==
|
||
IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_INPUT && info.published == 0U && info.failed_index == SIZE_MAX);
|
||
after = fe_queue();
|
||
CHECK(fe_enqueues == enqueues);
|
||
CHECK(before.normal_queue.pending_asdu_count == after.normal_queue.pending_asdu_count);
|
||
CHECK(before.normal_queue.stored_asdu_count == after.normal_queue.stored_asdu_count);
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:[FAULT/LIFECYCLE] 同一已冻结快照跨WAIT、校时、调用方数据复用、断线及新启动轮次仍逐字节一致。
|
||
* 输入参数:strict——是否启用严格优先级。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:未ACK便断线用于检验NORMAL保留原文;新轮次须重新开放门禁,旧身份不得越过该限制。
|
||
*/
|
||
static void test_wire_retry_reconnect(unsigned strict)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
IEC104_SpontaneousPublishInfo_t info;
|
||
IEC104_SpontaneousStatus_t status;
|
||
IEC104_TcpServerStatus_t queue;
|
||
IEC104_SlaveEventIdentity_t previous_identity;
|
||
uint64_t previous_activation;
|
||
size_t length;
|
||
const uint8_t *wire;
|
||
fe_open(strict, 1U, true);
|
||
report = fe_report();
|
||
fe_fail_at = 1U;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_WAIT);
|
||
CHECK(info.published == 0U && info.error == IEC104_SPONTANEOUS_ERROR_NONE);
|
||
CHECK(fe_last_length == sizeof(fe_expected) && memcmp(fe_last, fe_expected, sizeof(fe_expected)) == 0);
|
||
CHECK(fe_queue().normal_queue.stored_asdu_count == 0U && fe_now_reads == 0U);
|
||
/* 故意改为与故障日期不同的时间基准并推进50秒;重试必须继续使用报告内的冻结CP56。 */
|
||
CHECK(IEC104_Spontaneous_Set_Time_Base(fe_publisher, UINT64_C(1704067200123), fake.now, true));
|
||
fake.now += 50000U;
|
||
fe_now_reads = 0U;
|
||
fe_publish(&report);
|
||
CHECK(fe_last_length == 77U && memcmp(fe_last, fe_expected, 77U) == 0 && fe_now_reads == 0U);
|
||
/* OK后立即复用原数组,随后真正写出的字节仍须为发布时的快照。 */
|
||
memset(fe_signals, 0xa5, sizeof(fe_signals));
|
||
memset(fe_measurements, 0xa5, sizeof(fe_measurements));
|
||
fe_connect();
|
||
CHECK(fe_queue().startup_pending && fe_wire(0U, &length) == NULL);
|
||
/* 这里直接模拟应用完成启动条件后开放;初始化/总召的业务状态机由其各自专项覆盖。 */
|
||
CHECK(IEC104_Slave_Release_Startup(fe_slave, &fe_activation, fe_activation_id) == IEC104_TCP_SUBMIT_OK);
|
||
for (unsigned i = 0U; i < 8U; ++i) { fe_poll(); }
|
||
wire = fe_wire(0U, &length);
|
||
CHECK(wire != NULL && length == 77U && wire[-5] == 81U);
|
||
CHECK(memcmp(wire, fe_expected, sizeof(fe_expected)) == 0);
|
||
CHECK(fe_queue().normal_queue.stored_asdu_count == 1U);
|
||
previous_identity = fe_activation;
|
||
previous_activation = fe_activation_id;
|
||
fake.recv_eof = 1U;
|
||
for (unsigned i = 0U; i < 64U && fe_queue().connected; ++i) { fe_poll(); }
|
||
fake.recv_eof = 0U;
|
||
queue = fe_queue();
|
||
CHECK(!queue.connected && queue.listening && queue.normal_queue.pending_asdu_count == 1U);
|
||
fake.sent_bytes = 0U;
|
||
fake.now += 5000U;
|
||
fe_connect();
|
||
CHECK(fe_queue().startup_pending && fe_wire(0U, &length) == NULL);
|
||
CHECK(IEC104_Slave_Release_Startup(fe_slave, &previous_identity, previous_activation) == IEC104_TCP_SUBMIT_ERROR);
|
||
CHECK(IEC104_Slave_Release_Startup(fe_slave, &fe_activation, fe_activation_id) == IEC104_TCP_SUBMIT_OK);
|
||
for (unsigned i = 0U; i < 8U; ++i) { fe_poll(); }
|
||
wire = fe_wire(0U, &length);
|
||
CHECK(wire != NULL && length == 77U && memcmp(wire, fe_expected, 77U) == 0);
|
||
/* 重连重新从发送序号0开始;累计ACK=1确认这一份重发报告,才允许回收保留原文。 */
|
||
push_ack(1U);
|
||
for (unsigned i = 0U; i < 4U; ++i) { fe_poll(); }
|
||
CHECK(fe_queue().normal_queue.stored_asdu_count == 0U);
|
||
CHECK(IEC104_Spontaneous_Get_Status(fe_publisher, &status));
|
||
CHECK(status.published == 1U && status.asdus == 1U && status.objects == 9U && status.waits == 1U);
|
||
CHECK(status.time_invalid == 0U && status.clock_errors == 0U && status.accounting_errors == 0U);
|
||
fe_close();
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:[FAULT/INPUT] 公共格式可表达的空/零地址结构,在业务入口仍须满足注册站和必需8项测量约束。
|
||
* 输入参数:strict——是否启用严格优先级。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:数量256要在遍历前拒绝;重复地址同时覆盖必需8项内部和追加项,不能用同一点填满数量。
|
||
*/
|
||
static void test_invalid_reports(unsigned strict)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
uint64_t old_id;
|
||
fe_open(strict, 0U, true);
|
||
report = fe_report(); fe_expect_input(NULL);
|
||
report.common_address = 0U; fe_expect_input(&report);
|
||
report.common_address = UINT16_MAX; fe_expect_input(&report);
|
||
report.common_address = 2U; fe_expect_input(&report);
|
||
report = fe_report(); report.station_id++; fe_expect_input(&report);
|
||
report = fe_report(); report.event.signals = NULL; fe_expect_input(&report);
|
||
report = fe_report(); report.event.measurements = NULL; fe_expect_input(&report);
|
||
report = fe_report(); report.event.signal_count = 0U; fe_expect_input(&report);
|
||
report.event.signal_count = 256U; fe_expect_input(&report);
|
||
report = fe_report(); report.event.measurement_count = 7U; fe_expect_input(&report);
|
||
report.event.measurement_count = 256U; fe_expect_input(&report);
|
||
report = fe_report(); fe_signals[0].ioa = 0U; fe_expect_input(&report);
|
||
fe_signals[0].ioa = UINT32_C(0x1000000); fe_expect_input(&report);
|
||
report = fe_report(); fe_measurements[7].ioa = 0U; fe_expect_input(&report);
|
||
fe_measurements[7].ioa = UINT32_C(0x1000000); fe_expect_input(&report);
|
||
report = fe_report(); fe_measurements[7].ioa = fe_measurements[0].ioa; fe_expect_input(&report);
|
||
report = fe_report(); report.event.measurement_count = 9U;
|
||
fe_measurements[8].ioa = fe_measurements[7].ioa; fe_expect_input(&report);
|
||
report = fe_report(); report.event.signal_type = 2U; fe_expect_input(&report);
|
||
report = fe_report(); report.event.measurement_type = 12U; fe_expect_input(&report);
|
||
report = fe_report(); fe_signals[0].value = 4U; fe_expect_input(&report);
|
||
report = fe_report(); fe_signals[0].timestamp.time.month = 0U; fe_expect_input(&report);
|
||
report = fe_report(); fe_measurements[7].value.short_float = NAN; fe_expect_input(&report);
|
||
fe_measurements[7].value.short_float = INFINITY; fe_expect_input(&report);
|
||
report = fe_report(); report.event.measurement_count = 34U; fe_expect_input(&report);
|
||
report = fe_report(); report.event.signal_count = 24U; fe_expect_input(&report);
|
||
/* 同CA重新注册不接受旧身份,显式station_id=0仍允许按当前CA发布。 */
|
||
report = fe_report(); old_id = report.station_id;
|
||
CHECK(IEC104_Slave_Remove_Station(fe_slave, &fe_station) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Slave_Add_Station(fe_slave, &fe_station, 0x1234U, NULL) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Station_Get_Id(&fe_station) != old_id);
|
||
fe_expect_input(&report);
|
||
report.station_id = 0U;
|
||
fe_publish(&report);
|
||
fe_close();
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:1/3/30/31都带CP56。
|
||
* 输入参数:strict——是否启用严格优先级。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:遥测9/11走2字节无符号原码,0xFFFF不得被改编码为浮点或补入QDS。
|
||
* SU/IV保持原位;published/asdus按报告计、objects按N+M计、time_invalid只数遥信时标。
|
||
*/
|
||
static void test_type_variants_and_statistics(unsigned strict)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
IEC104_SpontaneousStatus_t status;
|
||
static const uint8_t signal_types[] = {1U,3U,30U,31U};
|
||
fe_open(strict, 0U, true);
|
||
for (unsigned i = 0U; i < sizeof(signal_types); ++i) {
|
||
report = fe_report();
|
||
report.event.signal_type = signal_types[i];
|
||
report.event.measurement_type = i % 2U ? 11U : 9U;
|
||
fe_signals[0].value = i % 2U ? 2U : 1U;
|
||
fe_signals[0].timestamp.is_invalid = 1U;
|
||
fe_signals[0].timestamp.is_summer_time = 1U;
|
||
for (unsigned j = 0U; j < 8U; ++j) { fe_measurements[j].value.unsigned_value = UINT16_MAX; }
|
||
fe_publish(&report);
|
||
CHECK(fe_last_length == 61U && fe_last[7] == signal_types[i]);
|
||
CHECK(fe_last[11] == fe_signals[0].value && fe_last[14] == 0x94U && fe_last[15] == 0x8aU);
|
||
CHECK(fe_last[19] == 8U && fe_last[20] == report.event.measurement_type);
|
||
for (unsigned j = 0U; j < 8U; ++j) {
|
||
size_t offset = 21U + j * 5U;
|
||
CHECK(fe_last[offset] == j + 1U && fe_last[offset + 1U] == 0x40U && fe_last[offset + 2U] == 0U);
|
||
CHECK(fe_last[offset + 3U] == 0xffU && fe_last[offset + 4U] == 0xffU);
|
||
}
|
||
}
|
||
CHECK(IEC104_Spontaneous_Get_Status(fe_publisher, &status));
|
||
CHECK(status.published == 4U && status.asdus == 4U && status.objects == 36U && status.time_invalid == 4U);
|
||
fe_close();
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:[FAULT/ERRORS] 分开检查无句柄/未Init、查询错误、无效快照、入队ERROR/FATAL和事后统计失败。
|
||
* 输入参数:strict——是否启用严格优先级。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:ERROR/FATAL由钩子模拟且不交接字节;这里验证发布契约,不模拟真实故障后的服务恢复过程。
|
||
*/
|
||
static void test_service_errors(unsigned strict)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
IEC104_SpontaneousPublishInfo_t info;
|
||
IEC104_SpontaneousStatus_t status;
|
||
fe_open(strict, 0U, false);
|
||
report = fe_report();
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(NULL, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_STATE);
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, NULL) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_STATE && info.published == 0U);
|
||
fe_initialize(strict, 0U);
|
||
fe_query_failure = IEC104_TCP_INVALID_HANDLE; fe_query_fail_at = fe_queries + 1U;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_STATE);
|
||
fe_query_failure = IEC104_TCP_INTERNAL_ERROR; fe_query_fail_at = fe_queries + 1U;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_QUEUE);
|
||
fe_query_invalid_at = fe_queries + 1U;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_QUEUE && fe_enqueues == 0U);
|
||
for (unsigned i = 0U; i < 2U; ++i) {
|
||
fe_fail_at = fe_enqueues + 1U;
|
||
fe_failure = i == 0U ? IEC104_TCP_SUBMIT_ERROR : IEC104_TCP_SUBMIT_FATAL;
|
||
CHECK(IEC104_Spontaneous_Publish_Fault_Event(fe_publisher, &report, &info) == IEC104_SPONTANEOUS_PUBLISH_ERROR);
|
||
CHECK(info.error == IEC104_SPONTANEOUS_ERROR_SUBMIT && info.submit_result == fe_failure && info.published == 0U);
|
||
}
|
||
CHECK(fe_queue().normal_queue.stored_asdu_count == 0U);
|
||
/* 成功后的诊断查询失败只标记统计不完整,不能把已入队报告改报为失败。 */
|
||
fe_query_invalid_at = fe_queries + 2U;
|
||
fe_publish(&report);
|
||
CHECK(IEC104_Spontaneous_Get_Status(fe_publisher, &status));
|
||
CHECK(status.published == 1U && status.accounting_errors == 1U);
|
||
fe_close();
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:[FAULT/QUEUE] 先缓存普通遥测再缓存故障:兼容模式保持受理顺序,严格模式先选等级6故障帧。
|
||
* 输入参数:strict——是否启用严格优先级。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:两者都走NORMAL并等待ACK;后半段离线填满容量,核对覆盖数量及overflow粘滞标志。
|
||
*/
|
||
static void test_priority_and_overwrite(unsigned strict)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
IEC104_SpontaneousChange_t change = { 0 };
|
||
IEC104_SpontaneousPublishInfo_t info;
|
||
IEC104_SpontaneousStatus_t status;
|
||
IEC104_TcpServerStatus_t queue;
|
||
const uint8_t *wire;
|
||
size_t length;
|
||
unsigned reports;
|
||
fe_open(strict, 0U, true);
|
||
report = fe_report();
|
||
change.common_address = 0x1234U;
|
||
change.ioa = 0x4001U;
|
||
change.kind = IEC104_SPONTANEOUS_FLOAT;
|
||
change.value.short_float = 1.0f;
|
||
CHECK(IEC104_Spontaneous_Publish(fe_publisher, &change, 1U, &info) == IEC104_SPONTANEOUS_PUBLISH_OK);
|
||
fe_publish(&report);
|
||
fe_connect();
|
||
wire = fe_wire(0U, &length);
|
||
CHECK(wire != NULL && wire[0] == (strict ? 42U : 13U));
|
||
wire = fe_wire(1U, &length);
|
||
CHECK(wire != NULL && wire[0] == (strict ? 13U : 42U));
|
||
push_ack(2U);
|
||
for (unsigned i = 0U; i < 4U; ++i) { fe_poll(); }
|
||
CHECK(fe_queue().normal_queue.stored_asdu_count == 0U);
|
||
fe_close();
|
||
fe_open(strict, 0U, true);
|
||
report = fe_report();
|
||
/* 队列条目还占管理头;按纯载荷估算后再加5份,保证各编译容量下都实际触发覆盖。 */
|
||
reports = (unsigned)(IEC104_TCP_NORMAL_CAPACITY / 77U + 5U);
|
||
for (unsigned i = 0U; i < reports; ++i) { fe_publish(&report); }
|
||
queue = fe_queue();
|
||
CHECK(IEC104_Spontaneous_Get_Status(fe_publisher, &status));
|
||
CHECK(status.published == reports && status.overflow);
|
||
CHECK(status.dropped_pending_asdus == reports - queue.normal_queue.pending_asdu_count);
|
||
CHECK(status.overwritten_submitted_asdus == 0U && status.accounting_errors == 0U);
|
||
IEC104_Spontaneous_Clear_Overflow(fe_publisher);
|
||
CHECK(IEC104_Spontaneous_Get_Status(fe_publisher, &status) && !status.overflow);
|
||
fe_close();
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:精确触及104上限:N=4/M=28为250字节,须整份拒绝。
|
||
* 输入参数:strict——是否启用严格优先级。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:N=2/M=31为249字节,须完整写出。
|
||
* 在NORMAL=272的构建中,最大ASDU连同队列管理头仍须可缓存;不能只测编码缓冲容量。
|
||
* 先独立核对第二个遥信和最后一个遥测的偏移/字节,再用Getter补充验证索引读取。
|
||
*/
|
||
static void test_full_frame_boundary(unsigned strict)
|
||
{
|
||
IEC104_SpontaneousFaultEvent_t report;
|
||
IEC60870_FaultEventView_t view;
|
||
IEC60870_FaultEventSignal_t signal;
|
||
IEC60870_FaultEventMeasurement_t measurement;
|
||
const uint8_t *wire;
|
||
size_t length;
|
||
fe_open(strict, 0U, true);
|
||
report = fe_report();
|
||
report.event.signal_count = 4U;
|
||
report.event.measurement_count = 28U; /* 6+4+4*11+28*7=250。 */
|
||
fe_expect_input(&report);
|
||
report.event.signal_count = 2U;
|
||
report.event.measurement_count = 31U; /* 6+4+2*11+31*7=249。 */
|
||
fe_signals[1].value = 0U;
|
||
fe_signals[1].timestamp.time.second = 31U;
|
||
fe_publish(&report);
|
||
CHECK(fe_last_length == 249U);
|
||
fe_connect();
|
||
wire = fe_wire(0U, &length);
|
||
CHECK(wire != NULL && length == 249U && wire[-5] == 253U);
|
||
CHECK(wire[0] == 42U && wire[6] == 2U && wire[7] == 30U);
|
||
/* 两个遥信各11字节:第二项始于19,M/type移至30/31,末项遥测始于242。 */
|
||
CHECK(wire[19] == 4U && wire[20] == 2U && wire[21] == 1U && wire[22] == 0U);
|
||
CHECK(wire[23] == 0x93U && wire[24] == 0x79U);
|
||
CHECK(wire[30] == 31U && wire[31] == 13U);
|
||
CHECK(wire[242] == 31U && wire[243] == 0x40U && wire[244] == 0U);
|
||
CHECK(wire[245] == 0U && wire[246] == 0U && wire[247] == 0xf8U && wire[248] == 0x41U);
|
||
CHECK(IEC60870_FaultEvent_Parse(IEC104_Slave_Get_Application_Layer_Parameters(fe_slave),
|
||
wire, (uint16_t)length, &view) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_FaultEvent_Get_Signal(&view, 1U, &signal) == IEC60870_ASDU_OK);
|
||
CHECK(signal.ioa == 0x010204U && signal.value == 0U && signal.timestamp.time.second == 31U);
|
||
CHECK(IEC60870_FaultEvent_Get_Measurement(&view, 30U, &measurement) == IEC60870_ASDU_OK);
|
||
CHECK(measurement.ioa == 0x401fU && measurement.value.short_float == 31.0f);
|
||
CHECK(fe_wire(1U, &length) == NULL);
|
||
push_ack(1U);
|
||
for (unsigned i = 0U; i < 4U; ++i) { fe_poll(); }
|
||
CHECK(fe_queue().normal_queue.stored_asdu_count == 0U);
|
||
fe_close();
|
||
}
|
||
|
||
/**
|
||
* 功能:[ENTRY] 构建脚本覆盖服务容量1/2及NORMAL容量4096/272。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
|
||
* 说明:每个可执行程序再跑strict=0/1。
|
||
* 服务池容量验证实例管理不依赖单槽;小NORMAL验证覆盖和最大帧适配,严格模式验证等级仲裁。
|
||
*/
|
||
int main(void)
|
||
{
|
||
CHECK(sizeof(fe_expected) == 77U);
|
||
for (unsigned strict = 0U; strict < 2U; ++strict) {
|
||
test_wire_retry_reconnect(strict);
|
||
test_invalid_reports(strict);
|
||
test_type_variants_and_statistics(strict);
|
||
test_service_errors(strict);
|
||
test_priority_and_overwrite(strict);
|
||
test_full_frame_boundary(strict);
|
||
}
|
||
printf("FaultEvent: groups=12 TCP=%u NORMAL=%u instance=%zu bytes OK\n",
|
||
(unsigned)IEC104_TCP_SERVER_CAPACITY, (unsigned)IEC104_TCP_NORMAL_CAPACITY,
|
||
sizeof(IEC104_Spontaneous_t));
|
||
return 0;
|
||
}
|