1
0
Fork 0

proto fw: refactor DownConnPacket

This commit is contained in:
morgan 2024-10-04 16:19:45 +08:00
parent 3642d81612
commit 485739404f
1 changed files with 45 additions and 39 deletions

View File

@ -85,7 +85,16 @@ pub enum DownConnPacket {
},
}
fn capture_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result<DownConnPacket, Error> {
impl DownConnPacket {
pub fn read_from(reader: &mut Cursor<&mut [u8]>, packet_type: u8) -> Result<Self, Error> {
match packet_type {
0x03 => DownConnPacket::get_ctrl_packet(reader, false),
0x06 => DownConnPacket::get_ctrl_packet(reader, true),
_ => Err(Error::UnknownPacket(packet_type)),
}
}
fn get_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result<Self, Error> {
let mut tag: Option<u8> = None;
if with_tag {
tag = Some(reader.read_4x_u8()?);
@ -125,6 +134,7 @@ fn capture_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result
}
_ => return Err(Error::CtrlAckError(ackcode)),
}
}
}
pub fn receive(channel: usize) -> Result<(), Error> {
@ -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 {