fix: remove clunky code
This commit is contained in:
parent
4be6659662
commit
b38cb3022b
5 changed files with 51 additions and 36 deletions
2
domo_proto/Cargo.lock
generated
2
domo_proto/Cargo.lock
generated
|
@ -19,7 +19,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "domo_proto"
|
name = "domo_proto"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crc32fast",
|
"crc32fast",
|
||||||
"rand",
|
"rand",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "domo_proto"
|
name = "domo_proto"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
|
@ -3,7 +3,6 @@ use crate::identifier::Identifier;
|
||||||
use crate::packet::packet_data::PacketData;
|
use crate::packet::packet_data::PacketData;
|
||||||
use crate::packet::raw_packet::RawPacket;
|
use crate::packet::raw_packet::RawPacket;
|
||||||
use crate::packet::{Packet, ToPacket};
|
use crate::packet::{Packet, ToPacket};
|
||||||
use crate::prelude::as_u32_be;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub mod vec;
|
pub mod vec;
|
||||||
|
@ -149,9 +148,14 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
|
||||||
0x1A => {
|
0x1A => {
|
||||||
if raw_packet.data_length == 36 {
|
if raw_packet.data_length == 36 {
|
||||||
Ok(PropertyControlCommand::Subscribe {
|
Ok(PropertyControlCommand::Subscribe {
|
||||||
device_id: Identifier::from(as_u32_be(
|
device_id: Identifier::from(
|
||||||
raw_packet.data[0x14..0x18].as_ref(),
|
u32::from_be_bytes([
|
||||||
)),
|
raw_packet.data[0x14],
|
||||||
|
raw_packet.data[0x15],
|
||||||
|
raw_packet.data[0x16],
|
||||||
|
raw_packet.data[0x17],
|
||||||
|
])
|
||||||
|
),
|
||||||
property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec())
|
property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec())
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8")
|
io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8")
|
||||||
|
@ -165,9 +169,14 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
|
||||||
0x1B => {
|
0x1B => {
|
||||||
if raw_packet.data_length == 36 {
|
if raw_packet.data_length == 36 {
|
||||||
Ok(PropertyControlCommand::Unsubscribe {
|
Ok(PropertyControlCommand::Unsubscribe {
|
||||||
device_id: Identifier::from(as_u32_be(
|
device_id: Identifier::from(
|
||||||
raw_packet.data[0x14..0x18].as_ref(),
|
u32::from_be_bytes([
|
||||||
)),
|
raw_packet.data[0x14],
|
||||||
|
raw_packet.data[0x15],
|
||||||
|
raw_packet.data[0x16],
|
||||||
|
raw_packet.data[0x17],
|
||||||
|
])
|
||||||
|
),
|
||||||
property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec())
|
property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec())
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8")
|
io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8")
|
||||||
|
|
|
@ -1,29 +1,53 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
use crate::packet::PACKET_HEADER_SIZE;
|
use crate::packet::PACKET_HEADER_SIZE;
|
||||||
use crate::packet::raw_packet::RawPacket;
|
use crate::packet::raw_packet::RawPacket;
|
||||||
use crate::prelude::as_u32_be;
|
|
||||||
|
|
||||||
impl TryFrom<Vec<u8>> for RawPacket {
|
impl TryFrom<Vec<u8>> for RawPacket {
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
fn try_from(data: Vec<u8>) -> io::Result<Self> {
|
fn try_from(data: Vec<u8>) -> io::Result<Self> {
|
||||||
if data.len() < PACKET_HEADER_SIZE {
|
if data.len() < PACKET_HEADER_SIZE {
|
||||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"))
|
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"));
|
||||||
}
|
}
|
||||||
let data_length = (((data[0x12] as u16) << 8) | data[0x13] as u16) as usize;
|
let data_length = (((data[0x12] as u16) << 8) | data[0x13] as u16) as usize;
|
||||||
if data.len() < 0x14 + data_length {
|
if data.len() < 0x14 + data_length {
|
||||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"))
|
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"));
|
||||||
}
|
}
|
||||||
let checksum = as_u32_be(data[(0x14 + data_length)..].as_ref());
|
let checksum = u32::from_be_bytes([
|
||||||
|
data[0x14 + data_length],
|
||||||
|
data[0x14 + data_length + 1],
|
||||||
|
data[0x14 + data_length + 2],
|
||||||
|
data[0x14 + data_length + 3],
|
||||||
|
]);
|
||||||
let built_checksum = crc32fast::hash(data[..(data.len() - 4)].as_ref());
|
let built_checksum = crc32fast::hash(data[..(data.len() - 4)].as_ref());
|
||||||
if checksum != built_checksum {
|
if checksum != built_checksum {
|
||||||
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}")))
|
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}")));
|
||||||
}
|
}
|
||||||
Ok(RawPacket {
|
Ok(RawPacket {
|
||||||
version: data[0],
|
version: data[0],
|
||||||
dest: as_u32_be(data[0x01..0x05].as_ref()),
|
dest: u32::from_be_bytes([
|
||||||
src: as_u32_be(data[0x05..0x09].as_ref()),
|
data[0x01],
|
||||||
packet_id: as_u32_be(data[0x09..0x0D].as_ref()),
|
data[0x02],
|
||||||
reply_to: as_u32_be(data[0x0D..0x11].as_ref()),
|
data[0x03],
|
||||||
|
data[0x04]
|
||||||
|
]),
|
||||||
|
src: u32::from_be_bytes([
|
||||||
|
data[0x05],
|
||||||
|
data[0x06],
|
||||||
|
data[0x07],
|
||||||
|
data[0x08],
|
||||||
|
]),
|
||||||
|
packet_id: u32::from_be_bytes([
|
||||||
|
data[0x09],
|
||||||
|
data[0x0A],
|
||||||
|
data[0x0B],
|
||||||
|
data[0x0C],
|
||||||
|
]),
|
||||||
|
reply_to: u32::from_be_bytes([
|
||||||
|
data[0x0D],
|
||||||
|
data[0x0E],
|
||||||
|
data[0x0F],
|
||||||
|
data[0x10],
|
||||||
|
]),
|
||||||
command: data[0x11],
|
command: data[0x11],
|
||||||
data_length,
|
data_length,
|
||||||
data: data[0x14..(0x14 + data_length)].to_vec(),
|
data: data[0x14..(0x14 + data_length)].to_vec(),
|
||||||
|
|
|
@ -1,21 +1,3 @@
|
||||||
pub fn as_u32_be(array: &[u8]) -> u32 {
|
|
||||||
((array[0] as u32) << 24)
|
|
||||||
+ ((array[1] as u32) << 16)
|
|
||||||
+ ((array[2] as u32) << 8)
|
|
||||||
+ ((array[3] as u32) << 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_u64_be(array: &[u8]) -> u64 {
|
|
||||||
((array[0] as u64) << 56)
|
|
||||||
+ ((array[1] as u64) << 48)
|
|
||||||
+ ((array[2] as u64) << 40)
|
|
||||||
+ ((array[3] as u64) << 32)
|
|
||||||
+ ((array[4] as u64) << 24)
|
|
||||||
+ ((array[5] as u64) << 16)
|
|
||||||
+ ((array[6] as u64) << 8)
|
|
||||||
+ ((array[7] as u64) << 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_into_enum_variant {
|
macro_rules! impl_into_enum_variant {
|
||||||
($houser: tt::$variant: tt, $impl_type: ty) => {
|
($houser: tt::$variant: tt, $impl_type: ty) => {
|
||||||
impl Into<$houser> for $impl_type {
|
impl Into<$houser> for $impl_type {
|
||||||
|
|
Loading…
Reference in a new issue