fix: update domo_proto according to spec

This commit is contained in:
Strix 2023-11-08 00:05:30 +01:00
parent 8b18423103
commit 7a3cde1299
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
5 changed files with 50 additions and 39 deletions

View file

@ -3,15 +3,26 @@ use std::io;
#[derive(Debug, PartialOrd, PartialEq)] #[derive(Debug, PartialOrd, PartialEq)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub enum NodeManagementError { pub enum NodeManagementError {
net_duplicate_packet = 0x00, /// recent packet was broken; this packet's data did not arrive properly.
net_broken_packet = 0x01, net_broken_packet = 0x00,
net_invalid_packet = 0x02, /// the packet sent is not valid; this packet's data does not make sense.
net_addr_in_use = 0x03, net_invalid_packet = 0x01,
net_dest_unreachable = 0x04, /// 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, //// the property specified is invalid.
node_invalid_property = 0x20,
errs_not_delivered = 0x20, /// the request could not be completed.
node_failed_request = 0x21,
} }
impl Into<u8> for NodeManagementError { impl Into<u8> for NodeManagementError {
@ -24,13 +35,14 @@ impl TryFrom<u8> for NodeManagementError {
type Error = io::Error; type Error = io::Error;
fn try_from(value: u8) -> io::Result<Self> { fn try_from(value: u8) -> io::Result<Self> {
match value { match value {
0x00 => Ok(NodeManagementError::net_duplicate_packet), 0x00 => Ok(NodeManagementError::net_broken_packet),
0x01 => Ok(NodeManagementError::net_broken_packet), 0x01 => Ok(NodeManagementError::net_invalid_packet),
0x02 => Ok(NodeManagementError::net_invalid_packet), 0x02 => Ok(NodeManagementError::net_dest_unreachable),
0x03 => Ok(NodeManagementError::net_addr_in_use),
0x04 => Ok(NodeManagementError::net_dest_unreachable), 0x10 => Ok(NodeManagementError::mgmt_duplicate_packet),
0x10 => Ok(NodeManagementError::errc_not_registered), 0x11 => Ok(NodeManagementError::mgmt_addr_in_use),
0x20 => Ok(NodeManagementError::errs_not_delivered), 0x12 => Ok(NodeManagementError::mgmt_not_registered),
0x13 => Ok(NodeManagementError::mgmt_not_delivered),
_ => Err(io::Error::new(io::ErrorKind::InvalidInput, "non supported error")) _ => Err(io::Error::new(io::ErrorKind::InvalidInput, "non supported error"))
} }
} }

View file

@ -56,7 +56,9 @@ impl TryFrom<RawPacket> for NodeManagementCommand {
Err(io::Error::new(io::ErrorKind::InvalidInput, r)) Err(io::Error::new(io::ErrorKind::InvalidInput, r))
}; };
match raw_packet.command { match raw_packet.command {
// ping
0x00 => Ok(NodeManagementCommand::Ping), 0x00 => Ok(NodeManagementCommand::Ping),
// register node
0x01 => if raw_packet.data_length == 4 { 0x01 => if raw_packet.data_length == 4 {
Ok(NodeManagementCommand::RegisterNode { Ok(NodeManagementCommand::RegisterNode {
device_id: Identifier::from(raw_packet.data) device_id: Identifier::from(raw_packet.data)
@ -64,7 +66,9 @@ impl TryFrom<RawPacket> for NodeManagementCommand {
} else { } else {
build_invalid_input_err("expected 4 bytes") build_invalid_input_err("expected 4 bytes")
}, },
// remove node
0x02 => Ok(NodeManagementCommand::RemoveNode), 0x02 => Ok(NodeManagementCommand::RemoveNode),
// register property
0x03 => if raw_packet.data_length == 34 { 0x03 => if raw_packet.data_length == 34 {
Ok(NodeManagementCommand::RegisterProperty { Ok(NodeManagementCommand::RegisterProperty {
property_name: String::from_utf8(raw_packet.data[..32].to_vec()) property_name: String::from_utf8(raw_packet.data[..32].to_vec())
@ -75,6 +79,7 @@ impl TryFrom<RawPacket> for NodeManagementCommand {
} else { } else {
build_invalid_input_err("expected 34 bytes") build_invalid_input_err("expected 34 bytes")
}, },
// remove property
0x04 => if raw_packet.data_length == 32 { 0x04 => if raw_packet.data_length == 32 {
Ok(NodeManagementCommand::RemoveProperty { Ok(NodeManagementCommand::RemoveProperty {
property_name: String::from_utf8(raw_packet.data.to_vec()) property_name: String::from_utf8(raw_packet.data.to_vec())
@ -83,16 +88,14 @@ impl TryFrom<RawPacket> for NodeManagementCommand {
} else { } else {
build_invalid_input_err("expected 32 bytes") 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 { Ok(NodeManagementCommand::Error {
error_code: raw_packet.data[0], error_code: raw_packet.data[0],
metadata: { metadata: raw_packet.data[1..].to_vec(),
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()
},
}) })
} else { } 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")) _ => Err(io::Error::new(io::ErrorKind::InvalidInput, "command group is unsupported"))
} }

View file

@ -60,6 +60,7 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
Err(io::Error::new(io::ErrorKind::InvalidInput, r)) Err(io::Error::new(io::ErrorKind::InvalidInput, r))
}; };
match raw_packet.command { match raw_packet.command {
// get property
0x10 => if raw_packet.data_length == 33 { 0x10 => if raw_packet.data_length == 33 {
Ok(PropertyControlCommand::Get { Ok(PropertyControlCommand::Get {
property_name: String::from_utf8(raw_packet.data[..0x34].to_vec()) property_name: String::from_utf8(raw_packet.data[..0x34].to_vec())
@ -69,6 +70,7 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
} else { } else {
build_invalid_input_err("expected 33 bytes") build_invalid_input_err("expected 33 bytes")
}, },
// set property
0x11 => { 0x11 => {
if raw_packet.data_length == 33 { if raw_packet.data_length == 33 {
Ok(PropertyControlCommand::Set { Ok(PropertyControlCommand::Set {
@ -80,6 +82,7 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
build_invalid_input_err("expected 33 bytes") build_invalid_input_err("expected 33 bytes")
} }
} }
// reset property
0x12 => { 0x12 => {
if raw_packet.data_length == 33 { if raw_packet.data_length == 33 {
Ok(PropertyControlCommand::Reset { Ok(PropertyControlCommand::Reset {
@ -90,6 +93,7 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
build_invalid_input_err("expected 33 bytes") build_invalid_input_err("expected 33 bytes")
} }
} }
// subscribe to property
0x1A => { 0x1A => {
if raw_packet.data_length == 36 { if raw_packet.data_length == 36 {
Ok(PropertyControlCommand::Subscribe { Ok(PropertyControlCommand::Subscribe {
@ -101,6 +105,7 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
build_invalid_input_err("expected 36 bytes") build_invalid_input_err("expected 36 bytes")
} }
} }
// unsubscribe from property
0x1B => { 0x1B => {
if raw_packet.data_length == 36 { if raw_packet.data_length == 36 {
Ok(PropertyControlCommand::Unsubscribe { Ok(PropertyControlCommand::Unsubscribe {

View file

@ -3,7 +3,6 @@ use std::io;
use crate::{ use crate::{
identifier::Identifier, identifier::Identifier,
packet::{packet_data::PacketData, raw_packet::RawPacket, Packet, ToPacket}, packet::{packet_data::PacketData, raw_packet::RawPacket, Packet, ToPacket},
prelude::as_u32_be,
}; };
pub mod vec; pub mod vec;
@ -21,8 +20,6 @@ pub enum RawDataTransmission {
Data { Data {
/// The number of the segment in the sequence of segments being sent. /// The number of the segment in the sequence of segments being sent.
sequence_number: u32, sequence_number: u32,
/// Segment size in bytes
size: u32,
/// A segment of the data being sent /// A segment of the data being sent
data: Vec<u8>, data: Vec<u8>,
}, },
@ -51,24 +48,19 @@ impl TryFrom<RawPacket> for RawDataTransmission {
type Error = io::Error; type Error = io::Error;
fn try_from(raw_packet: RawPacket) -> io::Result<Self> { fn try_from(raw_packet: RawPacket) -> io::Result<Self> {
let build_invalid_input_err = |r: &str| Err(io::Error::new(io::ErrorKind::InvalidInput, r)); 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 { match raw_packet.command {
0x20 => { // setup transmission
let size = u64::from_be_bytes([ 0x00 => {
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], 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();
let mime_type = String::from_utf8(data[8..].to_vec()).map_err(|_| {
io::Error::new(io::ErrorKind::InvalidInput, "Invalid mime type string")
})?;
Ok(RawDataTransmission::SetupTransmission { size, mime_type }) Ok(RawDataTransmission::SetupTransmission { size, mime_type })
} }
0x21 => { // data
let sequence_number = as_u32_be(&mut data); 0x01 => {
let size = as_u32_be(&mut data); 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 { Ok(RawDataTransmission::Data {
sequence_number, sequence_number,
size,
data, data,
}) })
} }

View file

@ -9,10 +9,9 @@ impl Into<Vec<u8>> for RawDataTransmission {
v.extend(mime_type.into_bytes()); v.extend(mime_type.into_bytes());
v v
}, },
RawDataTransmission::Data { sequence_number, size, data } => { RawDataTransmission::Data { sequence_number, data } => {
let mut v = vec![]; let mut v = vec![];
v.extend(sequence_number.to_be_bytes().to_vec()); v.extend(sequence_number.to_be_bytes().to_vec());
v.extend(size.to_be_bytes().to_vec());
v.extend(data); v.extend(data);
v v
} }