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>
This commit is contained in:
parent
03259dba90
commit
36edf92ca8
20 changed files with 753 additions and 154 deletions
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue