/* 模块名称:共享 IEC104 服务接口。 */ /* Shared IEC104 runtime; one event-loop owner, no RTOS dependencies. */ #ifndef IEC104_CORE_H #define IEC104_CORE_H #include #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