Skip to main content

zigbee_core/nwk/frame/command/
route_request.rs

1use zigbee_macros::impl_byte;
2use zigbee_types::IeeeAddress;
3use zigbee_types::ShortAddress;
4
5impl_byte! {
6    #[derive(Debug, Clone)]
7    pub struct RouteRequest {
8        pub command_options: CommandOptions,
9        pub route_request_id: u8,
10        pub destination_address: ShortAddress,
11        pub path_cost: u8,
12        #[parse_if = command_options.destination_ieee()]
13        pub destination_ieee_address: Option<IeeeAddress>,
14    }
15}
16
17impl_byte! {
18    /// Route Request Command Options
19    ///
20    /// See Section 3.4.3.1.1.
21    #[derive(Clone, Copy, Eq, PartialEq)]
22    pub struct CommandOptions(pub u8);
23}
24
25impl core::fmt::Debug for CommandOptions {
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        f.debug_struct("CommandOptions")
28            .field("many_to_one", &self.many_to_one())
29            .field("destination_ieee", &self.destination_ieee())
30            .finish()
31    }
32}
33
34impl CommandOptions {
35    /// Many-to-one flag
36    ///
37    /// The many-to-one flag shall be set to 1 if the route request is being
38    /// initiated by a concentrator device. Otherwise, it shall be set to 0.
39    pub fn many_to_one(&self) -> u8 {
40        (self.0 & mask::MANY_TO_ONE) >> offset::MANY_TO_ONE
41    }
42
43    /// Sets the Many-to-one flag
44    #[must_use]
45    pub fn set_many_to_one(mut self, value: u8) -> Self {
46        self.0 = (self.0 & !mask::MANY_TO_ONE) | (value << offset::MANY_TO_ONE);
47        self
48    }
49
50    /// Destination IEEE Address flag
51    ///
52    /// The destination IEEE address flag shall be set to 1 if the destination
53    /// IEEE address field is present. Otherwise, it shall be set to 0.
54    pub fn destination_ieee(&self) -> bool {
55        (self.0 & mask::DEST_IEEE) != 0
56    }
57
58    /// Sets the Destination IEEE Address flag
59    #[must_use]
60    pub fn set_destination_ieee(mut self, value: bool) -> Self {
61        self.0 = (self.0 & !mask::DEST_IEEE) | (u8::from(value) << offset::DEST_IEEE);
62        self
63    }
64
65    /// Multicast flag
66    pub fn multicast(&self) -> bool {
67        (self.0 & mask::MULTICAST) != 0
68    }
69
70    /// Sets the Multicast flag
71    #[must_use]
72    pub fn set_multicast(mut self, value: bool) -> Self {
73        self.0 = (self.0 & !mask::MULTICAST) | (u8::from(value) << offset::MULTICAST);
74        self
75    }
76}
77
78mod offset {
79    pub const MANY_TO_ONE: u8 = 3;
80    pub const DEST_IEEE: u8 = 5;
81    pub const MULTICAST: u8 = 6;
82}
83
84mod mask {
85    pub const MANY_TO_ONE: u8 = 0b0001_1000;
86    pub const DEST_IEEE: u8 = 0b0010_0000;
87    pub const MULTICAST: u8 = 0b0100_0000;
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn command_options() {
96        let options = CommandOptions(0b0110_1000);
97
98        assert_eq!(options.many_to_one(), 1);
99        assert!(options.destination_ieee());
100        assert!(options.multicast());
101
102        let options = options
103            .set_many_to_one(255)
104            .set_destination_ieee(false)
105            .set_multicast(false);
106
107        assert_eq!(options.many_to_one(), 3);
108        assert!(!options.destination_ieee());
109        assert!(!options.multicast());
110    }
111}