oxvif 0.7.0

Async Rust client library for the ONVIF IP camera protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use super::{xml_bool, xml_str};
use crate::error::OnvifError;
use crate::soap::{SoapError, XmlNode};

// ── DeviceInfo ────────────────────────────────────────────────────────────────

/// Hardware and firmware information returned by `GetDeviceInformation`.
///
/// Absent fields in the device response are represented as empty strings.
#[derive(Debug, Clone, Default)]
pub struct DeviceInfo {
    pub manufacturer: String,
    pub model: String,
    pub firmware_version: String,
    pub serial_number: String,
    pub hardware_id: String,
}

impl DeviceInfo {
    /// Parse from a `GetDeviceInformationResponse` node.
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        Ok(Self {
            manufacturer: xml_str(resp, "Manufacturer").unwrap_or_default(),
            model: xml_str(resp, "Model").unwrap_or_default(),
            firmware_version: xml_str(resp, "FirmwareVersion").unwrap_or_default(),
            serial_number: xml_str(resp, "SerialNumber").unwrap_or_default(),
            hardware_id: xml_str(resp, "HardwareId").unwrap_or_default(),
        })
    }
}

// ── SystemDateTime ────────────────────────────────────────────────────────────

/// Device clock information returned by `GetSystemDateAndTime`.
///
/// The primary use-case is computing the UTC offset for WS-Security:
///
/// ```no_run
/// # use oxvif::{OnvifClient, OnvifError};
/// # async fn run() -> Result<(), OnvifError> {
/// let client = OnvifClient::new("http://192.168.1.1/onvif/device_service");
/// let dt     = client.get_system_date_and_time().await?;
/// let client = client.with_utc_offset(dt.utc_offset_secs());
/// # Ok(()) }
/// ```
#[derive(Debug, Clone)]
pub struct SystemDateTime {
    /// Device UTC clock as a Unix timestamp (seconds since 1970-01-01T00:00:00Z).
    /// `None` if the response contained no `UTCDateTime` element.
    pub utc_unix: Option<i64>,
    /// POSIX timezone string (e.g. `"CST-8"`).  Empty if absent.
    pub timezone: String,
    /// Whether daylight saving time is currently active on the device.
    pub daylight_savings: bool,
}

impl SystemDateTime {
    /// Seconds between the device UTC clock and the local system UTC clock.
    ///
    /// Returns `0` when `utc_unix` is `None`.
    /// Pass the result to [`OnvifClient::with_utc_offset`](crate::client::OnvifClient::with_utc_offset).
    pub fn utc_offset_secs(&self) -> i64 {
        match self.utc_unix {
            Some(device_utc) => {
                let local_utc = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs() as i64;
                device_utc - local_utc
            }
            None => 0,
        }
    }

    /// Parse from a `GetSystemDateAndTimeResponse` node.
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let sdt = resp
            .child("SystemDateAndTime")
            .ok_or_else(|| SoapError::missing("SystemDateAndTime"))?;

        Ok(Self {
            utc_unix: sdt.child("UTCDateTime").and_then(parse_datetime_node),
            timezone: sdt
                .path(&["TimeZone", "TZ"])
                .map(|n| n.text().to_string())
                .unwrap_or_default(),
            daylight_savings: xml_bool(sdt, "DaylightSavings"),
        })
    }
}

/// Parse year/month/day/hour/minute/second from an ONVIF `DateTime` node and
/// return a Unix timestamp (seconds since epoch).
fn parse_datetime_node(node: &XmlNode) -> Option<i64> {
    let year = node
        .path(&["Date", "Year"])
        .and_then(|n| n.text().parse::<i32>().ok())?;
    let month = node
        .path(&["Date", "Month"])
        .and_then(|n| n.text().parse::<i32>().ok())?;
    let day = node
        .path(&["Date", "Day"])
        .and_then(|n| n.text().parse::<i32>().ok())?;
    let hour = node
        .path(&["Time", "Hour"])
        .and_then(|n| n.text().parse::<i32>().ok())?;
    let min = node
        .path(&["Time", "Minute"])
        .and_then(|n| n.text().parse::<i32>().ok())?;
    let sec = node
        .path(&["Time", "Second"])
        .and_then(|n| n.text().parse::<i32>().ok())?;
    Some(civil_to_unix(year, month, day, hour, min, sec))
}

/// Convert a proleptic Gregorian calendar date + time to a Unix timestamp.
/// Uses the Howard Hinnant days-from-civil algorithm.
pub(crate) fn civil_to_unix(year: i32, month: i32, day: i32, hour: i32, min: i32, sec: i32) -> i64 {
    let mut y = year as i64;
    let m = month as i64;
    if m <= 2 {
        y -= 1;
    }
    let era = if y >= 0 { y } else { y - 399 } / 400;
    let yoe = y - era * 400;
    let mp = if m > 2 { m - 3 } else { m + 9 };
    let doy = (153 * mp + 2) / 5 + day as i64 - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146_097 + doe - 719_468;
    days * 86_400 + hour as i64 * 3600 + min as i64 * 60 + sec as i64
}

// ── Hostname ──────────────────────────────────────────────────────────────────

/// Hostname configuration returned by `GetHostname`.
#[derive(Debug, Clone)]
pub struct Hostname {
    /// `true` if the hostname is assigned by DHCP rather than set manually.
    pub from_dhcp: bool,
    /// The configured hostname. `None` if no hostname is set.
    pub name: Option<String>,
}

impl Hostname {
    /// Parse from a `GetHostnameResponse` node.
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let info = resp
            .child("HostnameInformation")
            .ok_or_else(|| SoapError::missing("HostnameInformation"))?;
        Ok(Self {
            from_dhcp: xml_bool(info, "FromDHCP"),
            name: xml_str(info, "Name").filter(|s| !s.is_empty()),
        })
    }
}

// ── NtpInfo ───────────────────────────────────────────────────────────────────

/// NTP configuration returned by `GetNTP`.
#[derive(Debug, Clone)]
pub struct NtpInfo {
    /// `true` if NTP servers are obtained from DHCP rather than set manually.
    pub from_dhcp: bool,
    /// Manually configured NTP server addresses (DNS names or IP strings).
    /// Empty when `from_dhcp` is `true` or no servers are configured.
    pub servers: Vec<String>,
}

impl NtpInfo {
    /// Parse from a `GetNTPResponse` node.
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let info = resp
            .child("NTPInformation")
            .ok_or_else(|| SoapError::missing("NTPInformation"))?;
        Ok(Self {
            from_dhcp: xml_bool(info, "FromDHCP"),
            servers: info
                .children_named("NTPManual")
                .chain(info.children_named("NTPFromDHCP"))
                .filter_map(|entry| {
                    // Prefer DNS name, then IPv4, then IPv6
                    xml_str(entry, "DNSname")
                        .filter(|s| !s.is_empty())
                        .or_else(|| xml_str(entry, "IPv4Address").filter(|s| !s.is_empty()))
                        .or_else(|| xml_str(entry, "IPv6Address").filter(|s| !s.is_empty()))
                })
                .collect(),
        })
    }
}

// ── OnvifService ─────────────────────────────────────────────────────────────

/// A single service entry returned by `GetServices`.
///
/// `GetServices` is the proper ONVIF mechanism for discovering all service
/// endpoints, including Media2. Use [`OnvifService::is_media2`] to identify
/// the Media2 entry.
#[derive(Debug, Clone)]
pub struct OnvifService {
    /// Service namespace URI, e.g. `"http://www.onvif.org/ver20/media/wsdl"`.
    pub namespace: String,
    /// Service endpoint URL.
    pub url: String,
    pub version_major: u32,
    pub version_minor: u32,
}

impl OnvifService {
    /// Returns `true` if this entry is the Media2 service.
    pub fn is_media2(&self) -> bool {
        self.namespace == "http://www.onvif.org/ver20/media/wsdl"
    }

    pub(crate) fn vec_from_xml(resp: &XmlNode) -> Result<Vec<Self>, OnvifError> {
        Ok(resp
            .children_named("Service")
            .map(|s| Self {
                namespace: xml_str(s, "Namespace").unwrap_or_default(),
                url: xml_str(s, "XAddr").unwrap_or_default(),
                version_major: s
                    .path(&["Version", "Major"])
                    .and_then(|n| n.text().parse().ok())
                    .unwrap_or(0),
                version_minor: s
                    .path(&["Version", "Minor"])
                    .and_then(|n| n.text().parse().ok())
                    .unwrap_or(0),
            })
            .collect())
    }
}

// ── User ──────────────────────────────────────────────────────────────────────

/// A device user account returned by `GetUsers`.
#[derive(Debug, Clone)]
pub struct User {
    pub username: String,
    /// Access level: `"Administrator"`, `"Operator"`, `"User"`, `"Anonymous"`, or `"Extended"`.
    pub user_level: String,
}

impl User {
    pub(crate) fn vec_from_xml(resp: &XmlNode) -> Result<Vec<Self>, OnvifError> {
        resp.children_named("User")
            .map(|n| {
                let username = xml_str(n, "Username").unwrap_or_default();
                let user_level = xml_str(n, "UserLevel").unwrap_or_default();
                Ok(Self {
                    username,
                    user_level,
                })
            })
            .collect()
    }
}

// ── NetworkInterface ──────────────────────────────────────────────────────────

/// Network interface configuration returned by `GetNetworkInterfaces`.
#[derive(Debug, Clone)]
pub struct NetworkInterface {
    pub token: String,
    pub enabled: bool,
    pub name: String,
    pub hw_address: String,
    pub mtu: u32,
    pub ipv4_enabled: bool,
    /// Manual or DHCP-assigned IPv4 address. Empty when DHCP is active and no address is available.
    pub ipv4_address: String,
    pub ipv4_prefix_length: u32,
    pub ipv4_from_dhcp: bool,
}

impl NetworkInterface {
    pub(crate) fn vec_from_xml(resp: &XmlNode) -> Result<Vec<Self>, OnvifError> {
        resp.children_named("NetworkInterfaces")
            .map(|n| {
                let token = n
                    .attr("token")
                    .filter(|t| !t.is_empty())
                    .ok_or_else(|| SoapError::missing("NetworkInterfaces/@token"))?
                    .to_string();
                let enabled = xml_bool(n, "Enabled");
                let name = n
                    .path(&["Info", "Name"])
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                let hw_address = n
                    .path(&["Info", "HwAddress"])
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                let mtu = n
                    .path(&["Info", "MTU"])
                    .and_then(|x| x.text().parse().ok())
                    .unwrap_or(0);
                let ipv4_enabled = n
                    .path(&["IPv4", "Enabled"])
                    .map(|x| x.text() == "true" || x.text() == "1")
                    .unwrap_or(false);
                let ipv4_from_dhcp = n
                    .path(&["IPv4", "Config", "FromDHCP"])
                    .map(|x| x.text() == "true" || x.text() == "1")
                    .unwrap_or(false);
                let ipv4_address = n
                    .path(&["IPv4", "Config", "Manual", "Address"])
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                let ipv4_prefix_length = n
                    .path(&["IPv4", "Config", "Manual", "PrefixLength"])
                    .and_then(|x| x.text().parse().ok())
                    .unwrap_or(0);
                Ok(Self {
                    token,
                    enabled,
                    name,
                    hw_address,
                    mtu,
                    ipv4_enabled,
                    ipv4_address,
                    ipv4_prefix_length,
                    ipv4_from_dhcp,
                })
            })
            .collect()
    }
}

// ── NetworkProtocol ───────────────────────────────────────────────────────────

/// A network protocol entry returned by `GetNetworkProtocols`.
#[derive(Debug, Clone)]
pub struct NetworkProtocol {
    /// Protocol name, e.g. `"HTTP"`, `"HTTPS"`, `"RTSP"`.
    pub name: String,
    pub enabled: bool,
    /// Configured port numbers (typically one element).
    pub ports: Vec<u32>,
}

impl NetworkProtocol {
    pub(crate) fn vec_from_xml(resp: &XmlNode) -> Result<Vec<Self>, OnvifError> {
        Ok(resp
            .children_named("NetworkProtocols")
            .map(|n| Self {
                name: xml_str(n, "Name").unwrap_or_default(),
                enabled: xml_bool(n, "Enabled"),
                ports: n
                    .children_named("Port")
                    .filter_map(|p| p.text().parse().ok())
                    .collect(),
            })
            .collect())
    }
}

// ── DnsInformation ────────────────────────────────────────────────────────────

/// DNS configuration returned by `GetDNS`.
#[derive(Debug, Clone)]
pub struct DnsInformation {
    pub from_dhcp: bool,
    /// Manually configured DNS server addresses (IPv4 or IPv6 strings).
    pub servers: Vec<String>,
}

impl DnsInformation {
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let info = resp
            .child("DNSInformation")
            .ok_or_else(|| SoapError::missing("DNSInformation"))?;
        Ok(Self {
            from_dhcp: xml_bool(info, "FromDHCP"),
            servers: info
                .children_named("DNSManual")
                .chain(info.children_named("DNSFromDHCP"))
                .filter_map(|e| {
                    xml_str(e, "IPv4Address")
                        .filter(|s| !s.is_empty())
                        .or_else(|| xml_str(e, "IPv6Address").filter(|s| !s.is_empty()))
                        .or_else(|| xml_str(e, "DNSname").filter(|s| !s.is_empty()))
                })
                .collect(),
        })
    }
}

// ── NetworkGateway ────────────────────────────────────────────────────────────

/// Default gateway configuration returned by `GetNetworkDefaultGateway`.
#[derive(Debug, Clone)]
pub struct NetworkGateway {
    pub ipv4_addresses: Vec<String>,
    pub ipv6_addresses: Vec<String>,
}

impl NetworkGateway {
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let gw = resp
            .child("NetworkGateway")
            .ok_or_else(|| SoapError::missing("NetworkGateway"))?;
        Ok(Self {
            ipv4_addresses: gw
                .children_named("IPv4Address")
                .filter_map(|n| {
                    let t = n.text().to_string();
                    if t.is_empty() { None } else { Some(t) }
                })
                .collect(),
            ipv6_addresses: gw
                .children_named("IPv6Address")
                .filter_map(|n| {
                    let t = n.text().to_string();
                    if t.is_empty() { None } else { Some(t) }
                })
                .collect(),
        })
    }
}

// ── SystemLog ─────────────────────────────────────────────────────────────────

/// System log content returned by `GetSystemLog`.
#[derive(Debug, Clone)]
pub struct SystemLog {
    /// Plain-text log content. `None` if the device returned binary data only.
    pub string: Option<String>,
}

impl SystemLog {
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        let log = resp
            .child("SystemLog")
            .ok_or_else(|| SoapError::missing("SystemLog"))?;
        Ok(Self {
            string: log
                .child("String")
                .map(|n| n.text().to_string())
                .filter(|s| !s.is_empty()),
        })
    }
}

// ── RelayOutput ───────────────────────────────────────────────────────────────

/// A relay output port returned by `GetRelayOutputs`.
#[derive(Debug, Clone)]
pub struct RelayOutput {
    pub token: String,
    /// `"Bistable"` (latching) or `"Monostable"` (timed).
    pub mode: String,
    /// ISO 8601 duration for monostable mode (e.g. `"PT1S"`).
    pub delay_time: String,
    /// Idle electrical state: `"closed"` or `"open"`.
    pub idle_state: String,
}

impl RelayOutput {
    pub(crate) fn vec_from_xml(resp: &XmlNode) -> Result<Vec<Self>, OnvifError> {
        resp.children_named("RelayOutputs")
            .map(|n| {
                let token = n
                    .attr("token")
                    .filter(|t| !t.is_empty())
                    .ok_or_else(|| SoapError::missing("RelayOutputs/@token"))?
                    .to_string();
                let props = n.child("Properties");
                let mode = props
                    .and_then(|p| p.child("Mode"))
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                let delay_time = props
                    .and_then(|p| p.child("DelayTime"))
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                let idle_state = props
                    .and_then(|p| p.child("IdleState"))
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                Ok(Self {
                    token,
                    mode,
                    delay_time,
                    idle_state,
                })
            })
            .collect()
    }
}

// ── StorageConfiguration ──────────────────────────────────────────────────────

/// A storage location (SD card, NAS, etc.) returned by `GetStorageConfigurations`.
#[derive(Debug, Clone)]
pub struct StorageConfiguration {
    pub token: String,
    /// `"LocalStorage"` or `"NFS"`.
    pub storage_type: String,
    /// Mount path on the device (e.g. `"/mnt/sd"`).
    pub local_path: String,
    /// Network URI for NFS shares.
    pub storage_uri: String,
    /// Username for authenticated shares (empty if anonymous or local).
    pub user: String,
    /// Whether anonymous access is used.
    pub use_anonymous: bool,
}

impl StorageConfiguration {
    pub(crate) fn vec_from_xml(resp: &XmlNode) -> Result<Vec<Self>, OnvifError> {
        resp.children_named("StorageConfigurations")
            .map(|n| {
                let token = n
                    .attr("token")
                    .filter(|t| !t.is_empty())
                    .ok_or_else(|| SoapError::missing("StorageConfigurations/@token"))?
                    .to_string();
                let storage_type = xml_str(n, "StorageType").unwrap_or_default();
                let local_path = xml_str(n, "LocalPath").unwrap_or_default();
                let storage_uri = xml_str(n, "StorageUri").unwrap_or_default();
                let user = n
                    .child("UserInfo")
                    .and_then(|u| u.child("Username"))
                    .map(|x| x.text().to_string())
                    .unwrap_or_default();
                let use_anonymous = n
                    .child("UserInfo")
                    .and_then(|u| u.child("UseAnonymous"))
                    .map(|x| x.text() == "true" || x.text() == "1")
                    .unwrap_or(false);
                Ok(Self {
                    token,
                    storage_type,
                    local_path,
                    storage_uri,
                    user,
                    use_anonymous,
                })
            })
            .collect()
    }
}

// ── SystemUris ────────────────────────────────────────────────────────────────

/// HTTP URIs for system management tasks returned by `GetSystemUris`.
#[derive(Debug, Clone)]
pub struct SystemUris {
    /// URI for uploading a firmware image.
    pub firmware_upgrade_uri: Option<String>,
    /// URI for downloading the system log.
    pub system_log_uri: Option<String>,
    /// URI for downloading a support-info bundle.
    pub support_info_uri: Option<String>,
}

impl SystemUris {
    pub(crate) fn from_xml(resp: &XmlNode) -> Result<Self, OnvifError> {
        Ok(Self {
            firmware_upgrade_uri: xml_str(resp, "FirmwareUpgrade")
                .or_else(|| xml_str(resp, "FirmwareUpgradeUri")),
            system_log_uri: xml_str(resp, "SystemLog").or_else(|| xml_str(resp, "SystemLogUri")),
            support_info_uri: xml_str(resp, "SupportInfo")
                .or_else(|| xml_str(resp, "SupportInfoUri")),
        })
    }
}