eave-iwd 0.1.1

Wi-Fi controls for eave plugins, through iwd's D-Bus API
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
//! Wi-Fi controls for eave plugins, through iwd's D-Bus API
//! ([D-42](https://github.com/eaveshell/eave/blob/main/docs/decisions.md)): the networks
//! in range, scanning, joining (with a password, through an agent eave serves), leaving,
//! forgetting, and turning Wi-Fi on and off.
//!
//! The kernel tells eave-net what the connection is; this is only for changing it. Without iwd, or
//! without permission to use it (iwd allows root and the `wheel` and `network` groups), [`watch`]
//! says so instead of failing, and the plugin shows what the kernel knows.

use std::collections::HashMap;

use eave_dbus::{Snapshot, Stream};
use zbus::Connection;
use zbus::zvariant::{OwnedObjectPath, OwnedValue};

mod agent;

pub use agent::connect_with;
pub use zbus;

const SERVICE: &str = "net.connman.iwd";
const DEVICE: &str = "net.connman.iwd.Device";
const STATION: &str = "net.connman.iwd.Station";
const NETWORK: &str = "net.connman.iwd.Network";
const KNOWN: &str = "net.connman.iwd.KnownNetwork";

/// What iwd says about Wi-Fi.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Wifi {
    pub access: Access,
    /// The first Wi-Fi device, when there is one.
    pub station: Option<Station>,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Access {
    /// iwd answers.
    Ready,
    /// iwd isn't running.
    #[default]
    Absent,
    /// iwd is running, but its bus policy turns us away.
    Denied,
}

/// A Wi-Fi device in station mode, and what it sees.
#[derive(Debug, Clone, PartialEq)]
pub struct Station {
    /// The device's object, for [`set_powered`], [`scan`] and [`disconnect`].
    pub device: OwnedObjectPath,
    /// Like `wlan0`.
    pub interface: String,
    /// The device's MAC address.
    pub address: String,
    pub powered: bool,
    /// iwd's word for it: `connected`, `connecting`, `disconnected`, `roaming`…
    pub state: String,
    pub scanning: bool,
    /// Strongest first, the connected one among them.
    pub networks: Vec<Network>,
}

/// A network in range.
#[derive(Debug, Clone, PartialEq)]
pub struct Network {
    pub path: OwnedObjectPath,
    pub name: String,
    pub security: Security,
    /// Signal, 0 to 100.
    pub strength: u8,
    /// Signal in dBm.
    pub signal: i16,
    /// Set when iwd remembers it.
    pub known: Option<Known>,
    pub connected: bool,
}

/// A network iwd remembers.
#[derive(Debug, Clone, PartialEq)]
pub struct Known {
    pub path: OwnedObjectPath,
    pub autoconnect: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Security {
    Open,
    /// A password (WPA personal).
    Psk,
    /// A company login (WPA enterprise), which needs more than a password.
    Eap,
    Wep,
}

impl Security {
    fn from_iwd(kind: &str) -> Self {
        match kind {
            "open" => Self::Open,
            "8021x" => Self::Eap,
            "wep" => Self::Wep,
            _ => Self::Psk,
        }
    }
}

/// What iwd says about the connected network, for a details page.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Diagnostics {
    /// The access point's MAC address.
    pub bssid: Option<String>,
    /// MHz.
    pub frequency: Option<u32>,
    pub channel: Option<u16>,
    /// Like `WPA2-Personal`.
    pub security: Option<String>,
    /// dBm.
    pub rssi: Option<i16>,
    /// Mbit/s.
    pub tx_rate: Option<f32>,
    pub rx_rate: Option<f32>,
}

/// Wi-Fi now, once: for a widget that wants to know what there is without watching it.
pub async fn read() -> Option<Wifi> {
    use futures_util::StreamExt;
    std::pin::pin!(watch()).next().await
}

/// Wi-Fi now, then again after every change iwd signals. Meant to be watched only while
/// something shows it, like an open popover: iwd is chatty while it scans.
pub fn watch() -> impl Stream<Item = Wifi> {
    eave_dbus::watch_all(SERVICE, snapshot)
}

async fn snapshot(connection: Connection) -> zbus::Result<Snapshot<Wifi>> {
    let objects = match eave_dbus::managed_objects(&connection, SERVICE, "/").await {
        Ok(objects) => objects,
        Err(error) => {
            let access = if eave_dbus::is_access_denied(&error) {
                Access::Denied
            } else if eave_dbus::is_unavailable(&error) {
                Access::Absent
            } else {
                return Err(error);
            };
            return Ok(Snapshot {
                state: Wifi {
                    access,
                    station: None,
                },
                watch: Vec::new(),
            });
        }
    };
    let objects = Objects::from(objects);
    let device = objects.first_device();
    let ordered = match device
        .as_ref()
        .filter(|device| objects.has(device, STATION))
    {
        Some(device) => ordered_networks(&connection, device)
            .await
            .unwrap_or_default(),
        None => Vec::new(),
    };
    let station = device.map(|device| objects.station(&device, &ordered));
    Ok(Snapshot {
        state: Wifi {
            access: Access::Ready,
            station,
        },
        watch: Vec::new(),
    })
}

/// The networks a station sees, strongest first, with their signal in hundredths of a dBm.
async fn ordered_networks(
    connection: &Connection,
    device: &OwnedObjectPath,
) -> zbus::Result<Vec<(OwnedObjectPath, i16)>> {
    let reply = connection
        .call_method(
            Some(SERVICE),
            device,
            Some(STATION),
            "GetOrderedNetworks",
            &(),
        )
        .await?;
    reply.body().deserialize()
}

type Properties = HashMap<String, OwnedValue>;

/// iwd's objects, by path, then interface.
struct Objects(HashMap<String, HashMap<String, Properties>>);

impl From<zbus::fdo::ManagedObjects> for Objects {
    fn from(objects: zbus::fdo::ManagedObjects) -> Self {
        Self(
            objects
                .into_iter()
                .map(|(path, interfaces)| {
                    let interfaces = interfaces
                        .into_iter()
                        .map(|(name, properties)| (name.to_string(), properties))
                        .collect();
                    (path.to_string(), interfaces)
                })
                .collect(),
        )
    }
}

fn get<T: TryFrom<OwnedValue> + Default>(properties: &Properties, name: &str) -> T {
    properties
        .get(name)
        .and_then(|value| value.try_clone().ok())
        .and_then(|value| T::try_from(value).ok())
        .unwrap_or_default()
}

fn object_path(properties: &Properties, name: &str) -> Option<OwnedObjectPath> {
    let value = properties.get(name)?.try_clone().ok()?;
    let path = OwnedObjectPath::try_from(value).ok()?;
    (path.as_str() != "/").then_some(path)
}

impl Objects {
    fn interface(&self, path: &str, interface: &str) -> Option<&Properties> {
        self.0.get(path)?.get(interface)
    }

    fn has(&self, path: &OwnedObjectPath, interface: &str) -> bool {
        self.interface(path.as_str(), interface).is_some()
    }

    /// The first Wi-Fi device in station mode, by path, so it's the same one every time.
    fn first_device(&self) -> Option<OwnedObjectPath> {
        let first = self
            .0
            .iter()
            .filter(|(_, interfaces)| {
                interfaces
                    .get(DEVICE)
                    .is_some_and(|device| get::<String>(device, "Mode") == "station")
            })
            .map(|(path, _)| path)
            .min()?;
        OwnedObjectPath::try_from(first.as_str()).ok()
    }

    fn station(&self, device: &OwnedObjectPath, ordered: &[(OwnedObjectPath, i16)]) -> Station {
        let empty = Properties::new();
        let properties = self.interface(device.as_str(), DEVICE).unwrap_or(&empty);
        let station = self.interface(device.as_str(), STATION);
        let networks = ordered
            .iter()
            .filter_map(|(path, signal)| self.network(path, *signal))
            .collect();
        Station {
            device: device.clone(),
            interface: get(properties, "Name"),
            address: get(properties, "Address"),
            powered: get(properties, "Powered"),
            state: station
                .map(|station| get(station, "State"))
                .unwrap_or_default(),
            scanning: station.is_some_and(|station| get(station, "Scanning")),
            networks,
        }
    }

    fn network(&self, path: &OwnedObjectPath, signal: i16) -> Option<Network> {
        let network = self.interface(path.as_str(), NETWORK)?;
        let known = object_path(network, "KnownNetwork").map(|known| {
            let autoconnect = self
                .interface(known.as_str(), KNOWN)
                .is_some_and(|properties| get(properties, "AutoConnect"));
            Known {
                path: known,
                autoconnect,
            }
        });
        // GetOrderedNetworks gives hundredths of a dBm.
        let dbm = signal / 100;
        Some(Network {
            path: path.clone(),
            name: get(network, "Name"),
            security: Security::from_iwd(&get::<String>(network, "Type")),
            strength: strength(dbm),
            signal: dbm,
            known,
            connected: get(network, "Connected"),
        })
    }
}

/// A signal in dBm as 0 to 100, the way eave-net reads the kernel's: -100 dBm is nothing, -50 is
/// full.
fn strength(dbm: i16) -> u8 {
    (2 * (i32::from(dbm) + 100)).clamp(0, 100) as u8
}

async fn call(
    path: &OwnedObjectPath,
    interface: &'static str,
    method: &'static str,
) -> zbus::Result<()> {
    let connection = eave_dbus::system().await?;
    connection
        .call_method(Some(SERVICE), path, Some(interface), method, &())
        .await?;
    Ok(())
}

async fn set(
    path: &OwnedObjectPath,
    interface: &'static str,
    name: &'static str,
    value: bool,
) -> zbus::Result<()> {
    let connection = eave_dbus::system().await?;
    let properties = zbus::fdo::PropertiesProxy::builder(&connection)
        .destination(SERVICE)?
        .path(path.as_ref())?
        .build()
        .await?;
    let interface = zbus::names::InterfaceName::try_from(interface)?;
    properties
        .set(interface, name, zbus::zvariant::Value::from(value))
        .await?;
    Ok(())
}

/// Looks for networks again. iwd answers at once and reports what it finds as it goes.
pub async fn scan(station: &Station) -> zbus::Result<()> {
    call(&station.device, STATION, "Scan").await
}

/// Joins a network iwd remembers, or an open one.
pub async fn connect(network: &Network) -> zbus::Result<()> {
    call(&network.path, NETWORK, "Connect").await
}

pub async fn disconnect(station: &Station) -> zbus::Result<()> {
    call(&station.device, STATION, "Disconnect").await
}

/// Forgets a remembered network: its password, and joining it on its own.
pub async fn forget(known: &Known) -> zbus::Result<()> {
    call(&known.path, KNOWN, "Forget").await
}

/// Whether iwd joins a remembered network on its own when it's in range.
pub async fn set_autoconnect(known: &Known, on: bool) -> zbus::Result<()> {
    set(&known.path, KNOWN, "AutoConnect", on).await
}

/// Turns the Wi-Fi device on or off.
pub async fn set_powered(station: &Station, on: bool) -> zbus::Result<()> {
    set(&station.device, DEVICE, "Powered", on).await
}

/// What iwd knows about the connection right now.
pub async fn diagnostics(station: &Station) -> zbus::Result<Diagnostics> {
    let connection = eave_dbus::system().await?;
    let reply = connection
        .call_method(
            Some(SERVICE),
            &station.device,
            Some("net.connman.iwd.StationDiagnostic"),
            "GetDiagnostics",
            &(),
        )
        .await?;
    let properties: Properties = reply.body().deserialize()?;
    let some = |properties: &Properties, name: &str| properties.contains_key(name);
    Ok(Diagnostics {
        bssid: some(&properties, "ConnectedBss").then(|| get(&properties, "ConnectedBss")),
        frequency: some(&properties, "Frequency").then(|| get(&properties, "Frequency")),
        channel: some(&properties, "Channel").then(|| get(&properties, "Channel")),
        security: some(&properties, "Security").then(|| get(&properties, "Security")),
        rssi: some(&properties, "RSSI").then(|| get(&properties, "RSSI")),
        // iwd reports bitrates in 100 kbit/s.
        tx_rate: some(&properties, "TxBitrate")
            .then(|| get::<u32>(&properties, "TxBitrate") as f32 / 10.0),
        rx_rate: some(&properties, "RxBitrate")
            .then(|| get::<u32>(&properties, "RxBitrate") as f32 / 10.0),
    })
}

/// A short reason an iwd call failed, fit to show under a password field.
pub fn reason(error: &zbus::Error) -> &'static str {
    let name = match error {
        zbus::Error::MethodError(name, ..) => name.as_str(),
        _ => return "Couldn't reach iwd",
    };
    match name.rsplit('.').next().unwrap_or("") {
        "InvalidFormat" => "That password isn't valid for this network",
        "Failed" | "AuthenticationFailed" => "Couldn't join. Check the password and try again",
        "Aborted" | "Canceled" => "Joining was cancelled",
        "Busy" | "InProgress" => "Already joining a network",
        "NotSupported" => "This network's security isn't supported",
        "NotConfigured" => "This network needs setting up first",
        "AccessDenied" => "Not allowed to change Wi-Fi",
        "Timeout" => "The network didn't answer",
        _ => "Couldn't join the network",
    }
}

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

    fn value<T: Into<zbus::zvariant::Value<'static>>>(value: T) -> OwnedValue {
        OwnedValue::try_from(value.into()).unwrap()
    }

    fn object(path: &str) -> OwnedObjectPath {
        OwnedObjectPath::try_from(path).unwrap()
    }

    fn fixture() -> Objects {
        let mut objects = HashMap::new();
        let mut device = HashMap::new();
        device.insert(
            DEVICE.to_string(),
            Properties::from([
                ("Name".into(), value("wlan0")),
                ("Address".into(), value("c8:89:f3:db:f8:c1")),
                ("Powered".into(), value(true)),
                ("Mode".into(), value("station")),
            ]),
        );
        device.insert(
            STATION.to_string(),
            Properties::from([
                ("State".into(), value("connected")),
                ("Scanning".into(), value(false)),
            ]),
        );
        objects.insert("/net/connman/iwd/0/2".to_string(), device);
        let network = |name: &str, kind: &str, connected: bool, known: Option<&str>| {
            let mut properties = Properties::from([
                ("Name".into(), value(name.to_string())),
                ("Type".into(), value(kind.to_string())),
                ("Connected".into(), value(connected)),
            ]);
            if let Some(known) = known {
                properties.insert("KnownNetwork".into(), value(object(known)));
            }
            HashMap::from([(NETWORK.to_string(), properties)])
        };
        objects.insert(
            "/net/connman/iwd/0/2/home_psk".into(),
            network("Home", "psk", true, Some("/net/connman/iwd/home_psk")),
        );
        objects.insert(
            "/net/connman/iwd/0/2/cafe_open".into(),
            network("Cafe", "open", false, None),
        );
        objects.insert(
            "/net/connman/iwd/home_psk".into(),
            HashMap::from([(
                KNOWN.to_string(),
                Properties::from([("AutoConnect".into(), value(true))]),
            )]),
        );
        Objects(objects)
    }

    #[test]
    fn reads_the_station_and_its_networks_in_order() {
        let objects = fixture();
        let device = objects.first_device().unwrap();
        assert_eq!(device.as_str(), "/net/connman/iwd/0/2");
        let ordered = [
            (object("/net/connman/iwd/0/2/home_psk"), -4100),
            (object("/net/connman/iwd/0/2/cafe_open"), -8000),
            (object("/net/connman/iwd/0/2/gone_psk"), -9000),
        ];
        let station = objects.station(&device, &ordered);
        assert_eq!(station.interface, "wlan0");
        assert!(station.powered);
        assert_eq!(station.state, "connected");
        assert_eq!(
            station.networks.len(),
            2,
            "a network that's gone is skipped"
        );
        let home = &station.networks[0];
        assert_eq!(home.name, "Home");
        assert!(home.connected);
        assert_eq!(home.security, Security::Psk);
        assert_eq!(home.signal, -41);
        assert_eq!(home.strength, 100);
        assert!(home.known.as_ref().is_some_and(|known| known.autoconnect));
        let cafe = &station.networks[1];
        assert_eq!(cafe.security, Security::Open);
        assert_eq!(cafe.known, None);
        assert_eq!(cafe.strength, 40);
    }

    /// Prints what iwd says on this machine. Reads only.
    /// `cargo test -p eave-iwd -- --ignored --nocapture`
    #[test]
    #[ignore = "needs iwd"]
    fn live() {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        runtime.block_on(async {
            use futures_util::StreamExt;
            let wifi = std::pin::pin!(watch()).next().await.unwrap();
            println!("{wifi:#?}");
            if let Some(station) = &wifi.station {
                println!("{:#?}", diagnostics(station).await);
            }
        });
    }

    #[test]
    fn signal_maps_like_the_kernels() {
        assert_eq!(strength(-100), 0);
        assert_eq!(strength(-75), 50);
        assert_eq!(strength(-50), 100);
        assert_eq!(strength(-30), 100);
        assert_eq!(strength(-110), 0);
    }

    #[test]
    fn errors_read_as_reasons() {
        let error = zbus::Error::MethodError(
            zbus::names::OwnedErrorName::try_from("net.connman.iwd.Failed").unwrap(),
            None,
            zbus::Message::method_call("/", "x")
                .unwrap()
                .build(&())
                .unwrap(),
        );
        assert_eq!(
            reason(&error),
            "Couldn't join. Check the password and try again"
        );
    }
}