Skip to main content

zigbee_core/nwk/frame/command/
rejoin_request.rs

1use zigbee_macros::impl_byte;
2
3impl_byte! {
4    /// Rejoin Request Command Frame
5    #[derive(Debug, Clone)]
6    pub struct RejoinRequest {
7        pub capability_information: CapabilityInformation,
8    }
9}
10
11impl_byte! {
12    /// Capability Information field for Rejoin Request
13    /// See Zigbee spec, Section 3.4.3.5.1.
14    #[derive(Clone, Copy, Eq, PartialEq)]
15    pub struct CapabilityInformation(pub u8);
16}
17
18impl core::fmt::Debug for CapabilityInformation {
19    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20        f.debug_struct("CapabilityInformation")
21            .field("device_type", &self.device_type())
22            .field("power_source", &self.power_source())
23            .field("receiver_on_when_idle", &self.receiver_on_when_idle())
24            .field("allocate_address", &self.allocate_address())
25            .finish()
26    }
27}
28
29impl CapabilityInformation {
30    /// Device type: 1 = router, 0 = end device
31    pub fn device_type(&self) -> u8 {
32        (self.0 & mask::DEVICE_TYPE) >> offset::DEVICE_TYPE
33    }
34
35    #[must_use]
36    pub fn set_device_type(mut self, value: u8) -> Self {
37        self.0 = (self.0 & !mask::DEVICE_TYPE) | (value << offset::DEVICE_TYPE);
38        self
39    }
40
41    /// Power source: 1 = mains, 0 = other
42    pub fn power_source(&self) -> u8 {
43        (self.0 & mask::POWER_SOURCE) >> offset::POWER_SOURCE
44    }
45
46    #[must_use]
47    pub fn set_power_source(mut self, value: u8) -> Self {
48        self.0 = (self.0 & !mask::POWER_SOURCE) | (value << offset::POWER_SOURCE);
49        self
50    }
51
52    /// Receiver on when idle: 1 = enabled, 0 = may be disabled
53    pub fn receiver_on_when_idle(&self) -> u8 {
54        (self.0 & mask::RECEIVER_ON_WHEN_IDLE) >> offset::RECEIVER_ON_WHEN_IDLE
55    }
56
57    #[must_use]
58    pub fn set_receiver_on_when_idle(mut self, value: u8) -> Self {
59        self.0 = (self.0 & !mask::RECEIVER_ON_WHEN_IDLE) | (value << offset::RECEIVER_ON_WHEN_IDLE);
60        self
61    }
62
63    /// Allocate address: 1 = must be issued a 16-bit address, 0 = self-selected
64    pub fn allocate_address(&self) -> u8 {
65        (self.0 & mask::ALLOCATE_ADDRESS) >> offset::ALLOCATE_ADDRESS
66    }
67
68    #[must_use]
69    pub fn set_allocate_address(mut self, value: u8) -> Self {
70        self.0 = (self.0 & !mask::ALLOCATE_ADDRESS) | (value << offset::ALLOCATE_ADDRESS);
71        self
72    }
73}
74
75mod offset {
76    pub const DEVICE_TYPE: u8 = 1;
77    pub const POWER_SOURCE: u8 = 2;
78    pub const RECEIVER_ON_WHEN_IDLE: u8 = 3;
79    pub const ALLOCATE_ADDRESS: u8 = 7;
80}
81
82mod mask {
83    pub const DEVICE_TYPE: u8 = 0b0000_0010;
84    pub const POWER_SOURCE: u8 = 0b0000_0100;
85    pub const RECEIVER_ON_WHEN_IDLE: u8 = 0b0000_1000;
86    pub const ALLOCATE_ADDRESS: u8 = 0b1000_0000;
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_capability_information() {
95        let mut cap = CapabilityInformation(0);
96        assert_eq!(cap.device_type(), 0);
97        assert_eq!(cap.power_source(), 0);
98        assert_eq!(cap.receiver_on_when_idle(), 0);
99        assert_eq!(cap.allocate_address(), 0);
100
101        cap = cap
102            .set_device_type(1)
103            .set_power_source(1)
104            .set_receiver_on_when_idle(1)
105            .set_allocate_address(1);
106        assert_eq!(cap.device_type(), 1);
107        assert_eq!(cap.power_source(), 1);
108        assert_eq!(cap.receiver_on_when_idle(), 1);
109        assert_eq!(cap.allocate_address(), 1);
110    }
111}