From cc066933fb4be5a8d5c5a22e856e0334399649dc Mon Sep 17 00:00:00 2001 From: morgan Date: Wed, 9 Oct 2024 12:28:23 +0800 Subject: [PATCH] proto fw: add helper function for 4x writer --- src/libboard_artiq/src/cxp_proto.rs | 50 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/libboard_artiq/src/cxp_proto.rs b/src/libboard_artiq/src/cxp_proto.rs index e3d8ae5..d8409b3 100644 --- a/src/libboard_artiq/src/cxp_proto.rs +++ b/src/libboard_artiq/src/cxp_proto.rs @@ -266,16 +266,48 @@ pub fn receive(channel: usize) -> Result, Error> { } trait CxpWrite { + fn write_all_4x(&mut self, buf: &[u8]) -> Result<(), Error>; + fn write_4x_u8(&mut self, value: u8) -> Result<(), Error>; + fn write_4x_u16(&mut self, value: u16) -> Result<(), Error>; + + fn write_4x_u32(&mut self, value: u32) -> Result<(), Error>; + + fn write_4x_u64(&mut self, value: u64) -> Result<(), Error>; + fn write_u32(&mut self, value: u32) -> Result<(), Error>; } impl CxpWrite for Cursor { - fn write_4x_u8(&mut self, value: u8) -> Result<(), Error> { - self.write_all(&[value; 4])?; + fn write_all_4x(&mut self, buf: &[u8]) -> Result<(), Error> { + for byte in buf { + self.write_all(&[*byte; 4])?; + } Ok(()) } + fn write_4x_u8(&mut self, value: u8) -> Result<(), Error> { + self.write_all_4x(&[value]) + } + + fn write_4x_u16(&mut self, value: u16) -> Result<(), Error> { + let mut bytes = [0; 2]; + NetworkEndian::write_u16(&mut bytes, value); + self.write_all_4x(&bytes) + } + + fn write_4x_u32(&mut self, value: u32) -> Result<(), Error> { + let mut bytes = [0; 4]; + NetworkEndian::write_u32(&mut bytes, value); + self.write_all_4x(&bytes) + } + + fn write_4x_u64(&mut self, value: u64) -> Result<(), Error> { + let mut bytes = [0; 6]; + NetworkEndian::write_u64(&mut bytes, value); + self.write_all_4x(&bytes) + } + fn write_u32(&mut self, value: u32) -> Result<(), Error> { let mut bytes = [0; 4]; NetworkEndian::write_u32(&mut bytes, value); @@ -392,21 +424,11 @@ impl UpConnPacket { timestamp, data, } => { - let id_bytes = conn_id.to_be_bytes(); - let length_bytes = length.to_be_bytes(); - // event packet header writer.write_4x_u8(0x07)?; - - writer.write_4x_u8(id_bytes[0])?; - writer.write_4x_u8(id_bytes[1])?; - writer.write_4x_u8(id_bytes[2])?; - writer.write_4x_u8(id_bytes[3])?; - + writer.write_4x_u32(conn_id)?; writer.write_4x_u8(packet_tag)?; - - writer.write_4x_u8(length_bytes[0])?; - writer.write_4x_u8(length_bytes[1])?; + writer.write_4x_u16(length)?; // event message let ev_size = event_size.to_be_bytes();