./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
220 / 227
96.9%
Branches:
96 / 106
90.6%
Functions:
23 / 23
100.0%

kfsw-modules/gpredict/src/gpredict.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/platform/time.h>
9 #include <kfsw/services/log.h>
10
11 #include "gpredict_internal.h"
12
13 #define GPREDICT_PARK_AZIMUTH_MDEG (CONFIG_KFSW_GPREDICT_PARK_AZIMUTH_DEG * 1000)
14 #define GPREDICT_PARK_ELEVATION_MDEG (CONFIG_KFSW_GPREDICT_PARK_ELEVATION_DEG * 1000)
15
16 BUILD_ASSERT(CONFIG_KFSW_GPREDICT_AZIMUTH_MIN_DEG < CONFIG_KFSW_GPREDICT_AZIMUTH_MAX_DEG,
17 "kfsw,gpredict azimuth travel must have a minimum below its maximum");
18 BUILD_ASSERT(CONFIG_KFSW_GPREDICT_ELEVATION_MIN_DEG < CONFIG_KFSW_GPREDICT_ELEVATION_MAX_DEG,
19 "kfsw,gpredict elevation travel must have a minimum below its maximum");
20 BUILD_ASSERT(CONFIG_KFSW_GPREDICT_ELEVATION_MIN_DEG >= 0,
21 "a bearing below the horizon is refused, so the travel cannot reach below it");
22 BUILD_ASSERT((CONFIG_KFSW_GPREDICT_PARK_AZIMUTH_DEG >= CONFIG_KFSW_GPREDICT_AZIMUTH_MIN_DEG) &&
23 (CONFIG_KFSW_GPREDICT_PARK_AZIMUTH_DEG <=
24 CONFIG_KFSW_GPREDICT_AZIMUTH_MAX_DEG),
25 "the park azimuth must be inside the azimuth travel");
26 BUILD_ASSERT((CONFIG_KFSW_GPREDICT_PARK_ELEVATION_DEG >= CONFIG_KFSW_GPREDICT_ELEVATION_MIN_DEG) &&
27 (CONFIG_KFSW_GPREDICT_PARK_ELEVATION_DEG <=
28 CONFIG_KFSW_GPREDICT_ELEVATION_MAX_DEG),
29 "the park elevation must be inside the elevation travel");
30 BUILD_ASSERT((CONFIG_KFSW_GPREDICT_GRACE_MS >= KFSW_GPREDICT_GRACE_MIN_MS) &&
31 (CONFIG_KFSW_GPREDICT_GRACE_MS <= KFSW_GPREDICT_GRACE_MAX_MS),
32 "the compiled grace period must be one an operator could also set");
33
34 /*
35 * One tracker per node. The lock covers the status and the last bearing time.
36 * Parameter sample and change callbacks run under the table lock and call in
37 * here, and nothing in here calls the parameter service, so the order is always
38 * table lock then tracker lock and cannot invert.
39 */
40 static struct kfsw_gpredict_status tracker = {
41 .state = KFSW_GPREDICT_PARKED,
42 .grace_ms = CONFIG_KFSW_GPREDICT_GRACE_MS,
43 };
44 static K_MUTEX_DEFINE(tracker_lock);
45 static uint64_t last_bearing_ms;
46 static bool bearing_seen;
47 static bool initialized;
48 static bool refusing;
49
50 static void silence_work_handler(struct k_work *work);
51 static K_WORK_DELAYABLE_DEFINE(silence_work, silence_work_handler);
52
53 /* Lifetime counters saturate; one that wraps hides what it was counting. */
54 150 static void count_up(uint32_t *counter)
55 {
56 /* Saturate: a wrapped counter reads as a quiet pass. */
57
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 8 times.
150 if (*counter < UINT32_MAX) {
58 142 (*counter)++;
59 }
60 150 }
61
62 308 enum kfsw_gpredict_state kfsw_gpredict_next(enum kfsw_gpredict_state state,
63 enum kfsw_gpredict_event event)
64 {
65
5/6
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
308 switch (state) {
66 118 case KFSW_GPREDICT_PARKED:
67
4/5
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
118 switch (event) {
68 66 case KFSW_GPREDICT_EVENT_BEARING:
69 66 return KFSW_GPREDICT_TRACKING;
70 16 case KFSW_GPREDICT_EVENT_SILENCE:
71 case KFSW_GPREDICT_EVENT_PARKED:
72 case KFSW_GPREDICT_EVENT_CLEARED:
73 16 return KFSW_GPREDICT_PARKED;
74 6 case KFSW_GPREDICT_EVENT_PARK_COMMANDED:
75 6 return KFSW_GPREDICT_PARKING;
76 30 case KFSW_GPREDICT_EVENT_FAULT:
77 30 return KFSW_GPREDICT_FAULT;
78 }
79 ✗ break;
80 68 case KFSW_GPREDICT_TRACKING:
81
4/5
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
68 switch (event) {
82 20 case KFSW_GPREDICT_EVENT_BEARING:
83 case KFSW_GPREDICT_EVENT_PARKED:
84 case KFSW_GPREDICT_EVENT_CLEARED:
85 20 return KFSW_GPREDICT_TRACKING;
86 32 case KFSW_GPREDICT_EVENT_SILENCE:
87 32 return KFSW_GPREDICT_HOLDING;
88 10 case KFSW_GPREDICT_EVENT_PARK_COMMANDED:
89 10 return KFSW_GPREDICT_PARKING;
90 6 case KFSW_GPREDICT_EVENT_FAULT:
91 6 return KFSW_GPREDICT_FAULT;
92 }
93 ✗ break;
94 46 case KFSW_GPREDICT_HOLDING:
95
4/5
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
46 switch (event) {
96 6 case KFSW_GPREDICT_EVENT_BEARING:
97 6 return KFSW_GPREDICT_TRACKING;
98 26 case KFSW_GPREDICT_EVENT_SILENCE:
99 case KFSW_GPREDICT_EVENT_PARK_COMMANDED:
100 26 return KFSW_GPREDICT_PARKING;
101 8 case KFSW_GPREDICT_EVENT_PARKED:
102 case KFSW_GPREDICT_EVENT_CLEARED:
103 8 return KFSW_GPREDICT_HOLDING;
104 6 case KFSW_GPREDICT_EVENT_FAULT:
105 6 return KFSW_GPREDICT_FAULT;
106 }
107 ✗ break;
108 36 case KFSW_GPREDICT_PARKING:
109
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
36 switch (event) {
110 /*
111 * A pass does not resume halfway into a park. The park finishes
112 * and the bearing after that starts the next pass.
113 */
114 20 case KFSW_GPREDICT_EVENT_BEARING:
115 case KFSW_GPREDICT_EVENT_SILENCE:
116 case KFSW_GPREDICT_EVENT_PARK_COMMANDED:
117 case KFSW_GPREDICT_EVENT_CLEARED:
118 20 return KFSW_GPREDICT_PARKING;
119 8 case KFSW_GPREDICT_EVENT_PARKED:
120 8 return KFSW_GPREDICT_PARKED;
121 8 case KFSW_GPREDICT_EVENT_FAULT:
122 8 return KFSW_GPREDICT_FAULT;
123 }
124 ✗ break;
125 40 case KFSW_GPREDICT_FAULT:
126
2/3
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
40 switch (event) {
127 8 case KFSW_GPREDICT_EVENT_CLEARED:
128 8 return KFSW_GPREDICT_PARKED;
129 /* An operator clears a fault. Nothing else leaves it. */
130 32 case KFSW_GPREDICT_EVENT_BEARING:
131 case KFSW_GPREDICT_EVENT_SILENCE:
132 case KFSW_GPREDICT_EVENT_PARKED:
133 case KFSW_GPREDICT_EVENT_PARK_COMMANDED:
134 case KFSW_GPREDICT_EVENT_FAULT:
135 32 return KFSW_GPREDICT_FAULT;
136 }
137 ✗ break;
138 }
139
140 /* Not a state and event pair this module defines: contain it. */
141 ✗ return KFSW_GPREDICT_FAULT;
142 }
143
144 614 const char *kfsw_gpredict_state_name(enum kfsw_gpredict_state state)
145 {
146
6/6
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 162 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 98 times.
✓ Branch 4 taken 114 times.
✓ Branch 5 taken 2 times.
614 switch (state) {
147 146 case KFSW_GPREDICT_PARKED:
148 146 return "parked";
149 162 case KFSW_GPREDICT_TRACKING:
150 162 return "tracking";
151 92 case KFSW_GPREDICT_HOLDING:
152 92 return "holding";
153 98 case KFSW_GPREDICT_PARKING:
154 98 return "parking";
155 114 case KFSW_GPREDICT_FAULT:
156 114 return "fault";
157 }
158
159 2 return "unknown";
160 }
161
162 22 static bool pass_is_live(void)
163 {
164
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8 times.
36 return (tracker.state == KFSW_GPREDICT_TRACKING) ||
165
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 (tracker.state == KFSW_GPREDICT_HOLDING);
166 }
167
168 264 static uint32_t silence_now(void)
169 {
170 uint64_t now;
171 uint64_t elapsed;
172
173
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 158 times.
264 if (!bearing_seen) {
174 106 return 0U;
175 }
176
177 158 now = kfsw_time_monotonic_ms();
178 /* A clock that went backwards reports no silence rather than an age. */
179
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 138 times.
158 elapsed = (now > last_bearing_ms) ? (now - last_bearing_ms) : 0U;
180
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 return (elapsed > UINT32_MAX) ? UINT32_MAX : (uint32_t)elapsed;
181 }
182
183 188 static void apply_event(enum kfsw_gpredict_event event)
184 {
185 188 const enum kfsw_gpredict_state previous = (enum kfsw_gpredict_state)tracker.state;
186 188 const enum kfsw_gpredict_state next = kfsw_gpredict_next(previous, event);
187
188
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 180 times.
188 if (event == KFSW_GPREDICT_EVENT_PARKED) {
189 /* The one report that carries its own bearing: park is a position. */
190 8 tracker.actual_azimuth_mdeg = GPREDICT_PARK_AZIMUTH_MDEG;
191 8 tracker.actual_elevation_mdeg = GPREDICT_PARK_ELEVATION_MDEG;
192 }
193
194
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 160 times.
188 if (next == previous) {
195 28 return;
196 }
197
198 160 tracker.state = (uint8_t)next;
199
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 126 times.
160 if (next == KFSW_GPREDICT_FAULT) {
200 34 count_up(&tracker.faults);
201 34 tracker.last_error = -EIO;
202 }
203
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 6 times.
160 if ((previous == KFSW_GPREDICT_HOLDING) && (event == KFSW_GPREDICT_EVENT_SILENCE)) {
204 16 count_up(&tracker.passes_ended);
205 }
206
207 160 kfsw_log_info("Pass tracking %s -> %s", kfsw_gpredict_state_name(previous),
208 kfsw_gpredict_state_name(next));
209 }
210
211 92 void gpredict_apply_event(enum kfsw_gpredict_event event)
212 {
213 92 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
214 92 apply_event(event);
215 92 k_mutex_unlock(&tracker_lock);
216 92 }
217
218 162 static void reset(void)
219 {
220 162 (void)k_work_cancel_delayable(&silence_work);
221 162 tracker = (struct kfsw_gpredict_status){
222 .state = KFSW_GPREDICT_PARKED,
223 .grace_ms = CONFIG_KFSW_GPREDICT_GRACE_MS,
224 };
225 162 last_bearing_ms = 0U;
226 162 bearing_seen = false;
227 162 refusing = false;
228 162 initialized = true;
229 162 }
230
231 160 void gpredict_state_reset(void)
232 {
233 160 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
234 160 reset();
235 160 k_mutex_unlock(&tracker_lock);
236 160 }
237
238 6 int kfsw_gpredict_init(void)
239 {
240 bool first;
241
242 6 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
243 6 first = !initialized;
244
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (first) {
245 2 reset();
246 }
247 6 k_mutex_unlock(&tracker_lock);
248
249
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (first) {
250 2 kfsw_log_info("Pass tracking parked, %u ms grace",
251 (unsigned int)CONFIG_KFSW_GPREDICT_GRACE_MS);
252 }
253 6 return 0;
254 }
255
256 86 static bool within_travel(const struct kfsw_gpredict_bearing *bearing)
257 {
258 170 return (bearing->azimuth_mdeg >= GPREDICT_AZIMUTH_MIN_MDEG) &&
259
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 4 times.
84 (bearing->azimuth_mdeg <= GPREDICT_AZIMUTH_MAX_MDEG) &&
260
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 4 times.
246 (bearing->elevation_mdeg >= GPREDICT_ELEVATION_MIN_MDEG) &&
261
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 2 times.
76 (bearing->elevation_mdeg <= GPREDICT_ELEVATION_MAX_MDEG);
262 }
263
264 26 static void refuse(const struct kfsw_gpredict_bearing *bearing, int error)
265 {
266 26 count_up(&tracker.refusals);
267 26 tracker.last_error = error;
268
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
26 if (!refusing) {
269 /* Once per run of refusals: a stuck predictor sends many. */
270 14 kfsw_log_warning("Pass tracking refused az %d el %d mdeg: %d",
271 bearing->azimuth_mdeg, bearing->elevation_mdeg, error);
272 }
273 26 refusing = true;
274 26 }
275
276 8 void gpredict_refuse_bearing(const struct kfsw_gpredict_bearing *bearing, int error)
277 {
278 8 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
279 8 refuse(bearing, error);
280 8 k_mutex_unlock(&tracker_lock);
281 8 }
282
283 94 int kfsw_gpredict_bearing(const struct kfsw_gpredict_bearing *bearing)
284 {
285 94 int result = 0;
286
287
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 92 times.
94 if (bearing == NULL) {
288 /* Not counted: refusals are bearings the predictor sent that this
289 * rotator cannot reach, and a NULL never came off the wire.
290 */
291 2 return -EINVAL;
292 }
293
294 92 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
295
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 86 times.
92 if (tracker.state == KFSW_GPREDICT_FAULT) {
296 6 result = -EPERM;
297
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 74 times.
86 } else if (!within_travel(bearing)) {
298 12 result = -EINVAL;
299 }
300
301
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 74 times.
92 if (result != 0) {
302 18 refuse(bearing, result);
303 18 k_mutex_unlock(&tracker_lock);
304 18 return result;
305 }
306
307
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72 times.
74 if (refusing) {
308 2 kfsw_log_info("Pass tracking is taking bearings again");
309 }
310 74 refusing = false;
311 74 tracker.azimuth_mdeg = bearing->azimuth_mdeg;
312 74 tracker.elevation_mdeg = bearing->elevation_mdeg;
313 74 last_bearing_ms = kfsw_time_monotonic_ms();
314 74 bearing_seen = true;
315 74 count_up(&tracker.bearings);
316 74 apply_event(KFSW_GPREDICT_EVENT_BEARING);
317 74 (void)k_work_reschedule(&silence_work, K_MSEC(tracker.grace_ms));
318 74 k_mutex_unlock(&tracker_lock);
319 74 return 0;
320 }
321
322 4 int kfsw_gpredict_frequency(uint64_t frequency_hz)
323 {
324 4 int result = 0;
325
326 4 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
327
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (frequency_hz == 0U) {
328 /* Zero is what an unparsed or absent frequency looks like. */
329 2 result = -EINVAL;
330 2 tracker.last_error = result;
331 } else {
332 2 tracker.frequency_hz = frequency_hz;
333 }
334 4 k_mutex_unlock(&tracker_lock);
335 4 return result;
336 }
337
338 16 int kfsw_gpredict_park(void)
339 {
340 16 int result = 0;
341
342 16 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
343
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (tracker.state == KFSW_GPREDICT_FAULT) {
344 /* The rotator cannot be trusted to move, so a park would be a
345 * claim nothing confirmed. An operator clears the fault first.
346 */
347 4 result = -EPERM;
348 4 tracker.last_error = result;
349 } else {
350 12 apply_event(KFSW_GPREDICT_EVENT_PARK_COMMANDED);
351 }
352 16 k_mutex_unlock(&tracker_lock);
353 16 return result;
354 }
355
356 14 int kfsw_gpredict_clear(void)
357 {
358 14 int result = 0;
359
360 14 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
361
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 if (tracker.state != KFSW_GPREDICT_FAULT) {
362 10 result = -EALREADY;
363 } else {
364 4 apply_event(KFSW_GPREDICT_EVENT_CLEARED);
365 }
366 14 k_mutex_unlock(&tracker_lock);
367 14 return result;
368 }
369
370 258 int kfsw_gpredict_get_status(struct kfsw_gpredict_status *status)
371 {
372
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 256 times.
258 if (status == NULL) {
373 2 return -EINVAL;
374 }
375
376 256 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
377 256 tracker.silence_ms = silence_now();
378 256 *status = tracker;
379 256 k_mutex_unlock(&tracker_lock);
380 256 return 0;
381 }
382
383 10 int gpredict_grace_validate(uint32_t grace_ms)
384 {
385
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
10 if ((grace_ms < KFSW_GPREDICT_GRACE_MIN_MS) || (grace_ms > KFSW_GPREDICT_GRACE_MAX_MS)) {
386 4 return -ERANGE;
387 }
388 6 return 0;
389 }
390
391 6 void gpredict_grace_set(uint32_t grace_ms)
392 {
393 6 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
394 6 tracker.grace_ms = grace_ms;
395
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (pass_is_live()) {
396 /* A shortened grace must not have to wait out the old one. */
397 ✗ (void)k_work_reschedule(&silence_work, K_MSEC(grace_ms));
398 }
399 6 k_mutex_unlock(&tracker_lock);
400 6 }
401
402 8 void gpredict_silence_check(void)
403 {
404 uint32_t waited;
405
406 8 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
407 8 waited = silence_now();
408
3/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 2 times.
8 if (pass_is_live() && (waited >= tracker.grace_ms)) {
409 6 apply_event(KFSW_GPREDICT_EVENT_SILENCE);
410 }
411
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
8 if (pass_is_live()) {
412 /*
413 * Wake when the grace period is actually up, not a whole one
414 * later. Waking early by a tick and then waiting again would
415 * take two periods to notice a pass that ended.
416 */
417
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4 times.
6 (void)k_work_reschedule(&silence_work, K_MSEC((waited < tracker.grace_ms)
418 ? (tracker.grace_ms - waited)
419 : tracker.grace_ms));
420 }
421 8 k_mutex_unlock(&tracker_lock);
422 8 }
423
424 4 static void silence_work_handler(struct k_work *work)
425 {
426 ARG_UNUSED(work);
427
428 4 gpredict_silence_check();
429 4 }
430
431 #if CONFIG_ZTEST
432 2 void gpredict_test_set_counters(uint32_t value)
433 {
434 2 (void)k_mutex_lock(&tracker_lock, K_FOREVER);
435 2 tracker.bearings = value;
436 2 tracker.refusals = value;
437 2 tracker.passes_ended = value;
438 2 tracker.faults = value;
439 2 k_mutex_unlock(&tracker_lock);
440 2 }
441 #endif
442