Skip to main content

simple_someip/protocol/sd/
flags.rs

1const REBOOT_FLAG: u8 = 0b1000_0000;
2const UNICAST_FLAG: u8 = 0b0100_0000;
3
4/// Flags byte in the SD protocol.
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub struct Flags {
7    reboot: bool,
8    unicast: bool,
9}
10
11impl From<u8> for Flags {
12    /// Only the two most significant bits are used.
13    fn from(value: u8) -> Self {
14        Self {
15            reboot: value & REBOOT_FLAG != 0,
16            unicast: value & UNICAST_FLAG != 0,
17        }
18    }
19}
20
21impl From<Flags> for u8 {
22    fn from(flags: Flags) -> u8 {
23        let mut value = 0;
24        if flags.reboot {
25            value |= REBOOT_FLAG;
26        }
27        if flags.unicast {
28            value |= UNICAST_FLAG;
29        }
30        value
31    }
32}
33
34impl Flags {
35    #[must_use]
36    pub fn new(reboot: bool, unicast: bool) -> Self {
37        Self { reboot, unicast }
38    }
39    #[must_use]
40    pub fn new_sd(reboot: bool) -> Self {
41        Self {
42            reboot,
43            unicast: true,
44        }
45    }
46}