Skip to content

Commit ccf8c54

Browse files
nvlsianpucarlescufi
authored andcommitted
settings: API for get storage instance used
Added API function +int settings_storage_get(void **storage) which allows to get storage instance used by the settings backed to store its records. Signed-off-by: Andrzej Puzdrowski <[email protected]>
1 parent 63aff01 commit ccf8c54

File tree

14 files changed

+143
-7
lines changed

14 files changed

+143
-7
lines changed

include/zephyr/settings/settings.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,13 @@ struct settings_store_itf {
440440
* Parameters:
441441
* - cs - Corresponding backend handler node
442442
*/
443+
444+
/**< Get pointer to the storage instance used by the backend.
445+
*
446+
* Parameters:
447+
* - cs - Corresponding backend handler node
448+
*/
449+
void *(*csi_storage_get)(struct settings_store *cs);
443450
};
444451

445452
/**
@@ -585,6 +592,18 @@ int settings_runtime_commit(const char *name);
585592

586593
#endif /* CONFIG_SETTINGS_RUNTIME */
587594

595+
/**
596+
* Get the storage instance used by zephyr.
597+
*
598+
* The type of storage object instance depends on the settings backend used.
599+
* It might pointer to: `struct nvs_fs`, `struct fcb` or string witch file name
600+
* depends on settings backend type used.
601+
*
602+
* @retval Pointer to which reference to the storage object can be stored.
603+
*
604+
* @retval 0 on success, negative error code on failure.
605+
*/
606+
int settings_storage_get(void **storage);
588607

589608
#ifdef __cplusplus
590609
}

subsys/settings/src/settings_fcb.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ LOG_MODULE_DECLARE(settings, CONFIG_SETTINGS_LOG_LEVEL);
2727

2828
int settings_backend_init(void);
2929
void settings_mount_fcb_backend(struct settings_fcb *cf);
30+
static void *settings_fcb_storage_get(struct settings_store *cs);
3031

3132
static int settings_fcb_load(struct settings_store *cs,
3233
const struct settings_load_arg *arg);
3334
static int settings_fcb_save(struct settings_store *cs, const char *name,
3435
const char *value, size_t val_len);
36+
static void *settings_fcb_storage_get(struct settings_store *cs);
3537

3638
static const struct settings_store_itf settings_fcb_itf = {
3739
.csi_load = settings_fcb_load,
3840
.csi_save = settings_fcb_save,
41+
.csi_storage_get = settings_fcb_storage_get
3942
};
4043

4144
int settings_fcb_src(struct settings_fcb *cf)
@@ -436,3 +439,10 @@ int settings_backend_init(void)
436439

437440
return rc;
438441
}
442+
443+
static void *settings_fcb_storage_get(struct settings_store *cs)
444+
{
445+
struct settings_fcb *cf = (struct settings_fcb *)cs;
446+
447+
return &cf->cf_fcb;
448+
}

subsys/settings/src/settings_file.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ static int settings_file_load(struct settings_store *cs,
2626
const struct settings_load_arg *arg);
2727
static int settings_file_save(struct settings_store *cs, const char *name,
2828
const char *value, size_t val_len);
29+
static void *settings_fs_storage_get(struct settings_store *cs);
2930

3031
static const struct settings_store_itf settings_file_itf = {
3132
.csi_load = settings_file_load,
3233
.csi_save = settings_file_save,
34+
.csi_storage_get = settings_fs_storage_get
3335
};
3436

3537
/*
@@ -535,3 +537,10 @@ int settings_backend_init(void)
535537
}
536538
return rc;
537539
}
540+
541+
static void *settings_fs_storage_get(struct settings_store *cs)
542+
{
543+
struct settings_file *cf = (struct settings_file *)cs;
544+
545+
return (void *)cf->cf_name;
546+
}

subsys/settings/src/settings_nvs.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ static int settings_nvs_load(struct settings_store *cs,
3131
const struct settings_load_arg *arg);
3232
static int settings_nvs_save(struct settings_store *cs, const char *name,
3333
const char *value, size_t val_len);
34+
static void *settings_nvs_storage_get(struct settings_store *cs);
3435

3536
static struct settings_store_itf settings_nvs_itf = {
3637
.csi_load = settings_nvs_load,
3738
.csi_save = settings_nvs_save,
39+
.csi_storage_get = settings_nvs_storage_get
3840
};
3941

4042
static ssize_t settings_nvs_read_fn(void *back_end, void *data, size_t len)
@@ -332,3 +334,10 @@ int settings_backend_init(void)
332334

333335
return rc;
334336
}
337+
338+
static void *settings_nvs_storage_get(struct settings_store *cs)
339+
{
340+
struct settings_nvs *cf = (struct settings_nvs *)cs;
341+
342+
return &cf->cf_nvs;
343+
}

subsys/settings/src/settings_store.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ int settings_save(void)
158158
return rc;
159159
}
160160

161+
int settings_storage_get(void **storage)
162+
{
163+
struct settings_store *cs = settings_save_dst;
164+
165+
if (!cs) {
166+
return -ENOENT;
167+
}
168+
169+
if (cs->cs_itf->csi_storage_get) {
170+
*storage = cs->cs_itf->csi_storage_get(cs);
171+
}
172+
173+
return 0;
174+
}
175+
161176
void settings_store_init(void)
162177
{
163178
sys_slist_init(&settings_load_srcs);

tests/subsys/settings/functional/fcb/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
55
project(functional_fcb)
66

77
# The code is in the library common to several tests.
8-
target_sources(app PRIVATE placeholder.c)
8+
target_sources(app PRIVATE settings_test_fcb.c)
99

1010
add_subdirectory(../src func_test_bindir)
1111

tests/subsys/settings/functional/fcb/placeholder.c

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
/* Copyright (c) 2022 Nordic semiconductor ASA */
3+
4+
#include <zephyr/zephyr.h>
5+
#include <ztest.h>
6+
#include <errno.h>
7+
#include <zephyr/settings/settings.h>
8+
#include <zephyr/fs/fcb.h>
9+
10+
void test_setting_storage_get(void)
11+
{
12+
int rc;
13+
void *storage;
14+
struct fcb_entry loc;
15+
16+
rc = settings_storage_get(&storage);
17+
zassert_equal(0, rc, "Can't fetch storage reference (err=%d)", rc);
18+
19+
zassert_not_null(storage, "Null reference.");
20+
21+
loc.fe_sector = NULL;
22+
rc = fcb_getnext((struct fcb *)storage, &loc);
23+
24+
zassert_equal(rc, 0, "Can't read fcb (err=%d)", rc);
25+
}

tests/subsys/settings/functional/file/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ project(functional_file)
66

77
FILE(GLOB app_sources ../src/*.c)
88
target_sources(app PRIVATE ${app_sources})
9+
target_sources(app PRIVATE settings_test_fs.c)
910
zephyr_include_directories(
1011
${ZEPHYR_BASE}/subsys/settings/include
1112
${ZEPHYR_BASE}/subsys/settings/src
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
/* Copyright (c) 2022 Nordic semiconductor ASA */
3+
4+
#include <zephyr/zephyr.h>
5+
#include <ztest.h>
6+
#include <errno.h>
7+
#include <zephyr/settings/settings.h>
8+
#include <zephyr/fs/fs.h>
9+
10+
void test_setting_storage_get(void)
11+
{
12+
int rc;
13+
void *storage;
14+
15+
struct fs_dirent entry;
16+
17+
rc = settings_storage_get(&storage);
18+
zassert_equal(0, rc, "Can't fetch storage reference (err=%d)", rc);
19+
20+
zassert_not_null(storage, "Null reference.");
21+
22+
rc = fs_stat((const char *)storage, &entry);
23+
24+
zassert_true(rc >= 0, "Can't find the file (err=%d)", rc);
25+
}

0 commit comments

Comments
 (0)