2e9272cc8a
引入成熟 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 工程改动,未烧板。
440 lines
24 KiB
C
440 lines
24 KiB
C
/* 模块名称: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; } }
|