Generate packet from struct
This commit is contained in:
parent
1be1defc35
commit
7b7c85f976
@ -104,6 +104,21 @@ pub fn calculate_checksum(data: &[u8]) -> u8 {
|
|||||||
return checksum;
|
return checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait ImprovDataToPacket {
|
||||||
|
const packet_type: PacketType;
|
||||||
|
|
||||||
|
fn to_bytes(&self) -> Vec<u8>;
|
||||||
|
|
||||||
|
fn to_packet(&self) -> ImprovPacket {
|
||||||
|
return ImprovPacket {
|
||||||
|
version: IMPROV_VERSION,
|
||||||
|
r#type: Self::packet_type,
|
||||||
|
data: self.to_bytes().to_vec(),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ImprovPacket {
|
pub struct ImprovPacket {
|
||||||
pub version: u8,
|
pub version: u8,
|
||||||
pub r#type: PacketType,
|
pub r#type: PacketType,
|
||||||
@ -163,3 +178,17 @@ impl ImprovPacket {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct RequestCurrentStateCommand {
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImprovDataToPacket for RequestCurrentStateCommand {
|
||||||
|
const packet_type: PacketType = PacketType::RPCCommand;
|
||||||
|
|
||||||
|
fn to_bytes(self: &Self) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
out.push(RPCCommand::RequestCurrentState as u8);
|
||||||
|
out.push(0x00); // rpc command payload length
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
12
src/main.rs
12
src/main.rs
@ -15,7 +15,9 @@ use improv_setup::improv::{
|
|||||||
RPCCommand,
|
RPCCommand,
|
||||||
CurrentState,
|
CurrentState,
|
||||||
calculate_checksum,
|
calculate_checksum,
|
||||||
|
ImprovDataToPacket,
|
||||||
ImprovPacket,
|
ImprovPacket,
|
||||||
|
RequestCurrentStateCommand,
|
||||||
};
|
};
|
||||||
use log::{
|
use log::{
|
||||||
debug,
|
debug,
|
||||||
@ -117,15 +119,7 @@ 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 data: Vec<u8> = Vec::new();
|
let request_current_state_packet = (RequestCurrentStateCommand {}).to_packet();
|
||||||
data.push(RPCCommand::RequestCurrentState as u8); // command
|
|
||||||
data.push(0x00); // command data length
|
|
||||||
|
|
||||||
let request_current_state_packet = ImprovPacket {
|
|
||||||
version: IMPROV_VERSION,
|
|
||||||
r#type: PacketType::RPCCommand,
|
|
||||||
data: data,
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("{}", hex::encode(&request_current_state_packet.to_bytes()));
|
println!("{}", hex::encode(&request_current_state_packet.to_bytes()));
|
||||||
println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes()));
|
println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes()));
|
||||||
|
Loading…
Reference in New Issue
Block a user