From 3014be90082ddbeceaac13e0c435726d08941552 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 4 Apr 2023 18:49:04 +0700 Subject: [PATCH] Add abilityinfo.rs --- crates/core/src/bc_protocol/abilityinfo.rs | 104 +++++++++++++++++++++ crates/core/src/bc_protocol/talk.rs | 4 - 2 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 crates/core/src/bc_protocol/abilityinfo.rs diff --git a/crates/core/src/bc_protocol/abilityinfo.rs b/crates/core/src/bc_protocol/abilityinfo.rs new file mode 100644 index 0000000..6718584 --- /dev/null +++ b/crates/core/src/bc_protocol/abilityinfo.rs @@ -0,0 +1,104 @@ +use super::{BcCamera, Error, Result}; +use crate::bc::{model::*, xml::*}; + +impl BcCamera { + /// Get the ability info xml for the current user + pub async fn get_abilityinfo(&self) -> Result { + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let mut sub_get = connection.subscribe(msg_num).await?; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_ABILITY_INFO, + channel_id: self.channel_id, + msg_num, + response_code: 0, + stream_type: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + user_name: Some(self.get_credentials().username.clone()), + token: Some("system, streaming, PTZ, IO, security, replay, disk, network, alarm, record, video, image".to_string()), + ..Default::default() + }), + payload: None, + }), + }; + + sub_get.send(get).await?; + let msg = sub_get.recv().await?; + if msg.meta.response_code != 200 { + return Err(Error::CameraServiceUnavaliable); + } + + if let BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + ability_info: Some(ability_info), + .. + })), + .. + }) = msg.body + { + Ok(ability_info) + } else { + Err(Error::UnintelligibleReply { + reply: std::sync::Arc::new(Box::new(msg)), + why: "Expected AbilityInfo xml but it was not recieved", + }) + } + } + + /// Populate ability list of the camera + pub async fn polulate_abilities(&self) -> Result<()> { + let info = self.get_abilityinfo().await?; + let mut abilities: Vec = vec![]; + + let mut tokens: Vec> = vec![ + info.system.as_ref(), + info.network.as_ref(), + info.alarm.as_ref(), + info.image.as_ref(), + info.video.as_ref(), + info.security.as_ref(), + info.replay.as_ref(), + info.ptz.as_ref(), + info.io.as_ref(), + info.streaming.as_ref(), + ]; + for token in tokens.drain(..).flatten() { + for sub_module in token.sub_module.iter() { + abilities.extend( + sub_module + .ability_value + .replace(' ', "") + .split(',') + .map(|s| s.to_string()), + ); + } + } + + let mut locked_abilities = self.abilities.write().await; + for ability in abilities.iter() { + let mut abilities_ro = ability.split('_').map(|s| s.to_string()); + if let (Some(ability_name), Some(ability_kind)) = + (abilities_ro.next(), abilities_ro.next()) + { + match ability_kind.as_str() { + "rw" => { + locked_abilities.insert(ability_name, super::ReadKind::ReadWrite); + } + "ro" => { + locked_abilities.insert(ability_name, super::ReadKind::ReadOnly); + } + _ => { + continue; + } + } + } + } + + Ok(()) + } +} diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs index 9a37fe7..f4e08c2 100644 --- a/crates/core/src/bc_protocol/talk.rs +++ b/crates/core/src/bc_protocol/talk.rs @@ -14,7 +14,6 @@ impl BcCamera { /// It is also sent when the request for talk config returns status code 422 /// pub async fn talk_stop(&self) -> Result<()> { - self.has_ability_rw("talk").await?; let connection = self.get_connection(); let msg_num = self.new_message_num(); @@ -59,7 +58,6 @@ impl BcCamera { /// Requests the [`TalkAbility`] xml /// pub async fn talk_ability(&self) -> Result { - self.has_ability_ro("talk").await?; let connection = self.get_connection(); let msg_num = self.new_message_num(); let mut sub_get = connection.subscribe(msg_num).await?; @@ -119,7 +117,6 @@ impl BcCamera { /// /// pub async fn talk(&self, adpcm: &[u8], talk_config: TalkConfig) -> Result<()> { - self.has_ability_rw("talk").await?; let connection = self.get_connection(); let msg_num = self.new_message_num(); @@ -261,7 +258,6 @@ impl BcCamera { /// /// pub async fn talk_stream(&self, rx: Receiver>, talk_config: TalkConfig) -> Result<()> { - self.has_ability_rw("talk").await?; let connection = self.get_connection(); let msg_num = self.new_message_num();