diff --git a/src/libboard_artiq/src/cxp_proto.rs b/src/libboard_artiq/src/cxp_proto.rs index de1e39f..604e5ca 100644 --- a/src/libboard_artiq/src/cxp_proto.rs +++ b/src/libboard_artiq/src/cxp_proto.rs @@ -85,45 +85,55 @@ pub enum DownConnPacket { }, } -fn capture_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result { - let mut tag: Option = None; - if with_tag { - tag = Some(reader.read_4x_u8()?); +impl DownConnPacket { + pub fn read_from(reader: &mut Cursor<&mut [u8]>, packet_type: u8) -> Result { + match packet_type { + 0x03 => DownConnPacket::get_ctrl_packet(reader, false), + 0x06 => DownConnPacket::get_ctrl_packet(reader, true), + _ => Err(Error::UnknownPacket(packet_type)), + } } - let ackcode = reader.read_4x_u8()?; - - match ackcode { - 0x00 => { - let length = reader.read_u32()?; - let mut data: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; - reader.read(&mut data[0..length as usize])?; - - let checksum = get_cxp_crc(&reader.get_ref()[0..reader.position()]); - let recv_checksum = reader.read_u32()?; - println!("calculated checksum = {:#010X}", checksum); - println!("read checksum = {:#010X}", recv_checksum); - if recv_checksum != checksum { - return Err(Error::CorruptedPacket); - } - return Ok(DownConnPacket::CtrlReply { tag, length, data }); + fn get_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result { + let mut tag: Option = None; + if with_tag { + tag = Some(reader.read_4x_u8()?); } - 0x01 => return Ok(DownConnPacket::CtrlAck { tag }), - 0x04 => { - let length = reader.read_u32()?; - let mut time: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; - reader.read(&mut time[0..length as usize])?; - let checksum = get_cxp_crc(&reader.get_ref()[0..reader.position()]); - let recv_checksum = reader.read_u32()?; - println!("calculated checksum = {:#010X}", checksum); - println!("read checksum = {:#010X}", recv_checksum); - if recv_checksum != checksum { - return Err(Error::CorruptedPacket); + let ackcode = reader.read_4x_u8()?; + + match ackcode { + 0x00 => { + let length = reader.read_u32()?; + let mut data: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; + reader.read(&mut data[0..length as usize])?; + + let checksum = get_cxp_crc(&reader.get_ref()[0..reader.position()]); + let recv_checksum = reader.read_u32()?; + println!("calculated checksum = {:#010X}", checksum); + println!("read checksum = {:#010X}", recv_checksum); + if recv_checksum != checksum { + return Err(Error::CorruptedPacket); + } + return Ok(DownConnPacket::CtrlReply { tag, length, data }); } - return Ok(DownConnPacket::CtrlDelay { tag, length, time }); + 0x01 => return Ok(DownConnPacket::CtrlAck { tag }), + 0x04 => { + let length = reader.read_u32()?; + let mut time: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE]; + reader.read(&mut time[0..length as usize])?; + + let checksum = get_cxp_crc(&reader.get_ref()[0..reader.position()]); + let recv_checksum = reader.read_u32()?; + println!("calculated checksum = {:#010X}", checksum); + println!("read checksum = {:#010X}", recv_checksum); + if recv_checksum != checksum { + return Err(Error::CorruptedPacket); + } + return Ok(DownConnPacket::CtrlDelay { tag, length, time }); + } + _ => return Err(Error::CtrlAckError(ackcode)), } - _ => return Err(Error::CtrlAckError(ackcode)), } } @@ -133,11 +143,8 @@ pub fn receive(channel: usize) -> Result<(), Error> { let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); let packet_type = (CXP[channel].downconn_packet_type_read)(); - let packet = match packet_type { - 0x03 => capture_ctrl_packet(&mut reader, false), - 0x06 => capture_ctrl_packet(&mut reader, true), - _ => return Err(Error::UnknownPacket(packet_type)), - }; + let packet = DownConnPacket::read_from(&mut reader, packet_type); + println!("{:?}", packet); } Ok(()) @@ -190,7 +197,6 @@ pub enum UpConnPacket { impl UpConnPacket { pub fn write_to(&self, writer: &mut Cursor<&mut [u8]>) -> Result<(), Error> { - // CoaXpress use big endian match *self { UpConnPacket::CtrlRead { tag, addr, length } => { match tag {