1797 lines
86 KiB
C
1797 lines
86 KiB
C
/*
|
||
* IEC104配电自动化远程参数:200切区、201读区、202读参数、203预置/固化/撤销。
|
||
* 模块只管理线格式、固定FIFO和协议会话;运行值、暂存及业务原子性由应用负责。
|
||
* Handle_Event保存请求,Process推进期限/固定回复,Execute_Next同步调用应用。
|
||
* 三个入口与Slave事件循环串行使用,不建线程、不用堆、不访问硬件或文件系统。
|
||
*
|
||
* 必须保持的边界:
|
||
* 1. 请求原文和原Slave身份不变;回复字段另外冻结,WAIT不会重复已执行的动作。
|
||
* 2. 正常202帧WAIT后重新取值,固定否定末帧只重组字节,不再调用lookup。
|
||
* 3. 活动会话、恢复保护和待交付DISCARD独立于请求槽,清理结束才释放原站引用。
|
||
* 4. faulted粘滞到销毁,仅关闭记录的原连接一次;不能借新连接继续旧请求。
|
||
* 公开借用契约见同名头文件,完整状态规则见docs/104/IEC104_RemoteParameter_设计架构.md。
|
||
*/
|
||
#include "IEC104_RemoteParameter.h"
|
||
#include "IEC104_TcpServer_Config.h"
|
||
#include "IEC104_Net.h"
|
||
#include <float.h>
|
||
#include <limits.h>
|
||
#include <string.h>
|
||
|
||
/* 容量仅影响静态存储;上限还须保证Process的2*N+2预算计算不溢出。 */
|
||
#ifndef IEC104_REMOTE_PARAMETER_CAPACITY
|
||
#define IEC104_REMOTE_PARAMETER_CAPACITY IEC104_TCP_SERVER_CAPACITY
|
||
#endif
|
||
#ifndef IEC104_REMOTE_PARAMETER_TASK_CAPACITY
|
||
#define IEC104_REMOTE_PARAMETER_TASK_CAPACITY 2U
|
||
#endif
|
||
typedef char RemoteParameter_Capacity_Check[(IEC104_REMOTE_PARAMETER_CAPACITY > 0U &&
|
||
IEC104_REMOTE_PARAMETER_CAPACITY <= UINT_MAX && IEC104_REMOTE_PARAMETER_TASK_CAPACITY > 0U &&
|
||
IEC104_REMOTE_PARAMETER_TASK_CAPACITY <= (UINT_MAX - 2U) / 2U) ? 1 : -1];
|
||
|
||
/**
|
||
* 功能:测试替身只接管外部查询/发送/关闭/时钟,生产编译直接调用真实Slave和Net。
|
||
* 输入参数:self/identity——从站服务及请求身份;asdu——待提交的应答ASDU。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:OK已复制接手;WAIT未接手;ERROR为身份或ASDU非法;FATAL为连接故障。
|
||
* 说明:可直接交接或进入HIGH队列;本入口不返回票据,成功不表示对端已确认。
|
||
*/
|
||
#ifdef IEC104_REMOTE_PARAMETER_TEST_HOOKS
|
||
extern IEC104_TcpSubmitResult_t IEC104_RemoteParameter_Test_Send_Response(IEC104_Slave_t *,
|
||
const IEC104_SlaveEventIdentity_t *, const IEC60870_Asdu_t *);
|
||
/**
|
||
* 功能:统一判断业务是否应先处理通知、放弃失效请求,或继续当前会话的工作。
|
||
* 输入参数:identity为Get_Event_Identity的只读结果,允许零初始化表示尚未绑定会话。
|
||
* 输出参数:无。
|
||
* 返回值:ERROR为参数/查询失败;WAIT_EVENTS优先于会话判断;STALE为原身份不再可用;READY可继续。
|
||
* 说明:检查通知FIFO、服务实例、TCP代次和传输激活状态,不执行I/O或保留发送窗口。
|
||
* 先读空通知可避免同一代次STOP后又START时,仅凭最终激活状态误续旧业务。
|
||
*/
|
||
extern IEC104_SlaveRequestState_t IEC104_RemoteParameter_Test_Check_Request(const IEC104_Slave_t *,
|
||
const IEC104_SlaveEventIdentity_t *);
|
||
/**
|
||
* 功能:取得内部TCP服务、队列和最近会话的状态副本,不推进协议或消费队列。
|
||
* 输入参数:self——有效句柄;status——非NULL输出存储;error——可选诊断。
|
||
* 输出参数:status——状态快照的更新结果。
|
||
* error——错误诊断的更新结果。
|
||
* 返回值:OK写入status,失败保持输出;Create后、Init失败后和Close后均可查询。
|
||
* 说明:会话和队列各自有有效标志,connected与data_transfer_started含义不同。
|
||
*/
|
||
extern IEC104_TcpResult_t IEC104_RemoteParameter_Test_Get_Status(const IEC104_Slave_t *,
|
||
IEC104_TcpServerStatus_t *, IEC104_TcpError_t *);
|
||
/**
|
||
* 功能:按事件捕获的原连接身份主动断开主站,保留监听和服务级NORMAL。
|
||
* 输入参数:self——所属Slave;event——本服务事件或Copy_Event副本;error——可选诊断。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* error——错误诊断的更新结果。
|
||
* 返回值:OK已断开;其他服务/已复用Slave的事件为INVALID_ARGUMENT;当前无连接为NOT_CONNECTED;
|
||
* 仍有连接但代次不同为STALE_GENERATION;底层服务异常通过原结果和error返回。
|
||
* 说明:来源serial先由本层检查,原generation再由TcpServer核对;不用当前代次代替旧身份。
|
||
* 不等待STOPDT或ACK,成功后继续读取收尾事件,再Prepare更新监听条件。
|
||
*/
|
||
extern IEC104_TcpResult_t IEC104_RemoteParameter_Test_Close_Connection(IEC104_Slave_t *,
|
||
const IEC104_SlaveEventIdentity_t *, IEC104_TcpError_t *);
|
||
/**
|
||
* 功能:读取系统启动以来的64位毫秒计数;供相对期限计算,不受用户修改日历时间影响。
|
||
* 输入参数:无。
|
||
* 输出参数:无。
|
||
* 返回值:64位单调毫秒计数,不是UTC时间戳,也不用于显示日期;0及连续读取相等值有效。
|
||
* 说明:不在此处换算为UTC或本地时间,避免将协议超时与日历时钟混用。
|
||
*/
|
||
extern uint64_t IEC104_RemoteParameter_Test_Now_Ms(void);
|
||
#define RP_SEND IEC104_RemoteParameter_Test_Send_Response
|
||
#define RP_CHECK IEC104_RemoteParameter_Test_Check_Request
|
||
#define RP_STATUS IEC104_RemoteParameter_Test_Get_Status
|
||
#define RP_CLOSE IEC104_RemoteParameter_Test_Close_Connection
|
||
#define RP_NOW IEC104_RemoteParameter_Test_Now_Ms
|
||
#else
|
||
/* 扩展参数按命令应答级处理;读参数各段及错误末帧同级FIFO,CONT末帧不能越过前段。 */
|
||
#define RP_SEND(s, i, a) IEC104_Slave_Send_Response_Priority(s, i, a, IEC104_BUSINESS_PRIORITY_COMMAND)
|
||
#define RP_CHECK IEC104_Slave_Check_Request
|
||
#define RP_STATUS IEC104_Slave_Get_Status
|
||
#define RP_CLOSE IEC104_Slave_Close_Connection_By_Identity
|
||
#define RP_NOW IEC104_Net_Now_Ms
|
||
#endif
|
||
|
||
/* 任务只分待执行与固定回复;202是否已开始流式读取由read_started另行记录。
|
||
* 预置会话的三态不等于任务阶段:固化已结束会话时,其确认仍可占槽等待发送。 */
|
||
enum { RP_EXECUTE = 1, RP_REPLY = 2 };
|
||
enum { RP_MIRROR = 0, RP_AREA = 1, RP_READ_ABORT = 2, RP_COMPACT = 3 };
|
||
enum { RP_NONE = 0, RP_COLLECTING = 1, RP_COMPLETE = 2 };
|
||
typedef struct {
|
||
IEC104_SlaveEventIdentity_t identity; /* 原Slave/实例/代次,查询、发送、关闭原样传回。 */
|
||
IEC104_Station_t *station; /* 本槽持有的引用;转交恢复记录后置NULL。 */
|
||
uint64_t station_id, receive_serial, accepted_ms, reply_ms, read_ms;
|
||
size_t cursor; /* 仅正常202帧提交OK后推进的持久下标。 */
|
||
IEC104_RemoteParameterArea_t area; /* 201回调结果冻结,确认WAIT不重读区号。 */
|
||
uint8_t bytes[249]; /* 原ASDU不可改写,含错误或截短请求原文。 */
|
||
uint16_t length;
|
||
uint8_t phase, reply_kind, cause;
|
||
bool occupied, negative, checked_failure, reply_started, send_waiting;
|
||
/* checked_failure防止错误回复重试时再次使批次失效;read_aborted防止重复计数。
|
||
* validated区分已受理的202任务与无状态拒绝;cancel_pi只用于合法撤销结构。 */
|
||
bool validated, read_started, read_aborted, prechecked, cancel_pi;
|
||
} RemoteTask;
|
||
|
||
/* 此记录独立持有原站引用,不随某一帧确认出队而释放。
|
||
* 活动会话与recovery不并存;recovery与discard可同时存在。
|
||
* 解除恢复保护不等于完成应用清理,二者都结束后才归还引用。 */
|
||
typedef struct {
|
||
IEC104_RemoteParameterRequest_t source; /* 首帧来源的纯值副本,不借用元素区。 */
|
||
IEC104_Station_t *station;
|
||
uint64_t success_ms;
|
||
uint8_t phase;
|
||
bool recovery, discard;
|
||
} RemotePreset;
|
||
|
||
struct IEC104_RemoteParameterFrame {
|
||
uint16_t length; /* 本帧累计字节数,包含9字节202头。 */
|
||
uint8_t count, attempts; /* count跨lookup累加,attempts每次lookup重新计数。 */
|
||
uint32_t expected_ioa;
|
||
IEC104_RemoteParameterAddResult_t last_add;
|
||
bool borrowed, match_ioa, violation; /* violation一旦置位,本次lookup不能恢复成功。 */
|
||
};
|
||
|
||
struct IEC104_RemoteParameter {
|
||
IEC104_Slave_t *slave;
|
||
IEC104_SlaveEventIdentity_t session, fault_identity;
|
||
IEC104_RemoteParameterConfig_t config;
|
||
IEC104_RemoteParameterStatus_t status;
|
||
RemoteTask tasks[IEC104_REMOTE_PARAMETER_TASK_CAPACITY];
|
||
unsigned order[IEC104_REMOTE_PARAMETER_TASK_CAPACITY], count; /* 槽号FIFO,移序不搬动原文。 */
|
||
RemotePreset preset;
|
||
IEC104_RemoteParameterFrame_t frame;
|
||
const IEC104_RemoteParameterRequest_t *borrowed_request; /* 只在当前应用回调内非NULL。 */
|
||
uint64_t instance_id; /* 本模块编号,不能替代session中的Slave编号。 */
|
||
uint8_t buffer[249]; /* 预检、读取及固定回复共用;不跨WAIT保存点值。 */
|
||
bool in_use, in_callback, close_attempted, fault_ended;
|
||
};
|
||
typedef char RemoteParameter_Storage_Check[(
|
||
IEC104_REMOTE_PARAMETER_CAPACITY <= SIZE_MAX / sizeof(IEC104_RemoteParameter_t) &&
|
||
IEC104_REMOTE_PARAMETER_TASK_CAPACITY <= SIZE_MAX / sizeof(RemoteTask)) ? 1 : -1];
|
||
static IEC104_RemoteParameter_t remote_pool[IEC104_REMOTE_PARAMETER_CAPACITY];
|
||
static uint64_t next_instance_id;
|
||
|
||
/**
|
||
* 功能:先比较池内地址再解引用,NULL及不属于本池的句柄不会被当作结构体访问。
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:无。
|
||
* 返回值:true表示句柄属于当前占用的静态实例槽;NULL、池外地址或未占用槽返回false。
|
||
* 说明:直接处理传入的self,不经过其他函数调用。
|
||
*/
|
||
static bool Owned(const IEC104_RemoteParameter_t *self)
|
||
{
|
||
unsigned i;
|
||
for (i = 0U; i < IEC104_REMOTE_PARAMETER_CAPACITY; ++i) {
|
||
if (self == &remote_pool[i]) { return self->in_use; }
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 功能:按字节拼装小端字段,避免未对齐访问;
|
||
* 输入参数:p——const uint8_t *所指数据。
|
||
* 输出参数:无。
|
||
* 返回值:返回(uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8))的结果。
|
||
* 说明:调用方负责先证明剩余长度足够。
|
||
*/
|
||
static uint16_t Read16(const uint8_t *p)
|
||
{
|
||
return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));
|
||
}
|
||
|
||
/**
|
||
* 功能:从已确认完整的3字节小端字段读取IOA;
|
||
* 输入参数:p——const uint8_t *所指数据。
|
||
* 输出参数:无。
|
||
* 返回值:返回(uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16)的结果。
|
||
* 说明:不依赖地址对齐,不自行检查剩余长度。
|
||
*/
|
||
static uint32_t Read24(const uint8_t *p)
|
||
{
|
||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16);
|
||
}
|
||
|
||
/**
|
||
* 功能:写入2字节小端字段;
|
||
* 输入参数:p——uint8_t *所指数据。
|
||
* value——业务值。
|
||
* 输出参数:p——uint8_t *所指数据的更新结果。
|
||
* 返回值:无。
|
||
* 说明:调用方保证目标容量,不能直接用本机整数内存替代线格式。
|
||
*/
|
||
static void Write16(uint8_t *p, uint16_t value)
|
||
{
|
||
p[0] = (uint8_t)value;
|
||
p[1] = (uint8_t)(value >> 8);
|
||
}
|
||
|
||
/**
|
||
* 功能:CA不能代表连接身份,同一Slave重连后generation不同即是另一条连接。
|
||
* 输入参数:a——const IEC104_SlaveEventIdentity_t *所指数据。
|
||
* b——const IEC104_SlaveEventIdentity_t *所指数据。
|
||
* 输出参数:无。
|
||
* 返回值:服务指针、实例编号和连接代次三者相同返回true,否则false。
|
||
* 说明:直接处理传入的a、b,不经过其他函数调用。
|
||
*/
|
||
static bool Same_Identity(const IEC104_SlaveEventIdentity_t *a, const IEC104_SlaveEventIdentity_t *b)
|
||
{
|
||
return a->slave == b->slave && a->instance_id == b->instance_id && a->generation == b->generation;
|
||
}
|
||
|
||
/**
|
||
* 功能:启用期限时,时钟回退也视为到期,避免无符号差值回绕造成长时间误等待。
|
||
* 输入参数:now——当前时刻。
|
||
* start——uint64_t值。
|
||
* timeout——超时状态。
|
||
* 输出参数:无。
|
||
* 返回值:按处理分支返回0U、timeout - (uint32_t)(now - start)。
|
||
* 说明:timeout=0是否禁用由Expired/Add_Timer处理,不直接用本函数判禁用。
|
||
*/
|
||
static uint32_t Remaining(uint64_t now, uint64_t start, uint32_t timeout)
|
||
{
|
||
if (now < start || now - start >= timeout) { return 0U; }
|
||
return timeout - (uint32_t)(now - start);
|
||
}
|
||
|
||
/**
|
||
* 功能:timeout=0禁用期限;
|
||
* 输入参数:now——当前时刻。
|
||
* start——uint64_t值。
|
||
* timeout——超时状态。
|
||
* 输出参数:无。
|
||
* 返回值:启用期限且已经到期或单调时钟回退时返回true;期限禁用或尚未到期时返回false。
|
||
* 说明:启用后恰到期限或时钟回退均按到期处理。
|
||
*/
|
||
static bool Expired(uint64_t now, uint64_t start, uint32_t timeout)
|
||
{
|
||
return timeout != 0U && Remaining(now, start, timeout) == 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:只有会话、恢复和清理三项义务全空时才释放;
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:该函数本身不撤销任何义务。
|
||
*/
|
||
static void Release_Preset(IEC104_RemoteParameter_t *self)
|
||
{
|
||
if (self->preset.phase == RP_NONE && !self->preset.recovery && !self->preset.discard) {
|
||
if (self->preset.station != NULL) {
|
||
(void)IEC104_Slave_Release_Station(self->slave, self->preset.station);
|
||
}
|
||
memset(&self->preset, 0, sizeof(self->preset));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:终结活动会话并停预置计时;模块主动终结真实会话时只登记DISCARD,不调用应用。
|
||
* 输入参数:self——本模块实例。
|
||
* recovery——bool值。
|
||
* discard——bool值。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:正常终止动作已在回调内清理,传discard=false;TEST永不欠应用清理。
|
||
*/
|
||
static void End_Preset(IEC104_RemoteParameter_t *self, bool recovery, bool discard)
|
||
{
|
||
if (self->preset.phase != RP_NONE) {
|
||
if (discard && !self->preset.source.test) { self->preset.discard = true; }
|
||
self->preset.phase = RP_NONE;
|
||
self->preset.recovery = recovery;
|
||
}
|
||
Release_Preset(self);
|
||
}
|
||
|
||
/**
|
||
* 功能:出队仅归还本槽的引用;
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:下一固定回复成为队首的这一刻开始计时,不能拖到下次Process。
|
||
*/
|
||
static void Remove_Task(IEC104_RemoteParameter_t *self)
|
||
{
|
||
RemoteTask *task = &self->tasks[self->order[0]];
|
||
if (task->station != NULL) { (void)IEC104_Slave_Release_Station(self->slave, task->station); }
|
||
memset(task, 0, sizeof(*task));
|
||
--self->count;
|
||
memmove(self->order, self->order + 1U, self->count * sizeof(self->order[0]));
|
||
if (self->count != 0U) {
|
||
RemoteTask *next = &self->tasks[self->order[0]];
|
||
next->send_waiting = false;
|
||
if (next->phase == RP_REPLY && !next->reply_started) {
|
||
next->reply_started = true;
|
||
next->reply_ms = RP_NOW();
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:每次完整初始化,避免上一PRESET的元素指针/CONT/count泄漏给其他动作。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* operation——操作类型。
|
||
* request——请求。
|
||
* 输出参数:request——请求的更新结果。
|
||
* 返回值:无。
|
||
* 说明:CANCEL保留本条请求SN;清理时应用按完整归属找暂存,不能据此SN选另一个区。
|
||
*/
|
||
static void Request_View(IEC104_RemoteParameter_t *self, const RemoteTask *task,
|
||
IEC104_RemoteParameterOperation_t operation, IEC104_RemoteParameterRequest_t *request)
|
||
{
|
||
memset(request, 0, sizeof(*request));
|
||
request->identity = task->identity;
|
||
request->instance_id = self->instance_id;
|
||
request->generation = task->identity.generation;
|
||
request->receive_serial = task->receive_serial;
|
||
request->station_id = task->station_id;
|
||
request->common_address = Read16(task->bytes + 4U);
|
||
request->originator_address = task->bytes[3];
|
||
request->type_id = task->bytes[0];
|
||
request->operation = operation;
|
||
request->test = (task->bytes[2] & 0x80U) != 0U;
|
||
request->accepted_ms = task->accepted_ms;
|
||
if (operation != IEC104_REMOTE_PARAMETER_OP_READ_AREA && task->length >= 8U) {
|
||
request->sn = Read16(task->bytes + (task->bytes[0] == 200U ? 9U : 6U));
|
||
}
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_READ || operation == IEC104_REMOTE_PARAMETER_OP_PRESET) {
|
||
request->count = task->bytes[1] & 0x7FU;
|
||
}
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_PRESET) {
|
||
request->first_frame = self->preset.phase == RP_NONE;
|
||
request->more_follows = (task->bytes[8] & 1U) != 0U;
|
||
request->_data = task->bytes;
|
||
request->_length = task->length;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:只比较批次归属,不比较SN:续帧/固化另查SN,撤销允许请求SN不同。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:无。
|
||
* 返回值:请求与当前预置的原服务、连接、站点编号、公共地址和TEST标志匹配时返回true,否则false。
|
||
* 说明:TEST是归属的一部分;同CA重注册后的station_id也不能继承旧会话。
|
||
*/
|
||
static bool Matches_Preset(const IEC104_RemoteParameter_t *self, const RemoteTask *task)
|
||
{
|
||
return self->preset.station != NULL && Same_Identity(&self->preset.source.identity, &task->identity) &&
|
||
self->preset.source.station_id == task->station_id &&
|
||
self->preset.source.common_address == Read16(task->bytes + 4U) &&
|
||
self->preset.source.test == ((task->bytes[2] & 0x80U) != 0U);
|
||
}
|
||
|
||
/**
|
||
* 功能:在首次应用暂存之前取得独立引用,保证回调成功后即使请求槽释放也能交付清理。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:task->station == NULL || IEC104_Slave_Acquire_Station(self->slave, task->station) != IEC104_STATION_OK时返回false;其余分支返回false、true。
|
||
* 说明:失败首帧建立恢复记录时同样保存来源,但不凭空产生DISCARD。
|
||
*/
|
||
static bool Save_Owner(IEC104_RemoteParameter_t *self, const RemoteTask *task)
|
||
{
|
||
IEC104_RemoteParameterRequest_t *source = &self->preset.source;
|
||
if (task->station == NULL ||
|
||
IEC104_Slave_Acquire_Station(self->slave, task->station) != IEC104_STATION_OK) { return false; }
|
||
self->preset.station = task->station;
|
||
Request_View(self, task, IEC104_REMOTE_PARAMETER_OP_DISCARD, source);
|
||
source->type_id = 203U;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:仅COT=6且可读S/E=1认作预置尝试。连PI都截短时,只有同归属COLLECTING
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:无。
|
||
* 返回值:task->bytes[0] != 203U || (task->bytes[2] & 0x3FU) != 6U || task->station == NULL时返回false;其余分支返回false、(task->bytes[8] & 0x80U) != 0U、self->preset.phase == RP_COLLECTING && Matches_Preset(self, task)。
|
||
* 说明:才保守当作失败续帧;错误COT、固化、撤销以及他站截短帧不破坏当前批次。
|
||
*/
|
||
static bool Preset_Attempt(const IEC104_RemoteParameter_t *self, const RemoteTask *task)
|
||
{
|
||
if (task->bytes[0] != 203U || (task->bytes[2] & 0x3FU) != 6U || task->station == NULL) { return false; }
|
||
if (task->length >= 9U) { return (task->bytes[8] & 0x80U) != 0U; }
|
||
return self->preset.phase == RP_COLLECTING && Matches_Preset(self, task);
|
||
}
|
||
|
||
/**
|
||
* 功能:到达队首后才判错误帧对批次的影响,不能在Handle_Event抢先清理前序预置。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:!Preset_Attempt(self, task)时返回true;!Save_Owner(self, task)时返回false;其余分支返回true、false。
|
||
* 说明:同归属COLLECTING失效;COMPLETE、他站或已有恢复记录保持,空闲首帧失败建立恢复。
|
||
*/
|
||
static bool Invalidate_Attempt(IEC104_RemoteParameter_t *self, const RemoteTask *task)
|
||
{
|
||
if (!Preset_Attempt(self, task)) { return true; }
|
||
if (self->preset.phase == RP_COLLECTING && Matches_Preset(self, task)) {
|
||
End_Preset(self, true, true);
|
||
} else if (self->preset.phase == RP_NONE && !self->preset.recovery && !self->preset.discard) {
|
||
if (!Save_Owner(self, task)) { return false; }
|
||
self->preset.recovery = true;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:失败回复之后还可能被Cancel/断线清除,reads_aborted对同一已受理读取只记一次。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:task——业务任务的更新结果。
|
||
* 返回值:无。
|
||
* 说明:直接处理传入的self、task,不经过其他函数调用。
|
||
*/
|
||
static void Abort_Read(IEC104_RemoteParameter_t *self, RemoteTask *task)
|
||
{
|
||
if (task->validated && task->bytes[0] == 202U && !task->read_aborted) {
|
||
task->read_aborted = true;
|
||
++self->status.reads_aborted;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:boundary=true表示已确认STOP/断线/代次失效,可以解除协议恢复保护。
|
||
* 输入参数:self——本模块实例。
|
||
* boundary——bool值。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:本地Cancel或尚未证实断线的故障传false:清任务后仍挡住在途旧续帧。
|
||
* 首帧尚未执行也可能已被主站视为发出,清槽时须保留其恢复归属。
|
||
*/
|
||
static void Cancel_All(IEC104_RemoteParameter_t *self, bool boundary)
|
||
{
|
||
End_Preset(self, !boundary, true);
|
||
while (self->count != 0U) {
|
||
RemoteTask *task = &self->tasks[self->order[0]];
|
||
if (!boundary && self->preset.station == NULL &&
|
||
(task->phase == RP_EXECUTE || !task->checked_failure) && Preset_Attempt(self, task)) {
|
||
/* 本槽马上出队,直接转交已有引用,避免再次Acquire触及上限而丢失恢复责任。 */
|
||
Request_View(self, task, IEC104_REMOTE_PARAMETER_OP_DISCARD, &self->preset.source);
|
||
self->preset.station = task->station;
|
||
task->station = NULL;
|
||
self->preset.recovery = true;
|
||
}
|
||
Abort_Read(self, task);
|
||
++self->status.cancelled;
|
||
Remove_Task(self);
|
||
}
|
||
if (boundary) { self->preset.recovery = false; Release_Preset(self); }
|
||
}
|
||
|
||
/**
|
||
* 功能:这些结果只证明所指定的原连接已结束/不再存在,不授权关闭其后的新连接。
|
||
* 输入参数:result——处理结果。
|
||
* 输出参数:无。
|
||
* 返回值:返回result == IEC104_TCP_OK || result == IEC104_TCP_NOT_CONNECTED || result == IEC104_TCP_STALE_GENERATION的结果。
|
||
* 说明:直接处理传入的result,不经过其他函数调用。
|
||
*/
|
||
static bool Closed_Result(IEC104_TcpResult_t result)
|
||
{
|
||
return result == IEC104_TCP_OK || result == IEC104_TCP_NOT_CONNECTED || result == IEC104_TCP_STALE_GENERATION;
|
||
}
|
||
|
||
/**
|
||
* 功能:从有效Slave状态补充原连接终结证据;查询失败保持义务,单独STOP也不算TCP终结。
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:即使观察到断线也不清faulted,只解除销毁所需的连接前置条件。
|
||
*/
|
||
static void Observe_End(IEC104_RemoteParameter_t *self)
|
||
{
|
||
IEC104_TcpServerStatus_t status;
|
||
const IEC104_SlaveEventIdentity_t *identity = self->status.faulted ? &self->fault_identity :
|
||
(self->preset.station != NULL ? &self->preset.source.identity : &self->session);
|
||
if (identity->instance_id == 0U || RP_STATUS(self->slave, &status, NULL) != IEC104_TCP_OK) { return; }
|
||
if (!status.connected || status.generation != identity->generation) {
|
||
if (self->status.faulted) { self->fault_ended = true; }
|
||
End_Preset(self, false, true);
|
||
self->preset.recovery = false;
|
||
Release_Preset(self);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:统一故障出口:先复制原身份,再取消全部槽,防止identity指向被清零的任务。
|
||
* 输入参数:self——本模块实例。
|
||
* identity——原会话身份。
|
||
* already_attempted——bool值。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:already_attempted用于回复超时关闭已失败的分支;无论关闭成败都不再自动重试。
|
||
*/
|
||
static void Fault(IEC104_RemoteParameter_t *self, const IEC104_SlaveEventIdentity_t *identity,
|
||
bool already_attempted)
|
||
{
|
||
IEC104_SlaveEventIdentity_t original = *identity;
|
||
if (!self->status.faulted) {
|
||
self->status.faulted = true;
|
||
self->fault_identity = original;
|
||
self->close_attempted = already_attempted;
|
||
}
|
||
Cancel_All(self, false);
|
||
if (!self->close_attempted) {
|
||
IEC104_TcpResult_t result;
|
||
self->close_attempted = true;
|
||
result = RP_CLOSE(self->slave, &self->fault_identity, NULL);
|
||
if (Closed_Result(result)) {
|
||
self->fault_ended = true;
|
||
self->preset.recovery = false;
|
||
Release_Preset(self);
|
||
} else { ++self->status.close_errors; }
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:WAIT_EVENTS让调用者先分派通知;ERROR不能当成已断线,必须走粘滞故障收尾。
|
||
* 输入参数:self——本模块实例。
|
||
* identity——原会话身份。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:返回state的结果。
|
||
* 说明:STALE可结束旧业务和恢复,但不能单独充当faulted实例TCP已终结的销毁凭据。
|
||
*/
|
||
static IEC104_SlaveRequestState_t Check(IEC104_RemoteParameter_t *self,
|
||
const IEC104_SlaveEventIdentity_t *identity)
|
||
{
|
||
IEC104_SlaveRequestState_t state = RP_CHECK(self->slave, identity);
|
||
self->status.events_pending = state == IEC104_SLAVE_REQUEST_WAIT_EVENTS;
|
||
if (state == IEC104_SLAVE_REQUEST_ERROR) { Fault(self, identity, false); }
|
||
else if (state == IEC104_SLAVE_REQUEST_STALE) {
|
||
Cancel_All(self, true);
|
||
self->status.active = false;
|
||
}
|
||
return state;
|
||
}
|
||
|
||
/**
|
||
* 功能:Process与直接Execute都先查旧期限,阻止迟到续帧先刷新成功时刻而复活批次。
|
||
* 输入参数:self——本模块实例。
|
||
* now——当前时刻。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:self->preset.phase == RP_NONE || !Expired(now, self->preset.success_ms, self->config.preset_timeout_ms)时返回false;其余分支返回false、true。
|
||
* 说明:内部调用:Expired、End_Preset。
|
||
*/
|
||
static bool Expire_Preset(IEC104_RemoteParameter_t *self, uint64_t now)
|
||
{
|
||
if (self->preset.phase == RP_NONE ||
|
||
!Expired(now, self->preset.success_ms, self->config.preset_timeout_ms)) { return false; }
|
||
if (!self->preset.source.test) { ++self->status.preset_timeouts; }
|
||
End_Preset(self, true, true);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:固定结果只写回复元数据;
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* cause——传送原因。
|
||
* negative——否定确认标志。
|
||
* 输出参数:task——业务任务的更新结果。
|
||
* 返回值:无。
|
||
* 说明:原请求字节和归属始终保留,重试不会重新调用动作。
|
||
*/
|
||
static void Reply(IEC104_RemoteParameter_t *self, RemoteTask *task, uint8_t cause, bool negative)
|
||
{
|
||
task->phase = RP_REPLY;
|
||
task->cause = cause;
|
||
task->negative = negative;
|
||
task->send_waiting = false;
|
||
if (self->count != 0U && task == &self->tasks[self->order[0]]) {
|
||
task->reply_ms = RP_NOW();
|
||
task->reply_started = true;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:首帧前镜像拒绝;已有正常帧则固定否定空末帧。切入RP_REPLY后不再lookup,
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* unknown——bool值。
|
||
* 输出参数:task——业务任务的更新结果。
|
||
* 返回值:无。
|
||
* 说明:后续只受回复期限约束,不能因应用恢复取值而重开本次读取。
|
||
*/
|
||
static void Read_Failed(IEC104_RemoteParameter_t *self, RemoteTask *task, bool unknown)
|
||
{
|
||
Abort_Read(self, task);
|
||
task->reply_kind = task->read_started ? RP_READ_ABORT : RP_MIRROR;
|
||
Reply(self, task, (uint8_t)(unknown && !task->read_started ? 47U : 7U), true);
|
||
task->checked_failure = true;
|
||
}
|
||
|
||
/**
|
||
* 功能:队首execute/read/reply期限互斥,后方任务不参与本轮计时。
|
||
* 输入参数:self——本模块实例。
|
||
* now——当前时刻。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:self->count == 0U || self->status.faulted时返回true;其余分支返回true、false。
|
||
* 说明:执行/读取到期冻结否定;固定回复到期则按原身份关闭连接,关闭失败只处理一次。
|
||
* 本函数不调用应用,预置错误带来的DISCARD由后续Execute交付。
|
||
*/
|
||
static bool Expire_Head(IEC104_RemoteParameter_t *self, uint64_t now)
|
||
{
|
||
RemoteTask *task;
|
||
if (self->count == 0U || self->status.faulted) { return true; }
|
||
task = &self->tasks[self->order[0]];
|
||
if (task->phase == RP_REPLY) {
|
||
if (!task->reply_started) { task->reply_started = true; task->reply_ms = now; }
|
||
if (Expired(now, task->reply_ms, self->config.reply_timeout_ms)) {
|
||
IEC104_SlaveEventIdentity_t identity = task->identity;
|
||
IEC104_TcpResult_t result;
|
||
++self->status.reply_timeouts;
|
||
result = RP_CLOSE(self->slave, &identity, NULL);
|
||
if (!Closed_Result(result)) {
|
||
++self->status.close_errors;
|
||
Fault(self, &identity, true);
|
||
return false;
|
||
}
|
||
Cancel_All(self, true);
|
||
self->status.active = false;
|
||
}
|
||
} else if (task->read_started) {
|
||
if (Expired(now, task->read_ms, self->config.read_timeout_ms)) {
|
||
++self->status.read_timeouts;
|
||
Read_Failed(self, task, false);
|
||
}
|
||
} else if (Expired(now, task->accepted_ms, self->config.execute_timeout_ms)) {
|
||
++self->status.timeouts;
|
||
if (task->bytes[0] == 202U) { Read_Failed(self, task, false); }
|
||
else {
|
||
Reply(self, task, (uint8_t)((task->bytes[2] & 0x3FU) == 8U ? 9U : 7U), true);
|
||
task->cancel_pi = task->validated && task->bytes[0] == 203U &&
|
||
(task->bytes[2] & 0x3FU) == 8U;
|
||
task->checked_failure = false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:线格式值检查,不解释地址对应的权限/值域。浮点直接查原始小端指数位,
|
||
* 输入参数:tag——uint8_t值。
|
||
* length——字节长度。
|
||
* value——业务值。
|
||
* 输出参数:无。
|
||
* 返回值:Tag对应的长度和内容合法时返回true,否则false;浮点须有限,Bytes不按字符串解释。
|
||
* 说明:不依赖宿主isfinite或优化选项;Tag4原始Bytes不强行套用String语义。
|
||
*/
|
||
static bool Value_Valid(uint8_t tag, uint8_t length, const uint8_t *value)
|
||
{
|
||
if (length > 235U || (length != 0U && value == NULL)) { return false; }
|
||
switch (tag) {
|
||
case 1: return length == 1U && value[0] <= 1U;
|
||
case 43: case 32: return length == 1U;
|
||
case 33: case 45: return length == 2U;
|
||
case 2: case 35: return length == 4U;
|
||
case 36: case 37: return length == 8U;
|
||
case 38: return length == 4U && !((value[3] & 0x7FU) == 0x7FU && (value[2] & 0x80U) != 0U);
|
||
case 39: return length == 8U && !((value[7] & 0x7FU) == 0x7FU && (value[6] & 0xF0U) == 0xF0U);
|
||
case 4: return true;
|
||
default: return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 功能:返回首个命中的否定原因,0表示结构和值编码合法。优先级不能随意重排:
|
||
* 输入参数:data——数据存储。
|
||
* length——字节长度。
|
||
* registered——注册状态。
|
||
* 输出参数:无。
|
||
* 返回值:返回首个命中的否定原因,0表示结构和值编码合法。
|
||
* 说明:COT -> CA -> 固定IOA -> 完整结构 -> 变长IOA -> Tag/值。
|
||
* TLV分三遍:先证明所有边界,再查地址,最后查值,保证后续截短优先于前面坏地址。
|
||
* 此处不改会话;批次影响必须等错误任务真正轮到队首再判。
|
||
*/
|
||
static uint8_t Validate(const uint8_t *data, uint16_t length, bool registered)
|
||
{
|
||
uint8_t type = data[0], count = data[1] & 0x7FU, cot = data[2] & 0x3FU;
|
||
uint8_t negative = (uint8_t)(type == 203U && cot == 8U ? 9U : 7U);
|
||
size_t offset, i;
|
||
if ((type != 203U && cot != 6U) || (type == 203U && cot != 6U && cot != 8U)) { return 45U; }
|
||
if (!registered || Read16(data + 4U) == 65535U) { return 46U; }
|
||
if ((type == 200U || type == 201U) && length >= 9U && Read24(data + 6U) != 0U) { return 47U; }
|
||
if ((data[1] & 0x80U) != 0U || (data[2] & 0x40U) != 0U) { return negative; }
|
||
if (type == 200U || type == 201U) {
|
||
return (count != 1U || length != (type == 200U ? 11U : 9U)) ? negative : 0U;
|
||
}
|
||
if (type == 202U) {
|
||
if ((size_t)length != 8U + 3U * count) { return negative; }
|
||
for (i = 0U; i < count; ++i) { if (Read24(data + 8U + 3U * i) == 0U) { return 47U; } }
|
||
return 0U;
|
||
}
|
||
if (length < 9U) { return negative; }
|
||
if (cot == 8U) { return (count != 0U || length != 9U || (data[8] != 0U && data[8] != 0x40U)) ? negative : 0U; }
|
||
if (count == 0U) { return length != 9U || data[8] != 0U ? negative : 0U; }
|
||
if ((data[8] & 0xFEU) != 0x80U) { return negative; }
|
||
offset = 9U;
|
||
for (i = 0U; i < count; ++i) {
|
||
if ((size_t)length - offset < 5U) { return negative; }
|
||
if ((size_t)data[offset + 4U] > (size_t)length - offset - 5U) { return negative; }
|
||
offset += 5U + data[offset + 4U];
|
||
}
|
||
if (offset != length) { return negative; }
|
||
offset = 9U;
|
||
for (i = 0U; i < count; ++i) {
|
||
if (Read24(data + offset) == 0U) { return 47U; }
|
||
offset += 5U + data[offset + 4U];
|
||
}
|
||
offset = 9U;
|
||
for (i = 0U; i < count; ++i) {
|
||
if (!Value_Valid(data[offset + 3U], data[offset + 4U], data + offset + 5U)) { return negative; }
|
||
offset += 5U + data[offset + 4U];
|
||
}
|
||
return 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:仅发送ERROR且完整状态证明STOP正在等通知分派时让出,不把泛化故障误当STOP。
|
||
* 输入参数:self——本模块实例。
|
||
* identity——原会话身份。
|
||
* submitted——IEC104_TcpSubmitResult_t值。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:仅原会话仍健康、传输已停止且存在待消费通知时返回true;其他情况返回false。
|
||
* 说明:FATAL即使伴随未读通知也不适用,因为可能已有字节交出且连接尚未终结。
|
||
*/
|
||
static bool Defer_For_Stop(IEC104_RemoteParameter_t *self,
|
||
const IEC104_SlaveEventIdentity_t *identity, IEC104_TcpSubmitResult_t submitted)
|
||
{
|
||
IEC104_TcpServerStatus_t status;
|
||
if (submitted != IEC104_TCP_SUBMIT_ERROR || !Same_Identity(identity, &self->session) ||
|
||
identity->slave != self->slave || RP_STATUS(self->slave, &status, NULL) != IEC104_TCP_OK ||
|
||
status.generation != identity->generation || !status.connected || !status.connection_status_valid ||
|
||
!status.asdu_queue_status_valid || status.connection.phase != IEC104_TCP_SESSION_ACTIVE ||
|
||
status.connection.fault != IEC104_TCP_FAULT_NONE || status.connection.data_transfer_started ||
|
||
status.pending_event_count == 0U) { return false; }
|
||
self->status.events_pending = true;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:内部返回:1=发送层接手,0=等待容量/STOP通知,-1=已故障收尾。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* length——字节长度。
|
||
* negative——否定确认标志。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* task——业务任务的更新结果。
|
||
* 返回值:内部返回:1=发送层接手,0=等待容量/STOP通知,-1=已故障收尾。
|
||
* 说明:-1可能已清空task及其引用,调用者不得继续访问;1也不等于主站已经收到。
|
||
* 用原文包装ASDU,避免公共对象解析器把202/203的SN误当IOA。
|
||
*/
|
||
static int Send(IEC104_RemoteParameter_t *self, RemoteTask *task, uint16_t length, bool negative)
|
||
{
|
||
IEC60870_Asdu_t asdu;
|
||
IEC104_TcpSubmitResult_t result;
|
||
if (IEC60870_Asdu_Create_From_Buffer(&asdu, IEC104_Slave_Get_Application_Layer_Parameters(self->slave),
|
||
self->buffer, length) != IEC60870_ASDU_OK) {
|
||
Fault(self, &task->identity, false);
|
||
return -1;
|
||
}
|
||
result = RP_SEND(self->slave, &task->identity, &asdu);
|
||
if (result == IEC104_TCP_SUBMIT_OK) {
|
||
++self->status.submitted;
|
||
if (negative) { ++self->status.negative_submitted; }
|
||
task->send_waiting = false;
|
||
return 1;
|
||
}
|
||
if (result == IEC104_TCP_SUBMIT_WAIT) {
|
||
task->send_waiting = true;
|
||
++self->status.waits;
|
||
return 0;
|
||
}
|
||
++self->status.send_errors;
|
||
if (Defer_For_Stop(self, &task->identity, result)) { return 0; }
|
||
Fault(self, &task->identity, false);
|
||
return -1;
|
||
}
|
||
|
||
/**
|
||
* 功能:用原文和冻结字段重组固定回复。除201区号、202负末帧和显式COMPACT肯定外镜像。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* task——业务任务的更新结果。
|
||
* 返回值:返回result的结果。
|
||
* 说明:合法撤销才改PI;截短的错误撤销不能补写不存在的字段。只有提交OK才出队。
|
||
*/
|
||
static int Send_Reply(IEC104_RemoteParameter_t *self, RemoteTask *task)
|
||
{
|
||
uint16_t length = task->length;
|
||
int result;
|
||
memcpy(self->buffer, task->bytes, length);
|
||
self->buffer[2] = (uint8_t)((self->buffer[2] & 0x80U) | task->cause | (task->negative ? 0x40U : 0U));
|
||
if (task->reply_kind == RP_AREA) {
|
||
self->buffer[1] = 1U;
|
||
Write16(self->buffer + 9U, task->area.current);
|
||
Write16(self->buffer + 11U, task->area.minimum);
|
||
Write16(self->buffer + 13U, task->area.maximum);
|
||
length = 15U;
|
||
} else if (task->reply_kind == RP_READ_ABORT) {
|
||
self->buffer[1] = 0U;
|
||
self->buffer[8] = 0U;
|
||
length = 9U;
|
||
} else if (task->reply_kind == RP_COMPACT) {
|
||
self->buffer[1] = 0U;
|
||
length = 9U;
|
||
}
|
||
if (task->cancel_pi) { self->buffer[8] = 0x40U; }
|
||
result = Send(self, task, length, task->negative);
|
||
if (result == 1) { Remove_Task(self); }
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:每条错误回复至多判一次批次失效;
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* task——业务任务的更新结果。
|
||
* 返回值:无需批次失效或批次失效处理完成返回true;失效处理失败时登记故障并返回false。
|
||
* 说明:如产生DISCARD,Process会先停发等待应用清理。
|
||
*/
|
||
static bool Prepare_Failure(IEC104_RemoteParameter_t *self, RemoteTask *task)
|
||
{
|
||
if (task->phase == RP_REPLY && !task->checked_failure) {
|
||
task->checked_failure = true;
|
||
if (task->negative && !Invalidate_Attempt(self, task)) {
|
||
Fault(self, &task->identity, false);
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:工程默认值:全类型、镜像预置确认,执行/预置/读取/固定回复期限分别15/60/60/15秒。
|
||
* 输入参数:config——配置。
|
||
* 输出参数:config——配置的更新结果。
|
||
* 返回值:无。
|
||
* 说明:内部调用:memset。
|
||
*/
|
||
void IEC104_RemoteParameter_Config_Default(IEC104_RemoteParameterConfig_t *config)
|
||
{
|
||
if (config == NULL) { return; }
|
||
memset(config, 0, sizeof(*config));
|
||
config->type_mask = 0x0FU;
|
||
config->preset_confirm_mode = IEC104_REMOTE_PARAMETER_CONFIRM_MIRROR;
|
||
config->execute_timeout_ms = 15000U;
|
||
config->preset_timeout_ms = 60000U;
|
||
config->read_timeout_ms = 60000U;
|
||
config->reply_timeout_ms = 15000U;
|
||
}
|
||
|
||
/**
|
||
* 功能:先完成配置及重复绑定检查,再占用静态槽和递增编号;任何失败都不消耗编号。
|
||
* 输入参数:slave——所属从站服务。
|
||
* config——配置。
|
||
* 输出参数:无。
|
||
* 返回值:按处理分支返回NULL、slot。
|
||
* 说明:只读应用层参数,不要求Slave已Init;此时不推测或补发尚未收到的生命周期。
|
||
*/
|
||
IEC104_RemoteParameter_t *IEC104_RemoteParameter_Create(IEC104_Slave_t *slave,
|
||
const IEC104_RemoteParameterConfig_t *config)
|
||
{
|
||
const IEC60870_AppLayerParameters_t *parameters = IEC104_Slave_Get_Application_Layer_Parameters(slave);
|
||
IEC104_RemoteParameter_t *slot = NULL;
|
||
unsigned i;
|
||
if (parameters == NULL || config == NULL || parameters->sizeOfCOT != 2 || parameters->sizeOfCA != 2 ||
|
||
parameters->sizeOfIOA != 3 || config->type_mask == 0U || (config->type_mask & 0xF0U) != 0U ||
|
||
(config->preset_confirm_mode != IEC104_REMOTE_PARAMETER_CONFIRM_MIRROR &&
|
||
config->preset_confirm_mode != IEC104_REMOTE_PARAMETER_CONFIRM_COMPACT_COMPAT) || next_instance_id == UINT64_MAX) { return NULL; }
|
||
for (i = 0U; i < IEC104_REMOTE_PARAMETER_CAPACITY; ++i) {
|
||
if (remote_pool[i].in_use && remote_pool[i].slave == slave) { return NULL; }
|
||
if (!remote_pool[i].in_use && slot == NULL) { slot = &remote_pool[i]; }
|
||
}
|
||
if (slot == NULL) { return NULL; }
|
||
memset(slot, 0, sizeof(*slot));
|
||
slot->in_use = true;
|
||
slot->slave = slave;
|
||
slot->config = *config;
|
||
slot->instance_id = ++next_instance_id;
|
||
return slot;
|
||
}
|
||
|
||
/**
|
||
* 功能:仅在任务、预置及清理责任全部结束后释放静态槽;不隐式Cancel或调用应用清理。
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值: 回调重入、尚欠清理或故障原连接未完成关闭时返回false,实例保留。
|
||
* 说明:回调重入、尚欠清理或故障原连接未完成关闭时返回false,实例保留。
|
||
*/
|
||
bool IEC104_RemoteParameter_Destroy(IEC104_RemoteParameter_t *self)
|
||
{
|
||
if (!Owned(self) || self->in_callback) { return false; }
|
||
/* 即使TCP已断开也不能在Destroy中隐式Cancel;应用必须先显式交付全部清理。 */
|
||
if (self->count != 0U || self->preset.phase != RP_NONE || self->preset.recovery || self->preset.discard) { return false; }
|
||
Observe_End(self);
|
||
if (self->status.faulted && !self->fault_ended) { return false; }
|
||
Release_Preset(self);
|
||
memset(self, 0, sizeof(*self));
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:本地取消不替主站声明新批次边界,也不回滚已经发布的运行值或撤回已交接的帧。
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:内部调用:Owned、Cancel_All、Observe_End。
|
||
*/
|
||
void IEC104_RemoteParameter_Cancel(IEC104_RemoteParameter_t *self)
|
||
{
|
||
if (!Owned(self) || self->in_callback) { return; }
|
||
Cancel_All(self, false);
|
||
Observe_End(self);
|
||
}
|
||
|
||
/**
|
||
* 功能:受理入口只复制事件和引用,不调用应用。状态通知仍须由分派器交给其他业务模块;
|
||
* 输入参数:self——本模块实例。
|
||
* event——事件。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:按处理分支返回IEC104_REMOTE_PARAMETER_EVENT_ERROR、IEC104_REMOTE_PARAMETER_EVENT_IGNORED、IEC104_REMOTE_PARAMETER_EVENT_HANDLED。
|
||
* 说明:I帧一旦HANDLED(包括满槽/faulted丢弃)就不能继续按未知类型回44。
|
||
*/
|
||
IEC104_RemoteParameterEventResult_t IEC104_RemoteParameter_Handle_Event(IEC104_RemoteParameter_t *self,
|
||
const IEC104_SlaveEvent_t *event)
|
||
{
|
||
IEC104_SlaveEventIdentity_t identity;
|
||
const uint8_t *data;
|
||
uint16_t length;
|
||
IEC104_Station_t *station;
|
||
RemoteTask *task = NULL;
|
||
uint8_t cause;
|
||
unsigned i;
|
||
if (!Owned(self) || self->in_callback || !IEC104_Slave_Get_Event_Identity(self->slave, event, &identity) ||
|
||
(self->session.instance_id != 0U && self->session.instance_id != identity.instance_id)) { return IEC104_REMOTE_PARAMETER_EVENT_ERROR; }
|
||
/* 历史通知按原身份处理,不能用旧代次清掉新连接。新CONNECTED不解除faulted。 */
|
||
if (event->transport.type == IEC104_TCP_EVENT_CONNECTED) {
|
||
if (self->session.generation != 0U && identity.generation < self->session.generation) { return IEC104_REMOTE_PARAMETER_EVENT_IGNORED; }
|
||
Cancel_All(self, true);
|
||
if (self->status.faulted && !Same_Identity(&identity, &self->fault_identity)) { self->fault_ended = true; }
|
||
self->session = identity;
|
||
self->status.active = false;
|
||
return IEC104_REMOTE_PARAMETER_EVENT_HANDLED;
|
||
}
|
||
if (event->transport.type == IEC104_TCP_EVENT_DISCONNECTED && self->status.faulted &&
|
||
Same_Identity(&identity, &self->fault_identity)) { self->fault_ended = true; }
|
||
if (!Same_Identity(&identity, &self->session)) { return IEC104_REMOTE_PARAMETER_EVENT_IGNORED; }
|
||
switch (event->transport.type) {
|
||
case IEC104_TCP_EVENT_ACTIVATED:
|
||
self->status.active = true;
|
||
return IEC104_REMOTE_PARAMETER_EVENT_HANDLED;
|
||
case IEC104_TCP_EVENT_DEACTIVATED:
|
||
case IEC104_TCP_EVENT_DISCONNECTED:
|
||
Cancel_All(self, true);
|
||
self->status.active = false;
|
||
return IEC104_REMOTE_PARAMETER_EVENT_HANDLED;
|
||
case IEC104_TCP_EVENT_I_FRAME: break;
|
||
default: return IEC104_REMOTE_PARAMETER_EVENT_IGNORED;
|
||
}
|
||
if (!self->status.active || event->asdu_result != IEC60870_ASDU_OK ||
|
||
IEC60870_Asdu_Get_Data(&event->asdu, &data, &length) != IEC60870_ASDU_OK || length < 6U || length > 249U ||
|
||
data[0] < 200U || data[0] > 203U ||
|
||
(self->config.type_mask & IEC104_REMOTE_PARAMETER_TYPE_BIT(data[0])) == 0U) { return IEC104_REMOTE_PARAMETER_EVENT_IGNORED; }
|
||
/* 必须先筛选本模块会接手的类型,再执行faulted丢弃;其他业务仍可正常分派。 */
|
||
if (self->status.faulted) { ++self->status.faulted_dropped; return IEC104_REMOTE_PARAMETER_EVENT_HANDLED; }
|
||
station = IEC104_Slave_Get_Station(self->slave, Read16(data + 4U));
|
||
cause = Validate(data, length, station != NULL);
|
||
if (cause != 0U) { ++self->status.rejected; }
|
||
if (self->count == IEC104_REMOTE_PARAMETER_TASK_CAPACITY) {
|
||
++self->status.dropped_requests;
|
||
return IEC104_REMOTE_PARAMETER_EVENT_HANDLED;
|
||
}
|
||
/* 错误请求也固定受理时的原站;等它到队首才判批次影响,不能按CA重查替代站。 */
|
||
if (station != NULL && IEC104_Slave_Acquire_Station(self->slave, station) != IEC104_STATION_OK) { return IEC104_REMOTE_PARAMETER_EVENT_ERROR; }
|
||
for (i = 0U; i < IEC104_REMOTE_PARAMETER_TASK_CAPACITY; ++i) {
|
||
if (!self->tasks[i].occupied) { task = &self->tasks[i]; self->order[self->count++] = i; break; }
|
||
}
|
||
if (task == NULL) {
|
||
if (station != NULL) { (void)IEC104_Slave_Release_Station(self->slave, station); }
|
||
return IEC104_REMOTE_PARAMETER_EVENT_ERROR;
|
||
}
|
||
memset(task, 0, sizeof(*task));
|
||
task->occupied = true;
|
||
task->identity = identity;
|
||
task->station = station;
|
||
task->station_id = IEC104_Station_Get_Id(station);
|
||
task->receive_serial = event->transport.data.i_frame.receive_serial;
|
||
task->accepted_ms = RP_NOW();
|
||
task->length = length;
|
||
memcpy(task->bytes, data, length);
|
||
task->validated = cause == 0U;
|
||
task->phase = RP_EXECUTE;
|
||
/* 200 TEST只表示协议校验通过,直接排肯定回复;实际区号权限由真实动作判断。 */
|
||
if (cause == 0U) {
|
||
++self->status.accepted;
|
||
if (data[0] == 200U && (data[2] & 0x80U) != 0U) { Reply(self, task, 7U, false); task->checked_failure = true; }
|
||
} else { Reply(self, task, cause, true); }
|
||
return IEC104_REMOTE_PARAMETER_EVENT_HANDLED;
|
||
}
|
||
|
||
/**
|
||
* 功能:期限先于发送循环检查,即使上轮WAIT或队首等应用回调,也不能饿死到期处理。
|
||
* 输入参数:self——本模块实例。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:本轮协议推进及会话检查完成返回true;实例、重入、查询或内部处理失败返回false。
|
||
* 说明:循环预算固定;遇待执行动作、DISCARD或发送WAIT即让出,不在此入口调用应用。
|
||
*/
|
||
bool IEC104_RemoteParameter_Process(IEC104_RemoteParameter_t *self)
|
||
{
|
||
unsigned budget;
|
||
IEC104_SlaveRequestState_t state;
|
||
if (!Owned(self) || self->in_callback) { return false; }
|
||
if (self->status.faulted) { Observe_End(self); return false; }
|
||
if (self->count == 0U && self->preset.station == NULL && self->session.instance_id == 0U) { return true; }
|
||
(void)Expire_Preset(self, RP_NOW());
|
||
if (!Expire_Head(self, RP_NOW())) { return false; }
|
||
for (budget = 0U; budget < 2U * IEC104_REMOTE_PARAMETER_TASK_CAPACITY + 2U; ++budget) {
|
||
RemoteTask *task;
|
||
const IEC104_SlaveEventIdentity_t *identity = self->count != 0U ? &self->tasks[self->order[0]].identity :
|
||
(self->preset.station != NULL ? &self->preset.source.identity : &self->session);
|
||
state = Check(self, identity);
|
||
if (state == IEC104_SLAVE_REQUEST_ERROR) { return false; }
|
||
if (state != IEC104_SLAVE_REQUEST_READY || self->count == 0U) { return true; }
|
||
if (!Expire_Head(self, RP_NOW())) { return false; }
|
||
if (self->count == 0U) { return true; }
|
||
task = &self->tasks[self->order[0]];
|
||
if (!Prepare_Failure(self, task)) { return false; }
|
||
if (self->preset.discard || task->phase != RP_REPLY) { return true; }
|
||
{
|
||
int sent = Send_Reply(self, task);
|
||
if (sent < 0) { return false; }
|
||
if (sent == 0) { return true; }
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:先核对当前回调的Request地址,再读取字段;复制出去的Request不拥有借用权限。
|
||
* 输入参数:request——请求。
|
||
* index——索引。
|
||
* element——IEC104_RemoteParameterElement_t *所指数据。
|
||
* 输出参数:element——IEC104_RemoteParameterElement_t *所指数据的更新结果。
|
||
* 返回值:成功解析当前预置回调中指定对象返回true;借用权限、操作、索引或原文长度非法返回false。
|
||
* 说明:value直接指向本次任务原文,只读且不得越过回调寿命;失败保持element输出不变。
|
||
*/
|
||
bool IEC104_RemoteParameter_Get_Element(const IEC104_RemoteParameterRequest_t *request, size_t index,
|
||
IEC104_RemoteParameterElement_t *element)
|
||
{
|
||
unsigned slot;
|
||
size_t offset = 9U, i;
|
||
IEC104_RemoteParameterElement_t value;
|
||
for (slot = 0U; slot < IEC104_REMOTE_PARAMETER_CAPACITY; ++slot) {
|
||
if (remote_pool[slot].in_use && remote_pool[slot].in_callback && remote_pool[slot].borrowed_request == request) { break; }
|
||
}
|
||
if (slot == IEC104_REMOTE_PARAMETER_CAPACITY || request == NULL || element == NULL ||
|
||
request->operation != IEC104_REMOTE_PARAMETER_OP_PRESET || index >= request->count ||
|
||
request->_data == NULL || request->_length < 9U) { return false; }
|
||
for (i = 0U; i <= index; ++i) {
|
||
if (offset > request->_length || (size_t)request->_length - offset < 5U ||
|
||
(size_t)request->_data[offset + 4U] > (size_t)request->_length - offset - 5U) { return false; }
|
||
if (i == index) { break; }
|
||
offset += 5U + request->_data[offset + 4U];
|
||
}
|
||
value.ioa = Read24(request->_data + offset);
|
||
value.tag = request->_data[offset + 3U];
|
||
value.length = request->_data[offset + 4U];
|
||
value.value = request->_data + offset + 5U;
|
||
*element = value;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:Frame仅在当前lookup借用;
|
||
* 输入参数:frame——报文帧。
|
||
* 输出参数:无。
|
||
* 返回值:按处理分支返回&remote_pool[i]、NULL。
|
||
* 说明:先按池地址匹配再读控制字段,拒绝伪造/过期指针。
|
||
*/
|
||
static IEC104_RemoteParameter_t *Frame_Owner(IEC104_RemoteParameterFrame_t *frame)
|
||
{
|
||
unsigned i;
|
||
for (i = 0U; i < IEC104_REMOTE_PARAMETER_CAPACITY; ++i) {
|
||
if (remote_pool[i].in_use && frame == &remote_pool[i].frame &&
|
||
remote_pool[i].in_callback && frame->borrowed) { return &remote_pool[i]; }
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* 功能:添加违约返回ADD_ERROR并锁存本次借用违约;非法Frame不解引用,改标当前活动借用。
|
||
* 输入参数:frame——报文帧。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:添加违约返回ADD_ERROR并锁存本次借用违约;
|
||
* 说明:不改已有TLV字节,后续合法添加也不能掩盖此前违规。
|
||
*/
|
||
static IEC104_RemoteParameterAddResult_t Bad_Add(IEC104_RemoteParameterFrame_t *frame)
|
||
{
|
||
unsigned i;
|
||
if (Frame_Owner(frame) != NULL) { frame->violation = true; }
|
||
else {
|
||
/* 回调内使用伪造/过期Frame也锁存到当前借用,不让随后一次合法添加掩盖违约。
|
||
* 查找时仅访问池内已知对象,绝不解引用传入的非法地址。 */
|
||
for (i = 0U; i < IEC104_REMOTE_PARAMETER_CAPACITY; ++i) {
|
||
if (remote_pool[i].in_use && remote_pool[i].in_callback && remote_pool[i].frame.borrowed) {
|
||
remote_pool[i].frame.violation = true;
|
||
}
|
||
}
|
||
}
|
||
return IEC104_REMOTE_PARAMETER_ADD_ERROR;
|
||
}
|
||
|
||
/**
|
||
* 功能:每个lookup只允许尝试添加一次。参数和剩余空间全部验证后才写缓冲,
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* tag——uint8_t值。
|
||
* value——业务值。
|
||
* length——字节长度。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:按处理分支返回Bad_Add(frame)、frame->last_add。
|
||
* 说明:ADD_ERROR/ADD_FULL都不改已有字节/长度/元素数,但会留下用于结果复核的控制状态。
|
||
*/
|
||
IEC104_RemoteParameterAddResult_t IEC104_RemoteParameter_Add_Element(IEC104_RemoteParameterFrame_t *frame,
|
||
uint32_t ioa, uint8_t tag, const uint8_t *value, uint8_t length)
|
||
{
|
||
IEC104_RemoteParameter_t *self = Frame_Owner(frame);
|
||
uint8_t *p;
|
||
if (self == NULL) { return Bad_Add(frame); }
|
||
if (frame->violation || frame->attempts != 0U || ioa == 0U || ioa > 0xFFFFFFU ||
|
||
(frame->match_ioa && ioa != frame->expected_ioa) || !Value_Valid(tag, length, value)) { return Bad_Add(frame); }
|
||
++frame->attempts;
|
||
if ((size_t)length + 5U > sizeof(self->buffer) - frame->length) {
|
||
frame->last_add = IEC104_REMOTE_PARAMETER_ADD_FULL;
|
||
return frame->last_add;
|
||
}
|
||
p = self->buffer + frame->length;
|
||
p[0] = (uint8_t)ioa; p[1] = (uint8_t)(ioa >> 8); p[2] = (uint8_t)(ioa >> 16);
|
||
p[3] = tag; p[4] = length;
|
||
if (length != 0U) { memcpy(p + 5U, value, length); }
|
||
frame->length = (uint16_t)(frame->length + 5U + length);
|
||
++frame->count;
|
||
frame->last_add = IEC104_REMOTE_PARAMETER_ADD_OK;
|
||
return frame->last_add;
|
||
}
|
||
|
||
/**
|
||
* 功能:固定宽度、逐字节小端编码;
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* tag——uint8_t值。
|
||
* value——业务值。
|
||
* length——字节长度。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回IEC104_RemoteParameter_Add_Element(frame, ioa, tag, bytes, length)的结果。
|
||
* 说明:有符号数转uint64_t后取低位,避免有符号右移的差异。
|
||
*/
|
||
static IEC104_RemoteParameterAddResult_t Add_Integer(IEC104_RemoteParameterFrame_t *frame, uint32_t ioa,
|
||
uint8_t tag, uint64_t value, uint8_t length)
|
||
{
|
||
uint8_t bytes[8], i;
|
||
for (i = 0U; i < length; ++i) { bytes[i] = (uint8_t)value; value >>= 8; }
|
||
return IEC104_RemoteParameter_Add_Element(frame, ioa, tag, bytes, length);
|
||
}
|
||
|
||
/* 生成固定类型的便捷入口;先转无符号取低位,再由Add_Element统一校验地址、借用及容量。 */
|
||
#define RP_INTEGER_HELPER(name, type, tag, width) \
|
||
IEC104_RemoteParameterAddResult_t IEC104_RemoteParameter_Add_##name( \
|
||
IEC104_RemoteParameterFrame_t *frame, uint32_t ioa, type value) \
|
||
{ return Add_Integer(frame, ioa, tag, (uint64_t)value, width); }
|
||
/**
|
||
* 功能:添加Tag1布尔量,编码为单字节0或1;
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回统一添加结果。
|
||
* 说明:返回统一添加结果。
|
||
*/
|
||
RP_INTEGER_HELPER(Boolean, bool, 1U, 1U)
|
||
/**
|
||
* 功能:添加Tag43有符号8位量,保留转换后的低8位线格式。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 43U, (uint64_t)value, 1U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(Tiny, int8_t, 43U, 1U)
|
||
/**
|
||
* 功能:添加Tag32无符号8位量,不进行比例换算。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 32U, (uint64_t)value, 1U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(UTiny, uint8_t, 32U, 1U)
|
||
/**
|
||
* 功能:添加Tag33有符号16位量,以2字节小端保存低位。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 33U, (uint64_t)value, 2U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(Short, int16_t, 33U, 2U)
|
||
/**
|
||
* 功能:添加Tag45无符号16位量,沿用统一地址和容量检查。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 45U, (uint64_t)value, 2U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(UShort, uint16_t, 45U, 2U)
|
||
/**
|
||
* 功能:添加Tag2有符号32位量,以4字节小端编码,不使用有符号右移。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 2U, (uint64_t)value, 4U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(Int, int32_t, 2U, 4U)
|
||
/**
|
||
* 功能:添加Tag35无符号32位量,以4字节小端编码。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 35U, (uint64_t)value, 4U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(Uint, uint32_t, 35U, 4U)
|
||
/**
|
||
* 功能:添加Tag36有符号64位量,转无符号后保留完整64位。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 36U, (uint64_t)value, 8U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(Long, int64_t, 36U, 8U)
|
||
/**
|
||
* 功能:添加Tag37无符号64位量,以8字节小端编码。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回Add_Integer(frame, ioa, 37U, (uint64_t)value, 8U)的结果。
|
||
* 说明:内部调用:Add_Integer。
|
||
*/
|
||
RP_INTEGER_HELPER(Ulong, uint64_t, 37U, 8U)
|
||
#undef RP_INTEGER_HELPER
|
||
|
||
/**
|
||
* 功能:先核对浮点宽度/精度和1.0的实际表示,再memcpy位模式,避免别名规则与宿主格式假设。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:按处理分支返回Bad_Add(frame)、Add_Integer(frame, ioa, 38U, bits, 4U)。
|
||
* 说明:有限值检查仍统一留给Add_Element,便捷接口不能绕过原始TLV校验。
|
||
*/
|
||
IEC104_RemoteParameterAddResult_t IEC104_RemoteParameter_Add_Float(IEC104_RemoteParameterFrame_t *frame,
|
||
uint32_t ioa, float value)
|
||
{
|
||
uint32_t bits = 0U, one_bits = 0U;
|
||
float one = 1.0F;
|
||
if (sizeof(value) != sizeof(bits) || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) { return Bad_Add(frame); }
|
||
memcpy(&one_bits, &one, sizeof(bits));
|
||
if (one_bits != UINT32_C(0x3F800000)) { return Bad_Add(frame); }
|
||
memcpy(&bits, &value, sizeof(bits));
|
||
return Add_Integer(frame, ioa, 38U, bits, 4U);
|
||
}
|
||
|
||
/**
|
||
* 功能:验证本机double为binary64并编码为Tag39;有限值、借用次数及容量仍由Add_Element校验。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:按处理分支返回Bad_Add(frame)、Add_Integer(frame, ioa, 39U, bits, 8U)。
|
||
* 说明:平台表示不符时经Bad_Add锁存违约,不写入候选TLV。
|
||
*/
|
||
IEC104_RemoteParameterAddResult_t IEC104_RemoteParameter_Add_Double(IEC104_RemoteParameterFrame_t *frame,
|
||
uint32_t ioa, double value)
|
||
{
|
||
uint64_t bits = 0U, one_bits = 0U;
|
||
double one = 1.0;
|
||
if (sizeof(value) != sizeof(bits) || FLT_RADIX != 2 || DBL_MANT_DIG != 53 || DBL_MAX_EXP != 1024) { return Bad_Add(frame); }
|
||
memcpy(&one_bits, &one, sizeof(bits));
|
||
if (one_bits != UINT64_C(0x3FF0000000000000)) { return Bad_Add(frame); }
|
||
memcpy(&bits, &value, sizeof(bits));
|
||
return Add_Integer(frame, ioa, 39U, bits, 8U);
|
||
}
|
||
|
||
/**
|
||
* 功能:按Tag4复制显式长度的原始字节,不追加NUL或限制字符编码;
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* length——字节长度。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:返回IEC104_RemoteParameter_Add_Element(frame, ioa, 4U, value, length)的结果。
|
||
* 说明:允许零长度,沿用统一添加门禁。
|
||
*/
|
||
IEC104_RemoteParameterAddResult_t IEC104_RemoteParameter_Add_Bytes(IEC104_RemoteParameterFrame_t *frame,
|
||
uint32_t ioa, const uint8_t *value, uint8_t length)
|
||
{
|
||
return IEC104_RemoteParameter_Add_Element(frame, ioa, 4U, value, length);
|
||
}
|
||
|
||
/**
|
||
* 功能:显式内容长度,不使用strlen;调用方不传终止符,这里验证ASCII并追加唯一NUL。
|
||
* 输入参数:frame——报文帧。
|
||
* ioa——信息体地址。
|
||
* value——业务值。
|
||
* length——字节长度。
|
||
* 输出参数:frame——报文帧的更新结果。
|
||
* 返回值:按处理分支返回Bad_Add(frame)、IEC104_RemoteParameter_Add_Element(frame, ioa, 4U, bytes, (uint8_t)(length + 1U))。
|
||
* 说明:需要任意字节、空串或其他编码时由应用选择Bytes语义,不能借String自动放宽。
|
||
*/
|
||
IEC104_RemoteParameterAddResult_t IEC104_RemoteParameter_Add_String(IEC104_RemoteParameterFrame_t *frame,
|
||
uint32_t ioa, const char *value, size_t length)
|
||
{
|
||
uint8_t bytes[64];
|
||
size_t i;
|
||
if (value == NULL || length == 0U || length > 63U) { return Bad_Add(frame); }
|
||
for (i = 0U; i < length; ++i) {
|
||
if ((unsigned char)value[i] == 0U || (unsigned char)value[i] > 0x7FU) { return Bad_Add(frame); }
|
||
bytes[i] = (uint8_t)value[i];
|
||
}
|
||
bytes[length] = 0U;
|
||
return IEC104_RemoteParameter_Add_Element(frame, ioa, 4U, bytes, (uint8_t)(length + 1U));
|
||
}
|
||
|
||
/**
|
||
* 功能:每次预检元素及每轮正式组帧均从空帧开始,保留原SN/OA/CA/T,不沿用上轮WAIT点值。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:内部调用:memset、memcpy。
|
||
*/
|
||
static void Frame_Reset(IEC104_RemoteParameter_t *self, const RemoteTask *task)
|
||
{
|
||
memset(&self->frame, 0, sizeof(self->frame));
|
||
self->frame.length = 9U;
|
||
memcpy(self->buffer, task->bytes, 8U);
|
||
self->buffer[1] = 0U;
|
||
self->buffer[2] = (uint8_t)((task->bytes[2] & 0x80U) | 7U);
|
||
self->buffer[8] = 0U;
|
||
}
|
||
|
||
/**
|
||
* 功能:在此建立并撤销回调借用;返回枚举必须与实际添加次数、长度和结果吻合。
|
||
* 输入参数:self——本模块实例。
|
||
* handlers——回调集合。
|
||
* context——调用方上下文。
|
||
* request——请求。
|
||
* index——索引。
|
||
* ioa——信息体地址。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:返回枚举必须与实际添加次数、长度和结果吻合。
|
||
* 说明:FULL须真正因空间不足且已有元素,END只用于读全部;SIZE_MAX处不能ADDED后回绕。
|
||
* 违约只计一次invalid_callbacks并统一转读取失败,不发送残留的部分输出。
|
||
*/
|
||
static IEC104_RemoteParameterLookupResult_t Lookup(IEC104_RemoteParameter_t *self,
|
||
const IEC104_RemoteParameterHandlers_t *handlers, void *context,
|
||
IEC104_RemoteParameterRequest_t *request, size_t index, uint32_t ioa)
|
||
{
|
||
IEC104_RemoteParameterFrame_t *frame = &self->frame;
|
||
IEC104_RemoteParameterLookupResult_t result;
|
||
uint16_t old_length = frame->length;
|
||
uint8_t old_count = frame->count;
|
||
bool valid = false;
|
||
frame->attempts = 0U;
|
||
frame->last_add = IEC104_REMOTE_PARAMETER_ADD_ERROR;
|
||
frame->violation = false;
|
||
frame->expected_ioa = ioa;
|
||
frame->match_ioa = request->count != 0U;
|
||
frame->borrowed = true;
|
||
self->in_callback = true;
|
||
self->borrowed_request = request;
|
||
result = handlers->lookup(context, request, index, ioa, frame);
|
||
self->borrowed_request = NULL;
|
||
self->in_callback = false;
|
||
frame->borrowed = false;
|
||
if (!frame->violation) {
|
||
switch (result) {
|
||
case IEC104_REMOTE_PARAMETER_LOOKUP_ADDED:
|
||
valid = frame->attempts == 1U && frame->last_add == IEC104_REMOTE_PARAMETER_ADD_OK &&
|
||
frame->count == old_count + 1U && frame->length > old_length && index != SIZE_MAX;
|
||
break;
|
||
case IEC104_REMOTE_PARAMETER_LOOKUP_FULL:
|
||
valid = frame->attempts == 1U && frame->last_add == IEC104_REMOTE_PARAMETER_ADD_FULL &&
|
||
frame->count == old_count && frame->length == old_length && old_count != 0U;
|
||
break;
|
||
case IEC104_REMOTE_PARAMETER_LOOKUP_END:
|
||
valid = request->count == 0U && frame->attempts == 0U;
|
||
break;
|
||
case IEC104_REMOTE_PARAMETER_LOOKUP_UNKNOWN:
|
||
valid = request->count != 0U && frame->attempts == 0U;
|
||
break;
|
||
case IEC104_REMOTE_PARAMETER_LOOKUP_ERROR:
|
||
valid = frame->attempts == 0U;
|
||
break;
|
||
default: break;
|
||
}
|
||
}
|
||
if (!valid) {
|
||
++self->status.invalid_callbacks;
|
||
return IEC104_REMOTE_PARAMETER_LOOKUP_ERROR;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 功能:一次Execute至多提交一帧。预检只提早发现指定地址错误,不保存值、不承诺快照。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* handlers——回调集合。
|
||
* context——调用方上下文。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* task——业务任务的更新结果。
|
||
* 返回值:其后的查询/发送故障按已推进返回OK,不能诱使应用重做。
|
||
* 说明:本函数进入后会调用lookup;其后的查询/发送故障按已推进返回OK,不能诱使应用重做。
|
||
*/
|
||
static IEC104_RemoteParameterExecuteResult_t Execute_Read(IEC104_RemoteParameter_t *self, RemoteTask *task,
|
||
const IEC104_RemoteParameterHandlers_t *handlers, void *context)
|
||
{
|
||
IEC104_RemoteParameterRequest_t request;
|
||
IEC104_RemoteParameterLookupResult_t lookup;
|
||
IEC104_SlaveRequestState_t state;
|
||
size_t cursor, i;
|
||
bool last = false;
|
||
int sent;
|
||
Request_View(self, task, IEC104_REMOTE_PARAMETER_OP_READ, &request);
|
||
/* 指定列表至多80项,每项独占空临时帧;正式组帧仍会再次取同一下标。 */
|
||
if (request.count != 0U && !task->prechecked) {
|
||
for (i = 0U; i < request.count; ++i) {
|
||
Frame_Reset(self, task);
|
||
lookup = Lookup(self, handlers, context, &request, i, Read24(task->bytes + 8U + 3U * i));
|
||
if (lookup != IEC104_REMOTE_PARAMETER_LOOKUP_ADDED) {
|
||
Read_Failed(self, task, lookup == IEC104_REMOTE_PARAMETER_LOOKUP_UNKNOWN);
|
||
break;
|
||
}
|
||
}
|
||
task->prechecked = true;
|
||
state = Check(self, &task->identity);
|
||
if (state == IEC104_SLAVE_REQUEST_WAIT_EVENTS) { return IEC104_REMOTE_PARAMETER_EXECUTE_WAIT; }
|
||
if (state != IEC104_SLAVE_REQUEST_READY) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (!Expire_Head(self, RP_NOW()) || self->count == 0U) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (task->phase == RP_REPLY) { (void)Send_Reply(self, task); return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
}
|
||
Frame_Reset(self, task);
|
||
cursor = task->cursor;
|
||
/* 最小TLV占5字节,240字节元素区最多48项,再多一次lookup判FULL/END。
|
||
* cursor只是本轮候选游标,任何WAIT都丢弃候选,持久task->cursor保持不变。 */
|
||
for (i = 0U; i < 49U; ++i) {
|
||
if (request.count != 0U && cursor == request.count) { last = true; break; }
|
||
lookup = Lookup(self, handlers, context, &request, cursor,
|
||
request.count == 0U ? 0U : Read24(task->bytes + 8U + 3U * cursor));
|
||
if (lookup == IEC104_REMOTE_PARAMETER_LOOKUP_ADDED) { ++cursor; continue; }
|
||
if (lookup == IEC104_REMOTE_PARAMETER_LOOKUP_END) { last = true; break; }
|
||
if (lookup == IEC104_REMOTE_PARAMETER_LOOKUP_FULL) { break; }
|
||
Read_Failed(self, task, lookup == IEC104_REMOTE_PARAMETER_LOOKUP_UNKNOWN);
|
||
break;
|
||
}
|
||
/* lookup耗时也计入期限;交出任何字节前复核会话,避免把旧结果发给新代次。 */
|
||
state = Check(self, &task->identity);
|
||
if (state == IEC104_SLAVE_REQUEST_WAIT_EVENTS) { return IEC104_REMOTE_PARAMETER_EXECUTE_WAIT; }
|
||
if (state != IEC104_SLAVE_REQUEST_READY) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (!Expire_Head(self, RP_NOW()) || self->count == 0U) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (task->phase == RP_REPLY) { (void)Send_Reply(self, task); return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
self->buffer[1] = self->frame.count;
|
||
self->buffer[8] = last ? 0U : 1U;
|
||
sent = Send(self, task, self->frame.length, false);
|
||
if (sent == 0) { return IEC104_REMOTE_PARAMETER_EXECUTE_WAIT; }
|
||
if (sent < 0) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
/* 发送层接手才提交游标;首帧OK切换到读取总期限,之后不追认已交出帧的超时。 */
|
||
task->cursor = cursor;
|
||
++self->status.read_frames;
|
||
if (!task->read_started) { task->read_started = true; task->read_ms = RP_NOW(); }
|
||
if (last) { ++self->status.reads_completed; Remove_Task(self); }
|
||
return IEC104_REMOTE_PARAMETER_EXECUTE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:清理不要求原连接仍有效,也不受未读通知阻挡;归属永远来自首帧而非当前连接。
|
||
* 输入参数:self——本模块实例。
|
||
* handlers——回调集合。
|
||
* context——调用方上下文。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:无。
|
||
* 说明:回调必须完成清理,返回值不能否决;交付后仅清discard,恢复保护可能继续存在。
|
||
*/
|
||
static void Deliver_Discard(IEC104_RemoteParameter_t *self,
|
||
const IEC104_RemoteParameterHandlers_t *handlers, void *context)
|
||
{
|
||
IEC104_RemoteParameterRequest_t request = self->preset.source;
|
||
request.operation = IEC104_REMOTE_PARAMETER_OP_DISCARD;
|
||
request.test = false;
|
||
request.count = 0U;
|
||
request.first_frame = false;
|
||
request.more_follows = false;
|
||
request._data = NULL;
|
||
request._length = 0U;
|
||
self->in_callback = true;
|
||
self->borrowed_request = &request;
|
||
(void)handlers->action(context, &request, NULL);
|
||
self->borrowed_request = NULL;
|
||
self->in_callback = false;
|
||
self->preset.discard = false;
|
||
++self->status.discards;
|
||
Release_Preset(self);
|
||
}
|
||
|
||
/**
|
||
* 功能:仅对已通过无状态校验且未到期的执行任务调用,203此时必有完整合法PI。
|
||
* 输入参数:task——业务任务。
|
||
* 输出参数:无。
|
||
* 返回值:按处理分支返回IEC104_REMOTE_PARAMETER_OP_SWITCH_AREA、IEC104_REMOTE_PARAMETER_OP_READ_AREA、IEC104_REMOTE_PARAMETER_OP_READ、IEC104_REMOTE_PARAMETER_OP_CANCEL、(task->bytes[8] & 0x80U) != 0U ? IEC104_REMOTE_PARAMETER_OP_PRESET : IEC104_REMOTE_PARAMETER_OP_SOLIDIFY。
|
||
* 说明:直接处理传入的task,不经过其他函数调用。
|
||
*/
|
||
static IEC104_RemoteParameterOperation_t Operation(const RemoteTask *task)
|
||
{
|
||
if (task->bytes[0] == 200U) { return IEC104_REMOTE_PARAMETER_OP_SWITCH_AREA; }
|
||
if (task->bytes[0] == 201U) { return IEC104_REMOTE_PARAMETER_OP_READ_AREA; }
|
||
if (task->bytes[0] == 202U) { return IEC104_REMOTE_PARAMETER_OP_READ; }
|
||
if ((task->bytes[2] & 0x3FU) == 8U) { return IEC104_REMOTE_PARAMETER_OP_CANCEL; }
|
||
return (task->bytes[8] & 0x80U) != 0U ? IEC104_REMOTE_PARAMETER_OP_PRESET : IEC104_REMOTE_PARAMETER_OP_SOLIDIFY;
|
||
}
|
||
|
||
/**
|
||
* 功能:false可能表示拒绝,也可能已完成恢复CANCEL并冻结肯定回复;调用方先检查任务阶段。
|
||
* 输入参数:self——本模块实例。
|
||
* task——业务任务。
|
||
* operation——操作类型。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* task——业务任务的更新结果。
|
||
* 返回值:false可能表示拒绝,也可能已完成恢复CANCEL并冻结肯定回复;
|
||
* 说明:恢复CANCEL只解除协议屏障,不再调用应用;正常CANCEL在下面的动作路径清理暂存。
|
||
* 完成态不把重复末帧当作新批次,同归属COLLECTING的SN冲突才终结原批次。
|
||
*/
|
||
static bool Session_Admits(IEC104_RemoteParameter_t *self, RemoteTask *task,
|
||
IEC104_RemoteParameterOperation_t operation)
|
||
{
|
||
bool match = Matches_Preset(self, task);
|
||
if (self->preset.recovery) {
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_CANCEL && match) {
|
||
self->preset.recovery = false;
|
||
Release_Preset(self);
|
||
Reply(self, task, 9U, false);
|
||
task->cancel_pi = true;
|
||
task->checked_failure = true;
|
||
return false;
|
||
}
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_PRESET || operation == IEC104_REMOTE_PARAMETER_OP_SOLIDIFY) {
|
||
++self->status.recovery_rejected;
|
||
}
|
||
return false;
|
||
}
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_PRESET) {
|
||
if (self->preset.phase == RP_NONE) { return true; }
|
||
if (self->preset.phase != RP_COLLECTING || !match) { return false; }
|
||
if (Read16(task->bytes + 6U) != self->preset.source.sn) { End_Preset(self, true, true); return false; }
|
||
return true;
|
||
}
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_SOLIDIFY) {
|
||
return self->preset.phase == RP_COMPLETE && match && Read16(task->bytes + 6U) == self->preset.source.sn;
|
||
}
|
||
return self->preset.phase != RP_NONE && match;
|
||
}
|
||
|
||
/**
|
||
* 功能:显式应用执行入口:校验回调 -> 检查旧预置期限 -> 交付DISCARD -> 检查队首 -> 动作。
|
||
* 输入参数:self——本模块实例。
|
||
* handlers——回调集合。
|
||
* context——调用方上下文。
|
||
* 输出参数:self——本模块实例的更新结果。
|
||
* 返回值:按处理分支返回IEC104_REMOTE_PARAMETER_EXECUTE_ERROR、IEC104_REMOTE_PARAMETER_EXECUTE_OK、IEC104_REMOTE_PARAMETER_EXECUTE_EMPTY、IEC104_REMOTE_PARAMETER_EXECUTE_WAIT、Execute_Read(self, task, handlers, context)。
|
||
* 说明:一次调用只交付一个清理或处理一个请求,不能越过固定回复执行后面的任务。
|
||
*/
|
||
IEC104_RemoteParameterExecuteResult_t IEC104_RemoteParameter_Execute_Next(IEC104_RemoteParameter_t *self,
|
||
const IEC104_RemoteParameterHandlers_t *handlers, void *context)
|
||
{
|
||
IEC104_RemoteParameterRequest_t request;
|
||
IEC104_RemoteParameterActionResult_t result;
|
||
IEC104_RemoteParameterOperation_t operation;
|
||
IEC104_SlaveRequestState_t state;
|
||
RemoteTask *task;
|
||
bool expired, valid_result, applied;
|
||
if (!Owned(self) || self->in_callback || handlers == NULL ||
|
||
((self->config.type_mask & 0x0BU) != 0U && handlers->action == NULL) ||
|
||
((self->config.type_mask & 0x04U) != 0U && handlers->lookup == NULL)) { return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
/* 调用者即使跳过Process直接执行,也必须先结束到期会话,再考虑迟到续帧/固化。 */
|
||
expired = !self->status.faulted && Expire_Preset(self, RP_NOW());
|
||
if (self->preset.discard) {
|
||
Deliver_Discard(self, handlers, context);
|
||
return IEC104_REMOTE_PARAMETER_EXECUTE_OK;
|
||
}
|
||
if (expired) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (self->status.faulted) { Observe_End(self); return IEC104_REMOTE_PARAMETER_EXECUTE_EMPTY; }
|
||
if (self->count == 0U) {
|
||
if (self->preset.station != NULL) {
|
||
state = Check(self, &self->preset.source.identity);
|
||
if (state == IEC104_SLAVE_REQUEST_ERROR) { return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
if (state == IEC104_SLAVE_REQUEST_STALE) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (state == IEC104_SLAVE_REQUEST_WAIT_EVENTS) { return IEC104_REMOTE_PARAMETER_EXECUTE_WAIT; }
|
||
}
|
||
return IEC104_REMOTE_PARAMETER_EXECUTE_EMPTY;
|
||
}
|
||
task = &self->tasks[self->order[0]];
|
||
state = Check(self, &task->identity);
|
||
if (state == IEC104_SLAVE_REQUEST_ERROR) { return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
if (state == IEC104_SLAVE_REQUEST_STALE) { return IEC104_REMOTE_PARAMETER_EXECUTE_OK; }
|
||
if (state == IEC104_SLAVE_REQUEST_WAIT_EVENTS) { return IEC104_REMOTE_PARAMETER_EXECUTE_WAIT; }
|
||
if (task->phase == RP_REPLY) { return IEC104_REMOTE_PARAMETER_EXECUTE_EMPTY; }
|
||
if (!Expire_Head(self, RP_NOW())) { return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
if (self->count == 0U || task->phase == RP_REPLY) {
|
||
if (self->count != 0U && !Prepare_Failure(self, task)) { return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
return IEC104_REMOTE_PARAMETER_EXECUTE_OK;
|
||
}
|
||
operation = Operation(task);
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_READ) { return Execute_Read(self, task, handlers, context); }
|
||
if (task->bytes[0] == 203U && !Session_Admits(self, task, operation)) {
|
||
if (task->phase != RP_REPLY) {
|
||
Reply(self, task, operation == IEC104_REMOTE_PARAMETER_OP_CANCEL ? 9U : 7U, true);
|
||
task->cancel_pi = operation == IEC104_REMOTE_PARAMETER_OP_CANCEL;
|
||
task->checked_failure = true;
|
||
}
|
||
if (!self->preset.discard && Send_Reply(self, task) < 0) { return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
return IEC104_REMOTE_PARAMETER_EXECUTE_OK;
|
||
}
|
||
Request_View(self, task, operation, &request);
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_PRESET && self->preset.phase == RP_NONE) {
|
||
if (!Save_Owner(self, task)) { Fault(self, &task->identity, false); return IEC104_REMOTE_PARAMETER_EXECUTE_ERROR; }
|
||
}
|
||
/* TEST203模拟会话而不调用写动作;201包括TEST都只读,真实动作计数排除TEST。 */
|
||
memset(&task->area, 0, sizeof(task->area));
|
||
if (request.test && request.type_id == 203U) { result = IEC104_REMOTE_PARAMETER_ACTION_APPLIED; }
|
||
else {
|
||
self->in_callback = true;
|
||
self->borrowed_request = &request;
|
||
result = handlers->action(context, &request, operation == IEC104_REMOTE_PARAMETER_OP_READ_AREA ? &task->area : NULL);
|
||
self->borrowed_request = NULL;
|
||
self->in_callback = false;
|
||
if (!request.test) { ++self->status.executed; }
|
||
}
|
||
valid_result = result == IEC104_REMOTE_PARAMETER_ACTION_REJECTED || result == IEC104_REMOTE_PARAMETER_ACTION_APPLIED ||
|
||
result == IEC104_REMOTE_PARAMETER_ACTION_UNKNOWN_IOA;
|
||
applied = result == IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
||
if (applied && operation == IEC104_REMOTE_PARAMETER_OP_READ_AREA &&
|
||
(task->area.minimum > task->area.current || task->area.current > task->area.maximum)) { valid_result = false; applied = false; }
|
||
if (!valid_result) { ++self->status.invalid_callbacks; applied = false; }
|
||
if (!request.test && applied) { ++self->status.applied; }
|
||
if (!request.test && operation == IEC104_REMOTE_PARAMETER_OP_PRESET && result == IEC104_REMOTE_PARAMETER_ACTION_UNKNOWN_IOA) {
|
||
++self->status.unknown_ioa;
|
||
}
|
||
/* 应用返回后立即冻结协议结果;失败预置/固化及正常撤销的暂存须已由应用清完。
|
||
* 终止动作在这里停预置计时,之后确认WAIT不能再产生该批次的超时DISCARD。 */
|
||
if (operation == IEC104_REMOTE_PARAMETER_OP_PRESET) {
|
||
if (applied) {
|
||
self->preset.phase = request.more_follows ? RP_COLLECTING : RP_COMPLETE;
|
||
self->preset.success_ms = RP_NOW();
|
||
if (!request.test && request.first_frame) { ++self->status.presets_opened; }
|
||
} else {
|
||
self->preset.phase = RP_NONE;
|
||
self->preset.recovery = true;
|
||
}
|
||
} else if (operation == IEC104_REMOTE_PARAMETER_OP_SOLIDIFY) {
|
||
if (applied && !request.test) { ++self->status.solidified; }
|
||
End_Preset(self, !applied, false);
|
||
} else if (operation == IEC104_REMOTE_PARAMETER_OP_CANCEL) {
|
||
if (!request.test) { ++self->status.presets_cancelled; }
|
||
End_Preset(self, false, false);
|
||
}
|
||
Reply(self, task, operation == IEC104_REMOTE_PARAMETER_OP_CANCEL ? 9U :
|
||
(operation == IEC104_REMOTE_PARAMETER_OP_PRESET && result == IEC104_REMOTE_PARAMETER_ACTION_UNKNOWN_IOA ? 47U : 7U), !applied);
|
||
task->checked_failure = true;
|
||
task->cancel_pi = operation == IEC104_REMOTE_PARAMETER_OP_CANCEL;
|
||
if (applied && operation == IEC104_REMOTE_PARAMETER_OP_READ_AREA) { task->reply_kind = RP_AREA; }
|
||
if (applied && operation == IEC104_REMOTE_PARAMETER_OP_PRESET &&
|
||
self->config.preset_confirm_mode == IEC104_REMOTE_PARAMETER_CONFIRM_COMPACT_COMPAT) { task->reply_kind = RP_COMPACT; }
|
||
/* 动作已经发生:后续即使查询或发送失败也返回OK,故障与清理义务另存状态。 */
|
||
state = Check(self, &task->identity);
|
||
if (state == IEC104_SLAVE_REQUEST_READY) { (void)Send_Reply(self, task); }
|
||
return IEC104_REMOTE_PARAMETER_EXECUTE_OK;
|
||
}
|
||
|
||
/**
|
||
* 功能:只合并当前可推进的期限;
|
||
* 输入参数:status——状态快照。
|
||
* now——当前时刻。
|
||
* start——uint64_t值。
|
||
* timeout——超时状态。
|
||
* 输出参数:status——状态快照的更新结果。
|
||
* 返回值:无。
|
||
* 说明:未启用不报计时器,到期用timer_active=true且remaining=0表示。
|
||
*/
|
||
static void Add_Timer(IEC104_RemoteParameterStatus_t *status, uint64_t now, uint64_t start, uint32_t timeout)
|
||
{
|
||
uint32_t remaining;
|
||
if (timeout == 0U) { return; }
|
||
remaining = Remaining(now, start, timeout);
|
||
if (!status->timer_active || remaining < status->timeout_remaining_ms) { status->timeout_remaining_ms = remaining; }
|
||
status->timer_active = true;
|
||
}
|
||
|
||
/**
|
||
* 功能:只读合成快照,最后一次性写输出;查询失败不改变调用方旧值,也不推进清理。
|
||
* 输入参数:self——本模块实例。
|
||
* status——状态快照。
|
||
* 输出参数:status——状态快照的更新结果。
|
||
* 返回值:!Owned(self) || status == NULL || RP_STATUS(self->slave, &transport, NULL) != IEC104_TCP_OK时返回false;其余分支返回false、true。
|
||
* 说明:“队内有任务”和“现在可执行”分开:发送WAIT时后方EXECUTE不能让事件循环空转。
|
||
* 恢复保护本身没有期限;faulted停报无法处理的定时器,但仍允许唤醒交付DISCARD。
|
||
*/
|
||
bool IEC104_RemoteParameter_Get_Status(const IEC104_RemoteParameter_t *self, IEC104_RemoteParameterStatus_t *status)
|
||
{
|
||
IEC104_RemoteParameterStatus_t value;
|
||
IEC104_TcpServerStatus_t transport;
|
||
uint64_t now;
|
||
unsigned i;
|
||
if (!Owned(self) || status == NULL || RP_STATUS(self->slave, &transport, NULL) != IEC104_TCP_OK) { return false; }
|
||
value = self->status;
|
||
value.generation = self->session.generation;
|
||
value.events_pending = transport.pending_event_count != 0U;
|
||
value.pending_requests = self->count;
|
||
value.preset_active = self->preset.phase != RP_NONE;
|
||
value.discard_pending = self->preset.discard;
|
||
value.recovery_required = self->preset.recovery;
|
||
value.execute_pending = false;
|
||
value.confirm_pending = false;
|
||
value.execute_ready = false;
|
||
value.need_process = false;
|
||
value.timer_active = false;
|
||
value.timeout_remaining_ms = 0U;
|
||
now = RP_NOW();
|
||
for (i = 0U; i < self->count; ++i) {
|
||
if (self->tasks[self->order[i]].phase == RP_REPLY) { value.confirm_pending = true; }
|
||
else { value.execute_pending = true; }
|
||
}
|
||
if (!value.faulted) {
|
||
if (value.preset_active) { Add_Timer(&value, now, self->preset.success_ms, self->config.preset_timeout_ms); }
|
||
if (self->count != 0U) {
|
||
const RemoteTask *task = &self->tasks[self->order[0]];
|
||
if (task->phase == RP_REPLY) {
|
||
Add_Timer(&value, now, task->reply_started ? task->reply_ms : now, self->config.reply_timeout_ms);
|
||
value.need_process = !task->send_waiting && !value.events_pending && !self->preset.discard;
|
||
} else {
|
||
Add_Timer(&value, now, task->read_started ? task->read_ms : task->accepted_ms,
|
||
task->read_started ? self->config.read_timeout_ms : self->config.execute_timeout_ms);
|
||
value.execute_ready = value.active && !value.events_pending && !task->send_waiting;
|
||
}
|
||
}
|
||
if (value.timer_active && value.timeout_remaining_ms == 0U) { value.need_process = true; }
|
||
if (self->preset.station != NULL && (!transport.connected || transport.generation != self->preset.source.generation)) {
|
||
value.need_process = true;
|
||
}
|
||
}
|
||
if (self->preset.discard) { value.execute_ready = true; }
|
||
/* 回调中查询仅供观察,抑制可能诱使调用者递归推进本模块的就绪和期限提示。 */
|
||
if (self->in_callback) {
|
||
value.execute_ready = false;
|
||
value.need_process = false;
|
||
value.timer_active = false;
|
||
value.timeout_remaining_ms = 0U;
|
||
}
|
||
*status = value;
|
||
return true;
|
||
}
|