Fix NVS key bug and optimize for single mode

- Fixed a critical NVS key generation bug that would cause overwriting the values for all channels.
- Optimized the code for single channel mode since no formatting required.
- Improved multi-channel test coverage to cover that each value for each channel stored correctly.

Refs #1096, #1098 and fixes #1100
This commit is contained in:
2025-09-02 17:02:05 +03:00
parent 5e8e5a4cab
commit 0122ef0803
2 changed files with 124 additions and 44 deletions

View File

@@ -54,15 +54,15 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
// Store some test data first
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED;
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
}
// Set direction for all channels
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
TEST_ESP_OK(relay_chn_nvs_set_direction(1, direction));
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t sensitivity = 50;
for (int channel = 0; channel < CONFIG_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));
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, sensitivity));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, 100 + channel));
}
#endif
@@ -74,11 +74,13 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction));
#if CONFIG_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));
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(channel, &read_sensitivity));
uint16_t tilt_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &tilt_count));
uint16_t tilt_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(channel, &tilt_count));
}
#endif
TEST_ESP_OK(relay_chn_nvs_deinit());
@@ -89,14 +91,34 @@ TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint16_t run_limit_sec = 32;
// Use different values for each channel to detect overwrites
uint16_t test_limits[CONFIG_RELAY_CHN_COUNT];
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
test_limits[i] = 30 + i; // e.g., 30, 31, 32...
}
// 1. Set all values first
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ESP_OK(relay_chn_nvs_set_run_limit(i, test_limits[i]));
}
// 2. Then, read them all back and verify
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ESP_OK(relay_chn_nvs_set_run_limit(i, run_limit_sec));
uint16_t run_limit_read;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(i, &run_limit_read));
TEST_ASSERT_EQUAL(run_limit_sec, run_limit_read);
TEST_ASSERT_EQUAL_UINT16(test_limits[i], run_limit_read);
}
// 3. Verify that changing one channel doesn't affect another
uint16_t new_limit_ch0 = 99;
TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, new_limit_ch0));
uint16_t read_val_ch0, read_val_ch1;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &read_val_ch0));
TEST_ESP_OK(relay_chn_nvs_get_run_limit(1, &read_val_ch1));
TEST_ASSERT_EQUAL_UINT16(new_limit_ch0, read_val_ch0);
TEST_ASSERT_EQUAL_UINT16(test_limits[1], read_val_ch1); // Should still be the old value
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif
@@ -106,14 +128,21 @@ 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;
uint8_t test_sensitivities[CONFIG_RELAY_CHN_COUNT];
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
test_sensitivities[i] = 70 + i; // e.g., 70, 71, 72...
}
// Test all channels
for (int channel = 0; channel < CONFIG_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);
// 1. Set all values first
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(i, test_sensitivities[i]));
}
// 2. Then, read them all back and verify
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint8_t sensitivity_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(i, &sensitivity_read));
TEST_ASSERT_EQUAL_UINT8(test_sensitivities[i], sensitivity_read);
}
TEST_ESP_OK(relay_chn_nvs_deinit());
@@ -123,15 +152,21 @@ TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint16_t tilt_count = 100;
uint16_t tilt_count_read;
uint16_t test_counts[CONFIG_RELAY_CHN_COUNT];
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
test_counts[i] = 100 + i; // e.g., 100, 101, 102...
}
// Test all channels
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
// Test setting counters
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, tilt_count));
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &tilt_count_read));
TEST_ASSERT_EQUAL(tilt_count, tilt_count_read);
// 1. Set all values first
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(i, test_counts[i]));
}
// 2. Then, read them all back and verify
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint16_t count_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(i, &count_read));
TEST_ASSERT_EQUAL_UINT16(test_counts[i], count_read);
}
TEST_ESP_OK(relay_chn_nvs_deinit());