statime/datastructures/messages/
management.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::datastructures::{common::PortIdentity, WireFormat, WireFormatError};

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ManagementMessage {
    pub(super) target_port_identity: PortIdentity,
    pub(super) starting_boundary_hops: u8,
    pub(super) boundary_hops: u8,
    pub(super) action: ManagementAction,
}

impl ManagementMessage {
    pub(crate) fn content_size(&self) -> usize {
        14
    }

    pub(crate) fn serialize_content(
        &self,
        buffer: &mut [u8],
    ) -> Result<(), crate::datastructures::WireFormatError> {
        self.target_port_identity.serialize(&mut buffer[0..10])?;
        buffer[11] = self.starting_boundary_hops;
        buffer[12] = self.boundary_hops;
        buffer[13] = self.action.to_primitive();

        Ok(())
    }

    pub(crate) fn deserialize_content(
        buffer: &[u8],
    ) -> Result<Self, crate::datastructures::WireFormatError> {
        if buffer.len() < 14 {
            return Err(WireFormatError::BufferTooShort);
        }
        Ok(Self {
            target_port_identity: PortIdentity::deserialize(&buffer[0..10])?,
            starting_boundary_hops: buffer[11],
            boundary_hops: buffer[12],
            action: ManagementAction::from_primitive(buffer[13]),
        })
    }
}

/// See: 15.4.1.6
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::upper_case_acronyms)]
pub(crate) enum ManagementAction {
    Reserved,
    GET,
    SET,
    RESPONSE,
    COMMAND,
    ACKNOWLEDGE,
}

impl ManagementAction {
    pub fn to_primitive(self) -> u8 {
        match self {
            Self::GET => 0x0,
            Self::SET => 0x1,
            Self::RESPONSE => 0x2,
            Self::COMMAND => 0x3,
            Self::ACKNOWLEDGE => 0x4,
            Self::Reserved => 0x5,
        }
    }

    pub fn from_primitive(value: u8) -> Self {
        match value {
            0x0 => Self::GET,
            0x1 => Self::SET,
            0x2 => Self::RESPONSE,
            0x3 => Self::COMMAND,
            0x4 => Self::ACKNOWLEDGE,
            0x5..=u8::MAX => Self::Reserved,
        }
    }
}