Files
STM32F4-Base/App/protocol/iec104/iec104_source.h
T
wandering1 90cc9a7d99 feat(iec104): 把 104 骨架完整迁到 MCU 板端并在台架跑通总召
把 IEC104 从模拟器端整体搬到板端:协议栈原先由独立 iec104Task 驱动,
现在改为 netTask 单 owner 的 threadless 驱动,退役独立任务。

驱动与后端:
- 新增 iec104_net_mcu.c/.h,实现 IEC104_Net.h 的 11 个 ABI 函数,
  用槽表加单调句柄映射 net_socket 的单 fd 模型;CLIENT 拆除只走
  net_drop_conn_locked,绝不 net_close 监听 fd。
- net_socket 增 5 个 owner-only 接口(net_accept_locked、
  net_drop_conn_locked、net_pending_bytes、net_send_try、
  net_mono_now_ms)。
- iec104_serv.c 重写为 iec104_serv_net_tick(),在 netTask 1ms 主循环
  里驱动 core;无 osDelay/阻塞调用,喂狗是第一句。
- Src/freertos.c 删除 iec104Task 三件套,对应栈水位探针表项同步删除。

内存:目标板 192KB SRAM 装不下 vendor 默认容量档,按实测裁剪到
skeleton 档(TCP 缓冲、业务池、SOE 256->128、值槽密集化),
RW_IRAM1 118,408 B / 90.3%,留 2,178 B 余量。

台架修复(只在真硬件暴露):
- SNTP s_ctx.fd 零初始化后为 0,而 net fd 0 是合法槽位下标,
  首次代次变化时误 net_close(0) 关掉 ftpTask 的监听槽,导致 104
  连接被 FTP 的 220 banner 抢走;改为显式 .fd = -1。
- Accept 幂等:单 fd 模型下 vendor 每轮重试 Accept 会拿到同一 fd,
  后端改为会话在役时返 WOULD_BLOCK,Close 不再清槽。

验证:
- Keil 全量重建 0 error 0 warning,uvprojx 零抖动。
- 模拟器真实 TCP 13/13、transport 10/10。
- netmcu 后端单测 13/13(新增 N12)。
- 台架 serial-boot-test required 全中;板端 2404 总召 129 帧收到
  59 遥信(53 有源、全 IV)+ 16 双点 + 30 遥测(PT1 三相非零、
  UN/频率/温度按未标定置 IV)、遥控选择执行全否定、校时 COT 7。
- 期间顺带修正 UI 漂移自检文案、.serialtest required 陈旧项、
  coding-standard 对 vendor 与外部 ABI 名字的排除口径。
2026-11-18 10:29:18 +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;
/* 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;
/* 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