363 lines
18 KiB
C
363 lines
18 KiB
C
/* Test-only observer around the unchanged Windows example.
|
|
* Real sockets, values, CLI and lifecycle remain those of the production example.
|
|
* Public status permits checking the earliest scan timer, not every private
|
|
* next_due field. The zero-timeout guard is for this bounded 24-point fixture;
|
|
* it is an iteration limit, not a delivery/latency guarantee.
|
|
*/
|
|
#include "IEC104_PeriodicScan.h"
|
|
#include "IEC104_Net.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
|
|
#ifndef PERIODIC_SCAN_SOAK_ZERO_LIMIT
|
|
#define PERIODIC_SCAN_SOAK_ZERO_LIMIT 4096U
|
|
#endif
|
|
|
|
/**
|
|
* 功能:[SOAK/STATUS] 在真实单调时钟的前后采样包围状态查询,核对计数守恒、游标、最早期限和轮次年龄。
|
|
* 输入参数:self——被测实例或连接(只读);status——状态快照。
|
|
* 输出参数:status——状态快照。
|
|
* 返回值:true 表示本次操作或核对成功;false 表示拒绝、失败或命中注入故障。
|
|
* 说明:只依据公开状态,不读取私有next_due;固定24点夹具的断言不推广为一般送达时限。
|
|
*/
|
|
static bool Soak_Get_Status(const IEC104_PeriodicScan_t *self, IEC104_PeriodicScanStatus_t *status);
|
|
/**
|
|
* 功能:[SOAK/POLL] 统计连续无进展的零等待轮数,超限报告观察器失败。
|
|
* 输入参数:self——被测实例或连接;timeout_ms——轮询等待上限(毫秒);error——底层错误码存储。
|
|
* 输出参数:error——底层错误码存储。
|
|
* 返回值:真实传输接口或故障注入产生的 TCP 操作结果。
|
|
* 说明:限额针对固定小点表的忙循环检测,并非协议调度份额或最大时延保证。
|
|
*/
|
|
static IEC104_TcpResult_t Soak_Poll(IEC104_Slave_t *self, uint32_t timeout_ms, IEC104_TcpError_t *error);
|
|
/**
|
|
* 功能:[SOAK/EVENTS] 原样分派真实通知后检查状态。
|
|
* 输入参数:self——被测实例或连接;event——业务或传输事件(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:事件分派结果,区分接手、忽略和错误。
|
|
* 说明:生命周期处理改变epoch,阻断跨网格的期限比较。
|
|
*/
|
|
static IEC104_PeriodicScanEventResult_t Soak_Handle_Event(IEC104_PeriodicScan_t *self,
|
|
const IEC104_SlaveEvent_t *event);
|
|
/**
|
|
* 功能:[SOAK/STEP] 先调用真实Step,再观察结果状态。
|
|
* 输入参数:self——被测实例或连接;lookup——周期扫描查值回调;context——业务回调的测试上下文。
|
|
* 输出参数:无。
|
|
* 返回值:扫描单步结果,保留进展、等待、空闲及错误状态。
|
|
* 说明:检查通过时保留原返回结果,观察失败时报告STEP_ERROR。
|
|
*/
|
|
static IEC104_PeriodicScanStepResult_t Soak_Step(IEC104_PeriodicScan_t *self,
|
|
IEC104_PeriodicScanLookup_t lookup, void *context);
|
|
/**
|
|
* 功能:[SOAK/CLEANUP] 销毁前保留最后一次状态检查,仍执行真实Destroy并使旧快照失效。
|
|
* 输入参数:self——被测实例或连接。
|
|
* 输出参数:无。
|
|
* 返回值:true 表示销毁前观察与真实销毁均成功;任一步失败返回 false。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static bool Soak_Destroy(IEC104_PeriodicScan_t *self);
|
|
|
|
/* 只重定向被包含示例中的调用;include后立即撤销宏,使观察器内部仍调用真实库函数,避免递归。
|
|
* 因而CLI默认值和应用行为始终跟随主示例,观察器只额外核对公开状态。 */
|
|
#define IEC104_PeriodicScan_Get_Status Soak_Get_Status
|
|
#define IEC104_PeriodicScan_Handle_Event Soak_Handle_Event
|
|
#define IEC104_PeriodicScan_Step Soak_Step
|
|
#define IEC104_PeriodicScan_Destroy Soak_Destroy
|
|
#define IEC104_Slave_Poll Soak_Poll
|
|
#define main PeriodicScan_Soak_Example_Main
|
|
#include "../examples/iec104_slave_example.c"
|
|
#undef main
|
|
#undef IEC104_Slave_Poll
|
|
#undef IEC104_PeriodicScan_Destroy
|
|
#undef IEC104_PeriodicScan_Step
|
|
#undef IEC104_PeriodicScan_Handle_Event
|
|
#undef IEC104_PeriodicScan_Get_Status
|
|
|
|
typedef struct {
|
|
IEC104_PeriodicScanStatus_t status;
|
|
IEC104_PeriodicScanGroupStatus_t groups[IEC104_PERIODIC_SCAN_MAX_GROUPS];
|
|
uint64_t before_ms, after_ms;
|
|
uint64_t epoch;
|
|
bool valid;
|
|
} SoakSnapshot;
|
|
|
|
static SoakSnapshot soak_previous;
|
|
static uint64_t soak_checks, soak_polls, soak_epoch, soak_progress, soak_poll_progress;
|
|
static unsigned soak_zero_streak, soak_max_zero_streak;
|
|
static bool soak_failed, soak_poll_seen;
|
|
|
|
/**
|
|
* 功能:[SOAK/ASSERT] 只记录首个失败诊断并请求示例退出。
|
|
* 输入参数:condition——需要成立的观察条件;description——失败诊断描述(只读);group——扫描组或观察组编号。
|
|
* 输出参数:无。
|
|
* 返回值:原样返回 condition;false 同时登记观察器失败。
|
|
* 说明:之后仍累计检查次数,最终退出码由main统一决定。
|
|
*/
|
|
static bool Soak_Require(bool condition, const char *description, unsigned group)
|
|
{
|
|
++soak_checks;
|
|
if (!condition) {
|
|
if (!soak_failed) {
|
|
fprintf(stderr, "PeriodicScan soak invariants: FAIL group=%u check=%s\n", group, description);
|
|
}
|
|
soak_failed = true;
|
|
example_stop = 1; /* Also stop promptly if this happens during final cleanup. */
|
|
}
|
|
return condition;
|
|
}
|
|
|
|
/**
|
|
* 功能:定时器剩余时间按下界0递减,避免跨采样间隔时无符号下溢。
|
|
* 输入参数:remaining——当前剩余时间(毫秒);elapsed——采样间隔(毫秒)。
|
|
* 输出参数:无。
|
|
* 返回值:扣除经过时间后的剩余毫秒数,下限为 0。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static uint32_t Soak_Subtract(uint32_t remaining, uint64_t elapsed)
|
|
{
|
|
return elapsed >= remaining ? 0U : (uint32_t)(remaining - elapsed);
|
|
}
|
|
|
|
/**
|
|
* 功能:轮次年龄按UINT32_MAX饱和,匹配公开状态的耗时表示范围。
|
|
* 输入参数:age——当前轮次年龄(毫秒);elapsed——采样间隔(毫秒)。
|
|
* 输出参数:无。
|
|
* 返回值:增加经过时间后的轮次年龄,在 UINT32_MAX 饱和。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static uint32_t Soak_Add(uint32_t age, uint64_t elapsed)
|
|
{
|
|
return elapsed >= UINT32_MAX - age ? UINT32_MAX : age + (uint32_t)elapsed;
|
|
}
|
|
|
|
/**
|
|
* 功能:只在代次、生命周期epoch和轮次配置均未变化时比较相邻期限,避免跨重启/开轮误报。
|
|
* 输入参数:a——前一次扫描状态快照(只读);b——后一次扫描状态快照(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:true 表示两次快照属于可比较的同一网格;否则 false。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static bool Soak_Same_Grid(const SoakSnapshot *a, const SoakSnapshot *b)
|
|
{
|
|
unsigned g;
|
|
if (!a->valid || a->epoch != b->epoch || a->status.generation != b->status.generation ||
|
|
a->status.active != b->status.active || a->status.reserve_invalid != b->status.reserve_invalid ||
|
|
a->status.group_count != b->status.group_count) { return false; }
|
|
for (g = 0U; g < b->status.group_count; ++g) {
|
|
const IEC104_PeriodicScanGroupStatus_t *x = &a->groups[g], *y = &b->groups[g];
|
|
if (x->cycles_started != y->cycles_started || x->cycles_completed != y->cycles_completed ||
|
|
x->cycles_aborted != y->cycles_aborted || x->enabled != y->enabled ||
|
|
x->in_progress != y->in_progress || x->period_ms != y->period_ms) { return false; }
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 功能:以游标、交接/跳过计数或网格状态变化判断进展。
|
|
* 输入参数:a——前一次扫描状态快照(只读);b——后一次扫描状态快照(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:true 表示两次快照间出现状态推进;false 表示未发现进展。
|
|
* 说明:仅时间增长、重复lookup或WAIT不算进展。
|
|
*/
|
|
static bool Soak_Made_Progress(const SoakSnapshot *a, const SoakSnapshot *b)
|
|
{
|
|
unsigned g;
|
|
if (!Soak_Same_Grid(a, b)) { return true; }
|
|
for (g = 0U; g < b->status.group_count; ++g) {
|
|
const IEC104_PeriodicScanGroupStatus_t *x = &a->groups[g], *y = &b->groups[g];
|
|
if (x->cursor != y->cursor || x->asdus_sent != y->asdus_sent ||
|
|
x->objects_sent != y->objects_sent || x->objects_skipped != y->objects_skipped ||
|
|
x->unregistered_skipped != y->unregistered_skipped) { return true; }
|
|
}
|
|
/* Repeated WAIT, retries/lookups and the clock itself are not progress. */
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 功能:[SOAK/STATUS] 在真实单调时钟的前后采样包围状态查询,核对计数守恒、游标、最早期限和轮次年龄。
|
|
* 输入参数:self——被测实例或连接(只读);status——状态快照。
|
|
* 输出参数:status——状态快照。
|
|
* 返回值:true 表示本次操作或核对成功;false 表示拒绝、失败或命中注入故障。
|
|
* 说明:只依据公开状态,不读取私有next_due;固定24点夹具的断言不推广为一般送达时限。
|
|
*/
|
|
static bool Soak_Get_Status(const IEC104_PeriodicScan_t *self, IEC104_PeriodicScanStatus_t *status)
|
|
{
|
|
SoakSnapshot current;
|
|
uint64_t elapsed_min, elapsed_max;
|
|
unsigned g;
|
|
memset(¤t, 0, sizeof(current));
|
|
current.before_ms = IEC104_Net_Now_Ms();
|
|
if (!Soak_Require(IEC104_PeriodicScan_Get_Status(self, ¤t.status), "Get_Status", 0U)) {
|
|
return false;
|
|
}
|
|
if (!Soak_Require(current.status.group_count <= IEC104_PERIODIC_SCAN_MAX_GROUPS,
|
|
"group_count", 0U)) { return false; }
|
|
for (g = 0U; g < current.status.group_count; ++g) {
|
|
IEC104_PeriodicScanGroupStatus_t *s = ¤t.groups[g];
|
|
if (!Soak_Require(IEC104_PeriodicScan_Get_Group_Status(self, (uint8_t)g, s),
|
|
"Get_Group_Status", g)) { return false; }
|
|
(void)Soak_Require(s->cycles_started == s->cycles_completed + s->cycles_aborted +
|
|
(s->in_progress ? 1UL : 0UL), "cycle conservation", g);
|
|
(void)Soak_Require(s->asdus_sent <= s->objects_sent &&
|
|
(uint64_t)s->objects_sent <= (uint64_t)s->asdus_sent * 127U,
|
|
"ASDU/object counts", g);
|
|
(void)Soak_Require((uint64_t)s->objects_sent + s->objects_skipped <= s->lookups,
|
|
"committed objects/lookups", g);
|
|
(void)Soak_Require(s->sequence_frames <= s->asdus_sent, "sequence count", g);
|
|
(void)Soak_Require(s->last_cycle_ms <= s->max_cycle_ms, "completed cycle time", g);
|
|
(void)Soak_Require(s->enabled == (s->period_ms != 0U) && (s->enabled || !s->in_progress),
|
|
"enabled state", g);
|
|
(void)Soak_Require(s->in_progress ? s->cursor < EXAMPLE_SCAN_POINT_COUNT :
|
|
s->cursor == EXAMPLE_SCAN_POINT_COUNT, "cursor extent", g);
|
|
(void)Soak_Require(s->in_progress || s->cycle_age_ms == 0U, "inactive cycle age", g);
|
|
/* This fixture's source always supplies registered, valid values. */
|
|
(void)Soak_Require(s->lookup_errors == 0UL && s->invalid_responses == 0UL &&
|
|
s->submit_errors == 0UL && s->unregistered_skipped == 0UL,
|
|
"fixture error counters", g);
|
|
}
|
|
current.after_ms = IEC104_Net_Now_Ms();
|
|
current.epoch = soak_epoch;
|
|
current.valid = true;
|
|
(void)Soak_Require(current.after_ms >= current.before_ms, "monotonic sample", 0U);
|
|
(void)Soak_Require(current.status.active || (!current.status.timer_active &&
|
|
!current.status.need_process && !current.status.send_ready), "inactive scheduling", 0U);
|
|
(void)Soak_Require(!current.status.reserve_invalid && current.status.reserve_rejections == 0UL,
|
|
"fixture reserve", 0U);
|
|
(void)Soak_Require(!current.status.events_pending || !current.status.need_process,
|
|
"notification scheduling", 0U);
|
|
if (soak_previous.valid) {
|
|
(void)Soak_Require(current.before_ms >= soak_previous.after_ms, "monotonic observations", 0U);
|
|
}
|
|
if (Soak_Same_Grid(&soak_previous, ¤t)) {
|
|
/* 两次状态查询各被前后时刻包围;真实采样间隔位于这两个界限之间,
|
|
* 不能直接用某一个外部时间差判等,否则调度抢占会被误报为期限漂移。 */
|
|
elapsed_min = current.before_ms - soak_previous.after_ms;
|
|
elapsed_max = current.after_ms - soak_previous.before_ms;
|
|
(void)Soak_Require(current.status.timer_active == soak_previous.status.timer_active,
|
|
"stable timer presence", 0U);
|
|
if (current.status.timer_active && current.status.timeout_remaining_ms != UINT32_MAX &&
|
|
soak_previous.status.timeout_remaining_ms != UINT32_MAX) {
|
|
(void)Soak_Require(current.status.timeout_remaining_ms >=
|
|
Soak_Subtract(soak_previous.status.timeout_remaining_ms, elapsed_max) &&
|
|
current.status.timeout_remaining_ms <=
|
|
Soak_Subtract(soak_previous.status.timeout_remaining_ms, elapsed_min),
|
|
"stable earliest deadline", 0U);
|
|
}
|
|
for (g = 0U; g < current.status.group_count; ++g) {
|
|
if (current.groups[g].in_progress) {
|
|
(void)Soak_Require(current.groups[g].cycle_age_ms >=
|
|
Soak_Add(soak_previous.groups[g].cycle_age_ms, elapsed_min) &&
|
|
current.groups[g].cycle_age_ms <=
|
|
Soak_Add(soak_previous.groups[g].cycle_age_ms, elapsed_max),
|
|
"cycle age/monotonic time", g);
|
|
}
|
|
}
|
|
}
|
|
if (Soak_Made_Progress(&soak_previous, ¤t)) { ++soak_progress; }
|
|
soak_previous = current;
|
|
*status = current.status;
|
|
return !soak_failed;
|
|
}
|
|
|
|
/**
|
|
* 功能:[SOAK/EVENTS] 原样分派真实通知后检查状态。
|
|
* 输入参数:self——被测实例或连接;event——业务或传输事件(只读)。
|
|
* 输出参数:无。
|
|
* 返回值:事件分派结果,区分接手、忽略和错误。
|
|
* 说明:生命周期处理改变epoch,阻断跨网格的期限比较。
|
|
*/
|
|
static IEC104_PeriodicScanEventResult_t Soak_Handle_Event(IEC104_PeriodicScan_t *self,
|
|
const IEC104_SlaveEvent_t *event)
|
|
{
|
|
IEC104_PeriodicScanEventResult_t result = IEC104_PeriodicScan_Handle_Event(self, event);
|
|
IEC104_PeriodicScanStatus_t status;
|
|
++soak_progress; /* Consuming a real notification is application progress. */
|
|
if (result == IEC104_PERIODIC_SCAN_EVENT_HANDLED) { ++soak_epoch; }
|
|
if (result == IEC104_PERIODIC_SCAN_EVENT_ERROR || !Soak_Get_Status(self, &status)) {
|
|
(void)Soak_Require(false, "Handle_Event observer", 0U);
|
|
return IEC104_PERIODIC_SCAN_EVENT_ERROR;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 功能:[SOAK/STEP] 先调用真实Step,再观察结果状态。
|
|
* 输入参数:self——被测实例或连接;lookup——周期扫描查值回调;context——业务回调的测试上下文。
|
|
* 输出参数:无。
|
|
* 返回值:扫描单步结果,保留进展、等待、空闲及错误状态。
|
|
* 说明:检查通过时保留原返回结果,观察失败时报告STEP_ERROR。
|
|
*/
|
|
static IEC104_PeriodicScanStepResult_t Soak_Step(IEC104_PeriodicScan_t *self,
|
|
IEC104_PeriodicScanLookup_t lookup, void *context)
|
|
{
|
|
IEC104_PeriodicScanStepResult_t result = IEC104_PeriodicScan_Step(self, lookup, context);
|
|
IEC104_PeriodicScanStatus_t status;
|
|
if (!Soak_Get_Status(self, &status)) { return IEC104_PERIODIC_SCAN_STEP_ERROR; }
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 功能:[SOAK/POLL] 统计连续无进展的零等待轮数,超限报告观察器失败。
|
|
* 输入参数:self——被测实例或连接;timeout_ms——轮询等待上限(毫秒);error——底层错误码存储。
|
|
* 输出参数:error——底层错误码存储。
|
|
* 返回值:真实传输接口或故障注入产生的 TCP 操作结果。
|
|
* 说明:限额针对固定小点表的忙循环检测,并非协议调度份额或最大时延保证。
|
|
*/
|
|
static IEC104_TcpResult_t Soak_Poll(IEC104_Slave_t *self, uint32_t timeout_ms, IEC104_TcpError_t *error)
|
|
{
|
|
++soak_polls;
|
|
if (timeout_ms == 0U && soak_poll_seen && soak_poll_progress == soak_progress) {
|
|
++soak_zero_streak;
|
|
if (soak_zero_streak > soak_max_zero_streak) { soak_max_zero_streak = soak_zero_streak; }
|
|
(void)Soak_Require(soak_zero_streak <= PERIODIC_SCAN_SOAK_ZERO_LIMIT,
|
|
"zero-timeout iterations without progress", 0U);
|
|
} else { soak_zero_streak = 0U; }
|
|
soak_poll_progress = soak_progress;
|
|
soak_poll_seen = true;
|
|
if (soak_failed) {
|
|
if (error != NULL) {
|
|
error->result = IEC104_TCP_INTERNAL_ERROR;
|
|
error->stage = IEC104_TCP_STAGE_SCHEDULE;
|
|
error->socket_error = 0;
|
|
}
|
|
return IEC104_TCP_INTERNAL_ERROR;
|
|
}
|
|
return IEC104_Slave_Poll(self, timeout_ms, error);
|
|
}
|
|
|
|
/**
|
|
* 功能:[SOAK/CLEANUP] 销毁前保留最后一次状态检查,仍执行真实Destroy并使旧快照失效。
|
|
* 输入参数:self——被测实例或连接。
|
|
* 输出参数:无。
|
|
* 返回值:true 表示销毁前观察与真实销毁均成功;任一步失败返回 false。
|
|
* 说明:本辅助入口用于构造或核对上述测试场景,相关状态保存在当前测试夹具中。
|
|
*/
|
|
static bool Soak_Destroy(IEC104_PeriodicScan_t *self)
|
|
{
|
|
IEC104_PeriodicScanStatus_t status;
|
|
bool checked = Soak_Get_Status(self, &status);
|
|
bool destroyed = IEC104_PeriodicScan_Destroy(self);
|
|
soak_previous.valid = false;
|
|
return checked && destroyed;
|
|
}
|
|
|
|
/**
|
|
* 功能:[ENTRY/MAIN] 运行原示例入口。
|
|
* 输入参数:argc——命令行参数数量;argv——命令行参数数组。
|
|
* 输出参数:无。
|
|
* 返回值:正常完成返回 0;参数、运行或断言失败以非零状态结束进程。
|
|
* 说明:只有真实执行过状态检查及Poll且无失败时才输出观察器PASS。
|
|
*/
|
|
int main(int argc, char **argv)
|
|
{
|
|
int result = PeriodicScan_Soak_Example_Main(argc, argv);
|
|
if (soak_failed) { return EXIT_FAILURE; }
|
|
if (result == EXIT_SUCCESS && soak_checks != 0U && soak_polls != 0U) {
|
|
printf("PeriodicScan soak invariants: PASS checks=%llu polls=%llu max_zero_no_progress=%u limit=%u\n",
|
|
(unsigned long long)soak_checks, (unsigned long long)soak_polls,
|
|
soak_max_zero_streak, (unsigned)PERIODIC_SCAN_SOAK_ZERO_LIMIT);
|
|
}
|
|
return result;
|
|
}
|