Hi! I have same issue with store array of struct to NVS.
I'm have struct with embedded other struct:
Code: Select all
// Struct for store webhook notification
typedef struct
{
bool enable;
char url[128];
char username[20];
char password[20];
} webhook_notification_s;
// Struct for store gpio events action
typedef struct
{
uint8_t input_gpio; // Input GPIO number
uint8_t trigger_level; // trigger on high level or low level
uint8_t output_gpio; // Output GPIO for relay
webhook_notification_s webhook_url; // URL for HTTP notify
uint32_t timeout_ms; // delay timeout in milliseconds
} gpio_action_t;
I have my functions for read and write struct to NVS :
Code: Select all
esp_err_t read_struct(const char *key, void **read_struct, size_t size)
{
nvs_handle_t handle;
esp_err_t err;
err = nvs_open("storage", NVS_READWRITE, &handle);
err = nvs_get_blob(handle, key, *read_struct, &size);
DEBUG_NVS((err != ESP_OK) ? "[NVS] Read %s struct = Failed!\n" : "[NVS] Read %s struct = Done\n", key);
nvs_close(handle);
return err;
}
esp_err_t write_struct(const char *key, void *write_struct, size_t size)
{
nvs_handle_t handle;
esp_err_t err;
err = nvs_open("storage", NVS_READWRITE, &handle);
err = nvs_set_blob(handle, key, write_struct, size);
err = nvs_commit(handle);
DEBUG_NVS((err != ESP_OK) ? "[NVS] Write %s struct = Failed!\n" : "[NVS] Write %s struct = Done\n", key);
nvs_close(handle);
return err;
}
I create array with 8 items (size) and try to write:
Code: Select all
// defined array gpio_actions[8]
gpio_action_t *gpio_actions = (gpio_action_t *)malloc(8 * sizeof(gpio_action_t));
for (int i = 0; i < 8; i++)
{
gpio_action_t *gpio_action_test = malloc(sizeof(gpio_action_t));
webhook_notification_s *wh_test = malloc(sizeof(webhook_notification_s));
wh_test->enable = true;
strcpy(wh_test->url, "http://test.local/hello-webhook?test=1");
strcpy(wh_test->username, "admin");
strcpy(wh_test->password, "password");
gpio_action_test->input_gpio = 10;
gpio_action_test->output_gpio = 12;
gpio_action_test->timeout_ms = 1000;
gpio_action_test->trigger_level = i;
memcpy(&gpio_action_test->webhook_url, wh_test, sizeof(webhook_notification_s));
free(wh_test);
memcpy(&gpio_actions[i], gpio_action_test, sizeof(gpio_action_t));
free(gpio_action_test);
}
// debug prints
for (int i = 0; i < 8; i++)
{
printf("-------%d ----\n", i);
printf("gpio input: %u\n", gpio_actions[i].input_gpio);
printf("gpio out : %u\n", gpio_actions[i].output_gpio);
printf("gpio timeout: %d\n", (int)gpio_actions[i].timeout_ms);
printf("gpio level: %u\n", gpio_actions[i].trigger_level);
printf("gpio wh en: %d\n", gpio_actions[i].webhook_url.enable);
printf("gpio wh url: %s\n", gpio_actions[i].webhook_url.url);
printf("gpio wh user: %s\n", gpio_actions[i].webhook_url.username);
printf("gpio wh password: %s\n", gpio_actions[i].webhook_url.password);
printf("----------\n");
}
esp_err_t err = write_gpio_actions(gpio_actions);
if (err!=ESP_OK) {
printf("--> %s\n", esp_err_to_name(err));
}
free(gpio_actions);
Write done, without errors. Debug prints values is OK.
Then i try to read:
Code: Select all
gpio_action_t *gpio_actions_read = (gpio_action_t *)malloc(8 * sizeof(gpio_action_t));
if (read_gpio_actions(gpio_actions_read) == ESP_ERR_NVS_NOT_FOUND)
{
ESP_LOGE(TAG, "failed to read gpio actions");
free(gpio_actions_read);
return;
}
// debug prints
for (int i = 0; i < 8; i++)
{
printf("-------%d ----\n", i);
printf("gpio input: %u\n", gpio_actions_read[i].input_gpio);
printf("gpio out : %u\n", gpio_actions_read[i].output_gpio);
printf("gpio timeout: %d\n", (int)gpio_actions_read[i].timeout_ms);
printf("gpio level: %u\n", gpio_actions_read[i].trigger_level);
printf("gpio wh en: %d\n", gpio_actions_read[i].webhook_url.enable);
printf("gpio wh url: %s\n", gpio_actions_read[i].webhook_url.url);
printf("gpio wh user: %s\n", gpio_actions_read[i].webhook_url.username);
printf("gpio wh password: %s\n", gpio_actions_read[i].webhook_url.password);
printf("----------\n");
}
free(gpio_actions_read);
In read section, debug prints output wrong value in some fields for first and last items.
Code: Select all
[NVS] Read gpa struct = Done
-------0 ----
gpio input: 140 //<-- invalid value
gpio out : 202 //<-- invalid value
gpio timeout: 1000
gpio level: 135 //<-- invalid value
gpio wh en: 63 //<-- invalid value
gpio wh url: ���?://test.local/hello-webhook?test=1 //<-- invalid value, prefix with wrong characters
gpio wh user: admin
gpio wh password: password
----------
-------1 ----
gpio input: 10
gpio out : 12
gpio timeout: 1000
gpio level: 1
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
-------2 ----
gpio input: 10
gpio out : 12
gpio timeout: 1000
gpio level: 2
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
-------3 ----
gpio input: 10
gpio out : 12
gpio timeout: 1000
gpio level: 3
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
-------4 ----
gpio input: 10
gpio out : 12
gpio timeout: 1000
gpio level: 4
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
-------5 ----
gpio input: 10
gpio out : 12
gpio timeout: 1000
gpio level: 5
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
-------6 ----
gpio input: 10
gpio out : 12
gpio timeout: 1000
gpio level: 6
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
-------7 ----
gpio input: 10
gpio out : 12
gpio timeout: 1070280396 //<-- invalid value
gpio level: 7
gpio wh en: 1
gpio wh url: http://test.local/hello-webhook?test=1
gpio wh user: admin
gpio wh password: password
----------
Could anyone explain why?