From 2b3fa02fd18e5acfb98505fade131240b597bd7b Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 4 Oct 2023 14:50:24 +0200 Subject: [PATCH] Implement public set_attribute interface --- src/command/header.rs | 1 + src/command/request.rs | 3 +++ src/device.rs | 16 ++++++++++++---- src/devices/e1.rs | 37 ++++++++++++++++++++----------------- src/devices/mod.rs | 2 +- src/security.rs | 8 -------- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/command/header.rs b/src/command/header.rs index fdb9021..9f140e1 100644 --- a/src/command/header.rs +++ b/src/command/header.rs @@ -48,6 +48,7 @@ impl Header { MessageType::from_u8(self.0[9]).unwrap() } + #[allow(unused)] pub fn device_type(&self) -> u8 { self.0[2] } diff --git a/src/command/request.rs b/src/command/request.rs index 18641cc..2f59dcc 100644 --- a/src/command/request.rs +++ b/src/command/request.rs @@ -39,10 +39,12 @@ impl CommandRequest { } } + #[allow(unused)] pub fn header(&self) -> &Header { &self.command.header } + #[allow(unused)] pub fn body(&self) -> &Body { &self.command.body } @@ -119,6 +121,7 @@ impl CommandQueryCustom { } } + #[allow(unused)] pub fn request(self) -> CommandRequest { self.command } diff --git a/src/device.rs b/src/device.rs index 781ab02..cf8b94d 100644 --- a/src/device.rs +++ b/src/device.rs @@ -11,8 +11,8 @@ use anyhow::{bail, Context, Error, Result}; use crate::{ command::{ - CommandHeartbeat, CommandQuerySubtype, CommandSubtypeResponse, MessageType, - RequestSerializer, + Body, CommandHeartbeat, CommandQueryCustom, CommandQuerySubtype, CommandSubtypeResponse, + MessageType, RequestSerializer, }, devices::{e1::E1, AttributeValue, DeviceBackend}, hex, @@ -148,7 +148,15 @@ impl Device { } pub async fn set_attribute(&self, attribute: &str, value: AttributeValue) -> Result<()> { - todo!() + self.build_send(self.device_backend.set_attribute(attribute, value)) + } + + pub async fn send_command(&self, message_type: MessageType, body: &[u8]) -> Result<()> { + self.build_send(CommandQueryCustom::new( + self.info.device_type, + message_type, + Body::from(body), + )) } pub fn register_update(mut self, f: F) -> Self @@ -221,7 +229,7 @@ impl Device { Ok(()) } - fn send_heartbeat(&self) -> Result<()> { + pub async fn send_heartbeat(&self) -> Result<()> { let msg = PacketBuilder::builder(self.info.id, CommandHeartbeat).finalize(0); self.send_message(&msg) } diff --git a/src/devices/e1.rs b/src/devices/e1.rs index 2cb560e..ed34bf7 100644 --- a/src/devices/e1.rs +++ b/src/devices/e1.rs @@ -106,13 +106,28 @@ impl DeviceBackend for E1 { ); } - fn set_attribute(&self, attribute: &str, value: &str) -> () { + fn set_attribute(&self, attribute: &str, value: AttributeValue) -> CommandRequest { match attribute { - "power" => (), - "child_lock" => (), - "storage" => (), + "power" => { + let mut cmd = CommandPower::new(self.device_protocol_version); + cmd.set_power(value.bool()); - _ => (), + cmd.request() + } + "child_lock" => { + let mut cmd = CommandLock::new(self.device_protocol_version); + cmd.set_lock(value.bool()); + + cmd.request() + } + "storage" => { + let mut cmd = CommandStorage::new(self.device_protocol_version); + cmd.set_storage(value.bool()); + + cmd.request() + } + + _ => todo!(), } } @@ -169,10 +184,6 @@ impl CommandPower { } } - pub fn power(&self) -> bool { - self.body()[1] == 0x01 - } - pub fn set_power(&mut self, power: bool) { self.command.body_mut()[1] = if power { 0x01 } else { 0x00 }; } @@ -207,10 +218,6 @@ impl CommandLock { } } - pub fn lock(&self) -> bool { - self.body()[1] == 0x03 - } - pub fn set_lock(&mut self, lock: bool) { self.command.body_mut()[1] = if lock { 0x03 } else { 0x04 } } @@ -250,10 +257,6 @@ impl CommandStorage { } } - pub fn storage(&self) -> bool { - self.body()[4] == 0x01 - } - pub fn set_storage(&mut self, storage: bool) { self.command.body_mut()[4] = if storage { 0x01 } else { 0x00 } } diff --git a/src/devices/mod.rs b/src/devices/mod.rs index 98a43e2..5851e01 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -141,5 +141,5 @@ pub trait DeviceBackend: Send + Sync { fn build_query(&self) -> CommandRequest; fn process_message(&mut self, msg: &[u8]); fn attributes(&self) -> &HashMap<&'static str, AttributeValue>; - fn set_attribute(&self, attribute: &str, value: &str) -> (); + fn set_attribute(&self, attribute: &str, value: AttributeValue) -> CommandRequest; } diff --git a/src/security.rs b/src/security.rs index d635b20..8106cf8 100644 --- a/src/security.rs +++ b/src/security.rs @@ -205,15 +205,7 @@ impl Security { || msgtype == MsgType::ENCRYPTED_REQUEST as u8 { let sign = data[(data.len() - 32)..].to_vec(); - // let sign_ref = b"\xf5\x1c\x93\x18\xa3\xb9HR.X\x9aFU\xb1\xc4N\xe8J\x1a\x04\x0f\xd4\x90\xc4\x132\xeby\xa6\x83\x06e"; - - // assert_eq!(sign, sign_ref); - data = data[..(data.len() - 32)].to_vec(); - let data_ref = b"\xb2\x174\xbb\xf4\xf1\x8fWg\xc4\x97h%\x06\x9aCIL\xf6tf\x86\x8b\x83\xac\xa9@\xb1r\xb3\xa0>>\xd0_#\x03\x93\xcdZ[\x17\xde\xd8\xaeT\x1a\xd0\x0f\xa5\x8fk\xebJF\x0b\xfd\xf8\\o_\x06\xe9\xdd\xda45\x0b\x90\xf1\x82*\x9f\xa4\x1a\xc3\xb3\xde]\x04\x9e\x9c\xafy\xcfq\x02\xa34\xa4\x15\x0b\xcb\t\x96V\xd3f\x14\xd6\xf5X]\xc3'\xbc\x1b\xc1\x10\x08\x9c\x8d"; - - // assert_eq!(data, data_ref); - data = Self::aes_cbc_decrypt(&data, &self.tcp_key.unwrap()).to_vec(); let compare_sign: Vec = Sha256::digest(&[header.to_vec(), data.clone()].concat())