Modbus Vault v1.0
Loading...
Searching...
No Matches
nvs_manager.c File Reference

Implementation of the NVS manager. More...

#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "nvs_manager_internal.h"
#include "utils.h"
#include <string.h>
Include dependency graph for nvs_manager.c:

Functions

static const key_map_tfind_map_helper (nvs_keys_t key)
 Find entry in config map.
 
static esp_err_t save_config_internal (void)
 Save configuration to flash.
 
static esp_err_t load_config_internal (void)
 Load configuration from flash.
 
static void check_dirty_commit_callback (void *arg_void_ptr)
 Callback for timer.
 
static esp_err_t write_to_cfg_struct_helper (app_config_t *config_ptr, nvs_keys_t key, const void *value_void_ptr, size_t value_size)
 Write to configuration structure using an NVS key.
 
esp_err_t nvs_manager_init (nvs_manager_ops_t *ops_ptr)
 Initialize NVS manager.
 
esp_err_t nvs_manager_set_default (nvs_keys_t key, const void *value_void_ptr, size_t value_size)
 Set configuration default.
 
esp_err_t nvs_manager_write_cfg (nvs_keys_t key, const void *value_void_ptr, size_t value_size)
 Write configuration to flash.
 
esp_err_t nvs_manager_read_cfg (nvs_keys_t key, void *value_void_ptr)
 Read configuration from flash.
 
esp_err_t nvs_manager_flush_cfg (void)
 Flush configuration structure to flash.
 
void nvs_manager_deinit (void)
 Deinitialize NVS manager.
 

Variables

static const char * TAG = "NVS_MANAGER"
 
static const key_map_t config_map []
 
static app_config_t default_config = {.version = NVS_MANAGER_CONFIG_VERSION}
 
static SemaphoreHandle_t config_lock = NULL
 

Detailed Description

Implementation of the NVS manager.

  • Abstract driver saving/loading by providing local application config structure and saving/loading it as a blob to NVS
  • Calculates and checks CRC16-Modbus on Saving/Loading
  • Writes happens by writing to local application config structure
  • On write marks application config structure as dirty to signal a potential NVS update
  • Reads happens by reading from local application config structure
  • Uses syncing timer to flush data periodically

Function Documentation

◆ check_dirty_commit_callback()

static void check_dirty_commit_callback ( void *  arg_void_ptr)
static

Callback for timer.

Callback function for periodic timer. It checks for dirty configuration then commit it if so

Parameters
arg_void_ptrVoid pointer to callback argument
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_map_helper()

static const key_map_t * find_map_helper ( nvs_keys_t  key)
static

Find entry in config map.

Parameters
keyKey chosen from nvs_keys_t
Returns
key_map_t* Pointer to map entry
Here is the caller graph for this function:

◆ load_config_internal()

static esp_err_t load_config_internal ( void  )
static

Load configuration from flash.

Load configuration from flash. On success compare version and verify it's crc16 if all checks update shadow structure then return

Returns
esp_err_t Write result
Return values
ESP_OKWrite success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
anyAny error from down layers propagates upward
Note
On-fail it loads shadow configuration to primary configuration to undo any corruption happened during load attempt
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvs_manager_deinit()

void nvs_manager_deinit ( void  )

Deinitialize NVS manager.

Deinitialize NVS manager by commit data, close handle, delete timer and deinit flash

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

◆ nvs_manager_flush_cfg()

esp_err_t nvs_manager_flush_cfg ( void  )

Flush configuration structure to flash.

Returns
esp_err_t Flush result
Return values
ESP_OKFlush success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
anyAny error from down layers propagates upward
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvs_manager_init()

esp_err_t nvs_manager_init ( nvs_manager_ops_t ops_ptr)

Initialize NVS manager.

Initialize NVS manager by calling flash init, if succeeded load config, If failed with 'flash full' or 'new NVS version' erase flash then try to call flash init again. Finally initiate periodic timer to commit configuration if it was flagged dirty and newer than shadow structure

Parameters
ops_ptrPointer to NVS manager operations structure
Returns
esp_err_t Initialize result
Return values
ESP_OKInitialize success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
anyAny error from down layers propagates upward
Note
Assumes default name for NVS partition typically 'nvs'
WARNING: If there are no free-pages or NVS-full NVS partition gets erased
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvs_manager_read_cfg()

esp_err_t nvs_manager_read_cfg ( nvs_keys_t  key,
void *  value_void_ptr 
)

Read configuration from flash.

Read configuration from flash cached in configuration structure

Parameters
keyKey chosen from nvs_keys_t
value_void_ptrPointer to data
Returns
esp_err_t Read result
Return values
ESP_OKRead success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvs_manager_set_default()

esp_err_t nvs_manager_set_default ( nvs_keys_t  key,
const void *  value_void_ptr,
size_t  value_size 
)

Set configuration default.

Set configuration default values in case it was missing from flash

Parameters
keyKey chosen from nvs_keys_t
value_void_ptrPointer to data
value_sizeValue size
Returns
esp_err_t Set result
Return values
ESP_OKSet success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
Note
Defaults has no effect after initialization it should be used before initialization
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvs_manager_write_cfg()

esp_err_t nvs_manager_write_cfg ( nvs_keys_t  key,
const void *  value_void_ptr,
size_t  value_size 
)

Write configuration to flash.

Write configuration to flash by writing to configuration structure and flush it when preset time has passed

Parameters
keyKey chosen from nvs_keys_t
value_void_ptrPointer to data
value_sizeValue size
Returns
esp_err_t Write result
Return values
ESP_OKWrite success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_config_internal()

static esp_err_t save_config_internal ( void  )
static

Save configuration to flash.

Save configuration structure to flash as a blob and update shadow structure if save succeeded

Returns
esp_err_t Save result
Return values
ESP_OKSave success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
anyAny error from down layers propagates upward
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_to_cfg_struct_helper()

static esp_err_t write_to_cfg_struct_helper ( app_config_t config_ptr,
nvs_keys_t  key,
const void *  value_void_ptr,
size_t  value_size 
)
static

Write to configuration structure using an NVS key.

Parameters
config_ptrPointer to configuration structure
keyKey chosen from nvs_keys_t
value_void_ptrVoid pointer to value
value_sizeValue size
Returns
esp_err_t write result
Return values
ESP_OKWrite success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ config_lock

SemaphoreHandle_t config_lock = NULL
static

App configuration lock

◆ config_map

const key_map_t config_map[]
static
Initial value:
= {
{NVS_MANAGER_KEYS_BB_WRITE_OFFSET, offsetof (app_config_t, write_offset), sizeof (uint32_t)},
{NVS_MANAGER_KEYS_BB_REPLAY_OFFSET, offsetof (app_config_t, replay_offset), sizeof (uint32_t)},
{NVS_MANAGER_KEYS_BB_LAST_ID, offsetof (app_config_t, last_id), sizeof (uint32_t)}}
#define NVS_MANAGER_MAX_MQTT_PASSWORD_SIZE
Definition nvs_manager.h:23
@ NVS_MANAGER_KEYS_WIFI_PASS
Definition nvs_manager.h:34
@ NVS_MANAGER_KEYS_MQTT_URI
Definition nvs_manager.h:35
@ NVS_MANAGER_KEYS_BB_REPLAY_OFFSET
Definition nvs_manager.h:39
@ NVS_MANAGER_KEYS_MQTT_USER
Definition nvs_manager.h:36
@ NVS_MANAGER_KEYS_MQTT_PASS
Definition nvs_manager.h:37
@ NVS_MANAGER_KEYS_BB_WRITE_OFFSET
Definition nvs_manager.h:38
@ NVS_MANAGER_KEYS_BB_LAST_ID
Definition nvs_manager.h:40
@ NVS_MANAGER_KEYS_WIFI_SSID
Definition nvs_manager.h:33
#define NVS_MANAGER_MAX_WIFI_PASSWORD_SIZE
Definition nvs_manager.h:27
#define NVS_MANAGER_MAX_MQTT_USER_SIZE
Definition nvs_manager.h:21
#define NVS_MANAGER_MAX_MQTT_URI_SIZE
Definition nvs_manager.h:19
#define NVS_MANAGER_MAX_WIFI_SSID_SIZE
Definition nvs_manager.h:25
NVS manager application config structure.
Definition nvs_manager_internal.h:27

Helps in write/read management

◆ default_config

app_config_t default_config = {.version = NVS_MANAGER_CONFIG_VERSION}
static

Defaulting value when no data in NVS

◆ TAG

const char* TAG = "NVS_MANAGER"
static

NVS Manager TAG name