Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.1% 100 / 0 / 103
Functions: 100.0% 11 / 0 / 11
Branches: 77.8% 63 / 0 / 81

core/health/hrv_log.c
Line Branch Exec Source
1 /**
2 * @file hrv_log.c
3 * @brief The heart rate variability readings kept for a face's panels.
4 *
5 * @ingroup lib_core
6 */
7 #include "health/hrv_log.h"
8
9 #include <string.h>
10
11 15 void hrv_log_reset(HrvLog *log)
12 {
13 15 memset(log, 0, sizeof(*log));
14 15 }
15
16 6 HrvState hrv_log_state_of(int motion_score, int steps, uint16_t still_mg, uint16_t walk_steps)
17 {
18
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 5 times.
6 if (steps >= walk_steps)
19 {
20 1 return HRV_STATE_WALKING;
21 }
22
23
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 3 times.
5 if (motion_score > still_mg)
24 {
25 2 return HRV_STATE_BUSY;
26 }
27
28 3 return HRV_STATE_STILL;
29 }
30
31 5 bool hrv_log_counts(HrvState state, HrvCount count)
32 {
33
3/3
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 2 → 5 taken 3 times.
5 switch (count)
34 {
35 1 case HRV_COUNT_STILL:
36 1 return state == HRV_STATE_STILL;
37 1 case HRV_COUNT_ANY:
38 1 return true;
39 3 default:
40 3 return state != HRV_STATE_WALKING;
41 }
42 }
43
44 49 bool hrv_log_roll_day(HrvLog *log, int32_t day)
45 {
46
2/2
✓ Branch 2 → 3 taken 31 times.
✓ Branch 2 → 4 taken 18 times.
49 if (day == log->today)
47 {
48 31 return false;
49 }
50
51 18 memset(log->hour_sum, 0, sizeof(log->hour_sum));
52 18 memset(log->hour_count, 0, sizeof(log->hour_count));
53 18 log->today = day;
54 18 return true;
55 }
56
57 50 void hrv_log_add(HrvLog *log, int ms, uint32_t now_min, int32_t day, int minute_of_day, HrvState state, bool asleep)
58 {
59
2/4
✓ Branch 2 → 3 taken 50 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 50 times.
50 if (ms < 0 || state >= HRV_STATE_COUNT)
60 {
61 ✗ return;
62 }
63
64
2/2
✓ Branch 5 → 6 taken 49 times.
✓ Branch 5 → 7 taken 1 time.
50 uint8_t kept = (uint8_t)(ms > HRV_LOG_MS_MAX ? HRV_LOG_MS_MAX : ms);
65
66 50 log->at_min[log->head] = now_min;
67 50 log->ms[log->head] = kept;
68
2/2
✓ Branch 8 → 9 taken 8 times.
✓ Branch 8 → 10 taken 42 times.
50 log->flags[log->head] = (uint8_t)(state | (asleep ? HRV_LOG_ASLEEP : 0));
69 50 log->head = (uint8_t)((log->head + 1) % HRV_LOG_SLOTS);
70
71 50 log->state_ms[state] = kept;
72 50 log->state_at_min[state] = now_min;
73
74 // a walking reading shows in the history and as the latest walking one, but it is not a
75 // resting figure, so it stays out of the day's hours and the night
76
2/2
✓ Branch 11 → 12 taken 2 times.
✓ Branch 11 → 13 taken 48 times.
50 if (state == HRV_STATE_WALKING)
77 {
78 2 return;
79 }
80
81 48 hrv_log_roll_day(log, day);
82 48 int hour = minute_of_day / 60;
83
3/6
✓ Branch 14 → 15 taken 48 times.
✗ Branch 14 → 19 not taken.
✓ Branch 15 → 16 taken 48 times.
✗ Branch 15 → 19 not taken.
✓ Branch 16 → 17 taken 48 times.
✗ Branch 16 → 19 not taken.
48 if (hour >= 0 && hour < HRV_LOG_HOURS && log->hour_count[hour] < UINT8_MAX &&
84
1/2
✓ Branch 17 → 18 taken 48 times.
✗ Branch 17 → 19 not taken.
48 log->hour_sum[hour] <= UINT16_MAX - kept)
85 {
86 48 log->hour_sum[hour] = (uint16_t)(log->hour_sum[hour] + kept);
87 48 log->hour_count[hour]++;
88 }
89
90 // the watch calls a sleep a nap once it ends, when it started and ended between noon and 21:00.
91 // a reading taken during one cannot ask yet, so it goes by the same hours. a nap's reading
92 // stays out of the night and does not hold the night open either. a sleep in that window
93 // longer than a nap, such as after a night shift, is left out the same way
94
4/4
✓ Branch 19 → 20 taken 18 times.
✓ Branch 19 → 22 taken 30 times.
✓ Branch 20 → 21 taken 15 times.
✓ Branch 20 → 22 taken 3 times.
48 bool nap = minute_of_day >= HRV_LOG_NAP_FROM_MIN && minute_of_day < HRV_LOG_NAP_UNTIL_MIN;
95
4/4
✓ Branch 23 → 24 taken 8 times.
✓ Branch 23 → 25 taken 40 times.
✓ Branch 24 → 25 taken 1 time.
✓ Branch 24 → 26 taken 7 times.
48 if (!asleep || nap)
96 {
97 41 return;
98 }
99
100 // a night is the readings taken asleep with no long gap between them, so the first one after
101 // a day awake starts the count again rather than averaging into last night
102
3/4
✓ Branch 26 → 27 taken 3 times.
✓ Branch 26 → 29 taken 4 times.
✓ Branch 27 → 28 taken 3 times.
✗ Branch 27 → 29 not taken.
10 bool new_night = log->night_last_min == 0 || now_min < log->night_last_min ||
103
2/2
✓ Branch 28 → 29 taken 1 time.
✓ Branch 28 → 30 taken 2 times.
3 now_min - log->night_last_min >= HRV_LOG_NIGHT_GAP_MIN;
104
2/2
✓ Branch 31 → 32 taken 5 times.
✓ Branch 31 → 33 taken 2 times.
7 if (new_night)
105 {
106 5 log->night_sum = 0;
107 5 log->night_count = 0;
108 }
109
110
2/4
✓ Branch 33 → 34 taken 7 times.
✗ Branch 33 → 36 not taken.
✓ Branch 34 → 35 taken 7 times.
✗ Branch 34 → 36 not taken.
7 if (log->night_count < UINT8_MAX && log->night_sum <= UINT16_MAX - kept)
111 {
112 7 log->night_sum = (uint16_t)(log->night_sum + kept);
113 7 log->night_count++;
114 }
115 7 log->night_last_min = now_min;
116 }
117
118 1 int hrv_log_count(const HrvLog *log)
119 {
120 1 int count = 0;
121
2/2
✓ Branch 6 → 3 taken 24 times.
✓ Branch 6 → 7 taken 1 time.
25 for (int slot = 0; slot < HRV_LOG_SLOTS; slot++)
122 {
123
1/2
✓ Branch 3 → 4 taken 24 times.
✗ Branch 3 → 5 not taken.
24 if (log->at_min[slot] != 0)
124 {
125 24 count++;
126 }
127 }
128
129 1 return count;
130 }
131
132 4 bool hrv_log_get(const HrvLog *log, int back, int *ms, uint32_t *at_min, uint8_t *flags)
133 {
134
2/4
✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4 times.
4 if (back < 0 || back >= HRV_LOG_SLOTS)
135 {
136 ✗ return false;
137 }
138
139 4 int slot = (log->head - 1 - back + 2 * HRV_LOG_SLOTS) % HRV_LOG_SLOTS;
140
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 3 times.
4 if (log->at_min[slot] == 0)
141 {
142 1 return false;
143 }
144
145 3 *ms = log->ms[slot];
146 3 *at_min = log->at_min[slot];
147 3 *flags = log->flags[slot];
148 3 return true;
149 }
150
151 53 int hrv_log_hour_avg(const HrvLog *log, int hour)
152 {
153
4/6
✓ Branch 2 → 3 taken 53 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 53 times.
✗ Branch 3 → 5 not taken.
✓ Branch 4 → 5 taken 48 times.
✓ Branch 4 → 6 taken 5 times.
53 if (hour < 0 || hour >= HRV_LOG_HOURS || log->hour_count[hour] == 0)
154 {
155 48 return -1;
156 }
157
158 // rounded to the nearest, the same as the readings themselves
159 5 return (log->hour_sum[hour] + log->hour_count[hour] / 2) / log->hour_count[hour];
160 }
161
162 2 void hrv_log_today(const HrvLog *log, int *low_ms, int *avg_ms, int *high_ms)
163 {
164 2 uint32_t sum = 0;
165 2 uint32_t count = 0;
166 2 int low = -1;
167 2 int high = -1;
168
169
2/2
✓ Branch 12 → 3 taken 48 times.
✓ Branch 12 → 13 taken 2 times.
50 for (int hour = 0; hour < HRV_LOG_HOURS; hour++)
170 {
171 48 int avg = hrv_log_hour_avg(log, hour);
172
2/2
✓ Branch 4 → 5 taken 46 times.
✓ Branch 4 → 6 taken 2 times.
48 if (avg < 0)
173 {
174 46 continue;
175 }
176
177 2 sum += log->hour_sum[hour];
178 2 count += log->hour_count[hour];
179
3/4
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 1 time.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
2 if (low < 0 || avg < low)
180 {
181 1 low = avg;
182 }
183
1/2
✓ Branch 9 → 10 taken 2 times.
✗ Branch 9 → 11 not taken.
2 if (avg > high)
184 {
185 2 high = avg;
186 }
187 }
188
189 2 *low_ms = low;
190 2 *high_ms = high;
191
2/2
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 1 time.
2 *avg_ms = count == 0 ? -1 : (int)((sum + count / 2) / count);
192 2 }
193
194 4 int hrv_log_night_avg(const HrvLog *log)
195 {
196
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 4 times.
4 if (log->night_count == 0)
197 {
198 ✗ return -1;
199 }
200
201 4 return (log->night_sum + log->night_count / 2) / log->night_count;
202 }
203
204 2 bool hrv_log_last(const HrvLog *log, HrvState state, int *ms, uint32_t *at_min)
205 {
206
3/4
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1 time.
2 if (state >= HRV_STATE_COUNT || log->state_at_min[state] == 0)
207 {
208 1 return false;
209 }
210
211 1 *ms = log->state_ms[state];
212 1 *at_min = log->state_at_min[state];
213 1 return true;
214 }
215