1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "settings.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/settings/settings.h>
12 
13 static bool set_called;
14 static bool get_called;
15 static bool export_called;
16 static bool commit_called;
17 static uint8_t val_aa[4];
18 static uint8_t val_bb;
19 
20 static int val_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg);
21 static int val_handle_commit(void);
22 static int val_handle_export(int (*cb)(const char *name, const void *value, size_t val_len));
23 static int val_handle_get(const char *name, char *val, int val_len_max);
24 
25 SETTINGS_STATIC_HANDLER_DEFINE(val, "test_val", val_handle_get, val_handle_set, val_handle_commit,
26 			       val_handle_export);
27 
val_handle_set(const char * name,size_t len,settings_read_cb read_cb,void * cb_arg)28 static int val_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg)
29 {
30 	const char *next;
31 	int rc;
32 
33 	set_called = true;
34 
35 	if (settings_name_steq(name, "aa", &next) && !next) {
36 		if (len != sizeof(val_aa)) {
37 			return -EINVAL;
38 		}
39 
40 		rc = read_cb(cb_arg, val_aa, sizeof(val_aa));
41 
42 		return 0;
43 	} else if (settings_name_steq(name, "bb", &next) && !next) {
44 		if (len != sizeof(val_bb)) {
45 			return -EINVAL;
46 		}
47 
48 		rc = read_cb(cb_arg, &val_bb, sizeof(val_bb));
49 
50 		return 0;
51 	}
52 
53 	return -ENOENT;
54 }
55 
val_handle_commit(void)56 static int val_handle_commit(void)
57 {
58 	commit_called = true;
59 	return 0;
60 }
61 
val_handle_export(int (* cb)(const char * name,const void * value,size_t val_len))62 static int val_handle_export(int (*cb)(const char *name, const void *value, size_t val_len))
63 {
64 	export_called = true;
65 
66 	(void)cb("test_val/aa", val_aa, sizeof(val_aa));
67 	(void)cb("test_val/bb", &val_bb, sizeof(val_bb));
68 
69 	return 0;
70 }
71 
val_handle_get(const char * name,char * val,int val_len_max)72 static int val_handle_get(const char *name, char *val, int val_len_max)
73 {
74 	const char *next;
75 
76 	get_called = true;
77 
78 	if (settings_name_steq(name, "aa", &next) && !next) {
79 		val_len_max = MIN(val_len_max, sizeof(val_aa));
80 		memcpy(val, val_aa, val_len_max);
81 		return val_len_max;
82 	} else if (settings_name_steq(name, "bb", &next) && !next) {
83 		val_len_max = MIN(val_len_max, sizeof(val_bb));
84 		memcpy(val, &val_bb, val_len_max);
85 		return val_len_max;
86 	}
87 
88 	return -ENOENT;
89 }
90 
settings_state_reset(void)91 void settings_state_reset(void)
92 {
93 	set_called = false;
94 	get_called = false;
95 	export_called = false;
96 	commit_called = false;
97 }
98 
settings_state_get(bool * set,bool * get,bool * export,bool * commit)99 void settings_state_get(bool *set, bool *get, bool *export, bool *commit)
100 {
101 	*set = set_called;
102 	*get = get_called;
103 	*export = export_called;
104 	*commit = commit_called;
105 }
106