From 61edf11b7505bb463be9138d1f21570947c8d06a Mon Sep 17 00:00:00 2001 From: ismail Date: Tue, 19 Aug 2025 17:36:23 +0300 Subject: [PATCH] Refactor to remove redundant initialization and add NVS storage tests - Removed unnecessary calls to relay_chn_create and g_is_component_initialized in multiple test cases across test_relay_chn_core_single.c, test_relay_chn_listener_multi.c, and test_relay_chn_listener_single.c. - Introduced new test files for NVS functionality: test_relay_chn_nvs_multi.c and test_relay_chn_nvs_single.c, covering initialization, direction setting, invalid parameters, and erase operations. - Updated partition table configuration to support NVS storage, including the addition of a new partition file part_nvs.csv. - Adjusted sdkconfig files to enable NVS support and configure custom partition settings for relay channels. --- test_apps/main/CMakeLists.txt | 17 ++- test_apps/main/test_app_main.c | 66 ++++++++- test_apps/main/test_common.c | 21 ++- test_apps/main/test_common.h | 3 + test_apps/main/test_relay_chn_core_multi.c | 56 ------- test_apps/main/test_relay_chn_core_single.c | 30 ---- .../main/test_relay_chn_listener_multi.c | 12 -- .../main/test_relay_chn_listener_single.c | 12 -- test_apps/main/test_relay_chn_nvs_multi.c | 140 ++++++++++++++++++ test_apps/main/test_relay_chn_nvs_single.c | 126 ++++++++++++++++ test_apps/main/test_relay_chn_tilt_multi.c | 52 +------ test_apps/main/test_relay_chn_tilt_single.c | 40 +---- test_apps/partition_table/partitionTable.csv | 5 - test_apps/partitions/part_nvs.csv | 6 + test_apps/sdkconfig | 24 ++- test_apps/sdkconfig.defaults | 3 +- test_apps/sdkconfig.defaults.custom_nvs | 15 ++ test_apps/sdkconfig.defaults.single | 3 +- .../sdkconfig.defaults.single.custom_nvs | 15 ++ 19 files changed, 428 insertions(+), 218 deletions(-) create mode 100644 test_apps/main/test_relay_chn_nvs_multi.c create mode 100644 test_apps/main/test_relay_chn_nvs_single.c delete mode 100644 test_apps/partition_table/partitionTable.csv create mode 100644 test_apps/partitions/part_nvs.csv create mode 100644 test_apps/sdkconfig.defaults.custom_nvs create mode 100644 test_apps/sdkconfig.defaults.single.custom_nvs diff --git a/test_apps/main/CMakeLists.txt b/test_apps/main/CMakeLists.txt index 5ff1cfd..f148b59 100644 --- a/test_apps/main/CMakeLists.txt +++ b/test_apps/main/CMakeLists.txt @@ -2,6 +2,8 @@ set(srcs "test_common.c" "test_app_main.c") +set(incdirs ".") + # === Selective compilation based on channel count === if(CONFIG_RELAY_CHN_COUNT GREATER 1) list(APPEND srcs "test_relay_chn_core_multi.c" @@ -14,16 +16,27 @@ endif() if(CONFIG_RELAY_CHN_ENABLE_TILTING) if(CONFIG_RELAY_CHN_COUNT GREATER 1) list(APPEND srcs "test_relay_chn_tilt_multi.c") - else() + else() list(APPEND srcs "test_relay_chn_tilt_single.c") endif() endif() +if(CONFIG_RELAY_CHN_ENABLE_NVS) + list(APPEND incdirs "../../private_include") + list(APPEND srcs "../../src/relay_chn_nvs.c") + if(CONFIG_RELAY_CHN_COUNT GREATER 1) + list(APPEND srcs "test_relay_chn_nvs_multi.c") + else() + list(APPEND srcs "test_relay_chn_nvs_single.c") + endif() +endif() + + # In order for the cases defined by `TEST_CASE` to be linked into the final elf, # the component can be registered as WHOLE_ARCHIVE idf_component_register( SRCS ${srcs} - INCLUDE_DIRS "." + INCLUDE_DIRS ${incdirs} REQUIRES unity relay_chn WHOLE_ARCHIVE ) diff --git a/test_apps/main/test_app_main.c b/test_apps/main/test_app_main.c index c72d68f..edabae1 100644 --- a/test_apps/main/test_app_main.c +++ b/test_apps/main/test_app_main.c @@ -1,11 +1,16 @@ +#include #include "esp_log.h" #include "esp_system.h" -#include "test_common.h" #include "unity.h" #include "unity_internals.h" #include "unity_test_runner.h" -#include +#include "test_common.h" + +#if RELAY_CHN_ENABLE_NVS == 1 +#include "nvs_flash.h" +#include "relay_chn_nvs.h" +#endif #ifndef RELAY_CHN_UNITY_TEST_GROUP_TAG @@ -15,20 +20,60 @@ void setUp() { - g_is_component_initialized = false; + } void tearDown() { - // Clean up after each test - if (g_is_component_initialized) { - relay_chn_destroy(); - g_is_component_initialized = false; + reset_channels_to_idle_state(); +} + +static void test_nvs_flash_init(void) +{ + esp_err_t ret; +#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1 + ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME); + ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init partition return: %s", esp_err_to_name(ret)); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + // NVS partition is truncated and needs to be erased + ret = nvs_flash_erase_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME); + if (ret == ESP_OK) { + ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME); + } } +#else + ret = nvs_flash_init(); + ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init return: %s", esp_err_to_name(ret)); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + // NVS partition is truncated and needs to be erased + ret = nvs_flash_erase(); + if (ret == ESP_OK) { + ret = nvs_flash_init(); + } + } +#endif + TEST_ESP_OK(ret); +} + +static void test_nvs_flash_deinit(void) +{ + esp_err_t ret; +#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1 + ret = nvs_flash_deinit_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME); +#else + ret = nvs_flash_deinit(); +#endif + TEST_ESP_OK(ret); } void app_main(void) { + // Init NVS once for all tests + test_nvs_flash_init(); + + // Create relay_chn once for all tests + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + UNITY_BEGIN(); // Log general test information @@ -43,6 +88,13 @@ void app_main(void) } UNITY_END(); + + // Destroy relay_chn + relay_chn_destroy(); + + // Deinit NVS + test_nvs_flash_deinit(); + ESP_LOGI(TEST_TAG, "All tests complete."); esp_restart(); // Restart to invoke qemu exit diff --git a/test_apps/main/test_common.c b/test_apps/main/test_common.c index be7169c..3db95de 100644 --- a/test_apps/main/test_common.c +++ b/test_apps/main/test_common.c @@ -4,9 +4,7 @@ const char *TEST_TAG = "RELAY_CHN_TEST"; const uint8_t relay_chn_count = CONFIG_RELAY_CHN_COUNT; const uint32_t opposite_inertia_ms = CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS; -const uint32_t test_delay_margin_ms = 50; // ms toleransı - -bool g_is_component_initialized = false; +const uint32_t test_delay_margin_ms = 50; // ms tolerance // Test-wide GPIO map #if CONFIG_RELAY_CHN_COUNT > 1 @@ -36,4 +34,19 @@ const uint8_t gpio_map[] = { const uint8_t gpio_map[] = {4, 5}; #endif -const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]); \ No newline at end of file +const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]); + +void reset_channels_to_idle_state() +{ +#if CONFIG_RELAY_CHN_COUNT > 1 + relay_chn_stop(RELAY_CHN_ID_ALL); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); + } +#else + relay_chn_stop(); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); +#endif +} \ No newline at end of file diff --git a/test_apps/main/test_common.h b/test_apps/main/test_common.h index 6c9c9eb..7a17e80 100644 --- a/test_apps/main/test_common.h +++ b/test_apps/main/test_common.h @@ -21,3 +21,6 @@ extern const uint32_t test_delay_margin_ms; // Init state extern bool g_is_component_initialized; + +// Reset channels to Idle state +void reset_channels_to_idle_state(void); \ No newline at end of file diff --git a/test_apps/main/test_relay_chn_core_multi.c b/test_apps/main/test_relay_chn_core_multi.c index 8551f7c..8b35c5c 100644 --- a/test_apps/main/test_relay_chn_core_multi.c +++ b/test_apps/main/test_relay_chn_core_multi.c @@ -22,9 +22,6 @@ TEST_CASE("relay_chn_create handles invalid arguments", "[relay_chn][core]") // TEST_CASE: Test that relay channels initialize correctly to RELAY_CHN_STATE_IDLE TEST_CASE("Relay channels initialize correctly to FREE state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - for (uint8_t i = 0; i < relay_chn_count; i++) { TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); } @@ -32,8 +29,6 @@ TEST_CASE("Relay channels initialize correctly to FREE state", "[relay_chn][core // TEST_CASE: Test that relays do nothing when an invlid channel id given TEST_CASE("Run forward does nothing if channel id is invalid", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; for (uint8_t i = 0; i < relay_chn_count; i++) { int invalid_id = relay_chn_count * 2 + i; relay_chn_run_forward(invalid_id); // relay_chn_run_forward returns void @@ -45,9 +40,6 @@ TEST_CASE("Run forward does nothing if channel id is invalid", "[relay_chn][core // TEST_CASE: Test that relays run in the forward direction and update their state TEST_CASE("Relay channels run forward and update state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - for (uint8_t i = 0; i < relay_chn_count; i++) { relay_chn_run_forward(i); // relay_chn_run_forward returns void // Short delay for state to update @@ -58,9 +50,6 @@ TEST_CASE("Relay channels run forward and update state", "[relay_chn][core]") { // TEST_CASE: Test that relays do nothing when an invlid channel id given TEST_CASE("Run reverse does nothing if channel id is invalid", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Verify that no valid channels were affected for (uint8_t i = 0; i < relay_chn_count; i++) { int invalid_id = relay_chn_count * 2 + i; @@ -73,9 +62,6 @@ TEST_CASE("Run reverse does nothing if channel id is invalid", "[relay_chn][core // TEST_CASE: Test that relays run in the reverse direction and update their state TEST_CASE("Relay channels run reverse and update state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - for (uint8_t i = 0; i < relay_chn_count; i++) { relay_chn_run_reverse(i); // relay_chn_run_reverse returns void vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -88,9 +74,6 @@ TEST_CASE("Relay channels run reverse and update state", "[relay_chn][core]") { TEST_CASE("run_forward with ID_ALL sets all channels to FORWARD", "[relay_chn][core][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - relay_chn_run_forward(RELAY_CHN_ID_ALL); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -101,9 +84,6 @@ TEST_CASE("run_forward with ID_ALL sets all channels to FORWARD", "[relay_chn][c TEST_CASE("run_reverse with ID_ALL sets all channels to REVERSE", "[relay_chn][core][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - relay_chn_run_reverse(RELAY_CHN_ID_ALL); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -114,9 +94,6 @@ TEST_CASE("run_reverse with ID_ALL sets all channels to REVERSE", "[relay_chn][c TEST_CASE("stop with ID_ALL stops all running channels", "[relay_chn][core][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start all channels forward to ensure they are in a known running state relay_chn_run_forward(RELAY_CHN_ID_ALL); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -136,9 +113,6 @@ TEST_CASE("stop with ID_ALL stops all running channels", "[relay_chn][core][id_a // TEST_CASE: Test that relays stop and transition to RELAY_CHN_STATE_IDLE // This test also verifies the transition to FREE state after a STOP command. TEST_CASE("Relay channels stop and update to FREE state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - for (uint8_t i = 0; i < relay_chn_count; i++) { // First, run forward to test stopping and transitioning to FREE state relay_chn_run_forward(i); // relay_chn_run_forward returns void @@ -159,9 +133,6 @@ TEST_CASE("Relay channels stop and update to FREE state", "[relay_chn][core]") { // TEST_CASE: Get state should return UNDEFINED when id is not valid TEST_CASE("Get state returns UNDEFINED when id is invalid", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - for (uint8_t i = 0; i < relay_chn_count; i++) { int invalid_id = relay_chn_count * 2 + i; TEST_ASSERT_EQUAL(RELAY_CHN_STATE_UNDEFINED, relay_chn_get_state(invalid_id)); @@ -177,9 +148,6 @@ TEST_CASE("Get state returns UNDEFINED when id is invalid", "[relay_chn][core]") // TEST_CASE: Get state string should return "UNKNOWN" when id is not valid TEST_CASE("Get state string returns UNKNOWN when id is invalid", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - for (uint8_t i = 0; i < relay_chn_count; i++) { int invalid_id = relay_chn_count * 2 + i; TEST_ASSERT_EQUAL_STRING("UNKNOWN", relay_chn_get_state_str(invalid_id)); @@ -195,9 +163,6 @@ TEST_CASE("Get state string returns UNKNOWN when id is invalid", "[relay_chn][co // TEST_CASE: Test independent operation of multiple relay channels TEST_CASE("Multiple channels can operate independently", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - if (relay_chn_count >= 2) { // Start Channel 0 in forward direction relay_chn_run_forward(0); // relay_chn_run_forward returns void @@ -237,9 +202,6 @@ TEST_CASE("Multiple channels can operate independently", "[relay_chn][core]") { TEST_CASE("Forward to Reverse transition with opposite inertia", "[relay_chn][core][inertia]") { uint8_t ch = 0; // Channel to test - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start in forward direction relay_chn_run_forward(ch); // relay_chn_run_forward returns void vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Short delay for state stabilization @@ -261,9 +223,6 @@ TEST_CASE("Forward to Reverse transition with opposite inertia", "[relay_chn][co TEST_CASE("Reverse to Forward transition with opposite inertia", "[relay_chn][core][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start in reverse direction relay_chn_run_reverse(ch); // relay_chn_run_reverse returns void vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -284,9 +243,6 @@ TEST_CASE("Reverse to Forward transition with opposite inertia", "[relay_chn][co TEST_CASE("Running in same direction does not incur inertia", "[relay_chn][core][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start in forward direction relay_chn_run_forward(ch); // relay_chn_run_forward returns void vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -305,9 +261,6 @@ TEST_CASE("Running in same direction does not incur inertia", "[relay_chn][core] TEST_CASE("FREE to Running transition without inertia", "[relay_chn][core][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // setUp() should have already brought the channel to FREE state TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(ch)); @@ -322,8 +275,6 @@ TEST_CASE("FREE to Running transition without inertia", "[relay_chn][core][inert TEST_CASE("Single channel direction can be flipped", "[relay_chn][core][direction]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; const uint8_t ch = 0; // 1. Initial direction should be default @@ -346,9 +297,6 @@ TEST_CASE("Single channel direction can be flipped", "[relay_chn][core][directio TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][core][direction][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Flip all channels relay_chn_flip_direction(RELAY_CHN_ID_ALL); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); @@ -370,8 +318,6 @@ TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][c TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn][core][direction]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; const uint8_t ch = 0; // 1. Start channel running and verify state @@ -394,8 +340,6 @@ TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][core][direction]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; const uint8_t invalid_ch = relay_chn_count + 5; relay_chn_flip_direction(invalid_ch); // Call with an invalid ID diff --git a/test_apps/main/test_relay_chn_core_single.c b/test_apps/main/test_relay_chn_core_single.c index 734f4bf..3c3f4a8 100644 --- a/test_apps/main/test_relay_chn_core_single.c +++ b/test_apps/main/test_relay_chn_core_single.c @@ -22,17 +22,11 @@ TEST_CASE("relay_chn_create handles invalid arguments", "[relay_chn][core]") // TEST_CASE: Test that relay channels initialize correctly to RELAY_CHN_STATE_IDLE TEST_CASE("Relay channels initialize correctly to IDLE state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); } // TEST_CASE: Test that relays run in the forward direction and update their state TEST_CASE("Relay channels run forward and update state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - relay_chn_run_forward(); // Short delay for state to update vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -41,9 +35,6 @@ TEST_CASE("Relay channels run forward and update state", "[relay_chn][core]") { // TEST_CASE: Test that relays run in the reverse direction and update their state TEST_CASE("Relay channels run reverse and update state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - relay_chn_run_reverse(); // relay_chn_run_reverse returns void vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state()); @@ -53,9 +44,6 @@ TEST_CASE("Relay channels run reverse and update state", "[relay_chn][core]") { // TEST_CASE: Test that relays stop and transition to RELAY_CHN_STATE_IDLE // This test also verifies the transition to IDLE state after a STOP command. TEST_CASE("Relay channels stop and update to IDLE state", "[relay_chn][core]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // First, run forward to test stopping and transitioning to IDLE state relay_chn_run_forward(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -80,9 +68,6 @@ TEST_CASE("Relay channels stop and update to IDLE state", "[relay_chn][core]") { // TEST_CASE: Test transition from forward to reverse with inertia and state checks // Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_REVERSE TEST_CASE("Forward to Reverse transition with opposite inertia", "[relay_chn][core][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start in forward direction relay_chn_run_forward(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Short delay for state stabilization @@ -102,9 +87,6 @@ TEST_CASE("Forward to Reverse transition with opposite inertia", "[relay_chn][co // TEST_CASE: Test transition from reverse to forward with inertia and state checks // Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_run_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_FORWARD TEST_CASE("Reverse to Forward transition with opposite inertia", "[relay_chn][core][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start in reverse direction relay_chn_run_reverse(); // relay_chn_run_reverse returns void vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -123,9 +105,6 @@ TEST_CASE("Reverse to Forward transition with opposite inertia", "[relay_chn][co // TEST_CASE: Test issuing the same run command while already running (no inertia expected) // Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD TEST_CASE("Running in same direction does not incur inertia", "[relay_chn][core][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start in forward direction relay_chn_run_forward(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); @@ -142,9 +121,6 @@ TEST_CASE("Running in same direction does not incur inertia", "[relay_chn][core] // TEST_CASE: Test transition from IDLE state to running (no inertia expected) // Scenario: RELAY_CHN_STATE_IDLE -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD TEST_CASE("IDLE to Running transition without inertia", "[relay_chn][core][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // setUp() should have already brought the channel to IDLE state TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); @@ -159,9 +135,6 @@ TEST_CASE("IDLE to Running transition without inertia", "[relay_chn][core][inert TEST_CASE("Single channel direction can be flipped", "[relay_chn][core][direction]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Initial direction should be default TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction()); @@ -182,9 +155,6 @@ TEST_CASE("Single channel direction can be flipped", "[relay_chn][core][directio TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn][core][direction]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Start channel running and verify state relay_chn_run_forward(); vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); diff --git a/test_apps/main/test_relay_chn_listener_multi.c b/test_apps/main/test_relay_chn_listener_multi.c index 1d368d5..1f8d979 100644 --- a/test_apps/main/test_relay_chn_listener_multi.c +++ b/test_apps/main/test_relay_chn_listener_multi.c @@ -37,9 +37,6 @@ static void test_listener_2(uint8_t chn_id, relay_chn_state_t old_state, relay_c TEST_CASE("Listener is called on state change", "[relay_chn][listener]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); // 1. Register the listener @@ -61,9 +58,6 @@ TEST_CASE("Listener is called on state change", "[relay_chn][listener]") { TEST_CASE("Unregistered listener is not called", "[relay_chn][listener]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); // 1. Register and then immediately unregister the listener @@ -80,9 +74,6 @@ TEST_CASE("Unregistered listener is not called", "[relay_chn][listener]") { TEST_CASE("Multiple listeners are called on state change", "[relay_chn][listener]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); reset_listener_info(&listener2_info); @@ -110,9 +101,6 @@ TEST_CASE("Multiple listeners are called on state change", "[relay_chn][listener } TEST_CASE("Listener registration handles invalid arguments and duplicates", "[relay_chn][listener]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); // 1. Registering a NULL listener should fail diff --git a/test_apps/main/test_relay_chn_listener_single.c b/test_apps/main/test_relay_chn_listener_single.c index 4346967..9d0970d 100644 --- a/test_apps/main/test_relay_chn_listener_single.c +++ b/test_apps/main/test_relay_chn_listener_single.c @@ -35,9 +35,6 @@ static void test_listener_2(uint8_t chn_id, relay_chn_state_t old_state, relay_c // ### Listener Functionality Tests TEST_CASE("Listener is called on state change", "[relay_chn][listener]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); // 1. Register the listener @@ -57,9 +54,6 @@ TEST_CASE("Listener is called on state change", "[relay_chn][listener]") { } TEST_CASE("Unregistered listener is not called", "[relay_chn][listener]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); // 1. Register and then immediately unregister the listener @@ -75,9 +69,6 @@ TEST_CASE("Unregistered listener is not called", "[relay_chn][listener]") { } TEST_CASE("Multiple listeners are called on state change", "[relay_chn][listener]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); reset_listener_info(&listener2_info); @@ -105,9 +96,6 @@ TEST_CASE("Multiple listeners are called on state change", "[relay_chn][listener } TEST_CASE("Listener registration handles invalid arguments and duplicates", "[relay_chn][listener]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - reset_listener_info(&listener1_info); // 1. Registering a NULL listener should fail diff --git a/test_apps/main/test_relay_chn_nvs_multi.c b/test_apps/main/test_relay_chn_nvs_multi.c new file mode 100644 index 0000000..c17721f --- /dev/null +++ b/test_apps/main/test_relay_chn_nvs_multi.c @@ -0,0 +1,140 @@ +/* + * SPDX-FileCopyrightText: 2025 Kozmotronik Tech + * + * SPDX-License-Identifier: MIT + */ + +#include +#include "unity.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "relay_chn_nvs.h" + +TEST_CASE("Test relay storage init/deinit", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + // Test all channels + relay_chn_direction_t dir; + relay_chn_direction_t test_directions[] = { + RELAY_CHN_DIRECTION_DEFAULT, + RELAY_CHN_DIRECTION_FLIPPED + }; + + for (int channel = 0; channel < 2; channel++) { + TEST_ESP_OK(relay_chn_nvs_set_direction(channel, test_directions[channel])); + TEST_ESP_OK(relay_chn_nvs_get_direction(channel, &dir)); + TEST_ASSERT_EQUAL(test_directions[channel], dir); + } + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test invalid parameters", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + // Test NULL pointer for all channels + for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) { + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL)); + } + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + // Store some test data first + relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED; + for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) { + TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction)); + } + +#ifdef RELAY_CHN_ENABLE_TILTING + uint8_t sensitivity = 50; + for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) { + TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity)); + TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100, 200)); + } +#endif + + // Test erase all + TEST_ESP_OK(relay_chn_nvs_erase_all()); + + // Verify data was erased by trying to read it back + relay_chn_direction_t read_direction; + TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction)); + +#ifdef RELAY_CHN_ENABLE_TILTING + uint8_t read_sensitivity; + TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity)); + + uint32_t fwd_count, rev_count; + TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &fwd_count, &rev_count)); +#endif + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +#ifdef RELAY_CHN_ENABLE_TILTING +TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + const uint8_t test_sensitivity = 75; + uint8_t sensitivity; + + // Test all channels + for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) { + TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, test_sensitivity)); + TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(channel, &sensitivity)); + TEST_ASSERT_EQUAL(test_sensitivity, sensitivity); + } + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + const uint32_t fwd_count = 100; + const uint32_t rev_count = 200; + uint32_t fwd_read, rev_read; + + // Test all channels + for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) { + // Test setting counters + TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, fwd_count, rev_count)); + TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &fwd_read, &rev_read)); + TEST_ASSERT_EQUAL(fwd_count, fwd_read); + TEST_ASSERT_EQUAL(rev_count, rev_read); + } + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + uint32_t fwd_count, rev_count; + + // Test NULL pointers for all channels + for (int channel = 0; channel < RELAY_CHN_COUNT; channel++) { + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL, &rev_count)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, &fwd_count, NULL)); + } + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} +#endif // RELAY_CHN_ENABLE_TILTING \ No newline at end of file diff --git a/test_apps/main/test_relay_chn_nvs_single.c b/test_apps/main/test_relay_chn_nvs_single.c new file mode 100644 index 0000000..3a21638 --- /dev/null +++ b/test_apps/main/test_relay_chn_nvs_single.c @@ -0,0 +1,126 @@ +/* + * SPDX-FileCopyrightText: 2025 Kozmotronik Tech + * + * SPDX-License-Identifier: MIT + */ + +#include +#include "unity.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "relay_chn_nvs.h" + + +TEST_CASE("Test relay storage init/deinit", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + // Test channel 0 + TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_DEFAULT)); + relay_chn_direction_t dir; + TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir)); + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, dir); + + // Test channel 1 + TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_FLIPPED)); + TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir)); + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, dir); + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test invalid parameters", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + // Test NULL pointer + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(0, NULL)); + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + // Store some test data first + relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED; + TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction)); + +#ifdef RELAY_CHN_ENABLE_TILTING + uint8_t sensitivity = 50; + TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity)); + TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100, 200)); +#endif + + // Test erase all + TEST_ESP_OK(relay_chn_nvs_erase_all()); + + // Verify data was erased by trying to read it back + relay_chn_direction_t read_direction; + TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction)); + +#ifdef RELAY_CHN_ENABLE_TILTING + uint8_t read_sensitivity; + TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity)); + + uint32_t fwd_count, rev_count; + TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &fwd_count, &rev_count)); +#endif + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +#ifdef RELAY_CHN_ENABLE_TILTING +TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + const uint8_t test_sensitivity = 75; + TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, test_sensitivity)); + + uint8_t sensitivity; + TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &sensitivity)); + TEST_ASSERT_EQUAL(test_sensitivity, sensitivity); + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + const uint32_t fwd_count = 100; + const uint32_t rev_count = 200; + + // Test setting counters + TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, fwd_count, rev_count)); + + uint32_t fwd_read, rev_read; + TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &fwd_read, &rev_read)); + TEST_ASSERT_EQUAL(fwd_count, fwd_read); + TEST_ASSERT_EQUAL(rev_count, rev_read); + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} + +TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]") +{ + TEST_ESP_OK(relay_chn_nvs_init()); + + uint32_t fwd_count, rev_count; + + // Test NULL pointers + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(0, NULL)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL, &rev_count)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, &fwd_count, NULL)); + + TEST_ESP_OK(relay_chn_nvs_deinit()); +} +#endif // RELAY_CHN_ENABLE_TILTING \ No newline at end of file diff --git a/test_apps/main/test_relay_chn_tilt_multi.c b/test_apps/main/test_relay_chn_tilt_multi.c index 916afc3..2f17e47 100644 --- a/test_apps/main/test_relay_chn_tilt_multi.c +++ b/test_apps/main/test_relay_chn_tilt_multi.c @@ -14,6 +14,10 @@ // Helper function to prepare channel for tilt tests void prepare_channel_for_tilt(uint8_t chn_id, int initial_cmd) { + // Ensure the channel reset tilt control + relay_chn_tilt_stop(chn_id); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + // Ensure the channel has had a 'last_run_cmd' if (initial_cmd == RELAY_CHN_CMD_FORWARD) { relay_chn_run_forward(chn_id); @@ -31,9 +35,6 @@ void prepare_channel_for_tilt(uint8_t chn_id, int initial_cmd) { TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); @@ -58,9 +59,6 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running reverse first to set last_run_cmd prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE); @@ -83,9 +81,6 @@ TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][ti TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(ch)); // Ensure we are back to FREE @@ -102,9 +97,6 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running reverse first to set last_run_cmd prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(ch)); // Ensure we are back to FREE @@ -120,9 +112,6 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd, then tilt prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(ch); // Go to tilt state @@ -142,9 +131,6 @@ TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][ti TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running reverse first to set last_run_cmd, then tilt prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE); relay_chn_tilt_reverse(ch); // Go to tilt state @@ -163,9 +149,6 @@ TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][ti TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd, then tilt prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(ch); // Go to tilt state @@ -183,9 +166,6 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn] TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd, then tilt prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(ch); // Go to tilt state @@ -203,9 +183,6 @@ TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_ TEST_CASE("tilt_forward with ID_ALL sets all channels to TILT_FORWARD", "[relay_chn][tilt][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Prepare all channels. for (uint8_t i = 0; i < relay_chn_count; i++) { prepare_channel_for_tilt(i, RELAY_CHN_CMD_FORWARD); @@ -223,9 +200,6 @@ TEST_CASE("tilt_forward with ID_ALL sets all channels to TILT_FORWARD", "[relay_ TEST_CASE("tilt_reverse with ID_ALL sets all channels to TILT_REVERSE", "[relay_chn][tilt][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Prepare all channels. for (uint8_t i = 0; i < relay_chn_count; i++) { prepare_channel_for_tilt(i, RELAY_CHN_CMD_REVERSE); @@ -243,9 +217,6 @@ TEST_CASE("tilt_reverse with ID_ALL sets all channels to TILT_REVERSE", "[relay_ TEST_CASE("tilt_stop with ID_ALL stops all tilting channels", "[relay_chn][tilt][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // 1. Prepare and start all channels tilting forward for (uint8_t i = 0; i < relay_chn_count; i++) { prepare_channel_for_tilt(i, RELAY_CHN_CMD_REVERSE); @@ -265,9 +236,6 @@ TEST_CASE("tilt_stop with ID_ALL stops all tilting channels", "[relay_chn][tilt] TEST_CASE("tilt_auto with ID_ALL tilts channels based on last run direction", "[relay_chn][tilt][id_all]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // This test requires at least 2 channels to demonstrate different behaviors TEST_ASSERT_GREATER_OR_EQUAL_MESSAGE(2, relay_chn_count, "Test requires at least 2 channels"); @@ -287,9 +255,6 @@ TEST_CASE("tilt_auto with ID_ALL tilts channels based on last run direction", "[ // Test relay_chn_tilt_auto() chooses correct tilt direction TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][auto]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare FORWARD prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); relay_chn_tilt_auto(ch); @@ -309,9 +274,6 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivity]") { uint8_t ch = 0; uint8_t val = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - relay_chn_tilt_set_sensitivity(ch, 0); TEST_ESP_OK(relay_chn_tilt_get_sensitivity(ch, &val, 1)); TEST_ASSERT_EQUAL_UINT8(0, val); @@ -336,9 +298,6 @@ TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivi // Test tilt counter logic: forward x3, reverse x3, extra reverse fails TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); // Tilt forward 3 times @@ -372,9 +331,6 @@ TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][ti // Test run command during TILT state TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][tilt][run-during-tilt]") { uint8_t ch = 0; - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(ch); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); diff --git a/test_apps/main/test_relay_chn_tilt_single.c b/test_apps/main/test_relay_chn_tilt_single.c index 7a0b8b0..b302838 100644 --- a/test_apps/main/test_relay_chn_tilt_single.c +++ b/test_apps/main/test_relay_chn_tilt_single.c @@ -14,6 +14,10 @@ // Helper function to prepare channel for tilt tests void prepare_channel_for_tilt(int initial_cmd) { + // Ensure the channel reset tilt control + relay_chn_tilt_stop(); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + // Ensure the channel has had a 'last_run_cmd' if (initial_cmd == RELAY_CHN_CMD_FORWARD) { relay_chn_run_forward(); @@ -29,9 +33,6 @@ void prepare_channel_for_tilt(int initial_cmd) { // TEST_CASE: Test transition from running forward to tilt forward // Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); @@ -54,9 +55,6 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti // TEST_CASE: Test transition from running reverse to tilt reverse // Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running reverse first to set last_run_cmd prepare_channel_for_tilt(RELAY_CHN_CMD_REVERSE); @@ -77,9 +75,6 @@ TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][ti // TEST_CASE: Test transition from FREE state to tilt forward (now with preparation) // Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); // Ensure we are back to FREE @@ -94,9 +89,6 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn // TEST_CASE: Test transition from FREE state to tilt reverse (now with preparation) // Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running reverse first to set last_run_cmd prepare_channel_for_tilt(RELAY_CHN_CMD_REVERSE); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); // Ensure we are back to FREE @@ -110,9 +102,6 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn // TEST_CASE: Test transition from tilt forward to run forward (inertia expected for run) // Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd, then tilt prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(); // Go to tilt state @@ -130,9 +119,6 @@ TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][ti // TEST_CASE: Test transition from tilt reverse to run reverse (no inertia expected for run) // Scenario: RELAY_CHN_STATE_TILT_REVERSE -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running reverse first to set last_run_cmd, then tilt prepare_channel_for_tilt(RELAY_CHN_CMD_REVERSE); relay_chn_tilt_reverse(); // Go to tilt state @@ -149,9 +135,6 @@ TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][ti // TEST_CASE: Test transition from tilt forward to run reverse (without inertia) // Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd, then tilt prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(); // Go to tilt state @@ -167,9 +150,6 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn] // TEST_CASE: Test stopping from a tilt state (no inertia for stop command itself) // Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_stop) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_IDLE TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare channel by running forward first to set last_run_cmd, then tilt prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(); // Go to tilt state @@ -185,9 +165,6 @@ TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_ // Test relay_chn_tilt_auto() chooses correct tilt direction TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][auto]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - // Prepare FORWARD prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_auto(); @@ -205,9 +182,6 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au // Test sensitivity set/get TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivity]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - relay_chn_tilt_set_sensitivity(0); TEST_ASSERT_EQUAL_UINT8(0, relay_chn_tilt_get_sensitivity()); @@ -223,9 +197,6 @@ TEST_CASE("relay_chn_tilt_set_sensitivity and get", "[relay_chn][tilt][sensitivi // Test tilt counter logic: forward x3, reverse x3, extra reverse fails TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); // Tilt forward 3 times @@ -258,9 +229,6 @@ TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][ti // Test run command during TILT state TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][tilt][run-during-tilt]") { - TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); - g_is_component_initialized = true; - prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(); vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); diff --git a/test_apps/partition_table/partitionTable.csv b/test_apps/partition_table/partitionTable.csv deleted file mode 100644 index 4f87a1f..0000000 --- a/test_apps/partition_table/partitionTable.csv +++ /dev/null @@ -1,5 +0,0 @@ -# ESP-IDF Partition Table -# Name, Type, SubType, Offset, Size, Flags -nvs,data,nvs,0xa000,24K, -phy_init,data,phy,0x10000,4K, -factory,app,factory,0x20000,1M, diff --git a/test_apps/partitions/part_nvs.csv b/test_apps/partitions/part_nvs.csv new file mode 100644 index 0000000..ef2a150 --- /dev/null +++ b/test_apps/partitions/part_nvs.csv @@ -0,0 +1,6 @@ +# ESP-IDF Partition Table +# Name, Type, SubType, Offset, Size, Flags +nvs,data,nvs,0xa000,24K,, +phy_init,data,phy,0x10000,4K,, +factory,app,factory,0x20000,1M,, +app_data,data,nvs,,8K,, diff --git a/test_apps/sdkconfig b/test_apps/sdkconfig index a20e982..386f402 100644 --- a/test_apps/sdkconfig +++ b/test_apps/sdkconfig @@ -395,13 +395,13 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 # # Partition Table # -CONFIG_PARTITION_TABLE_SINGLE_APP=y +# CONFIG_PARTITION_TABLE_SINGLE_APP is not set # CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set # CONFIG_PARTITION_TABLE_TWO_OTA is not set # CONFIG_PARTITION_TABLE_TWO_OTA_LARGE is not set -# CONFIG_PARTITION_TABLE_CUSTOM is not set -CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" -CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv" +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions/part_nvs.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions/part_nvs.csv" CONFIG_PARTITION_TABLE_OFFSET=0x8000 CONFIG_PARTITION_TABLE_MD5=y # end of Partition Table @@ -1162,6 +1162,13 @@ CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC_HRT=y # CONFIG_NEWLIB_TIME_SYSCALL_USE_NONE is not set # end of Newlib +# +# NVS +# +# CONFIG_NVS_ASSERT_ERROR_CHECK is not set +# CONFIG_NVS_LEGACY_DUP_KEYS_COMPATIBILITY is not set +# end of NVS + # # PThreads # @@ -1263,7 +1270,16 @@ CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS=200 CONFIG_RELAY_CHN_COUNT=1 CONFIG_RELAY_CHN_ENABLE_TILTING=y +CONFIG_RELAY_CHN_ENABLE_NVS=y # end of Relay Channel Driver Configuration + +# +# Relay Channel NVS Storage Configuration +# +CONFIG_RELAY_CHN_NVS_NAMESPACE="relay_chn" +CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION=y +CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION_NAME="app_data" +# end of Relay Channel NVS Storage Configuration # end of Component config # CONFIG_IDF_EXPERIMENTAL_FEATURES is not set diff --git a/test_apps/sdkconfig.defaults b/test_apps/sdkconfig.defaults index b7641e3..53c4d00 100644 --- a/test_apps/sdkconfig.defaults +++ b/test_apps/sdkconfig.defaults @@ -5,4 +5,5 @@ CONFIG_ESP_TASK_WDT_INIT=n # Keep this as short as possible for tests CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS=200 CONFIG_RELAY_CHN_COUNT=2 -CONFIG_RELAY_CHN_ENABLE_TILTING=y \ No newline at end of file +CONFIG_RELAY_CHN_ENABLE_TILTING=y +CONFIG_RELAY_CHN_ENABLE_NVS=y \ No newline at end of file diff --git a/test_apps/sdkconfig.defaults.custom_nvs b/test_apps/sdkconfig.defaults.custom_nvs new file mode 100644 index 0000000..cc1df53 --- /dev/null +++ b/test_apps/sdkconfig.defaults.custom_nvs @@ -0,0 +1,15 @@ +# Disable task WDT for tests +CONFIG_ESP_TASK_WDT_INIT=n + +# Partition configuration +CONFIG_PARTITION_TABLE_SINGLE_APP=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions/part_nvs.csv" + +# Relay Channel Driver Default Configuration for Testing +# Keep this as short as possible for tests +CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS=200 +CONFIG_RELAY_CHN_COUNT=2 +CONFIG_RELAY_CHN_ENABLE_TILTING=y +CONFIG_RELAY_CHN_ENABLE_NVS=y +CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION=y \ No newline at end of file diff --git a/test_apps/sdkconfig.defaults.single b/test_apps/sdkconfig.defaults.single index 13ad935..b468a54 100644 --- a/test_apps/sdkconfig.defaults.single +++ b/test_apps/sdkconfig.defaults.single @@ -5,4 +5,5 @@ CONFIG_ESP_TASK_WDT_INIT=n # Keep this as short as possible for tests CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS=200 CONFIG_RELAY_CHN_COUNT=1 -CONFIG_RELAY_CHN_ENABLE_TILTING=y \ No newline at end of file +CONFIG_RELAY_CHN_ENABLE_TILTING=y +CONFIG_RELAY_CHN_ENABLE_NVS=y \ No newline at end of file diff --git a/test_apps/sdkconfig.defaults.single.custom_nvs b/test_apps/sdkconfig.defaults.single.custom_nvs new file mode 100644 index 0000000..9cbcb0c --- /dev/null +++ b/test_apps/sdkconfig.defaults.single.custom_nvs @@ -0,0 +1,15 @@ +# Disable task WDT for tests +CONFIG_ESP_TASK_WDT_INIT=n + +# Partition configuration +CONFIG_PARTITION_TABLE_SINGLE_APP=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions/part_nvs.csv" + +# Relay Channel Driver Default Configuration for Testing +# Keep this as short as possible for tests +CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS=200 +CONFIG_RELAY_CHN_COUNT=1 +CONFIG_RELAY_CHN_ENABLE_TILTING=y +CONFIG_RELAY_CHN_ENABLE_NVS=y +CONFIG_RELAY_CHN_NVS_CUSTOM_PARTITION=y \ No newline at end of file