statime_wire/messages/
p_delay_resp.rs1use crate::{
2 Error,
3 common::{PortIdentity, Timestamp},
4};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct PDelayRespMessage {
9 pub request_receive_timestamp: Timestamp,
11 pub requesting_port_identity: PortIdentity,
13}
14
15impl PDelayRespMessage {
16 pub(crate) fn content_size(&self) -> usize {
17 20
18 }
19
20 pub(crate) fn serialize_content(&self, buffer: &mut [u8]) -> Result<(), crate::Error> {
21 if buffer.len() < 20 {
22 return Err(Error::BufferTooShort);
23 }
24 self.request_receive_timestamp
25 .serialize(&mut buffer[0..10])?;
26 self.requesting_port_identity
27 .serialize(&mut buffer[10..20])?;
28
29 Ok(())
30 }
31
32 pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, crate::Error> {
33 if buffer.len() < 20 {
34 return Err(Error::BufferTooShort);
35 }
36 Ok(Self {
37 request_receive_timestamp: Timestamp::deserialize(&buffer[0..10])?,
38 requesting_port_identity: PortIdentity::deserialize(&buffer[10..20])?,
39 })
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46 use crate::common::{ClockIdentity, PortIdentity};
47
48 #[test]
49 fn timestamp_wireformat() {
50 let representations = [(
51 [
52 0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0, 0x01, 0x02, 0x03, 0x04,
53 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
54 ],
55 PDelayRespMessage {
56 request_receive_timestamp: Timestamp::new(1_169_232_218, 174_389_936).unwrap(),
57 requesting_port_identity: PortIdentity {
58 clock_identity: ClockIdentity([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
59 port_number: 0x090a,
60 },
61 },
62 )];
63
64 for (byte_representation, object_representation) in representations {
65 let mut serialization_buffer = [0; 20];
67 object_representation
68 .serialize_content(&mut serialization_buffer)
69 .unwrap();
70 assert_eq!(serialization_buffer, byte_representation);
71
72 let deserialized_data =
74 PDelayRespMessage::deserialize_content(&byte_representation).unwrap();
75 assert_eq!(deserialized_data, object_representation);
76 }
77 }
78}