From b239b50abe1b79e67339c1657baf5d4f02089904 Mon Sep 17 00:00:00 2001 From: ismail Date: Fri, 11 Jul 2025 18:28:59 +0300 Subject: [PATCH] Fix invalid channel ID handling. Fix the issue where get_state* functions do not handle when id is RELAY_CHN_ID_ALL. Fixes #1037. --- include/relay_chn.h | 1 + src/relay_chn.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/relay_chn.h b/include/relay_chn.h index 4f500bf..6f31edc 100644 --- a/include/relay_chn.h +++ b/include/relay_chn.h @@ -49,6 +49,7 @@ typedef enum relay_chn_direction_enum relay_chn_direction_t; * @brief Enums that represent the state of a relay channel. */ enum relay_chn_state_enum { + RELAY_CHN_STATE_UNDEFINED, ///< The relay channel state is undefined. RELAY_CHN_STATE_FREE, ///< The relay channel is free to run or execute commands. RELAY_CHN_STATE_STOPPED, ///< The relay channel is stopped and not running. RELAY_CHN_STATE_FORWARD, ///< The relay channel is running in the forward direction. diff --git a/src/relay_chn.c b/src/relay_chn.c index 124ff18..02ce94b 100644 --- a/src/relay_chn.c +++ b/src/relay_chn.c @@ -648,16 +648,16 @@ static void relay_chn_issue_cmd(relay_chn_t* relay_chn, relay_chn_cmd_t cmd) /* relay_chn APIs */ relay_chn_state_t relay_chn_get_state(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return RELAY_CHN_STATE_STOPPED; + if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) { + return RELAY_CHN_STATE_UNDEFINED; } return relay_channels[chn_id].state; } char *relay_chn_get_state_str(uint8_t chn_id) { - if (!relay_chn_is_channel_id_valid(chn_id)) { - return "INVALID"; + if (!relay_chn_is_channel_id_valid(chn_id) || chn_id == RELAY_CHN_ID_ALL) { + return relay_chn_state_str(RELAY_CHN_STATE_UNDEFINED); } return relay_chn_state_str(relay_channels[chn_id].state); }