feat: bugs and feats (ambiguous)

This commit is contained in:
Strix 2023-10-15 18:06:29 +02:00
parent d4a57edd95
commit 9865fd21ce
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
5 changed files with 53 additions and 77 deletions

View file

@ -1,4 +1,2 @@
use crate::packet::Packet;
pub mod node_management; pub mod node_management;
pub mod property_control; pub mod property_control;

View file

@ -35,40 +35,31 @@ impl NodeManagement {
impl Into<NodeManagement> for Packet { impl Into<NodeManagement> for Packet {
fn into(self) -> NodeManagement { fn into(self) -> NodeManagement {
match self { match self.command {
Packet::V1 { 0x00 => NodeManagement::Ping,
src, 0x01 => NodeManagement::RegisterNode { device_id: self.src },
dest, 0x02 => NodeManagement::RemoveNode,
packet_id, 0x03 => NodeManagement::RegisterProperty {
reply_to, property_name: String::from_utf8(self.data.data[..32].to_vec()).unwrap(),
command, data_type: self.data.data[33],
data, read_only: self.data.data[34] != 0,
} => match command {
0x00 => NodeManagement::Ping,
0x01 => NodeManagement::RegisterNode { device_id: src },
0x02 => NodeManagement::RemoveNode,
0x03 => NodeManagement::RegisterProperty {
property_name: String::from_utf8(data.get_data()[..32].to_vec()).unwrap(),
data_type: data.get_data()[33],
read_only: data.get_data()[34] != 0
},
0x04 => NodeManagement::RemoveProperty {
property_name: String::from_utf8(data.get_data()[..32].to_vec()).unwrap()
},
0x0E => NodeManagement::Error {
error_code: data.get_data()[0],
metadata: {
let mut metadata = [0_u8; 0xFFFF];
let data_slice = data.get_data();
let metadata_length = u16::from_be_bytes([data_slice[1], data_slice[2]]) as usize;
if metadata_length <= 0xFFFF {
metadata[..metadata_length].copy_from_slice(&data_slice[3..(3 + metadata_length)]);
}
metadata
}
},
_ => unreachable!("Invalid command in Packet::V1"),
}, },
0x04 => NodeManagement::RemoveProperty {
property_name: String::from_utf8(self.data.data[..32].to_vec()).unwrap()
},
0x0E => NodeManagement::Error {
error_code: self.data.data[0],
metadata: {
let mut metadata = [0_u8; 0xFFFF];
let data_slice = self.data.data;
let metadata_length = u16::from_be_bytes([data_slice[1], data_slice[2]]) as usize;
if metadata_length <= 0xFFFF {
metadata[..metadata_length].copy_from_slice(&data_slice[3..(3 + metadata_length)]);
}
metadata
},
},
_ => unreachable!("Invalid command in Packet::V1"),
} }
} }
} }
@ -99,14 +90,14 @@ impl Into<PacketData> for NodeManagement {
} }
impl ToPacket for NodeManagement { impl ToPacket for NodeManagement {
fn to_v1_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet { fn to_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet {
Packet::V1 { Packet {
src, src,
dest, dest,
packet_id, packet_id,
reply_to, reply_to,
command: self.get_command(), command: self.get_command(),
data: self.into() data: self.into(),
} }
} }
} }

View file

@ -12,6 +12,12 @@ impl Identifier {
} }
} }
impl Default for Identifier {
fn default() -> Self {
Identifier::from(0x00000000)
}
}
impl Display for Identifier { impl Display for Identifier {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!( write!(
@ -35,9 +41,9 @@ impl LowerHex for Identifier {
impl From<&[u8]> for Identifier { impl From<&[u8]> for Identifier {
fn from(value: &[u8]) -> Self { fn from(value: &[u8]) -> Self {
let mut bytes = [0_u8; 4]; let mut bytes = [0_u8; 4];
bytes[..value.len()].copy_from_slice(value); bytes[..4].copy_from_slice(value[..4].as_ref());
Identifier { Identifier {
bytes: [bytes[0], bytes[1], bytes[2], bytes[3]], bytes
} }
} }
} }

View file

@ -7,43 +7,23 @@ use identifier::Identifier;
use packet_data::PacketData; use packet_data::PacketData;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt::Debug; use std::fmt::Debug;
use std::io::Read;
#[derive(Debug)] #[derive(Debug)]
pub enum Packet { pub struct Packet {
V1 { pub src: Identifier,
src: Identifier, pub dest: Identifier,
dest: Identifier, pub packet_id: Identifier,
packet_id: Identifier, pub reply_to: Identifier,
reply_to: Identifier, pub command: u8,
command: u8, pub data: PacketData,
data: PacketData,
},
} }
impl Packet { impl Packet {
pub fn packet_id(&self) -> Identifier {
match self {
Packet::V1 { packet_id, .. } => packet_id.clone()
}
}
pub fn command(&self) -> u8 {
match self {
Packet::V1 { command, .. } => command.clone()
}
}
pub fn data(&self) -> PacketData {
match self {
Packet::V1 { data, .. } => data.clone()
}
}
/// Build packet **without** CRC32 /// Build packet **without** CRC32
pub fn build_base_packet(&self) -> Vec<u8> { pub fn build_base_packet(&self) -> Vec<u8> {
match self { match self {
Packet::V1 { Packet {
src, src,
dest, dest,
packet_id, packet_id,
@ -61,15 +41,20 @@ impl Packet {
buf.push(command.clone()); buf.push(command.clone());
buf.push((data_length >> 8) as u8); buf.push((data_length >> 8) as u8);
buf.push((data_length & 0xFF) as u8); buf.push((data_length & 0xFF) as u8);
buf.extend(data.get_data()); buf.extend(&data.data);
buf buf
} }
} }
} }
pub fn get_crc32(&self) -> u32 {
let d = self.build_base_packet();
crc32fast::hash(d.as_slice())
}
pub fn build_full_packet(&self) -> Vec<u8> { pub fn build_full_packet(&self) -> Vec<u8> {
match self { match self {
Packet::V1 { .. } => { Packet { .. } => {
let mut buf = self.build_base_packet(); let mut buf = self.build_base_packet();
buf.extend_from_slice(crc32fast::hash(&buf.as_slice()).to_be_bytes().as_ref()); buf.extend_from_slice(crc32fast::hash(&buf.as_slice()).to_be_bytes().as_ref());
buf buf
@ -87,7 +72,7 @@ impl TryFrom<Vec<u8>> for Packet {
let header: Vec<u16> = value[..0x15].iter().map(|v| u16::from(*v)).collect(); let header: Vec<u16> = value[..0x15].iter().map(|v| u16::from(*v)).collect();
let data_length = (header[0x12] << 8) | header[0x13]; let data_length = (header[0x12] << 8) | header[0x13];
let crc32 = prelude::as_u32_be(&value[(0x14 + data_length as usize)..]); let crc32 = prelude::as_u32_be(&value[(0x14 + data_length as usize)..]);
let packet = Packet::V1 { let packet = Packet {
src: Identifier::from(&value[0x01..0x05]), src: Identifier::from(&value[0x01..0x05]),
dest: Identifier::from(&value[0x05..0x09]), dest: Identifier::from(&value[0x05..0x09]),
packet_id: Identifier::from(&value[0x09..0x0D]), packet_id: Identifier::from(&value[0x09..0x0D]),
@ -113,5 +98,5 @@ impl Into<Vec<u8>> for Packet {
} }
pub trait ToPacket { pub trait ToPacket {
fn to_v1_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet; fn to_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet;
} }

View file

@ -1,6 +1,6 @@
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PacketData { pub struct PacketData {
data: Vec<u8>, pub data: Vec<u8>,
} }
impl PacketData { impl PacketData {
@ -11,10 +11,6 @@ impl PacketData {
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.data.len() self.data.len()
} }
pub fn get_data(&self) -> Vec<u8> {
self.data.clone()
}
} }
impl Default for PacketData { impl Default for PacketData {