fix: up to spec
This commit is contained in:
parent
432c761112
commit
04216c8d7a
3 changed files with 42 additions and 19 deletions
|
@ -1,3 +0,0 @@
|
||||||
[node]
|
|
||||||
name = "domo-main"
|
|
||||||
role = "master"
|
|
|
@ -3,8 +3,11 @@ use crate::prelude::as_u64_be;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum DataType {
|
pub enum DataType {
|
||||||
// Basic types
|
// Meta
|
||||||
Nothing,
|
Nothing,
|
||||||
|
Array(Vec<DataType>),
|
||||||
|
|
||||||
|
// Basic types
|
||||||
Switch(bool),
|
Switch(bool),
|
||||||
Slider(u8),
|
Slider(u8),
|
||||||
Text(String),
|
Text(String),
|
||||||
|
@ -22,10 +25,14 @@ pub enum DataType {
|
||||||
impl DataType {
|
impl DataType {
|
||||||
pub fn get_data_size(data_type: u8) -> usize {
|
pub fn get_data_size(data_type: u8) -> usize {
|
||||||
match data_type {
|
match data_type {
|
||||||
0x01 => 1,
|
0x00 => 0,
|
||||||
0x02 => 1,
|
0x01 => {
|
||||||
0x03 => 256,
|
2
|
||||||
0x10 => 3,
|
},
|
||||||
|
0x10 => 1,
|
||||||
|
0x11 => 1,
|
||||||
|
0x12 => 256,
|
||||||
|
0x20 => 3,
|
||||||
0x90 => 8,
|
0x90 => 8,
|
||||||
0xF0 => 4,
|
0xF0 => 4,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
|
@ -63,7 +70,7 @@ impl DataType {
|
||||||
|
|
||||||
macro_rules! impl_data_type {
|
macro_rules! impl_data_type {
|
||||||
($v: ident, $len: literal, $($t:tt)*) => {
|
($v: ident, $len: literal, $($t:tt)*) => {
|
||||||
if $v.len() >= $len {
|
if $v.len() - 1 == $len {
|
||||||
$($t)*
|
$($t)*
|
||||||
} else {
|
} else {
|
||||||
DataType::Nothing
|
DataType::Nothing
|
||||||
|
@ -79,9 +86,15 @@ impl From<Vec<u8>> for DataType {
|
||||||
return DataType::Nothing;
|
return DataType::Nothing;
|
||||||
};
|
};
|
||||||
match value[0] {
|
match value[0] {
|
||||||
0x01 => impl_data_type!(value, 1, DataType::Switch(value[1] != 0)),
|
0x01 => {
|
||||||
0x02 => impl_data_type!(value, 1, DataType::Slider(value[1])),
|
if value.len() - 1 < 2 { return DataType::Nothing }
|
||||||
0x03 => impl_data_type!(
|
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,
|
value,
|
||||||
256,
|
256,
|
||||||
if let Ok(s) = String::from_utf8(value[1..257].to_vec()) {
|
if let Ok(s) = String::from_utf8(value[1..257].to_vec()) {
|
||||||
|
@ -90,7 +103,7 @@ impl From<Vec<u8>> for DataType {
|
||||||
DataType::Nothing
|
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]))),
|
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]))),
|
0xF0 => impl_data_type!(value, 4, DataType::NodeRef(Identifier::from(&value[1..5]))),
|
||||||
_ => DataType::Nothing,
|
_ => DataType::Nothing,
|
||||||
|
@ -102,6 +115,13 @@ impl Into<Vec<u8>> for DataType {
|
||||||
fn into(self) -> Vec<u8> {
|
fn into(self) -> Vec<u8> {
|
||||||
match self {
|
match self {
|
||||||
DataType::Nothing => vec![0x00],
|
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::Switch(b) => vec![0x01, b as u8],
|
||||||
DataType::Slider(v) => vec![0x02, v],
|
DataType::Slider(v) => vec![0x02, v],
|
||||||
DataType::Text(s) => {
|
DataType::Text(s) => {
|
||||||
|
|
|
@ -8,10 +8,17 @@ use packet_data::PacketData;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Debug;
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct Packet {
|
pub struct Packet {
|
||||||
pub src: Identifier,
|
|
||||||
pub dest: Identifier,
|
pub dest: Identifier,
|
||||||
|
pub src: Identifier,
|
||||||
pub packet_id: Identifier,
|
pub packet_id: Identifier,
|
||||||
pub reply_to: Identifier,
|
pub reply_to: Identifier,
|
||||||
pub command: u8,
|
pub command: u8,
|
||||||
|
@ -19,13 +26,12 @@ pub struct Packet {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Packet {
|
impl Packet {
|
||||||
|
|
||||||
/// Build packet **without** CRC32
|
/// Build packet **without** CRC32
|
||||||
pub fn build_base_packet(&self) -> Vec<u8> {
|
pub fn build_base_packet(&self) -> Vec<u8> {
|
||||||
match self {
|
match self {
|
||||||
Packet {
|
Packet {
|
||||||
src,
|
|
||||||
dest,
|
dest,
|
||||||
|
src,
|
||||||
packet_id,
|
packet_id,
|
||||||
reply_to,
|
reply_to,
|
||||||
command,
|
command,
|
||||||
|
@ -34,8 +40,8 @@ impl Packet {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
let data_length = data.len();
|
let data_length = data.len();
|
||||||
buf.push(0x01);
|
buf.push(0x01);
|
||||||
buf.extend_from_slice(&src.bytes);
|
|
||||||
buf.extend_from_slice(&dest.bytes);
|
buf.extend_from_slice(&dest.bytes);
|
||||||
|
buf.extend_from_slice(&src.bytes);
|
||||||
buf.extend_from_slice(&packet_id.bytes);
|
buf.extend_from_slice(&packet_id.bytes);
|
||||||
buf.extend_from_slice(&reply_to.bytes);
|
buf.extend_from_slice(&reply_to.bytes);
|
||||||
buf.push(command.clone());
|
buf.push(command.clone());
|
||||||
|
@ -73,8 +79,8 @@ impl TryFrom<Vec<u8>> for Packet {
|
||||||
let data_length = (header[0x12] << 8) | header[0x13];
|
let data_length = (header[0x12] << 8) | header[0x13];
|
||||||
let crc32 = prelude::as_u32_be(&value[(0x14 + data_length as usize)..]);
|
let crc32 = prelude::as_u32_be(&value[(0x14 + data_length as usize)..]);
|
||||||
let packet = Packet {
|
let packet = Packet {
|
||||||
src: Identifier::from(&value[0x01..0x05]),
|
dest: Identifier::from(&value[0x01..0x05]),
|
||||||
dest: Identifier::from(&value[0x05..0x09]),
|
src: Identifier::from(&value[0x05..0x09]),
|
||||||
packet_id: Identifier::from(&value[0x09..0x0D]),
|
packet_id: Identifier::from(&value[0x09..0x0D]),
|
||||||
reply_to: Identifier::from(&value[0x0D..0x11]),
|
reply_to: Identifier::from(&value[0x0D..0x11]),
|
||||||
command: value[0x11],
|
command: value[0x11],
|
||||||
|
|
Loading…
Reference in a new issue