1 /*
2  * Copyright (c) 2018-2023 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef __SETTINGS_PRIV_H_
9 #define __SETTINGS_PRIV_H_
10 
11 #include <sys/types.h>
12 #include <zephyr/sys/slist.h>
13 #include <errno.h>
14 #include <zephyr/settings/settings.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 int settings_line_write(const char *name, const char *value, size_t val_len,
21 			off_t w_loc, void *cb_arg);
22 
23 /* Get len of record without alignment to write-block-size */
24 int settings_line_len_calc(const char *name, size_t val_len);
25 
26 int settings_line_dup_check_cb(const char *name, void *val_read_cb_ctx,
27 				off_t off, void *cb_arg);
28 
29 int settings_line_load_cb(const char *name, void *val_read_cb_ctx,
30 			   off_t off, void *cb_arg);
31 
32 typedef int (*line_load_cb)(const char *name, void *val_read_cb_ctx,
33 			     off_t off, void *cb_arg);
34 
35 struct settings_line_read_value_cb_ctx {
36 	void *read_cb_ctx;
37 	off_t off;
38 };
39 
40 struct settings_line_dup_check_arg {
41 	const char *name;
42 	const char *val;
43 	size_t val_len;
44 	int is_dup;
45 };
46 
47 #ifdef CONFIG_SETTINGS_ENCODE_LEN
48 /* in storage line contex */
49 struct line_entry_ctx {
50 	void *stor_ctx;
51 	off_t seek; /* offset of id-value pair within the file */
52 	size_t len; /* len of line without len value */
53 };
54 
55 int settings_next_line_ctx(struct line_entry_ctx *entry_ctx);
56 #endif
57 
58 /**
59  * Read RAW settings line entry data from the storage.
60  *
61  * @param seek offset form the line beginning.
62  * @param[out] out buffer for name
63  * @param[in] len_req size of <p>out</p> buffer
64  * @param[out] len_read length of read name
65  * @param[in] cb_arg settings line storage context expected by the
66  * <p>read_cb</p> implementation
67  *
68  * @retval 0 on success,
69  * -ERCODE on storage errors
70  */
71 int settings_line_raw_read(off_t seek, char *out, size_t len_req,
72 			   size_t *len_read, void *cb_arg);
73 
74 /*
75  * @param val_off offset of the value-string.
76  * @param off from val_off (so within the value-string)
77  */
78 int settings_line_val_read(off_t val_off, off_t off, char *out, size_t len_req,
79 			   size_t *len_read, void *cb_arg);
80 
81 /**
82  * Read the settings line entry name from the storage.
83  *
84  * @param[out] out buffer for name
85  * @param[in] len_req size of <p>out</p> buffer
86  * @param[out] len_read length of read name
87  * @param[in] cb_arg settings line storage context expected by the
88  * <p>read_cb</p> implementation
89  *
90  * @retval 0 on read proper name,
91  * 1 on when read improper name,
92  * -ERCODE on storage errors
93  */
94 int settings_line_name_read(char *out, size_t len_req, size_t *len_read,
95 			    void *cb_arg);
96 
97 size_t settings_line_val_get_len(off_t val_off, void *read_cb_ctx);
98 
99 int settings_line_entry_copy(void *dst_ctx, off_t dst_off, void *src_ctx,
100 			off_t src_off, size_t len);
101 
102 void settings_line_io_init(int (*read_cb)(void *ctx, off_t off, char *buf,
103 					  size_t *len),
104 			  int (*write_cb)(void *ctx, off_t off, char const *buf,
105 					  size_t len),
106 			  size_t (*get_len_cb)(void *ctx),
107 			  uint8_t io_rwbs);
108 
109 
110 extern sys_slist_t settings_load_srcs;
111 extern sys_slist_t settings_handlers;
112 extern struct settings_store *settings_save_dst;
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* __SETTINGS_PRIV_H_ */
119