/* * IEC104 service adapter host unit test. * Compiles the real App/task/iec104_serv.c into this TU. All collaborators are * controlled stubs: core lifecycle, source binding, measurement snapshot, * calibration flags, net generation, event flags and task watchdog. */ #include #include #include #include static int s_total = 0, s_passed = 0, s_failed = 0; #define CHECK(cond, name) do { \ s_total++; \ if (cond) { s_passed++; printf(" [PASS] %s\n", name); } \ else { s_failed++; printf(" [FAIL] %s (line %d)\n", name, __LINE__); } \ } while (0) #include "cmsis_os2.h" #include "sys_events.h" #include "net_socket.h" #include "cfg_file.h" static int g_cfg_register_calls, g_cfg_save_result, g_cfg_save_calls; int cfg_file_register(const cfg_section_reg_t *reg) { (void)reg; g_cfg_register_calls++; return 0; } int cfg_file_save(void) { g_cfg_save_calls++; return g_cfg_save_result; } int g_i104_info_count = 0; int g_i104_error_count = 0; /* ---------- CMSIS/RTOS fake ---------- */ osEventFlagsId_t sys_evtHandle = (osEventFlagsId_t)1; static uint32_t g_tick; static uint32_t g_ready_wait_result; static uint32_t g_ready_wait_options; static uint32_t g_events; uint32_t osKernelGetTickCount(void) { return g_tick; } uint32_t osEventFlagsWait(osEventFlagsId_t id, uint32_t flags, uint32_t options, uint32_t timeout) { (void)id; (void)flags; (void)timeout; g_ready_wait_options = options; return g_ready_wait_result; } uint32_t osEventFlagsSet(osEventFlagsId_t id, uint32_t flags) { (void)id; g_events |= flags; return 0u; } /* ---------- production headers, then production source ---------- */ #include "iec104_comm.c" #include "iec104_core.h" #include "iec104_source.h" #include "iec104_net_mcu.h" #include "meas.h" #include "adc_cal.h" #include "ioboard_di.h" #include "prot_set.h" #include "run_par.h" #include "task_watchdog.h" #include "dbg_log.h" /* ---------- collaborator stubs ---------- */ static uint32_t g_net_generation; uint32_t net_get_generation(void) { return g_net_generation; } static iec104_source_bindings_t g_bound; static int g_core_start_result; static int g_core_poll_result; static iec104_core_status_t g_core_status; static iec104_core_config_t g_last_core_cfg; static int g_core_start_calls, g_core_poll_calls, g_core_stop_calls, g_bind_calls; int iec104_core_start(const iec104_core_config_t *cfg) { g_core_start_calls++; if (cfg != NULL) { g_last_core_cfg = *cfg; } return g_core_start_result; } int iec104_core_poll(void) { g_core_poll_calls++; return g_core_poll_result; } int iec104_core_stop(void) { g_core_stop_calls++; return 0; } void iec104_core_get_status(iec104_core_status_t *status) { if (status != NULL) { *status = g_core_status; } } void iec104_source_bind(const iec104_source_bindings_t *bindings) { g_bind_calls++; if (bindings != NULL) { g_bound = *bindings; } } static uint32_t g_rx_bytes; void iec104_net_mcu_stats(uint32_t *rx_bytes) { if (rx_bytes != NULL) { *rx_bytes = g_rx_bytes; } } static meas_snap_t g_snap; static int g_meas_result; int meas_get(meas_snap_t *out) { if (out != NULL) { *out = g_snap; } return g_meas_result; } static uint32_t g_cal_flags[ADC_SEM_COUNT]; uint32_t adc_cal_flags(adc_semantic_t sem) { if ((int)sem < 0 || (int)sem >= (int)ADC_SEM_COUNT) { return 0u; } return g_cal_flags[sem]; } static uint8_t g_prot_dirty, g_run_dirty; uint8_t prot_set_dirty_peek(void) { return g_prot_dirty; } uint8_t run_par_dirty_peek(void) { return g_run_dirty; } static float g_prot_value; static prot_set_change_t g_last_batch[PROT_SET_BATCH_MAX]; static size_t g_last_batch_n; static int g_last_validate_only, g_apply_batch_calls; float prot_set_get_f(prot_set_id_t id) { (void)id; return g_prot_value; } int prot_set_apply_batch(const prot_set_change_t *changes, size_t n, int validate_only) { size_t i; g_apply_batch_calls++; g_last_batch_n = n; g_last_validate_only = validate_only; for (i = 0U; i < n && i < PROT_SET_BATCH_MAX; ++i) { g_last_batch[i] = changes[i]; } return 0; } static int g_wd_beats; static task_wd_id_t g_last_wd; void task_wd_beat(task_wd_id_t id) { g_wd_beats++; g_last_wd = id; } #include "iec104_serv.c" static void reset_all(void) { g_tick = 0u; g_ready_wait_result = SYS_EVT_NET_READY; g_ready_wait_options = 0u; g_events = 0u; g_net_generation = 0u; g_core_start_result = 0; g_core_poll_result = 0; memset(&g_core_status, 0, sizeof(g_core_status)); memset(&g_last_core_cfg, 0, sizeof(g_last_core_cfg)); g_core_start_calls = g_core_poll_calls = g_core_stop_calls = g_bind_calls = 0; g_rx_bytes = 0u; memset(&g_snap, 0, sizeof(g_snap)); g_meas_result = 0; memset(g_cal_flags, 0, sizeof(g_cal_flags)); g_prot_dirty = g_run_dirty = 0u; g_prot_value = 0u; g_apply_batch_calls = 0; g_last_batch_n = 0u; g_last_validate_only = 0; memset(g_last_batch, 0, sizeof(g_last_batch)); g_wd_beats = 0; g_i104_info_count = g_i104_error_count = 0; g_cfg_register_calls = g_cfg_save_calls = 0; g_cfg_save_result = 0; (void)iec104_comm_init(); /* reset service static owner state via a fresh start cycle */ s_started = 0u; s_gen = 0u; s_retry_tick = 0u; s_dirty_next = 0u; s_last_active = 0u; s_link_run = 0u; memset(&s_stats, 0, sizeof(s_stats)); s_last_conn = s_carry_conn = 0u; s_last_apdu = s_carry_apdu = 0u; s_last_txfail = s_carry_txfail = 0u; s_last_rx = s_carry_rx = 0u; g_tick = I104_START_RETRY_MS + 1u; iec104_serv_net_tick(); g_tick = 0u; g_core_start_calls = g_core_poll_calls = g_core_stop_calls = g_bind_calls = 0; g_wd_beats = 0; g_events = 0u; g_i104_info_count = g_i104_error_count = 0; } static void tc_parameter_binding(void) { iec104_source_change_t changes[2]; float value = 0.0f; reset_all(); CHECK(g_bound.parameter_read != NULL && g_bound.parameter_apply != NULL, "parameter callbacks installed as a pair"); g_prot_value = 12.5f; CHECK(g_bound.parameter_read(NULL, (uint16_t)PS_ID_COUNT, &value) == false, "out-of-range semantic id refused"); CHECK(g_bound.parameter_read(NULL, 2u, &value) == true && value == 12.5f, "valid semantic id reads live prot_set value"); CHECK(g_bound.parameter_read(NULL, 2u, NULL) == false, "null output refused"); changes[0].id = 2u; changes[0].value = 1.25f; changes[1].id = 3u; changes[1].value = 500.0f; CHECK(g_bound.parameter_apply(NULL, changes, 2u, true) == true, "validate-only batch accepted"); CHECK(g_apply_batch_calls == 1 && g_last_batch_n == 2u && g_last_validate_only == 1, "validate-only forwarded once without commit"); CHECK(g_last_batch[0].id == 2u && g_last_batch[0].q10 == PROT_SET_Q10(1.25f) && g_last_batch[1].id == 3u && g_last_batch[1].q10 == PROT_SET_Q10(500.0f), "semantic values quantized into prot_set batch"); CHECK(g_bound.parameter_apply(NULL, changes, 0u, false) == false, "empty batch refused"); CHECK(g_bound.parameter_apply(NULL, changes, 2u + IEC104_DTU_CONSUMER_COUNT, false) == false, "over-capacity batch refused"); CHECK(g_apply_batch_calls == 1, "rejected batches never reach prot_set"); } static void tc_monotonic(void) { uint32_t last = 100u, carry = 7u; /* 正序:不跨回绕,carry 与 last 一起向后走。 */ CHECK(s_monotonic(150u, &last, &carry) == 157u, "monotonic normal increment"); CHECK(last == 150u && carry == 7u, "monotonic normal state"); /* 一次真回绕:now < last,把上一段的 last 折进 carry。 */ CHECK(s_monotonic(50u, &last, &carry) == 207u, "monotonic reset carries previous"); CHECK(last == 50u && carry == 157u, "monotonic reset state"); /* now == last 不算回绕:时间没有倒退,只是原地等了一拍。 * 这一档单独写断言,是因为 `now < last` 误写成 `now <= last` 时 * 返回值会多出一整个 last(本例 5 -> 10),而只看"单调增"是看不出来的。 */ last = 5u; carry = 0u; CHECK(s_monotonic(5u, &last, &carry) == 5u, "monotonic same tick does not wrap"); CHECK(last == 5u && carry == 0u, "monotonic same tick leaves carry alone"); /* 折进 last 会溢出时,carry 直接钉到 SAT —— 断言 carry 本身而不只是 * 返回值:把 `*carry = I104_STAT_SAT` 改成 SAT-1,返回值仍是 SAT * (下一句的饱和分支照样命中),只有 carry 的内部状态能抓出来。 */ last = 20u; carry = I104_STAT_SAT - 5u; CHECK(s_monotonic(10u, &last, &carry) == I104_STAT_SAT, "monotonic carry saturation"); CHECK(carry == I104_STAT_SAT, "monotonic carry pinned exactly at SAT"); /* 已经饱和之后再进来,仍然饱和且不再叠加。 */ last = 10u; carry = I104_STAT_SAT; CHECK(s_monotonic(15u, &last, &carry) == I104_STAT_SAT, "monotonic already saturated"); CHECK(carry == I104_STAT_SAT && last == 15u, "monotonic stays saturated"); /* 32 位 tick 自然回绕(now 从 0xFFFFFFF0 走到 5)必须被判成回绕并饱和, * 不能算成"倒退了 4G 多"。 */ last = 0xFFFFFFF0u; carry = 0u; CHECK(s_monotonic(5u, &last, &carry) == I104_STAT_SAT, "monotonic u32 wrap saturates"); /* 边界对照组:carry + now 恰好等于 SAT-1 时**不许**提前饱和。 * 饱和判据是 `carry > SAT - now`,等号留给正常的加法。 * 说明:carry + now 恰等于 SAT 的那一档与饱和分支返回值相同, * 属等价变异,这里不假装能区分,只用 SAT-1 这一档卡住"提前饱和"。 */ last = 10u; carry = I104_STAT_SAT - 11u; CHECK(s_monotonic(10u, &last, &carry) == I104_STAT_SAT - 1u, "monotonic sum SAT-1 is not saturated early"); } static void tc_measure(void) { float value = -1.0f; uint8_t quality = 0xFFu; reset_all(); memset(&g_snap, 0, sizeof(g_snap)); g_snap.yc[ADC_SEM_UB] = 12.5f; g_snap.qds[ADC_SEM_UB] = 0x20u; /* NT only */ g_cal_flags[ADC_SEM_UB] = ADC_CAL_F_VALID; CHECK(g_bound.measurement != NULL && g_bound.signal != NULL, "source bindings installed after core start"); CHECK(g_bound.measurement(NULL, 0x9606u, &value, &quality) == true, "mapped UB source available"); CHECK(value == 12.5f && quality == 0x20u, "UB value and quality copied from snapshot"); g_cal_flags[ADC_SEM_UB] = 0u; CHECK(g_bound.measurement(NULL, 0x9606u, &value, &quality) == true, "uncalibrated UB still reports snapshot"); CHECK(quality == 0xA0u, "uncalibrated UB gets IV bit"); g_snap.freq_valid = 1u; g_snap.freq_hz = 50.01f; CHECK(g_bound.measurement(NULL, 0x960Du, &value, &quality) == true, "frequency source available"); CHECK(value == 50.01f && quality == 0u, "frequency value and clean quality"); g_snap.freq_valid = 0u; CHECK(g_bound.measurement(NULL, 0x960Du, &value, &quality) == false, "invalid frequency is unavailable"); CHECK(g_bound.measurement(NULL, 0x9999u, &value, &quality) == false, "unmapped source is unavailable"); CHECK(g_bound.measurement(NULL, 0x9605u, NULL, &quality) == false, "NULL value output rejected"); CHECK(g_bound.measurement(NULL, 0x9605u, &value, NULL) == false, "NULL quality output rejected"); g_meas_result = -2; CHECK(g_bound.measurement(NULL, 0x9605u, &value, &quality) == false, "no published snapshot is unavailable"); g_meas_result = 0; CHECK(g_bound.signal(NULL, 0x9141u, &quality, &quality) == false, "signal source always unavailable"); } static void tc_stats_and_link(void) { iec104_serv_stats_t stats; uint8_t link; reset_all(); memset(&stats, 0x55, sizeof(stats)); iec104_serv_get_stats(&stats); CHECK(stats.conn_up == 0u && stats.apdu_tx == 0u && stats.tx_fail == 0u && stats.rx_bytes == 0u, "get_stats fills four fields"); iec104_serv_get_stats(NULL); CHECK(true, "get_stats NULL is harmless"); link = iec104_serv_link_run(); CHECK(link == 0u, "link_run starts at zero"); g_tick = I104_START_RETRY_MS; g_core_status.connections = 3u; g_core_status.written_i_frames = 4u; g_core_status.frame_failures = 5u; g_core_status.active = 1u; g_rx_bytes = 6u; iec104_serv_net_tick(); iec104_serv_get_stats(&stats); CHECK(stats.conn_up == 3u && stats.apdu_tx == 4u && stats.tx_fail == 5u && stats.rx_bytes == 6u, "stats mirror one core poll"); CHECK(iec104_serv_link_run() == 1u, "link_run mirrors active"); CHECK(s_last_active == 1u, "active edge recorded"); g_core_status.connections = 0u; g_core_status.written_i_frames = 0u; g_core_status.frame_failures = 0u; g_core_status.active = 0u; g_rx_bytes = 0u; g_tick += 1u; iec104_serv_net_tick(); iec104_serv_get_stats(&stats); CHECK(stats.conn_up == 3u && stats.apdu_tx == 4u && stats.tx_fail == 5u && stats.rx_bytes == 6u, "stats stay monotonic when sources reset"); CHECK(iec104_serv_link_run() == 0u, "link_run follows active low"); } static void tc_tick_control(void) { iec104_serv_stats_t stats; int wd_before; reset_all(); g_ready_wait_result = osFlagsError; g_tick = 1000u; iec104_serv_net_tick(); CHECK(g_wd_beats == 1 && g_last_wd == TASK_WD_IEC104, "watchdog beat precedes NET_READY return"); CHECK((g_ready_wait_options & osFlagsNoClear) != 0u, "NET_READY is queried without clearing"); CHECK(g_core_start_calls == 0 && g_core_poll_calls == 0, "network not ready: no core calls"); reset_all(); g_ready_wait_result = SYS_EVT_NET_READY; g_tick = I104_START_RETRY_MS; g_core_start_result = -1; s_started = 0u; s_retry_tick = 0u; iec104_serv_net_tick(); CHECK(g_core_start_calls == 1 && g_bind_calls == 0, "failed start does not bind sources"); g_core_start_result = 0; iec104_serv_net_tick(); CHECK(g_core_start_calls == 1, "start retry is throttled"); g_tick += I104_START_RETRY_MS; iec104_serv_net_tick(); CHECK(g_core_start_calls == 2 && g_bind_calls == 1, "retry after throttle binds sources once"); CHECK(strcmp(g_last_core_cfg.bind_address, "0.0.0.0") == 0, "core bind address is wildcard"); CHECK(g_last_core_cfg.port == iec104_comm_port() && g_last_core_cfg.common_address == iec104_comm_ca(), "core config uses live comm getters"); CHECK(g_core_poll_calls > 0, "successful start allows polling in later ticks"); reset_all(); g_tick = I104_START_RETRY_MS + 1u; g_net_generation = 9u; iec104_serv_net_tick(); /* generation change: stop + restart_core (throttled) */ g_tick += I104_START_RETRY_MS; iec104_serv_net_tick(); /* retry window elapsed: core starts again + rebind */ CHECK(g_core_stop_calls >= 1 && g_core_start_calls >= 1, "generation change restarts core without depending on stop status"); CHECK(g_bind_calls >= 1, "generation restart rebinds sources"); CHECK(g_i104_info_count >= 1, "generation change leaves an info trace"); reset_all(); g_tick = I104_START_RETRY_MS + 1u; iec104_serv_net_tick(); g_tick += 1u; g_core_poll_result = -1; wd_before = g_wd_beats; iec104_serv_net_tick(); CHECK(g_core_poll_calls == 2 && g_i104_error_count >= 1, "poll failure logs and restarts"); iec104_serv_get_stats(&stats); CHECK(stats.conn_up == 0u, "poll failure does not publish stats"); reset_all(); g_tick = I104_START_RETRY_MS; iec104_serv_net_tick(); g_prot_dirty = 1u; g_run_dirty = 1u; g_tick = s_dirty_next; iec104_serv_net_tick(); CHECK((g_events & SYS_EVT_PROT_SET_SAVE) != 0u, "protection dirty posts save event on deadline"); CHECK((g_events & SYS_EVT_RUN_PAR_SAVE) != 0u, "run-param dirty posts save event on deadline"); g_events = 0u; g_tick += 1u; iec104_serv_net_tick(); CHECK(g_events == 0u, "no duplicate dirty event before next deadline"); (void)wd_before; } int main(void) { printf("=== IEC104 Service Host Unit Tests ===\n"); tc_monotonic(); tc_measure(); tc_parameter_binding(); tc_stats_and_link(); tc_tick_control(); printf("=== IEC104 Serv Tests: %d/%d PASSED (failed=%d) ===\n", s_passed, s_total, s_failed); return (s_failed == 0) ? 0 : 1; }