Skip to main content

statime_wire/messages/
management.rs

1use crate::{Error, common::PortIdentity};
2
3/// Management message body
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct ManagementMessage {
6    /// Port identity of the port to which this management message applies
7    pub target_port_identity: PortIdentity,
8    /// Maximum number of times this message can be forwarded by boundary clocks in total.
9    pub starting_boundary_hops: u8,
10    /// Number of further times this message can be forwarded by a boundary clock.
11    pub boundary_hops: u8,
12    /// The requested management action.
13    pub action: ManagementAction,
14}
15
16impl ManagementMessage {
17    pub(crate) fn content_size(&self) -> usize {
18        14
19    }
20
21    pub(crate) fn serialize_content(&self, buffer: &mut [u8]) -> Result<(), crate::Error> {
22        self.target_port_identity
23            .serialize(buffer.get_mut(0..10).ok_or(Error::BufferTooShort)?)?;
24        *buffer.get_mut(11).ok_or(Error::BufferTooShort)? = self.starting_boundary_hops;
25        *buffer.get_mut(12).ok_or(Error::BufferTooShort)? = self.boundary_hops;
26        *buffer.get_mut(13).ok_or(Error::BufferTooShort)? = self.action.to_primitive();
27
28        Ok(())
29    }
30
31    pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, crate::Error> {
32        if buffer.len() < 14 {
33            return Err(Error::BufferTooShort);
34        }
35        Ok(Self {
36            target_port_identity: PortIdentity::deserialize(&buffer[0..10])?,
37            starting_boundary_hops: buffer[11],
38            boundary_hops: buffer[12],
39            action: ManagementAction::from_primitive(buffer[13]),
40        })
41    }
42}
43
44/// Requested action for management of the port, see: 15.4.1.6
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46#[allow(
47    clippy::upper_case_acronyms,
48    reason = "Match the exact terminology used in IEEE1588.2019"
49)]
50pub enum ManagementAction {
51    /// Reserved for future use
52    Reserved,
53    /// Get the requested values
54    GET,
55    /// Set the requested values
56    SET,
57    /// Response to an earlier management request
58    RESPONSE,
59    /// Request the receiver to perform an action
60    COMMAND,
61    /// Acknowledgement of an earlier management request.
62    ACKNOWLEDGE,
63}
64
65impl ManagementAction {
66    pub(crate) fn to_primitive(self) -> u8 {
67        match self {
68            Self::GET => 0x0,
69            Self::SET => 0x1,
70            Self::RESPONSE => 0x2,
71            Self::COMMAND => 0x3,
72            Self::ACKNOWLEDGE => 0x4,
73            Self::Reserved => 0x5,
74        }
75    }
76
77    pub(crate) fn from_primitive(value: u8) -> Self {
78        match value {
79            0x0 => Self::GET,
80            0x1 => Self::SET,
81            0x2 => Self::RESPONSE,
82            0x3 => Self::COMMAND,
83            0x4 => Self::ACKNOWLEDGE,
84            0x5..=u8::MAX => Self::Reserved,
85        }
86    }
87}