feat(iec104): 在模拟器跑通共享协议核心与标准DTU点表
引入成熟 IEC 60870-5-104 子站的只读子集(App/protocol/iec60870,28 个源文件 按原字节复制,UPSTREAM.sha256 记录来源,兼容补丁保留全部断言与容量),在其上 装配单属主共享核心(Start/Poll/Stop/Get_Status),并把标准 DTU 演示点表全量 接入为唯一真源。 - iec104_core:threadless 推进,poll 零等待,通知/交互/COV 扫描全部有界; 生命周期区分失败启动待重试、重复启动拒绝、断线保固化 RAM。 - iec104_dtu_table / iec104_source:标准 DTU 全点表注册,无数据源的点读 0, 遥控只做模拟状态可读回,不触碰实际出口。 - iec104_sim:仅窗口与独立服务模式起真实 TCP,无头黄金模式保持确定性假统计。 - test/host/iec104_dtu:独立真实 TCP 11 项、传输层 10 项、生命周期与借用负例。 验证:lvgl_sim 真实 TCP 11/11、传输 10/10,UI 像素回归未回退;AC5 单 TU 编译 0 错误 0 诊断。板端尚未切换:完整库闭包 RAM 超 92% 门禁净增预算 25730B, 结论为停止转实施,记录在 IEC104_MCU最小可行性验证.md。旧实现与 board 服务 保持原样,未提交 MDK 工程改动,未烧板。
This commit is contained in:
@@ -0,0 +1,697 @@
|
||||
/* 模块名称:共享 IEC104 产品业务装配。
|
||||
* Product assembly of the imported IEC60870-Slave state machines.
|
||||
* No protocol parser, socket implementation or example main is duplicated here.
|
||||
*/
|
||||
#include "iec104_core.h"
|
||||
#include "iec104_source.h"
|
||||
#include "IEC104_Startup.h"
|
||||
#include "IEC104_GeneralInterrogation.h"
|
||||
#include "IEC104_CounterInterrogation.h"
|
||||
#include "IEC104_Read.h"
|
||||
#include "IEC104_PeriodicScan.h"
|
||||
#include "IEC104_Net.h"
|
||||
#include <string.h>
|
||||
|
||||
#define CORE_EVENT_BUDGET 16U
|
||||
#define CORE_DATA_BUDGET 20U
|
||||
#define CORE_WORK_BUDGET 4U
|
||||
#define CORE_FEEDBACK_CAPACITY 8U
|
||||
#define CORE_SCAN_CAPACITY 30U
|
||||
#define CORE_MONITOR_CAPACITY 105U
|
||||
|
||||
typedef struct {
|
||||
uint32_t value;
|
||||
uint8_t quality;
|
||||
bool initialized;
|
||||
} core_previous_t;
|
||||
|
||||
typedef struct {
|
||||
IEC104_Interrogation_t *module;
|
||||
IEC104_InterrogationEvent_t event;
|
||||
size_t cursor;
|
||||
uint64_t request_id;
|
||||
bool pending;
|
||||
bool counter;
|
||||
} core_interrogation_t;
|
||||
|
||||
typedef union {
|
||||
IEC60870_SinglePointWithCP56Time2a_t single;
|
||||
IEC60870_DoublePointWithCP56Time2a_t double_point;
|
||||
} core_timestamped_t;
|
||||
|
||||
typedef struct {
|
||||
IEC104_Slave_t *slave;
|
||||
IEC104_Station_t station;
|
||||
IEC104_Startup_t startup;
|
||||
IEC104_SystemCommand_t *system;
|
||||
IEC104_Command_t *command;
|
||||
IEC104_ClockSynchronization_t *clock;
|
||||
IEC104_RemoteParameter_t *remote;
|
||||
IEC104_Parameter_t *parameter;
|
||||
IEC104_Read_t *read;
|
||||
IEC104_PeriodicScan_t *scan;
|
||||
IEC104_Spontaneous_t *spontaneous;
|
||||
IEC104_SpontaneousChange_t change;
|
||||
IEC104_SpontaneousPublishInfo_t publication;
|
||||
core_previous_t previous[CORE_MONITOR_CAPACITY];
|
||||
size_t sample_cursor;
|
||||
iec104_source_stats_t source_stats;
|
||||
IEC60870_CP56Time2a_t sample_time;
|
||||
core_timestamped_t timestamped;
|
||||
core_interrogation_t gi, ci;
|
||||
IEC104_SlaveEvent_t event;
|
||||
IEC104_CommandEvent_t command_event, next_command;
|
||||
IEC104_CommandActionResult_t action_result;
|
||||
IEC104_SystemCommandStatus_t system_status;
|
||||
IEC104_TcpServerStatus_t transport_status;
|
||||
IEC104_TcpError_t error;
|
||||
IEC60870_Asdu_t batch;
|
||||
uint8_t bytes[IEC104_MAX_ASDU_LEN];
|
||||
iec104_source_object_t object;
|
||||
IEC104_PeriodicScanPoint_t scan_points[CORE_SCAN_CAPACITY];
|
||||
uint32_t feedback[CORE_FEEDBACK_CAPACITY];
|
||||
size_t feedback_count, feedback_index;
|
||||
uint16_t ca;
|
||||
uint8_t command_step;
|
||||
bool command_pending, reject_pending, closing;
|
||||
iec104_core_status_t status;
|
||||
} core_t;
|
||||
|
||||
static core_t s_core;
|
||||
/* Init configuration storage is static: never put these on a 640-word task stack. */
|
||||
static IEC104_SlaveConfig_t s_transport_config;
|
||||
static IEC104_SystemCommandConfig_t s_system_config;
|
||||
static IEC104_InterrogationConfig_t s_gi_config, s_ci_config;
|
||||
static IEC104_CommandConfig_t s_command_config;
|
||||
static IEC104_ClockSynchronizationConfig_t s_clock_config;
|
||||
static IEC104_RemoteParameterConfig_t s_remote_config;
|
||||
static IEC104_ParameterConfig_t s_parameter_config;
|
||||
static IEC104_ReadConfig_t s_read_config;
|
||||
static IEC104_PeriodicScanConfig_t s_scan_config;
|
||||
static IEC104_PeriodicScanGroupConfig_t s_scan_group;
|
||||
static IEC104_SpontaneousConfig_t s_spontaneous_config;
|
||||
|
||||
/* 函数功能:将累计观测计数饱和递增一次。 */
|
||||
static void increment(uint32_t *value)
|
||||
{
|
||||
if (*value != UINT32_MAX) { ++*value; }
|
||||
}
|
||||
|
||||
/* 函数功能:保存最近一次服务失败诊断。 */
|
||||
static int fail(void)
|
||||
{
|
||||
s_core.status.last_error = s_core.error.result != IEC104_TCP_OK ?
|
||||
(int)s_core.error.result : (int)IEC104_TCP_INTERNAL_ERROR;
|
||||
s_core.status.last_stage = (int)s_core.error.stage;
|
||||
s_core.status.native_error = s_core.error.socket_error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 函数功能:以本站及请求身份建立零对象肯定数据帧。 */
|
||||
static bool new_batch(uint8_t cause, uint8_t oa, bool test)
|
||||
{
|
||||
IEC60870_AsduHeader_t header;
|
||||
memset(&header, 0, sizeof(header));
|
||||
header.common_address = s_core.ca;
|
||||
header.cause_of_transmission = cause;
|
||||
header.originator_address = oa;
|
||||
header.is_test = test ? 1U : 0U;
|
||||
/* P/N stays zero: success data is never encoded as a negative confirm. */
|
||||
return IEC60870_Asdu_Create(&s_core.batch,
|
||||
IEC104_Slave_Get_Application_Layer_Parameters(s_core.slave),
|
||||
s_core.bytes, sizeof(s_core.bytes), &header, 0) == IEC60870_ASDU_OK;
|
||||
}
|
||||
|
||||
static IEC104_ReadLookupResult_t read_point(void *context,
|
||||
const IEC104_ReadRequest_t *request, IEC60870_Asdu_t *response)
|
||||
{
|
||||
(void)context;
|
||||
if (!iec104_source_find(request->ioa, &s_core.object)) { return IEC104_READ_LOOKUP_UNKNOWN; }
|
||||
/* Counters and parameter objects are not in Type102's monitor whitelist. */
|
||||
if (s_core.object.object.type_id == 15U || s_core.object.object.type_id == 37U ||
|
||||
s_core.object.object.type_id >= 100U) { return IEC104_READ_LOOKUP_UNKNOWN; }
|
||||
if (s_core.object.object.type_id == 1U || s_core.object.object.type_id == 3U) {
|
||||
uint64_t utc_ms = iec104_source_clock_ms(request->base_ms);
|
||||
uint64_t utc_seconds = utc_ms / 1000U;
|
||||
iec104_source_stats(&s_core.source_stats);
|
||||
memset(&s_core.sample_time, 0, sizeof(s_core.sample_time));
|
||||
if (utc_seconds > IEC60870_TIME_MAX_SECONDS ||
|
||||
!IEC60870_Time_Seconds_To_Local((uint32_t)utc_seconds,
|
||||
(uint16_t)(utc_ms % 1000U), &s_core.sample_time.time)) { return IEC104_READ_LOOKUP_ERROR; }
|
||||
s_core.sample_time.has_weekday = 1U;
|
||||
s_core.sample_time.is_invalid = s_core.source_stats.synchronized ? 0U : 1U;
|
||||
if (s_core.object.object.type_id == 1U) {
|
||||
if (IEC60870_SinglePointWithCP56Time2a_Create(&s_core.timestamped.single,
|
||||
request->ioa, s_core.object.single.value, s_core.object.single.quality,
|
||||
&s_core.sample_time) != IEC60870_ASDU_OK) { return IEC104_READ_LOOKUP_ERROR; }
|
||||
return IEC60870_Asdu_Add_InformationObject(response, &s_core.timestamped.single.base.object) ==
|
||||
IEC60870_ASDU_OK ? IEC104_READ_LOOKUP_FOUND : IEC104_READ_LOOKUP_ERROR;
|
||||
}
|
||||
if (IEC60870_DoublePointWithCP56Time2a_Create(&s_core.timestamped.double_point,
|
||||
request->ioa, s_core.object.double_point.value, s_core.object.double_point.quality,
|
||||
&s_core.sample_time) != IEC60870_ASDU_OK) { return IEC104_READ_LOOKUP_ERROR; }
|
||||
return IEC60870_Asdu_Add_InformationObject(response, &s_core.timestamped.double_point.base.object) ==
|
||||
IEC60870_ASDU_OK ? IEC104_READ_LOOKUP_FOUND : IEC104_READ_LOOKUP_ERROR;
|
||||
}
|
||||
return IEC60870_Asdu_Add_InformationObject(response, &s_core.object.object) == IEC60870_ASDU_OK ?
|
||||
IEC104_READ_LOOKUP_FOUND : IEC104_READ_LOOKUP_ERROR;
|
||||
}
|
||||
|
||||
static IEC104_PeriodicScanLookupResult_t scan_point(void *context,
|
||||
const IEC104_PeriodicScanRequest_t *request, IEC60870_Asdu_t *response)
|
||||
{
|
||||
(void)context;
|
||||
if (!iec104_source_find(request->ioa, &s_core.object)) { return IEC104_PERIODIC_SCAN_LOOKUP_ERROR; }
|
||||
return IEC60870_Asdu_Add_InformationObject(response, &s_core.object.object) == IEC60870_ASDU_OK ?
|
||||
IEC104_PERIODIC_SCAN_LOOKUP_ADDED : IEC104_PERIODIC_SCAN_LOOKUP_ERROR;
|
||||
}
|
||||
|
||||
/* Lifecycle events go to EVERY module, even if another module handled them. */
|
||||
/* 函数功能:分派原连接事件并对未支持请求执行有界否定回显。 */
|
||||
static bool dispatch(const IEC104_SlaveEvent_t *event)
|
||||
{
|
||||
bool reset_data = false;
|
||||
int handled;
|
||||
bool information = event->transport.type == IEC104_TCP_EVENT_I_FRAME;
|
||||
if (s_core.closing && information) { return true; }
|
||||
if (!s_core.closing &&
|
||||
!IEC104_Startup_Handle_Event(&s_core.startup, s_core.slave, event, &reset_data)) { return false; }
|
||||
if (reset_data) {
|
||||
s_core.gi.cursor = s_core.ci.cursor = 0U;
|
||||
s_core.gi.request_id = s_core.ci.request_id = 0U;
|
||||
}
|
||||
#define CORE_DISPATCH(module, function) \
|
||||
do { \
|
||||
if ((module) != NULL) { \
|
||||
handled = (int)function((module), event); \
|
||||
if (handled < 0) { return false; } \
|
||||
if (information && handled > 0) { return true; } \
|
||||
} \
|
||||
} while (0)
|
||||
CORE_DISPATCH(s_core.gi.module, IEC104_Interrogation_Handle_Event);
|
||||
CORE_DISPATCH(s_core.ci.module, IEC104_Interrogation_Handle_Event);
|
||||
CORE_DISPATCH(s_core.command, IEC104_Command_Handle_Event);
|
||||
CORE_DISPATCH(s_core.clock, IEC104_ClockSynchronization_Handle_Event);
|
||||
CORE_DISPATCH(s_core.remote, IEC104_RemoteParameter_Handle_Event);
|
||||
CORE_DISPATCH(s_core.parameter, IEC104_Parameter_Handle_Event);
|
||||
CORE_DISPATCH(s_core.read, IEC104_Read_Handle_Event);
|
||||
CORE_DISPATCH(s_core.system, IEC104_SystemCommand_Handle_Event);
|
||||
CORE_DISPATCH(s_core.scan, IEC104_PeriodicScan_Handle_Event);
|
||||
#undef CORE_DISPATCH
|
||||
if (information && event->asdu_result == IEC60870_ASDU_OK && !s_core.closing) {
|
||||
IEC104_TcpSubmitResult_t result = IEC104_Slave_Reject_Request(s_core.slave, event,
|
||||
IEC60870_ASDU_COT_UNKNOWN_TYPE);
|
||||
if (result == IEC104_TCP_SUBMIT_WAIT) { s_core.reject_pending = true; }
|
||||
else if (result != IEC104_TCP_SUBMIT_OK && result != IEC104_TCP_SUBMIT_ERROR) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 函数功能:按预算消费传输通知,保留背压请求的稳定存储。 */
|
||||
static bool drain_events(unsigned budget)
|
||||
{
|
||||
unsigned count;
|
||||
if (s_core.reject_pending && !s_core.closing) {
|
||||
IEC104_TcpSubmitResult_t reply = IEC104_Slave_Reject_Request(s_core.slave, &s_core.event,
|
||||
IEC60870_ASDU_COT_UNKNOWN_TYPE);
|
||||
if (reply == IEC104_TCP_SUBMIT_WAIT) { return true; }
|
||||
s_core.reject_pending = false;
|
||||
if (reply == IEC104_TCP_SUBMIT_FATAL) { return false; }
|
||||
}
|
||||
for (count = 0U; count < budget; ++count) {
|
||||
IEC104_TcpEventResult_t next = IEC104_Slave_NextEvent(s_core.slave, &s_core.event);
|
||||
const IEC104_TcpEvent_t *transport = &s_core.event.transport;
|
||||
if (next == IEC104_TCP_EVENT_EMPTY) { return true; }
|
||||
if (next != IEC104_TCP_EVENT_READY) { return false; }
|
||||
if (transport->type == IEC104_TCP_EVENT_CONNECTED) { increment(&s_core.status.connections); }
|
||||
if (transport->type == IEC104_TCP_EVENT_I_FRAME) { increment(&s_core.status.received_i_frames); }
|
||||
if (transport->type == IEC104_TCP_EVENT_RECORD) {
|
||||
if (transport->data.record.value.local_send_completed != 0U) {
|
||||
increment(&s_core.status.written_i_frames);
|
||||
}
|
||||
if (transport->data.record.value.peer_acknowledged != 0U) {
|
||||
increment(&s_core.status.acknowledged_i_frames);
|
||||
}
|
||||
}
|
||||
if (!dispatch(&s_core.event)) { return false; }
|
||||
if (s_core.reject_pending) { return true; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 函数功能:按召唤分组限定本次取点范围。 */
|
||||
static bool group_member(const core_interrogation_t *work, uint8_t type)
|
||||
{
|
||||
if (work->counter || work->event.request.qualifier == 20U) { return true; }
|
||||
if (work->event.request.qualifier == 21U) { return type == 1U || type == 3U; }
|
||||
return type == 9U || type == 11U || type == 13U;
|
||||
}
|
||||
|
||||
/* 函数功能:有界推进召唤业务,仅在发送接手后提交取点游标。 */
|
||||
static bool interrogation_step(core_interrogation_t *work)
|
||||
{
|
||||
unsigned round;
|
||||
for (round = 0U; round < CORE_WORK_BUDGET; ++round) {
|
||||
IEC104_InterrogationReplyResult_t reply = IEC104_INTERROGATION_REPLY_OK;
|
||||
IEC104_InterrogationEvent_t *event = &work->event;
|
||||
if (!IEC104_Interrogation_Process(work->module)) { return false; }
|
||||
if (!work->counter && !IEC104_Startup_Can_Consume_General(&s_core.startup)) { return true; }
|
||||
if (!work->pending) {
|
||||
IEC104_InterrogationNextResult_t next = IEC104_Interrogation_NextEvent(work->module, event);
|
||||
if (next == IEC104_INTERROGATION_NEXT_EMPTY) { return true; }
|
||||
if (next != IEC104_INTERROGATION_NEXT_READY) { return false; }
|
||||
work->pending = true;
|
||||
}
|
||||
if (event->type == IEC104_INTERROGATION_BEGIN) {
|
||||
bool accept = work->counter ?
|
||||
(event->request.qualifier == 5U && event->request.freeze == 0U) :
|
||||
(event->request.qualifier >= 20U && event->request.qualifier <= 22U);
|
||||
if (!work->counter && accept) {
|
||||
IEC104_TcpSubmitResult_t observed = IEC104_Startup_Observe_General(
|
||||
&s_core.startup, s_core.slave, event, true);
|
||||
if (observed == IEC104_TCP_SUBMIT_WAIT) { return true; }
|
||||
if (observed != IEC104_TCP_SUBMIT_OK) { return false; }
|
||||
}
|
||||
reply = IEC104_Interrogation_Reply_Begin(work->module, event, accept);
|
||||
} else if (event->type == IEC104_INTERROGATION_NEED_DATA) {
|
||||
size_t cursor, count = 0U, inspected = 0U;
|
||||
size_t total = work->counter ? 8U : iec104_source_count();
|
||||
if (work->request_id != event->request_id) {
|
||||
work->request_id = event->request_id;
|
||||
work->cursor = 0U;
|
||||
}
|
||||
cursor = work->cursor;
|
||||
if (!new_batch(event->request.data_cause, event->request.originator_address,
|
||||
event->request.is_test)) { return false; }
|
||||
while (cursor < total && inspected < CORE_DATA_BUDGET) {
|
||||
bool read_ok = work->counter ? iec104_source_counter(cursor, &s_core.object) :
|
||||
iec104_source_point(cursor, &s_core.object);
|
||||
IEC60870_AsduResult_t added;
|
||||
++inspected;
|
||||
if (!read_ok) { return false; }
|
||||
if (!group_member(work, s_core.object.object.type_id)) { ++cursor; continue; }
|
||||
if (count != 0U && s_core.batch.type_id != s_core.object.object.type_id) { break; }
|
||||
added = IEC60870_Asdu_Add_InformationObject(&s_core.batch, &s_core.object.object);
|
||||
if (added == IEC60870_ASDU_FULL) { break; }
|
||||
if (added != IEC60870_ASDU_OK) { return false; }
|
||||
++cursor;
|
||||
++count;
|
||||
}
|
||||
if (count != 0U) {
|
||||
reply = IEC104_Interrogation_Submit_Batch(work->module, event, &s_core.batch);
|
||||
if (reply == IEC104_INTERROGATION_REPLY_OK) { work->cursor = cursor; }
|
||||
} else if (cursor == total) {
|
||||
reply = IEC104_Interrogation_Complete_Data(work->module, event);
|
||||
} else {
|
||||
/* Filtering consumed a bounded number of rows but produced no ASDU. */
|
||||
work->cursor = cursor;
|
||||
return true;
|
||||
}
|
||||
} else if (event->type == IEC104_INTERROGATION_FINISHED) {
|
||||
if (!work->counter && event->reason == IEC104_INTERROGATION_FINISH_COMPLETED) {
|
||||
IEC104_TcpSubmitResult_t observed = IEC104_Startup_Observe_General(
|
||||
&s_core.startup, s_core.slave, event, false);
|
||||
if (observed == IEC104_TCP_SUBMIT_WAIT) { return true; }
|
||||
if (observed == IEC104_TCP_SUBMIT_FATAL) { return false; }
|
||||
increment(&s_core.status.gi_completed);
|
||||
}
|
||||
if (work->request_id == event->request_id) { work->request_id = 0U; work->cursor = 0U; }
|
||||
} else { return false; }
|
||||
if (reply == IEC104_INTERROGATION_REPLY_WAIT) { return true; }
|
||||
if (reply != IEC104_INTERROGATION_REPLY_OK && reply != IEC104_INTERROGATION_REPLY_STALE) { return false; }
|
||||
work->pending = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 函数功能:执行一次数据源模拟动作并保存启动结果。 */
|
||||
static IEC104_CommandActionResult_t command_action(void *context, const IEC104_CommandRequest_t *request)
|
||||
{
|
||||
s_core.action_result = iec104_source_command(context, request);
|
||||
return s_core.action_result;
|
||||
}
|
||||
|
||||
/* 函数功能:推进遥控动作结果、反馈及终止;背压不重复动作。 */
|
||||
static bool command_step(void)
|
||||
{
|
||||
unsigned step;
|
||||
IEC104_CommandReplyResult_t reply;
|
||||
for (step = 0U; step < CORE_WORK_BUDGET; ++step) {
|
||||
IEC104_CommandNextResult_t next = IEC104_Command_NextEvent(s_core.command, &s_core.next_command);
|
||||
if (next == IEC104_COMMAND_NEXT_EMPTY) { break; }
|
||||
if (next != IEC104_COMMAND_NEXT_READY) { return false; }
|
||||
if (s_core.next_command.type == IEC104_COMMAND_FINISHED) {
|
||||
if (s_core.next_command.reason == IEC104_COMMAND_FINISH_COMPLETED) {
|
||||
increment(&s_core.status.commands_completed);
|
||||
}
|
||||
if (s_core.command_event.transaction_id == s_core.next_command.transaction_id) {
|
||||
s_core.command_pending = false;
|
||||
s_core.command_step = 0U;
|
||||
}
|
||||
} else {
|
||||
s_core.command_event = s_core.next_command;
|
||||
s_core.command_pending = true;
|
||||
s_core.command_step = 0U;
|
||||
}
|
||||
}
|
||||
if (!s_core.command_pending) { return true; }
|
||||
reply = IEC104_COMMAND_REPLY_OK;
|
||||
if (s_core.command_step == 0U) {
|
||||
s_core.action_result = IEC104_COMMAND_ACTION_NOT_STARTED;
|
||||
reply = IEC104_Command_Accept(s_core.command, &s_core.command_event, command_action, NULL);
|
||||
if (reply == IEC104_COMMAND_REPLY_OK) {
|
||||
if (s_core.command_event.type == IEC104_COMMAND_SELECT ||
|
||||
s_core.action_result != IEC104_COMMAND_ACTION_STARTED) {
|
||||
s_core.command_pending = false;
|
||||
return true;
|
||||
}
|
||||
s_core.feedback_count = iec104_source_command_feedback(s_core.command_event.request.ioa,
|
||||
s_core.feedback, CORE_FEEDBACK_CAPACITY);
|
||||
if (s_core.feedback_count > CORE_FEEDBACK_CAPACITY) { return false; }
|
||||
s_core.feedback_index = 0U;
|
||||
s_core.command_step = 1U;
|
||||
}
|
||||
}
|
||||
if (reply == IEC104_COMMAND_REPLY_OK && s_core.command_step == 1U) {
|
||||
reply = IEC104_Command_Report_Result(s_core.command, &s_core.command_event, IEC104_COMMAND_RESULT_SUCCEEDED);
|
||||
if (reply == IEC104_COMMAND_REPLY_OK) { s_core.command_step = 2U; }
|
||||
}
|
||||
if (reply == IEC104_COMMAND_REPLY_OK && s_core.command_step == 2U) {
|
||||
if (s_core.feedback_index < s_core.feedback_count) {
|
||||
if (!new_batch(IEC60870_ASDU_COT_RETURN_INFO_REMOTE,
|
||||
s_core.command_event.request.originator_address, false) ||
|
||||
!iec104_source_find(s_core.feedback[s_core.feedback_index], &s_core.object) ||
|
||||
IEC60870_Asdu_Add_InformationObject(&s_core.batch, &s_core.object.object) != IEC60870_ASDU_OK) {
|
||||
return false;
|
||||
}
|
||||
reply = IEC104_Command_Submit_Return_Info(s_core.command, &s_core.command_event, &s_core.batch);
|
||||
if (reply == IEC104_COMMAND_REPLY_OK) { ++s_core.feedback_index; }
|
||||
}
|
||||
if (s_core.feedback_index == s_core.feedback_count) { s_core.command_step = 3U; }
|
||||
}
|
||||
if (reply == IEC104_COMMAND_REPLY_OK && s_core.command_step == 3U) {
|
||||
reply = IEC104_Command_Complete(s_core.command, &s_core.command_event);
|
||||
if (reply == IEC104_COMMAND_REPLY_OK) { s_core.command_pending = false; }
|
||||
}
|
||||
if (reply == IEC104_COMMAND_REPLY_STALE) { s_core.command_pending = false; return true; }
|
||||
return reply != IEC104_COMMAND_REPLY_ERROR;
|
||||
}
|
||||
|
||||
/* 函数功能:等待初始化和真实全站总召交接后开放上送门禁。 */
|
||||
static bool startup_step(void)
|
||||
{
|
||||
return IEC104_Startup_Process(&s_core.startup, s_core.slave, s_core.system, &s_core.system_status);
|
||||
}
|
||||
|
||||
/* Re-read current snapshots after backpressure; never consume source events. */
|
||||
/* 函数功能:有界读取当前快照并将变化交给成熟突发引擎。 */
|
||||
static bool collect_changes(void)
|
||||
{
|
||||
unsigned row;
|
||||
uint64_t now = IEC104_Net_Now_Ms();
|
||||
iec104_source_stats(&s_core.source_stats);
|
||||
if (!IEC104_Spontaneous_Set_Time_Base(s_core.spontaneous,
|
||||
iec104_source_clock_ms(now), now, s_core.source_stats.synchronized)) { return false; }
|
||||
for (row = 0U; row < CORE_DATA_BUDGET; ++row) {
|
||||
uint32_t value = 0U;
|
||||
uint8_t quality;
|
||||
core_previous_t *previous = &s_core.previous[s_core.sample_cursor];
|
||||
if (!iec104_source_point(s_core.sample_cursor, &s_core.object)) { return false; }
|
||||
memset(&s_core.change, 0, sizeof(s_core.change));
|
||||
s_core.change.common_address = s_core.ca;
|
||||
s_core.change.ioa = s_core.object.object.ioa;
|
||||
s_core.change.time_kind = IEC104_SPONTANEOUS_TIME_MONOTONIC;
|
||||
s_core.change.time_ms = now;
|
||||
switch (s_core.object.object.type_id) {
|
||||
case 1U:
|
||||
value = s_core.object.single.value;
|
||||
quality = s_core.object.single.quality;
|
||||
s_core.change.kind = IEC104_SPONTANEOUS_SINGLE;
|
||||
s_core.change.value.single = (uint8_t)value;
|
||||
break;
|
||||
case 3U:
|
||||
value = s_core.object.double_point.value;
|
||||
quality = s_core.object.double_point.quality;
|
||||
s_core.change.kind = IEC104_SPONTANEOUS_DOUBLE;
|
||||
s_core.change.value.double_point = (uint8_t)value;
|
||||
break;
|
||||
case 13U:
|
||||
memcpy(&value, &s_core.object.measurement.value, sizeof(value));
|
||||
quality = s_core.object.measurement.quality;
|
||||
s_core.change.kind = IEC104_SPONTANEOUS_FLOAT;
|
||||
s_core.change.value.short_float = s_core.object.measurement.value;
|
||||
break;
|
||||
default: return false;
|
||||
}
|
||||
s_core.change.quality = quality;
|
||||
if (previous->initialized && (previous->value != value || previous->quality != quality)) {
|
||||
IEC104_SpontaneousPublishResult_t result = IEC104_Spontaneous_Publish(s_core.spontaneous,
|
||||
&s_core.change, 1U, &s_core.publication);
|
||||
if (result == IEC104_SPONTANEOUS_PUBLISH_WAIT) { return true; }
|
||||
if (result != IEC104_SPONTANEOUS_PUBLISH_OK) { return false; }
|
||||
}
|
||||
previous->value = value;
|
||||
previous->quality = quality;
|
||||
previous->initialized = true;
|
||||
++s_core.sample_cursor;
|
||||
if (s_core.sample_cursor == CORE_MONITOR_CAPACITY) { s_core.sample_cursor = 0U; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static IEC104_ParameterActionResult_t parameter_action(void *context,
|
||||
const IEC104_ParameterRequest_t *request)
|
||||
{
|
||||
IEC104_ParameterActionResult_t result = iec104_source_parameter_action(context, request);
|
||||
if (result == IEC104_PARAMETER_ACTION_APPLIED && request->type_id == 113U &&
|
||||
request->qpa == IEC60870_ASDU_QPA_CYCLIC_TRANSMISSION && !request->test &&
|
||||
request->phase == IEC104_PARAMETER_PHASE_EXECUTE) {
|
||||
uint32_t period = request->operation == IEC104_PARAMETER_OP_ACTIVATE ? 10000U : 0U;
|
||||
if (!IEC104_PeriodicScan_Set_Group_Period(s_core.scan, 0U, period)) {
|
||||
return IEC104_PARAMETER_ACTION_REJECTED;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* 函数功能:按严格业务优先级逐轮调度已装配模块。 */
|
||||
static bool business_step(void)
|
||||
{
|
||||
unsigned priority;
|
||||
uint64_t now = IEC104_Net_Now_Ms();
|
||||
if (!IEC104_Command_Set_Time_Base(s_core.command, iec104_source_clock_ms(now), now) ||
|
||||
!collect_changes()) { return false; }
|
||||
for (priority = IEC104_BUSINESS_PRIORITY_INITIALIZATION; priority < IEC104_BUSINESS_PRIORITY_COUNT; ++priority) {
|
||||
if (IEC104_Slave_Set_Business_Priority(s_core.slave, (IEC104_BusinessPriority_t)priority) != IEC104_TCP_SUBMIT_OK) {
|
||||
return false;
|
||||
}
|
||||
switch ((IEC104_BusinessPriority_t)priority) {
|
||||
case IEC104_BUSINESS_PRIORITY_INITIALIZATION:
|
||||
if (!IEC104_SystemCommand_Process(s_core.system) || !startup_step()) { return false; }
|
||||
break;
|
||||
case IEC104_BUSINESS_PRIORITY_INITIAL_INTERROGATION:
|
||||
case IEC104_BUSINESS_PRIORITY_INTERROGATION:
|
||||
if (!interrogation_step(&s_core.gi) || !startup_step()) { return false; }
|
||||
if (priority == IEC104_BUSINESS_PRIORITY_INTERROGATION &&
|
||||
(!IEC104_Read_Process(s_core.read) || IEC104_Read_Execute_Next(s_core.read, read_point, NULL) ==
|
||||
IEC104_READ_EXECUTE_ERROR)) { return false; }
|
||||
break;
|
||||
case IEC104_BUSINESS_PRIORITY_COMMAND:
|
||||
if (!IEC104_Command_Process(s_core.command) || !command_step() ||
|
||||
!IEC104_Command_Process(s_core.command) || !IEC104_Parameter_Process(s_core.parameter) ||
|
||||
IEC104_Parameter_Execute_Next(s_core.parameter, parameter_action, NULL) ==
|
||||
IEC104_PARAMETER_EXECUTE_ERROR || !IEC104_RemoteParameter_Process(s_core.remote) ||
|
||||
IEC104_RemoteParameter_Execute_Next(s_core.remote, &iec104_source_remote_handlers, NULL) ==
|
||||
IEC104_REMOTE_PARAMETER_EXECUTE_ERROR) { return false; }
|
||||
break;
|
||||
case IEC104_BUSINESS_PRIORITY_CLOCK:
|
||||
if (!IEC104_ClockSynchronization_Process(s_core.clock) ||
|
||||
IEC104_ClockSynchronization_Execute_Next_With_Read(s_core.clock,
|
||||
iec104_source_clock_set, iec104_source_clock_read, NULL) == IEC104_CLOCK_SYNCHRONIZATION_EXECUTE_ERROR ||
|
||||
!IEC104_ClockSynchronization_Process(s_core.clock)) { return false; }
|
||||
break;
|
||||
case IEC104_BUSINESS_PRIORITY_MEASUREMENT:
|
||||
if (IEC104_PeriodicScan_Step(s_core.scan, scan_point, NULL) == IEC104_PERIODIC_SCAN_STEP_ERROR) { return false; }
|
||||
break;
|
||||
case IEC104_BUSINESS_PRIORITY_COUNTER_INTERROGATION:
|
||||
if (!interrogation_step(&s_core.ci)) { return false; }
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 函数功能:更新同所有者线程读取的传输观测镜像。 */
|
||||
static bool refresh_status(void)
|
||||
{
|
||||
if (IEC104_Slave_Get_Status(s_core.slave, &s_core.transport_status, &s_core.error) != IEC104_TCP_OK) { return false; }
|
||||
s_core.status.bound_port = s_core.transport_status.bound_port;
|
||||
s_core.status.connected = s_core.transport_status.connected;
|
||||
s_core.status.active = (uint8_t)(s_core.transport_status.connected != 0U &&
|
||||
s_core.transport_status.connection_status_valid != 0U &&
|
||||
s_core.transport_status.connection.data_transfer_started != 0U);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 函数功能:执行一次零等待网络推进及有界业务调度。 */
|
||||
int iec104_core_poll(void)
|
||||
{
|
||||
bool ok;
|
||||
if (s_core.slave == NULL || s_core.status.running == 0U || s_core.closing) { return -1; }
|
||||
if (IEC104_Slave_Poll(s_core.slave, 0U, &s_core.error) != IEC104_TCP_OK ||
|
||||
!drain_events(CORE_EVENT_BUDGET) || !refresh_status()) { return fail(); }
|
||||
if (s_core.transport_status.pending_event_count != 0U || s_core.reject_pending) { return 0; }
|
||||
if (IEC104_Slave_Begin_Business_Cycle(s_core.slave) != IEC104_TCP_SUBMIT_OK) { return fail(); }
|
||||
ok = business_step();
|
||||
if (IEC104_Slave_End_Business_Cycle(s_core.slave) != IEC104_TCP_SUBMIT_OK) { ok = false; }
|
||||
return ok ? 0 : fail();
|
||||
}
|
||||
|
||||
/* 函数功能:分步取得静态模块及监听资源,失败由外层统一清理。 */
|
||||
static int initialize_core(const iec104_core_config_t *config)
|
||||
{
|
||||
size_t i, scan_count = 0U;
|
||||
if (s_core.slave != NULL || config == NULL || config->bind_address == NULL ||
|
||||
strlen(config->bind_address) >= sizeof(s_transport_config.bind_address) ||
|
||||
config->common_address == 0U || config->common_address == UINT16_MAX) { return -1; }
|
||||
memset(&s_core, 0, sizeof(s_core));
|
||||
s_core.ca = config->common_address;
|
||||
iec104_source_init();
|
||||
s_core.slave = IEC104_Slave_Create();
|
||||
if (s_core.slave == NULL) { return fail(); }
|
||||
s_core.status.owns_resources = 1U;
|
||||
IEC104_Startup_Init(&s_core.startup);
|
||||
IEC104_Startup_Config_Default(&s_transport_config, &s_system_config, &s_gi_config, &s_ci_config, &s_spontaneous_config);
|
||||
memcpy(s_transport_config.bind_address, config->bind_address, strlen(config->bind_address) + 1U);
|
||||
s_transport_config.port = config->port;
|
||||
/* Only initialization and protocol tests are enabled: no MCU reset side effects. */
|
||||
s_system_config.features = IEC104_SYSTEM_COMMAND_FEATURE_INIT_END | IEC104_SYSTEM_COMMAND_FEATURE_TEST_COMMAND;
|
||||
if (IEC104_Slave_Add_Station(s_core.slave, &s_core.station, s_core.ca, NULL) != IEC104_STATION_OK) { return fail(); }
|
||||
s_core.system = IEC104_SystemCommand_Create(s_core.slave, &s_system_config);
|
||||
s_core.gi.module = IEC104_Interrogation_Create(s_core.slave, &s_gi_config);
|
||||
s_core.ci.module = IEC104_CounterInterrogation_Create(s_core.slave, &s_ci_config);
|
||||
s_core.ci.counter = true;
|
||||
IEC104_Command_Config_Default(&s_command_config);
|
||||
s_command_config.points = iec104_source_commands(s_core.ca, &s_command_config.point_count);
|
||||
s_core.command = IEC104_Command_Create(s_core.slave, &s_command_config);
|
||||
IEC104_ClockSynchronization_Config_Default(&s_clock_config);
|
||||
s_core.clock = IEC104_ClockSynchronization_Create(s_core.slave, &s_clock_config);
|
||||
IEC104_RemoteParameter_Config_Default(&s_remote_config);
|
||||
s_core.remote = IEC104_RemoteParameter_Create(s_core.slave, &s_remote_config);
|
||||
iec104_source_parameter_config(s_core.ca, &s_parameter_config);
|
||||
s_core.parameter = IEC104_Parameter_Create(s_core.slave, &s_parameter_config);
|
||||
IEC104_Read_Config_Default(&s_read_config);
|
||||
s_core.read = IEC104_Read_Create(s_core.slave, &s_read_config);
|
||||
for (i = 0U; i < iec104_source_count(); ++i) {
|
||||
if (!iec104_source_point(i, &s_core.object)) { return fail(); }
|
||||
if (s_core.object.object.type_id == 13U) {
|
||||
if (scan_count == CORE_SCAN_CAPACITY) { return fail(); }
|
||||
s_core.scan_points[scan_count].common_address = s_core.ca;
|
||||
s_core.scan_points[scan_count].type_id = 13U;
|
||||
s_core.scan_points[scan_count].groups = 1U;
|
||||
s_core.scan_points[scan_count].ioa = s_core.object.object.ioa;
|
||||
++scan_count;
|
||||
}
|
||||
}
|
||||
IEC104_PeriodicScan_Config_Default(&s_scan_config);
|
||||
memset(&s_scan_group, 0, sizeof(s_scan_group));
|
||||
s_scan_group.period_ms = 10000U;
|
||||
s_scan_group.first_delay_ms = 10000U;
|
||||
s_scan_group.cause = IEC60870_ASDU_COT_PERIODIC;
|
||||
s_scan_config.points = s_core.scan_points;
|
||||
s_scan_config.point_count = scan_count;
|
||||
s_scan_config.groups = &s_scan_group;
|
||||
s_scan_config.group_count = 1U;
|
||||
s_scan_config.max_objects_per_asdu = CORE_DATA_BUDGET;
|
||||
s_scan_config.max_asdus_per_step = 1U;
|
||||
s_core.scan = IEC104_PeriodicScan_Create(s_core.slave, &s_scan_config);
|
||||
s_core.spontaneous = IEC104_Spontaneous_Create(s_core.slave, &s_spontaneous_config);
|
||||
if (s_core.system == NULL || s_core.gi.module == NULL || s_core.ci.module == NULL ||
|
||||
s_core.command == NULL || s_core.clock == NULL || s_core.remote == NULL ||
|
||||
s_core.parameter == NULL || s_core.read == NULL || s_core.scan == NULL || s_core.spontaneous == NULL ||
|
||||
!IEC104_SystemCommand_Set_Initialization(s_core.system, 0U) ||
|
||||
IEC104_Slave_Init(s_core.slave, &s_transport_config, &s_core.error) != IEC104_TCP_OK) { return fail(); }
|
||||
s_core.status.running = 1U;
|
||||
if (!refresh_status()) { return fail(); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:启动单实例服务,失败时保留错误并有界回收已取得资源。 */
|
||||
int iec104_core_start(const iec104_core_config_t *config)
|
||||
{
|
||||
int result;
|
||||
/* An existing instance must never be torn down by a rejected double start. */
|
||||
if (s_core.slave != NULL) { return -1; }
|
||||
result = initialize_core(config);
|
||||
if (result != 0 && s_core.slave != NULL) {
|
||||
int original_error = s_core.status.last_error;
|
||||
int original_stage = s_core.status.last_stage;
|
||||
int original_native = s_core.status.native_error;
|
||||
(void)iec104_core_stop();
|
||||
s_core.status.last_error = original_error;
|
||||
s_core.status.last_stage = original_stage;
|
||||
s_core.status.native_error = original_native;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* 函数功能:取消业务、排空有限通知并按依赖顺序释放模块。 */
|
||||
int iec104_core_stop(void)
|
||||
{
|
||||
unsigned n;
|
||||
bool ok = true;
|
||||
if (s_core.slave == NULL) { return 0; }
|
||||
s_core.closing = true;
|
||||
s_core.status.running = 0U;
|
||||
if (!IEC104_Startup_Reset(&s_core.startup, s_core.slave)) { return fail(); }
|
||||
IEC104_Interrogation_Cancel(s_core.gi.module);
|
||||
IEC104_Interrogation_Cancel(s_core.ci.module);
|
||||
IEC104_Command_Cancel(s_core.command);
|
||||
IEC104_ClockSynchronization_Cancel(s_core.clock);
|
||||
IEC104_RemoteParameter_Cancel(s_core.remote);
|
||||
IEC104_Parameter_Cancel(s_core.parameter);
|
||||
IEC104_Read_Cancel(s_core.read);
|
||||
IEC104_PeriodicScan_Cancel(s_core.scan);
|
||||
IEC104_SystemCommand_Cancel(s_core.system);
|
||||
if (IEC104_Slave_Close(s_core.slave, &s_core.error) != IEC104_TCP_OK) { return fail(); }
|
||||
s_core.reject_pending = false;
|
||||
if (!drain_events(256U)) { return fail(); }
|
||||
for (n = 0U; n < 8U; ++n) {
|
||||
if (s_core.gi.module != NULL) { (void)IEC104_Interrogation_NextEvent(s_core.gi.module, &s_core.gi.event); }
|
||||
if (s_core.ci.module != NULL) { (void)IEC104_Interrogation_NextEvent(s_core.ci.module, &s_core.ci.event); }
|
||||
if (s_core.command != NULL) { (void)IEC104_Command_NextEvent(s_core.command, &s_core.next_command); }
|
||||
if (s_core.remote != NULL) {
|
||||
(void)IEC104_RemoteParameter_Execute_Next(s_core.remote, &iec104_source_remote_handlers, NULL);
|
||||
}
|
||||
}
|
||||
#define CORE_DESTROY(member, function) \
|
||||
do { if ((member) != NULL) { if (function(member)) { (member) = NULL; } else { ok = false; } } } while (0)
|
||||
CORE_DESTROY(s_core.gi.module, IEC104_Interrogation_Destroy);
|
||||
CORE_DESTROY(s_core.ci.module, IEC104_Interrogation_Destroy);
|
||||
CORE_DESTROY(s_core.command, IEC104_Command_Destroy);
|
||||
CORE_DESTROY(s_core.clock, IEC104_ClockSynchronization_Destroy);
|
||||
CORE_DESTROY(s_core.remote, IEC104_RemoteParameter_Destroy);
|
||||
CORE_DESTROY(s_core.parameter, IEC104_Parameter_Destroy);
|
||||
CORE_DESTROY(s_core.read, IEC104_Read_Destroy);
|
||||
CORE_DESTROY(s_core.scan, IEC104_PeriodicScan_Destroy);
|
||||
CORE_DESTROY(s_core.system, IEC104_SystemCommand_Destroy);
|
||||
CORE_DESTROY(s_core.spontaneous, IEC104_Spontaneous_Destroy);
|
||||
#undef CORE_DESTROY
|
||||
if (!ok) { return fail(); }
|
||||
if (IEC104_Station_Is_Registered(&s_core.station) &&
|
||||
IEC104_Slave_Remove_Station(s_core.slave, &s_core.station) != IEC104_STATION_OK) { return fail(); }
|
||||
if (IEC104_Slave_Destroy(s_core.slave, &s_core.error) != IEC104_TCP_OK) { return fail(); }
|
||||
s_core.slave = NULL;
|
||||
s_core.status.owns_resources = 0U;
|
||||
s_core.status.connected = s_core.status.active = 0U;
|
||||
s_core.status.bound_port = 0U;
|
||||
s_core.closing = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 函数功能:同所有者只读复制状态,空指针无操作。 */
|
||||
void iec104_core_get_status(iec104_core_status_t *status)
|
||||
{
|
||||
if (status != NULL) { *status = s_core.status; }
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/* 模块名称:共享 IEC104 服务接口。 */
|
||||
/* Shared IEC104 runtime; one event-loop owner, no RTOS dependencies. */
|
||||
#ifndef IEC104_CORE_H
|
||||
#define IEC104_CORE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
const char *bind_address; /* Numeric IPv4, copied during start. */
|
||||
uint16_t port; /* 0 allows a host-assigned test port. */
|
||||
uint16_t common_address; /* 1..65534, frozen until stop. */
|
||||
} iec104_core_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t connections;
|
||||
uint32_t received_i_frames;
|
||||
uint32_t written_i_frames; /* Entire APDU handed to local TCP, not ACK. */
|
||||
uint32_t acknowledged_i_frames; /* IEC104 N(R), NOT TCP ACK. */
|
||||
uint32_t gi_completed; /* Business submission complete, not delivery. */
|
||||
uint32_t commands_completed;
|
||||
uint16_t bound_port;
|
||||
uint8_t running;
|
||||
uint8_t owns_resources; /* Includes failed cleanup; stop must be retried. */
|
||||
uint8_t connected;
|
||||
uint8_t active;
|
||||
int last_error;
|
||||
int last_stage;
|
||||
int native_error;
|
||||
} iec104_core_status_t;
|
||||
|
||||
/* All calls must be serialized on the same owner thread. 0=OK, -1=error.
|
||||
* start refuses double start; failure attempts bounded cleanup. If
|
||||
* owns_resources remains 1, stop must be retried before another start.
|
||||
* Each admitted start resets counters, errors, simulated values and RAM
|
||||
* settings; ordinary TCP disconnect preserves committed RAM values but
|
||||
* cancels SBO and uncommitted parameter transactions in the library.
|
||||
* poll never waits: one network turn, <=16 transport notifications, <=4
|
||||
* GI/CI interactions per priority, <=20 point reads per GI interaction.
|
||||
* Connection faults/reconnect are handled internally and return 0.
|
||||
* A -1 poll result is a service/assembly failure: caller must stop; polling
|
||||
* before start or after stop returns -1 without allocating resources.
|
||||
* stop is idempotent after success; failure retains ownership for retry.
|
||||
* Counters saturate at UINT32_MAX, survive TCP reconnect and stop, and reset
|
||||
* at the next admitted start. Connections counts accepted TCP sessions.
|
||||
* written_i_frames is observed when ACK/closing records are delivered;
|
||||
* it can lag the actual write. commands_completed counts the library's
|
||||
* FINISHED(COMPLETED), not model updates, TCP ACK or physical DO actions.
|
||||
* get_status is same-owner, read-only, and a NULL output is a no-op.
|
||||
* The linked IEC104_Net backend owns I/O and a monotonic clock independent
|
||||
* from the source wall clock. Source hooks must be short and nonblocking.
|
||||
*/
|
||||
int iec104_core_start(const iec104_core_config_t *config);
|
||||
int iec104_core_poll(void);
|
||||
int iec104_core_stop(void);
|
||||
void iec104_core_get_status(iec104_core_status_t *status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,93 @@
|
||||
/* 模块名称:DTU标准点表查询;作者:DTU项目组。 */
|
||||
#include "iec104_dtu_table.h"
|
||||
#include "iec104_dtu_profile.h"
|
||||
#include <string.h>
|
||||
/* Compile-time audit against the single upstream table, not hand-counted ranges. */
|
||||
typedef char profile_sp_count[(DTU_SIGNAL_COUNT == IEC104_DTU_SP_COUNT) ? 1 : -1];
|
||||
typedef char profile_me_count[(DTU_MEASUREMENT_COUNT == IEC104_DTU_ME_COUNT) ? 1 : -1];
|
||||
typedef char profile_param_count[(DTU_PARAMETER_COUNT == IEC104_DTU_PARAMETER_COUNT) ? 1 : -1];
|
||||
/* 函数功能:按稳定索引查询标准监视点元数据。 */
|
||||
bool iec104_dtu_point(size_t index, iec104_dtu_point_t *out)
|
||||
{
|
||||
if (out == NULL) { return false; }
|
||||
memset(out, 0, sizeof(*out));
|
||||
if (index < DTU_SIGNAL_COUNT) {
|
||||
out->ioa = dtu_signals[index].ioa;
|
||||
out->source_addr = dtu_signals[index].source_addr;
|
||||
out->type = 1U; out->name = dtu_signals[index].name; out->unit = "1";
|
||||
return true;
|
||||
}
|
||||
index -= DTU_SIGNAL_COUNT;
|
||||
if (index < IEC104_DTU_DP_COUNT) {
|
||||
out->ioa = (uint32_t)(0x1001U + index); out->type = 3U;
|
||||
out->name = "Simulated switch position"; out->unit = "DPI";
|
||||
return true;
|
||||
}
|
||||
index -= IEC104_DTU_DP_COUNT;
|
||||
if (index < DTU_MEASUREMENT_COUNT) {
|
||||
out->ioa = dtu_measurements[index].ioa;
|
||||
out->source_addr = dtu_measurements[index].source_addr;
|
||||
out->type = 13U; out->name = dtu_measurements[index].name;
|
||||
out->unit = dtu_measurements[index].unit;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/* 函数功能:读取保留原始类型和可见性的参数元数据。 */
|
||||
bool iec104_dtu_parameter(size_t index, iec104_dtu_parameter_t *out)
|
||||
{
|
||||
const DtuProfileParameter *p;
|
||||
if (index >= DTU_PARAMETER_COUNT || out == NULL) { return false; }
|
||||
p = &dtu_parameters[index];
|
||||
out->ioa = p->ioa; out->tag = p->tag; out->source_tag = p->source_tag;
|
||||
out->configured = p->configured; out->initial = p->value;
|
||||
out->area_scoped = p->ioa >= 0x8820U && p->ioa <= 0x88ffU;
|
||||
out->writable = p->configured && (out->area_scoped ||
|
||||
(p->ioa >= 0x8020U && p->ioa <= 0x8056U) ||
|
||||
(p->ioa >= 0xa2c1U && p->ioa <= 0xa2ecU) ||
|
||||
(p->ioa >= 0x9e01U && p->ioa <= 0x9e5dU));
|
||||
return true;
|
||||
}
|
||||
/* 函数功能:按参数IOA查索引,未知返回参数总数。 */
|
||||
size_t iec104_dtu_parameter_index(uint32_t ioa)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0U; i < DTU_PARAMETER_COUNT; ++i) {
|
||||
if (dtu_parameters[i].ioa == ioa) { break; }
|
||||
}
|
||||
return i;
|
||||
}
|
||||
/* 函数功能:依据原表配置状态和区域判定参数可见性。 */
|
||||
bool iec104_dtu_parameter_visible(size_t index, uint16_t area)
|
||||
{
|
||||
iec104_dtu_parameter_t p;
|
||||
return area < IEC104_DTU_AREA_COUNT && iec104_dtu_parameter(index, &p) &&
|
||||
p.configured && (area == 0U || p.area_scoped);
|
||||
}
|
||||
/* Candidate semantic bridge, not an activated platform binding. No mapping for alarm-enable (not overall enable),
|
||||
* low-frequency blocking (not underfrequency trip), zero drift (not ADC offset),
|
||||
* or remote-control permission (not protection global enable).
|
||||
* Existing model IDs 0..39 are stable persistent IDs; COUNT is not a parameter.
|
||||
* PT1 and phase-CT refer to the one existing feeder, secondary units A/V.
|
||||
* Other phases/bays MUST NOT silently share these IDs. */
|
||||
/* 函数功能:查询未激活的候选语义桥及单位比例。 */
|
||||
bool iec104_dtu_protection_mapping(uint32_t ioa, uint16_t *id, float *source_scale)
|
||||
{
|
||||
static const struct { uint16_t ioa, id; float scale; } map[] = {
|
||||
{0x8841U, 1U, 1.0f}, {0x8842U, 2U, 1.0f}, {0x8843U, 3U, 1000.0f},
|
||||
{0x8845U, 5U, 1.0f}, {0x8846U, 6U, 1.0f}, {0x8847U, 7U, 1000.0f},
|
||||
{0x8849U, 9U, 1.0f}, {0x884aU, 10U, 1.0f}, {0x884bU, 11U, 1000.0f},
|
||||
{0x8027U, 20U, 1.0f}, {0x8028U, 21U, 1000.0f},
|
||||
{0x8029U, 17U, 1.0f}, {0x802aU, 18U, 1000.0f},
|
||||
{0x8024U, 27U, 1000.0f}, {0x8025U, 28U, 1.0f},
|
||||
{0x8040U, 29U, 1.0f}, {0x8041U, 30U, 1.0f}
|
||||
/* 8046/8047 are load-limit alarms, not the existing overload trip
|
||||
* model. Do not map those by a superficially similar threshold. */
|
||||
};
|
||||
size_t i;
|
||||
if (id == NULL || source_scale == NULL) { return false; }
|
||||
for (i = 0U; i < sizeof(map) / sizeof(map[0]); ++i) {
|
||||
if (map[i].ioa == ioa) { *id = map[i].id; *source_scale = map[i].scale; return true; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/* 模块名称:DTU标准点表元数据接口;作者:DTU项目组。
|
||||
* Metadata is taken from the unmodified upstream DTU demonstration profile. */
|
||||
#ifndef IEC104_DTU_TABLE_H
|
||||
#define IEC104_DTU_TABLE_H
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#define IEC104_DTU_SP_COUNT 59U
|
||||
#define IEC104_DTU_DP_COUNT 16U
|
||||
#define IEC104_DTU_ME_COUNT 30U
|
||||
#define IEC104_DTU_ENERGY_COUNT 8U
|
||||
#define IEC104_DTU_PARAMETER_COUNT 430U
|
||||
#define IEC104_DTU_STANDARD_COUNT 120U
|
||||
#define IEC104_DTU_COMMAND_COUNT 65U
|
||||
#define IEC104_DTU_AREA_COUNT 5U
|
||||
#define IEC104_DTU_GI_COUNT 225U
|
||||
/* Point order: SP59, DP16, ME30. GI then appends standard parameters120. */
|
||||
typedef struct {
|
||||
uint32_t ioa;
|
||||
uint16_t source_addr;
|
||||
uint8_t type;
|
||||
const char *name;
|
||||
const char *unit;
|
||||
} iec104_dtu_point_t;
|
||||
typedef struct {
|
||||
uint32_t ioa;
|
||||
uint8_t tag, source_tag;
|
||||
bool configured, writable, area_scoped;
|
||||
const char *initial;
|
||||
} iec104_dtu_parameter_t;
|
||||
bool iec104_dtu_point(size_t index, iec104_dtu_point_t *out);
|
||||
bool iec104_dtu_parameter(size_t index, iec104_dtu_parameter_t *out);
|
||||
size_t iec104_dtu_parameter_index(uint32_t ioa);
|
||||
bool iec104_dtu_parameter_visible(size_t index, uint16_t area);
|
||||
/* IDs are existing prot_set_id_t values, not legacy communication addresses.
|
||||
* wire_value * source_scale = source engineering value. */
|
||||
bool iec104_dtu_protection_mapping(uint32_t ioa, uint16_t *id, float *source_scale);
|
||||
#endif
|
||||
@@ -0,0 +1,439 @@
|
||||
/* 模块名称:DTU数据源与模拟事务;作者:DTU项目组。 */
|
||||
#include "iec104_source.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <float.h>
|
||||
/* 430 metadata slots plus four additional banks of 88 action settings.
|
||||
* Raw binary32/uint32/uint16/Boolean bits are preserved, never stored as float. */
|
||||
#define ACTION_COUNT 88U
|
||||
#define VALUE_SLOTS (IEC104_DTU_PARAMETER_COUNT + 4U * ACTION_COUNT)
|
||||
#define SIM_QUALITY 0x20U
|
||||
#define NO_SOURCE_QUALITY 0x80U
|
||||
static uint32_t values[VALUE_SLOTS];
|
||||
static uint32_t staged[IEC104_DTU_PARAMETER_COUNT];
|
||||
static uint8_t dirty[(IEC104_DTU_PARAMETER_COUNT + 7U) / 8U];
|
||||
static IEC104_RemoteParameterRequest_t owner;
|
||||
static bool stage_active, stage_complete;
|
||||
static iec104_source_bindings_t bindings;
|
||||
static iec104_source_stats_t stats;
|
||||
static uint8_t singles[IEC104_DTU_SP_COUNT], single_set[IEC104_DTU_SP_COUNT];
|
||||
static uint8_t doubles[IEC104_DTU_DP_COUNT];
|
||||
static int8_t steps[8];
|
||||
static uint32_t bits[8];
|
||||
static float measurements[IEC104_DTU_ME_COUNT];
|
||||
static uint8_t measurement_set[IEC104_DTU_ME_COUNT];
|
||||
static IEC104_CommandPoint_t commands[IEC104_DTU_COMMAND_COUNT];
|
||||
static IEC104_ParameterPoint_t standard_points[IEC104_DTU_STANDARD_COUNT];
|
||||
static IEC104_ParameterValue_t standard_values[IEC104_DTU_STANDARD_COUNT];
|
||||
static uint64_t clock_utc, clock_mono;
|
||||
static iec104_source_change_t changes[17];
|
||||
static void increment(uint32_t *value) { if (*value != UINT32_MAX) { ++*value; } }
|
||||
static uint32_t float_bits(float v) { uint32_t b; memcpy(&b, &v, 4U); return b; }
|
||||
static float bits_float(uint32_t b) { float v; memcpy(&v, &b, 4U); return v; }
|
||||
/* 函数功能:将区域参数索引换算为压缩RAM槽位。 */
|
||||
static size_t slot(size_t index, uint16_t area)
|
||||
{
|
||||
size_t i, n = 0U; iec104_dtu_parameter_t p;
|
||||
if (area == 0U) { return index; }
|
||||
for (i = 0U; i < index; ++i) {
|
||||
(void)iec104_dtu_parameter(i, &p); if (p.area_scoped) { ++n; }
|
||||
}
|
||||
return IEC104_DTU_PARAMETER_COUNT + (area - 1U) * ACTION_COUNT + n;
|
||||
}
|
||||
/* 函数功能:丢弃预置和事务身份,不改变已固化值。 */
|
||||
static void clear_stage(void)
|
||||
{
|
||||
stage_active = false; stage_complete = false;
|
||||
memset(dirty, 0, sizeof(dirty)); memset(&owner, 0, sizeof(owner));
|
||||
}
|
||||
/* 函数功能:校验预置事务所属服务、连接和站点身份。 */
|
||||
static bool same_owner(const IEC104_RemoteParameterRequest_t *r)
|
||||
{
|
||||
return stage_active && owner.identity.slave == r->identity.slave &&
|
||||
owner.identity.instance_id == r->identity.instance_id && owner.identity.generation == r->identity.generation &&
|
||||
owner.instance_id == r->instance_id && owner.generation == r->generation &&
|
||||
owner.station_id == r->station_id && owner.common_address == r->common_address && owner.test == r->test;
|
||||
}
|
||||
static bool is_dirty(size_t i) { return (dirty[i / 8U] & (1U << (i % 8U))) != 0U; }
|
||||
/* 函数功能:判定参数是否由完整的真实源回调对负责。 */
|
||||
static bool mapped(const iec104_dtu_parameter_t *p, uint16_t area, uint16_t *id, float *scale)
|
||||
{
|
||||
return bindings.parameter_read != NULL && bindings.parameter_apply != NULL &&
|
||||
(!p->area_scoped || area == stats.current_area) && iec104_dtu_protection_mapping(p->ioa, id, scale);
|
||||
}
|
||||
/* 函数功能:读原始TLV位模式;真实源失败不降级为模拟值。 */
|
||||
static bool read_bits(size_t index, uint16_t area, uint32_t *out)
|
||||
{
|
||||
iec104_dtu_parameter_t p; uint16_t id; float scale, v;
|
||||
if (!iec104_dtu_parameter_visible(index, area) || !iec104_dtu_parameter(index, &p)) { return false; }
|
||||
if (mapped(&p, area, &id, &scale)) {
|
||||
if (!bindings.parameter_read(bindings.context, id, &v)) { return false; }
|
||||
v /= scale;
|
||||
if (!(v >= 0.0f && v <= 1000000.0f)) { return false; }
|
||||
if ((p.tag == 1U && v != 0.0f && v != 1.0f) ||
|
||||
(p.tag == 45U && (v > 65535.0f || (float)(uint32_t)v != v))) { return false; }
|
||||
*out = p.tag == 38U ? float_bits(v) : (uint32_t)v;
|
||||
} else { *out = values[slot(index, area)]; }
|
||||
return true;
|
||||
}
|
||||
/* 函数功能:按原始类型校验预置值和值域。 */
|
||||
static bool valid_bits(size_t index, uint32_t raw)
|
||||
{
|
||||
iec104_dtu_parameter_t p; double v;
|
||||
if (!iec104_dtu_parameter(index, &p) || !p.writable) { return false; }
|
||||
v = p.tag == 38U ? (double)bits_float(raw) : (double)raw;
|
||||
if (!(v >= 0.0)) { return false; }
|
||||
if ((p.tag == 1U && raw > 1U) || (p.tag == 45U && raw > 65535U) ||
|
||||
(p.tag == 38U && v > 1000000.0)) { return false; }
|
||||
if (p.ioa >= 0x9e01U && p.ioa <= 0x9e5dU && raw > 1U) { return false; }
|
||||
if (p.ioa >= 0xa2c1U && p.ioa <= 0xa2ecU && v <= 0.0) { return false; }
|
||||
if ((p.ioa == 0x8864U && v > 3.0) || (p.ioa == 0x802fU && v > 23.0)) { return false; }
|
||||
if (((p.ioa >= 0x8024U && p.ioa <= 0x8026U) || p.ioa == 0x802cU || p.ioa == 0x802dU ||
|
||||
(p.ioa >= 0x8040U && p.ioa <= 0x8045U) || p.ioa == 0x8054U || p.ioa == 0x8055U ||
|
||||
p.ioa == 0x8842U || p.ioa == 0x8846U || p.ioa == 0x884aU || p.ioa == 0x884eU ||
|
||||
p.ioa == 0x8852U) && v <= 0.0) { return false; }
|
||||
return p.tag == 1U || p.tag == 45U || p.tag == 35U || p.tag == 38U;
|
||||
}
|
||||
/* 函数功能:读预置候选或当前值供整批交叉校验。 */
|
||||
static bool candidate(uint32_t ioa, uint16_t area, float *out)
|
||||
{
|
||||
size_t i = iec104_dtu_parameter_index(ioa); uint32_t raw;
|
||||
if (i >= IEC104_DTU_PARAMETER_COUNT) { return false; }
|
||||
if (is_dirty(i)) { raw = staged[i]; } else if (!read_bits(i, area, &raw)) { return false; }
|
||||
*out = bits_float(raw); return *out >= 0.0f && *out <= 1000000.0f;
|
||||
}
|
||||
/* 函数功能:完整校验后原子提交真实源,再发布RAM候选。 */
|
||||
static bool commit_stage(uint16_t area)
|
||||
{
|
||||
size_t i, n = 0U; iec104_dtu_parameter_t p; uint16_t id; float scale;
|
||||
float high, low, residual;
|
||||
if (!candidate(0x8833U, area, &high) || !candidate(0x8834U, area, &low) ||
|
||||
!candidate(0x8835U, area, &residual) || high < low || low < residual) { return false; }
|
||||
if (area == 0U && (!candidate(0x8027U, 0U, &low) ||
|
||||
!candidate(0x8029U, 0U, &high) || low > high)) { return false; }
|
||||
for (i = 0U; i < IEC104_DTU_PARAMETER_COUNT; ++i) {
|
||||
if (!is_dirty(i)) { continue; }
|
||||
if (!valid_bits(i, staged[i])) { return false; }
|
||||
(void)iec104_dtu_parameter(i, &p);
|
||||
if (mapped(&p, area, &id, &scale)) {
|
||||
if (n >= sizeof(changes) / sizeof(changes[0])) { return false; }
|
||||
changes[n].id = id;
|
||||
changes[n++].value = (p.tag == 38U ? bits_float(staged[i]) : (float)staged[i]) * scale;
|
||||
}
|
||||
}
|
||||
if (n != 0U && (!bindings.parameter_apply(bindings.context, changes, n, true) ||
|
||||
!bindings.parameter_apply(bindings.context, changes, n, false))) { return false; }
|
||||
for (i = 0U; i < IEC104_DTU_PARAMETER_COUNT; ++i) {
|
||||
if (is_dirty(i)) { values[slot(i, area)] = staged[i]; }
|
||||
}
|
||||
increment(&stats.parameter_commits); return true;
|
||||
}
|
||||
/* 函数功能:服务启动恢复typed表默认,观测归零且无源置IV。 */
|
||||
void iec104_source_init(void)
|
||||
{
|
||||
size_t i, a, g, m; iec104_dtu_parameter_t p; uint32_t raw;
|
||||
memset(&bindings, 0, sizeof(bindings)); memset(&stats, 0, sizeof(stats));
|
||||
memset(values, 0, sizeof(values)); memset(singles, 0, sizeof(singles));
|
||||
memset(single_set, 0, sizeof(single_set)); memset(doubles, 0, sizeof(doubles));
|
||||
memset(steps, 0, sizeof(steps)); memset(bits, 0, sizeof(bits));
|
||||
memset(measurements, 0, sizeof(measurements)); memset(measurement_set, 0, sizeof(measurement_set));
|
||||
clear_stage(); clock_utc = UINT64_C(946684800000); clock_mono = 0U;
|
||||
for (i = 0U; i < IEC104_DTU_PARAMETER_COUNT; ++i) {
|
||||
(void)iec104_dtu_parameter(i, &p);
|
||||
if (!p.configured) { continue; }
|
||||
raw = p.tag == 38U ? float_bits((float)strtod(p.initial, NULL)) : (uint32_t)strtoul(p.initial, NULL, 10);
|
||||
for (a = 0U; a < IEC104_DTU_AREA_COUNT; ++a) {
|
||||
if (iec104_dtu_parameter_visible(i, (uint16_t)a)) { values[slot(i, (uint16_t)a)] = raw; }
|
||||
}
|
||||
}
|
||||
memset(standard_values, 0, sizeof(standard_values));
|
||||
for (i = 0U; i < IEC104_DTU_STANDARD_COUNT; ++i) {
|
||||
g = i / 30U; m = i % 30U;
|
||||
standard_points[i].ioa = (uint32_t)(0x8601U + g * 0x20U + m);
|
||||
standard_points[i].kind = g == 0U ? IEC60870_ASDU_KPA_THRESHOLD :
|
||||
(g == 1U ? IEC60870_ASDU_KPA_LOW_LIMIT :
|
||||
(g == 2U ? IEC60870_ASDU_KPA_HIGH_LIMIT : IEC60870_ASDU_KPA_SMOOTHING));
|
||||
standard_points[i].type_id = g == 3U ? 112U : (g == 0U && m >= 4U ? 110U : 111U);
|
||||
if (g == 0U) { standard_values[i].scaled = m >= 4U ? 655 : 200; }
|
||||
if (g == 1U) { standard_values[i].scaled = -10000; }
|
||||
if (g == 2U) { standard_values[i].scaled = 10000; }
|
||||
}
|
||||
}
|
||||
/* 函数功能:启动后、首轮poll前复制平台数据源回调表。 */
|
||||
void iec104_source_bind(const iec104_source_bindings_t *b)
|
||||
{
|
||||
if (b == NULL) { memset(&bindings, 0, sizeof(bindings)); } else { bindings = *b; }
|
||||
}
|
||||
size_t iec104_source_count(void) { return IEC104_DTU_GI_COUNT; }
|
||||
/* 函数功能:构造GI对象,保留真实品质并明确模拟身份。 */
|
||||
bool iec104_source_point(size_t index, iec104_source_object_t *out)
|
||||
{
|
||||
iec104_dtu_point_t p; uint8_t v = 0U, q = NO_SOURCE_QUALITY; float f = 0.0f; size_t n;
|
||||
if (out == NULL || index >= IEC104_DTU_GI_COUNT) { return false; }
|
||||
memset(out, 0, sizeof(*out));
|
||||
if (index < 105U) {
|
||||
if (!iec104_dtu_point(index, &p)) { return false; }
|
||||
if (p.type == 1U) {
|
||||
if (bindings.signal != NULL && !bindings.signal(bindings.context, p.source_addr, &v, &q)) {
|
||||
v = 0U; q = NO_SOURCE_QUALITY;
|
||||
}
|
||||
if (single_set[index]) { v = singles[index]; q = SIM_QUALITY; }
|
||||
return IEC60870_SinglePointInformation_Create(&out->single, p.ioa, v, q) == IEC60870_ASDU_OK;
|
||||
}
|
||||
if (p.type == 3U) {
|
||||
n = index - 59U; v = doubles[n]; q = v == 0U ? NO_SOURCE_QUALITY : SIM_QUALITY;
|
||||
return IEC60870_DoublePointInformation_Create(&out->double_point, p.ioa, v, q) == IEC60870_ASDU_OK;
|
||||
}
|
||||
n = index - 75U;
|
||||
if (bindings.measurement != NULL && !bindings.measurement(bindings.context, p.source_addr, &f, &q)) {
|
||||
f = 0.0f; q = NO_SOURCE_QUALITY;
|
||||
}
|
||||
if (measurement_set[n]) { f = measurements[n]; q = SIM_QUALITY; }
|
||||
return IEC60870_MeasuredValueShort_Create(&out->measurement, p.ioa, f, q) == IEC60870_ASDU_OK;
|
||||
}
|
||||
n = index - 105U;
|
||||
if (standard_points[n].type_id == 110U) {
|
||||
return IEC60870_ParameterNormalizedValue_Create(&out->normalized, standard_points[n].ioa,
|
||||
standard_values[n].normalized, standard_points[n].kind) == IEC60870_ASDU_OK;
|
||||
}
|
||||
if (standard_points[n].type_id == 111U) {
|
||||
return IEC60870_ParameterScaledValue_Create(&out->scaled, standard_points[n].ioa,
|
||||
standard_values[n].scaled, standard_points[n].kind) == IEC60870_ASDU_OK;
|
||||
}
|
||||
return IEC60870_ParameterFloatValue_Create(&out->parameter, standard_points[n].ioa,
|
||||
standard_values[n].short_float, standard_points[n].kind) == IEC60870_ASDU_OK;
|
||||
}
|
||||
/* 函数功能:构造无源电度点的零值和无效品质。 */
|
||||
bool iec104_source_counter(size_t index, iec104_source_object_t *out)
|
||||
{
|
||||
if (index >= 8U || out == NULL) { return false; }
|
||||
return IEC60870_IntegratedTotalsShort_Create(&out->energy, (uint32_t)(0x6401U + index),
|
||||
0.0f, NO_SOURCE_QUALITY) == IEC60870_ASDU_OK;
|
||||
}
|
||||
/* 函数功能:查运行可见IOA,诊断别名和未知地址不伪造零值成功。 */
|
||||
bool iec104_source_find(uint32_t ioa, iec104_source_object_t *out)
|
||||
{
|
||||
size_t i; iec104_dtu_point_t p;
|
||||
if (out == NULL) { return false; }
|
||||
for (i = 0U; i < 105U; ++i) {
|
||||
if (iec104_dtu_point(i, &p) && p.ioa == ioa) { return iec104_source_point(i, out); }
|
||||
}
|
||||
for (i = 0U; i < 120U; ++i) {
|
||||
if (standard_points[i].ioa == ioa) { return iec104_source_point(i + 105U, out); }
|
||||
}
|
||||
if (ioa >= 0x1201U && ioa <= 0x1208U) {
|
||||
return IEC60870_StepPositionInformation_Create(&out->step, ioa, steps[ioa - 0x1201U], 0U,
|
||||
SIM_QUALITY) == IEC60870_ASDU_OK;
|
||||
}
|
||||
if (ioa >= 0x1301U && ioa <= 0x1308U) {
|
||||
return IEC60870_BitString32_Create(&out->bits, ioa, bits[ioa - 0x1301U], SIM_QUALITY) == IEC60870_ASDU_OK;
|
||||
}
|
||||
return ioa >= 0x6401U && ioa <= 0x6408U && iec104_source_counter(ioa - 0x6401U, out);
|
||||
}
|
||||
const IEC104_CommandPoint_t *iec104_source_commands(uint16_t ca, size_t *count)
|
||||
{
|
||||
size_t i; uint8_t type;
|
||||
for (i = 0U; i < 65U; ++i) {
|
||||
commands[i].common_address = ca;
|
||||
if (i < 19U) { commands[i].ioa = (uint32_t)(0x6001U + i); type = i >= 1U && i <= 16U ? 46U : 45U; }
|
||||
else if (i < 27U) { commands[i].ioa = (uint32_t)(0x6101U + i - 19U); type = 47U; }
|
||||
else if (i < 57U) { commands[i].ioa = (uint32_t)(0x6201U + i - 27U); type = 49U; }
|
||||
else { commands[i].ioa = (uint32_t)(0x6301U + i - 57U); type = 51U; }
|
||||
commands[i].type_mask = IEC104_COMMAND_TYPE_BIT(type) | IEC104_COMMAND_TYPE_BIT(type + 13U);
|
||||
commands[i].mode = type == 51U ? IEC104_COMMAND_MODE_DIRECT : IEC104_COMMAND_MODE_SELECT_EXECUTE;
|
||||
}
|
||||
if (count != NULL) { *count = 65U; } return commands;
|
||||
}
|
||||
/* 函数功能:改变模拟遥信并标识替代源,绝不驱动物理出口。 */
|
||||
static void set_single(uint32_t ioa, uint8_t v)
|
||||
{
|
||||
size_t i; iec104_dtu_point_t p;
|
||||
for (i = 0U; i < 59U; ++i) {
|
||||
(void)iec104_dtu_point(i, &p);
|
||||
if (p.ioa == ioa) { singles[i] = v; single_set[i] = 1U; break; }
|
||||
}
|
||||
}
|
||||
/* 函数功能:选择不动作,执行修改可观测模拟状态。 */
|
||||
IEC104_CommandActionResult_t iec104_source_command(void *context, const IEC104_CommandRequest_t *r)
|
||||
{
|
||||
size_t n; uint8_t type;
|
||||
(void)context;
|
||||
if (r == NULL) { return IEC104_COMMAND_ACTION_NOT_STARTED; }
|
||||
type = r->type_id >= 58U ? (uint8_t)(r->type_id - 13U) : r->type_id;
|
||||
if (r->is_select) { return IEC104_COMMAND_ACTION_STARTED; }
|
||||
if (r->ioa >= 0x6002U && r->ioa <= 0x6011U && type == 46U && (r->state == 1U || r->state == 2U)) {
|
||||
n = r->ioa - 0x6002U; doubles[n] = r->state;
|
||||
if (n == 0U) { set_single(0x11U, r->state == 2U); set_single(0x28U, r->state == 1U); }
|
||||
} else if (type == 45U && r->ioa == 0x6001U) { set_single(9U, r->state); }
|
||||
else if (type == 45U && r->ioa == 0x6013U) { set_single(0x26U, r->state); }
|
||||
else if (type == 45U && r->ioa == 0x6012U) {
|
||||
/* Reset only the simulated latch. Never resets real protection state. */
|
||||
set_single(0x16U, 0U); increment(&stats.reset_actions);
|
||||
} else if (type == 47U && r->ioa >= 0x6101U && r->ioa <= 0x6108U) {
|
||||
if (r->state != 1U && r->state != 2U) { return IEC104_COMMAND_ACTION_NOT_STARTED; }
|
||||
n = r->ioa - 0x6101U;
|
||||
if ((r->state == 1U && steps[n] == -64) || (r->state == 2U && steps[n] == 63)) { return IEC104_COMMAND_ACTION_NOT_STARTED; }
|
||||
steps[n] = (int8_t)(steps[n] + (r->state == 2U ? 1 : -1));
|
||||
} else if (type == 49U && r->ioa >= 0x6201U && r->ioa <= 0x621eU) {
|
||||
n = r->ioa - 0x6201U;
|
||||
if (r->value.scaled < -10000 || r->value.scaled > 10000) { return IEC104_COMMAND_ACTION_NOT_STARTED; }
|
||||
measurements[n] = (float)r->value.scaled; measurement_set[n] = 1U;
|
||||
} else if (type == 51U && r->ioa >= 0x6301U && r->ioa <= 0x6308U) { bits[r->ioa - 0x6301U] = r->value.bitstring; }
|
||||
else { return IEC104_COMMAND_ACTION_NOT_STARTED; }
|
||||
increment(&stats.simulated_actions); return IEC104_COMMAND_ACTION_STARTED;
|
||||
}
|
||||
/* 函数功能:列举动作反馈IOA;容量不足不写,WAIT不重执行动作。 */
|
||||
size_t iec104_source_command_feedback(uint32_t ioa, uint32_t *out, size_t capacity)
|
||||
{
|
||||
uint32_t first; size_t n = 1U;
|
||||
if (out == NULL || capacity == 0U) { return 0U; }
|
||||
if (ioa >= 0x6002U && ioa <= 0x6011U) { first = 0x1001U + ioa - 0x6002U; if (ioa == 0x6002U) { n = 3U; } }
|
||||
else if (ioa == 0x6001U) { first = 9U; }
|
||||
else if (ioa == 0x6012U) { first = 0x16U; }
|
||||
else if (ioa == 0x6013U) { first = 0x26U; }
|
||||
else if (ioa >= 0x6101U && ioa <= 0x6108U) { first = 0x1201U + ioa - 0x6101U; }
|
||||
else if (ioa >= 0x6201U && ioa <= 0x621eU) { first = 0x4001U + ioa - 0x6201U; }
|
||||
else if (ioa >= 0x6301U && ioa <= 0x6308U) { first = 0x1301U + ioa - 0x6301U; }
|
||||
else { return 0U; }
|
||||
if (capacity < n) { return 0U; }
|
||||
out[0] = first; if (n == 3U) { out[1] = 0x11U; out[2] = 0x28U; } return n;
|
||||
}
|
||||
/* 函数功能:按原始TLV类型和小端顺序读取已固化参数。 */
|
||||
bool iec104_source_remote_read(uint16_t area, uint32_t ioa, iec104_source_value_t *out)
|
||||
{
|
||||
size_t i = iec104_dtu_parameter_index(ioa), b; uint32_t raw;
|
||||
iec104_dtu_parameter_t p;
|
||||
if (out == NULL || !iec104_dtu_parameter(i, &p) || !read_bits(i, area, &raw)) { return false; }
|
||||
out->tag = p.tag; out->length = p.tag == 1U ? 1U : (p.tag == 45U ? 2U : 4U);
|
||||
for (b = 0U; b < 4U; ++b) { out->bytes[b] = (uint8_t)(raw >> (8U * b)); }
|
||||
return true;
|
||||
}
|
||||
static IEC104_RemoteParameterLookupResult_t remote_lookup(void *context,
|
||||
const IEC104_RemoteParameterRequest_t *r, size_t ordinal, uint32_t ioa,
|
||||
IEC104_RemoteParameterFrame_t *frame)
|
||||
{
|
||||
size_t i, n = 0U; iec104_dtu_parameter_t p; iec104_source_value_t value;
|
||||
IEC104_RemoteParameterAddResult_t result;
|
||||
(void)context;
|
||||
if (r->sn >= IEC104_DTU_AREA_COUNT) { return IEC104_REMOTE_PARAMETER_LOOKUP_ERROR; }
|
||||
if (r->count == 0U) {
|
||||
for (i = 0U; i < IEC104_DTU_PARAMETER_COUNT; ++i) {
|
||||
if (iec104_dtu_parameter_visible(i, r->sn) && n++ == ordinal) { break; }
|
||||
}
|
||||
if (i == IEC104_DTU_PARAMETER_COUNT) { return IEC104_REMOTE_PARAMETER_LOOKUP_END; }
|
||||
(void)iec104_dtu_parameter(i, &p); ioa = p.ioa;
|
||||
}
|
||||
if (!iec104_source_remote_read(r->sn, ioa, &value)) { return IEC104_REMOTE_PARAMETER_LOOKUP_UNKNOWN; }
|
||||
result = IEC104_RemoteParameter_Add_Element(frame, ioa, value.tag, value.bytes, value.length);
|
||||
if (result == IEC104_REMOTE_PARAMETER_ADD_FULL) { return IEC104_REMOTE_PARAMETER_LOOKUP_FULL; }
|
||||
return result == IEC104_REMOTE_PARAMETER_ADD_OK ? IEC104_REMOTE_PARAMETER_LOOKUP_ADDED : IEC104_REMOTE_PARAMETER_LOOKUP_ERROR;
|
||||
}
|
||||
static IEC104_RemoteParameterActionResult_t remote_action(void *context,
|
||||
const IEC104_RemoteParameterRequest_t *r, IEC104_RemoteParameterArea_t *area)
|
||||
{
|
||||
size_t e, i, b; uint32_t raw; iec104_dtu_parameter_t p;
|
||||
IEC104_RemoteParameterElement_t element;
|
||||
bool ok;
|
||||
(void)context;
|
||||
if (r == NULL) { return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
if (r->operation == IEC104_REMOTE_PARAMETER_OP_CANCEL || r->operation == IEC104_REMOTE_PARAMETER_OP_DISCARD) {
|
||||
clear_stage(); return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
||||
}
|
||||
if (r->operation == IEC104_REMOTE_PARAMETER_OP_READ_AREA && area != NULL) {
|
||||
area->current = stats.current_area; area->minimum = 0U; area->maximum = 4U;
|
||||
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
||||
}
|
||||
if (r->sn >= 5U || r->test) { return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
if (r->operation == IEC104_REMOTE_PARAMETER_OP_SWITCH_AREA) {
|
||||
/* A bound real model has a single bank; switching without an atomic
|
||||
* whole-bank platform transaction would change the meaning of live data. */
|
||||
if (stage_active || bindings.parameter_read != NULL) { return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
stats.current_area = r->sn; return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
||||
}
|
||||
if (r->operation == IEC104_REMOTE_PARAMETER_OP_SOLIDIFY) {
|
||||
ok = same_owner(r) && owner.sn == r->sn && stage_complete && commit_stage(r->sn);
|
||||
clear_stage(); return ok ? IEC104_REMOTE_PARAMETER_ACTION_APPLIED : IEC104_REMOTE_PARAMETER_ACTION_REJECTED;
|
||||
}
|
||||
if (r->operation != IEC104_REMOTE_PARAMETER_OP_PRESET) { return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
if (r->first_frame) { clear_stage(); owner = *r; owner._data = NULL; owner._length = 0U; stage_active = true; }
|
||||
if (!same_owner(r) || owner.sn != r->sn || stage_complete) { clear_stage(); return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
for (e = 0U; e < r->count; ++e) {
|
||||
if (!IEC104_RemoteParameter_Get_Element(r, e, &element)) { clear_stage(); return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
i = iec104_dtu_parameter_index(element.ioa);
|
||||
if (!iec104_dtu_parameter_visible(i, r->sn)) { clear_stage(); return IEC104_REMOTE_PARAMETER_ACTION_UNKNOWN_IOA; }
|
||||
(void)iec104_dtu_parameter(i, &p);
|
||||
if (!p.writable || element.tag != p.tag || element.length != (p.tag == 1U ? 1U : (p.tag == 45U ? 2U : 4U)) || is_dirty(i)) {
|
||||
clear_stage(); return IEC104_REMOTE_PARAMETER_ACTION_REJECTED;
|
||||
}
|
||||
raw = 0U;
|
||||
for (b = 0U; b < element.length; ++b) { raw |= (uint32_t)element.value[b] << (8U * b); }
|
||||
if (!valid_bits(i, raw)) { clear_stage(); return IEC104_REMOTE_PARAMETER_ACTION_REJECTED; }
|
||||
staged[i] = raw; dirty[i / 8U] |= (uint8_t)(1U << (i % 8U));
|
||||
}
|
||||
stage_complete = !r->more_follows;
|
||||
return IEC104_REMOTE_PARAMETER_ACTION_APPLIED;
|
||||
}
|
||||
const IEC104_RemoteParameterHandlers_t iec104_source_remote_handlers = {remote_action, remote_lookup};
|
||||
/* 函数功能:装配标准参数模块借用的稳定点表和值存储。 */
|
||||
void iec104_source_parameter_config(uint16_t ca, IEC104_ParameterConfig_t *config)
|
||||
{
|
||||
size_t i;
|
||||
if (config == NULL) { return; }
|
||||
IEC104_Parameter_Config_Default(config);
|
||||
for (i = 0U; i < 120U; ++i) { standard_points[i].common_address = ca; }
|
||||
config->points = standard_points; config->values = standard_values; config->point_count = 120U;
|
||||
}
|
||||
/* 函数功能:校验标准参数装载;TEST不改值,周期许可由核心执行。 */
|
||||
IEC104_ParameterActionResult_t iec104_source_parameter_action(void *context, const IEC104_ParameterRequest_t *r)
|
||||
{
|
||||
size_t i, m, g; bool valid = false;
|
||||
(void)context;
|
||||
if (r == NULL || r->phase != IEC104_PARAMETER_PHASE_EXECUTE) { return IEC104_PARAMETER_ACTION_REJECTED; }
|
||||
if (r->operation != IEC104_PARAMETER_OP_LOAD) {
|
||||
/* QPA=3 selects periodic transmission; no value mutation is needed.
|
||||
* Core owns scheduling and must apply its scan enable after acceptance. */
|
||||
return r->qpa == 3U && r->ioa == 0U ? IEC104_PARAMETER_ACTION_APPLIED : IEC104_PARAMETER_ACTION_REJECTED;
|
||||
}
|
||||
i = r->point_index;
|
||||
if (i >= 120U || standard_points[i].ioa != r->ioa || standard_points[i].type_id != r->type_id) { return IEC104_PARAMETER_ACTION_UNKNOWN_IOA; }
|
||||
m = i % 30U; g = i / 30U;
|
||||
if (g == 0U) { valid = (r->type_id == 110U ? r->value.normalized : r->value.scaled) >= 0; }
|
||||
if (g == 1U) { valid = r->value.scaled >= -10000 && r->value.scaled <= standard_values[60U + m].scaled; }
|
||||
if (g == 2U) { valid = r->value.scaled <= 10000 && r->value.scaled >= standard_values[30U + m].scaled; }
|
||||
if (g == 3U) { valid = r->value.short_float >= 0.0f && r->value.short_float <= 60.0f; }
|
||||
if (!valid) { return IEC104_PARAMETER_ACTION_REJECTED; }
|
||||
if (!r->test) { standard_values[i] = r->value; increment(&stats.parameter_commits); }
|
||||
return IEC104_PARAMETER_ACTION_APPLIED;
|
||||
}
|
||||
/* 函数功能:设置仅设备虚拟墙钟的UTC纪元及单调基点。 */
|
||||
void iec104_source_clock_seed(uint64_t utc_ms, uint64_t mono_ms)
|
||||
{
|
||||
clock_utc = utc_ms; clock_mono = mono_ms;
|
||||
}
|
||||
/* 函数功能:按同一单调时基推进设备虚拟UTC毫秒。 */
|
||||
uint64_t iec104_source_clock_ms(uint64_t mono_ms)
|
||||
{
|
||||
return clock_utc + (mono_ms >= clock_mono ? mono_ms - clock_mono : 0U);
|
||||
}
|
||||
/* 函数功能:应用成熟校时模块换算结果;不调用操作系统设时。 */
|
||||
bool iec104_source_clock_set(void *context, const IEC104_ClockSynchronizationRequest_t *r)
|
||||
{
|
||||
(void)context;
|
||||
if (r == NULL) { return false; }
|
||||
iec104_source_clock_seed(r->utc_ms, r->base_ms); stats.synchronized = true; increment(&stats.clock_sets);
|
||||
return true;
|
||||
}
|
||||
/* 函数功能:以固定UTC+8日历读设备墙钟,未校时保留IV。 */
|
||||
bool iec104_source_clock_read(void *context, const IEC104_ClockSynchronizationReadRequest_t *r, IEC60870_CP56Time2a_t *out)
|
||||
{
|
||||
uint64_t ms, seconds;
|
||||
(void)context;
|
||||
if (r == NULL || out == NULL) { return false; }
|
||||
ms = iec104_source_clock_ms(r->base_ms);
|
||||
seconds = ms / 1000U;
|
||||
if (seconds > IEC60870_TIME_MAX_SECONDS) { return false; }
|
||||
memset(out, 0, sizeof(*out));
|
||||
if (!IEC60870_Time_Seconds_To_Local((uint32_t)(ms / 1000U), (uint16_t)(ms % 1000U), &out->time)) { return false; }
|
||||
out->has_weekday = 1U; out->is_invalid = stats.synchronized ? 0U : 1U; return true;
|
||||
}
|
||||
void iec104_source_stats(iec104_source_stats_t *out) { if (out != NULL) { *out = stats; } }
|
||||
@@ -0,0 +1,77 @@
|
||||
/* 模块名称:DTU数据源接口;作者:DTU项目组。
|
||||
* Single-owner application data. No sockets, hardware outputs or allocation. */
|
||||
#ifndef IEC104_SOURCE_H
|
||||
#define IEC104_SOURCE_H
|
||||
#include "iec104_dtu_table.h"
|
||||
#include "IEC104_Command.h"
|
||||
#include "IEC104_RemoteParameter.h"
|
||||
#include "IEC104_Parameter.h"
|
||||
#include "IEC104_ClockSynchronization.h"
|
||||
/* InformationObject is only a base class. The caller must retain full storage
|
||||
* until Add_InformationObject has copied it; never allocate the base alone. */
|
||||
typedef union {
|
||||
IEC60870_InformationObject_t object;
|
||||
IEC60870_SinglePointInformation_t single;
|
||||
IEC60870_DoublePointInformation_t double_point;
|
||||
IEC60870_MeasuredValueShort_t measurement;
|
||||
IEC60870_ParameterNormalizedValue_t normalized;
|
||||
IEC60870_ParameterScaledValue_t scaled;
|
||||
IEC60870_ParameterFloatValue_t parameter;
|
||||
IEC60870_StepPositionInformation_t step;
|
||||
IEC60870_BitString32_t bits;
|
||||
IEC60870_IntegratedTotalsShort_t energy;
|
||||
} iec104_source_object_t;
|
||||
/* All 345 visible profile entries have numeric tags 1/35/38/45 and width <=4.
|
||||
* Tag4 metadata remains unconfigured and invisible, never silently truncated. */
|
||||
typedef struct { uint8_t tag, length, bytes[4]; } iec104_source_value_t;
|
||||
typedef struct { uint16_t id; float value; } iec104_source_change_t;
|
||||
/* All callbacks run serially in the protocol owner. NULL means unavailable.
|
||||
* read returns false only for unavailable input; quality is never normalized.
|
||||
* apply must validate the whole list before changing anything and be atomic.
|
||||
* The platform must not call per-item setters that can fail after partial writes.
|
||||
* init does not initialize/reset protection/calibration or invoke apply. */
|
||||
typedef struct {
|
||||
void *context;
|
||||
bool (*measurement)(void *, uint16_t source_addr, float *, uint8_t *quality);
|
||||
bool (*signal)(void *, uint16_t source_addr, uint8_t *, uint8_t *quality);
|
||||
bool (*parameter_read)(void *, uint16_t semantic_id, float *);
|
||||
bool (*parameter_apply)(void *, const iec104_source_change_t *, size_t, bool validate_only);
|
||||
} iec104_source_bindings_t;
|
||||
typedef struct {
|
||||
uint32_t simulated_actions, physical_do_actions, parameter_commits, clock_sets;
|
||||
uint32_t reset_actions;
|
||||
uint16_t current_area;
|
||||
bool synchronized;
|
||||
} iec104_source_stats_t;
|
||||
/* core_start calls init exactly once: clears bindings, stages, stats, virtual
|
||||
* clock and observations; restores typed profile/standard parameter defaults.
|
||||
* TCP reconnect does NOT call init. RAM solidification is not persistence. */
|
||||
void iec104_source_init(void);
|
||||
/* Copies the vtable. Context must outlive the service. Bind only immediately
|
||||
* after start and before poll, never while a command/parameter batch is active.
|
||||
* Register parameter_read/apply as a pair. validate_only is side-effect free;
|
||||
* failed apply MUST leave the entire real source unchanged. Persistence remains
|
||||
* defaultTask-owned; this callback is not permission for direct config I/O. */
|
||||
void iec104_source_bind(const iec104_source_bindings_t *bindings);
|
||||
size_t iec104_source_count(void);
|
||||
bool iec104_source_point(size_t index, iec104_source_object_t *out);
|
||||
bool iec104_source_find(uint32_t ioa, iec104_source_object_t *out);
|
||||
bool iec104_source_counter(size_t index, iec104_source_object_t *out);
|
||||
const IEC104_CommandPoint_t *iec104_source_commands(uint16_t ca, size_t *count);
|
||||
IEC104_CommandActionResult_t iec104_source_command(void *context, const IEC104_CommandRequest_t *request);
|
||||
/* A successful execute has already changed state. Core reports success then
|
||||
* publishes reads of the affected IOAs, and only then completes the command. */
|
||||
/* At most three IOAs. Insufficient capacity returns zero without writing.
|
||||
* Network WAIT retries publication only, never the already-executed action. */
|
||||
size_t iec104_source_command_feedback(uint32_t command_ioa, uint32_t *ioas, size_t capacity);
|
||||
extern const IEC104_RemoteParameterHandlers_t iec104_source_remote_handlers;
|
||||
void iec104_source_parameter_config(uint16_t ca, IEC104_ParameterConfig_t *config);
|
||||
IEC104_ParameterActionResult_t iec104_source_parameter_action(void *, const IEC104_ParameterRequest_t *);
|
||||
bool iec104_source_remote_read(uint16_t area, uint32_t ioa, iec104_source_value_t *out);
|
||||
bool iec104_source_clock_set(void *, const IEC104_ClockSynchronizationRequest_t *);
|
||||
bool iec104_source_clock_read(void *, const IEC104_ClockSynchronizationReadRequest_t *, IEC60870_CP56Time2a_t *);
|
||||
/* Virtual device wall clock only; mono_ms and base_ms use the same Net epoch. */
|
||||
uint64_t iec104_source_clock_ms(uint64_t mono_ms);
|
||||
void iec104_source_clock_seed(uint64_t utc_ms, uint64_t mono_ms);
|
||||
void iec104_source_stats(iec104_source_stats_t *out);
|
||||
#endif
|
||||
Reference in New Issue
Block a user