Skip to main content

fritz_core/
capabilities.rs

1//! What a device offers and what its user may do there.
2
3use serde::Serialize;
4
5use crate::generic::Error;
6use crate::read::Reader;
7use crate::status::{self, Right};
8
9/// The features and the services that offer them. A device has a feature
10/// if it has one of the services.
11const FEATURES: [(&str, &[&str]); 17] = [
12    ("dsl", &["WANDSLInterfaceConfig"]),
13    ("mobile", &["X_AVM-DE_WANMobileConnection"]),
14    ("wlan", &["WLANConfiguration"]),
15    ("hosts", &["Hosts"]),
16    ("port_forwarding", &["WANPPPConnection", "WANIPConnection"]),
17    ("upnp", &["X_AVM-DE_UPnP"]),
18    ("parental_controls", &["X_AVM-DE_HostFilter"]),
19    ("telephony", &["X_VoIP", "X_AVM-DE_OnTel"]),
20    ("dect", &["X_AVM-DE_Dect"]),
21    ("answering_machine", &["X_AVM-DE_TAM"]),
22    ("smarthome", &["X_AVM-DE_Homeauto"]),
23    ("storage", &["X_AVM-DE_Storage"]),
24    ("webdav", &["X_AVM-DE_WebDAVClient"]),
25    ("powerline", &["X_AVM-DE_Homeplug"]),
26    ("myfritz", &["X_AVM-DE_MyFritz"]),
27    ("remote_access", &["X_AVM-DE_RemoteAccess"]),
28    ("speedtest", &["X_AVM-DE_Speedtest"]),
29];
30
31/// What the device offers and what the user may do.
32#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
33pub struct Capabilities {
34    /// The features the device offers, like `wlan` or `dect`. The rights
35    /// decide whether the user may use them.
36    pub features: Vec<String>,
37    /// What the user may do, like `BoxAdmin: readwrite`.
38    pub rights: Vec<Right>,
39    /// Whether changes need a confirmation on the device, like a button
40    /// press.
41    pub two_factor: Option<bool>,
42    /// The number of services of the device.
43    pub services: usize,
44}
45
46/// The features of a device with services of these names.
47pub fn features<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<String> {
48    let names: Vec<&str> = names.into_iter().collect();
49    FEATURES
50        .iter()
51        .filter(|(_, services)| services.iter().any(|s| names.contains(s)))
52        .map(|(feature, _)| (*feature).to_owned())
53        .collect()
54}
55
56/// Reads the capabilities.
57///
58/// # Errors
59///
60/// The errors of reading other than missing parts, see
61/// [`Reader::try_read`].
62pub async fn capabilities(reader: &mut Reader) -> Result<Capabilities, Error> {
63    let description = reader.description();
64    let features = features(description.services().map(tr064::Service::name));
65    let services = description.services().count();
66    let rights = reader
67        .try_read("LANConfigSecurity1", "X_AVM-DE_GetCurrentUser", &[])
68        .await?
69        .and_then(|values| values.text("NewX_AVM-DE_CurrentUserRights"))
70        .map_or_else(Vec::new, |xml| status::rights(&xml));
71    let two_factor = reader
72        .try_read("X_AVM-DE_Auth1", "GetInfo", &[])
73        .await?
74        .and_then(|values| values.flag("NewEnabled"));
75    Ok(Capabilities {
76        features,
77        rights,
78        two_factor,
79        services,
80    })
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn names_features_by_their_services() {
89        assert_eq!(
90            features(["WANIPConnection", "X_AVM-DE_Dect", "Other"]),
91            ["port_forwarding", "dect"]
92        );
93        assert_eq!(features([]), Vec::<String>::new());
94    }
95}