2018-02-02 14:30:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2015 Runtime Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2018-09-14 10:43:44 -07:00
|
|
|
#ifndef ZEPHYR_INCLUDE_SETTINGS_SETTINGS_H_
|
|
|
|
#define ZEPHYR_INCLUDE_SETTINGS_SETTINGS_H_
|
2018-02-02 14:30:56 +01:00
|
|
|
|
2018-05-15 14:25:29 +02:00
|
|
|
#include <sys/types.h>
|
2018-02-02 14:30:56 +01:00
|
|
|
#include <misc/util.h>
|
|
|
|
#include <misc/slist.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2018-03-22 15:06:36 +01:00
|
|
|
* @defgroup settings Settings subsystem
|
2018-12-08 13:53:36 -05:00
|
|
|
* @ingroup file_system_storage
|
2018-02-02 14:30:56 +01:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 "/"
|
|
|
|
|
|
|
|
/* pleace for settings additions:
|
|
|
|
* up to 7 separators, '=', '\0'
|
|
|
|
*/
|
|
|
|
#define SETTINGS_EXTRA_LEN ((SETTINGS_MAX_DIR_DEPTH - 1) + 2)
|
|
|
|
|
|
|
|
#define SETTINGS_NMGR_OP 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @struct settings_handler
|
|
|
|
* Config handlers for subtree implement a set of handler functions.
|
2018-05-15 14:25:29 +02:00
|
|
|
* These are registered using a call to @ref settings_register.
|
2018-02-02 14:30:56 +01:00
|
|
|
*/
|
|
|
|
struct settings_handler {
|
|
|
|
sys_snode_t node;
|
2018-05-15 14:25:29 +02:00
|
|
|
/**< Linked list node info for module internal usage. */
|
|
|
|
|
2018-02-02 14:30:56 +01:00
|
|
|
char *name;
|
2018-05-15 14:25:29 +02:00
|
|
|
/**< Name of subtree. */
|
|
|
|
|
|
|
|
int (*h_get)(int argc, char **argv, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int (*h_set)(int argc, char **argv, void *value_ctx);
|
|
|
|
/**< Set value handler of settings items identified by keyword names.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - argc - count of item in argv, argv - array of pointers to keyword
|
|
|
|
* names.
|
|
|
|
* - value_ctx - pointer to the value contex which is used paramiter
|
|
|
|
* for data extracting routine (@ref settings_val_read_cb).
|
|
|
|
*/
|
|
|
|
|
2018-02-02 14:30:56 +01:00
|
|
|
int (*h_commit)(void);
|
2018-05-15 14:25:29 +02:00
|
|
|
/**< This handler gets called after settings has been loaded in full.
|
|
|
|
* User might use it to apply setting to the application.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int (*h_export)(int (*export_func)(const char *name, void *val,
|
|
|
|
size_t val_len));
|
|
|
|
/**< This gets called to dump all current settings items.
|
|
|
|
*
|
|
|
|
* This happens when @ref settings_save tries to save the settings.
|
|
|
|
* Parameters:
|
|
|
|
* - export_func: the pointer to the internal function which appends
|
|
|
|
* a single key-value pair to persisted settings. Don't store
|
|
|
|
* duplicated value. The name is subtree/key string, val is the string
|
|
|
|
* with value.
|
|
|
|
*
|
|
|
|
* @remarks The User might limit a implementations of handler to serving
|
|
|
|
* only one keyword at one call - what will impose limit to get/set
|
|
|
|
* values using full subtree/key name.
|
|
|
|
*/
|
2018-02-02 14:30:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization of settings and backend
|
|
|
|
*
|
|
|
|
* Can be called at application startup.
|
|
|
|
* In case the backend is NFFS Remember to call it after FS was mounted.
|
|
|
|
* For FCB backend it can be called without such a restriction.
|
2018-04-16 12:48:37 +02:00
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
2018-02-02 14:30:56 +01:00
|
|
|
*/
|
2018-04-16 12:48:37 +02:00
|
|
|
int settings_subsys_init(void);
|
2018-02-02 14:30:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a handler for settings items.
|
|
|
|
*
|
|
|
|
* @param cf Structure containing registration info.
|
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*/
|
|
|
|
int settings_register(struct settings_handler *cf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load serialized items from registered persistence sources. Handlers for
|
|
|
|
* serialized item subtrees registered earlier will be called for encountered
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*/
|
|
|
|
int settings_load(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int settings_save(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a single serialized value to persisted storage (if it has
|
|
|
|
* changed value).
|
|
|
|
*
|
|
|
|
* @param name Name/key of the settings item.
|
2018-05-15 14:25:29 +02:00
|
|
|
* @param value Pointer to the value of the settings item. This value will
|
|
|
|
* be transferred to the @ref settings_handler::h_export handler implementation.
|
|
|
|
* @param val_len Length of the value.
|
2018-02-02 14:30:56 +01:00
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*/
|
2018-05-15 14:25:29 +02:00
|
|
|
int settings_save_one(const char *name, void *value, size_t val_len);
|
2018-02-02 14:30:56 +01:00
|
|
|
|
2018-12-06 15:02:06 +01:00
|
|
|
/**
|
|
|
|
* Delete a single serialized in persisted storage.
|
|
|
|
*
|
|
|
|
* Deleting an existing key-value pair in the settings mean
|
|
|
|
* to set its value to NULL.
|
|
|
|
*
|
|
|
|
* @param name Name/key of the settings item.
|
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*/
|
|
|
|
int settings_delete(const char *name);
|
|
|
|
|
2018-02-02 14:30:56 +01:00
|
|
|
/**
|
2018-05-15 14:25:29 +02:00
|
|
|
* Set settings item identified by @p name to be value @p value.
|
2018-02-02 14:30:56 +01:00
|
|
|
* This finds the settings handler for this subtree and calls it's
|
|
|
|
* set handler.
|
|
|
|
*
|
|
|
|
* @param name Name/key of the settings item.
|
2018-05-15 14:25:29 +02:00
|
|
|
* @param value Pointer to the value of the settings item. This value will
|
|
|
|
* be transferred to the @ref settings_handler::h_set handler implementation.
|
|
|
|
* @param len Length of value string.
|
2018-02-02 14:30:56 +01:00
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*/
|
2018-05-15 14:25:29 +02:00
|
|
|
int settings_set_value(char *name, void *value, size_t len);
|
2018-02-02 14:30:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of settings item identified by @p name.
|
|
|
|
* This calls the settings handler h_get for the subtree.
|
|
|
|
*
|
2018-05-15 14:25:29 +02:00
|
|
|
* Configuration handler should copy the string to @p buf, the maximum
|
2018-02-02 14:30:56 +01:00
|
|
|
* number of bytes it will copy is limited by @p buf_len.
|
|
|
|
*
|
|
|
|
* @param name Name/key of the settings item.
|
|
|
|
*
|
|
|
|
* @param buf buffer for value of the settings item.
|
|
|
|
* If value is not string, the value will be filled in *buf.
|
|
|
|
*
|
|
|
|
* @param buf_len size of buf.
|
|
|
|
*
|
2018-05-15 14:25:29 +02:00
|
|
|
* @return Positive: Length of copied dat. Negative: -ERCODE
|
2018-02-02 14:30:56 +01:00
|
|
|
*/
|
2018-05-15 14:25:29 +02:00
|
|
|
int settings_get_value(char *name, char *buf, int buf_len);
|
2018-02-02 14:30:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call commit for all settings handler. This should apply all
|
|
|
|
* settings which has been set, but not applied yet.
|
|
|
|
*
|
|
|
|
* @param name Name of the settings subtree, or NULL to commit everything.
|
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*/
|
|
|
|
int settings_commit(char *name);
|
|
|
|
|
|
|
|
/**
|
2018-05-15 14:25:29 +02:00
|
|
|
* Persistent data extracting routine.
|
2018-02-02 14:30:56 +01:00
|
|
|
*
|
2018-05-15 14:25:29 +02:00
|
|
|
* This function read and decode data from non volatile storage to user buffer
|
|
|
|
* This function should be used inside set handler in order to read the settings
|
|
|
|
* data from backend storage.
|
2018-02-02 14:30:56 +01:00
|
|
|
*
|
2018-05-15 14:25:29 +02:00
|
|
|
* @param[in] value_ctx Data contex provided by the <p>h_set</p> handler.
|
|
|
|
* @param[out] buf Buffer for data read.
|
|
|
|
* @param[in] len Length of <p>buf</p>.
|
2018-02-02 14:30:56 +01:00
|
|
|
*
|
2018-05-15 14:25:29 +02:00
|
|
|
* @retval Negative value on failure. 0 and positive: Length of data loaded to
|
|
|
|
* the <p>buf</p>.
|
2018-02-02 14:30:56 +01:00
|
|
|
*/
|
2018-05-15 14:25:29 +02:00
|
|
|
int settings_val_read_cb(void *value_ctx, void *buf, size_t len);
|
2018-02-02 14:30:56 +01:00
|
|
|
|
2018-11-23 17:03:44 +01:00
|
|
|
/**
|
|
|
|
* This function fetch length of decode data.
|
|
|
|
* This function should be used inside set handler in order to detect the
|
|
|
|
* settings data length.
|
|
|
|
*
|
|
|
|
* @param[in] value_ctx Data contex provided by the <p>h_set</p> handler.
|
|
|
|
*
|
|
|
|
* @retval length of data.
|
|
|
|
*/
|
|
|
|
size_t settings_val_get_len_cb(void *value_ctx);
|
|
|
|
|
2018-02-02 14:30:56 +01:00
|
|
|
/**
|
|
|
|
* @} settings
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Config storage
|
|
|
|
*/
|
|
|
|
struct settings_store_itf;
|
|
|
|
struct settings_store {
|
|
|
|
sys_snode_t cs_next;
|
|
|
|
const struct settings_store_itf *cs_itf;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-14 10:43:44 -07:00
|
|
|
#endif /* ZEPHYR_INCLUDE_SETTINGS_SETTINGS_H_ */
|