Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 35 / 0 / 35
Functions: 100.0% 3 / 0 / 3
Branches: 100.0% 26 / 0 / 26

core/health/goal_vibe_pattern.c
Line Branch Exec Source
1 /**
2 * @file goal_vibe_pattern.c
3 * @brief The Goal Met Vibration pick and pattern reader, host-testable off-device.
4 *
5 * @ingroup lib_core
6 */
7 #include "health/goal_vibe_pattern.h"
8
9 #include <stdbool.h>
10 #include <stddef.h>
11
12 7 GoalVibeKind goal_vibe_pattern_kind(const char *pick)
13 {
14
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 6 times.
7 if (pick == NULL)
15 {
16 1 return GOAL_VIBE_KIND_NONE;
17 }
18
19
6/6
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
✓ Branch 4 → 7 taken 1 time.
✓ Branch 4 → 8 taken 1 time.
✓ Branch 4 → 9 taken 1 time.
✓ Branch 4 → 10 taken 1 time.
6 switch (pick[0])
20 {
21 1 case '\0': return GOAL_VIBE_KIND_NONE;
22 1 case 'S': return GOAL_VIBE_KIND_SHORT;
23 1 case 'L': return GOAL_VIBE_KIND_LONG;
24 1 case 'D': return GOAL_VIBE_KIND_DOUBLE;
25 1 case 'C': return GOAL_VIBE_KIND_CUSTOM;
26 1 default: return GOAL_VIBE_KIND_LIST;
27 }
28 }
29
30 /**
31 * @brief A step held to what the firmware plays.
32 *
33 * @param value The step as written.
34 * @return The step, from GOAL_VIBE_PATTERN_MIN_MS to GOAL_VIBE_PATTERN_MAX_MS.
35 */
36 52 static uint32_t clamp_step(uint32_t value)
37 {
38
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 50 times.
52 if (value < GOAL_VIBE_PATTERN_MIN_MS)
39 {
40 2 return GOAL_VIBE_PATTERN_MIN_MS;
41 }
42
43
2/2
✓ Branch 4 → 5 taken 3 times.
✓ Branch 4 → 6 taken 47 times.
50 if (value > GOAL_VIBE_PATTERN_MAX_MS)
44 {
45 3 return GOAL_VIBE_PATTERN_MAX_MS;
46 }
47
48 47 return value;
49 }
50
51 9 int goal_vibe_pattern_parse(const char *text, uint32_t *out, int capacity)
52 {
53
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 8 times.
9 if (text == NULL)
54 {
55 1 return 0;
56 }
57
58 8 int count = 0;
59 8 uint32_t value = 0;
60 8 bool have = false;
61
62 209 for (const char *at = text;; at++)
63 {
64
4/4
✓ Branch 5 → 6 taken 144 times.
✓ Branch 5 → 10 taken 65 times.
✓ Branch 6 → 7 taken 135 times.
✓ Branch 6 → 10 taken 9 times.
209 if (*at >= '0' && *at <= '9')
65 {
66 // a number already past the longest step is clamped to it anyway, so more digits are
67 // left off rather than wrapping the value round to something short
68
2/2
✓ Branch 7 → 8 taken 120 times.
✓ Branch 7 → 9 taken 15 times.
135 if (value <= GOAL_VIBE_PATTERN_MAX_MS)
69 {
70 120 value = value * 10 + (uint32_t)(*at - '0');
71 }
72 135 have = true;
73 135 continue;
74 }
75
76
4/4
✓ Branch 10 → 11 taken 54 times.
✓ Branch 10 → 14 taken 20 times.
✓ Branch 11 → 12 taken 52 times.
✓ Branch 11 → 14 taken 2 times.
74 if (have && count < capacity)
77 {
78 52 out[count++] = clamp_step(value);
79 }
80 74 value = 0;
81 74 have = false;
82
83
2/2
✓ Branch 14 → 15 taken 8 times.
✓ Branch 14 → 16 taken 66 times.
74 if (*at == '\0')
84 {
85 8 break;
86 }
87 }
88
89 8 return count;
90 }
91