From 9865fd21ceaca2b7222f0ef0811364b02484210f Mon Sep 17 00:00:00 2001 From: Raine Date: Sun, 15 Oct 2023 18:06:29 +0200 Subject: [PATCH] feat: bugs and feats (ambiguous) --- domo_proto/src/commands/mod.rs | 2 - domo_proto/src/commands/node_management.rs | 63 ++++++++++------------ domo_proto/src/packet/identifier.rs | 10 +++- domo_proto/src/packet/mod.rs | 49 ++++++----------- domo_proto/src/packet/packet_data.rs | 6 +-- 5 files changed, 53 insertions(+), 77 deletions(-) diff --git a/domo_proto/src/commands/mod.rs b/domo_proto/src/commands/mod.rs index 0ab4552..8d3086b 100644 --- a/domo_proto/src/commands/mod.rs +++ b/domo_proto/src/commands/mod.rs @@ -1,4 +1,2 @@ -use crate::packet::Packet; - pub mod node_management; pub mod property_control; diff --git a/domo_proto/src/commands/node_management.rs b/domo_proto/src/commands/node_management.rs index 1436d04..1187ac7 100644 --- a/domo_proto/src/commands/node_management.rs +++ b/domo_proto/src/commands/node_management.rs @@ -35,40 +35,31 @@ impl NodeManagement { impl Into for Packet { fn into(self) -> NodeManagement { - match self { - Packet::V1 { - src, - dest, - packet_id, - reply_to, - command, - data, - } => 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"), + match self.command { + 0x00 => NodeManagement::Ping, + 0x01 => NodeManagement::RegisterNode { device_id: self.src }, + 0x02 => NodeManagement::RemoveNode, + 0x03 => NodeManagement::RegisterProperty { + property_name: String::from_utf8(self.data.data[..32].to_vec()).unwrap(), + data_type: self.data.data[33], + read_only: self.data.data[34] != 0, }, + 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 for NodeManagement { } impl ToPacket for NodeManagement { - fn to_v1_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet { - Packet::V1 { + fn to_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet { + Packet { src, dest, packet_id, reply_to, command: self.get_command(), - data: self.into() + data: self.into(), } } } diff --git a/domo_proto/src/packet/identifier.rs b/domo_proto/src/packet/identifier.rs index acf5da4..8268758 100644 --- a/domo_proto/src/packet/identifier.rs +++ b/domo_proto/src/packet/identifier.rs @@ -12,6 +12,12 @@ impl Identifier { } } +impl Default for Identifier { + fn default() -> Self { + Identifier::from(0x00000000) + } +} + impl Display for Identifier { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( @@ -35,9 +41,9 @@ impl LowerHex for Identifier { impl From<&[u8]> for Identifier { fn from(value: &[u8]) -> Self { let mut bytes = [0_u8; 4]; - bytes[..value.len()].copy_from_slice(value); + bytes[..4].copy_from_slice(value[..4].as_ref()); Identifier { - bytes: [bytes[0], bytes[1], bytes[2], bytes[3]], + bytes } } } diff --git a/domo_proto/src/packet/mod.rs b/domo_proto/src/packet/mod.rs index 1207ceb..eb6dcfe 100644 --- a/domo_proto/src/packet/mod.rs +++ b/domo_proto/src/packet/mod.rs @@ -7,43 +7,23 @@ use identifier::Identifier; use packet_data::PacketData; use std::convert::TryFrom; use std::fmt::Debug; -use std::io::Read; #[derive(Debug)] -pub enum Packet { - V1 { - src: Identifier, - dest: Identifier, - packet_id: Identifier, - reply_to: Identifier, - command: u8, - data: PacketData, - }, +pub struct Packet { + pub src: Identifier, + pub dest: Identifier, + pub packet_id: Identifier, + pub reply_to: Identifier, + pub command: u8, + pub data: PacketData, } 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 pub fn build_base_packet(&self) -> Vec { match self { - Packet::V1 { + Packet { src, dest, packet_id, @@ -61,15 +41,20 @@ impl Packet { buf.push(command.clone()); buf.push((data_length >> 8) as u8); buf.push((data_length & 0xFF) as u8); - buf.extend(data.get_data()); + buf.extend(&data.data); 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 { match self { - Packet::V1 { .. } => { + Packet { .. } => { let mut buf = self.build_base_packet(); buf.extend_from_slice(crc32fast::hash(&buf.as_slice()).to_be_bytes().as_ref()); buf @@ -87,7 +72,7 @@ impl TryFrom> for Packet { let header: Vec = value[..0x15].iter().map(|v| u16::from(*v)).collect(); let data_length = (header[0x12] << 8) | header[0x13]; 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]), dest: Identifier::from(&value[0x05..0x09]), packet_id: Identifier::from(&value[0x09..0x0D]), @@ -113,5 +98,5 @@ impl Into> for Packet { } 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; } \ No newline at end of file diff --git a/domo_proto/src/packet/packet_data.rs b/domo_proto/src/packet/packet_data.rs index 0e4d008..d7ffd0a 100644 --- a/domo_proto/src/packet/packet_data.rs +++ b/domo_proto/src/packet/packet_data.rs @@ -1,6 +1,6 @@ #[derive(Debug, Clone)] pub struct PacketData { - data: Vec, + pub data: Vec, } impl PacketData { @@ -11,10 +11,6 @@ impl PacketData { pub fn len(&self) -> usize { self.data.len() } - - pub fn get_data(&self) -> Vec { - self.data.clone() - } } impl Default for PacketData {