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