Convert ImprovPackets back to bytes

This commit is contained in:
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,
}
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
let mut checksum: u8 = 0x00;
@@ -136,4 +136,30 @@ impl ImprovPacket {
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;
}
}