From 7a3cde12995c52896ca00aa0449e43676049304f Mon Sep 17 00:00:00 2001 From: Raine Date: Wed, 8 Nov 2023 00:05:30 +0100 Subject: [PATCH] fix: update domo_proto according to spec --- .../src/commands/node_management/errors.rs | 42 ++++++++++++------- .../src/commands/node_management/mod.rs | 15 ++++--- .../src/commands/property_control/mod.rs | 5 +++ .../src/commands/raw_data_transmission/mod.rs | 24 ++++------- .../src/commands/raw_data_transmission/vec.rs | 3 +- 5 files changed, 50 insertions(+), 39 deletions(-) diff --git a/domo_proto/src/commands/node_management/errors.rs b/domo_proto/src/commands/node_management/errors.rs index 9372420..85d1e86 100644 --- a/domo_proto/src/commands/node_management/errors.rs +++ b/domo_proto/src/commands/node_management/errors.rs @@ -3,15 +3,26 @@ use std::io; #[derive(Debug, PartialOrd, PartialEq)] #[allow(non_camel_case_types)] pub enum NodeManagementError { - net_duplicate_packet = 0x00, - net_broken_packet = 0x01, - net_invalid_packet = 0x02, - net_addr_in_use = 0x03, - net_dest_unreachable = 0x04, + /// recent packet was broken; this packet's data did not arrive properly. + net_broken_packet = 0x00, + /// the packet sent is not valid; this packet's data does not make sense. + net_invalid_packet = 0x01, + /// the destination could not be reached. + net_dest_unreachable = 0x02, + + /// `packet_id` was used recently. + mgmt_duplicate_packet = 0x10, + /// the addr requested is already in use. + mgmt_addr_in_use = 0x11, + /// the src addr is not registered with the master node. + mgmt_not_registered = 0x12, + /// the sent packet could not be delivered. + mgmt_not_delivered = 0x13, - errc_not_registered = 0x10, - - errs_not_delivered = 0x20, + //// the property specified is invalid. + node_invalid_property = 0x20, + /// the request could not be completed. + node_failed_request = 0x21, } impl Into for NodeManagementError { @@ -24,13 +35,14 @@ impl TryFrom for NodeManagementError { type Error = io::Error; fn try_from(value: u8) -> io::Result { match value { - 0x00 => Ok(NodeManagementError::net_duplicate_packet), - 0x01 => Ok(NodeManagementError::net_broken_packet), - 0x02 => Ok(NodeManagementError::net_invalid_packet), - 0x03 => Ok(NodeManagementError::net_addr_in_use), - 0x04 => Ok(NodeManagementError::net_dest_unreachable), - 0x10 => Ok(NodeManagementError::errc_not_registered), - 0x20 => Ok(NodeManagementError::errs_not_delivered), + 0x00 => Ok(NodeManagementError::net_broken_packet), + 0x01 => Ok(NodeManagementError::net_invalid_packet), + 0x02 => Ok(NodeManagementError::net_dest_unreachable), + + 0x10 => Ok(NodeManagementError::mgmt_duplicate_packet), + 0x11 => Ok(NodeManagementError::mgmt_addr_in_use), + 0x12 => Ok(NodeManagementError::mgmt_not_registered), + 0x13 => Ok(NodeManagementError::mgmt_not_delivered), _ => Err(io::Error::new(io::ErrorKind::InvalidInput, "non supported error")) } } diff --git a/domo_proto/src/commands/node_management/mod.rs b/domo_proto/src/commands/node_management/mod.rs index df6fb06..4790bec 100644 --- a/domo_proto/src/commands/node_management/mod.rs +++ b/domo_proto/src/commands/node_management/mod.rs @@ -56,7 +56,9 @@ impl TryFrom for NodeManagementCommand { Err(io::Error::new(io::ErrorKind::InvalidInput, r)) }; match raw_packet.command { + // ping 0x00 => Ok(NodeManagementCommand::Ping), + // register node 0x01 => if raw_packet.data_length == 4 { Ok(NodeManagementCommand::RegisterNode { device_id: Identifier::from(raw_packet.data) @@ -64,7 +66,9 @@ impl TryFrom for NodeManagementCommand { } else { build_invalid_input_err("expected 4 bytes") }, + // remove node 0x02 => Ok(NodeManagementCommand::RemoveNode), + // register property 0x03 => if raw_packet.data_length == 34 { Ok(NodeManagementCommand::RegisterProperty { property_name: String::from_utf8(raw_packet.data[..32].to_vec()) @@ -75,6 +79,7 @@ impl TryFrom for NodeManagementCommand { } else { build_invalid_input_err("expected 34 bytes") }, + // remove property 0x04 => if raw_packet.data_length == 32 { Ok(NodeManagementCommand::RemoveProperty { property_name: String::from_utf8(raw_packet.data.to_vec()) @@ -83,16 +88,14 @@ impl TryFrom for NodeManagementCommand { } else { build_invalid_input_err("expected 32 bytes") }, - 0x0E => if raw_packet.data_length == 3 { + // error + 0x0E => if raw_packet.data_length >= 1 { Ok(NodeManagementCommand::Error { error_code: raw_packet.data[0], - metadata: { - let metadata_length = u16::from_be_bytes([raw_packet.data[1], raw_packet.data[2]]) as usize; - raw_packet.data[3..metadata_length].to_vec() - }, + metadata: raw_packet.data[1..].to_vec(), }) } else { - build_invalid_input_err("expected 3 bytes") + build_invalid_input_err("expected at least 3 bytes") }, _ => Err(io::Error::new(io::ErrorKind::InvalidInput, "command group is unsupported")) } diff --git a/domo_proto/src/commands/property_control/mod.rs b/domo_proto/src/commands/property_control/mod.rs index f8e0aae..186d40b 100644 --- a/domo_proto/src/commands/property_control/mod.rs +++ b/domo_proto/src/commands/property_control/mod.rs @@ -60,6 +60,7 @@ impl TryFrom for PropertyControlCommand { Err(io::Error::new(io::ErrorKind::InvalidInput, r)) }; match raw_packet.command { + // get property 0x10 => if raw_packet.data_length == 33 { Ok(PropertyControlCommand::Get { property_name: String::from_utf8(raw_packet.data[..0x34].to_vec()) @@ -69,6 +70,7 @@ impl TryFrom for PropertyControlCommand { } else { build_invalid_input_err("expected 33 bytes") }, + // set property 0x11 => { if raw_packet.data_length == 33 { Ok(PropertyControlCommand::Set { @@ -80,6 +82,7 @@ impl TryFrom for PropertyControlCommand { build_invalid_input_err("expected 33 bytes") } } + // reset property 0x12 => { if raw_packet.data_length == 33 { Ok(PropertyControlCommand::Reset { @@ -90,6 +93,7 @@ impl TryFrom for PropertyControlCommand { build_invalid_input_err("expected 33 bytes") } } + // subscribe to property 0x1A => { if raw_packet.data_length == 36 { Ok(PropertyControlCommand::Subscribe { @@ -101,6 +105,7 @@ impl TryFrom for PropertyControlCommand { build_invalid_input_err("expected 36 bytes") } } + // unsubscribe from property 0x1B => { if raw_packet.data_length == 36 { Ok(PropertyControlCommand::Unsubscribe { diff --git a/domo_proto/src/commands/raw_data_transmission/mod.rs b/domo_proto/src/commands/raw_data_transmission/mod.rs index f58afe0..22c65f8 100644 --- a/domo_proto/src/commands/raw_data_transmission/mod.rs +++ b/domo_proto/src/commands/raw_data_transmission/mod.rs @@ -3,7 +3,6 @@ use std::io; use crate::{ identifier::Identifier, packet::{packet_data::PacketData, raw_packet::RawPacket, Packet, ToPacket}, - prelude::as_u32_be, }; pub mod vec; @@ -21,8 +20,6 @@ pub enum RawDataTransmission { Data { /// The number of the segment in the sequence of segments being sent. sequence_number: u32, - /// Segment size in bytes - size: u32, /// A segment of the data being sent data: Vec, }, @@ -51,24 +48,19 @@ impl TryFrom for RawDataTransmission { type Error = io::Error; fn try_from(raw_packet: RawPacket) -> io::Result { let build_invalid_input_err = |r: &str| Err(io::Error::new(io::ErrorKind::InvalidInput, r)); - - let mut data = raw_packet.data; match raw_packet.command { - 0x20 => { - let size = u64::from_be_bytes([ - data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], - ]); - let mime_type = String::from_utf8(data[8..].to_vec()).map_err(|_| { - io::Error::new(io::ErrorKind::InvalidInput, "Invalid mime type string") - })?; + // setup transmission + 0x00 => { + let size = u64::from_be_bytes(raw_packet.data[0..8].try_into().unwrap()); + let mime_type = String::from_utf8(raw_packet.data[8..].to_vec()).unwrap(); Ok(RawDataTransmission::SetupTransmission { size, mime_type }) } - 0x21 => { - let sequence_number = as_u32_be(&mut data); - let size = as_u32_be(&mut data); + // data + 0x01 => { + let sequence_number = u32::from_be_bytes(raw_packet.data[0..4].try_into().unwrap()); + let data = raw_packet.data[4..].to_vec(); Ok(RawDataTransmission::Data { sequence_number, - size, data, }) } diff --git a/domo_proto/src/commands/raw_data_transmission/vec.rs b/domo_proto/src/commands/raw_data_transmission/vec.rs index 9b1dacf..64aa5ec 100644 --- a/domo_proto/src/commands/raw_data_transmission/vec.rs +++ b/domo_proto/src/commands/raw_data_transmission/vec.rs @@ -9,10 +9,9 @@ impl Into> for RawDataTransmission { v.extend(mime_type.into_bytes()); v }, - RawDataTransmission::Data { sequence_number, size, data } => { + RawDataTransmission::Data { sequence_number, data } => { let mut v = vec![]; v.extend(sequence_number.to_be_bytes().to_vec()); - v.extend(size.to_be_bytes().to_vec()); v.extend(data); v }