Skip to content
Permalink
Browse files

subsys/settings: change processing to const char

The settings module processes the variable name by splitting it up in
a set of variables. This PR removes the splitting up and keeps the
variable name as one string.

It is an alternative to #16609

The possibility is introduced to register handler including a
separator, or to register a handler for each variable.

The ability is introduced to load a subtree from flash or even to load
a single item.

Two ways to operate on variable and settings_handler names are provided:

settings_name_steq(const char *name, const char *key, const char **next)
which checks if name starts with key, returns 1/0 if it does/does not
the remaining part of name is in next.

settings_name_split(const char *name, char *argv, const char **next)
which splits up name in a part before "/"" that is found in argv and
the remaining part that is in next.

A mutex is added to make settings thread-safe

The settings_handlers list is stored in reverse alphabetical order, this
allows registration of e.g. bt and bt/mesh in separate handlers, the bt
handler itself should not contain any handling of bt/mesh.

A settings_deregister() method is added. Settings_handlers can now be
added/removed when required. This saves RAM when settings_handlers are
not needed.

Tests have been updated to reflect changes in the settings api.

Updates after meeting:
1. Removed settings_deregister

2. Changed settings_name_split() in settings_name_next:

int settings_name_next(const char *name, const char **next): returns
the number of characters before the first separator. This can then be
used to read the correct number of characters from name using strncpy
for processing.

3. New functional test added

Update in settings.h: settings_name_next() changed position -> index

Added some comments in settings.h (settings_name_steq())

Updated tests to reflect change to settings_name_next() and pointer
value comparison. The functional test can now also run on qemu_x86.

Corrected some documentation in header.

Changed registration of handlers to be non ordered.

Changed handler lookup to handle non ordered list of handlers, this
improves handler matching in case different length names are compared
and also makes it easier to add rom based handlers as they will not be
ordered.

Signed-off-by: Laczen JMS <laczenjms@gmail.com>
  • Loading branch information...
Laczen authored and carlescufi committed Jun 8, 2019
1 parent 03259db commit 36edf92ca8bf28201363e7e8af4292ceda0cfe89
@@ -26,7 +26,8 @@ extern "C" {
#define SETTINGS_MAX_DIR_DEPTH 8 /* max depth of settings tree */
#define SETTINGS_MAX_NAME_LEN (8 * SETTINGS_MAX_DIR_DEPTH)
#define SETTINGS_MAX_VAL_LEN 256
#define SETTINGS_NAME_SEPARATOR "/"
#define SETTINGS_NAME_SEPARATOR '/'
#define SETTINGS_NAME_END '='

/* pleace for settings additions:
* up to 7 separators, '=', '\0'
@@ -47,26 +48,27 @@ struct settings_handler {
char *name;
/**< Name of subtree. */

int (*h_get)(int argc, char **argv, char *val, int val_len_max);
int (*h_get)(const char *key, char *val, int val_len_max);
/**< Get values handler of settings items identified by keyword names.
*
* Parameters:
* - argc - count of item in argv.
* - argv - array of pointers to keyword names.
* - val - buffer for a value.
* - val_len_max - size of that buffer.
* - key[in] the name with skipped part that was used as name in
* handler registration
* - val[out] buffer to receive value.
* - val_len_max[in] size of that buffer.
*/

int (*h_set)(int argc, char **argv, size_t len,
settings_read_cb read_cb, void *cb_arg);
int (*h_set)(const char *key, size_t len, settings_read_cb read_cb,
void *cb_arg);
/**< Set value handler of settings items identified by keyword names.
*
* Parameters:
* - argc - count of item in argv.
* - argv - array of pointers to keyword names.
* - len - the size of the data found in the backend.
* - read_cb - function provided to read the data from the backend.
* - cb_arg - arguments for the read function provided by the backend.
* - key[in] the name with skipped part that was used as name in
* handler registration
* - len[in] the size of the data found in the backend.
* - read_cb[in] function provided to read the data from the backend.
* - cb_arg[in] arguments for the read function provided by the
* backend.
*/

int (*h_commit)(void);
@@ -121,8 +123,18 @@ int settings_register(struct settings_handler *cf);
int settings_load(void);

/**
* Save currently running serialized items. All serialized items which are different
* from currently persisted values will be saved.
* Load limited set of serialized items from registered persistence sources.
* Handlers for serialized item subtrees registered earlier will be called for
* encountered values that belong to the subtree.
*
* @param[in] subtree name of the subtree to be loaded.
* @return 0 on success, non-zero on failure.
*/
int settings_load_subtree(const char *subtree);

/**
* Save currently running serialized items. All serialized items which are
* different from currently persisted values will be saved.
*
* @return 0 on success, non-zero on failure.
*/
@@ -161,6 +173,16 @@ int settings_delete(const char *name);
*/
int settings_commit(void);

/**
* Call commit for settings handler that belong to subtree.
* This should apply all settings which has been set, but not applied yet.
*
* @param[in] subtree name of the subtree to be committed.
*
* @return 0 on success, non-zero on failure.
*/
int settings_commit_subtree(const char *subtree);

/**
* @} settings
*/
@@ -191,8 +213,9 @@ struct settings_store {
* Destinations are registered using a call to @ref settings_dst_register.
*/
struct settings_store_itf {
int (*csi_load)(struct settings_store *cs);
/**< Loads all values from storage.
int (*csi_load)(struct settings_store *cs, const char *subtree);
/**< Loads values from storage limited to subtree defined by subtree. If
* subtree = NULL loads all values.
*
* Parameters:
* - cs - Corresponding backend handler node
@@ -248,16 +271,53 @@ void settings_dst_register(struct settings_store *cs);
/**
* Parses a key to an array of elements and locate corresponding module handler.
*
* @param name Key in string format
* @param name_argc Parsed number of elements.
* @param name_argv Parsed array of elements.
* @param[in] name in string format
* @param[out] next remaining of name after matched handler
*
* @return settings_handler node on success, NULL on failure.
*/
struct settings_handler *settings_parse_and_lookup(char *name, int *name_argc,
char *name_argv[]);
struct settings_handler *settings_parse_and_lookup(const char *name,
const char **next);


/*
* API for const name processing
*/

/**
* Compares the start of name with a key
*
* @param[in] name in string format
* @param[in] key comparison string
* @param[out] next pointer to remaining of name, when the remaining part
* starts with a separator the separator is removed from next
*
* Some examples:
* settings_name_steq("bt/btmesh/iv", "b", &next) returns 1, next="t/btmesh/iv"
* settings_name_steq("bt/btmesh/iv", "bt", &next) returns 1, next="btmesh/iv"
* settings_name_steq("bt/btmesh/iv", "bt/", &next) returns 0, next=NULL
* settings_name_steq("bt/btmesh/iv", "bta", &next) returns 0, next=NULL
*
* REMARK: This routine could be simplified if the settings_handler names
* would include a separator at the end.
*
* @return 0: no match
* 1: match, next can be used to check if match is full
*/
int settings_name_steq(const char *name, const char *key, const char **next);

/**
* determine the number of characters before the first separator
*
* @param[in] name in string format
* @param[out] next pointer to remaining of name (excluding separator)
*
* @return index of the first separator, in case no separator was found this
* is the size of name
*
*/
int settings_name_next(const char *name, const char **next);

/*
* API for runtime settings
*/
@@ -18,10 +18,9 @@
LOG_MODULE_REGISTER(settings, CONFIG_SETTINGS_LOG_LEVEL);

sys_slist_t settings_handlers;
struct k_mutex settings_lock;


static struct settings_handler *settings_handler_lookup(char *name);

void settings_store_init(void);

void settings_init(void)
@@ -32,80 +31,151 @@ void settings_init(void)

int settings_register(struct settings_handler *handler)
{
if (settings_handler_lookup(handler->name)) {
return -EEXIST;
}
sys_slist_prepend(&settings_handlers, &handler->node);
int rc;
struct settings_handler *ch;

return 0;
k_mutex_lock(&settings_lock, K_FOREVER);

SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (strcmp(handler->name, ch->name) == 0) {
rc = -EEXIST;
goto end;
}
}
sys_slist_append(&settings_handlers, &handler->node);
rc = 0;
end:
k_mutex_unlock(&settings_lock);
return rc;
}

/*
* Find settings_handler based on name.
*/
static struct settings_handler *settings_handler_lookup(char *name)
int settings_name_steq(const char *name, const char *key, const char **next)
{
struct settings_handler *ch;
if (next) {
*next = NULL;
}

SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (!strcmp(name, ch->name)) {
return ch;
if ((!name) || (!key)) {
return 0;
}

/* name might come from flash directly, in flash the name would end
* with '=' or '\0' depending how storage is done. Flash reading is
* limited to what can be read
*/

while ((*key != '\0') && (*key == *name) &&
(*name != '\0') && (*name != SETTINGS_NAME_END)) {
key++;
name++;
}

if (*key != '\0') {
return 0;
}

if (*name == SETTINGS_NAME_SEPARATOR) {
if (next) {
*next = name + 1;
}
return 1;
}
return NULL;

if ((*name == SETTINGS_NAME_END) || (*name == '\0')) {
return 1;
}

return 0;
}

/*
* Separate string into argv array.
*/
int settings_parse_name(char *name, int *name_argc, char *name_argv[])
int settings_name_next(const char *name, const char **next)
{
int i = 0;
int rc = 0;

while (name) {
name_argv[i++] = name;
if (next) {
*next = NULL;
}

while (1) {
if (*name == '\0') {
name = NULL;
break;
}
if (!name) {
return 0;
}

if (*name == *SETTINGS_NAME_SEPARATOR) {
*name = '\0';
name++;
break;
}
name++;
/* name might come from flash directly, in flash the name would end
* with '=' or '\0' depending how storage is done. Flash reading is
* limited to what can be read
*/
while ((*name != '\0') && (*name != SETTINGS_NAME_END) &&
(*name != SETTINGS_NAME_SEPARATOR)) {
rc++;
name++;
}

if (*name == SETTINGS_NAME_SEPARATOR) {
if (next) {
*next = name + 1;
}
return rc;
}

*name_argc = i;
return rc;
}

return 0;
struct settings_handler *settings_parse_and_lookup(const char *name,
const char **next)
{
struct settings_handler *ch, *bestmatch;
const char *tmpnext;

bestmatch = NULL;
if (next) {
*next = NULL;
}

SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (settings_name_steq(name, ch->name, &tmpnext)) {
if ((!bestmatch) ||
(settings_name_steq(ch->name,
bestmatch->name, NULL))) {
bestmatch = ch;
if (next) {
*next = tmpnext;
}
}
}
}
return bestmatch;
}

struct settings_handler *settings_parse_and_lookup(char *name,
int *name_argc,
char *name_argv[])
int settings_commit(void)
{
struct settings_handler *ch;
int rc;
int rc2;

rc = settings_parse_name(name, name_argc, name_argv);
if (rc) {
return NULL;
rc = 0;
SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (ch->h_commit) {
rc2 = ch->h_commit();
if (!rc) {
rc = rc2;
}
}
}
return settings_handler_lookup(name_argv[0]);
return rc;
}

int settings_commit(void)
int settings_commit_subtree(const char *subtree)
{
struct settings_handler *ch;

int rc;
int rc2;

rc = 0;
SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
}
if (ch->h_commit) {
rc2 = ch->h_commit();
if (!rc) {
@@ -23,11 +23,11 @@ struct settings_fcb_load_cb_arg {
void *cb_arg;
};

static int settings_fcb_load(struct settings_store *cs);
static int settings_fcb_load(struct settings_store *cs, const char *subtree);
static int settings_fcb_save(struct settings_store *cs, const char *name,
const char *value, size_t val_len);

static struct settings_store_itf settings_fcb_itf = {
static const struct settings_store_itf settings_fcb_itf = {
.csi_load = settings_fcb_load,
.csi_save = settings_fcb_save,
};
@@ -117,9 +117,10 @@ static int settings_fcb_load_priv(struct settings_store *cs, line_load_cb cb,
return 0;
}

static int settings_fcb_load(struct settings_store *cs)
static int settings_fcb_load(struct settings_store *cs, const char *subtree)
{
return settings_fcb_load_priv(cs, settings_line_load_cb, NULL);
return settings_fcb_load_priv(cs, settings_line_load_cb,
(void *)subtree);
}


0 comments on commit 36edf92

Please sign in to comment.
You can’t perform that action at this time.