Config

The Config library provides a simple interface to read and write configuration data to the flash memory of the ESP32. It is backed under the hood by the NVS (Non-Volatile Storage) library, which is a key-value pair storage system that is used to store persistent data in the flash memory of the ESP32.

Important Note

avoid using this library in a interrupt or other unknown context, as it may cause a crash. this has been seen when trying to read the configuration data in the mqtt callback functions.

Configuration

CONFIG_NAMESPACE

The namespace to use for the configuration data. This is a string that is used to separate the configuration data from other data stored in the NVS.

#define CONFIG_NAMESPACE "enr_config"

Public functions

config_init

Initializes the configuration library. This function must be called before any other functions in the library are called.

void config_init(void)
{
    nvs_flash_init();
}

config_read

Reads a value from the configuration data, treats all data as raw binary data, hence the void pointer. if we ever need to migrate to and from different data types between versions, we may need to complicate this further.

void config_read(const char *key, void *value, size_t size)
{
    nvs_handle_t handle;
    nvs_open(CONFIG_NAMESPACE, NVS_READONLY, &handle);
    nvs_get_blob(handle, key, value, &size);
    nvs_close(handle);
}

config_write

Writes a value to the configuration data, treats all data as raw binary data, hence the void pointer. if we ever need to migrate to and from different data types between versions, we may need to complicate this further.

void config_write(const char *key, const void *value, size_t size)
{
    nvs_handle_t handle;
    nvs_open(CONFIG_NAMESPACE, NVS_READWRITE, &handle);
    nvs_set_blob(handle, key, value, size);
    nvs_commit(handle);
    nvs_close(handle);
}