Convert ImprovPackets back to bytes

This commit is contained in:
clerie 2024-10-30 20:35:31 +01:00
parent 5582ec6cb2
commit 1be1defc35
2 changed files with 35 additions and 15 deletions

View File

@ -92,7 +92,7 @@ pub enum RPCCommand {
RequestCurrentState = 0x02, RequestCurrentState = 0x02,
} }
pub fn calculate_checksum(data: &Vec<u8>) -> u8 { pub fn calculate_checksum(data: &[u8]) -> u8 {
// Pass data as full packet, with header, but without checksum byte // Pass data as full packet, with header, but without checksum byte
let mut checksum: u8 = 0x00; let mut checksum: u8 = 0x00;
@ -136,4 +136,30 @@ impl ImprovPacket {
data: bytes[9..(bytes.len()-1)].to_vec(), data: bytes[9..(bytes.len()-1)].to_vec(),
}); });
} }
pub fn to_bytes(self: &Self) -> Vec<u8> {
let mut out = Vec::new();
for b in IMPROV_HEADER.clone() {
out.push(b);
}
out.push(self.version.clone());
out.push(self.r#type.clone() as u8);
let length: u8 = self.data.len().try_into().unwrap();
out.push(length.clone());
for b in self.data.clone() {
out.push(b);
}
let checksum: u8 = calculate_checksum(&out);
out.push(checksum.clone());
return out;
}
} }

View File

@ -117,28 +117,22 @@ async fn main() -> Result<()>{
.fold(String::new(), |a, b| a + &b + &String::from("\n"))); .fold(String::new(), |a, b| a + &b + &String::from("\n")));
}, },
Commands::Device {path, baud_rate, device_command} => { Commands::Device {path, baud_rate, device_command} => {
let mut rpc_command_request_current_state: Vec<u8> = Vec::new();
rpc_command_request_current_state.extend(IMPROV_HEADER);
rpc_command_request_current_state.push(IMPROV_VERSION);
rpc_command_request_current_state.push(PacketType::RPCCommand as u8);
let mut data: Vec<u8> = Vec::new(); let mut data: Vec<u8> = Vec::new();
data.push(RPCCommand::RequestCurrentState as u8); // command data.push(RPCCommand::RequestCurrentState as u8); // command
data.push(0x00); // command data length data.push(0x00); // command data length
rpc_command_request_current_state.push(data.len().try_into().unwrap()); // length let request_current_state_packet = ImprovPacket {
rpc_command_request_current_state.extend(data.clone()); // data version: IMPROV_VERSION,
r#type: PacketType::RPCCommand,
data: data,
};
let checksum: u8 = calculate_checksum(&rpc_command_request_current_state); println!("{}", hex::encode(&request_current_state_packet.to_bytes()));
println!("{}", hex::encode([checksum.clone()])); println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes()));
rpc_command_request_current_state.push(checksum);
println!("{}", hex::encode(&rpc_command_request_current_state));
println!("{}", to_ascii_debug(&rpc_command_request_current_state));
let mut serial_interface = tokio_serial::new(path, *baud_rate).open().unwrap(); let mut serial_interface = tokio_serial::new(path, *baud_rate).open().unwrap();
serial_interface.write(&rpc_command_request_current_state).unwrap(); serial_interface.write(&request_current_state_packet.to_bytes()).unwrap();
let mut buffer: Vec<u8> = Vec::new(); let mut buffer: Vec<u8> = Vec::new();
serial_interface.read_to_end(&mut buffer); serial_interface.read_to_end(&mut buffer);