Skip to content

Commit

Permalink
Add support for the endpoint reference
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelYvon committed Feb 6, 2024
1 parent 9e945d4 commit 5e81599
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
27 changes: 21 additions & 6 deletions onvif/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Error {

#[derive(Clone, Eq, Hash, PartialEq)]
pub struct Device {
/// The WS-Discovery UUID / address reference
pub address: String,
pub name: Option<String>,
pub urls: Vec<Url>,
}
Expand All @@ -42,6 +44,7 @@ impl Debug for Device {
f.debug_struct("Device")
.field("name", &self.name)
.field("url", &DisplayList(&self.urls))
.field("address", &self.address)
.finish()
}
}
Expand Down Expand Up @@ -226,8 +229,13 @@ fn device_from_envelope(envelope: probe_matches::Envelope) -> Option<Device> {

let name = onvif_probe_match.name();
let urls = onvif_probe_match.x_addrs();
let address = onvif_probe_match.endpoint_reference_address();

Some(Device { name, urls })
Some(Device {
name,
urls,
address,
})
}

fn build_probe() -> probe::Envelope {
Expand All @@ -245,7 +253,9 @@ fn build_probe() -> probe::Envelope {

#[test]
fn test_xaddrs_extraction() {
fn make_xml(relates_to: &str, xaddrs: &str) -> String {
const DEVICE_ADDRESS: &str = "an address";

let make_xml = |relates_to: &str, xaddrs: &str| -> String {
format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
Expand All @@ -262,6 +272,9 @@ fn test_xaddrs_extraction() {
<d:XAddrs>http://something.else</d:XAddrs>
</d:ProbeMatch>
<d:ProbeMatch>
<wsa:EndpointReference>
<wsa:Address>{device_address}</wsa:Address>
</wsa:EndpointReference>
<d:Scopes>onvif://www.onvif.org/name/MyCamera2000</d:Scopes>
<d:XAddrs>{xaddrs}</d:XAddrs>
</d:ProbeMatch>
Expand All @@ -270,9 +283,10 @@ fn test_xaddrs_extraction() {
</SOAP-ENV:Envelope>
"#,
relates_to = relates_to,
xaddrs = xaddrs
xaddrs = xaddrs,
device_address = DEVICE_ADDRESS
)
}
};

let our_uuid = "uuid:84ede3de-7dec-11d0-c360-F01234567890";
let bad_uuid = "uuid:84ede3de-7dec-11d0-c360-F00000000000";
Expand All @@ -298,9 +312,10 @@ fn test_xaddrs_extraction() {
urls: vec![
Url::parse("http://addr_20").unwrap(),
Url::parse("http://addr_21").unwrap(),
Url::parse("http://addr_22").unwrap()
Url::parse("http://addr_22").unwrap(),
],
name: Some("MyCamera2000".to_string())
name: Some("MyCamera2000".to_string()),
address: DEVICE_ADDRESS.to_string(),
}]
);
}
29 changes: 25 additions & 4 deletions wsdl_rs/ws_discovery/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod probe {

use yaserde_derive::YaSerialize;

#[derive(Default, Eq, PartialEq, Debug, YaSerialize)]
Expand Down Expand Up @@ -56,18 +55,36 @@ pub mod probe {
}
}

pub mod probe_matches {
pub mod endpoint_reference {
use yaserde_derive::YaDeserialize;

#[derive(Default, Eq, PartialEq, Debug, YaDeserialize)]
#[yaserde(
prefix = "wsa",
namespace = "wsa: http://schemas.xmlsoap.org/ws/2004/08/addressing"
)]
pub struct EndpointReference {
#[yaserde(prefix = "wsa", rename = "Address")]
pub address: String,
}
}

pub mod probe_matches {
use crate::endpoint_reference::EndpointReference;
use percent_encoding::percent_decode_str;
use url::Url;
use yaserde_derive::YaDeserialize;

#[derive(Default, Eq, PartialEq, Debug, YaDeserialize)]
#[yaserde(
prefix = "d",
namespace = "d: http://schemas.xmlsoap.org/ws/2005/04/discovery"
namespace = "d: http://schemas.xmlsoap.org/ws/2005/04/discovery",
namespace = "wsa: http://schemas.xmlsoap.org/ws/2004/08/addressing"
)]
pub struct ProbeMatch {
#[yaserde(prefix = "wsa", rename = "EndpointReference")]
pub endpoint_reference: EndpointReference,

#[yaserde(prefix = "d", rename = "Types")]
pub types: String,

Expand Down Expand Up @@ -141,6 +158,10 @@ pub mod probe_matches {
self.find_in_scopes("onvif://www.onvif.org/hardware/")
}

pub fn endpoint_reference_address(&self) -> String {
self.endpoint_reference.address.to_string()
}

pub fn find_in_scopes(&self, prefix: &str) -> Option<String> {
self.scopes().iter().find_map(|url| {
url.as_str()
Expand Down Expand Up @@ -197,7 +218,7 @@ pub mod probe_matches {
de.x_addrs(),
vec![
Url::parse("http://192.168.0.100:80/onvif/device_service").unwrap(),
Url::parse("http://10.0.0.200:80/onvif/device_service").unwrap()
Url::parse("http://10.0.0.200:80/onvif/device_service").unwrap(),
]
);
}
Expand Down

0 comments on commit 5e81599

Please sign in to comment.