Skip to content

Commit

Permalink
subsys/settings: Reworked settings module
Browse files Browse the repository at this point in the history
Preparation to fix zephyrproject-rtos#12160 - settings NVS backend

To allow easier integration of backends the settings module has been
reworked. Major changes are:

1. Change to the api: the set() routines are now of the form:
	''set(int argc, char **argv, size_t len, read_cb read, void *cb_arg)''
	the length that was found in the backend in len, and the data
	can be read using: ''read(data, len, cb_arg);
2. The reading of data in the set routines is directly reading in the
backend.
3. The records no longer need to be in the line format
4. The runtime interface has been removed (temporarily). A runtime
backend will be added.
5. Only the fcb backend is supported at the moment
6. A sample is added under samples/subsys/nsettings

More rework, changes:
1. Runtime "backend" added
2. Reworked configuration of the module. It now allows the selection of
a default backend (only FCB at the moment). This sets up the settings
src and dst to use the default backend. Other sources could be added by
the application. These sources could also be of different type. The
module can also be configured without a backend, in this case the
application needs to set up the sources and destinations itself.

Signed-off-by: Laczen JMS <laczenjms@gmail.com>
  • Loading branch information
Laczen committed Jan 14, 2019
1 parent 2e632d8 commit 5aafbd7
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/nsettings/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ struct settings_handler {
char *name;
/**< Name of subtree. */

<<<<<<< HEAD
int (*h_get)(int argc, char **argv, char *val, int val_len_max);
=======
int (*h_get)(int argc, char **argv, void *val, int val_len_max);
>>>>>>> subsys/settings: Reworked settings module
/**< Get values handler of settings items identified by keyword names.
*
* Parameters:
Expand Down Expand Up @@ -178,6 +182,15 @@ struct settings_store {
const struct settings_store_itf *cs_itf;
};

<<<<<<< HEAD
=======
#ifdef CONFIG_NSETTINGS_RUNTIME
/* If runtime interface is enabled */
int settings_runtime_set(const char *name, void *data, size_t len);
int settings_runtime_get(const char *name, void *data, size_t len);
#endif

>>>>>>> subsys/settings: Reworked settings module
#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 8 additions & 0 deletions samples/subsys/nsettings/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_FCB=y
CONFIG_NSETTINGS=y
<<<<<<< HEAD
CONFIG_NSETTINGS_FCB=y

CONFIG_LOG=y
CONFIG_REBOOT=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
=======
CONFIG_NSETTINGS_DEFAULT_FCB=y

CONFIG_LOG=y
CONFIG_REBOOT=y
#CONFIG_MPU_ALLOW_FLASH_WRITE=y
>>>>>>> subsys/settings: Reworked settings module
30 changes: 30 additions & 0 deletions samples/subsys/nsettings/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ u8_t reset_counter;
static int ps_set(int argc, char **argv, size_t len, read_cb read, void *cb_arg)
{
int rc;
<<<<<<< HEAD
=======
u8_t test;
>>>>>>> subsys/settings: Reworked settings module

if (argc == 1) {
if (!strcmp(argv[0], "ra0")) {
rc = read(&reset_counter, sizeof(reset_counter),
cb_arg);
<<<<<<< HEAD
printk("ra0 found\n");
=======
printk("Reset Counter ra0: %d\n", reset_counter);
>>>>>>> subsys/settings: Reworked settings module
}
if (!strcmp(argv[0], "ra1")) {
printk("ra1 found\n");
Expand All @@ -35,7 +43,12 @@ static int ps_set(int argc, char **argv, size_t len, read_cb read, void *cb_arg)
printk("ra2 found\n");
}
if (!strcmp(argv[0], "rb")) {
<<<<<<< HEAD
printk("rb found\n");
=======
rc = read(&test, sizeof(test), cb_arg);
printk("Reset Counter rb: %d\n", test);
>>>>>>> subsys/settings: Reworked settings module
}
return (len < 0) ? len : 0;
}
Expand Down Expand Up @@ -68,14 +81,27 @@ int ps_settings_init(void)

void main(void)
{
<<<<<<< HEAD
=======
u8_t test = 8;
>>>>>>> subsys/settings: Reworked settings module
reset_counter = 0U;
ps_settings_init();
settings_load();

while (reset_counter < 6) {
<<<<<<< HEAD
k_sleep(SLEEP_TIME);
reset_counter++;
printk("Reset counter %u\n", reset_counter);
=======

reset_counter++;
printk("Reset counter %u\n", reset_counter);
if (reset_counter == 1) {
settings_runtime_set("ps/rb", &test, sizeof(test));
}
>>>>>>> subsys/settings: Reworked settings module
settings_save_one("ps/ra0", &reset_counter,
sizeof(reset_counter));
settings_save_one("ps/ra1", &reset_counter,
Expand All @@ -90,6 +116,10 @@ void main(void)
if (reset_counter == 4) {
settings_delete("ps/r");
}
<<<<<<< HEAD
=======
k_sleep(SLEEP_TIME);
>>>>>>> subsys/settings: Reworked settings module
sys_reboot(0);
}
printk("Finished");
Expand Down
7 changes: 7 additions & 0 deletions subsys/nsettings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ zephyr_sources(
settings_store.c
settings.c
settings_init.c
<<<<<<< HEAD
)

zephyr_sources_ifdef(CONFIG_NSETTINGS_FCB settings_fcb.c)
=======
settings_fcb.c
)

zephyr_sources_ifdef(CONFIG_NSETTINGS_RUNTIME settings_runtime.c)
>>>>>>> subsys/settings: Reworked settings module
83 changes: 83 additions & 0 deletions subsys/nsettings/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ menuconfig NSETTINGS
bool "Enable settings subsystem with non-volatile storage"
# Only NFFS is currently supported as FS.
# The reason in that FatFs doesn't implement the fs_rename() API
<<<<<<< HEAD
depends on (FILE_SYSTEM && FILE_SYSTEM_NFFS) || (FCB && FLASH_PAGE_LAYOUT)
=======
>>>>>>> subsys/settings: Reworked settings module
help
The settings subsystem allows its users to serialize and
deserialize state in memory into and from non-volatile memory.
It supports several back-ends to store and load serialized data from
and it can do so atomically for all involved modules.

<<<<<<< HEAD
# Hidden option to enable encoding length into settings entry
config NSETTINGS_ENCODE_LEN
depends on NSETTINGS
Expand Down Expand Up @@ -52,10 +56,55 @@ config NSETTINGS_FCB_NUM_AREAS
int "Number of flash areas used by the settings subsystem"
default 8
depends on NSETTINGS && NSETTINGS_FCB
=======
config NSETTINGS_RUNTIME
bool "Runtime support"
default y
depends on NSETTINGS
help
Enables runtime support.

choice
prompt "Default backend type"
default NSETTINGS_DEFAULT_FCB
depends on NSETTINGS
help
Default storage back-end to be used by the settings subsystem.

config NSETTINGS_DEFAULT_FCB
bool "FCB"
depends on FCB
help
Use FCB as the default storage back-end.

config NSETTINGS_DEFAULT_FS
bool "File System"
depends on FILE_SYSTEM
help
Use a file system as the default storage back-end.

config NSETTINGS_DEFAULT_NVS
bool "NVS"
depends on NVS
help
Use NVS as the default storage back-end.

config NSETTINGS_DEFAULT_NONE
bool "None"
help
Don't use a default storage back-end.
endchoice

config NSETTINGS_DEFAULT_FCB_NUM_AREAS
int "Number of flash areas used by the settings subsystem"
default 8
depends on NSETTINGS && NSETTINGS_DEFAULT_FCB
>>>>>>> subsys/settings: Reworked settings module
help
Number of areas to allocate in the settings FCB. A smaller number is
used if the flash hardware cannot support this value.

<<<<<<< HEAD
config NSETTINGS_FCB_MAGIC
hex "FCB magic for the settings subsystem"
default 0xc0ffeeee
Expand All @@ -67,10 +116,24 @@ config NSETTINGS_FCB_FLASH_AREA
int "Flash area id used for settings"
default 4
depends on NSETTINGS && NSETTINGS_FCB
=======
config NSETTINGS_DEFAULT_FCB_MAGIC
hex "FCB magic for the settings subsystem"
default 0xc0ffeeee
depends on NSETTINGS && NSETTINGS_DEFAULT_FCB
help
Magic 32-bit word for to identify valid settings area

config NSETTINGS_DEFAULT_FCB_FLASH_AREA
int "Flash area id used for settings"
default 4
depends on NSETTINGS && NSETTINGS_DEFAULT_FCB
>>>>>>> subsys/settings: Reworked settings module
help
Id of the Flash area where FCB instance used for settings is
expected to operate.

<<<<<<< HEAD
config NSETTINGS_FS_DIR
string "Serialization directory"
default "/settings"
Expand All @@ -89,5 +152,25 @@ config NSETTINGS_FS_MAX_LINES
int "Compression threshold"
default 32
depends on NSETTINGS && NSETTINGS_FS
=======
config NSETTINGS_DEFAULT_FS_DIR
string "Serialization directory"
default "/settings"
depends on NSETTINGS && NSETTINGS_DEFAULT_FS
help
Directory where the settings data is stored

config NSETTINGS_DEFAULT_FS_FILE
string "Default settings file"
default "/settings/run"
depends on NSETTINGS && NSETTINGS_DEFAULT_FS
help
Full path to the default settings file.

config NSETTINGS_DEFAULT_FS_MAX_LINES
int "Compression threshold"
default 32
depends on NSETTINGS && NSETTINGS_DEFAULT_FS
>>>>>>> subsys/settings: Reworked settings module
help
Limit how many items stored in a file before compressing
32 changes: 32 additions & 0 deletions subsys/nsettings/settings_fcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#include "settings_fcb.h"
#include "settings_priv.h"

<<<<<<< HEAD
#define SETTINGS_FCB_VERS 1

=======
>>>>>>> subsys/settings: Reworked settings module
struct settings_fcb_read_fn_arg {
struct fcb_entry_ctx *entry_ctx;
off_t off;
Expand All @@ -29,6 +32,7 @@ static struct settings_store_itf settings_fcb_itf = {
.csi_save = settings_fcb_save,
};

<<<<<<< HEAD
int settings_fcb_src(struct settings_fcb *cf)
{
int rc;
Expand Down Expand Up @@ -63,14 +67,27 @@ int settings_fcb_src(struct settings_fcb *cf)

cf->cf_store.cs_itf = &settings_fcb_itf;
settings_src_register(&cf->cf_store);
=======
int settings_fcb_dst(struct settings_fcb *cf)
{
cf->cf_store.cs_itf = &settings_fcb_itf;
settings_dst_register(&cf->cf_store);
>>>>>>> subsys/settings: Reworked settings module

return 0;
}

<<<<<<< HEAD
int settings_fcb_dst(struct settings_fcb *cf)
{
cf->cf_store.cs_itf = &settings_fcb_itf;
settings_dst_register(&cf->cf_store);
=======
int settings_fcb_src(struct settings_fcb *cf)
{
cf->cf_store.cs_itf = &settings_fcb_itf;
settings_src_register(&cf->cf_store);
>>>>>>> subsys/settings: Reworked settings module

return 0;
}
Expand Down Expand Up @@ -103,6 +120,10 @@ static ssize_t settings_fcb_read_fn(void *data, size_t len, void *read_fn_arg)
if (rc < 0) {
return rc;
}
<<<<<<< HEAD
=======

>>>>>>> subsys/settings: Reworked settings module
return len_read;
}

Expand Down Expand Up @@ -202,6 +223,10 @@ static void settings_fcb_compress(struct settings_fcb *cf)

if (rc) {
continue;
<<<<<<< HEAD
=======
}
>>>>>>> subsys/settings: Reworked settings module

len1_read = strchr(name1, '=') - name1;

Expand Down Expand Up @@ -302,7 +327,11 @@ static int settings_fcb_save_record(struct settings_fcb *cf,
len += wbs - rem;
}

<<<<<<< HEAD
for (i = 0; i < CONFIG_NSETTINGS_FCB_NUM_AREAS; i++) {
=======
for (i = 0; i < cf->cf_fcb.f_sector_cnt; i++) {
>>>>>>> subsys/settings: Reworked settings module
rc = fcb_append(&cf->cf_fcb, len, &loc.loc);
if (rc != FCB_ERR_NOSPACE) {
break;
Expand Down Expand Up @@ -551,7 +580,10 @@ static int settings_fcb_save(struct settings_store *cs, const char *name,
}
return rc;
}
<<<<<<< HEAD

void settings_mount_fcb_backend(struct settings_fcb *cf)
{
}
=======
>>>>>>> subsys/settings: Reworked settings module
Loading

0 comments on commit 5aafbd7

Please sign in to comment.