Files
STM32F4-Base/App/iec60870/104/iec104_source.h
T
wandering1 7d1db02164 perf(iec104): 参数区按 17 项真实消费者收窄并接上真实定值模型
104 协议侧参数通道此前镜像了厂商点表的全部 430 条参数(697 槽),
但其中 413 条在本设备上没有任何消费者——104 与设备侧真实定值模型
prot_set 之间零调用,主站改定值只落在无人读的 RAM 镜像里,保护功能
毫无感知。本次按"严格按消费者收窄"口径收口:

可见性以 iec104_dtu_protection_mapping() 语义桥表为唯一判据,只放行
三段过流投退/定值/延时(9 项)、低压与过压定值/延时(4 项)、PT 与
CT 一次/二次额定(4 项)。其余参数在位表枚举层即不可见,主站读取
得到 COT=47 + P/N=1 的显式否定确认,不静默丢弃。values[697]→values[53]、
staged[264]→staged[17],并删除生成式 iec104_source_ranks.h。

桥表接真实源:iec104_serv 的 parameter_read/apply 从 NULL 改为直读
prot_set 与整批原子提交,Q10 量化后一次事务落地,持久化仍由
defaultTask 的脏标志闭环负责。

prot_set 因此出现两个运行时写者(netTask 的 104 与 uiTask 的本地整
定),原"写者靠启动时序串行"的约定失效。加入写事务互斥与 seqlock
序号:四个写入口全部进锁,校验与提前返回一律在 enter 之前;序号卡
奇数连续 8 拍即判保护闭锁,避免自检被永久跳过而静默失去防篡改能力。
锁的安装在 defaultTask 的存储 bring-up 阶段,不在 app_main_init——
后者早于 osKernelInitialize,内核启动前禁止任何 RTOS 创建类 API。

实测:RW_IRAM1 117,304→113,720 B(−3,584 B,89.50%→86.76%);
ER_IROM1 413,392→411,668 B(−1,724 B);iec104_source.o 5,656→2,056 B;
prot_set.o 344→352 B(+8 B,写事务序号与锁句柄)。Keil 构建 0 错误
0 警告;主机侧 prot_set 175/175、iec104_dtu 7/7;真实 TCP 验收
13/13;暂存口径门禁 18 项全通过。

已知边界:模拟器不链接 iec104_serv.c,TCP 验收不覆盖 prot_set 绑定,
板端需实机确认主站写入能反映到本地定值页;17 项中 15 项的出厂默认
值与点表字面量不同,主站读到的是真实模型的值;绑定真实源后定值区
切换一律拒绝(单套活跃值无原子整区事务)。
2026-11-19 15:48:21 +08:00

82 lines
4.8 KiB
C

/* 模块名称: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;
/* The 17 accepted consumer entries have numeric tags 1/38 and width <=4.
* Every other profile row is refused, 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;
/* Counts parameter writes refused because the build has no value store
* (IEC104_SOURCE_NO_VALUE_STORE). Always 0 in the full-feature build, where
* refusals are ordinary protocol rejections and not tracked here. */
uint32_t parameter_rejected;
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