Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 6 / 0 / 6
Functions: 100.0% 1 / 0 / 1
Branches: 100.0% 4 / 0 / 4

core/wire/phone_level.h
Line Branch Exec Source
1 /**
2 * @file phone_level.h
3 * @brief Reads the phone's battery level as the phone sends it.
4 *
5 * The phone sends `PHONE_BATTERY` as a whole percent, -1 when it has no reading yet, or -2 while no
6 * plugin on the phone serves the battery. The number comes off a phone, so it is not trusted: any
7 * other negative is no reading and anything past 100 is full, which keeps a battery readout or a bar
8 * from drawing past either end.
9 *
10 * The sender on the other side is ts/plugin-sources/phone.ts, through the phone battery feature.
11 *
12 * @ingroup lib_core
13 */
14 #pragma once
15 #include <stdint.h>
16
17 // PHONE_NO_LEVEL and WIRE_NO_PLUGIN are generated from the phone's cap table, so the two sides
18 // cannot drift
19 #include "wire/wire_caps.g.h"
20
21 /**
22 * @addtogroup lib_core
23 * @{
24 */
25
26 /**
27 * @brief Reads one level off the wire.
28 *
29 * @param sent The number the phone sent.
30 * @return The level from 0 to 100, WIRE_NO_PLUGIN while no plugin serves the battery, or
31 * PHONE_NO_LEVEL for no reading.
32 */
33 7 static inline int phone_level_read(int32_t sent)
34 {
35
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 6 times.
7 if (sent == WIRE_NO_PLUGIN)
36 {
37 1 return WIRE_NO_PLUGIN;
38 }
39
40
2/2
✓ Branch 4 → 5 taken 3 times.
✓ Branch 4 → 6 taken 3 times.
6 if (sent < 0)
41 {
42 3 return PHONE_NO_LEVEL;
43 }
44
45 3 return sent > 100 ? 100 : (int)sent;
46 }
47
48 /** @} */
49