Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
settings.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Runtime Inc
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_SETTINGS_SETTINGS_H_
9#define ZEPHYR_INCLUDE_SETTINGS_SETTINGS_H_
10
11#include <sys/types.h>
12#include <zephyr/sys/util.h>
13#include <zephyr/sys/slist.h>
15#include <stdint.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21
35#define SETTINGS_MAX_DIR_DEPTH 8 /* max depth of settings tree */
36#define SETTINGS_MAX_NAME_LEN (8 * SETTINGS_MAX_DIR_DEPTH)
37#define SETTINGS_MAX_VAL_LEN 256
38#define SETTINGS_NAME_SEPARATOR '/'
39#define SETTINGS_NAME_END '='
40
41/* place for settings additions:
42 * up to 7 separators, '=', '\0'
43 */
44#define SETTINGS_EXTRA_LEN ((SETTINGS_MAX_DIR_DEPTH - 1) + 2)
45
59typedef ssize_t (*settings_read_cb)(void *cb_arg, void *data, size_t len);
60
67
68 const char *name;
71 int (*h_get)(const char *key, char *val, int val_len_max);
83 int (*h_set)(const char *key, size_t len, settings_read_cb read_cb,
84 void *cb_arg);
98 int (*h_commit)(void);
105 int (*h_export)(int (*export_func)(const char *name, const void *val,
106 size_t val_len));
125};
126
133
134 const char *name;
137 int (*h_get)(const char *key, char *val, int val_len_max);
149 int (*h_set)(const char *key, size_t len, settings_read_cb read_cb,
150 void *cb_arg);
164 int (*h_commit)(void);
169 int (*h_export)(int (*export_func)(const char *name, const void *val,
170 size_t val_len));
186};
187
202#define SETTINGS_STATIC_HANDLER_DEFINE(_hname, _tree, _get, _set, _commit, \
203 _export) \
204 const STRUCT_SECTION_ITERABLE(settings_handler_static, \
205 settings_handler_ ## _hname) = { \
206 .name = _tree, \
207 .h_get = _get, \
208 .h_set = _set, \
209 .h_commit = _commit, \
210 .h_export = _export, \
211 }
212
223
232
241
250int settings_load_subtree(const char *subtree);
251
268 const char *key,
269 size_t len,
270 settings_read_cb read_cb,
271 void *cb_arg,
272 void *param);
273
292 const char *subtree,
294 void *param);
295
303
315int settings_save_one(const char *name, const void *value, size_t val_len);
316
327int settings_delete(const char *name);
328
336
345int settings_commit_subtree(const char *subtree);
346
358/*
359 * API for config storage
360 */
361
362struct settings_store_itf;
363
373};
374
385 const char *subtree;
397 void *param;
398};
399
406 int (*csi_load)(struct settings_store *cs,
407 const struct settings_load_arg *arg);
421 int (*csi_save_start)(struct settings_store *cs);
428 int (*csi_save)(struct settings_store *cs, const char *name,
429 const char *value, size_t val_len);
439 int (*csi_save_end)(struct settings_store *cs);
451 void *(*csi_storage_get)(struct settings_store *cs);
452};
453
461
469
470
471/*
472 * API for handler lookup
473 */
474
484 const char **next);
485
500 size_t len,
501 settings_read_cb read_cb,
502 void *read_cb_arg,
503 const struct settings_load_arg *load_arg);
535int settings_name_steq(const char *name, const char *key, const char **next);
536
547int settings_name_next(const char *name, const char **next);
552#ifdef CONFIG_SETTINGS_RUNTIME
553
570int settings_runtime_set(const char *name, const void *data, size_t len);
571
581int settings_runtime_get(const char *name, void *data, size_t len);
582
595#endif /* CONFIG_SETTINGS_RUNTIME */
596
608int settings_storage_get(void **storage);
609
610#ifdef __cplusplus
611}
612#endif
613
614#endif /* ZEPHYR_INCLUDE_SETTINGS_SETTINGS_H_ */
void settings_dst_register(struct settings_store *cs)
Register a backend handler acting as destination.
struct settings_handler_static * settings_parse_and_lookup(const char *name, const char **next)
Parses a key to an array of elements and locate corresponding module handler.
void settings_src_register(struct settings_store *cs)
Register a backend handler acting as source.
int settings_call_set_handler(const char *name, size_t len, settings_read_cb read_cb, void *read_cb_arg, const struct settings_load_arg *load_arg)
Calls settings handler.
int settings_name_steq(const char *name, const char *key, const char **next)
Compares the start of name with a key.
int settings_name_next(const char *name, const char **next)
determine the number of characters before the first separator
int settings_runtime_get(const char *name, void *data, size_t len)
Get a value corresponding to a key from a module handler.
int settings_runtime_set(const char *name, const void *data, size_t len)
Set a value with a specific key to a module handler.
int settings_runtime_commit(const char *name)
Apply settings in a module handler.
int settings_delete(const char *name)
Delete a single serialized in persisted storage.
int settings_commit_subtree(const char *subtree)
Call commit for settings handler that belong to subtree.
int settings_load_subtree_direct(const char *subtree, settings_load_direct_cb cb, void *param)
Load limited set of serialized items using given callback.
ssize_t(* settings_read_cb)(void *cb_arg, void *data, size_t len)
Function used to read the data from the settings storage in h_set handler implementations.
Definition: settings.h:59
int settings_commit(void)
Call commit for all settings handler.
int(* settings_load_direct_cb)(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg, void *param)
Callback function used for direct loading.
Definition: settings.h:267
int settings_save(void)
Save currently running serialized items.
int settings_load(void)
Load serialized items from registered persistence sources.
int settings_register(struct settings_handler *cf)
Register a handler for settings items stored in RAM.
int settings_load_subtree(const char *subtree)
Load limited set of serialized items from registered persistence sources.
int settings_save_one(const char *name, const void *value, size_t val_len)
Write a single serialized value to persisted storage (if it has changed value).
int settings_subsys_init(void)
Initialization of settings and backend.
struct _snode sys_snode_t
Single-linked list node structure.
Definition: slist.h:39
__SIZE_TYPE__ ssize_t
Definition: types.h:28
int settings_storage_get(void **storage)
Get the storage instance used by zephyr.
Config handlers without the node element, used for static handlers.
Definition: settings.h:132
int(* h_commit)(void)
This handler gets called after settings has been loaded in full.
Definition: settings.h:164
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.
Definition: settings.h:149
const char * name
Name of subtree.
Definition: settings.h:134
int(* h_export)(int(*export_func)(const char *name, const void *val, size_t val_len))
This gets called to dump all current settings items.
Definition: settings.h:169
int(* h_get)(const char *key, char *val, int val_len_max)
Get values handler of settings items identified by keyword names.
Definition: settings.h:137
Config handlers for subtree implement a set of handler functions.
Definition: settings.h:66
int(* h_export)(int(*export_func)(const char *name, const void *val, size_t val_len))
This gets called to dump all current settings items.
Definition: settings.h:105
sys_snode_t node
Linked list node info for module internal usage.
Definition: settings.h:123
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.
Definition: settings.h:83
int(* h_get)(const char *key, char *val, int val_len_max)
Get values handler of settings items identified by keyword names.
Definition: settings.h:71
const char * name
Name of subtree.
Definition: settings.h:68
int(* h_commit)(void)
This handler gets called after settings has been loaded in full.
Definition: settings.h:98
Arguments for data loading.
Definition: settings.h:379
const char * subtree
Name of the subtree to be loaded.
Definition: settings.h:385
void * param
Parameter for callback function.
Definition: settings.h:397
settings_load_direct_cb cb
Pointer to the callback function.
Definition: settings.h:391
Backend handler functions.
Definition: settings.h:405
int(* csi_load)(struct settings_store *cs, const struct settings_load_arg *arg)
Loads values from storage limited to subtree defined by subtree.
Definition: settings.h:406
int(* csi_save_end)(struct settings_store *cs)
Handler called after an export operation.
Definition: settings.h:439
int(* csi_save_start)(struct settings_store *cs)
Handler called before an export operation.
Definition: settings.h:421
int(* csi_save)(struct settings_store *cs, const char *name, const char *value, size_t val_len)
Save a single key-value pair to storage.
Definition: settings.h:428
Backend handler node for storage handling.
Definition: settings.h:367
sys_snode_t cs_next
Linked list node info for internal usage.
Definition: settings.h:368
const struct settings_store_itf * cs_itf
Backend handler structure.
Definition: settings.h:371
Misc utilities.