./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
96 / 107
89.7%
Branches:
33 / 40
82.5%
Functions:
10 / 10
100.0%

kfsw-modules/button-test/src/button_test.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4
5 #include <zephyr/kernel.h>
6 #include <zephyr/sys/util.h>
7
8 #include <kfsw/modules/button_test.h>
9 #include <kfsw/services/log.h>
10
11 #include "button_test_internal.h"
12
13 struct button_test_state {
14 uint32_t press_count;
15 uint32_t last_press_s;
16 uint8_t led_green;
17 uint8_t led_blue;
18 uint8_t led_red;
19 };
20
21 static struct button_test_state button_test_status;
22 static bool button_test_stable_pressed;
23 static bool button_test_initialized;
24
25 static K_MUTEX_DEFINE(kfsw_button_test_lock);
26
27 56 static uint32_t monotonic_seconds(uint64_t monotonic_ms)
28 {
29 56 const uint64_t seconds = monotonic_ms / 1000U;
30
31
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 4 times.
56 return (seconds > UINT32_MAX) ? UINT32_MAX : (uint32_t)seconds;
32 }
33
34 68 void kfsw_button_test_state_reset(bool initially_pressed)
35 {
36 68 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
37 #if CONFIG_KFSW_BUTTON_TEST_LED_GPIO
38 36 (void)kfsw_button_test_led_gpio_set(KFSW_BUTTON_TEST_LED_GREEN, false);
39 36 (void)kfsw_button_test_led_gpio_set(KFSW_BUTTON_TEST_LED_BLUE, false);
40 36 (void)kfsw_button_test_led_gpio_set(KFSW_BUTTON_TEST_LED_RED, false);
41 #endif
42 68 button_test_status = (struct button_test_state){0};
43 68 button_test_stable_pressed = initially_pressed;
44 68 button_test_initialized = true;
45 68 k_mutex_unlock(&kfsw_button_test_lock);
46 68 }
47
48 94 static uint8_t *led_state(enum kfsw_button_test_led led)
49 {
50
4/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 4 times.
94 switch (led) {
51 50 case KFSW_BUTTON_TEST_LED_GREEN:
52 50 return &button_test_status.led_green;
53 20 case KFSW_BUTTON_TEST_LED_BLUE:
54 20 return &button_test_status.led_blue;
55 20 case KFSW_BUTTON_TEST_LED_RED:
56 20 return &button_test_status.led_red;
57 4 default:
58 4 return NULL;
59 }
60 }
61
62 94 static int set_led_locked(enum kfsw_button_test_led led, bool on)
63 {
64 94 uint8_t *state = led_state(led);
65 94 int result = 0;
66
67
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 90 times.
94 if (state == NULL) {
68 4 return -EINVAL;
69 }
70
71 #if CONFIG_KFSW_BUTTON_TEST_LED_GPIO
72 66 result = kfsw_button_test_led_gpio_set(led, on);
73 #endif
74
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 16 times.
90 if (result == 0) {
75
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 34 times.
74 *state = on ? 1U : 0U;
76 }
77 90 return result;
78 }
79
80 94 int kfsw_button_test_set_led(enum kfsw_button_test_led led, bool on)
81 {
82 int result;
83
84 94 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (!button_test_initialized) {
86 ✗ result = -EACCES;
87 } else {
88 94 result = set_led_locked(led, on);
89 }
90 94 k_mutex_unlock(&kfsw_button_test_lock);
91
92
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 74 times.
94 if (result != 0) {
93 20 kfsw_log_warning("Button test LED %d not switched: %d", (int)led, result);
94 }
95 94 return result;
96 }
97
98 106 void kfsw_button_test_process_level(bool pressed, uint64_t monotonic_ms)
99 {
100 106 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
101
3/4
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 76 times.
106 if (!button_test_initialized || (pressed == button_test_stable_pressed)) {
102 30 k_mutex_unlock(&kfsw_button_test_lock);
103 30 return;
104 }
105
106 76 button_test_stable_pressed = pressed;
107
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 20 times.
76 if (pressed) {
108
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 8 times.
56 if (button_test_status.press_count < UINT32_MAX) {
109 48 button_test_status.press_count++;
110 }
111 56 button_test_status.last_press_s = monotonic_seconds(monotonic_ms);
112 }
113 76 uint32_t press_count = button_test_status.press_count;
114 76 k_mutex_unlock(&kfsw_button_test_lock);
115
116
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 20 times.
76 if (pressed) {
117 56 kfsw_log_info("Button pressed, %u presses since boot", press_count);
118 }
119 }
120
121 #if CONFIG_ZTEST
122 4 void kfsw_button_test_test_disable(void)
123 {
124 4 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
125 4 button_test_initialized = false;
126 4 k_mutex_unlock(&kfsw_button_test_lock);
127 4 }
128
129 4 void kfsw_button_test_test_set_press_count(uint32_t press_count)
130 {
131 4 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
132 4 button_test_status.press_count = press_count;
133 4 k_mutex_unlock(&kfsw_button_test_lock);
134 4 }
135 #endif
136
137 6 int kfsw_button_test_init(void)
138 {
139 6 bool initially_pressed = false;
140 6 int result = 0;
141
142 6 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (button_test_initialized) {
144 ✗ k_mutex_unlock(&kfsw_button_test_lock);
145 ✗ return 0;
146 }
147
148 #if CONFIG_KFSW_BUTTON_TEST_GPIO
149 2 result = kfsw_button_test_gpio_prepare(&initially_pressed);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
151 ✗ k_mutex_unlock(&kfsw_button_test_lock);
152 ✗ kfsw_log_error("Button GPIO not ready: %d", result);
153 ✗ return result;
154 }
155 #endif
156
157 #if CONFIG_KFSW_BUTTON_TEST_LED_GPIO
158 4 result = kfsw_button_test_led_gpio_prepare();
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (result != 0) {
160 ✗ k_mutex_unlock(&kfsw_button_test_lock);
161 ✗ kfsw_log_error("LED GPIO not ready: %d", result);
162 ✗ return result;
163 }
164 #endif
165
166 6 button_test_status = (struct button_test_state){0};
167 6 button_test_stable_pressed = initially_pressed;
168 6 button_test_initialized = true;
169
170 #if CONFIG_KFSW_BUTTON_TEST_GPIO
171 2 result = kfsw_button_test_gpio_start();
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
173 ✗ button_test_initialized = false;
174 }
175 #endif
176
177 6 k_mutex_unlock(&kfsw_button_test_lock);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (result != 0) {
179 ✗ kfsw_log_error("Button test did not start: %d", result);
180 } else {
181 6 kfsw_log_info("Button test started");
182 }
183 6 return result;
184 }
185
186 178 int kfsw_button_test_get_status(struct kfsw_button_test_status *status)
187 {
188 178 int result = 0;
189
190
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 174 times.
178 if (status == NULL) {
191 4 return -EINVAL;
192 }
193
194 174 k_mutex_lock(&kfsw_button_test_lock, K_FOREVER);
195
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 140 times.
174 if (!button_test_initialized) {
196 34 result = -EACCES;
197 } else {
198 140 status->press_count = button_test_status.press_count;
199 140 status->last_press_s = button_test_status.last_press_s;
200 140 status->led_green = button_test_status.led_green != 0U;
201 140 status->led_blue = button_test_status.led_blue != 0U;
202 140 status->led_red = button_test_status.led_red != 0U;
203 }
204 174 k_mutex_unlock(&kfsw_button_test_lock);
205 174 return result;
206 }
207