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 property_control;

View file

@ -35,40 +35,31 @@ impl NodeManagement {
impl Into<NodeManagement> 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<PacketData> 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(),
}
}
}

View file

@ -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
}
}
}

View file

@ -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<u8> {
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<u8> {
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<Vec<u8>> for Packet {
let header: Vec<u16> = 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<Vec<u8>> 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;
}

View file

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