feat: new stuff
This commit is contained in:
parent
3eece7ecd9
commit
f88706bac8
20 changed files with 232 additions and 77 deletions
|
@ -5,7 +5,9 @@ impl Into<Vec<u8>> for NodeManagementCommand {
|
|||
fn into(self) -> Vec<u8> {
|
||||
match self {
|
||||
NodeManagementCommand::Ping => vec![],
|
||||
NodeManagementCommand::RegisterNode { device_id } => device_id.bytes.to_vec(),
|
||||
NodeManagementCommand::RegisterNode { device_id } => {
|
||||
vec![device_id.bytes[0], device_id.bytes[1], device_id.bytes[2], device_id.bytes[3]]
|
||||
},
|
||||
NodeManagementCommand::RemoveNode => vec![],
|
||||
NodeManagementCommand::RegisterProperty { property_name, data_type, read_only } => {
|
||||
let mut vec = vec![];
|
||||
|
|
|
@ -31,7 +31,9 @@ impl PacketData {
|
|||
impl TryFrom<RawPacket> for PacketData {
|
||||
type Error = io::Error;
|
||||
fn try_from(raw_packet: RawPacket) -> io::Result<Self> {
|
||||
match raw_packet.command {
|
||||
match {
|
||||
raw_packet.command >> 4
|
||||
} {
|
||||
0x0 => Ok(PacketData::NodeManagement(NodeManagementCommand::try_from(raw_packet)?)),
|
||||
0x1 => Ok(PacketData::PropertyControl(PropertyControlCommand::try_from(raw_packet)?)),
|
||||
_ => Err(io::Error::new(io::ErrorKind::InvalidInput, "command group is unsupported"))
|
||||
|
|
|
@ -40,6 +40,10 @@ impl Into<Packet> for RawPacket {
|
|||
reply_to: Identifier::from(self.reply_to),
|
||||
command: self.command,
|
||||
data: PacketData::try_from(self.clone())
|
||||
.map_err(|e| {
|
||||
println!("{}", e);
|
||||
e
|
||||
})
|
||||
.unwrap_or(PacketData::Raw(self.data)),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,11 @@ impl TryFrom<Vec<u8>> for 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;
|
||||
println!("LENGTH {data_length}");
|
||||
if data.len() < 0x14 + data_length {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"))
|
||||
}
|
||||
let checksum = as_u32_be(data[(data.len() - 4)..].as_ref());
|
||||
let checksum = as_u32_be(data[(0x14 + data_length)..].as_ref());
|
||||
let built_checksum = crc32fast::hash(data[..(data.len() - 4)].as_ref());
|
||||
if checksum != built_checksum {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}")))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue