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