fix: up to spec

This commit is contained in:
Strix 2023-10-15 18:06:31 +02:00
parent 432c761112
commit 04216c8d7a
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
3 changed files with 42 additions and 19 deletions

View file

@ -1,3 +0,0 @@
[node]
name = "domo-main"
role = "master"

View file

@ -3,8 +3,11 @@ use crate::prelude::as_u64_be;
#[derive(Debug, Eq, PartialEq)]
pub enum DataType {
// Basic types
// Meta
Nothing,
Array(Vec<DataType>),
// Basic types
Switch(bool),
Slider(u8),
Text(String),
@ -22,10 +25,14 @@ pub enum DataType {
impl DataType {
pub fn get_data_size(data_type: u8) -> usize {
match data_type {
0x01 => 1,
0x02 => 1,
0x03 => 256,
0x10 => 3,
0x00 => 0,
0x01 => {
2
},
0x10 => 1,
0x11 => 1,
0x12 => 256,
0x20 => 3,
0x90 => 8,
0xF0 => 4,
_ => 0,
@ -63,7 +70,7 @@ impl DataType {
macro_rules! impl_data_type {
($v: ident, $len: literal, $($t:tt)*) => {
if $v.len() >= $len {
if $v.len() - 1 == $len {
$($t)*
} else {
DataType::Nothing
@ -79,9 +86,15 @@ impl From<Vec<u8>> for DataType {
return DataType::Nothing;
};
match value[0] {
0x01 => impl_data_type!(value, 1, DataType::Switch(value[1] != 0)),
0x02 => impl_data_type!(value, 1, DataType::Slider(value[1])),
0x03 => impl_data_type!(
0x01 => {
if value.len() - 1 < 2 { return DataType::Nothing }
let size = ((value[1] as u16) << 8) | value[2] as u16;
if value.len() - 1 < size as usize { return DataType::Nothing }
DataType::Array(DataType::get_data(value[3..(size as usize)].to_vec()))
},
0x10 => impl_data_type!(value, 1, DataType::Switch(value[1] != 0)),
0x11 => impl_data_type!(value, 1, DataType::Slider(value[1])),
0x12 => impl_data_type!(
value,
256,
if let Ok(s) = String::from_utf8(value[1..257].to_vec()) {
@ -90,7 +103,7 @@ impl From<Vec<u8>> for DataType {
DataType::Nothing
}
),
0x10 => impl_data_type!(value, 3, DataType::RGB(value[1], value[2], value[3])),
0x20 => impl_data_type!(value, 3, DataType::RGB(value[1], value[2], value[3])),
0x90 => impl_data_type!(value, 8, DataType::Seconds(as_u64_be(&value[1..9]))),
0xF0 => impl_data_type!(value, 4, DataType::NodeRef(Identifier::from(&value[1..5]))),
_ => DataType::Nothing,
@ -102,6 +115,13 @@ impl Into<Vec<u8>> for DataType {
fn into(self) -> Vec<u8> {
match self {
DataType::Nothing => vec![0x00],
DataType::Array(vec) => {
let mut res = vec![];
for item in vec {
res.extend(Into::<Vec<u8>>::into(item).iter());
}
res
},
DataType::Switch(b) => vec![0x01, b as u8],
DataType::Slider(v) => vec![0x02, v],
DataType::Text(s) => {

View file

@ -8,10 +8,17 @@ use packet_data::PacketData;
use std::convert::TryFrom;
use std::fmt::Debug;
/// Size of the packet header.
pub const PACKET_HEADER_SIZE: usize = 20;
/// Packet size with CRC32.
pub const FULL_PACKET_SIZE: usize = 65559;
/// Packet size without CRC32.
pub const BASE_PACKET_SIZE: usize = 65555;
#[derive(Debug)]
pub struct Packet {
pub src: Identifier,
pub dest: Identifier,
pub src: Identifier,
pub packet_id: Identifier,
pub reply_to: Identifier,
pub command: u8,
@ -19,13 +26,12 @@ pub struct Packet {
}
impl Packet {
/// Build packet **without** CRC32
pub fn build_base_packet(&self) -> Vec<u8> {
match self {
Packet {
src,
dest,
src,
packet_id,
reply_to,
command,
@ -34,8 +40,8 @@ impl Packet {
let mut buf = Vec::new();
let data_length = data.len();
buf.push(0x01);
buf.extend_from_slice(&src.bytes);
buf.extend_from_slice(&dest.bytes);
buf.extend_from_slice(&src.bytes);
buf.extend_from_slice(&packet_id.bytes);
buf.extend_from_slice(&reply_to.bytes);
buf.push(command.clone());
@ -73,8 +79,8 @@ impl TryFrom<Vec<u8>> for Packet {
let data_length = (header[0x12] << 8) | header[0x13];
let crc32 = prelude::as_u32_be(&value[(0x14 + data_length as usize)..]);
let packet = Packet {
src: Identifier::from(&value[0x01..0x05]),
dest: Identifier::from(&value[0x05..0x09]),
dest: Identifier::from(&value[0x01..0x05]),
src: Identifier::from(&value[0x05..0x09]),
packet_id: Identifier::from(&value[0x09..0x0D]),
reply_to: Identifier::from(&value[0x0D..0x11]),
command: value[0x11],