Skip to main content

device/
power.rs

1//! System power device contract (`/devices/power`, kind `b'P'`).
2//!
3//! The device performs hardware power actions only. It does not coordinate an
4//! OS shutdown: the lifecycle subsystem stops tasks and commits storage first,
5//! then calls this device as its final step. Hosts restrict the node to
6//! privileged users through VFS permissions.
7//!
8//! | Number | Command            | Direction | Input | Output | Effect                                   |
9//! |--------|--------------------|-----------|-------|--------|------------------------------------------|
10//! | `0x00` | `GET_CAPABILITIES` | Read      | `()`  | `u8`   | Bit set of [`Capabilities`].             |
11//! | `0x01` | `POWER_OFF`        | Write     | `u8`  | `()`   | Halt or power off; input: exit status.   |
12//! | `0x02` | `RESTART`          | Write     | `u8`  | `()`   | Reset or restart; input: exit status.    |
13//! | `0x03` | `SLEEP`            | Write     | `()`  | `()`   | Enter a low-power state it resumes from. |
14//!
15//! The exit status is meaningful only to hosted targets (process exit code).
16//! An unsupported action fails with `Unsupported`; a successful
17//! action may never return. Payloads are plain bytes.
18
19use file_system::{ControlCommand, define_command};
20
21define_command!(GET_CAPABILITIES, Read, crate::kind::POWER, 0, (), u8);
22define_command!(POWER_OFF, Write, crate::kind::POWER, 1, u8, ());
23define_command!(RESTART, Write, crate::kind::POWER, 2, u8, ());
24define_command!(SLEEP, Write, crate::kind::POWER, 3, (), ());
25
26/// Bit set of power actions a target really performs.
27#[repr(transparent)]
28#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
29pub struct Capabilities(pub u8);
30
31impl Capabilities {
32    pub const NONE: Self = Self(0);
33    pub const POWER_OFF: Self = Self(1 << 0);
34    pub const RESTART: Self = Self(1 << 1);
35    pub const SLEEP: Self = Self(1 << 2);
36
37    pub const fn union(self, other: Self) -> Self {
38        Self(self.0 | other.0)
39    }
40
41    pub const fn contains(self, other: Self) -> bool {
42        self.0 & other.0 == other.0
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use file_system::ControlDirectionFlags;
50
51    #[test]
52    fn identifiers_are_distinct_and_actions_need_write_access() {
53        let identifiers = [
54            GET_CAPABILITIES::IDENTIFIER,
55            POWER_OFF::IDENTIFIER,
56            RESTART::IDENTIFIER,
57            SLEEP::IDENTIFIER,
58        ];
59        for (index, left) in identifiers.iter().enumerate() {
60            for right in &identifiers[index + 1..] {
61                assert_ne!(left, right);
62            }
63        }
64        for action in &identifiers[1..] {
65            assert!(
66                action
67                    .get_direction()
68                    .contains(ControlDirectionFlags::Write)
69            );
70        }
71    }
72
73    #[test]
74    fn capabilities_combine() {
75        let capabilities = Capabilities::POWER_OFF.union(Capabilities::RESTART);
76        assert!(capabilities.contains(Capabilities::RESTART));
77        assert!(!capabilities.contains(Capabilities::SLEEP));
78        assert!(Capabilities::NONE.contains(Capabilities::NONE));
79    }
80}