ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
config.c File Reference

Thread-safe configuration component utilizing NVS for ESP32. Implements the private storage structures, mutex protection mechanisms, and input validation logic defined in config.h. More...

#include "config.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "led.h"
#include "logger.h"
#include "nvs.h"
#include <stdio.h>
#include <string.h>
Include dependency graph for config.c:

Go to the source code of this file.

Data Structures

struct  app_wifi_creds_t
 WIFI credentials structure for both Station and Access Point modes. More...
struct  __attribute__
 Internal configuration storage layout. Mirrors the private runtime configuration context state in RAM. More...

Macros

#define LOG_TAG   "CONFIG"
 "CONFIG" log tag for this file
#define NVS_NAMESPACE   "app_config"
 NVS storage namespace for configuration data.
#define NVS_BLOB_KEY   "config_blob"
 Key under which the entire config blob is stored in NVS.
#define APP_CONFIG_MAGIC   0x43444D58
#define APP_CONFIG_VERSION   4
#define LOCK()
 Locks the configuration mutex for exclusive access.
#define UNLOCK()
 Unlocks the configuration mutex after access.

Functions

static void load_factory_defaults (void)
 Restores all configuration fields to their factory-defined values in RAM.
esp_err_t config_init (void)
 Initializes the configuration component.
esp_err_t config_reset_defaults (void)
 Resets all configuration settings back to factory defaults.
esp_err_t config_save (void)
 Flushes all staged RAM changes permanently to the non-volatile storage.
config_connection_t config_get_connection (void)
 Gets the current network connection type.
bool config_set_connection (config_connection_t conn)
 Sets the network connection type in RAM.
config_ip_method_t config_get_ip_method (void)
 Gets the current IP allocation method.
bool config_set_ip_method (config_ip_method_t method)
 Sets the IP assignment method in RAM.
uint8_t config_get_led_brightness (void)
 Gets the current status LED brightness level.
bool config_set_led_brightness (uint8_t brightness)
 Sets the status LED brightness level in RAM.
uint16_t config_get_dmx_universe (uint8_t port_index)
 Gets the configured DMX universe for a specific port.
bool config_set_dmx_universe (uint8_t port_index, uint16_t universe)
 Sets the DMX universe for a specific port in RAM.
config_direction_t config_get_dmx_direction (uint8_t port_index)
 Gets the data direction for a specific port.
bool config_set_dmx_direction (uint8_t port_index, config_direction_t direction)
 Sets the data direction for a specific port in RAM.
void config_get_wifi_sta_config (wifi_config_t *dest)
 Gets the Wi-Fi Station mode configuration.
bool config_set_wifi_sta_config (const wifi_config_t *src)
 Sets the Wi-Fi Station mode configuration in RAM.
void config_get_wifi_ap_config (wifi_config_t *dest)
 Gets the Wi-Fi Access Point mode configuration.
bool config_set_wifi_ap_config (const wifi_config_t *src)
 Sets the Wi-Fi Access Point mode configuration in RAM.
config_button_action_t config_get_button_action (app_button_event_t event)
 Retrieves the action assigned to a specific button event.
bool config_set_button_action (app_button_event_t event, config_button_action_t action)
 Assigns a new action to a specific button event in RAM.

Variables

static config_storage_t s_config
 Private runtime memory footprint of the active configuration.
static SemaphoreHandle_t s_config_mutex = NULL
 Mutex guard ensuring thread-safe operations on the shared configuration context.
static bool s_is_initialized = false
 Tracks if the configuration component has been successfully initialized.
static bool s_is_dirty = false
 Set to true if any RAM values diverge from the persisted flash storage state.

Detailed Description

Thread-safe configuration component utilizing NVS for ESP32. Implements the private storage structures, mutex protection mechanisms, and input validation logic defined in config.h.

Definition in file config.c.

Macro Definition Documentation

◆ APP_CONFIG_MAGIC

#define APP_CONFIG_MAGIC   0x43444D58

ASCII for 'CDMX'

Definition at line 28 of file config.c.

Referenced by config_init(), and load_factory_defaults().

◆ APP_CONFIG_VERSION

#define APP_CONFIG_VERSION   4

Incremented when struct layout changes

Definition at line 29 of file config.c.

Referenced by config_init(), and load_factory_defaults().

◆ LOCK

◆ LOG_TAG

#define LOG_TAG   "CONFIG"

"CONFIG" log tag for this file

Definition at line 8 of file config.c.

◆ NVS_BLOB_KEY

#define NVS_BLOB_KEY   "config_blob"

Key under which the entire config blob is stored in NVS.

Definition at line 24 of file config.c.

Referenced by config_init(), and config_save().

◆ NVS_NAMESPACE

#define NVS_NAMESPACE   "app_config"

NVS storage namespace for configuration data.

Definition at line 23 of file config.c.

Referenced by config_init(), and config_save().

◆ UNLOCK

Function Documentation

◆ config_get_button_action()

config_button_action_t config_get_button_action ( app_button_event_t event)

Retrieves the action assigned to a specific button event.

Parameters
[in]eventThe button event to query.
Returns
The config_button_action_t assigned to the event, or APP_BUTTON_ACTION_NONE if uninitialized or invalid.

Definition at line 447 of file config.c.

References APP_BUTTON_ACTION_NONE, APP_BUTTON_EVENT_DOUBLE_CLICK, APP_BUTTON_EVENT_MULTIPLE_CLICK, APP_BUTTON_EVENT_SINGLE_CLICK, LOCK, s_config, s_is_initialized, and UNLOCK.

Referenced by iot_button_double_click_cb(), iot_button_multiple_click_cb(), and iot_button_single_click_cb().

Here is the caller graph for this function:

◆ config_get_connection()

config_connection_t config_get_connection ( void )

Gets the current network connection type.

Returns
Current connection mode as config_connection_t.

Definition at line 243 of file config.c.

References APP_CONFIG_DEFAULT_CONNECTION, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_dmx_direction()

config_direction_t config_get_dmx_direction ( uint8_t port_index)

Gets the data direction for a specific port.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
Returns
Configuration direction (Defaults to OUTPUT if index invalid).

Definition at line 342 of file config.c.

References APP_CONFIG_DEFAULT_DMX_DIR, APP_CONFIG_DMX_PORT_COUNT, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_dmx_universe()

uint16_t config_get_dmx_universe ( uint8_t port_index)

Gets the configured DMX universe for a specific port.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
Returns
Universe number (0-32768), or APP_CONFIG_INVALID_UNIVERSE if port_index is invalid.

Definition at line 318 of file config.c.

References APP_CONFIG_DMX_PORT_COUNT, APP_CONFIG_INVALID_UNIVERSE, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_ip_method()

config_ip_method_t config_get_ip_method ( void )

Gets the current IP allocation method.

Returns
Current IP method as config_ip_method_t.

Definition at line 267 of file config.c.

References APP_CONFIG_DEFAULT_IP_METHOD, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_led_brightness()

uint8_t config_get_led_brightness ( void )

Gets the current status LED brightness level.

Returns
Brightness value ranging from 0 to 255.

Definition at line 290 of file config.c.

References APP_CONFIG_DEFAULT_LED_BRIGHTNESS, LOCK, s_config, s_is_initialized, and UNLOCK.

Referenced by app_main(), and execute_button_action().

Here is the caller graph for this function:

◆ config_get_wifi_ap_config()

void config_get_wifi_ap_config ( wifi_config_t * dest)

Gets the Wi-Fi Access Point mode configuration.

Parameters
[out]destPointer to a wifi_config_t struct to receive the data.

Definition at line 404 of file config.c.

References LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_wifi_sta_config()

void config_get_wifi_sta_config ( wifi_config_t * dest)

Gets the Wi-Fi Station mode configuration.

Parameters
[out]destPointer to a wifi_config_t struct to receive the data.

Definition at line 367 of file config.c.

References LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_init()

esp_err_t config_init ( void )

Initializes the configuration component.

Sets up internal mutexes and loads the persisted configuration blob from NVS.

Note
The NVS flash subsystem must be initialized (via nvs_flash_init()) before calling this function.
Returns
  • ESP_OK on success.
  • ESP_ERR_NO_MEM if mutex creation failed.
  • Other NVS-related error codes if loading fails.

Definition at line 136 of file config.c.

References APP_CONFIG_MAGIC, APP_CONFIG_VERSION, load_factory_defaults(), LOGE, LOGI, LOGW, NVS_BLOB_KEY, NVS_NAMESPACE, s_config, s_config_mutex, s_is_dirty, and s_is_initialized.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_reset_defaults()

esp_err_t config_reset_defaults ( void )

Resets all configuration settings back to factory defaults.

Note
This stages the defaults in RAM. A call to config_save() is required to permanently write them to flash.
Returns
  • ESP_OK on success
  • ESP_FAIL if memory or initialization state is invalid

Definition at line 194 of file config.c.

References load_factory_defaults(), LOCK, LOGI, s_is_initialized, and UNLOCK.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_save()

esp_err_t config_save ( void )

Flushes all staged RAM changes permanently to the non-volatile storage.

Note
To prevent flash wear, this function returns early with ESP_OK if no settings were mutated since the last call.
Returns
  • ESP_OK on success or if no write was required
  • ESP_ERR_NVS_NOT_INITIALIZED if NVS system is down
  • Other flash driver error codes on write failures

Definition at line 204 of file config.c.

References LOCK, LOGE, LOGI, NVS_BLOB_KEY, NVS_NAMESPACE, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

Referenced by app_main(), and execute_button_action().

Here is the caller graph for this function:

◆ config_set_button_action()

bool config_set_button_action ( app_button_event_t event,
config_button_action_t action )

Assigns a new action to a specific button event in RAM.

Parameters
[in]eventThe button event to modify.
[in]actionThe config_button_action_t to assign.
Returns
true if the configuration was updated, false if uninitialized, unchanged, or arguments were invalid.

Definition at line 477 of file config.c.

References APP_BUTTON_ACTION_MAX, APP_BUTTON_EVENT_DOUBLE_CLICK, APP_BUTTON_EVENT_MULTIPLE_CLICK, APP_BUTTON_EVENT_SINGLE_CLICK, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_connection()

bool config_set_connection ( config_connection_t conn)

Sets the network connection type in RAM.

Parameters
[in]connNew connection mode to apply.
Returns
true if the value was modified, false if it was identical or invalid.

Definition at line 252 of file config.c.

References APP_CONFIG_CONN_ETHERNET, LOCK, LOGI, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_dmx_direction()

bool config_set_dmx_direction ( uint8_t port_index,
config_direction_t direction )

Sets the data direction for a specific port in RAM.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
[in]directionNew direction (Input/Output).
Returns
true if updated, false if index invalid or unchanged.

Definition at line 351 of file config.c.

References APP_CONFIG_DIR_INPUT, APP_CONFIG_DMX_PORT_COUNT, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_dmx_universe()

bool config_set_dmx_universe ( uint8_t port_index,
uint16_t universe )

Sets the DMX universe for a specific port in RAM.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
[in]universeDMX Universe number (typically 0 - 32768).
Returns
true if updated, false if index invalid or unchanged.

Definition at line 327 of file config.c.

References APP_CONFIG_DMX_PORT_COUNT, APP_CONFIG_INVALID_UNIVERSE, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_ip_method()

bool config_set_ip_method ( config_ip_method_t method)

Sets the IP assignment method in RAM.

Parameters
[in]methodNew IP allocation method to apply.
Returns
true if the value was modified, false if identical or invalid.

Definition at line 276 of file config.c.

References APP_CONFIG_IP_DHCP, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_led_brightness()

bool config_set_led_brightness ( uint8_t brightness)

Sets the status LED brightness level in RAM.

Parameters
[in]brightnessDesired brightness (0 - 255).
Returns
true if value was updated, false if invalid (out of bounds) or unchanged.

Definition at line 299 of file config.c.

References led_set_brightness(), LOCK, LOGI, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

Referenced by execute_button_action().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_set_wifi_ap_config()

bool config_set_wifi_ap_config ( const wifi_config_t * src)

Sets the Wi-Fi Access Point mode configuration in RAM.

Parameters
[in]srcPointer to a wifi_config_t struct containing the new config.
Returns
true if updated, false if invalid or unchanged.

Definition at line 415 of file config.c.

References LOCK, LOGE, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_wifi_sta_config()

bool config_set_wifi_sta_config ( const wifi_config_t * src)

Sets the Wi-Fi Station mode configuration in RAM.

Parameters
[in]srcPointer to a wifi_config_t struct containing the new config.
Returns
true if updated, false if invalid or unchanged.

Definition at line 379 of file config.c.

References LOCK, LOGE, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ load_factory_defaults()

void load_factory_defaults ( void )
static

Restores all configuration fields to their factory-defined values in RAM.

Sets the magic number, version, and all functional parameters (Wi-Fi, DMX, LED). Marks the configuration as dirty to trigger a save on next config_save() call.

Definition at line 102 of file config.c.

References APP_CONFIG_DEFAULT_AP_PASSWORD, APP_CONFIG_DEFAULT_AP_SSID_PREFIX, APP_CONFIG_DEFAULT_CONNECTION, APP_CONFIG_DEFAULT_DMX_DIR, APP_CONFIG_DEFAULT_DOUBLE_CLICK_ACT, APP_CONFIG_DEFAULT_IP_METHOD, APP_CONFIG_DEFAULT_LED_BRIGHTNESS, APP_CONFIG_DEFAULT_MULTI_CLICK_ACT, APP_CONFIG_DEFAULT_SINGLE_CLICK_ACT, APP_CONFIG_DEFAULT_STA_PASSWORD, APP_CONFIG_DEFAULT_STA_SSID, APP_CONFIG_DEFAULT_START_UNIVERSE, APP_CONFIG_DMX_PORT_COUNT, APP_CONFIG_MAGIC, APP_CONFIG_VERSION, s_config, and s_is_dirty.

Referenced by config_init(), and config_reset_defaults().

Here is the caller graph for this function:

Variable Documentation

◆ s_config

◆ s_config_mutex

SemaphoreHandle_t s_config_mutex = NULL
static

Mutex guard ensuring thread-safe operations on the shared configuration context.

Definition at line 71 of file config.c.

Referenced by config_init().

◆ s_is_dirty

bool s_is_dirty = false
static

◆ s_is_initialized