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 __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_cli_register(void);
21 int settings_nmgr_register(void);
22 
23 struct mgmt_cbuf;
24 int settings_cbor_line(struct mgmt_cbuf *cb, char *name, int nlen, char *value,
25 		       int vlen);
26 
27 void settings_line_io_init(int (*read_cb)(void *ctx, off_t off, char *buf,
28 					  size_t *len),
29 			   int (*write_cb)(void *ctx, off_t off,
30 					   char const *buf, size_t len),
31 			   size_t (*get_len_cb)(void *ctx),
32 			   uint8_t io_rwbs);
33 
34 int settings_line_write(const char *name, const char *value, size_t val_len,
35 			off_t w_loc, void *cb_arg);
36 
37 /* Get len of record without alignment to write-block-size */
38 int settings_line_len_calc(const char *name, size_t val_len);
39 
40 int settings_line_dup_check_cb(const char *name, void *val_read_cb_ctx,
41 				off_t off, void *cb_arg);
42 
43 int settings_line_load_cb(const char *name, void *val_read_cb_ctx,
44 			   off_t off, void *cb_arg);
45 
46 typedef int (*line_load_cb)(const char *name, void *val_read_cb_ctx,
47 			     off_t off, void *cb_arg);
48 
49 struct settings_line_read_value_cb_ctx {
50 	void *read_cb_ctx;
51 	off_t off;
52 };
53 
54 struct settings_line_dup_check_arg {
55 	const char *name;
56 	const char *val;
57 	size_t val_len;
58 	int is_dup;
59 };
60 
61 #ifdef CONFIG_SETTINGS_ENCODE_LEN
62 /* in storage line contex */
63 struct line_entry_ctx {
64 	void *stor_ctx;
65 	off_t seek; /* offset of id-value pair within the file */
66 	size_t len; /* len of line without len value */
67 };
68 
69 int settings_next_line_ctx(struct line_entry_ctx *entry_ctx);
70 #endif
71 
72 /**
73  * Read RAW settings line entry data from the storage.
74  *
75  * @param seek offset form the line beginning.
76  * @param[out] out buffer for name
77  * @param[in] len_req size of <p>out</p> buffer
78  * @param[out] len_read length of read name
79  * @param[in] cb_arg settings line storage context expected by the
80  * <p>read_cb</p> implementation
81  *
82  * @retval 0 on success,
83  * -ERCODE on storage errors
84  */
85 int settings_line_raw_read(off_t seek, char *out, size_t len_req,
86 			   size_t *len_read, void *cb_arg);
87 
88 /*
89  * @param val_off offset of the value-string.
90  * @param off from val_off (so within the value-string)
91  */
92 int settings_line_val_read(off_t val_off, off_t off, char *out, size_t len_req,
93 			   size_t *len_read, void *cb_arg);
94 
95 /**
96  * Read the settings line entry name from the storage.
97  *
98  * @param[out] out buffer for name
99  * @param[in] len_req size of <p>out</p> buffer
100  * @param[out] len_read length of read name
101  * @param[in] cb_arg settings line storage context expected by the
102  * <p>read_cb</p> implementation
103  *
104  * @retval 0 on read proper name,
105  * 1 on when read improper name,
106  * -ERCODE on storage errors
107  */
108 int settings_line_name_read(char *out, size_t len_req, size_t *len_read,
109 			    void *cb_arg);
110 
111 size_t settings_line_val_get_len(off_t val_off, void *read_cb_ctx);
112 
113 int settings_line_entry_copy(void *dst_ctx, off_t dst_off, void *src_ctx,
114 			off_t src_off, size_t len);
115 
116 void settings_line_io_init(int (*read_cb)(void *ctx, off_t off, char *buf,
117 					  size_t *len),
118 			  int (*write_cb)(void *ctx, off_t off, char const *buf,
119 					  size_t len),
120 			  size_t (*get_len_cb)(void *ctx),
121 			  uint8_t io_rwbs);
122 
123 
124 extern sys_slist_t settings_load_srcs;
125 extern sys_slist_t settings_handlers;
126 extern struct settings_store *settings_save_dst;
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* __SETTINGS_PRIV_H_ */
133