statime_wire/messages/
management.rs1use crate::{Error, common::PortIdentity};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct ManagementMessage {
6 pub target_port_identity: PortIdentity,
8 pub starting_boundary_hops: u8,
10 pub boundary_hops: u8,
12 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#[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,
53 GET,
55 SET,
57 RESPONSE,
59 COMMAND,
61 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}