Skip to content

Commit

Permalink
subsys/settings: add a function to save subtree
Browse files Browse the repository at this point in the history
When running multiple subsystems or applications on a MCU, the usual
strategy is to use different settings subtrees.

The API provides a way to load or commit a subtree, to avoid adding
dependencies between subsystems or applications, but lacks the way to
save a subtree only. Fix that by adding a settings_save_subtree()
function.

Signed-off-by: Aurelien Jarno <[email protected]>
  • Loading branch information
aurel32 authored and fabiobaltieri committed May 1, 2024
1 parent 37c23f6 commit e94fe44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/zephyr/settings/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ int settings_load_subtree_direct(
*/
int settings_save(void);

/**
* Save limited set of currently running serialized items. All serialized items
* that belong to subtree and which are different from currently persisted
* values will be saved.
*
* @param[in] subtree name of the subtree to be loaded.
* @return 0 on success, non-zero on failure.
*/
int settings_save_subtree(const char *subtree);

/**
* Write a single serialized value to persisted storage (if it has
* changed value).
Expand Down
11 changes: 11 additions & 0 deletions subsys/settings/src/settings_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ int settings_delete(const char *name)
}

int settings_save(void)
{
return settings_save_subtree(NULL);
}

int settings_save_subtree(const char *subtree)
{
struct settings_store *cs;
int rc;
Expand All @@ -132,6 +137,9 @@ int settings_save(void)
rc = 0;

STRUCT_SECTION_FOREACH(settings_handler_static, ch) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
}
if (ch->h_export) {
rc2 = ch->h_export(settings_save_one);
if (!rc) {
Expand All @@ -143,6 +151,9 @@ int settings_save(void)
#if defined(CONFIG_SETTINGS_DYNAMIC_HANDLERS)
struct settings_handler *ch;
SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
}
if (ch->h_export) {
rc2 = ch->h_export(settings_save_one);
if (!rc) {
Expand Down

0 comments on commit e94fe44

Please sign in to comment.