fritz-core 0.1.0

FRITZ!Box specific logic for the fritz CLI and MCP server (unofficial, not affiliated with FRITZ! GmbH)
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
//! The state of a device, read through actions that only read: an
//! overview, the hosts, the mesh, the wireless networks and the internet
//! connection.
//!
//! Parts the device lacks, or that the user may not read, are left out
//! instead of failing the whole read.

use serde::Serialize;

use crate::generic::Error;
use crate::lists::{self, Mesh};
use crate::read::{Reader, Values};

/// The most hosts read, as a guard against a device that never ends the
/// list.
pub const MAX_HOSTS: u32 = 1000;

/// The device, its connection and the rights of the user.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Overview {
    pub model: Option<String>,
    pub firmware: Option<String>,
    /// Seconds since the device started.
    pub uptime: Option<u64>,
    pub update_available: Option<bool>,
    /// Whether changes need a confirmation on the device, like a button
    /// press.
    pub two_factor: Option<bool>,
    /// What the user may do, like `BoxAdmin: readwrite`.
    pub rights: Vec<Right>,
    pub internet: Option<Connection>,
}

/// An area of the device and the access of the user to it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Right {
    pub area: String,
    pub access: String,
}

/// The connection to the internet.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Connection {
    /// Like `DSL`, `Ethernet` or `X_AVM-DE_Mobile`.
    pub access_type: Option<String>,
    /// The physical link, like `Up`.
    pub link: Option<String>,
    /// Like `Connected`.
    pub status: Option<String>,
    pub external_address: Option<String>,
    /// Seconds since the connection was made.
    pub uptime: Option<u64>,
}

/// The internet connection in detail.
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct Internet {
    #[serde(flatten)]
    pub connection: Connection,
    pub provider: Option<String>,
    pub dns_servers: Vec<String>,
    /// The speed of the physical link, in kbit/s.
    pub downstream_max: Option<u64>,
    pub upstream_max: Option<u64>,
    pub dsl: Option<Dsl>,
}

/// The DSL line.
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct Dsl {
    pub status: Option<String>,
    /// The synchronized and the attainable rates, in kbit/s.
    pub downstream_rate: Option<u64>,
    pub upstream_rate: Option<u64>,
    pub downstream_max: Option<u64>,
    pub upstream_max: Option<u64>,
    /// In dB.
    pub downstream_noise_margin: Option<f64>,
    pub upstream_noise_margin: Option<f64>,
    pub downstream_attenuation: Option<f64>,
    pub upstream_attenuation: Option<f64>,
}

/// A host the device knows, active or not.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Host {
    pub name: Option<String>,
    pub ip: Option<String>,
    pub mac: Option<String>,
    pub active: bool,
    /// Like `Ethernet` or `802.11`.
    pub interface: Option<String>,
    /// Like `DHCP` or `Static`. Only in the entries, not in the host list.
    pub address_source: Option<String>,
    /// Whether the host is in the guest network. Only in the host list.
    pub guest: Option<bool>,
    /// Whether the host may use the internet: `granted`, `denied` or
    /// `error`. Only in the host list.
    pub internet_access: Option<String>,
    /// The speed of the connection to the host, in Mbit/s. Only in the
    /// host list.
    pub speed: Option<u32>,
}

/// The hosts and how many the device reported.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Hosts {
    /// The number of hosts the device reported. More than the hosts if it
    /// stopped answering before.
    pub reported: u32,
    pub hosts: Vec<Host>,
}

/// A wireless network.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Wlan {
    /// The service, like `WLANConfiguration1`.
    pub service: String,
    pub ssid: Option<String>,
    /// Like `2.4 GHz`.
    pub band: Option<String>,
    pub channel: Option<u32>,
    /// Like `ax` or `be`.
    pub standard: Option<String>,
    pub enabled: Option<bool>,
    /// Like `Up` or `Disabled`.
    pub status: Option<String>,
    /// The number of connected clients.
    pub clients: Option<u32>,
}

/// Reads the overview.
///
/// # Errors
///
/// The errors of reading other than missing parts, see
/// [`Reader::try_read`].
pub async fn overview(reader: &mut Reader) -> Result<Overview, Error> {
    let info = reader.try_read("DeviceInfo1", "GetInfo", &[]).await?;
    let interface = reader.try_read("UserInterface1", "GetInfo", &[]).await?;
    let auth = reader.try_read("X_AVM-DE_Auth1", "GetInfo", &[]).await?;
    let user = reader
        .try_read("LANConfigSecurity1", "X_AVM-DE_GetCurrentUser", &[])
        .await?;
    let rights = user
        .and_then(|u| u.text("NewX_AVM-DE_CurrentUserRights"))
        .map_or_else(Vec::new, |xml| rights(&xml));
    let info = info.unwrap_or_default();
    Ok(Overview {
        model: info.text("NewModelName"),
        firmware: info.text("NewSoftwareVersion"),
        uptime: info.number("NewUpTime"),
        update_available: interface.and_then(|i| i.flag("NewUpgradeAvailable")),
        two_factor: auth.and_then(|a| a.flag("NewEnabled")),
        rights,
        internet: Some(connection(reader).await?.0).filter(|c| *c != Connection::default()),
    })
}

/// Parses rights like
/// `<rights><path>BoxAdmin</path><access>readwrite</access></rights>`.
pub(crate) fn rights(xml: &str) -> Vec<Right> {
    let Ok(document) = roxmltree::Document::parse(xml) else {
        return Vec::new();
    };
    let mut rights = Vec::new();
    let mut area = None;
    for node in document
        .root_element()
        .children()
        .filter(roxmltree::Node::is_element)
    {
        let text = node.text().unwrap_or_default().trim().to_owned();
        match node.tag_name().name() {
            "path" => area = Some(text),
            "access" => {
                if let Some(area) = area.take() {
                    rights.push(Right { area, access: text });
                }
            }
            _ => {}
        }
    }
    rights
}

/// Reads the connection, and the answer of the connection service for
/// the details.
async fn connection(reader: &mut Reader) -> Result<(Connection, Values), Error> {
    let common = reader
        .try_read("WANCommonInterfaceConfig1", "GetCommonLinkProperties", &[])
        .await?
        .unwrap_or_default();
    // Connections over PPP, like most DSL lines, are in WANPPPConnection,
    // the others in WANIPConnection.
    let mut details = Values::default();
    for service in ["WANPPPConnection1", "WANIPConnection1"] {
        if let Some(values) = reader.try_read(service, "GetInfo", &[]).await? {
            details = values;
            break;
        }
    }
    let connection = Connection {
        access_type: common.text("NewWANAccessType"),
        link: common.text("NewPhysicalLinkStatus"),
        status: details.text("NewConnectionStatus"),
        external_address: details.text("NewExternalIPAddress"),
        uptime: details.number("NewUptime"),
    };
    let details = Values(common.0.into_iter().chain(details.0).collect());
    Ok((connection, details))
}

/// Reads the internet connection.
///
/// # Errors
///
/// The errors of reading other than missing parts, see
/// [`Reader::try_read`].
pub async fn internet(reader: &mut Reader) -> Result<Internet, Error> {
    let (connection, details) = connection(reader).await?;
    let provider = reader
        .try_read(
            "WANCommonInterfaceConfig1",
            "X_AVM-DE_GetActiveProvider",
            &[],
        )
        .await?;
    let dsl = reader
        .try_read("WANDSLInterfaceConfig1", "GetInfo", &[])
        .await?;
    let kbits = |name| details.number::<u64>(name).map(|b| b / 1000);
    Ok(Internet {
        provider: provider.and_then(|p| p.text("NewX_AVM-DE_Provider")),
        dns_servers: details.get("NewDNSServers").map_or_else(Vec::new, |s| {
            s.split(',')
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_owned)
                .collect()
        }),
        downstream_max: kbits("NewLayer1DownstreamMaxBitRate"),
        upstream_max: kbits("NewLayer1UpstreamMaxBitRate"),
        dsl: dsl
            .filter(|_| connection.access_type.as_deref() == Some("DSL"))
            .map(|d| dsl_line(&d)),
        connection,
    })
}

/// The DSL line from the answer of `WANDSLInterfaceConfig:GetInfo`, whose
/// noise margins and attenuations are in tenths of a dB.
fn dsl_line(values: &Values) -> Dsl {
    let decibels = |name| values.number::<i64>(name).map(tenths);
    Dsl {
        status: values.text("NewStatus"),
        downstream_rate: values.number("NewDownstreamCurrRate"),
        upstream_rate: values.number("NewUpstreamCurrRate"),
        downstream_max: values.number("NewDownstreamMaxRate"),
        upstream_max: values.number("NewUpstreamMaxRate"),
        downstream_noise_margin: decibels("NewDownstreamNoiseMargin"),
        upstream_noise_margin: decibels("NewUpstreamNoiseMargin"),
        downstream_attenuation: decibels("NewDownstreamAttenuation"),
        upstream_attenuation: decibels("NewUpstreamAttenuation"),
    }
}

/// A value in tenths as a decimal, like 70 as 7.0.
#[expect(clippy::cast_precision_loss, reason = "tenths of a dB are small")]
fn tenths(value: i64) -> f64 {
    value as f64 / 10.0
}

/// Reads the hosts from the host list, in one request. Without a host list,
/// it reads them one call per host, up to [`MAX_HOSTS`], and stops early at
/// the first index the device has no entry for.
///
/// # Errors
///
/// The errors of reading other than missing parts, see
/// [`Reader::try_read`], and [`Error::InvalidDocument`] for a host list
/// that cannot be parsed.
pub async fn hosts(reader: &mut Reader) -> Result<Hosts, Error> {
    if let Some(xml) = reader
        .document(
            "Hosts1",
            "X_AVM-DE_GetHostListPath",
            "NewX_AVM-DE_HostListPath",
        )
        .await?
    {
        let hosts = lists::hosts(&xml)?;
        return Ok(Hosts {
            reported: u32::try_from(hosts.len()).unwrap_or(u32::MAX),
            hosts,
        });
    }
    let Some((reported, entries)) = reader
        .entries(
            "Hosts1",
            ("GetHostNumberOfEntries", "NewHostNumberOfEntries"),
            ("GetGenericHostEntry", "NewIndex"),
            MAX_HOSTS,
        )
        .await?
    else {
        return Ok(Hosts::default());
    };
    let hosts = entries
        .iter()
        .map(|entry| Host {
            name: entry.text("NewHostName"),
            ip: entry.text("NewIPAddress"),
            mac: entry.text("NewMACAddress"),
            active: entry.flag("NewActive").unwrap_or_default(),
            interface: entry.text("NewInterfaceType"),
            address_source: entry.text("NewAddressSource"),
            ..Host::default()
        })
        .collect();
    Ok(Hosts { reported, hosts })
}

/// Reads the mesh from the mesh list. `None` if the device has none.
///
/// # Errors
///
/// The errors of reading other than missing parts, see
/// [`Reader::try_read`], and [`Error::InvalidDocument`] for a mesh list
/// that cannot be parsed.
pub async fn mesh(reader: &mut Reader) -> Result<Option<Mesh>, Error> {
    reader
        .document(
            "Hosts1",
            "X_AVM-DE_GetMeshListPath",
            "NewX_AVM-DE_MeshListPath",
        )
        .await?
        .map(|json| lists::mesh(&json))
        .transpose()
}

/// Reads the wireless networks.
///
/// # Errors
///
/// The errors of reading other than missing parts, see
/// [`Reader::try_read`].
pub async fn wlans(reader: &mut Reader) -> Result<Vec<Wlan>, Error> {
    let mut wlans = Vec::new();
    for service in reader.services("WLANConfiguration") {
        let Some(info) = reader.try_read(&service, "GetInfo", &[]).await? else {
            continue;
        };
        let clients = reader
            .try_read(&service, "GetTotalAssociations", &[])
            .await?
            .and_then(|v| v.number("NewTotalAssociations"));
        wlans.push(Wlan {
            band: info.get("NewX_AVM-DE_FrequencyBand").and_then(band),
            ssid: info.text("NewSSID"),
            channel: info.number("NewChannel"),
            standard: info.text("NewStandard"),
            enabled: info.flag("NewEnable"),
            status: info.text("NewStatus"),
            clients,
            service,
        });
    }
    Ok(wlans)
}

/// The frequency band in MHz as text, like `2400` as `2.4 GHz`.
fn band(megahertz: &str) -> Option<String> {
    match megahertz {
        "2400" => Some("2.4 GHz".to_owned()),
        "5000" => Some("5 GHz".to_owned()),
        "6000" => Some("6 GHz".to_owned()),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_rights() {
        let xml = "<rights><path>BoxAdmin</path><access>readwrite</access>\
                   <access>orphan</access><other/><path>NAS</path><access>read</access></rights>";
        assert_eq!(
            rights(xml),
            [
                Right {
                    area: "BoxAdmin".to_owned(),
                    access: "readwrite".to_owned()
                },
                Right {
                    area: "NAS".to_owned(),
                    access: "read".to_owned()
                },
            ]
        );
        assert_eq!(rights("<rights"), []);
    }

    #[test]
    fn names_bands() {
        assert_eq!(band("2400").as_deref(), Some("2.4 GHz"));
        assert_eq!(band("5000").as_deref(), Some("5 GHz"));
        assert_eq!(band("6000").as_deref(), Some("6 GHz"));
        assert_eq!(band("unknown"), None);
    }
}