1use 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#[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}