Skip to main content

zigbee_core/nwk/frame/command/
leave.rs

1use zigbee_macros::impl_byte;
2
3impl_byte! {
4    /// Leave Command Frame
5    #[derive(Debug, Clone)]
6    pub struct Leave {
7        pub command_options: CommandOptions,
8    }
9}
10
11impl_byte! {
12    /// Leave Command Options
13    #[derive(Clone, Copy, Eq, PartialEq)]
14    pub struct CommandOptions(pub u8);
15}
16
17impl core::fmt::Debug for CommandOptions {
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        f.debug_struct("CommandOptions")
20            .field("rejoin", &self.rejoin())
21            .field("request", &self.request())
22            .field("remove_children", &self.remove_children())
23            .finish()
24    }
25}
26
27impl CommandOptions {
28    /// Rejoin flag
29    pub fn rejoin(&self) -> bool {
30        (self.0 & mask::REJOIN) != 0
31    }
32
33    /// Sets the Rejoin flag
34    #[must_use]
35    pub fn set_rejoin(mut self, value: bool) -> Self {
36        self.0 = (self.0 & !mask::REJOIN) | (u8::from(value) << offset::REJOIN);
37        self
38    }
39
40    /// Request flag
41    pub fn request(&self) -> bool {
42        (self.0 & mask::REQUEST) != 0
43    }
44
45    /// Sets the Request flag
46    #[must_use]
47    pub fn set_request(mut self, value: bool) -> Self {
48        self.0 = (self.0 & !mask::REQUEST) | (u8::from(value) << offset::REQUEST);
49        self
50    }
51
52    /// Remove children flag
53    pub fn remove_children(&self) -> bool {
54        (self.0 & mask::REMOVE_CHILDREN) != 0
55    }
56
57    /// Sets the Remove children flag
58    #[must_use]
59    pub fn set_remove_children(mut self, value: bool) -> Self {
60        self.0 = (self.0 & !mask::REMOVE_CHILDREN) | (u8::from(value) << offset::REMOVE_CHILDREN);
61        self
62    }
63}
64
65mod offset {
66    pub const REJOIN: u8 = 5;
67    pub const REQUEST: u8 = 6;
68    pub const REMOVE_CHILDREN: u8 = 7;
69}
70
71mod mask {
72    pub const REJOIN: u8 = 0b0010_0000;
73    pub const REQUEST: u8 = 0b0100_0000;
74    pub const REMOVE_CHILDREN: u8 = 0b1000_0000;
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_command_options() {
83        let mut options = CommandOptions(0);
84        assert!(!options.rejoin());
85        assert!(!options.request());
86        assert!(!options.remove_children());
87
88        options = options
89            .set_rejoin(true)
90            .set_request(true)
91            .set_remove_children(true);
92        assert!(options.rejoin());
93        assert!(options.request());
94        assert!(options.remove_children());
95    }
96}