From 9a6b8c9f803b9de5fe830f8b274905c99090300e Mon Sep 17 00:00:00 2001 From: ismail Date: Tue, 26 Aug 2025 17:43:19 +0300 Subject: [PATCH] Add upper boundary checks for tilt sensitivity settings The tilt sensitivity values were passed to the NVS module without the boundaries being checked, even though the maximum percent value is 100. This commit fixes this issue. Also test cases are added to cover the upper boundary checks for the tilt sensitivity settings. Fixes #1086 --- src/relay_chn_tilt.c | 5 ++++ test_apps/main/test_relay_chn_tilt_multi.c | 29 +++++++++++++++++++++ test_apps/main/test_relay_chn_tilt_single.c | 12 +++++++++ 3 files changed, 46 insertions(+) diff --git a/src/relay_chn_tilt.c b/src/relay_chn_tilt.c index a7ea07a..fdc732f 100644 --- a/src/relay_chn_tilt.c +++ b/src/relay_chn_tilt.c @@ -39,6 +39,7 @@ static const char *TAG = "RELAY_CHN_TILT"; * 100 / (RELAY_CHN_TILT_RUN_MAX_MS - RELAY_CHN_TILT_RUN_MIN_MS) ) /**@}*/ +#define ADJUST_TILT_SENS_BOUNDARIES(sens) if (sens > 100) sens = 100 /// @brief Tilt steps. typedef enum { @@ -316,6 +317,7 @@ static void relay_chn_tilt_compute_set_sensitivity(relay_chn_tilt_ctl_t *tilt_ct void relay_chn_tilt_set_sensitivity(uint8_t chn_id, uint8_t sensitivity) { if (relay_chn_is_channel_id_valid(chn_id)) { + ADJUST_TILT_SENS_BOUNDARIES(sensitivity); relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[chn_id], sensitivity); #if CONFIG_RELAY_CHN_ENABLE_NVS @@ -334,6 +336,7 @@ esp_err_t relay_chn_tilt_set_sensitivity_all(uint8_t *sensitivities) ESP_LOGW(TAG, "set_sensitivity_all: Run limits have been set until channel %d since sensitivities[%d] is NULL", i, i); break; } + ADJUST_TILT_SENS_BOUNDARIES(*src_sensitivity); relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], *src_sensitivity); #if CONFIG_RELAY_CHN_ENABLE_NVS relay_chn_nvs_set_tilt_sensitivity(i, *src_sensitivity); @@ -345,6 +348,7 @@ esp_err_t relay_chn_tilt_set_sensitivity_all(uint8_t *sensitivities) void relay_chn_tilt_set_sensitivity_all_with(uint8_t sensitivity) { for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + ADJUST_TILT_SENS_BOUNDARIES(sensitivity); relay_chn_tilt_compute_set_sensitivity(&tilt_ctls[i], sensitivity); #if CONFIG_RELAY_CHN_ENABLE_NVS relay_chn_nvs_set_tilt_sensitivity(i, sensitivity); @@ -377,6 +381,7 @@ esp_err_t relay_chn_tilt_get_sensitivity_all(uint8_t *sensitivities) void relay_chn_tilt_set_sensitivity(uint8_t sensitivity) { + ADJUST_TILT_SENS_BOUNDARIES(sensitivity); relay_chn_tilt_compute_set_sensitivity(&tilt_ctl, sensitivity); #if CONFIG_RELAY_CHN_ENABLE_NVS diff --git a/test_apps/main/test_relay_chn_tilt_multi.c b/test_apps/main/test_relay_chn_tilt_multi.c index 2a30196..40e10a4 100644 --- a/test_apps/main/test_relay_chn_tilt_multi.c +++ b/test_apps/main/test_relay_chn_tilt_multi.c @@ -322,6 +322,35 @@ TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivi TEST_ASSERT_EQUAL_UINT8_ARRAY(expect, vals, CONFIG_RELAY_CHN_COUNT); } +// Test sensitivity upper boundary for all set functions +TEST_CASE("relay_chn_tilt_set_sensitivity functions handle upper boundary", "[relay_chn][tilt][sensitivity]") +{ + // 1. Test relay_chn_tilt_set_sensitivity() for each channel + for (uint8_t ch = 0; ch < CONFIG_RELAY_CHN_COUNT; ch++) { + relay_chn_tilt_set_sensitivity(ch, 101); + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity(ch)); + + relay_chn_tilt_set_sensitivity(ch, 255); + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity(ch)); + } + + // 2. Test relay_chn_tilt_set_sensitivity_all_with() + relay_chn_tilt_set_sensitivity_all_with(150); + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity(i)); + } + + // 3. Test relay_chn_tilt_set_sensitivity_all() + uint8_t sensitivities[CONFIG_RELAY_CHN_COUNT]; + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + sensitivities[i] = 100 + i * 10; // Values like 100, 110, 120... + } + TEST_ESP_OK(relay_chn_tilt_set_sensitivity_all(sensitivities)); + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity(i)); + } +} + // Test tilt counter logic: forward x3, reverse x3, extra reverse fails TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") { diff --git a/test_apps/main/test_relay_chn_tilt_single.c b/test_apps/main/test_relay_chn_tilt_single.c index 40f8dcb..4cef671 100644 --- a/test_apps/main/test_relay_chn_tilt_single.c +++ b/test_apps/main/test_relay_chn_tilt_single.c @@ -205,6 +205,18 @@ TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivi TEST_ASSERT_EQUAL_UINT8(42, relay_chn_tilt_get_sensitivity()); } +// Test sensitivity upper boundary +TEST_CASE("relay_chn_tilt_set_sensitivity handles upper boundary", "[relay_chn][tilt][sensitivity]") +{ + // Set sensitivity to a value greater than 100 + relay_chn_tilt_set_sensitivity(101); + // It should be capped at 100 + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity()); + + relay_chn_tilt_set_sensitivity(200); + TEST_ASSERT_EQUAL_UINT8(100, relay_chn_tilt_get_sensitivity()); +} + // Test tilt counter logic: forward x3, reverse x3, extra reverse fails TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") {