1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * Generic Settings storage
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2020 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci *
24d4afb5ceSopenharmony_ci *
25d4afb5ceSopenharmony_ci * This is like an abstract class for non-volatile storage, whether in a file-
26d4afb5ceSopenharmony_ci * system or flash-backed blocks, etc.  Named blobs of variable size are stored
27d4afb5ceSopenharmony_ci * in nonvolatile media of some sort.  Typically, these are JSON objects under
28d4afb5ceSopenharmony_ci * a naming scheme like, eg, "network".
29d4afb5ceSopenharmony_ci *
30d4afb5ceSopenharmony_ci * There's a platform-specific storage identifier opaque_plat provided when the
31d4afb5ceSopenharmony_ci * storage object is instantiated, this describes eg the storage device or
32d4afb5ceSopenharmony_ci * partition in instantiation-specific terms.
33d4afb5ceSopenharmony_ci *
34d4afb5ceSopenharmony_ci * Blobs have a further "filename" associated with them.
35d4afb5ceSopenharmony_ci */
36d4afb5ceSopenharmony_ci
37d4afb5ceSopenharmony_ci#define LSOOPEN_FLAG_WRITEABLE				(1 << 0)
38d4afb5ceSopenharmony_ci
39d4afb5ceSopenharmony_cistruct lws_settings_ops;
40d4afb5ceSopenharmony_ci
41d4afb5ceSopenharmony_citypedef struct {
42d4afb5ceSopenharmony_ci	void						*handle_plat;
43d4afb5ceSopenharmony_ci	const struct lws_settings_ops			*so;
44d4afb5ceSopenharmony_ci	uint8_t						refcount;
45d4afb5ceSopenharmony_ci	void						*opaque_plat;
46d4afb5ceSopenharmony_ci} lws_settings_instance_t;
47d4afb5ceSopenharmony_ci
48d4afb5ceSopenharmony_citypedef struct lws_settings_ops {
49d4afb5ceSopenharmony_ci	int (*get)(lws_settings_instance_t *si, const char *name,
50d4afb5ceSopenharmony_ci		   uint8_t *dest, size_t *max_actual);
51d4afb5ceSopenharmony_ci	/**< if dest is NULL, max_actual is set to the actual length without
52d4afb5ceSopenharmony_ci	 * copying anything out */
53d4afb5ceSopenharmony_ci	int (*set)(lws_settings_instance_t *si, const char *name,
54d4afb5ceSopenharmony_ci		   const uint8_t *src, size_t len);
55d4afb5ceSopenharmony_ci} lws_settings_ops_t;
56d4afb5ceSopenharmony_ci
57d4afb5ceSopenharmony_ci/**
58d4afb5ceSopenharmony_ci * lws_settings_plat_get() - read a named blob from a settings instance
59d4afb5ceSopenharmony_ci *
60d4afb5ceSopenharmony_ci * \param si: the settings instance
61d4afb5ceSopenharmony_ci * \param name: the name of the setting blob in the instance
62d4afb5ceSopenharmony_ci * \param dest: NULL, or the buffer to copy the setting blob info
63d4afb5ceSopenharmony_ci * \param max_actual: point to size of dest, or zero; actual blob size on exit
64d4afb5ceSopenharmony_ci *
65d4afb5ceSopenharmony_ci * If the named blob doesn't exist in the si, or can't read, returns nonzero.
66d4afb5ceSopenharmony_ci * Otherwise, returns 0 and sets *max_actual to the true blob size.  If dest is
67d4afb5ceSopenharmony_ci * non-NULL, as much of the blob as will fit in the amount specified by
68d4afb5ceSopenharmony_ci * *max_actual on entry is copied to dest.
69d4afb5ceSopenharmony_ci */
70d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN int
71d4afb5ceSopenharmony_cilws_settings_plat_get(lws_settings_instance_t *si, const char *name,
72d4afb5ceSopenharmony_ci		      uint8_t *dest, size_t *max_actual);
73d4afb5ceSopenharmony_ci
74d4afb5ceSopenharmony_ci/**
75d4afb5ceSopenharmony_ci * lws_settings_plat_get() - read a named blob from a settings instance
76d4afb5ceSopenharmony_ci *
77d4afb5ceSopenharmony_ci * \param si: the settings instance
78d4afb5ceSopenharmony_ci * \param name: the name of the setting blob in the instance
79d4afb5ceSopenharmony_ci * \param src: blob to copy to settings instance
80d4afb5ceSopenharmony_ci * \param len: length of blob to copy
81d4afb5ceSopenharmony_ci *
82d4afb5ceSopenharmony_ci * Creates or replaces a settings blob of the given name made up of the \p len
83d4afb5ceSopenharmony_ci * bytes of data from \p src.
84d4afb5ceSopenharmony_ci */
85d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN int
86d4afb5ceSopenharmony_cilws_settings_plat_set(lws_settings_instance_t *si, const char *name,
87d4afb5ceSopenharmony_ci		      const uint8_t *src, size_t len);
88d4afb5ceSopenharmony_ci
89d4afb5ceSopenharmony_ci/**
90d4afb5ceSopenharmony_ci * lws_settings_plat_printf() - read a named blob from a settings instance
91d4afb5ceSopenharmony_ci *
92d4afb5ceSopenharmony_ci * \param si: the settings instance
93d4afb5ceSopenharmony_ci * \param name: the name of the setting blob in the instance
94d4afb5ceSopenharmony_ci * \param format: printf-style format string
95d4afb5ceSopenharmony_ci *
96d4afb5ceSopenharmony_ci * Creates or replaces a settings blob of the given name from the printf-style
97d4afb5ceSopenharmony_ci * format string and arguments provided.  There's no specific limit to the size,
98d4afb5ceSopenharmony_ci * the size is computed and then a temp heap buffer used.
99d4afb5ceSopenharmony_ci */
100d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN int
101d4afb5ceSopenharmony_cilws_settings_plat_printf(lws_settings_instance_t *si, const char *name,
102d4afb5ceSopenharmony_ci		         const char *format, ...) LWS_FORMAT(3);
103d4afb5ceSopenharmony_ci
104d4afb5ceSopenharmony_ci#define lws_settings_ops_plat \
105d4afb5ceSopenharmony_ci	.get		= lws_settings_plat_get, \
106d4afb5ceSopenharmony_ci	.set		= lws_settings_plat_set,
107d4afb5ceSopenharmony_ci
108d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN lws_settings_instance_t *
109d4afb5ceSopenharmony_cilws_settings_init(const lws_settings_ops_t *so, void *opaque_plat);
110d4afb5ceSopenharmony_ci
111d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN void
112d4afb5ceSopenharmony_cilws_settings_deinit(lws_settings_instance_t **si);
113