Skip to content

Commit e94fe44

Browse files
aurel32fabiobaltieri
authored andcommitted
subsys/settings: add a function to save subtree
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]>
1 parent 37c23f6 commit e94fe44

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

include/zephyr/settings/settings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ int settings_load_subtree_direct(
303303
*/
304304
int settings_save(void);
305305

306+
/**
307+
* Save limited set of currently running serialized items. All serialized items
308+
* that belong to subtree and which are different from currently persisted
309+
* values will be saved.
310+
*
311+
* @param[in] subtree name of the subtree to be loaded.
312+
* @return 0 on success, non-zero on failure.
313+
*/
314+
int settings_save_subtree(const char *subtree);
315+
306316
/**
307317
* Write a single serialized value to persisted storage (if it has
308318
* changed value).

subsys/settings/src/settings_store.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ int settings_delete(const char *name)
116116
}
117117

118118
int settings_save(void)
119+
{
120+
return settings_save_subtree(NULL);
121+
}
122+
123+
int settings_save_subtree(const char *subtree)
119124
{
120125
struct settings_store *cs;
121126
int rc;
@@ -132,6 +137,9 @@ int settings_save(void)
132137
rc = 0;
133138

134139
STRUCT_SECTION_FOREACH(settings_handler_static, ch) {
140+
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
141+
continue;
142+
}
135143
if (ch->h_export) {
136144
rc2 = ch->h_export(settings_save_one);
137145
if (!rc) {
@@ -143,6 +151,9 @@ int settings_save(void)
143151
#if defined(CONFIG_SETTINGS_DYNAMIC_HANDLERS)
144152
struct settings_handler *ch;
145153
SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
154+
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
155+
continue;
156+
}
146157
if (ch->h_export) {
147158
rc2 = ch->h_export(settings_save_one);
148159
if (!rc) {

0 commit comments

Comments
 (0)