Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/port control #231

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/core/src/bc/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub const MSG_ID_REBOOT: u32 = 23;
pub const MSG_ID_MOTION_REQUEST: u32 = 31;
/// Motion detection messages
pub const MSG_ID_MOTION: u32 = 33;
/// Set service ports
pub const MSG_ID_SET_SERVICE_PORTS: u32 = 36;
/// Get service ports
pub const MSG_ID_GET_SERVICE_PORTS: u32 = 37;
/// Version messages have this ID
pub const MSG_ID_VERSION: u32 = 80;
/// Ping messages have this ID
Expand Down
102 changes: 102 additions & 0 deletions crates/core/src/bc/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ pub struct BcXml {
/// Play a sound
#[serde(rename = "audioPlayInfo", skip_serializing_if = "Option::is_none")]
pub audio_play_info: Option<AudioPlayInfo>,
/// For changing baichaun server port
#[serde(rename = "ServerPort", skip_serializing_if = "Option::is_none")]
pub server_port: Option<ServerPort>,
/// For changing http server port
#[serde(rename = "HttpPort", skip_serializing_if = "Option::is_none")]
pub http_port: Option<HttpPort>,
/// For changing https server port
#[serde(rename = "HttpsPort", skip_serializing_if = "Option::is_none")]
pub https_port: Option<HttpsPort>,
/// For changing rtsp server port
#[serde(rename = "RtspPort", skip_serializing_if = "Option::is_none")]
pub rtsp_port: Option<RtspPort>,
/// For changing rtmp server port
#[serde(rename = "RtmpPort", skip_serializing_if = "Option::is_none")]
pub rtmp_port: Option<RtmpPort>,
/// For changing rtmp server port
#[serde(rename = "OnvifPort", skip_serializing_if = "Option::is_none")]
pub onvif_port: Option<OnvifPort>,
}

impl BcXml {
Expand Down Expand Up @@ -1373,6 +1391,90 @@ pub struct AudioPlayInfo {
pub on_off: u32,
}

/// Server port for baichaun defaults 9000
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct ServerPort {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// The port number
#[serde(rename = "serverPort")]
pub port: u32,
/// The enable status known values are `1`, `0`
#[serde(skip_serializing_if = "Option::is_none")]
pub enable: Option<u32>,
}

/// Server port for http defaults to 80
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct HttpPort {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// The port number
#[serde(rename = "httpPort")]
pub port: u32,
/// The enable status known values are `1`, `0`
#[serde(skip_serializing_if = "Option::is_none")]
pub enable: Option<u32>,
}

/// Server port for https defaults to 443
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct HttpsPort {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// The port number
#[serde(rename = "httpsPort")]
pub port: u32,
/// The enable status known values are `1`, `0`
#[serde(skip_serializing_if = "Option::is_none")]
pub enable: Option<u32>,
}

/// Server port for Rtsp defaults to 554
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct RtspPort {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// The port number
#[serde(rename = "rtspPort")]
pub port: u32,
/// The enable status known values are `1`, `0`
#[serde(skip_serializing_if = "Option::is_none")]
pub enable: Option<u32>,
}

/// Server port for Rtmp defaults to 1935
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct RtmpPort {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// The port number
#[serde(rename = "rtmpPort")]
pub port: u32,
/// The enable status known values are `1`, `0`, can be `None` on cameras that can't change it
#[serde(skip_serializing_if = "Option::is_none")]
pub enable: Option<u32>,
}

/// Server port for Onvif defaults to 8000
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct OnvifPort {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// The port number
#[serde(rename = "onvifPort")]
pub port: u32,
/// The enable status known values are `1`, `0`
#[serde(skip_serializing_if = "Option::is_none")]
pub enable: Option<u32>,
}

/// Convience function to return the xml version used throughout the library
pub fn xml_ver() -> String {
"1.1".to_string()
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/bc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod ptz;
mod pushinfo;
mod reboot;
mod resolution;
mod services;
mod siren;
mod snap;
mod stream;
Expand Down
22 changes: 14 additions & 8 deletions crates/core/src/bc_protocol/connection/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,24 @@ impl Discoverer {
}),
};
trace!("Sending look up {:?}", msg);
let (packet, _) = self
let (reg, relay, _) = self
.retry_send(msg, addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload: UdpXml::M2cQr(m2c_q_r),
} if valid_addr(&m2c_q_r.reg) => Some((m2c_q_r, addr)),
payload:
UdpXml::M2cQr(M2cQr {
reg: Some(reg),
relay: Some(relay),
..
}),
} if valid_addr(&reg) && valid_addr(&relay) => Some((reg, relay, addr)),
_ => None,
})
.await?;
trace!("Look up complete");
Ok(UidLookupResults {
reg: SocketAddr::new(packet.reg.ip.parse()?, packet.reg.port),
relay: SocketAddr::new(packet.relay.ip.parse()?, packet.relay.port),
reg: SocketAddr::new(reg.ip.parse()?, reg.port),
relay: SocketAddr::new(relay.ip.parse()?, relay.port),
})
}

Expand Down Expand Up @@ -489,14 +494,15 @@ impl Discoverer {
dmap,
dev,
relay,
sid,
sid: Some(sid),
rsp,
..
}),
} if (dev.as_ref().map(valid_addr).unwrap_or(false)
|| dmap.as_ref().map(valid_addr).unwrap_or(false)
|| relay.as_ref().map(valid_addr).unwrap_or(false))
&& rsp != -1 =>
&& rsp != -1
&& rsp != -3 =>
{
Some(Ok((sid, dev, dmap, relay)))
}
Expand Down Expand Up @@ -544,7 +550,7 @@ impl Discoverer {
} if (dev.as_ref().map(valid_addr).unwrap_or(false)
|| dmap.as_ref().map(valid_addr).unwrap_or(false)
|| relay.as_ref().map(valid_addr).unwrap_or(false))
&& rsp == -1 =>
&& (rsp == -1 || rsp == -3) =>
{
Some(Err(Error::RegisterError))
}
Expand Down
11 changes: 10 additions & 1 deletion crates/core/src/bc_protocol/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::bc::model::Bc;
use super::bc::model::{Bc, BcXml};
use crate::NomErrorType;
use thiserror::Error;

Expand Down Expand Up @@ -35,6 +35,15 @@ pub enum Error {
why: &'static str,
},

/// Raised when a BcXml reply was not understood
#[error("Communication error")]
UnintelligibleXml {
/// The Bc packet that was not understood
reply: std::sync::Arc<Box<BcXml>>,
/// The message attached to the error
why: &'static str,
},

/// Raised when the camera responds with a status code over than OK
#[error("Camera responded with Service Unavaliable: {0}")]
CameraServiceUnavailable(u16),
Expand Down
Loading
Loading