1
0
Fork 0

proto fw: refactor crc & add downconn packet

This commit is contained in:
morgan 2024-10-04 10:19:15 +08:00
parent 8467f2d4f0
commit 25f29bc009
1 changed files with 49 additions and 45 deletions

View File

@ -41,9 +41,51 @@ impl From<IoError> for Error {
} }
} }
#[derive(Debug)]
pub enum DownConnPacket {
CtrlReply {
tag: Option<u8>,
length: u32,
data: [u8; DATA_MAXSIZE],
},
CtrlDelay {
tag: Option<u8>,
length: u32,
time: [u8; DATA_MAXSIZE],
},
CtrlAck {
tag: Option<u8>,
},
}
pub fn capture_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result<DownConnPacket, Error> {
let mut tag: Option<u8> = None;
if with_tag {
tag = Some((read_word(reader)? & 0xFF) as u8);
}
let ackcode: u8 = (read_word(reader)? & 0xFF) as u8;
match ackcode {
0x00 => {
let length = read_word(reader)?;
let mut data: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE];
reader.read(&mut data[0..length as usize])?;
return Ok(DownConnPacket::CtrlReply { tag, length, data });
}
0x01 => return Ok(DownConnPacket::CtrlAck { tag }),
0x04 => {
let length = read_word(reader)?;
let mut time: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE];
reader.read(&mut time[0..length as usize])?;
return Ok(DownConnPacket::CtrlDelay { tag, length, time });
}
_ => return Err(Error::CtrlAckError(ackcode)),
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum Packet { pub enum Packet {
// Upconn packet
CtrlRead { CtrlRead {
addr: u32, addr: u32,
length: u8, length: u8,
@ -69,26 +111,6 @@ pub enum Packet {
}, },
TestPacket, TestPacket,
// Downconn packet
CtrlAck {
ackcode: u8,
length: u32,
data: [u8; DATA_MAXSIZE],
},
CtrlAckWithTag {
tag: u8,
ackcode: u8,
length: u32,
data: [u8; DATA_MAXSIZE],
},
CtrlAckNoData {
ackcode: u8,
},
CtrlAckNoDataWithTag {
tag: u8,
ackcode: u8,
},
// DEBUG: Loopback message // DEBUG: Loopback message
CtrlAckLoopback { CtrlAckLoopback {
ackcode: u8, ackcode: u8,
@ -104,27 +126,6 @@ fn read_word(reader: &mut Cursor<&mut [u8]>) -> Result<u32, Error> {
} }
impl Packet { impl Packet {
pub fn read_from(reader: &mut Cursor<&mut [u8]>, packet_type: u8) -> Result<Self, Error> {
match packet_type {
0x03 => {
let ackcode: u8 = (read_word(reader)? & 0xFF) as u8;
let length = read_word(reader)?;
let mut data: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE];
if ackcode == 0x00 || ackcode == 0x04 {
reader.read(&mut data[0..length as usize])?;
};
match ackcode {
0x00 | 0x04 => return Ok(Packet::CtrlAck { ackcode, length, data }),
0x01 => return Ok(Packet::CtrlAckNoData { ackcode }),
_ => return Err(Error::CtrlAckError(ackcode)),
}
}
0x06 => return Err(Error::UnknownPacket(packet_type)),
_ => return Err(Error::UnknownPacket(packet_type)),
}
}
pub fn write_to(&self, writer: &mut Cursor<&mut [u8]>) -> Result<(), Error> { pub fn write_to(&self, writer: &mut Cursor<&mut [u8]>) -> Result<(), Error> {
// CoaXpress use big endian // CoaXpress use big endian
match *self { match *self {
@ -161,7 +162,6 @@ impl Packet {
writer.write(&[0x08; 4])?; writer.write(&[0x08; 4])?;
writer.write(&[packet_tag; 4])?; writer.write(&[packet_tag; 4])?;
} }
// DEBUG: Loopback message // DEBUG: Loopback message
Packet::CtrlAckLoopback { ackcode, length, data } => { Packet::CtrlAckLoopback { ackcode, length, data } => {
writer.write(&[0x03; 4])?; writer.write(&[0x03; 4])?;
@ -175,7 +175,7 @@ impl Packet {
let checksum = (!crc::crc32::checksum_ieee(&writer.get_ref()[4..writer.position()])).swap_bytes(); let checksum = (!crc::crc32::checksum_ieee(&writer.get_ref()[4..writer.position()])).swap_bytes();
writer.write(&checksum.to_be_bytes())?; writer.write(&checksum.to_be_bytes())?;
} }
_ => return Err(Error::UnknownPacket(0)), _ => {}
} }
// Section 9.2.2.2 (CXP-001-2021) // Section 9.2.2.2 (CXP-001-2021)
// Only Control packet need CRC32 appended in the end of the packet // Only Control packet need CRC32 appended in the end of the packet
@ -202,7 +202,11 @@ pub fn receive(channel: usize) -> Result<(), Error> {
let mut reader = Cursor::new(slice::from_raw_parts_mut(ptr as *mut u8, MEM_LEN)); 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_type = (CXP[channel].downconn_packet_type_read)();
let packet = Packet::read_from(&mut reader, packet_type)?; let packet = match packet_type {
0x03 => capture_ctrl_packet(&mut reader, false),
0x06 => capture_ctrl_packet(&mut reader, false),
_ => return Err(Error::UnknownPacket(packet_type)),
};
println!("{:?}", packet); println!("{:?}", packet);
// Section 9.2.2.2 (CXP-001-2021) // Section 9.2.2.2 (CXP-001-2021)