1
0
Fork 0

proto fw: unify Ctrl packet with/without tags

This commit is contained in:
morgan 2024-10-04 12:45:20 +08:00
parent 6231795418
commit 35b84c768a
1 changed files with 47 additions and 45 deletions

View File

@ -110,21 +110,12 @@ fn capture_ctrl_packet(reader: &mut Cursor<&mut [u8]>, with_tag: bool) -> Result
#[derive(Debug)]
pub enum UpConnPacket {
CtrlRead {
tag: Option<u8>,
addr: u32,
length: u8,
},
CtrlWrite {
addr: u32,
length: u8,
data: [u8; DATA_MAXSIZE],
}, // max register size is 8 bytes
CtrlReadWithTag {
tag: u8,
addr: u32,
length: u8,
},
CtrlWriteWithTag {
tag: u8,
tag: Option<u8>,
addr: u32,
length: u8,
data: [u8; DATA_MAXSIZE],
@ -152,34 +143,47 @@ impl UpConnPacket {
pub fn write_to(&self, writer: &mut Cursor<&mut [u8]>) -> Result<(), Error> {
// CoaXpress use big endian
match *self {
UpConnPacket::CtrlRead { addr, length } => {
writer.write(&[0x02; 4])?;
writer.write(&[0x00, 0x00, 0x00, length])?;
writer.write(&addr.to_be_bytes())?;
}
UpConnPacket::CtrlWrite { addr, length, data } => {
writer.write(&[0x02; 4])?;
writer.write(&[0x01, 0x00, 0x00, length])?;
writer.write(&addr.to_be_bytes())?;
writer.write(&data[0..length as usize])?;
}
UpConnPacket::CtrlReadWithTag { tag, addr, length } => {
UpConnPacket::CtrlRead { tag, addr, length } => {
match tag {
Some(t) => {
writer.write(&[0x05; 4])?;
writer.write(&[tag; 4])?;
writer.write(&[t; 4])?;
}
None => {
writer.write(&[0x02; 4])?;
}
}
writer.write(&[0x00, 0x00, 0x00, length])?;
writer.write(&addr.to_be_bytes())?;
// Section 9.6.2 (CXP-001-2021)
// only bytes after the first 4 are used in calculating the checksum
let checksum = get_cxp_crc(&writer.get_ref()[4..writer.position()]);
writer.write(&checksum.to_be_bytes())?;
}
UpConnPacket::CtrlWriteWithTag {
UpConnPacket::CtrlWrite {
tag,
addr,
length,
data,
} => {
match tag {
Some(t) => {
writer.write(&[0x05; 4])?;
writer.write(&[tag; 4])?;
writer.write(&[t; 4])?;
}
None => {
writer.write(&[0x02; 4])?;
}
}
writer.write(&[0x01, 0x00, 0x00, length])?;
writer.write(&addr.to_be_bytes())?;
writer.write(&data[0..length as usize])?;
// Section 9.6.2 (CXP-001-2021)
// only bytes after the first 4 are used in calculating the checksum
let checksum = get_cxp_crc(&writer.get_ref()[4..writer.position()]);
writer.write(&checksum.to_be_bytes())?;
}
UpConnPacket::EventAck { packet_tag } => {
writer.write(&[0x08; 4])?;
@ -199,17 +203,6 @@ impl UpConnPacket {
}
_ => {}
}
// Section 9.2.2.2 (CXP-001-2021)
// The crc calculation does not include the first 4 bytes of packet_type
match *self {
UpConnPacket::CtrlRead { .. }
| UpConnPacket::CtrlWrite { .. }
| UpConnPacket::CtrlReadWithTag { .. }
| UpConnPacket::CtrlWriteWithTag { .. } => {
writer.write(&get_cxp_crc(&writer.get_ref()[4..writer.position()]).to_be_bytes())?;
}
_ => {}
}
Ok(())
}
}
@ -270,12 +263,18 @@ fn send_test_packet(channel: usize) -> Result<(), Error> {
Ok(())
}
//
// DEBUG: use only
//
//
//
pub fn write_u32(channel: usize, addr: u32, data: u32) -> Result<(), Error> {
let mut data_slice: [u8; DATA_MAXSIZE] = [0; DATA_MAXSIZE];
data_slice[..4].clone_from_slice(&data.to_be_bytes());
send(
channel,
&UpConnPacket::CtrlWrite {
tag: None,
addr,
length: 4,
data: data_slice,
@ -286,7 +285,14 @@ pub fn write_u32(channel: usize, addr: u32, data: u32) -> Result<(), Error> {
}
pub fn read_u32(channel: usize, addr: u32) -> Result<(), Error> {
send(channel, &UpConnPacket::CtrlRead { addr, length: 4 })?;
send(
channel,
&UpConnPacket::CtrlRead {
tag: None,
addr,
length: 4,
},
)?;
Ok(())
}
@ -297,6 +303,7 @@ pub fn write_u64(channel: usize, addr: u32, data: u64) -> Result<(), Error> {
send(
channel,
&UpConnPacket::CtrlWrite {
tag: None,
addr,
length: 8,
data: data_slice,
@ -306,11 +313,6 @@ pub fn write_u64(channel: usize, addr: u32, data: u64) -> Result<(), Error> {
Ok(())
}
//
// DEBUG: use only
//
//
//
pub fn print_packet(pak: &[u8]) {
println!("pak = [");
for i in 0..(pak.len() / 4) {