./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
15 / 33
45.5%
Branches:
2 / 4
50.0%
Functions:
4 / 13
30.8%

kfsw-services/src/param-parameters/param_table.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdint.h>
3
4 #include <zephyr/sys/util.h>
5
6 #include <kfsw/services/parameter.h>
7
8 /*
9 * Parameter service counters and settings.
10 */
11
12 static uint16_t param_count;
13 static uint16_t param_tables;
14 static uint16_t param_persistent;
15 static uint32_t param_saves;
16 static uint32_t param_load_failures;
17 static uint32_t param_save_failures;
18 static uint32_t param_requests_dropped;
19 static uint32_t param_read_resends;
20 static uint8_t param_autosave;
21 #if CONFIG_KFSW_PARAM_PERSISTENCE
22 static uint32_t param_persist_bytes;
23 static uint32_t param_persist_max_bytes;
24 #endif
25
26 48 static void sample_stats(void)
27 {
28 struct kfsw_param_stats stats;
29
30
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
48 if (kfsw_param_get_stats(&stats) != 0) {
31 ✗ return;
32 }
33 48 param_count = stats.count;
34 48 param_tables = stats.tables;
35 48 param_persistent = stats.persistent;
36 #if CONFIG_KFSW_PARAM_PERSISTENCE
37 ✗ param_persist_bytes = kfsw_param_persist_bytes();
38 ✗ param_persist_max_bytes = kfsw_param_persist_max_bytes();
39 #endif
40 48 param_saves = stats.saves;
41 48 param_load_failures = stats.load_failures;
42 48 param_save_failures = stats.save_failures;
43 48 param_requests_dropped = stats.requests_dropped;
44 48 param_read_resends = stats.read_resends;
45 }
46
47 #define PARAM_SAMPLE(field, type) \
48 static void sample_##field(void *value) \
49 { \
50 sample_stats(); \
51 *(type *)value = param_##field; \
52 }
53
54 ✗ PARAM_SAMPLE(count, uint16_t)
55 ✗ PARAM_SAMPLE(tables, uint16_t)
56 ✗ PARAM_SAMPLE(persistent, uint16_t)
57 ✗ PARAM_SAMPLE(saves, uint32_t)
58 ✗ PARAM_SAMPLE(load_failures, uint32_t)
59 ✗ PARAM_SAMPLE(save_failures, uint32_t)
60 ✗ PARAM_SAMPLE(requests_dropped, uint32_t)
61 48 PARAM_SAMPLE(read_resends, uint32_t)
62
63 106 static int validate_autosave(const union kfsw_param_scalar *value)
64 {
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 return (value->u8 > 1U) ? -ERANGE : 0;
66 }
67
68 #if CONFIG_KFSW_PARAM_PERSISTENCE
69 ✗ static void sample_persist_bytes(void *value)
70 {
71 ✗ sample_stats();
72 ✗ *(uint32_t *)value = param_persist_bytes;
73 ✗ }
74
75 ✗ static void sample_persist_max_bytes(void *value)
76 {
77 ✗ sample_stats();
78 ✗ *(uint32_t *)value = param_persist_max_bytes;
79 ✗ }
80 #endif
81
82 static const struct kfsw_param_definition param_param_definitions[] = {
83 {
84 .offset = 0x00U,
85 .type = KFSW_PARAM_U16,
86 .flags = KFSW_PARAM_FLAG_READ_ONLY,
87 .name = "param_count",
88 .description = "Parameters registered across every table",
89 .value = &param_count,
90 .sample = sample_count,
91 },
92 {
93 .offset = 0x02U,
94 .type = KFSW_PARAM_U16,
95 .flags = KFSW_PARAM_FLAG_READ_ONLY,
96 .name = "param_tables",
97 .description = "Tables registered",
98 .value = &param_tables,
99 .sample = sample_tables,
100 },
101 {
102 .offset = 0x04U,
103 .type = KFSW_PARAM_U16,
104 .flags = KFSW_PARAM_FLAG_READ_ONLY,
105 .name = "param_persistent",
106 .description = "Of those, the ones a snapshot carries",
107 .value = &param_persistent,
108 .sample = sample_persistent,
109 },
110 {
111 .offset = 0x08U,
112 .type = KFSW_PARAM_U32,
113 .flags = KFSW_PARAM_FLAG_READ_ONLY,
114 .name = "param_saves",
115 .description = "Snapshots written since boot",
116 .value = &param_saves,
117 .sample = sample_saves,
118 },
119 {
120 .offset = 0x0cU,
121 .type = KFSW_PARAM_U32,
122 .flags = KFSW_PARAM_FLAG_READ_ONLY,
123 .name = "param_load_failures",
124 .description = "Snapshot loads that failed for a reason other than absence",
125 .value = &param_load_failures,
126 .sample = sample_load_failures,
127 },
128 {
129 .offset = 0x10U,
130 .type = KFSW_PARAM_U8,
131 /* Stored and live. */
132 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT |
133 KFSW_PARAM_FLAG_LIVE,
134 .name = "param_autosave",
135 .description = "Write a snapshot after every accepted change",
136 .value = &param_autosave,
137 /* On by default: an accepted change to a persistent value writes the snapshot. */
138 .default_value = {.u8 = 1U},
139 .validate = validate_autosave,
140 },
141 #if CONFIG_KFSW_PARAM_PERSISTENCE
142 {
143 .offset = 0x14U,
144 .type = KFSW_PARAM_U32,
145 .flags = KFSW_PARAM_FLAG_READ_ONLY,
146 .name = "param_persist_bytes",
147 .description = "Space the last snapshot occupied",
148 .value = &param_persist_bytes,
149 .sample = sample_persist_bytes,
150 },
151 {
152 .offset = 0x18U,
153 .type = KFSW_PARAM_U32,
154 .flags = KFSW_PARAM_FLAG_READ_ONLY,
155 /* Snapshot size limit, also the size of its buffer. */
156 .name = "param_persist_max_bytes",
157 .description = "Most space a snapshot is allowed",
158 .value = &param_persist_max_bytes,
159 .sample = sample_persist_max_bytes,
160 },
161 #endif
162 {
163 .offset = 0x1cU,
164 .type = KFSW_PARAM_U32,
165 .flags = KFSW_PARAM_FLAG_READ_ONLY,
166 .name = "param_requests_dropped",
167 .description = "Value requests dropped before worker admission",
168 .value = &param_requests_dropped,
169 .sample = sample_requests_dropped,
170 },
171 {
172 .offset = 0x20U,
173 .type = KFSW_PARAM_U32,
174 .flags = KFSW_PARAM_FLAG_READ_ONLY,
175 .name = "param_read_resends",
176 .description = "Extra read request packets after timeouts since boot; saturates",
177 .value = &param_read_resends,
178 .sample = sample_read_resends,
179 },
180 {
181 .offset = 0x24U,
182 .type = KFSW_PARAM_U32,
183 .flags = KFSW_PARAM_FLAG_READ_ONLY,
184 .name = "param_save_failures",
185 .description = "Snapshot writes that failed since boot",
186 .value = &param_save_failures,
187 .sample = sample_save_failures,
188 },
189 };
190
191 KFSW_PARAM_TABLE_FITS_THE_LINK(param_param_definitions);
192
193 const struct kfsw_param_definition_set kfsw_param_param_definitions = {
194 .table = KFSW_PARAM_PARAM_TABLE_ID,
195 .name = KFSW_PARAM_PARAM_TABLE_NAME,
196 .description = "Parameter counts and snapshot saves",
197 .definitions = param_param_definitions,
198 .count = ARRAY_SIZE(param_param_definitions),
199 };
200
201 72 bool kfsw_param_autosave_enabled(void)
202 {
203 72 return param_autosave != 0U;
204 }
205