./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
91 / 99
91.9%
Branches:
32 / 44
72.7%
Functions:
9 / 9
100.0%

kfsw-modules/temperature-sensor-example/src/temperature_sensor_example.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <string.h>
5
6 #include <zephyr/kernel.h>
7 #include <zephyr/sys/byteorder.h>
8 #include <zephyr/sys/util.h>
9
10 #include <kfsw/platform/time.h>
11 #if CONFIG_KFSW_EVENT
12 #include <kfsw/services/event.h>
13 #endif
14 #include <kfsw/services/log.h>
15
16 #include "temperature_sensor_example_internal.h"
17
18 /*
19 * Reading the ADC takes a driver mutex and sample callbacks run under the table
20 * lock, so the module polls on its own work queue and the table copies the
21 * cached value.
22 */
23
24 static struct kfsw_temp_example_reading cached = {
25 .milli_c = KFSW_TEMP_EXAMPLE_INVALID_MILLI_C,
26 };
27 static K_MUTEX_DEFINE(cache_lock);
28 static bool initialized;
29 static uint64_t last_success_ms;
30 static int32_t alarm_milli_c = CONFIG_KFSW_TEMP_EXAMPLE_ALARM_MILLI_C;
31
32 4 static void report_alarm(bool raised, int32_t milli_c)
33 {
34 #if CONFIG_KFSW_EVENT
35 uint8_t payload[sizeof(int32_t)];
36
37 4 sys_put_be32((uint32_t)milli_c, payload);
38
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 kfsw_event_emit(KFSW_EVENT_SOURCE_APP,
39 raised ? KFSW_EVENT_TEMP_EXAMPLE_ALARM
40 : KFSW_EVENT_TEMP_EXAMPLE_ALARM_CLEARED,
41 raised ? KFSW_EVENT_WARNING : KFSW_EVENT_INFO, payload, sizeof(payload));
42 #endif
43
2/4
✗ Branch 0 not taken.
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 2 times.
4 if (raised) {
44 2 kfsw_log_warning("Temperature %d mC reached the alarm threshold", milli_c);
45 } else {
46 2 kfsw_log_info("Temperature %d mC is back below the alarm threshold", milli_c);
47 }
48 4 }
49
50 28 void kfsw_temp_example_store(int32_t milli_c, uint64_t monotonic_ms)
51 {
52 bool was_alarm;
53 bool is_alarm;
54
55 28 (void)k_mutex_lock(&cache_lock, K_FOREVER);
56 28 cached.milli_c = milli_c;
57 28 cached.last_uptime_ms = (uint32_t)monotonic_ms;
58 28 last_success_ms = monotonic_ms;
59 28 cached.valid = true;
60
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (cached.samples < UINT32_MAX) {
61 28 cached.samples++;
62 }
63 28 was_alarm = cached.alarm;
64 28 is_alarm = milli_c >= alarm_milli_c;
65 28 cached.alarm = is_alarm;
66
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
28 if (is_alarm && !was_alarm && (cached.alarms < UINT32_MAX)) {
67 2 cached.alarms++;
68 }
69 28 k_mutex_unlock(&cache_lock);
70
71 /* Outside the lock: the event ring and the log take their own. */
72
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (is_alarm != was_alarm) {
73 4 report_alarm(is_alarm, milli_c);
74 }
75 28 }
76
77 10 void kfsw_temp_example_store_failure(void)
78 {
79 10 (void)k_mutex_lock(&cache_lock, K_FOREVER);
80 /* Drop the last good reading when a read fails. */
81 10 cached.milli_c = KFSW_TEMP_EXAMPLE_INVALID_MILLI_C;
82 10 cached.valid = false;
83
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (cached.failures < UINT32_MAX) {
84 10 cached.failures++;
85 }
86 10 k_mutex_unlock(&cache_lock);
87 10 }
88
89 8 int kfsw_temp_example_set_alarm(int32_t milli_c)
90 {
91
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 if ((milli_c < KFSW_TEMP_EXAMPLE_ALARM_MIN_MILLI_C) ||
92 (milli_c > KFSW_TEMP_EXAMPLE_ALARM_MAX_MILLI_C)) {
93 2 return -ERANGE;
94 }
95 6 (void)k_mutex_lock(&cache_lock, K_FOREVER);
96 6 alarm_milli_c = milli_c;
97 6 k_mutex_unlock(&cache_lock);
98 6 return 0;
99 }
100
101 2 int kfsw_temp_example_clear(void)
102 {
103 2 int result = 0;
104
105 2 (void)k_mutex_lock(&cache_lock, K_FOREVER);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!initialized) {
107 ✗ result = -EACCES;
108 } else {
109 2 cached.samples = 0U;
110 2 cached.failures = 0U;
111 2 cached.alarms = 0U;
112 }
113 2 k_mutex_unlock(&cache_lock);
114 2 return result;
115 }
116
117 #if CONFIG_KFSW_TEMP_EXAMPLE_SENSOR
118 static struct k_work_q sensor_queue;
119 static K_THREAD_STACK_DEFINE(sensor_stack, CONFIG_KFSW_TEMP_EXAMPLE_STACK_SIZE);
120 static void poll_work_handler(struct k_work *work);
121 static K_WORK_DELAYABLE_DEFINE(poll_work, poll_work_handler);
122
123 8 static int sample_once(void)
124 {
125 static bool failing;
126 int32_t milli_c;
127 int result;
128
129 8 result = kfsw_temp_example_sensor_read(&milli_c);
130
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (result != 0) {
131 2 kfsw_temp_example_store_failure();
132 /* Once per outage, not once per period. */
133
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!failing) {
134 2 kfsw_log_warning("Temperature read failed: %d", result);
135 }
136 2 failing = true;
137 2 return result;
138 }
139
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (failing) {
140 2 kfsw_log_info("Temperature readings are back");
141 }
142 4 failing = false;
143 4 kfsw_temp_example_store(milli_c, kfsw_time_monotonic_ms());
144 4 return 0;
145 }
146
147 8 static void poll_work_handler(struct k_work *work)
148 {
149 ARG_UNUSED(work);
150
151 8 (void)sample_once();
152 6 (void)k_work_reschedule_for_queue(&sensor_queue, &poll_work,
153 6 K_MSEC(CONFIG_KFSW_TEMP_EXAMPLE_PERIOD_MS));
154 6 }
155 #endif
156
157 4 int kfsw_temp_example_init(void)
158 {
159 4 (void)k_mutex_lock(&cache_lock, K_FOREVER);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (initialized) {
161 ✗ k_mutex_unlock(&cache_lock);
162 ✗ return 0;
163 }
164
165 #if CONFIG_KFSW_TEMP_EXAMPLE_SENSOR
166 2 int result = kfsw_temp_example_sensor_prepare();
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
168 ✗ k_mutex_unlock(&cache_lock);
169 ✗ kfsw_log_error("Temperature sensor not ready: %d", result);
170 ✗ return result;
171 }
172 2 k_work_queue_start(&sensor_queue, sensor_stack, K_THREAD_STACK_SIZEOF(sensor_stack),
173 CONFIG_KFSW_TEMP_EXAMPLE_PRIORITY, NULL);
174 2 (void)k_thread_name_set(&sensor_queue.thread, "kfsw_temp");
175 2 (void)k_work_reschedule_for_queue(&sensor_queue, &poll_work, K_NO_WAIT);
176 2 kfsw_log_info("Temperature sampled every %d ms", CONFIG_KFSW_TEMP_EXAMPLE_PERIOD_MS);
177 #endif
178 4 initialized = true;
179 4 k_mutex_unlock(&cache_lock);
180 4 return 0;
181 }
182
183 64 int kfsw_temp_example_get(struct kfsw_temp_example_reading *reading)
184 {
185
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 62 times.
64 if (reading == NULL) {
186 2 return -EINVAL;
187 }
188 62 (void)k_mutex_lock(&cache_lock, K_FOREVER);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!initialized) {
190 ✗ k_mutex_unlock(&cache_lock);
191 ✗ return -EACCES;
192 }
193 62 *reading = cached;
194 62 uint64_t now = kfsw_time_monotonic_ms();
195
196
3/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
62 if (reading->valid && ((now < last_success_ms) ||
197
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 28 times.
34 (now - last_success_ms > CONFIG_KFSW_TEMP_EXAMPLE_MAX_AGE_MS))) {
198 6 reading->valid = false;
199 6 reading->milli_c = KFSW_TEMP_EXAMPLE_INVALID_MILLI_C;
200 }
201 62 k_mutex_unlock(&cache_lock);
202 62 return 0;
203 }
204