diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index 8c0d3aa..8ee9bc8 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -15,6 +15,12 @@ pub(crate) struct BcCodex { } impl BcCodex { + pub(crate) fn new_with_debug(credentials: Credentials) -> Self { + let mut context = BcContext::new(credentials); + + context.debug_on(); + Self { context } + } pub(crate) fn new(credentials: Credentials) -> Self { Self { context: BcContext::new(credentials), diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 6014485..515c5c1 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -116,10 +116,12 @@ fn bc_modern_msg<'a>( let mut encrypted_len = None; // Now we'll take the buffer that Nom gave a ref to and parse it. let extension = if ext_len > 0 { - log::trace!( - "Extension Txt: {:?}", - String::from_utf8(processed_ext_buf.to_vec()).unwrap_or("Not Text".to_string()) - ); + if context.debug { + println!( + "Extension Txt: {:?}", + String::from_utf8(processed_ext_buf.to_vec()).unwrap_or("Not Text".to_string()) + ); + } // Apply the XML parse function, but throw away the reference to decrypted in the Ok and // Err case. This error-error-error thing is the same idiom Nom uses internally. let parsed = Extension::try_parse(processed_ext_buf).map_err(|_| { @@ -182,8 +184,9 @@ fn bc_modern_msg<'a>( if context.in_bin_mode.contains(&(header.msg_num)) || in_binary { payload = match (context.get_encrypted(), encrypted_len) { (EncryptionProtocol::FullAes(_), Some(encrypted_len)) => { - log::trace!("Binary: {:X?}", &processed_payload_buf[0..30]); - + // if if context.debug { + // log::trace!("Binary: {:X?}", &processed_payload_buf[0..30]); + // } Some(BcPayloads::Binary( processed_payload_buf[0..(encrypted_len as usize)].to_vec(), )) @@ -191,10 +194,13 @@ fn bc_modern_msg<'a>( _ => Some(BcPayloads::Binary(payload_buf.to_vec())), }; } else { - log::trace!( - "Payload Txt: {:?}", - String::from_utf8(processed_payload_buf.to_vec()).unwrap_or("Not Text".to_string()) - ); + if context.debug { + println!( + "Payload Txt: {:?}", + String::from_utf8(processed_payload_buf.to_vec()) + .unwrap_or("Not Text".to_string()) + ); + } let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| { error!("header.msg_id: {}", header.msg_id); error!( diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 50e9483..219b1b9 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -210,6 +210,7 @@ pub(crate) struct BcContext { pub(crate) credentials: Credentials, pub(crate) in_bin_mode: HashSet, pub(crate) encryption_protocol: EncryptionProtocol, + pub(crate) debug: bool, } impl Bc { @@ -251,6 +252,7 @@ impl BcContext { credentials, in_bin_mode: HashSet::new(), encryption_protocol: EncryptionProtocol::Unencrypted, + debug: false, } } @@ -260,6 +262,7 @@ impl BcContext { credentials: Default::default(), in_bin_mode: HashSet::new(), encryption_protocol, + debug: false, } } @@ -279,6 +282,10 @@ impl BcContext { pub(crate) fn binary_off(&mut self, msg_id: u16) { self.in_bin_mode.remove(&msg_id); } + + pub(crate) fn debug_on(&mut self) { + self.debug = true; + } } impl BcHeader { diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 2691c7b..5259e8a 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -87,6 +87,8 @@ pub struct BcCameraOpt { pub discovery: DiscoveryMethods, /// Credentials for login pub credentials: Credentials, + /// Toggle debug print of underlying data + pub debug: bool, } /// Used to choose the print format of various status messages like battery levels @@ -314,16 +316,20 @@ impl BcCamera { let (sink, source): (BcConnSink, BcConnSource) = { match BcCamera::find_camera(options).await? { CameraLocation::Tcp(addr) => { - let (x, r) = TcpSource::new(addr, &username, passwd.as_ref()) + let (x, r) = TcpSource::new(addr, &username, passwd.as_ref(), options.debug) .await? .split(); (Box::new(x), Box::new(r)) } CameraLocation::Udp(discovery) => { - let (x, r) = - UdpSource::new_from_discovery(discovery, &username, passwd.as_ref()) - .await? - .split(); + let (x, r) = UdpSource::new_from_discovery( + discovery, + &username, + passwd.as_ref(), + options.debug, + ) + .await? + .split(); (Box::new(x), Box::new(r)) } } diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index 8e6d220..b85daab 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -1030,8 +1030,11 @@ impl Discovery { pub(crate) async fn check_tcp(&self, addr: SocketAddr, channel_id: u8) -> Result<()> { let username = "admin"; let password = Some("123456"); - let mut tcp_source = - timeout(*MAXIMUM_WAIT, TcpSource::new(addr, username, password)).await??; + let mut tcp_source = timeout( + *MAXIMUM_WAIT, + TcpSource::new(addr, username, password, false), + ) + .await??; let md5_username = md5_string(username, Md5Trunc::ZeroLast); let md5_password = password diff --git a/crates/core/src/bc_protocol/connection/tcpsource.rs b/crates/core/src/bc_protocol/connection/tcpsource.rs index 96b2cbb..a77262d 100644 --- a/crates/core/src/bc_protocol/connection/tcpsource.rs +++ b/crates/core/src/bc_protocol/connection/tcpsource.rs @@ -18,11 +18,17 @@ impl TcpSource { addr: SocketAddr, username: T, password: Option, + debug: bool, ) -> Result { let stream = connect_to(addr).await?; + let codex = if debug { + BcCodex::new_with_debug(Credentials::new(username, password)) + } else { + BcCodex::new(Credentials::new(username, password)) + }; Ok(Self { - inner: Framed::new(stream, BcCodex::new(Credentials::new(username, password))), + inner: Framed::new(stream, codex), }) } } diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index d87ecb7..f9feaff 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -43,15 +43,20 @@ impl UdpSource { camera_id: i32, username: T, password: Option, + debug: bool, ) -> Result { let stream = Arc::new(connect().await?); - Self::new_from_socket(stream, addr, client_id, camera_id, username, password).await + Self::new_from_socket( + stream, addr, client_id, camera_id, username, password, debug, + ) + .await } pub(crate) async fn new_from_discovery, U: Into>( discovery: DiscoveryResult, username: T, password: Option, + debug: bool, ) -> Result { // Ensure that the discovery keep alive are all stopped here // We now handle all coms in UdpSource @@ -62,6 +67,7 @@ impl UdpSource { discovery.camera_id, username, password, + debug, ) .await } @@ -73,14 +79,17 @@ impl UdpSource { camera_id: i32, username: T, password: Option, + debug: bool, ) -> Result { let bcudp_source = BcUdpSource::new_from_socket(stream, addr).await?; let payload_source = bcudp_source.into_payload_source(client_id, camera_id); let async_read = payload_source.into_async_read().compat(); - let framed = Framed::new( - async_read, - BcCodex::new(Credentials::new(username, password)), - ); + let codex = if debug { + BcCodex::new_with_debug(Credentials::new(username, password)) + } else { + BcCodex::new(Credentials::new(username, password)) + }; + let framed = Framed::new(async_read, codex); Ok(Self { inner: framed }) } diff --git a/src/config.rs b/src/config.rs index aa8d075..95ed506 100644 --- a/src/config.rs +++ b/src/config.rs @@ -126,6 +126,9 @@ pub(crate) struct CameraConfig { #[serde(default = "default_true", alias = "enable")] pub(crate) enabled: bool, + + #[serde(default = "default_false", alias = "verbose")] + pub(crate) debug: bool, } #[derive(Debug, Deserialize, Validate, Clone)] @@ -191,6 +194,10 @@ const fn default_true() -> bool { true } +const fn default_false() -> bool { + true +} + fn default_mqtt() -> Option { None } diff --git a/src/utils.rs b/src/utils.rs index 9cd2d4e..c7808fa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -97,6 +97,7 @@ impl AddressOrUid { username: camera_config.username.clone(), password: camera_config.password.clone(), }, + debug: camera_config.debug, }; trace!("Camera Info: {:?}", options);