./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
30 / 30
100.0%
Branches:
6 / 10
60.0%
Functions:
10 / 10
100.0%

kfsw-modules/button-test/parameters/button_test_table.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4
5 #include <zephyr/sys/util.h>
6
7 #include <kfsw/modules/button_test.h>
8 #include <kfsw/services/parameter.h>
9
10 /*
11 * The table keeps its own copy of each value and refreshes it from the module's
12 * snapshot, so the module's lock never covers what the parameter service reads.
13 * The names predate the prefix rule and are kept because the ground uses them.
14 *
15 * TODO(template): one definition per value the ground needs; the LEDs show a
16 * setting whose write can fail, so it uses apply.
17 */
18
19 static uint32_t press_count;
20 static uint32_t last_press_s;
21 static uint8_t led_green;
22 static uint8_t led_blue;
23 static uint8_t led_red;
24
25 38 static struct kfsw_button_test_status sample_status(void)
26 {
27 38 struct kfsw_button_test_status status = {
28 .press_count = press_count,
29 .last_press_s = last_press_s,
30 38 .led_green = led_green != 0U,
31 38 .led_blue = led_blue != 0U,
32 38 .led_red = led_red != 0U,
33 };
34
35 38 (void)kfsw_button_test_get_status(&status);
36 38 return status;
37 }
38
39 10 static void sample_press_count(void *value)
40 {
41 10 *(uint32_t *)value = sample_status().press_count;
42 10 }
43
44 10 static void sample_last_press_s(void *value)
45 {
46 10 *(uint32_t *)value = sample_status().last_press_s;
47 10 }
48
49 6 static void sample_led_green(void *value)
50 {
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 *(uint8_t *)value = sample_status().led_green ? 1U : 0U;
52 6 }
53
54 6 static void sample_led_blue(void *value)
55 {
56
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 *(uint8_t *)value = sample_status().led_blue ? 1U : 0U;
57 6 }
58
59 6 static void sample_led_red(void *value)
60 {
61
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 *(uint8_t *)value = sample_status().led_red ? 1U : 0U;
62 6 }
63
64 108 static int validate_led(const union kfsw_param_scalar *value)
65 {
66
3/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 104 times.
108 return ((value == NULL) || (value->u8 > 1U)) ? -ERANGE : 0;
67 }
68
69 38 static int apply_green_led(const union kfsw_param_scalar *value)
70 {
71 38 return kfsw_button_test_set_led(KFSW_BUTTON_TEST_LED_GREEN, value->u8 != 0U);
72 }
73
74 8 static int apply_blue_led(const union kfsw_param_scalar *value)
75 {
76 8 return kfsw_button_test_set_led(KFSW_BUTTON_TEST_LED_BLUE, value->u8 != 0U);
77 }
78
79 8 static int apply_red_led(const union kfsw_param_scalar *value)
80 {
81 8 return kfsw_button_test_set_led(KFSW_BUTTON_TEST_LED_RED, value->u8 != 0U);
82 }
83
84 static const struct kfsw_param_definition button_test_param_definitions[] = {
85 {
86 .offset = 0x00U,
87 .type = KFSW_PARAM_U32,
88 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_DEBUG,
89 .name = "press_count",
90 .unit = "count",
91 .description = "Valid debounced button presses since boot",
92 .value = &press_count,
93 .sample = sample_press_count,
94 },
95 {
96 .offset = 0x04U,
97 .type = KFSW_PARAM_U32,
98 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_DEBUG,
99 .name = "last_press_s",
100 .unit = "s",
101 .description = "Monotonic whole seconds of the latest valid press",
102 .value = &last_press_s,
103 .sample = sample_last_press_s,
104 },
105 {
106 .offset = 0x08U,
107 .type = KFSW_PARAM_U8,
108 .flags = KFSW_PARAM_FLAG_DEBUG,
109 .name = "led_green",
110 .description = "Logical green developer LED state",
111 .value = &led_green,
112 .validate = validate_led,
113 .apply = apply_green_led,
114 .sample = sample_led_green,
115 },
116 {
117 .offset = 0x09U,
118 .type = KFSW_PARAM_U8,
119 .flags = KFSW_PARAM_FLAG_DEBUG,
120 .name = "led_blue",
121 .description = "Logical blue developer LED state",
122 .value = &led_blue,
123 .validate = validate_led,
124 .apply = apply_blue_led,
125 .sample = sample_led_blue,
126 },
127 {
128 .offset = 0x0aU,
129 .type = KFSW_PARAM_U8,
130 .flags = KFSW_PARAM_FLAG_DEBUG,
131 .name = "led_red",
132 .description = "Logical red developer LED state",
133 .value = &led_red,
134 .validate = validate_led,
135 .apply = apply_red_led,
136 .sample = sample_led_red,
137 },
138 };
139
140 KFSW_PARAM_TABLE_FITS_THE_LINK(button_test_param_definitions);
141
142 const struct kfsw_param_definition_set kfsw_button_test_param_definitions = {
143 .table = KFSW_BUTTON_TEST_PARAM_TABLE_ID,
144 .name = KFSW_BUTTON_TEST_PARAM_TABLE_NAME,
145 .description = "Button presses and LED controls",
146 .definitions = button_test_param_definitions,
147 .count = ARRAY_SIZE(button_test_param_definitions),
148 };
149