1275 lines
58 KiB
C
1275 lines
58 KiB
C
/*
|
||
* Type102读命令专项:可控Net配合真实Slave、TCP和发送队列,独立字节检查线上应答。
|
||
* 按TCP容量1/2、任务槽1/4构建;lookup调用、等待、超时和引用归还分别断言。
|
||
* 本文件只提供同步取值替身,不依赖示例程序,也不执行实际网络等待。
|
||
*/
|
||
/*
|
||
* 文件内导航(搜索方括号标记;Suite:Read):
|
||
* 仅验证Type102读命令;复用公共fake Net和真实传输。read_*辅助在本文件共用,lookup与故障钩子属于本专项。
|
||
* [COMMON/BASE] 公共基础:依赖、编译选项与断言入口
|
||
* [READ/FIXTURE_STATE] 读夹具状态:服务、站点、任务、lookup模式及注入结果
|
||
* [READ/HOOKS] 测试钩子:HIGH余量查询和应答提交结果
|
||
* [COMMON/LIFECYCLE] 专项共用辅助:通知、Poll、状态、连接与资源回收
|
||
* [COMMON/WIRE] 专项共用辅助:读请求构造、独立字节应答收集与比较
|
||
* [READ/LOOKUP_HELPERS] lookup替身:点值、误用、重入及白名单类型构造
|
||
* [READ/CONFIGURATION] 读配置用例:接口约束、默认期限及实例池
|
||
* [READ/RESULTS] 读结果用例:16种应答类型、无效品质/Type21边界、UNKNOWN/ERROR及非法容器
|
||
* [READ/WAIT_TIMEOUT] 读等待用例:余量预检、提交WAIT和受理期限
|
||
* [READ/LIFETIME] 读生命周期用例:会话、重入、故障及原站引用
|
||
* [READ/FIFO] 读队列用例:管线、真实HIGH余量和到期队首
|
||
* [READ/VALIDATION] 读校验用例:结构优先级、45/46/47、满槽与期限
|
||
* [READ/SERVICE_ERRORS] 服务查询故障:只读失败原子性、任务清理及站点引用归还
|
||
* [ENTRY/MAIN] 测试入口:读命令各类用例登记
|
||
* 配置和实例池放在功能用例之前;服务查询故障单列,便于核对只读失败和终结清理两种契约。
|
||
* 函数定义按主题归类;main保留原调用顺序,RUN/CASE标记细分执行组。
|
||
*/
|
||
|
||
/* ============================================================================
|
||
* [COMMON/BASE] 公共基础:依赖、编译选项与断言入口
|
||
* ============================================================================ */
|
||
#include "iec104_test_config.h"
|
||
#include "iec104_tcp_fixture.h"
|
||
#include "IEC104_Read.h"
|
||
|
||
#ifndef IEC104_READ_TASK_CAPACITY
|
||
#define IEC104_READ_TASK_CAPACITY 4U
|
||
#endif
|
||
#ifndef IEC104_READ_CAPACITY
|
||
#define IEC104_READ_CAPACITY IEC104_TCP_SERVER_CAPACITY
|
||
#endif
|
||
|
||
/* ============================================================================
|
||
* [READ/FIXTURE_STATE] 读夹具状态:服务、站点、任务、lookup模式及注入结果
|
||
* ============================================================================ */
|
||
static IEC104_Slave_t *read_slave;
|
||
static IEC104_Read_t *reader;
|
||
static IEC104_Station_t read_station;
|
||
static IEC104_ReadStatus_t rd;
|
||
static IEC104_SlaveEvent_t read_input;
|
||
static IEC104_ReadEventResult_t read_handled;
|
||
static unsigned read_ns, read_groups, read_calls;
|
||
static uint16_t read_window = 32U;
|
||
static int read_value = 1;
|
||
/* room_override=-1使用真实余量,非负值仅覆盖预检快照,不改变实际队列。
|
||
* query_fault:1查询失败,2队列统计无效;submit_fault:1 WAIT、2 FATAL、
|
||
* 3普通ERROR、4先收到STOP再报ERROR。submit_advance只推进提交期间的时钟。 */
|
||
static int room_override = -1, query_fault, submit_fault;
|
||
static uint64_t submit_advance;
|
||
static unsigned lookup_mode;
|
||
|
||
/* ============================================================================
|
||
* [READ/HOOKS] 测试钩子:HIGH余量查询和应答提交结果
|
||
* ============================================================================ */
|
||
#ifdef IEC104_READ_TEST_HOOKS
|
||
/**
|
||
* 功能:只覆盖读模块看到的HIGH余量或查询结果。
|
||
* 输入参数:slave——被测 Slave 实例(只读);status——状态快照;error——底层错误码存储。
|
||
* 输出参数:status——状态快照;error——底层错误码存储。
|
||
* 返回值:真实传输接口或故障注入产生的 TCP 操作结果。
|
||
* 说明:真实队列内容不变,便于区分WAIT和查询故障。
|
||
*/
|
||
IEC104_TcpResult_t IEC104_Read_Test_Get_Status(const IEC104_Slave_t *slave,
|
||
IEC104_TcpServerStatus_t *status, IEC104_TcpError_t *error)
|
||
{
|
||
IEC104_TcpResult_t result;
|
||
if (query_fault == 1) { return IEC104_TCP_INTERNAL_ERROR; }
|
||
result = IEC104_Slave_Get_Status(slave, status, error);
|
||
if (result == IEC104_TCP_OK) {
|
||
if (query_fault == 2) { status->asdu_queue_status_valid = false; }
|
||
if (room_override >= 0) { status->high_queue.free_bytes = (uint32_t)room_override; }
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:在唯一提交边界注入WAIT/ERROR/FATAL或时钟推进,其余调用仍交给真实发送层。
|
||
* 输入参数:slave——被测 Slave 实例;identity——请求所属的 Slave、实例及连接代次身份(只读);response——响应 ASDU(只读)。
|
||
* 输出参数:无。
|
||
* 返回值:发送提交结果,区分接收成功、WAIT、ERROR 与 FATAL。
|
||
* 说明:STOP模式保留未读通知,让模块按通知顺序停用,不能直接把该ERROR等同于内部故障。
|
||
*/
|
||
IEC104_TcpSubmitResult_t IEC104_Read_Test_Send_Response(IEC104_Slave_t *slave,
|
||
const IEC104_SlaveEventIdentity_t *identity, const IEC60870_Asdu_t *response)
|
||
{
|
||
fake.now += submit_advance;
|
||
if (submit_fault == 1) { return IEC104_TCP_SUBMIT_WAIT; }
|
||
if (submit_fault == 2) { return IEC104_TCP_SUBMIT_FATAL; }
|
||
if (submit_fault == 3) { return IEC104_TCP_SUBMIT_ERROR; }
|
||
if (submit_fault == 4) {
|
||
push(stop_act, sizeof(stop_act));
|
||
CHECK(IEC104_Slave_Poll(slave, 0U, NULL) == IEC104_TCP_OK);
|
||
return IEC104_TCP_SUBMIT_ERROR;
|
||
}
|
||
return IEC104_Slave_Send_Response(slave, identity, response);
|
||
}
|
||
#endif
|
||
|
||
/* ============================================================================
|
||
* [COMMON/LIFECYCLE] 专项共用辅助:通知、Poll、状态、连接与资源回收
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:按通知FIFO分派生命周期和I帧。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:复制最后一条请求供重入、旧代次测试使用,
|
||
* 不借用NextEvent内部缓冲,也不在分派阶段执行lookup。
|
||
*/
|
||
static void read_drain(void)
|
||
{
|
||
IEC104_SlaveEvent_t event;
|
||
IEC104_TcpEventResult_t result;
|
||
while ((result = IEC104_Slave_NextEvent(read_slave, &event)) == IEC104_TCP_EVENT_READY) {
|
||
IEC104_ReadEventResult_t handled = IEC104_Read_Handle_Event(reader, &event);
|
||
CHECK(handled != IEC104_READ_EVENT_ERROR);
|
||
if (event.transport.type == IEC104_TCP_EVENT_I_FRAME) {
|
||
CHECK(IEC104_Slave_Copy_Event(&read_input, &event));
|
||
read_handled = handled;
|
||
}
|
||
}
|
||
CHECK(result == IEC104_TCP_EVENT_EMPTY);
|
||
}
|
||
|
||
/**
|
||
* 功能:网络推进后立即分派通知。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:失败诊断带用例组、lookup模式及时间,便于定位矩阵分支。
|
||
*/
|
||
static void read_poll(void)
|
||
{
|
||
IEC104_TcpResult_t result = IEC104_Slave_Poll(read_slave, 0U, NULL);
|
||
if (result != IEC104_TCP_OK) {
|
||
fprintf(stderr, "Read poll: group=%u mode=%u now=%llu result=%d\n", read_groups,
|
||
lookup_mode, (unsigned long long)fake.now, (int)result);
|
||
}
|
||
CHECK(result == IEC104_TCP_OK);
|
||
read_drain();
|
||
}
|
||
|
||
/**
|
||
* 功能:每次观察状态同时检查统计恒等式,避免负应答混入answered。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:检查不满足时由 CHECK 立即报告失败,不继续本用例。
|
||
*/
|
||
static void read_status(void)
|
||
{
|
||
CHECK(IEC104_Read_Get_Status(reader, &rd));
|
||
CHECK(rd.submitted == rd.answered + rd.negative_submitted);
|
||
}
|
||
|
||
/**
|
||
* 功能:只推进网络与错误回复。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:故意不调用Execute_Next,取值次数由各用例显式控制。
|
||
*/
|
||
static void read_flush(void)
|
||
{
|
||
unsigned i;
|
||
for (i = 0U; i < 12U; ++i) {
|
||
read_poll();
|
||
CHECK(IEC104_Read_Process(reader));
|
||
}
|
||
read_status();
|
||
}
|
||
|
||
/**
|
||
* 功能:accept只建立连接,STARTDT之后才允许读业务。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:重新建连同时重置主站发送序号。
|
||
*/
|
||
static void read_connect(void)
|
||
{
|
||
fake.accept_pending = 1U;
|
||
read_poll();
|
||
push(start_act, sizeof(start_act));
|
||
read_poll();
|
||
read_ns = 0U;
|
||
read_status();
|
||
CHECK(rd.active);
|
||
}
|
||
|
||
/**
|
||
* 功能:每例重建服务、站点和读模块。
|
||
* 输入参数:timeout——等待或执行期限(毫秒)。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:传输期限放宽到600秒,避免10毫秒等读期限用例
|
||
* 被链路超时抢先结束;timeout=0只禁用读模块期限。
|
||
*/
|
||
static void read_open(uint32_t timeout)
|
||
{
|
||
IEC104_SlaveConfig_t config;
|
||
IEC104_ReadConfig_t read_config;
|
||
unsigned i;
|
||
memset(&fake, 0, sizeof(fake));
|
||
memset(&read_station, 0, sizeof(read_station));
|
||
memset(&read_input, 0, sizeof(read_input));
|
||
fake.now = 1000U;
|
||
read_calls = 0U;
|
||
read_value = 1;
|
||
room_override = -1; query_fault = submit_fault = 0; submit_advance = 0U; lookup_mode = 0U;
|
||
read_slave = IEC104_Slave_Create();
|
||
CHECK(read_slave != NULL);
|
||
test_transport_config(&config);
|
||
config.protocol.link.k = read_window;
|
||
config.protocol.link.t1_ms = 600000U;
|
||
config.protocol.link.t3_ms = 600000U;
|
||
config.protocol.local_send_timeout_ms = 600000U;
|
||
CHECK(IEC104_Slave_Init(read_slave, &config, NULL) == IEC104_TCP_OK);
|
||
CHECK(IEC104_Slave_Add_Station(read_slave, &read_station, 1U, &read_value) == IEC104_STATION_OK);
|
||
IEC104_Read_Config_Default(&read_config);
|
||
read_config.request_timeout_ms = timeout;
|
||
reader = IEC104_Read_Create(read_slave, &read_config);
|
||
CHECK(reader != NULL);
|
||
sut = NULL;
|
||
for (i = 0U; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||
if (server_pool[i].in_use) { sut = &server_pool[i].server; break; }
|
||
}
|
||
CHECK(sut != NULL);
|
||
read_connect();
|
||
}
|
||
|
||
/**
|
||
* 功能:Cancel先归还任务引用,关闭后仍须读空通知。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:最后删除站点并检查可复用,
|
||
* 使正常用例退出也能暴露站点引用泄漏。
|
||
*/
|
||
static void read_close(void)
|
||
{
|
||
IEC104_Read_Cancel(reader);
|
||
CHECK(IEC104_Slave_Close(read_slave, NULL) == IEC104_TCP_OK);
|
||
read_drain();
|
||
CHECK(IEC104_Read_Destroy(reader));
|
||
if (IEC104_Station_Is_Registered(&read_station)) {
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &read_station) == IEC104_STATION_OK);
|
||
}
|
||
CHECK(IEC104_Station_Is_Reusable(&read_station));
|
||
CHECK(IEC104_Slave_Destroy(read_slave, NULL) == IEC104_TCP_OK);
|
||
read_window = 32U;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [COMMON/WIRE] 专项共用辅助:读请求构造、独立字节应答收集与比较
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:只封装APCI并送入fake Net。
|
||
* 输入参数:bytes——报文字节缓冲区(只读);length——字节长度。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:不推进服务,便于多条请求在同次Poll中到达。
|
||
*/
|
||
static void read_queue(const uint8_t *bytes, size_t length)
|
||
{
|
||
uint8_t frame[255] = {0x68};
|
||
CHECK(length <= 249U);
|
||
frame[1] = (uint8_t)(length + 4U);
|
||
frame[2] = (uint8_t)(read_ns << 1);
|
||
frame[3] = (uint8_t)(read_ns >> 7);
|
||
++read_ns;
|
||
memcpy(frame + 6U, bytes, length);
|
||
push(frame, length + 6U);
|
||
}
|
||
|
||
/**
|
||
* 功能:手工填写Type102请求并收取通知。
|
||
* 输入参数:cot——完整传送原因字节;ca——公共地址;ioa——信息对象地址;oa——发起者地址。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:不自动执行lookup,使接收与取值两个阶段可分别断言。
|
||
*/
|
||
static void read_send(unsigned cot, unsigned ca, unsigned ioa, unsigned oa)
|
||
{
|
||
uint8_t bytes[] = {102, 1, 5, 0, 1, 0, 0xe9, 3, 0};
|
||
bytes[2] = (uint8_t)cot;
|
||
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);
|
||
read_queue(bytes, sizeof(bytes));
|
||
read_poll();
|
||
}
|
||
|
||
/**
|
||
* 功能:返回fake.output内的只读ASDU视图,跳过U/S帧和专门挤占HIGH的Type200填充帧。
|
||
* 输入参数:frames——按线上顺序排列的 ASDU 视图数组;lengths——各 ASDU 的长度数组。
|
||
* 输出参数:frames——按线上顺序排列的 ASDU 视图数组;lengths——各 ASDU 的长度数组。
|
||
* 返回值:本次收集或统计到的有效业务帧数量。
|
||
* 说明:此处只定位字节,不调用待测ASDU解码器来生成测试预期。
|
||
*/
|
||
static unsigned read_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 = (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;
|
||
}
|
||
|
||
/**
|
||
* 功能:按线上顺序比较完整ASDU长度及字节。
|
||
* 输入参数:index——待访问或核对的条目下标;expected——期望的结果或数量(只读);length——字节长度。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:期望由用例提供,不从待测解析结果反推。
|
||
*/
|
||
static void read_expect(unsigned index, const uint8_t *expected, size_t length)
|
||
{
|
||
const uint8_t *frames[64];
|
||
size_t lengths[64];
|
||
CHECK(read_collect(frames, lengths) > index);
|
||
CHECK(lengths[index] == length);
|
||
CHECK(memcmp(frames[index], expected, length) == 0);
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/LOOKUP_HELPERS] lookup替身:点值、误用、重入及白名单类型构造
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:正常模式按受理时station_id取context。
|
||
* 输入参数:context——业务回调的测试上下文;request——当前业务请求(只读);response——响应 ASDU。
|
||
* 输出参数:response——构造、解析或追加完成的对象/ASDU。
|
||
* 返回值:查值结果,区分追加成功、结束/跳过、容量不足及失败。
|
||
* 说明:不能按CA重查,否则站点替换测试会读错。
|
||
* 模式1..3注入返回值,4..6控制时间/重入,10..23检查容器误用,30..32检查
|
||
* 回调后服务状态复查。违约模式仅用于防御分支测试,不是应用lookup的写法。
|
||
*/
|
||
static IEC104_ReadLookupResult_t read_point(void *context,
|
||
const IEC104_ReadRequest_t *request, IEC60870_Asdu_t *response)
|
||
{
|
||
IEC60870_SinglePointInformation_t point;
|
||
IEC104_Station_t *station = IEC104_Slave_Get_Station_By_Id(read_slave, request->station_id);
|
||
int *value = IEC104_Station_Get_Context(station);
|
||
CHECK(context == &read_calls && value != NULL);
|
||
++read_calls;
|
||
CHECK(IEC60870_SinglePointInformation_Create(&point, request->ioa, *value, 0U) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.object) == IEC60870_ASDU_OK);
|
||
switch (lookup_mode) {
|
||
case 1: return IEC104_READ_LOOKUP_UNKNOWN;
|
||
case 2: return IEC104_READ_LOOKUP_ERROR;
|
||
case 3: return (IEC104_ReadLookupResult_t)99;
|
||
case 4: fake.now += 10U; break;
|
||
case 6: fake.now = 1000U; break; /* Restore transport clock after testing disabled read deadline. */
|
||
case 5: {
|
||
IEC104_ReadStatus_t before, after;
|
||
CHECK(IEC104_Read_Get_Status(reader, &before));
|
||
CHECK(!IEC104_Read_Process(reader) && !IEC104_Read_Destroy(reader));
|
||
CHECK(IEC104_Read_Handle_Event(reader, &read_input) == IEC104_READ_EVENT_ERROR);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_point, context) == IEC104_READ_EXECUTE_ERROR);
|
||
IEC104_Read_Cancel(reader);
|
||
CHECK(IEC104_Read_Get_Status(reader, &after));
|
||
CHECK(before.pending_requests == after.pending_requests && before.cancelled == after.cancelled);
|
||
break;
|
||
}
|
||
/* Intentional contract violations stop short of writing outside the local buffer. */
|
||
case 10: response->buffer = NULL; break;
|
||
case 11: ++response->capacity; break;
|
||
case 12: response->parameters = NULL; break;
|
||
case 13: CHECK(IEC60870_Asdu_Set_COT(response, 3U) == IEC60870_ASDU_OK); break;
|
||
case 14: CHECK(IEC60870_Asdu_Set_CA(response, 2U) == IEC60870_ASDU_OK); break;
|
||
case 15: CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.object) == IEC60870_ASDU_OK); break;
|
||
case 16: response->buffer[3] ^= 1U; break;
|
||
case 17: response->buffer[2] ^= 0x80U; break;
|
||
case 18: response->buffer[6] ^= 1U; break;
|
||
case 19: response->buffer[9] |= 2U; break; /* SIQ reserved bit: Parse must reject. */
|
||
case 20: response->is_from_buffer = 1U; break;
|
||
case 21: response->length = 22U; break;
|
||
case 22: CHECK(IEC60870_Asdu_Set_Negative(response, 1) == IEC60870_ASDU_OK); break;
|
||
case 23: response->is_sequence = 1U; response->buffer[1] |= 0x80U; break;
|
||
case 30: /* Produce an unread notification without consuming it. */
|
||
push(stop_act, sizeof(stop_act));
|
||
CHECK(IEC104_Slave_Poll(read_slave, 0U, NULL) == IEC104_TCP_OK);
|
||
break;
|
||
case 31: /* Violate contract and consume STOP too: post-callback stale check still cancels. */
|
||
push(stop_act, sizeof(stop_act));
|
||
CHECK(IEC104_Slave_Poll(read_slave, 0U, NULL) == IEC104_TCP_OK);
|
||
{ IEC104_SlaveEvent_t event;
|
||
while (IEC104_Slave_NextEvent(read_slave, &event) == IEC104_TCP_EVENT_READY) { } }
|
||
break;
|
||
case 32: sut->in_poll = 1U; break; /* Service query error; restored by test. */
|
||
default: break;
|
||
}
|
||
return IEC104_READ_LOOKUP_FOUND;
|
||
}
|
||
|
||
/**
|
||
* 功能:仅尝试执行一个队首READ。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:单步执行结果,区分已执行、无任务、等待及错误。
|
||
* 说明:是否先Poll、重取或继续等待均由用例显式决定。
|
||
*/
|
||
static IEC104_ReadExecuteResult_t read_execute(void)
|
||
{
|
||
IEC104_ReadExecuteResult_t result = IEC104_Read_Execute_Next(reader, read_point, &read_calls);
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:逐类型构造合法应用对象,供白名单及非法类型回归使用。
|
||
* 输入参数:context——业务回调的测试上下文;request——当前业务请求(只读);response——响应 ASDU。
|
||
* 输出参数:response——构造、解析或追加完成的对象/ASDU。
|
||
* 返回值:查值结果,区分追加成功、结束/跳过、容量不足及失败。
|
||
* 说明:预期载荷另在用例中手写,避免构造器和应答校验共用同一种编码错误。
|
||
*/
|
||
static IEC104_ReadLookupResult_t read_typed_point(void *context,
|
||
const IEC104_ReadRequest_t *request, IEC60870_Asdu_t *response)
|
||
{
|
||
unsigned type = *(unsigned *)context;
|
||
IEC60870_CP56Time2a_t time;
|
||
IEC60870_BinaryCounterReading_t counter;
|
||
IEC60870_CP16Time2a_t elapsed;
|
||
memset(&time, 0, sizeof(time));
|
||
memset(&counter, 0, sizeof(counter));
|
||
memset(&elapsed, 0, sizeof(elapsed));
|
||
time.time.year = 2026; time.time.month = 11; time.time.day = 3;
|
||
time.time.hour = 12; time.time.minute = 34; time.time.second = 12; time.time.ms = 345;
|
||
time.has_weekday = 1U;
|
||
#define ADD_POINT(id, name, ...) case id: { IEC60870_##name##_t point; \
|
||
CHECK(IEC60870_##name##_Create(&point, request->ioa, __VA_ARGS__) == IEC60870_ASDU_OK); \
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, (const IEC60870_InformationObject_t *)&point) == IEC60870_ASDU_OK); break; }
|
||
switch (type) {
|
||
ADD_POINT(1, SinglePointInformation, 1, 0x80)
|
||
ADD_POINT(3, DoublePointInformation, 2, 0)
|
||
ADD_POINT(5, StepPositionInformation, -2, 1, 0)
|
||
ADD_POINT(7, BitString32, UINT32_C(0x12345678), 0)
|
||
ADD_POINT(9, MeasuredValueNormalized, -123, 0)
|
||
ADD_POINT(11, MeasuredValueScaled, -123, 0)
|
||
ADD_POINT(13, MeasuredValueShort, 1.0f, 0)
|
||
ADD_POINT(20, PackedSinglePointWithSCD, 0x1234, 0x5678, 0)
|
||
ADD_POINT(21, MeasuredValueNormalizedWithoutQuality, -123)
|
||
ADD_POINT(30, SinglePointWithCP56Time2a, 1, 0x80, &time)
|
||
ADD_POINT(31, DoublePointWithCP56Time2a, 2, 0, &time)
|
||
ADD_POINT(32, StepPositionWithCP56Time2a, -2, 1, 0, &time)
|
||
ADD_POINT(33, Bitstring32WithCP56Time2a, UINT32_C(0x12345678), 0, &time)
|
||
ADD_POINT(34, MeasuredValueNormalizedWithCP56Time2a, -123, 0, &time)
|
||
ADD_POINT(35, MeasuredValueScaledWithCP56Time2a, -123, 0, &time)
|
||
ADD_POINT(36, MeasuredValueShortWithCP56Time2a, 1.0f, 0, &time)
|
||
ADD_POINT(15, IntegratedTotals, &counter)
|
||
ADD_POINT(37, IntegratedTotalsWithCP56Time2a, &counter, &time)
|
||
ADD_POINT(38, EventOfProtectionEquipmentWithCP56Time2a, 1, 0, &elapsed, &time)
|
||
ADD_POINT(45, SingleCommand, 1, 0, 0)
|
||
ADD_POINT(100, InterrogationCommand, 20)
|
||
default: CHECK(0);
|
||
}
|
||
#undef ADD_POINT
|
||
if (lookup_mode == 40U) { response->buffer[response->length - 2U] = 0U; } /* Invalid CP56 month. */
|
||
return IEC104_READ_LOOKUP_FOUND;
|
||
}
|
||
|
||
/* 应用取值快照替身:点表固定IOA=0x030201和Type,available只描述数据是否可用。
|
||
* 有品质的已知点仍返回FOUND并置IV,其他品质位及原值保留;Type21无品质时才返回ERROR。
|
||
* UNKNOWN仅由点表查找失败产生;CP56时标IV与数据IV分别来自快照,不互相推导。 */
|
||
typedef struct {
|
||
uint8_t type_id, quality;
|
||
int16_t value;
|
||
bool available, time_invalid;
|
||
} ReadSnapshot;
|
||
|
||
/**
|
||
* 功能:按固定IOA读取快照并追加对应类型对象。
|
||
* 输入参数:context——业务回调的测试上下文;request——当前业务请求(只读);response——响应 ASDU。
|
||
* 输出参数:response——构造、解析或追加完成的对象/ASDU。
|
||
* 返回值:查值结果,区分追加成功、结束/跳过、容量不足及失败。
|
||
* 说明:不可用点保留原值并置IV,无品质的Type21返回ERROR。
|
||
*/
|
||
static IEC104_ReadLookupResult_t read_snapshot_point(void *context,
|
||
const IEC104_ReadRequest_t *request, IEC60870_Asdu_t *response)
|
||
{
|
||
const ReadSnapshot *snapshot = context;
|
||
uint8_t quality;
|
||
++read_calls;
|
||
CHECK(IEC104_Slave_Get_Station_By_Id(read_slave, request->station_id) == &read_station);
|
||
if (request->ioa != UINT32_C(0x030201)) { return IEC104_READ_LOOKUP_UNKNOWN; }
|
||
if (snapshot->type_id == 21U && !snapshot->available) { return IEC104_READ_LOOKUP_ERROR; }
|
||
quality = snapshot->quality;
|
||
if (!snapshot->available) { quality |= IEC60870_ASDU_QUALITY_IV; }
|
||
switch (snapshot->type_id) {
|
||
case 1U: {
|
||
IEC60870_SinglePointInformation_t point;
|
||
CHECK(IEC60870_SinglePointInformation_Create(&point, request->ioa,
|
||
snapshot->value, quality) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.object) == IEC60870_ASDU_OK);
|
||
break;
|
||
}
|
||
case 3U: {
|
||
IEC60870_DoublePointInformation_t point;
|
||
CHECK(IEC60870_DoublePointInformation_Create(&point, request->ioa,
|
||
snapshot->value, quality) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.object) == IEC60870_ASDU_OK);
|
||
break;
|
||
}
|
||
case 11U: {
|
||
IEC60870_MeasuredValueScaled_t point;
|
||
CHECK(IEC60870_MeasuredValueScaled_Create(&point, request->ioa,
|
||
snapshot->value, quality) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.object) == IEC60870_ASDU_OK);
|
||
break;
|
||
}
|
||
case 21U: {
|
||
IEC60870_MeasuredValueNormalizedWithoutQuality_t point;
|
||
CHECK(IEC60870_MeasuredValueNormalizedWithoutQuality_Create(&point,
|
||
request->ioa, snapshot->value) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.object) == IEC60870_ASDU_OK);
|
||
break;
|
||
}
|
||
case 35U: {
|
||
IEC60870_MeasuredValueScaledWithCP56Time2a_t point;
|
||
IEC60870_CP56Time2a_t time;
|
||
memset(&time, 0, sizeof(time));
|
||
time.time.year = 2026; time.time.month = 11; time.time.day = 3;
|
||
time.time.hour = 12; time.time.minute = 34; time.time.second = 12; time.time.ms = 345;
|
||
time.has_weekday = 1U;
|
||
time.is_invalid = snapshot->time_invalid ? 1U : 0U;
|
||
CHECK(IEC60870_MeasuredValueScaledWithCP56Time2a_Create(&point, request->ioa,
|
||
snapshot->value, quality, &time) == IEC60870_ASDU_OK);
|
||
CHECK(IEC60870_Asdu_Add_InformationObject(response, &point.base.object) == IEC60870_ASDU_OK);
|
||
break;
|
||
}
|
||
default: CHECK(0); return IEC104_READ_LOOKUP_ERROR;
|
||
}
|
||
return IEC104_READ_LOOKUP_FOUND;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/CONFIGURATION] 读配置用例:接口约束、默认期限及实例池
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:默认期限为15秒。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:空句柄、空回调和重复绑定均应拒绝,失败的只读查询不得改写输出。
|
||
*/
|
||
static void test_read_configuration(void)
|
||
{
|
||
IEC104_ReadConfig_t config;
|
||
IEC104_ReadStatus_t output, before;
|
||
IEC104_Read_Config_Default(NULL);
|
||
IEC104_Read_Config_Default(&config);
|
||
CHECK(config.request_timeout_ms == 15000U);
|
||
CHECK(IEC104_Read_Create(NULL, &config) == NULL);
|
||
CHECK(!IEC104_Read_Destroy(NULL) && !IEC104_Read_Process(NULL));
|
||
IEC104_Read_Cancel(NULL);
|
||
memset(&output, 0x5a, sizeof(output)); before = output;
|
||
CHECK(!IEC104_Read_Get_Status(NULL, &output) && memcmp(&output, &before, sizeof(output)) == 0);
|
||
read_open(15000U);
|
||
CHECK(IEC104_Read_Create(read_slave, &config) == NULL);
|
||
CHECK(IEC104_Read_Create(read_slave, NULL) == NULL);
|
||
CHECK(IEC104_Read_Handle_Event(reader, NULL) == IEC104_READ_EVENT_ERROR);
|
||
CHECK(IEC104_Read_Execute_Next(reader, NULL, NULL) == IEC104_READ_EXECUTE_ERROR);
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:填满Slave/Read静态实例池,同时保持首实例有待处理请求。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:不同实例不能接收首实例的事件;创建/取消其他实例也不能改动首实例的任务计数。
|
||
*/
|
||
static void test_read_instance_pool(void)
|
||
{
|
||
IEC104_Slave_t *slaves[IEC104_TCP_SERVER_CAPACITY];
|
||
IEC104_Read_t *readers[IEC104_TCP_SERVER_CAPACITY];
|
||
IEC104_ReadConfig_t config;
|
||
unsigned i;
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U);
|
||
IEC104_Read_Config_Default(&config);
|
||
for (i = 1U; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||
slaves[i] = IEC104_Slave_Create(); CHECK(slaves[i] != NULL);
|
||
readers[i] = IEC104_Read_Create(slaves[i], &config);
|
||
if (i < IEC104_READ_CAPACITY) {
|
||
IEC104_ReadStatus_t status;
|
||
CHECK(readers[i] != NULL);
|
||
CHECK(IEC104_Read_Handle_Event(readers[i], &read_input) == IEC104_READ_EVENT_ERROR);
|
||
IEC104_Read_Cancel(readers[i]);
|
||
CHECK(IEC104_Read_Get_Status(readers[i], &status) && status.pending_requests == 0U);
|
||
} else { CHECK(readers[i] == NULL); }
|
||
}
|
||
CHECK(IEC104_Slave_Create() == NULL);
|
||
read_status(); CHECK(rd.pending_requests == 1U && rd.accepted == 1U && rd.cancelled == 0U);
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
for (i = 1U; i < IEC104_TCP_SERVER_CAPACITY; ++i) {
|
||
if (readers[i] != NULL) { CHECK(IEC104_Read_Destroy(readers[i])); }
|
||
CHECK(IEC104_Slave_Destroy(slaves[i], NULL) == IEC104_TCP_OK);
|
||
}
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/RESULTS] 读结果用例:16种应答类型、无效品质/Type21边界、UNKNOWN/ERROR及非法容器
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:16种白名单类型逐字节比较。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:固定TEST/OA同时验证它们由请求传入数据应答,
|
||
* 预期payload独立手写,避免构造器和预期共用同一编码错误。
|
||
*/
|
||
static void test_read_types(void)
|
||
{
|
||
static const struct { unsigned type, length; uint8_t payload[12]; } cases[] = {
|
||
{1, 1, {0x81}}, {3, 1, {2}}, {5, 2, {0xfe, 0}},
|
||
{7, 5, {0x78, 0x56, 0x34, 0x12, 0}}, {9, 3, {0x85, 0xff, 0}},
|
||
{11, 3, {0x85, 0xff, 0}}, {13, 5, {0, 0, 0x80, 0x3f, 0}},
|
||
{20, 5, {0x34, 0x12, 0x78, 0x56, 0}}, {21, 2, {0x85, 0xff}},
|
||
{30, 8, {0x81, 0x39, 0x30, 34, 12, 0x43, 11, 26}},
|
||
{31, 8, {2, 0x39, 0x30, 34, 12, 0x43, 11, 26}},
|
||
{32, 9, {0xfe, 0, 0x39, 0x30, 34, 12, 0x43, 11, 26}},
|
||
{33, 12, {0x78, 0x56, 0x34, 0x12, 0, 0x39, 0x30, 34, 12, 0x43, 11, 26}},
|
||
{34, 10, {0x85, 0xff, 0, 0x39, 0x30, 34, 12, 0x43, 11, 26}},
|
||
{35, 10, {0x85, 0xff, 0, 0x39, 0x30, 34, 12, 0x43, 11, 26}},
|
||
{36, 12, {0, 0, 0x80, 0x3f, 0, 0x39, 0x30, 34, 12, 0x43, 11, 26}}
|
||
};
|
||
unsigned i;
|
||
for (i = 0U; i < sizeof(cases) / sizeof(cases[0]); ++i) {
|
||
uint8_t expected[21] = {0, 1, 0x85, 0x52, 1, 0, 0xe9, 3, 0};
|
||
unsigned type = cases[i].type;
|
||
expected[0] = (uint8_t)type;
|
||
memcpy(expected + 9U, cases[i].payload, cases[i].length);
|
||
read_open(15000U);
|
||
read_send(0x85U, 1U, 1001U, 0x52U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_typed_point, &type) == IEC104_READ_EXECUTE_OK);
|
||
read_flush();
|
||
CHECK(rd.answered == 1U && rd.lookups == 1U);
|
||
read_expect(0U, expected, 9U + cases[i].length);
|
||
read_close();
|
||
}
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:已知点无效时仍是COT5/P-N=0数据应答,不能变为未知IOA、静默错误或换Type。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:SIQ、DIQ、QDS分别检查原状态/数值和BL/SB/NT/OV;带时标值交叉数据IV与时标IV四组合。
|
||
* 线上期望逐字节手写,不从待测对象或编码器复制品质和时间字段。
|
||
*/
|
||
static void test_read_unavailable_quality(void)
|
||
{
|
||
static const struct { uint8_t type_id, quality; int16_t value; unsigned length; uint8_t payload[3]; } cases[] = {
|
||
{1U, 0x70U, 1, 1U, {0xf1U}},
|
||
{3U, 0x70U, 2, 1U, {0xf2U}},
|
||
{11U, 0x71U, -123, 3U, {0x85U, 0xffU, 0xf1U}}
|
||
};
|
||
unsigned i, invalid, time_invalid;
|
||
for (i = 0U; i < sizeof(cases) / sizeof(cases[0]); ++i) {
|
||
ReadSnapshot snapshot = {cases[i].type_id, cases[i].quality, cases[i].value, false, false};
|
||
uint8_t expected[12] = {0, 1, 5, 0x62, 1, 0, 1, 2, 3};
|
||
expected[0] = cases[i].type_id;
|
||
memcpy(expected + 9U, cases[i].payload, cases[i].length);
|
||
read_open(15000U); read_send(5U, 1U, UINT32_C(0x030201), 0x62U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush();
|
||
CHECK(rd.answered == 1U && rd.submitted == 1U && rd.lookups == 1U && read_calls == 1U);
|
||
CHECK(rd.unknown_ioa == 0U && rd.lookup_errors == 0U && rd.invalid_responses == 0U &&
|
||
rd.negative_submitted == 0U && rd.pending_requests == 0U);
|
||
read_expect(0U, expected, 9U + cases[i].length);
|
||
read_close();
|
||
}
|
||
for (invalid = 0U; invalid < 2U; ++invalid) {
|
||
for (time_invalid = 0U; time_invalid < 2U; ++time_invalid) {
|
||
ReadSnapshot snapshot = {35U, 0x71U, -123, invalid == 0U, time_invalid != 0U};
|
||
uint8_t expected[] = {35, 1, 0x85, 0x63, 1, 0, 1, 2, 3,
|
||
0x85, 0xff, 0x71, 0x39, 0x30, 34, 12, 0x43, 11, 26};
|
||
expected[11] = invalid != 0U ? 0xf1U : 0x71U;
|
||
expected[14] = time_invalid != 0U ? 0xa2U : 34U;
|
||
read_open(15000U); read_send(0x85U, 1U, UINT32_C(0x030201), 0x63U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush();
|
||
CHECK(rd.answered == 1U && rd.submitted == 1U && rd.lookups == 1U && read_calls == 1U);
|
||
CHECK(rd.unknown_ioa == 0U && rd.lookup_errors == 0U && rd.invalid_responses == 0U &&
|
||
rd.negative_submitted == 0U && rd.pending_requests == 0U);
|
||
read_expect(0U, expected, sizeof(expected));
|
||
read_close();
|
||
}
|
||
}
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:同一个Type21点经历可用、不可用和恢复,不允许库或应用为了携带IV改成Type9。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:不可用的ERROR无帧且不计unknown_ioa;另一个不在表中的IOA才回47。
|
||
* 完整长度比较还能发现虚构品质字节;最终删除站点核对所有路径均归还引用。
|
||
*/
|
||
static void test_read_type21_unavailable(void)
|
||
{
|
||
ReadSnapshot snapshot = {21U, 0x71U, -123, true, false};
|
||
const uint8_t original[] = {21, 1, 5, 0x64, 1, 0, 1, 2, 3, 0x85, 0xff};
|
||
const uint8_t unknown[] = {102, 1, 0x6f, 0x64, 1, 0, 2, 2, 3};
|
||
const uint8_t recovered[] = {21, 1, 5, 0x64, 1, 0, 1, 2, 3, 0x41, 1};
|
||
const uint8_t *frames[64];
|
||
size_t lengths[64];
|
||
read_open(15000U); read_send(5U, 1U, UINT32_C(0x030201), 0x64U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush(); read_expect(0U, original, sizeof(original));
|
||
snapshot.available = false;
|
||
read_send(5U, 1U, UINT32_C(0x030201), 0x64U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush();
|
||
CHECK(rd.answered == 1U && rd.lookup_errors == 1U && rd.unknown_ioa == 0U &&
|
||
rd.negative_submitted == 0U && rd.pending_requests == 0U);
|
||
CHECK(read_collect(frames, lengths) == 1U);
|
||
read_send(5U, 1U, UINT32_C(0x030202), 0x64U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush(); read_expect(1U, unknown, sizeof(unknown));
|
||
CHECK(rd.answered == 1U && rd.lookup_errors == 1U && rd.unknown_ioa == 1U && rd.negative_submitted == 1U);
|
||
snapshot.available = true; snapshot.value = 321;
|
||
read_send(5U, 1U, UINT32_C(0x030201), 0x64U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush(); read_expect(2U, recovered, sizeof(recovered));
|
||
CHECK(rd.answered == 2U && rd.submitted == 3U && rd.lookup_errors == 1U && rd.unknown_ioa == 1U &&
|
||
rd.negative_submitted == 1U && rd.invalid_responses == 0U && rd.pending_requests == 0U);
|
||
CHECK(rd.lookups == 4U && read_calls == 4U && read_collect(frames, lengths) == 3U);
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &read_station) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Station_Is_Reusable(&read_station));
|
||
read_close(); ++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:UNKNOWN只产生47。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:ERROR和非法响应静默结束,且三者分别计数。
|
||
* 请求完成后删除仍注册的站点,应立即可复用,证明该路径已归还读引用。
|
||
*/
|
||
static void test_read_lookup_results(void)
|
||
{
|
||
unsigned mode;
|
||
for (mode = 1U; mode <= 23U; ++mode) {
|
||
if (mode >= 4U && mode < 10U) { continue; }
|
||
read_open(15000U); lookup_mode = mode;
|
||
read_send(5U, 1U, 1001U, 0x52U);
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
read_flush();
|
||
CHECK(rd.pending_requests == 0U && rd.lookups == 1U);
|
||
CHECK(rd.lookup_errors == (mode == 2U ? 1U : 0U));
|
||
CHECK(rd.invalid_responses == (mode == 3U || mode >= 10U ? 1U : 0U));
|
||
if (mode == 1U) {
|
||
const uint8_t expected[] = {102, 1, 0x6f, 0x52, 1, 0, 0xe9, 3, 0};
|
||
CHECK(rd.unknown_ioa == 1U && rd.negative_submitted == 1U && rd.rejected == 0U);
|
||
read_expect(0U, expected, sizeof(expected));
|
||
} else { CHECK(rd.submitted == 0U); }
|
||
CHECK(!IEC104_Station_Is_Reusable(&read_station));
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &read_station) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Station_Is_Reusable(&read_station));
|
||
read_close();
|
||
}
|
||
for (mode = 0U; mode < 6U; ++mode) {
|
||
static const unsigned types[] = {45U, 100U, 15U, 37U, 38U, 36U};
|
||
unsigned type = types[mode];
|
||
read_open(15000U); if (mode == 5U) { lookup_mode = 40U; }
|
||
read_send(5U, 1U, 1001U, 0U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_typed_point, &type) == IEC104_READ_EXECUTE_OK);
|
||
read_status(); CHECK(rd.invalid_responses == 1U && rd.submitted == 0U);
|
||
read_close();
|
||
}
|
||
++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/WAIT_TIMEOUT] 读等待用例:余量预检、提交WAIT和受理期限
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:22/23字节是“2字节HIGH头+21字节最大响应”的两侧边界。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:预检WAIT不取值;提交WAIT保留READ并在下次重取新值;UNKNOWN则转为REPLY,
|
||
* 即使提交WAIT也只重发47,不能再次调用lookup。
|
||
*/
|
||
static void test_read_waits(void)
|
||
{
|
||
const uint8_t expected[] = {1, 1, 5, 0x52, 1, 0, 0xe9, 3, 0, 0};
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0x52U);
|
||
room_override = 22;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_WAIT);
|
||
read_status(); CHECK(rd.lookups == 0U && rd.room_waits == 1U && rd.send_waiting && !rd.need_process);
|
||
room_override = 23; submit_fault = 1;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_WAIT);
|
||
read_status(); CHECK(rd.lookups == 1U && rd.waits == 1U && rd.execute_pending && rd.send_waiting);
|
||
CHECK(rd.pending_requests == 1U && !rd.need_process);
|
||
read_value = 0; submit_fault = 0; read_poll();
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
read_flush(); CHECK(rd.lookups == 2U && rd.answered == 1U);
|
||
read_expect(0U, expected, sizeof(expected));
|
||
read_close();
|
||
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U); lookup_mode = 1U; submit_fault = 1;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
read_status(); CHECK(!rd.execute_pending && rd.send_waiting && rd.unknown_ioa == 1U);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_point, &read_calls) == IEC104_READ_EXECUTE_EMPTY);
|
||
read_drain(); submit_fault = 0; read_flush();
|
||
CHECK(rd.lookups == 1U && rd.negative_submitted == 1U);
|
||
read_close();
|
||
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U); lookup_mode = 1U; submit_fault = 1;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
push(stop_act, sizeof(stop_act)); CHECK(IEC104_Slave_Poll(read_slave, 0U, NULL) == IEC104_TCP_OK);
|
||
read_status(); CHECK(rd.events_pending && rd.pending_requests == 1U && !rd.execute_pending);
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_EMPTY); /* REPLY wins over pending notifications. */
|
||
read_drain(); read_status(); CHECK(rd.cancelled == 1U && rd.lookups == 1U);
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:提交WAIT丢弃的是本次数据快照,不把IV对象转成持久错误回复。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:无效变有效、有效变无效均重取新值及新品质;仅最后一次快照交接,Type/IOA/OA保持。
|
||
* 此处只改变应用快照,不改变模块配置或请求,因此能发现缓存旧值/旧品质的错误。
|
||
*/
|
||
static void test_read_quality_wait_refresh(void)
|
||
{
|
||
unsigned direction;
|
||
for (direction = 0U; direction < 2U; ++direction) {
|
||
ReadSnapshot snapshot = {11U, 0x31U, -123, direction != 0U, false};
|
||
uint8_t expected[] = {11, 1, 5, 0x65, 1, 0, 1, 2, 3, 0xc8, 1, 0x50};
|
||
read_open(15000U); read_send(5U, 1U, UINT32_C(0x030201), 0x65U);
|
||
submit_fault = 1;
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_WAIT);
|
||
read_status();
|
||
CHECK(rd.lookups == 1U && read_calls == 1U && rd.answered == 0U && rd.submitted == 0U && rd.waits == 1U);
|
||
CHECK(rd.execute_pending && rd.send_waiting && rd.pending_requests == 1U && !rd.need_process);
|
||
snapshot.available = !snapshot.available; snapshot.value = 456; snapshot.quality = 0x50U;
|
||
expected[11] = direction == 0U ? 0x50U : 0xd0U;
|
||
submit_fault = 0; read_poll();
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_OK);
|
||
read_flush();
|
||
CHECK(rd.lookups == 2U && read_calls == 2U && rd.answered == 1U && rd.submitted == 1U && rd.waits == 1U);
|
||
CHECK(rd.unknown_ioa == 0U && rd.lookup_errors == 0U && rd.invalid_responses == 0U &&
|
||
rd.negative_submitted == 0U && rd.pending_requests == 0U);
|
||
read_expect(0U, expected, sizeof(expected));
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_snapshot_point, &snapshot) == IEC104_READ_EXECUTE_EMPTY);
|
||
CHECK(read_calls == 2U);
|
||
read_close();
|
||
}
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:模式0恰好到期、1时钟倒退、2回调内到期、3禁用期限、4提交期间达到期限、
|
||
* 5余量WAIT期间到期、6尚差1毫秒。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:尤其模式4已提交OK,不能事后改判超时。
|
||
*/
|
||
static void test_read_deadlines(void)
|
||
{
|
||
unsigned mode;
|
||
for (mode = 0U; mode < 7U; ++mode) {
|
||
read_open(mode == 3U ? 0U : 10U);
|
||
read_send(5U, 1U, 1001U, 0U);
|
||
if (mode == 0U) { fake.now += 10U; }
|
||
if (mode == 1U || mode == 3U) { --fake.now; }
|
||
if (mode == 3U) { lookup_mode = 6U; }
|
||
if (mode == 2U) { lookup_mode = 4U; }
|
||
if (mode == 4U) { submit_advance = 10U; }
|
||
if (mode == 5U) { room_override = 0; CHECK(read_execute() == IEC104_READ_EXECUTE_WAIT); fake.now += 10U; }
|
||
if (mode == 6U) { fake.now += 9U; }
|
||
CHECK(read_execute() == (mode == 0U || mode == 1U || mode == 5U ?
|
||
IEC104_READ_EXECUTE_EMPTY : IEC104_READ_EXECUTE_OK));
|
||
if (fake.now < 1000U) { fake.now = 1000U; }
|
||
read_flush();
|
||
CHECK(rd.pending_requests == 0U);
|
||
CHECK(rd.timeouts == (mode == 0U || mode == 1U || mode == 2U || mode == 5U ? 1U : 0U));
|
||
CHECK(rd.answered == (mode == 3U || mode == 4U || mode == 6U ? 1U : 0U));
|
||
read_close();
|
||
}
|
||
read_open(10U); submit_fault = 1; read_send(6U, 1U, 1001U, 0U);
|
||
fake.now += 10U; CHECK(IEC104_Read_Process(reader)); read_status();
|
||
CHECK(rd.pending_requests == 0U && rd.timeouts == 1U && rd.submitted == 0U);
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/LIFETIME] 读生命周期用例:会话、重入、故障及原站引用
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:同代次STOP/START也必须取消旧请求。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:未读通知先于lookup处理。
|
||
* 回调内推进Slave属于契约违约,这里仅验证能被事后检查发现的两种情况。
|
||
*/
|
||
static void test_read_session_and_reentry(void)
|
||
{
|
||
unsigned mode;
|
||
for (mode = 0U; mode < 5U; ++mode) {
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U);
|
||
if (mode == 0U) {
|
||
lookup_mode = 5U; CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
read_status(); CHECK(rd.answered == 1U);
|
||
} else if (mode <= 2U) {
|
||
lookup_mode = mode == 1U ? 30U : 31U;
|
||
CHECK(read_execute() == (mode == 1U ? IEC104_READ_EXECUTE_WAIT : IEC104_READ_EXECUTE_OK));
|
||
read_drain(); read_status(); CHECK(rd.answered == 0U && rd.pending_requests == 0U);
|
||
} else {
|
||
push(stop_act, sizeof(stop_act));
|
||
if (mode == 3U) { push(start_act, sizeof(start_act)); }
|
||
CHECK(IEC104_Slave_Poll(read_slave, 0U, NULL) == IEC104_TCP_OK);
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_WAIT);
|
||
CHECK(read_calls == 0U);
|
||
read_drain(); read_status(); CHECK(rd.pending_requests == 0U && rd.cancelled == 1U);
|
||
if (mode == 4U) { push(start_act, sizeof(start_act)); read_poll(); }
|
||
read_send(5U, 1U, 1001U, 0U); CHECK(read_execute() == IEC104_READ_EXECUTE_OK);
|
||
}
|
||
read_close();
|
||
}
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U);
|
||
{ uint64_t generation = rd.generation;
|
||
fake.recv_eof = 1U; read_poll(); fake.recv_eof = 0U;
|
||
read_status(); CHECK(rd.generation == 0U && rd.cancelled == 1U);
|
||
read_connect(); CHECK(rd.generation != generation); }
|
||
CHECK(IEC104_Read_Handle_Event(reader, &read_input) == IEC104_READ_EVENT_IGNORED);
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_EMPTY);
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:先删站、保留挂起READ,再分别走查询失败、发送失败、取消、到期与成功。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:每条结束路径都必须让原节点恢复可复用,正常STOP的ERROR则先等待通知。
|
||
*/
|
||
static void test_read_faults_and_refs(void)
|
||
{
|
||
unsigned mode;
|
||
for (mode = 0U; mode < 8U; ++mode) {
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U);
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &read_station) == IEC104_STATION_OK);
|
||
CHECK(!IEC104_Station_Is_Reusable(&read_station));
|
||
if (mode < 2U) {
|
||
query_fault = (int)mode + 1;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_ERROR);
|
||
} else if (mode < 5U) {
|
||
submit_fault = (int)mode;
|
||
CHECK(read_execute() == (mode == 4U ? IEC104_READ_EXECUTE_WAIT : IEC104_READ_EXECUTE_OK));
|
||
read_drain();
|
||
} else if (mode == 5U) { IEC104_Read_Cancel(reader); }
|
||
else if (mode == 6U) { fake.now += 15000U; CHECK(IEC104_Read_Process(reader)); }
|
||
else { CHECK(read_execute() == IEC104_READ_EXECUTE_OK); }
|
||
read_status(); CHECK(rd.pending_requests == 0U);
|
||
CHECK(IEC104_Station_Is_Reusable(&read_station));
|
||
read_close();
|
||
}
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:同CA先删后建:OA=1的旧请求必须读旧context的1,OA=2新请求才读新值0。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:CA相同而注册编号不同,可直接发现lookup错误地按CA重新查站。
|
||
*/
|
||
static void test_read_replaced_station(void)
|
||
{
|
||
IEC104_Station_t replacement;
|
||
int replacement_value = 0;
|
||
static const uint8_t old_value[] = {1, 1, 5, 1, 1, 0, 0xe9, 3, 0, 1};
|
||
static const uint8_t new_value[] = {1, 1, 5, 2, 1, 0, 0xe9, 3, 0, 0};
|
||
memset(&replacement, 0, sizeof(replacement));
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 1U);
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &read_station) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Slave_Add_Station(read_slave, &replacement, 1U, &replacement_value) == IEC104_STATION_OK);
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_OK); read_flush();
|
||
CHECK(IEC104_Station_Is_Reusable(&read_station)); read_expect(0U, old_value, sizeof(old_value));
|
||
read_send(5U, 1U, 1001U, 2U); CHECK(read_execute() == IEC104_READ_EXECUTE_OK); read_flush();
|
||
read_expect(1U, new_value, sizeof(new_value));
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &replacement) == IEC104_STATION_OK);
|
||
CHECK(IEC104_Station_Is_Reusable(&replacement));
|
||
read_close(); ++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/FIFO] 读队列用例:管线、真实HIGH余量和到期队首
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:k=1限制在途帧,交错合法读与错误COT。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:逐帧ACK后按OA检查同一读模块FIFO。
|
||
* 此断言不要求读应答与其他业务之间存在全局顺序。
|
||
*/
|
||
static void test_read_fifo_and_pipeline(void)
|
||
{
|
||
const uint8_t *frames[64]; size_t lengths[64];
|
||
unsigned i, count = IEC104_READ_TASK_CAPACITY;
|
||
read_window = 1U;
|
||
read_open(15000U);
|
||
for (i = 0U; i < count; ++i) {
|
||
uint8_t bytes[] = {102, 1, 5, 0, 1, 0, 0xe9, 3, 0};
|
||
bytes[3] = (uint8_t)(i + 1U);
|
||
if ((i & 1U) != 0U) { bytes[2] = 6U; }
|
||
read_queue(bytes, sizeof(bytes));
|
||
}
|
||
read_poll();
|
||
for (i = 0U; i < 32U; ++i) {
|
||
read_poll(); CHECK(IEC104_Read_Process(reader));
|
||
(void)read_execute();
|
||
if (sut->connection.link_state.vs != sut->connection.link_state.va) {
|
||
push_ack(sut->connection.link_state.vs);
|
||
}
|
||
}
|
||
read_flush(); CHECK(read_collect(frames, lengths) == count);
|
||
for (i = 0U; i < count; ++i) {
|
||
CHECK(frames[i][3] == i + 1U);
|
||
CHECK(frames[i][0] == ((i & 1U) ? 102U : 1U));
|
||
CHECK(frames[i][2] == ((i & 1U) ? 0x6dU : 5U));
|
||
}
|
||
read_close(); ++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:不使用余量钩子:阻塞真实发送并填满HIGH,确认拥塞时lookup一次也不执行。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:解堵前改变数据源,最终线上值应来自恢复后的首次取值。
|
||
*/
|
||
static void test_read_real_room(void)
|
||
{
|
||
uint8_t filler[249] = {200, 0, 3, 0, 1, 0};
|
||
const uint8_t expected[] = {1, 1, 5, 0x52, 1, 0, 0xe9, 3, 0, 0};
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0x52U);
|
||
fake.send_blocked = 1U;
|
||
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation, filler, sizeof(filler)) == IEC104_TCP_SUBMIT_OK);
|
||
CHECK(IEC104_TcpServer_Send_Response(sut, sut->generation, filler, sizeof(filler)) == IEC104_TCP_SUBMIT_OK);
|
||
read_drain();
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_WAIT);
|
||
read_status(); CHECK(rd.lookups == 0U && rd.send_waiting);
|
||
read_value = 0; fake.send_blocked = 0U;
|
||
read_flush(); CHECK(read_execute() == IEC104_READ_EXECUTE_OK); read_flush();
|
||
read_expect(0U, expected, sizeof(expected));
|
||
read_close(); ++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:到期清理可能令队列变空,也可能露出REPLY。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:两者都须返回EMPTY而非访问旧队首。
|
||
* 后半段将错误回复晚受理1毫秒,确保只有原READ到期,剩余45/46/47保持FIFO。
|
||
*/
|
||
static void test_read_expired_head_and_fifo(void)
|
||
{
|
||
unsigned i;
|
||
read_open(10U);
|
||
for (i = 0U; i < IEC104_READ_TASK_CAPACITY; ++i) { read_send(5U, 1U, 1001U, i); }
|
||
fake.now += 10U;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_EMPTY);
|
||
read_status(); CHECK(rd.timeouts == IEC104_READ_TASK_CAPACITY && rd.pending_requests == 0U);
|
||
read_close();
|
||
#if IEC104_READ_TASK_CAPACITY >= 4U
|
||
read_open(10U); read_send(5U, 1U, 1001U, 1U);
|
||
++fake.now; read_send(6U, 1U, 1001U, 2U);
|
||
read_send(5U, 99U, 1001U, 3U);
|
||
read_send(5U, 1U, 0U, 4U);
|
||
fake.now += 9U;
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_EMPTY); /* Expired READ exposes REPLY. */
|
||
read_flush(); CHECK(rd.timeouts == 1U && rd.negative_submitted == 3U);
|
||
{ const uint8_t *frames[64]; size_t lengths[64];
|
||
CHECK(read_collect(frames, lengths) == 3U);
|
||
for (i = 0U; i < 3U; ++i) { CHECK(frames[i][3] == i + 2U && frames[i][2] == 0x6dU + i); } }
|
||
read_close();
|
||
#endif
|
||
++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/VALIDATION] 读校验用例:结构优先级、45/46/47、满槽与期限
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:混合错误先验证结构:SQ/数量/长度/P-N异常均静默丢弃,即使还含未知CA等错误。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:结构合法时才依次检查COT、CA、IOA,并核对否定位及TEST/OA回显。
|
||
*/
|
||
static void test_read_validation(void)
|
||
{
|
||
static const uint8_t malformed[][13] = {
|
||
{102, 0, 5, 0, 1, 0, 0xe9, 3, 0},
|
||
{102, 0x81, 5, 0, 99, 0, 0xe9, 3, 0},
|
||
{102, 1, 0x46, 0, 1, 0, 0xe9, 3, 0},
|
||
{102, 1, 5, 0, 1, 0, 0, 0, 0, 0xaa},
|
||
{102, 2, 6, 0, 1, 0, 1, 0, 0, 2, 0, 0}
|
||
};
|
||
static const size_t lengths[] = {9, 9, 9, 10, 12};
|
||
static const uint8_t expected[][9] = {
|
||
{102, 1, 0xed, 0x52, 99, 0, 0, 0, 0},
|
||
{102, 1, 0x6e, 0x53, 99, 0, 0, 0, 0},
|
||
{102, 1, 0x6e, 0x54, 255, 255, 0xe9, 3, 0},
|
||
{102, 1, 0x6f, 0x55, 1, 0, 0, 0, 0}
|
||
};
|
||
unsigned i;
|
||
read_open(15000U);
|
||
for (i = 0U; i < sizeof(lengths) / sizeof(lengths[0]); ++i) {
|
||
read_queue(malformed[i], lengths[i]);
|
||
read_poll();
|
||
CHECK(read_handled == IEC104_READ_EVENT_HANDLED);
|
||
read_status();
|
||
CHECK(rd.rejected == i + 1U && rd.submitted == 0U && rd.pending_requests == 0U);
|
||
}
|
||
read_send(0x86U, 99U, 0U, 0x52U); read_flush();
|
||
read_send(5U, 99U, 0U, 0x53U); read_flush();
|
||
read_send(5U, 65535U, 1001U, 0x54U); read_flush();
|
||
read_send(5U, 1U, 0U, 0x55U); read_flush();
|
||
for (i = 0U; i < 4U; ++i) { read_expect(i, expected[i], 9U); }
|
||
CHECK(rd.negative_submitted == 4U && rd.rejected == 9U);
|
||
{ const uint8_t other[] = {106, 1, 6, 0, 1, 0, 0, 0, 0, 0, 0};
|
||
read_queue(other, sizeof(other)); read_poll(); CHECK(read_handled == IEC104_READ_EVENT_IGNORED); }
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/**
|
||
* 功能:验证以下场景与约束:满槽丢新不能覆盖已受理请求。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:Get_Status只报告到期,真正释放由Process完成。
|
||
* 清空后还要能重新受理,并由STOP取消,防止到期清理破坏后续生命周期。
|
||
*/
|
||
static void test_read_slots_and_expiry(void)
|
||
{
|
||
unsigned i;
|
||
read_open(10U);
|
||
for (i = 0U; i < IEC104_READ_TASK_CAPACITY; ++i) { read_send(5U, 1U, 1001U, i); }
|
||
CHECK(!IEC104_Read_Destroy(reader));
|
||
read_send(5U, 1U, 1002U, 0U);
|
||
read_status();
|
||
CHECK(rd.pending_requests == IEC104_READ_TASK_CAPACITY && rd.accepted == IEC104_READ_TASK_CAPACITY);
|
||
CHECK(rd.overloads == 1U && rd.dropped_requests == 1U);
|
||
fake.now += 10U;
|
||
read_status();
|
||
CHECK(rd.need_process && rd.timeout_remaining_ms == 0U);
|
||
CHECK(IEC104_Read_Process(reader));
|
||
read_status();
|
||
CHECK(rd.pending_requests == 0U && rd.timeouts == IEC104_READ_TASK_CAPACITY);
|
||
CHECK(IEC104_Read_Execute_Next(reader, read_point, &read_calls) == IEC104_READ_EXECUTE_EMPTY);
|
||
read_send(5U, 1U, 1001U, 0U);
|
||
push(stop_act, sizeof(stop_act)); read_poll();
|
||
read_status(); CHECK(!rd.active && rd.pending_requests == 0U && rd.cancelled == 1U);
|
||
push(start_act, sizeof(start_act)); read_poll();
|
||
read_status(); CHECK(rd.active);
|
||
read_close();
|
||
++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [READ/SERVICE_ERRORS] 服务查询故障:只读失败原子性、任务清理及站点引用归还
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:验证以下场景与约束:人为置in_poll使服务查询失败,覆盖Process、取值前、取值后三个窗口。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:无。
|
||
* 说明:状态输出应保持原字节;修改入口则取消任务、停用模块并归还已删除站点的引用。
|
||
*/
|
||
static void test_read_query_failure(void)
|
||
{
|
||
unsigned mode;
|
||
for (mode = 0U; mode < 3U; ++mode) {
|
||
IEC104_ReadStatus_t output, before;
|
||
read_open(15000U); read_send(5U, 1U, 1001U, 0U);
|
||
CHECK(IEC104_Slave_Remove_Station(read_slave, &read_station) == IEC104_STATION_OK);
|
||
memset(&output, 0x5a, sizeof(output)); before = output;
|
||
sut->in_poll = 1U;
|
||
CHECK(!IEC104_Read_Get_Status(reader, &output));
|
||
CHECK(memcmp(&output, &before, sizeof(output)) == 0);
|
||
if (mode == 0U) { CHECK(!IEC104_Read_Process(reader)); }
|
||
else {
|
||
if (mode == 2U) { sut->in_poll = 0U; lookup_mode = 32U; }
|
||
CHECK(read_execute() == IEC104_READ_EXECUTE_ERROR);
|
||
}
|
||
sut->in_poll = 0U;
|
||
read_status();
|
||
CHECK(!rd.active && rd.pending_requests == 0U && rd.cancelled == 1U);
|
||
CHECK(rd.lookups == (mode == 2U ? 1U : 0U) && rd.submitted == 0U);
|
||
CHECK(IEC104_Station_Is_Reusable(&read_station));
|
||
read_close();
|
||
}
|
||
++read_groups;
|
||
}
|
||
|
||
/* ============================================================================
|
||
* [ENTRY/MAIN] 测试入口:读命令各类用例登记
|
||
* ============================================================================ */
|
||
/**
|
||
* 功能:文件按接口与业务阶段归类,运行顺序仍固定为原回归顺序。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
|
||
* 说明:每组内部组合独立检查。
|
||
* 结构尺寸仅报告当前宿主ABI,不作为嵌入式目标的RAM测量结果。
|
||
*/
|
||
int main(void)
|
||
{
|
||
/* [RUN/VALIDATION] 配置、请求校验与任务容量 */
|
||
test_read_configuration();
|
||
test_read_validation();
|
||
test_read_slots_and_expiry();
|
||
/* [RUN/RESULTS] 应答类型和lookup结果 */
|
||
test_read_types();
|
||
test_read_unavailable_quality();
|
||
test_read_type21_unavailable();
|
||
test_read_lookup_results();
|
||
/* [RUN/WAIT_TIMEOUT] WAIT与受理期限 */
|
||
test_read_waits();
|
||
test_read_quality_wait_refresh();
|
||
test_read_deadlines();
|
||
/* [RUN/LIFETIME] 会话、重入及站点引用 */
|
||
test_read_session_and_reentry();
|
||
test_read_faults_and_refs();
|
||
test_read_replaced_station();
|
||
/* [RUN/FIFO] FIFO、真实容量与过期队首 */
|
||
test_read_fifo_and_pipeline();
|
||
test_read_real_room();
|
||
test_read_expired_head_and_fifo();
|
||
/* [RUN/FAULTS] 查询失败及实例池 */
|
||
test_read_query_failure();
|
||
test_read_instance_pool();
|
||
printf("Read: groups=%u TCP=%u tasks=%u request=%zu status=%zu OK\n", read_groups,
|
||
(unsigned)IEC104_TCP_SERVER_CAPACITY, (unsigned)IEC104_READ_TASK_CAPACITY,
|
||
sizeof(IEC104_ReadRequest_t), sizeof(IEC104_ReadStatus_t));
|
||
return 0;
|
||
}
|