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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! The VAPIX v3 parameters interface at `/axis-cgi/param.cgi`.

use crate::{Device, Error, Transport};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::fmt;
use std::str::FromStr;

/// A device's legacy parameters API.
pub struct Parameters<'a, T: Transport>(&'a Device<T>, String);

impl<'a, T: Transport> Parameters<'a, T> {
    pub(crate) fn new(device: &'a Device<T>, api_version: String) -> Self {
        Self(device, api_version)
    }

    /// List parameters, including their definitions and current values.
    ///
    /// If `groups` is provided, return a subset of the parameter tree.
    pub async fn list_definitions(
        &self,
        groups: Option<&[&str]>,
    ) -> Result<ParameterDefinitions, Error<T::Error>> {
        let req = http::Request::builder()
            .method(http::Method::GET)
            .uri(
                self.0
                    .uri_for_args(
                        "/axis-cgi/param.cgi",
                        ListParams {
                            action: "listdefinitions",
                            list_format: Some("xmlschema"),
                            groups,
                        },
                    )
                    .unwrap(),
            )
            .body(Vec::new())
            .unwrap();

        let (_resp, resp_body) = self.0.roundtrip(req, "text/xml").await?;

        let resp_body =
            std::str::from_utf8(resp_body.as_slice()).map_err(|_| Error::Other("invalid UTF-8"))?;

        let params: ParameterDefinitions = quick_xml::de::from_str(resp_body)?;

        Ok(params)
    }

    /// List parameters, including their current values.
    ///
    /// If `groups` is provided, return a subset of the parameter tree.
    pub async fn list(
        &self,
        groups: Option<&[&str]>,
    ) -> Result<BTreeMap<String, String>, Error<T::Error>> {
        let req = http::request::Builder::new()
            .method(http::Method::GET)
            .uri(
                self.0
                    .uri_for_args(
                        "/axis-cgi/param.cgi",
                        ListParams {
                            action: "list",
                            list_format: None,
                            groups,
                        },
                    )
                    .unwrap(),
            )
            .body(Vec::new())
            .unwrap();

        let (_, body) = self.0.roundtrip(req, "text/plain").await?;
        Ok(body
            .as_slice()
            .split(|byte| *byte == b'\n')
            .filter_map(|line| {
                let line = std::str::from_utf8(line).unwrap_or("");
                let mut parts = line.splitn(2, '=');
                match (parts.next(), parts.next()) {
                    (Some(key), Some(value)) => Some((key.to_string(), value.to_string())),
                    _ => None,
                }
            })
            .collect())
    }

    // todo: ?action=add, optional force=yes
    // The force parameter can be used to exceed limits set for adding dynamic parameter groups.
    // Example: Axis products can be configured for up to 10 event types. The force parameter can be used to exceed this maximum number of events.

    // todo action=remove

    /// Attempt to update one or more parameters.
    ///
    /// TODO: what happens with partial failure?
    pub async fn update<I: IntoIterator<Item = (K, V)>, K: AsRef<str>, V: AsRef<str>>(
        &self,
        parameters: I,
    ) -> Result<(), Error<T::Error>> {
        let mut query_params: BTreeMap<String, String> = parameters
            .into_iter()
            .map(move |(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
            .collect();
        query_params.insert("action".into(), "update".into());

        assert!(!query_params.is_empty());

        let req = http::request::Builder::new()
            .method(http::Method::GET)
            .uri(
                self.0
                    .uri_for_args("/axis-cgi/param.cgi", query_params)
                    .unwrap(),
            )
            .body(Vec::new())
            .unwrap();

        let (_, body) = self.0.roundtrip(req, "text/plain").await?;
        if body.as_slice() == b"OK" {
            Ok(())
        } else if body.as_slice().starts_with(b"# ") {
            // xxx: body contains error message
            Err(Error::Other("call failed for specific reason"))
        } else {
            Err(Error::Other("call failed for unknown reason"))
        }
    }
}

#[derive(Serialize)]
struct ListParams<'a> {
    action: &'a str,
    #[serde(skip_serializing_if = "Option::is_none", rename = "listformat")]
    list_format: Option<&'a str>,
    #[serde(
        rename = "group",
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_list_params_groups"
    )]
    groups: Option<&'a [&'a str]>,
}

fn serialize_list_params_groups<S>(groups: &Option<&[&str]>, ser: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    match groups {
        Some(groups) => {
            let groups = groups.join(",");
            ser.serialize_str(&groups)
        }
        None => unreachable!(),
    }
}

/// A set of parameter definitions.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParameterDefinitions {
    /// The version of the data structures used to describe the parameter definitions.
    ///
    /// In practice, always `"1.0"`.
    #[serde(rename = "version")]
    pub schema_version: String,

    /// The name of the device model.
    pub model: Option<String>,

    /// The version of firmware running on the device.
    pub firmware_version: Option<String>,

    /// Parameter groups provided by this device.
    #[serde(rename = "group")]
    pub groups: Vec<ParameterGroupDefinition>,
}

/// A group of parameter definitions.
///
/// May contain parameters or additional groups.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParameterGroupDefinition {
    /// The name of the parameter group.
    pub name: String,

    /// Purpose unknown.
    pub max_groups: Option<u32>,

    /// The parameter groups nested within this parameter group.
    #[serde(rename = "group", default)]
    pub groups: Vec<ParameterGroupDefinition>,

    /// The parameters nested within this parameter group.
    #[serde(rename = "parameter", default)]
    pub parameters: Vec<ParameterDefinition>,
}

impl ParameterGroupDefinition {
    /// Find a nested parameter group by name.
    pub fn group(&self, name: &str) -> Option<&ParameterGroupDefinition> {
        self.groups.iter().find(|g| g.name == name)
    }

    /// Find a nested parameter by name.
    pub fn parameter(&self, name: &str) -> Option<&ParameterDefinition> {
        self.parameters.iter().find(|g| g.name == name)
    }
}

/// A parameter definition.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParameterDefinition {
    /// The name of the parameter.
    pub name: String,

    /// The current value of the parameter, if any, expressed as a string.
    #[serde(rename = "value")]
    pub current_value: Option<String>,

    /// The security level of the parameter.
    pub security_level: Option<u32>, // FIXME: this is a 4-digit octal string. What does it mean?

    /// The name to display to the user, if different from `name`.
    pub nice_name: Option<String>,

    /// The type of this parameter, if provided.
    #[serde(rename = "type")]
    pub parameter_type: Option<ParameterTypeDefinition>,
}

impl ParameterDefinition {
    /// Return this parameter as a `bool`. Returns `None` if this parameter has no `current_value`,
    /// has no `parameter_type`, has a `parameter_type` with a `type_definition` other than
    /// `TypeDefinition::Bool`, or if `current_value` is neither `true_value` nor `false_value`.
    pub fn as_bool(&self) -> Option<bool> {
        match (self.current_value.as_ref(), self.parameter_type.as_ref()) {
            (
                Some(value),
                Some(ParameterTypeDefinition {
                    type_definition: TypeDefinition::Bool(td),
                    ..
                }),
            ) => {
                if value == &td.true_value {
                    Some(true)
                } else if value == &td.false_value {
                    Some(false)
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct SecurityLevel {
    pub create: AccessLevel,
    pub delete: AccessLevel,
    pub read: AccessLevel,
    pub write: AccessLevel,
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum BadSecurityLevelError {
    BadAccessLevel(BadAccessLevelError),
    WrongLength(String),
}

impl fmt::Display for BadSecurityLevelError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            BadSecurityLevelError::BadAccessLevel(BadAccessLevelError(c)) => {
                write!(f, "expected access level digit, got '{}'", c)
            }
            BadSecurityLevelError::WrongLength(str) => {
                write!(f, "expected 4 digits, got {:?}", str)
            }
        }
    }
}

impl From<BadAccessLevelError> for BadSecurityLevelError {
    fn from(l: BadAccessLevelError) -> Self {
        BadSecurityLevelError::BadAccessLevel(l)
    }
}

impl FromStr for SecurityLevel {
    type Err = BadSecurityLevelError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut chars = s.chars();
        fn next(
            s: &str,
            chars: &mut std::str::Chars,
        ) -> Result<AccessLevel, BadSecurityLevelError> {
            chars
                .next()
                .ok_or_else(|| BadSecurityLevelError::WrongLength(s.into()))
                .and_then(|c| AccessLevel::try_from(c).map_err(|e| e.into()))
        }

        let security_level = Self {
            create: next(s, &mut chars)?,
            delete: next(s, &mut chars)?,
            read: next(s, &mut chars)?,
            write: next(s, &mut chars)?,
        };

        if chars.next().is_none() {
            Ok(security_level)
        } else {
            Err(BadSecurityLevelError::WrongLength(s.into()))
        }
    }
}

impl fmt::Display for SecurityLevel {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use std::fmt::Write;
        f.write_char(self.create.into())?;
        f.write_char(self.delete.into())?;
        f.write_char(self.read.into())?;
        f.write_char(self.write.into())
    }
}

impl<'de> serde::de::Deserialize<'de> for SecurityLevel {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        FromStr::from_str(&s).map_err(serde::de::Error::custom)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum AccessLevel {
    /// Not subject to access control.
    Unprotected,
    /// Accessible to viewers, operators, or administrators.
    ViewerAccess,
    /// Accessible to operators or administrators.
    OperatorAccess,
    /// Accessible to administrators.
    AdministratorAccess,
    /// Root access. Internal parameters that can be changed by firmware applications or by root
    /// editing the configuration files directly.
    RootAccess,
}

impl From<AccessLevel> for char {
    fn from(al: AccessLevel) -> Self {
        match al {
            AccessLevel::Unprotected => '0',
            AccessLevel::ViewerAccess => '1',
            AccessLevel::OperatorAccess => '4',
            AccessLevel::AdministratorAccess => '6',
            AccessLevel::RootAccess => '7',
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct BadAccessLevelError(char);

impl TryFrom<char> for AccessLevel {
    type Error = BadAccessLevelError;

    fn try_from(value: char) -> Result<Self, Self::Error> {
        match value {
            '0' => Ok(AccessLevel::Unprotected),
            '1' => Ok(AccessLevel::ViewerAccess),
            '4' => Ok(AccessLevel::OperatorAccess),
            '6' => Ok(AccessLevel::AdministratorAccess),
            '7' => Ok(AccessLevel::RootAccess),
            other => Err(BadAccessLevelError(other)),
        }
    }
}

/// A parameter type definition, describing flags and type information.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParameterTypeDefinition {
    /// Is this parameter read-only?
    #[serde(rename = "readonly")]
    pub read_only: Option<bool>,

    /// Is this parameter write-only?
    #[serde(rename = "writeonly")]
    pub write_only: Option<bool>,

    /// Should this parameter be displayed?
    pub hidden: Option<bool>,

    /// Is this parameter constant?
    ///
    /// (FIXME: How does this differ from `read_only`?)
    #[serde(rename = "const")]
    pub constant: Option<bool>,

    /// Purpose unknown.
    #[serde(rename = "nosync")]
    pub no_sync: Option<bool>,

    /// Purpose unknown.
    pub internal: Option<bool>,

    /// The type definition of this parameter, describing its domain and encoding.
    #[serde(rename = "$value")]
    pub type_definition: TypeDefinition,
}

/// A type definition, describing a parameter's domain and encoding.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TypeDefinition {
    /// A string, to be displayed as a text box.
    String(StringParameterDefinition),
    /// A string, to be displayed as a password box.
    Password(PasswordParameterDefinition),
    /// An integer, to be displayed as a text box.
    Int(IntParameterDefinition),
    /// An enumeration, to be displayed as a select box.
    Enum(EnumParameterDefinition),
    /// A boolean, to be displayed as a select box.
    Bool(BoolParameterDefinition),
    /// An IP address.
    ///
    /// FIXME: IPv4 or IPv6?
    Ip,
    /// A list of IP addresses.
    ///
    /// FIXME: encoding?
    IpList,
    /// A hostname.
    ///
    /// FIXME: details?
    Hostname,
    /// A string, to be displayed as a multiline text box.
    TextArea,
}

/// String parameter definition details.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StringParameterDefinition {
    /// The maximum length of the string.
    #[serde(rename = "maxlen")]
    pub max_len: Option<u32>,
}

/// Password parameter definition details.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PasswordParameterDefinition {
    /// The maximum length of the string.
    #[serde(rename = "maxlen")]
    pub max_len: Option<u32>,
}

/// Integer parameter definition details.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntParameterDefinition {
    /// The minimum value of the integer.
    pub min: Option<i64>,
    /// The maximum value of the integer.
    pub max: Option<i64>,
    /// The maximum length of the integer as a string.
    #[serde(rename = "maxlen")]
    pub max_len: Option<u8>,
    /// Range(s) in which the integer must be contained.
    pub range_entries: Option<Vec<IntParameterRangeDefinition>>,
}

/// Integer parameter range definiton details.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntParameterRangeDefinition {
    /// TODO: parse "0" and "1024-65534" into something more appropriate
    pub value: String,
}

/// Enumeration parameter definition details.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EnumParameterDefinition {
    /// A list of entries from which the parameter value must be selected.
    #[serde(rename = "entry")]
    pub values: Vec<EnumEntryDefinition>,
}

/// An enumeration entry.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EnumEntryDefinition {
    /// The value of the parameter.
    pub value: String,
    /// The value to display to the user, if different from `value`.
    pub nice_value: Option<String>,
}

/// Boolean parameter definition details.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BoolParameterDefinition {
    /// The string value used to represent `true`.
    #[serde(rename = "true")]
    pub true_value: String,
    /// The string value used to represent `false`.
    #[serde(rename = "false")]
    pub false_value: String,
}

#[cfg(test)]
mod tests {
    #[test]
    fn list() {
        crate::test_with_devices(|test_device| async move {
            let parameters = test_device.device.parameters();

            let all_params = parameters.list(None).await?;

            let brand_params = parameters.list(Some(&["root.Brand"])).await?;
            let brand_and_firmware_params = parameters
                .list(Some(&["root.Brand", "root.Properties.Firmware"]))
                .await?;

            assert!(all_params.len() > 0);

            assert!(
                all_params.len() > brand_params.len(),
                "all_params.len() = {} is not greater than brand_params.len() = {}",
                all_params.len(),
                brand_params.len()
            );
            assert!(
                all_params.len() > brand_and_firmware_params.len(),
                "all_params.len() = {} is not greater than brand_and_firmware_params.len() = {}",
                all_params.len(),
                brand_and_firmware_params.len()
            );
            assert!(
                brand_and_firmware_params.len() > brand_params.len(),
                "brand_and_firmware_params.len() = {} is not greater than brand_params.len() = {}",
                brand_and_firmware_params.len(),
                brand_params.len()
            );

            Ok(())
        });
    }

    #[test]
    fn list_definitions() {
        crate::test_with_devices(|test_device| async move {
            let parameters = test_device.device.parameters();

            let all_params = parameters.list_definitions(None).await?;

            let brand_params = parameters.list_definitions(Some(&["root.Brand"])).await?;
            let brand_and_firmware_params = parameters
                .list_definitions(Some(&["root.Brand", "root.Properties.Firmware"]))
                .await?;

            assert_eq!(all_params.groups.len(), 1);
            assert_eq!(brand_params.groups.len(), 1);
            assert_eq!(brand_and_firmware_params.groups.len(), 1);

            assert_eq!(all_params.model, brand_params.model);
            assert_eq!(all_params.model, brand_and_firmware_params.model);

            assert_eq!(all_params.firmware_version, brand_params.firmware_version);
            assert_eq!(
                all_params.firmware_version,
                brand_and_firmware_params.firmware_version
            );

            assert!(all_params.groups[0].groups.len() > 2);
            assert_eq!(brand_params.groups[0].groups.len(), 1);
            assert_eq!(brand_and_firmware_params.groups[0].groups.len(), 2);

            Ok(())
        });
    }

    #[tokio::test]
    async fn update() {
        let device = crate::mock_device(|req| {
            assert_eq!(req.method(), http::Method::GET);
            assert_eq!(
                req.uri().path_and_query().map(|pq| pq.as_str()),
                Some("/axis-cgi/param.cgi?action=update&foo.bar=baz+quxx")
            );

            http::Response::builder()
                .status(http::StatusCode::OK)
                .header(http::header::CONTENT_TYPE, "text/plain")
                .body(vec![b"OK".to_vec()])
        });

        let response = device
            .parameters()
            .update(vec![("foo.bar", "baz quxx")])
            .await;
        match response {
            Ok(()) => {}
            Err(e) => panic!("update should succeed: {}", e),
        };

        let device = crate::mock_device(|req| {
            assert_eq!(req.method(), http::Method::GET);
            assert_eq!(
                req.uri().path_and_query().map(|pq| pq.as_str()),
                Some("/axis-cgi/param.cgi?action=update&foo.bar=baz+quxx")
            );

            http::Response::builder()
                .status(http::StatusCode::OK)
                .header(http::header::CONTENT_TYPE, "text/plain")
                .body(vec![
                    b"# Error: Error setting 'foo.bar' to 'baz quxx'!".to_vec()
                ])
        });

        let response = device
            .parameters()
            .update(vec![("foo.bar", "baz quxx")])
            .await;
        match response {
            Err(crate::Error::Other(_)) => {}
            Ok(()) => panic!("update should fail"),
            Err(e) => panic!("update should fail with a different error: {}", e),
        };
    }
}