Files
STM32F4-Base/App/protocol/iec104/iec104_core.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

70 lines
3.0 KiB
C

/* 模块名称:共享 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;
/* Counts sessions that ended with a send-class fault: on DISCONNECTED, when
* connection_status_valid and fault is LOCAL_SEND_TIMEOUT or TRANSPORT.
* It is NOT a count of failed bytes, short writes, or library-internal
* abandoned frames. Reset at each admitted start. */
uint32_t frame_failures;
} 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