Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
27
35
36#define SETTINGS_MAX_DIR_DEPTH 8 /* max depth of settings tree */
37#define SETTINGS_MAX_NAME_LEN (8 * SETTINGS_MAX_DIR_DEPTH)
38#define SETTINGS_MAX_VAL_LEN 256
39#define SETTINGS_NAME_SEPARATOR '/'
40#define SETTINGS_NAME_END '='
41
42/* place for settings additions:
43 * up to 7 separators, '=', '\0'
44 */
45#define SETTINGS_EXTRA_LEN ((SETTINGS_MAX_DIR_DEPTH - 1) + 2)
46
47/* Maximum Settings name length including separators */
48#define SETTINGS_FULL_NAME_LEN ((SETTINGS_MAX_NAME_LEN) + (SETTINGS_EXTRA_LEN) + 1)
49
63typedef ssize_t (*settings_read_cb)(void *cb_arg, void *data, size_t len);
64
71
73 const char *name;
74
76 int cprio;
77
88 int (*h_get)(const char *key, char *val, int val_len_max);
89
102 int (*h_set)(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg);
103
111 int (*h_commit)(void);
112
129 int (*h_export)(int (*export_func)(const char *name, const void *val, size_t val_len));
130
133};
134
142 const char *name;
143
145 int cprio;
146
157 int (*h_get)(const char *key, char *val, int val_len_max);
158
171 int (*h_set)(const char *key, size_t len, settings_read_cb read_cb, void *cb_arg);
172
180 int (*h_commit)(void);
181
198 int (*h_export)(int (*export_func)(const char *name, const void *val, size_t val_len));
199};
200
215
216#define SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(_hname, _tree, _get, _set, _commit, _export, \
217 _cprio) \
218 const STRUCT_SECTION_ITERABLE(settings_handler_static, settings_handler_##_hname) = { \
219 .name = _tree, \
220 .cprio = _cprio, \
221 .h_get = _get, \
222 .h_set = _set, \
223 .h_commit = _commit, \
224 .h_export = _export, \
225 }
226
242#define SETTINGS_STATIC_HANDLER_DEFINE(_hname, _tree, _get, _set, _commit, _export) \
243 SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(_hname, _tree, _get, _set, _commit, _export, 0)
244
255
266
276
285
294int settings_load_subtree(const char *subtree);
295
305ssize_t settings_load_one(const char *name, void *buf, size_t buf_len);
306
314
330typedef int (*settings_load_direct_cb)(const char *key, size_t len, settings_read_cb read_cb,
331 void *cb_arg, void *param);
332
350int settings_load_subtree_direct(const char *subtree, settings_load_direct_cb cb, void *param);
351
359
368int settings_save_subtree(const char *subtree);
369
381int settings_save_one(const char *name, const void *value, size_t val_len);
382
393int settings_delete(const char *name);
394
402
411int settings_commit_subtree(const char *subtree);
412
413#if defined(CONFIG_SETTINGS_SAVE_SINGLE_SUBTREE_WITHOUT_MODIFICATION) || defined(__DOXYGEN__)
427 bool save_if_subtree,
428 bool save_if_single_setting);
429#endif
430
434
440
441/*
442 * API for config storage
443 */
444
445struct settings_store_itf;
446
457
482
501 int (*csi_load)(struct settings_store *cs, const struct settings_load_arg *arg);
502
511 ssize_t (*csi_load_one)(struct settings_store *cs, const char *name, char *buf,
512 size_t buf_len);
513
524 ssize_t (*csi_get_val_len)(struct settings_store *cs, const char *name);
525
531 int (*csi_save_start)(struct settings_store *cs);
532
541 int (*csi_save)(struct settings_store *cs, const char *name, const char *value,
542 size_t val_len);
543
549 int (*csi_save_end)(struct settings_store *cs);
550
556 void *(*csi_storage_get)(struct settings_store *cs);
557};
558
566
574
575/*
576 * API for handler lookup
577 */
578
587struct settings_handler_static *settings_parse_and_lookup(const char *name, const char **next);
588
602int settings_call_set_handler(const char *name, size_t len, settings_read_cb read_cb,
603 void *read_cb_arg, const struct settings_load_arg *load_arg);
607
614
635int settings_name_steq(const char *name, const char *key, const char **next);
636
647int settings_name_next(const char *name, const char **next);
651
652#ifdef CONFIG_SETTINGS_RUNTIME
653
660
670int settings_runtime_set(const char *name, const void *data, size_t len);
671
681int settings_runtime_get(const char *name, void *data, size_t len);
682
694
695#endif /* CONFIG_SETTINGS_RUNTIME */
696
708int settings_storage_get(void **storage);
709
710#ifdef __cplusplus
711}
712#endif
713
714#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:63
int settings_save_subtree_or_single_without_modification(const char *name, bool save_if_subtree, bool save_if_single_setting)
Save a single currently running serialized value to persisted storage (if it has changed value) by re...
int settings_commit(void)
Call commit for all settings handler.
ssize_t settings_load_one(const char *name, void *buf, size_t buf_len)
Load one serialized item from registered persistence sources.
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:330
int settings_save(void)
Save currently running serialized items.
int settings_load(void)
Load serialized items from registered persistence sources.
int settings_save_subtree(const char *subtree)
Save limited set of currently running serialized items.
int settings_register(struct settings_handler *cf)
Register a handler for settings items stored in RAM with commit priority set to default.
int settings_load_subtree(const char *subtree)
Load limited set of serialized items from registered persistence sources.
ssize_t settings_get_val_len(const char *key)
Get the data length of the value relative to the key.
int settings_register_with_cprio(struct settings_handler *cf, int cprio)
Register a handler for settings items stored in RAM with commit priority.
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:140
int(* h_commit)(void)
This handler gets called after settings has been loaded in full.
Definition settings.h:180
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:171
int cprio
Priority of commit, lower value is higher priority.
Definition settings.h:145
const char * name
Name of subtree.
Definition settings.h:142
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:198
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:157
Config handlers for subtree implement a set of handler functions.
Definition settings.h:70
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:129
sys_snode_t node
Linked list node info for module internal usage.
Definition settings.h:132
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:102
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:88
const char * name
Name of subtree.
Definition settings.h:73
int cprio
Priority of commit, lower value is higher priority.
Definition settings.h:76
int(* h_commit)(void)
This handler gets called after settings has been loaded in full.
Definition settings.h:111
Arguments for data loading.
Definition settings.h:462
const char * subtree
Name of the subtree to be loaded.
Definition settings.h:468
void * param
Parameter for callback function.
Definition settings.h:480
settings_load_direct_cb cb
Pointer to the callback function.
Definition settings.h:474
Backend handler functions.
Definition settings.h:488
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:501
int(* csi_save_end)(struct settings_store *cs)
Handler called after an export operation.
Definition settings.h:549
ssize_t(* csi_get_val_len)(struct settings_store *cs, const char *name)
Gets the value's length associated to the Key defined by name.
Definition settings.h:524
ssize_t(* csi_load_one)(struct settings_store *cs, const char *name, char *buf, size_t buf_len)
Loads one value from storage that corresponds to the key defined by name.
Definition settings.h:511
int(* csi_save_start)(struct settings_store *cs)
Handler called before an export operation.
Definition settings.h:531
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:541
Backend handler node for storage handling.
Definition settings.h:450
sys_snode_t cs_next
Linked list node info for internal usage.
Definition settings.h:452
const struct settings_store_itf * cs_itf
Backend handler structure.
Definition settings.h:455
Misc utilities.