1 /*
2 * Copyright (c) 2019 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "settings_priv.h"
10 #include "settings_test.h"
11
12 uint8_t val8;
13 uint8_t val8_un;
14 uint32_t val32;
15 uint64_t val64;
16
17 int test_get_called;
18 int test_set_called;
19 int test_commit_called;
20 int test_export_block;
21
22 int c1_handle_get(const char *name, char *val, int val_len_max);
23 int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb,
24 void *cb_arg);
25 int c1_handle_commit(void);
26 int c1_handle_export(int (*cb)(const char *name,
27 const void *value, size_t val_len));
28
29 struct settings_handler c_test_handlers[] = {
30 {
31 .name = "myfoo",
32 .h_get = c1_handle_get,
33 .h_set = c1_handle_set,
34 .h_commit = c1_handle_commit,
35 .h_export = c1_handle_export
36 },
37 };
38
c1_handle_get(const char * name,char * val,int val_len_max)39 int c1_handle_get(const char *name, char *val, int val_len_max)
40 {
41 const char *next;
42
43 test_get_called = 1;
44
45 if (settings_name_steq(name, "mybar", &next) && !next) {
46 val_len_max = MIN(val_len_max, sizeof(val8));
47 memcpy(val, &val8, MIN(val_len_max, sizeof(val8)));
48 return val_len_max;
49 }
50
51 if (settings_name_steq(name, "mybar64", &next) && !next) {
52 val_len_max = MIN(val_len_max, sizeof(val64));
53 memcpy(val, &val64, MIN(val_len_max, sizeof(val64)));
54 return val_len_max;
55 }
56
57 return -ENOENT;
58 }
59
c1_handle_set(const char * name,size_t len,settings_read_cb read_cb,void * cb_arg)60 int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb,
61 void *cb_arg)
62 {
63 size_t val_len;
64 int rc;
65 const char *next;
66
67 test_set_called = 1;
68 if (settings_name_steq(name, "mybar", &next) && !next) {
69 rc = read_cb(cb_arg, &val8, sizeof(val8));
70 zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
71 return 0;
72 }
73
74 if (settings_name_steq(name, "mybar64", &next) && !next) {
75 rc = read_cb(cb_arg, &val64, sizeof(val64));
76 zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
77 return 0;
78 }
79
80 if (settings_name_steq(name, "unaligned", &next) && !next) {
81 val_len = len;
82 zassert_equal(val_len, sizeof(val8_un),
83 "value length: %d, ought equal 1", val_len);
84 rc = read_cb(cb_arg, &val8_un, sizeof(val8_un));
85 zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
86 return 0;
87 }
88
89 return -ENOENT;
90 }
91
c1_handle_commit(void)92 int c1_handle_commit(void)
93 {
94 test_commit_called = 1;
95 return 0;
96 }
97
c1_handle_export(int (* cb)(const char * name,const void * value,size_t val_len))98 int c1_handle_export(int (*cb)(const char *name,
99 const void *value, size_t val_len))
100 {
101 if (test_export_block) {
102 return 0;
103 }
104
105 (void)cb("myfoo/mybar", &val8, sizeof(val8));
106
107 (void)cb("myfoo/mybar64", &val64, sizeof(val64));
108
109 (void)cb("myfoo/unaligned", &val8_un, sizeof(val8_un));
110
111 return 0;
112 }
113
ctest_clear_call_state(void)114 void ctest_clear_call_state(void)
115 {
116 test_get_called = 0;
117 test_set_called = 0;
118 test_commit_called = 0;
119 }
120
ctest_get_call_state(void)121 int ctest_get_call_state(void)
122 {
123 return test_get_called + test_set_called + test_commit_called;
124 }
125
config_wipe_srcs(void)126 void config_wipe_srcs(void)
127 {
128 sys_slist_init(&settings_load_srcs);
129 settings_save_dst = NULL;
130 }
131
132 ZTEST_SUITE(settings_config, NULL, settings_config_setup, NULL, NULL,
133 settings_config_teardown);
134