Convert ImprovPackets back to bytes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user