Skip to main content

zigbee_core/nwk/frame/command/
route_reply.rs

1use zigbee_macros::impl_byte;
2use zigbee_types::IeeeAddress;
3use zigbee_types::ShortAddress;
4
5impl_byte! {
6    /// Route Reply Command Frame
7    #[derive(Debug, Clone)]
8    pub struct RouteReply {
9        pub command_options: CommandOptions,
10        pub route_request_id: u8,
11        pub originator_address: ShortAddress,
12        pub responder_address: ShortAddress,
13        pub path_cost: u8,
14        #[parse_if = command_options.originator_ieee()]
15        pub originator_ieee_address: Option<IeeeAddress>,
16        #[parse_if = command_options.responder_ieee()]
17        pub responder_ieee_address: Option<IeeeAddress>,
18    }
19}
20
21impl_byte! {
22    /// Route Reply Command Options
23    ///
24    /// See Section 3.4.3.2.1.
25    #[derive(Clone, Copy, Eq, PartialEq)]
26    pub struct CommandOptions(pub u8);
27}
28
29impl core::fmt::Debug for CommandOptions {
30    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
31        f.debug_struct("CommandOptions")
32            .field("responder_ieee", &self.responder_ieee())
33            .field("originator_ieee", &self.originator_ieee())
34            .field("multicast", &self.multicast())
35            .finish()
36    }
37}
38
39impl CommandOptions {
40    /// Originator IEEE Address flag
41    pub fn originator_ieee(&self) -> bool {
42        (self.0 & mask::ORIGINATOR_IEEE) != 0
43    }
44
45    /// Sets the Originator IEEE Address flag
46    #[must_use]
47    pub fn set_originator_ieee(mut self, value: bool) -> Self {
48        self.0 = (self.0 & !mask::ORIGINATOR_IEEE) | (u8::from(value) << offset::ORIGINATOR_IEEE);
49        self
50    }
51
52    /// Responder IEEE Address flag
53    pub fn responder_ieee(&self) -> bool {
54        (self.0 & mask::RESPONDER_IEEE) != 0
55    }
56
57    /// Sets the Responder IEEE Address flag
58    #[must_use]
59    pub fn set_responder_ieee(mut self, value: bool) -> Self {
60        self.0 = (self.0 & !mask::RESPONDER_IEEE) | (u8::from(value) << offset::RESPONDER_IEEE);
61        self
62    }
63
64    /// Multicast flag
65    pub fn multicast(&self) -> bool {
66        (self.0 & mask::MULTICAST) != 0
67    }
68
69    /// Sets the Multicast flag
70    #[must_use]
71    pub fn set_multicast(mut self, value: bool) -> Self {
72        self.0 = (self.0 & !mask::MULTICAST) | (u8::from(value) << offset::MULTICAST);
73        self
74    }
75}
76
77mod offset {
78    pub const ORIGINATOR_IEEE: u8 = 4;
79    pub const RESPONDER_IEEE: u8 = 5;
80    pub const MULTICAST: u8 = 6;
81}
82
83mod mask {
84    pub const ORIGINATOR_IEEE: u8 = 0b0001_0000;
85    pub const RESPONDER_IEEE: u8 = 0b0010_0000;
86    pub const MULTICAST: u8 = 0b0100_0000;
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_command_options() {
95        let mut options = CommandOptions(0);
96
97        assert!(!options.originator_ieee());
98        assert!(!options.responder_ieee());
99        assert!(!options.multicast());
100
101        options = options
102            .set_originator_ieee(true)
103            .set_responder_ieee(true)
104            .set_multicast(true);
105        assert!(options.originator_ieee());
106        assert!(options.responder_ieee());
107        assert!(options.multicast());
108    }
109}