|
esp_iot_framework
v0.1.0-alpha
© 2026 AmakeSasha, distributed under a license Apache-2.0
|
NVS extension layer for data persistence and configuration profiles. More...
String | |
Functions for reading and writing strings to NVS with length validation. | |
| esp_err_t | eif_nvs_value_save (const char *const key, const char *const value, size_t min_len, size_t max_len, bool it_can_be_empty) |
| Validates string length and writes it to NVS. More... | |
| esp_err_t | eif_nvs_value_load (const char *const key, char *const value_out, size_t max_len) |
| Loads a string from NVS into a pre-allocated buffer. More... | |
| esp_err_t | eif_nvs_value_load_malloc (const char *key, char **const value_out, size_t *const value_out_len) |
| Dynamically allocates memory and loads a string from NVS. More... | |
Wi-Fi profile | |
Functions for reading and writing Wi-Fi network credentials to NVS. | |
| esp_err_t | eif_nvs_wifi_profile_save (uint8_t index, const char *const ssid, const char *const pass) |
| Saves Wi-Fi network credentials for a specific profile index. More... | |
| esp_err_t | eif_nvs_wifi_profile_load (uint8_t index, char *const ssid_out, char *const pass_out) |
| Loads Wi-Fi network credentials for a specific profile index. More... | |
HTTP Basic Authentication | |
Functions for reading, encoding, and writing Basic Auth credentials to NVS. | |
| esp_err_t | eif_nvs_basic_auth_line_save (const unsigned char *const pass) |
| Encodes and saves the HTTP Basic Auth credentials to NVS. More... | |
| esp_err_t | eif_nvs_basic_auth_line_load (char *const buf_out) |
| Loads the complete HTTP Basic Auth credentials from NVS. More... | |
This module provides high-level functions for data persistence in NVS. It enforces runtime buffer protection, boundary validation, and key compatibility checks, completely preventing memory corruption and invalid configurations within the framework.
eif_core_initialize() and eif_nvs_initialize() must be executed exactly once.| esp_err_t eif_nvs_value_save | ( | const char *const | key, |
| const char *const | value, | ||
| size_t | min_len, | ||
| size_t | max_len, | ||
| bool | it_can_be_empty | ||
| ) |
Writes the passed string (value) to NVS using the passed key (key). Before writing, the function checks whether the string length is in the range from min_len to max_len.
Checking the length depending on it_can_be_empty (len it is length value):
it_can_be_empty is true: ((len >= min_len) && (len <= max_len)) || (len == 0)it_can_be_empty is false: ((len >= min_len) && (len <= max_len))| key | NVS storage key (maximum 15 characters). Cannot be NULL. |
| value | The string to save. Cannot be NULL. |
| min_len | Minimum allowed length (excluding the null-terminator). |
| max_len | Maximum allowed length (including the null-terminator). |
| it_can_be_empty | Allow saving an empty string, bypassing length limits. |
ESP_OK: String verified, written, and committed successfully.ESP_ERR_INVALID_ARG: The pointer key or value is NULL.ESP_ERR_INVALID_SIZE: The length of the value is outside the acceptable range.ESP_ERR_NVS_*: System errors propagated from nvs_open(), nvs_set_str(), or nvs_commit().Example of use:
| esp_err_t eif_nvs_value_load | ( | const char *const | key, |
| char *const | value_out, | ||
| size_t | max_len | ||
| ) |
Reads the string associated with the key and copies it into value_out. The function prevents buffer overflows by forcing the native read operation to respect the max_len limit, ensuring the output is safely stored within the allocated boundaries.
ESP_ERR_NVS_NOT_FOUND occurs and max_len > 1U, the value_out[0] is explicitly set to \0 to ensure safety.max_len must match the length of value_out, as this requires nvs_get_str(). Otherwise, data may be truncated.| key | NVS storage key (maximum 15 characters). Cannot be NULL. |
| value_out | Pointer to the buffer where the string will be stored. Cannot be NULL. |
| max_len | Maximum bytes allocated for value_out (including the null-terminator). |
ESP_OK: Value located and copied successfully.ESP_ERR_INVALID_ARG: The pointer key or value_out is NULL.ESP_ERR_NVS_NOT_FOUND: The key does not exist in storage.ESP_ERR_NVS_*: System errors propagated from nvs_open() or nvs_get_str().Example of use:
| esp_err_t eif_nvs_value_load_malloc | ( | const char * | key, |
| char **const | value_out, | ||
| size_t *const | value_out_len | ||
| ) |
Queries storage to determine the data size, allocates a buffer from the heap via pvPortMalloc(), and copies the string into it. If any operation fails, *value_out is guaranteed to be set to NULL and *value_out_len is set to 0U.
vPortFree(*value_out) at the beginning to clear pre-existing allocations.ESP_OK, the caller MUST release the memory using vPortFree(*value_out) (or the respective pointer variable) to avoid leaks.| key | NVS storage key (maximum 15 characters). Cannot be NULL. |
| value_out | Double pointer to store the allocated buffer address. Cannot be NULL. |
| value_out_len | Pointer to store the string length (including the \0). Cannot be NULL. |
ESP_OK: Memory allocated and string loaded successfully.ESP_ERR_INVALID_ARG: The pointer key, value_out, or value_out_len is NULL.ESP_ERR_NVS_NOT_FOUND: The key does not exist in storage.ESP_ERR_NO_MEM: Memory could not be allocated due to the lack of an empty block of the required size.ESP_ERR_NVS_*: System errors propagated from nvs_open() or nvs_get_str().Example of use:
| esp_err_t eif_nvs_wifi_profile_save | ( | uint8_t | index, |
| const char *const | ssid, | ||
| const char *const | pass | ||
| ) |
Writes the provided SSID and password to NVS under auto-generated keys. Before writing, the function verifies that the profile index is within the valid user range and checks the credential string lengths.
Valid index range check: (index >= 1) && (index <= WIFI_PROFILES_MAX_COUNT)
If both the ssid and the pass have a length of 0, the length checks are ignored. This is done to clear the specified slot. The verification is bypassed according to the following logic: (len(ssid) == 0) && (len(pass) == 0)
0 is system-reserved (read-only). If you try to overwrite it, you will get an error.| index | Profile index slot. Must be from 1 to EIF_WIFI_PROFILES_MAX_COUNT. |
| ssid | Wi-Fi SSID string. Cannot be NULL. The length should be from EIF_WIFI_SSID_MIN_LEN to EIF_WIFI_SSID_MAX_LEN. |
| pass | Wi-Fi Password string. Cannot be NULL. The length should be from EIF_WIFI_PASS_MIN_LEN to EIF_WIFI_PASS_MAX_LEN. |
ESP_OK: Profile validated and saved successfully.ESP_ERR_INVALID_ARG: The pointer ssid or pass is NULL.ESP_ERR_INVALID_SIZE: The index is out of the possible range.ESP_ERR_NVS_*: System errors propagated from eif_nvs_value_save().Example of use:
| esp_err_t eif_nvs_wifi_profile_load | ( | uint8_t | index, |
| char *const | ssid_out, | ||
| char *const | pass_out | ||
| ) |
Reads SSID and password from NVS into pre-allocated buffers.
Valid index range: (index >= 0) && (index <= WIFI_PROFILES_MAX_COUNT)
EIF_WIFI_SSID_DEFAULT and EIF_WIFI_PASS_DEFAULT), completely bypassing flash memory access. Any other profile index explicitly queries the underlying NVS partition.| index | Profile index slot. Must be from 0 to EIF_WIFI_PROFILES_MAX_COUNT. |
| ssid_out | Buffer for SSID. Cannot be NULL. The length should be EIF_WIFI_SSID_MAX_LEN. |
| pass_out | Buffer for password. Cannot be NULL. The length should be EIF_WIFI_PASS_MAX_LEN. |
ESP_OK: Profile loaded successfully.ESP_ERR_INVALID_ARG: The pointer ssid_out or pass_out is NULL.ESP_ERR_INVALID_SIZE: The index is out of the possible range.ESP_ERR_NVS_NOT_FOUND: No data at specified index.ESP_ERR_NVS_*: System errors propagated from eif_nvs_value_load().Example of use:
| esp_err_t eif_nvs_basic_auth_line_save | ( | const unsigned char *const | pass | ) |
Takes the raw password, combines it with the default username (admin), and encodes the admin:password combination into Base64. The final string is prefixed with Basic (e.g., Basic YWRtaW46cGFzcw==) and saved to NVS.
If an empty password (length 0) is provided, the function automatically generates and saves the default empty-password line (Basic YWRtaW46).
| pass | Raw password string. Cannot be NULL. The length should be from EIF_BASIC_AUTH_PASS_MIN_LEN to EIF_BASIC_AUTH_PASS_MAX_LEN. |
ESP_OK: Password successfully encoded, formatted, and saved to NVS.ESP_ERR_INVALID_ARG: The pass pointer is NULL.ESP_ERR_INVALID_SIZE: The length of the pass is outside the acceptable range.ESP_ERR_NO_MEM: Memory could not be allocated due to the lack of an empty block of the required size.ESP_ERR_NVS_*: System errors propagated from eif_nvs_value_save().| esp_err_t eif_nvs_basic_auth_line_load | ( | char *const | buf_out | ) |
Reads the stored authorization string from NVS into a pre-allocated buffer. The output string contains the configuration prefix, username, and password.
Example of output written to buf_out:
| buf_out | Buffer for Basic Auth string (e.g., Basic YWRtaW46cGFzcw==). Cannot be NULL. The length should be EIF_BASIC_AUTH_LINE_MAX_LEN. |
ESP_OK: Credential string located and loaded successfully.ESP_ERR_INVALID_ARG: The buf_out pointer is NULL.ESP_ERR_NVS_*: System errors propagated from eif_nvs_value_load().