Rename ImprovPacket to RawPacket
This commit is contained in:
parent
c61e437ef8
commit
e63d279229
@ -111,8 +111,8 @@ pub trait ImprovDataPacketType {
|
|||||||
pub trait ImprovDataToPacket: ImprovDataPacketType {
|
pub trait ImprovDataToPacket: ImprovDataPacketType {
|
||||||
fn to_bytes(&self) -> Vec<u8>;
|
fn to_bytes(&self) -> Vec<u8>;
|
||||||
|
|
||||||
fn to_packet(&self) -> ImprovPacket {
|
fn to_raw_packet(&self) -> RawPacket {
|
||||||
return ImprovPacket {
|
return RawPacket {
|
||||||
version: IMPROV_VERSION,
|
version: IMPROV_VERSION,
|
||||||
r#type: Self::packet_type,
|
r#type: Self::packet_type,
|
||||||
data: self.to_bytes().to_vec(),
|
data: self.to_bytes().to_vec(),
|
||||||
@ -124,16 +124,16 @@ pub trait ImprovDataToPacket: ImprovDataPacketType {
|
|||||||
pub trait ImprovDataFromPacket: ImprovDataPacketType + Sized {
|
pub trait ImprovDataFromPacket: ImprovDataPacketType + Sized {
|
||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
fn try_from_packet(improv_packet: &ImprovPacket) -> Result<Self, Self::Error>;
|
fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ImprovPacket {
|
pub struct RawPacket {
|
||||||
pub version: u8,
|
pub version: u8,
|
||||||
pub r#type: PacketType,
|
pub r#type: PacketType,
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ImprovPacket {
|
impl RawPacket {
|
||||||
pub fn try_from_bytes(bytes: &Vec<u8>) -> Result<Self, &str> {
|
pub fn try_from_bytes(bytes: &Vec<u8>) -> Result<Self, &str> {
|
||||||
if bytes.len() < 11 {
|
if bytes.len() < 11 {
|
||||||
return Err("Packet too small");
|
return Err("Packet too small");
|
||||||
@ -204,13 +204,13 @@ impl ImprovDataToPacket for CurrentStateResponse {
|
|||||||
impl ImprovDataFromPacket for CurrentStateResponse {
|
impl ImprovDataFromPacket for CurrentStateResponse {
|
||||||
type Error = &'static str;
|
type Error = &'static str;
|
||||||
|
|
||||||
fn try_from_packet(improv_packet: &ImprovPacket) -> Result<Self, Self::Error>{
|
fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self, Self::Error>{
|
||||||
if improv_packet.r#type != Self::packet_type {
|
if raw_packet.r#type != Self::packet_type {
|
||||||
return Err("Packet is not CurrentStateResponse");
|
return Err("Packet is not CurrentStateResponse");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(Self {
|
return Ok(Self {
|
||||||
current_state: CurrentState::try_from(&improv_packet.data[0])?,
|
current_state: CurrentState::try_from(&raw_packet.data[0])?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/main.rs
14
src/main.rs
@ -17,7 +17,7 @@ use improv_setup::improv::{
|
|||||||
calculate_checksum,
|
calculate_checksum,
|
||||||
ImprovDataToPacket,
|
ImprovDataToPacket,
|
||||||
ImprovDataFromPacket,
|
ImprovDataFromPacket,
|
||||||
ImprovPacket,
|
RawPacket,
|
||||||
CurrentStateResponse,
|
CurrentStateResponse,
|
||||||
RequestCurrentStateCommand,
|
RequestCurrentStateCommand,
|
||||||
};
|
};
|
||||||
@ -121,7 +121,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 request_current_state_packet = (RequestCurrentStateCommand {}).to_packet();
|
let request_current_state_packet = (RequestCurrentStateCommand {}).to_raw_packet();
|
||||||
|
|
||||||
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()));
|
||||||
@ -141,16 +141,16 @@ async fn main() -> Result<()>{
|
|||||||
println!("{}", improv_packet_offset);
|
println!("{}", improv_packet_offset);
|
||||||
|
|
||||||
let improv_packet_end = improv_packet_offset + 10 + <u8 as Into<usize>>::into(buffer[improv_packet_offset+8]);
|
let improv_packet_end = improv_packet_offset + 10 + <u8 as Into<usize>>::into(buffer[improv_packet_offset+8]);
|
||||||
let improv_packet = ImprovPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap();
|
let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap();
|
||||||
|
|
||||||
// version
|
// version
|
||||||
println!("Version: {}", &improv_packet.version);
|
println!("Version: {}", &raw_packet.version);
|
||||||
|
|
||||||
// type
|
// type
|
||||||
println!("Type: {}", &improv_packet.r#type);
|
println!("Type: {}", &raw_packet.r#type);
|
||||||
|
|
||||||
if improv_packet.r#type == PacketType::CurrentState {
|
if raw_packet.r#type == PacketType::CurrentState {
|
||||||
let current_state_response = CurrentStateResponse::try_from_packet(&improv_packet).unwrap();
|
let current_state_response = CurrentStateResponse::try_from_raw_packet(&raw_packet).unwrap();
|
||||||
|
|
||||||
println!("Current state: {}", ¤t_state_response.current_state);
|
println!("Current state: {}", ¤t_state_response.current_state);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user