1 /* Copyright (c) 2019 Laczen
2 *
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <errno.h>
7 #include <string.h>
8 #include <zephyr/sys/util.h>
9
10 #include <zephyr/settings/settings.h>
11 #include "settings_priv.h"
12
13 struct read_cb_arg {
14 const void *data;
15 size_t len;
16 };
17
settings_runtime_read_cb(void * cb_arg,void * data,size_t len)18 static ssize_t settings_runtime_read_cb(void *cb_arg, void *data, size_t len)
19 {
20 struct read_cb_arg *arg = (struct read_cb_arg *)cb_arg;
21
22 memcpy(data, arg->data, MIN(arg->len, len));
23 return MIN(arg->len, len);
24 }
25
settings_runtime_set(const char * name,const void * data,size_t len)26 int settings_runtime_set(const char *name, const void *data, size_t len)
27 {
28 struct settings_handler_static *ch;
29 const char *name_key;
30 struct read_cb_arg arg;
31
32 ch = settings_parse_and_lookup(name, &name_key);
33 if (!ch) {
34 return -EINVAL;
35 }
36
37 arg.data = data;
38 arg.len = len;
39 return ch->h_set(name_key, len, settings_runtime_read_cb, (void *)&arg);
40 }
41
settings_runtime_get(const char * name,void * data,size_t len)42 int settings_runtime_get(const char *name, void *data, size_t len)
43 {
44 struct settings_handler_static *ch;
45 const char *name_key;
46
47 ch = settings_parse_and_lookup(name, &name_key);
48 if (!ch) {
49 return -EINVAL;
50 }
51
52 if (!ch->h_get) {
53 return -ENOTSUP;
54 }
55
56 return ch->h_get(name_key, data, len);
57 }
58
settings_runtime_commit(const char * name)59 int settings_runtime_commit(const char *name)
60 {
61 struct settings_handler_static *ch;
62 const char *name_key;
63
64 ch = settings_parse_and_lookup(name, &name_key);
65 if (!ch) {
66 return -EINVAL;
67 }
68
69 if (ch->h_commit) {
70 return ch->h_commit();
71 } else {
72 return 0;
73 }
74 }
75