hal-9001 0.2.0

HAL-9001 — Central TUI de Controle do Sistema & Assistente de Sistema (Rust/ratatui)
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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
use std::collections::HashMap;
use std::time::{Duration, Instant};

use tokio::sync::broadcast;
use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value};
use zbus::Connection;

use crate::events::{Action, AppEvent, DeviceId, EventTx, Toast};
use crate::i18n::SharedLang;

const NM_SERVICE: &str = "org.freedesktop.NetworkManager";
const NM_PATH: &str = "/org/freedesktop/NetworkManager";
const NM_IFACE: &str = "org.freedesktop.NetworkManager";
const NM_DEVICE_IFACE: &str = "org.freedesktop.NetworkManager.Device";
const NM_WIRELESS_IFACE: &str = "org.freedesktop.NetworkManager.Device.Wireless";
const NM_AP_IFACE: &str = "org.freedesktop.NetworkManager.AccessPoint";
const NM_SETTINGS_PATH: &str = "/org/freedesktop/NetworkManager/Settings";
const NM_SETTINGS_IFACE: &str = "org.freedesktop.NetworkManager.Settings";
const NM_CONN_IFACE: &str = "org.freedesktop.NetworkManager.Settings.Connection";
const NM_ACTIVE_CONN_IFACE: &str = "org.freedesktop.NetworkManager.Connection.Active";
const NM_IP4_IFACE: &str = "org.freedesktop.NetworkManager.IP4Config";

const NM_DEVICE_TYPE_WIFI: u32 = 2;

const NM_802_11_AP_FLAGS_PRIVACY: u32 = 0x1;
const NM_SEC_KEY_MGMT_PSK: u32 = 0x100;
const NM_SEC_KEY_MGMT_8021X: u32 = 0x200;
const NM_SEC_KEY_MGMT_SAE: u32 = 0x400;
const NM_SEC_KEY_MGMT_OWE: u32 = 0x800;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Security {
    Open,
    Wep,
    Wpa,
    Wpa2,
    Wpa3,
    Wpa2Enterprise,
    Owe,
}

impl Security {
    pub fn label(self) -> &'static str {
        match self {
            Security::Open => "Aberta",
            Security::Wep => "WEP",
            Security::Wpa => "WPA",
            Security::Wpa2 => "WPA2-PSK",
            Security::Wpa3 => "WPA3-SAE",
            Security::Wpa2Enterprise => "WPA2-Ent",
            Security::Owe => "OWE",
        }
    }

    pub fn needs_password(self) -> bool {
        matches!(
            self,
            Security::Wep | Security::Wpa | Security::Wpa2 | Security::Wpa3
        )
    }

    pub fn key_mgmt(self) -> Option<&'static str> {
        match self {
            Security::Wpa | Security::Wpa2 => Some("wpa-psk"),
            Security::Wpa3 => Some("sae"),
            Security::Wep => Some("none"),
            Security::Owe => Some("owe"),
            _ => None,
        }
    }
}

pub fn derive_security(flags: u32, wpa: u32, rsn: u32) -> Security {
    let has = |bits: u32, m: u32| bits & m != 0;
    if has(rsn, NM_SEC_KEY_MGMT_SAE) {
        return Security::Wpa3;
    }
    if has(rsn, NM_SEC_KEY_MGMT_OWE) {
        return Security::Owe;
    }
    if has(rsn, NM_SEC_KEY_MGMT_8021X) || has(wpa, NM_SEC_KEY_MGMT_8021X) {
        return Security::Wpa2Enterprise;
    }
    if has(rsn, NM_SEC_KEY_MGMT_PSK) {
        return Security::Wpa2;
    }
    if wpa != 0 {
        return Security::Wpa;
    }
    if has(flags, NM_802_11_AP_FLAGS_PRIVACY) {
        return Security::Wep;
    }
    Security::Open
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WifiBand {
    Ghz24,
    Ghz5,
    Ghz6,
    Unknown,
}

impl WifiBand {
    pub fn label(self) -> &'static str {
        match self {
            WifiBand::Ghz24 => "2.4 GHz",
            WifiBand::Ghz5 => "5 GHz",
            WifiBand::Ghz6 => "6 GHz",
            WifiBand::Unknown => "Wi-Fi",
        }
    }
}

pub fn band_of(freq_mhz: u32) -> WifiBand {
    match freq_mhz {
        2400..=2500 => WifiBand::Ghz24,
        4900..=5900 => WifiBand::Ghz5,
        5925..=7125 => WifiBand::Ghz6,
        _ => WifiBand::Unknown,
    }
}

#[derive(Debug, Clone)]
pub struct NetworkSnapshot {
    pub nm_available: bool,
    pub networking_enabled: bool,
    pub wireless_enabled: bool,
    pub wireless_hw_enabled: bool,
    pub wifi_device: Option<WifiDevice>,
    pub access_points: Vec<AccessPoint>,
    pub active: Option<ActiveConnectionInfo>,
    pub telemetry: NetTelemetry,
}

#[derive(Debug, Clone)]
pub struct WifiDevice {
    pub id: DeviceId,
    pub iface: String,
    pub hw_address: String,
    pub state: u32,
    pub bitrate_kbps: u32,
    pub active_ap: Option<DeviceId>,
    pub last_scan_ms: i64,
}

#[derive(Debug, Clone)]
pub struct AccessPoint {
    pub id: DeviceId,
    pub ssid: String,
    pub ssid_raw: Vec<u8>,
    pub bssid: String,
    pub strength: u8,
    pub frequency: u32,
    pub band: WifiBand,
    pub max_bitrate_kbps: u32,
    pub security: Security,
    pub is_active: bool,
    pub is_saved: bool,
    pub saved_conn_path: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ActiveConnectionInfo {
    pub id: DeviceId,
    pub ssid: String,
    pub state: u32,
    pub connection_path: Option<String>,
}

#[derive(Debug, Clone, Default)]
pub struct NetTelemetry {
    pub ipv4: Option<String>,
    pub gateway: Option<String>,
    pub dns: Vec<String>,
    pub rx_rate_kbps: f64,
    pub tx_rate_kbps: f64,
    pub total_rx_bytes: u64,
    pub total_tx_bytes: u64,
}

struct ThroughputTracker {
    last_rx: u64,
    last_tx: u64,
    last_time: Instant,
}

impl ThroughputTracker {
    fn new() -> Self {
        Self {
            last_rx: 0,
            last_tx: 0,
            last_time: Instant::now(),
        }
    }

    fn update(&mut self, rx: u64, tx: u64) -> (f64, f64) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_time).as_secs_f64();
        if elapsed <= 0.001 {
            return (0.0, 0.0);
        }

        let rx_diff = rx.saturating_sub(self.last_rx);
        let tx_diff = tx.saturating_sub(self.last_tx);

        self.last_rx = rx;
        self.last_tx = tx;
        self.last_time = now;

        let rx_kbps = (rx_diff as f64 / 1024.0) / elapsed;
        let tx_kbps = (tx_diff as f64 / 1024.0) / elapsed;

        (rx_kbps, tx_kbps)
    }
}

pub async fn run(
    polling_ms: u64,
    lang: SharedLang,
    tx: EventTx,
    mut actions: broadcast::Receiver<Action>,
) -> anyhow::Result<()> {
    let mut interval = tokio::time::interval(Duration::from_millis(polling_ms.max(2000)));
    let mut throughput_interval = tokio::time::interval(Duration::from_millis(1000));
    let mut throughput = ThroughputTracker::new();

    let mut last_snapshot: Option<NetworkSnapshot> = None;

    loop {
        tokio::select! {
            _ = interval.tick() => {
                match collect_snapshot(&mut throughput).await {
                    Ok(snap) => {
                        let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                        last_snapshot = Some(snap);
                    }
                    Err(e) => {
                        let _ = tx.send(AppEvent::ServiceDegraded {
                            name: "network",
                            reason: format!("NetworkManager D-Bus: {e}"),
                        });
                    }
                }
            }
            _ = throughput_interval.tick() => {
                if let Some(mut snap) = last_snapshot.clone() {
                    if let Some(dev) = &snap.wifi_device {
                        let (rx, tx_b) = read_sysfs_stats(&dev.iface);
                        let (rx_rate, tx_rate) = throughput.update(rx, tx_b);
                        snap.telemetry.rx_rate_kbps = rx_rate;
                        snap.telemetry.tx_rate_kbps = tx_rate;
                        snap.telemetry.total_rx_bytes = rx;
                        snap.telemetry.total_tx_bytes = tx_b;
                        let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                        last_snapshot = Some(snap);
                    }
                }
            }
            Ok(action) = actions.recv() => {
                let m = lang.messages();
                match action {
                    Action::NetworkRescan => {
                        let _ = tx.send(AppEvent::NetworkScanning(true));
                        if let Err(e) = trigger_rescan().await {
                            let _ = tx.send(AppEvent::Toast(Toast::error(format!("{}: {e}", m.net_err_rescan_failed))));
                        } else {
                            let _ = tx.send(AppEvent::Toast(Toast::info(m.net_toast_scan_started)));
                        }
                        tokio::time::sleep(Duration::from_millis(1500)).await;
                        if let Ok(snap) = collect_snapshot(&mut throughput).await {
                            let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                            last_snapshot = Some(snap);
                        }
                        let _ = tx.send(AppEvent::NetworkScanning(false));
                    }
                    Action::NetworkToggleRadio => {
                        match toggle_wireless_radio().await {
                            Ok(new_state) => {
                                let label = if new_state { m.net_toast_radio_on_state } else { m.net_toast_radio_off_state };
                                let _ = tx.send(AppEvent::Toast(Toast::info(format!("{} {label}", m.net_toast_radio_prefix))));
                                if let Ok(snap) = collect_snapshot(&mut throughput).await {
                                    let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                                    last_snapshot = Some(snap);
                                }
                            }
                            Err(e) => {
                                let _ = tx.send(AppEvent::Toast(Toast::error(format!("{}: {e}", m.net_err_radio_toggle))));
                            }
                        }
                    }
                    Action::NetworkConnect { ap_id, ssid, password } => {
                        let _ = tx.send(AppEvent::Toast(Toast::info(format!("{} {ssid}...", m.net_toast_connecting))));
                        match connect_network(&ap_id, &ssid, password.as_deref(), lang.get()).await {
                            Ok(_) => {
                                let _ = tx.send(AppEvent::Toast(Toast::info(format!("{} {ssid}", m.net_toast_connect_started))));
                            }
                            Err(e) => {
                                let _ = tx.send(AppEvent::Toast(Toast::error(format!("{}: {e}", m.net_err_connect))));
                            }
                        }
                        tokio::time::sleep(Duration::from_millis(1000)).await;
                        if let Ok(snap) = collect_snapshot(&mut throughput).await {
                            let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                            last_snapshot = Some(snap);
                        }
                    }
                    Action::NetworkDisconnect(dev_id) => {
                        if let Err(e) = disconnect_network(&dev_id.0).await {
                            let _ = tx.send(AppEvent::Toast(Toast::error(format!("{}: {e}", m.net_err_disconnect))));
                        } else {
                            let _ = tx.send(AppEvent::Toast(Toast::info(m.net_toast_disconnected)));
                        }
                        if let Ok(snap) = collect_snapshot(&mut throughput).await {
                            let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                            last_snapshot = Some(snap);
                        }
                    }
                    Action::NetworkForget(conn_path) => {
                        if let Err(e) = forget_network(&conn_path).await {
                            let _ = tx.send(AppEvent::Toast(Toast::error(format!("{}: {e}", m.net_err_forget))));
                        } else {
                            let _ = tx.send(AppEvent::Toast(Toast::info(m.net_toast_forgotten)));
                        }
                        if let Ok(snap) = collect_snapshot(&mut throughput).await {
                            let _ = tx.send(AppEvent::Network(Box::new(snap.clone())));
                            last_snapshot = Some(snap);
                        }
                    }
                    _ => {}
                }
            }
        }
    }
}

async fn collect_snapshot(throughput: &mut ThroughputTracker) -> anyhow::Result<NetworkSnapshot> {
    let conn = Connection::system().await?;

    let nm_proxy = zbus::Proxy::new(&conn, NM_SERVICE, NM_PATH, NM_IFACE).await?;

    let wireless_enabled: bool = nm_proxy
        .get_property("WirelessEnabled")
        .await
        .unwrap_or(false);
    let wireless_hw_enabled: bool = nm_proxy
        .get_property("WirelessHardwareEnabled")
        .await
        .unwrap_or(false);
    let networking_enabled: bool = nm_proxy
        .get_property("NetworkingEnabled")
        .await
        .unwrap_or(false);

    let devices: Vec<OwnedObjectPath> = nm_proxy.call("GetDevices", &()).await?;

    let mut wifi_device: Option<WifiDevice> = None;
    let mut raw_aps: Vec<AccessPoint> = Vec::new();
    let mut active_conn_info: Option<ActiveConnectionInfo> = None;
    let mut telemetry = NetTelemetry::default();

    let saved_profiles = list_saved_profiles(&conn).await.unwrap_or_default();

    for dev_path in devices {
        let dev_proxy =
            zbus::Proxy::new(&conn, NM_SERVICE, dev_path.as_str(), NM_DEVICE_IFACE).await?;
        let dev_type: u32 = dev_proxy.get_property("DeviceType").await.unwrap_or(0);

        if dev_type == NM_DEVICE_TYPE_WIFI {
            let iface: String = dev_proxy
                .get_property("Interface")
                .await
                .unwrap_or_default();
            let hw_address: String = dev_proxy
                .get_property("HwAddress")
                .await
                .unwrap_or_default();
            let state: u32 = dev_proxy.get_property("State").await.unwrap_or(0);
            let active_conn_path: OwnedObjectPath = dev_proxy
                .get_property("ActiveConnection")
                .await
                .unwrap_or_else(|_| OwnedObjectPath::try_from("/").unwrap());
            let ip4_config_path: OwnedObjectPath = dev_proxy
                .get_property("Ip4Config")
                .await
                .unwrap_or_else(|_| OwnedObjectPath::try_from("/").unwrap());

            let wifi_proxy =
                zbus::Proxy::new(&conn, NM_SERVICE, dev_path.as_str(), NM_WIRELESS_IFACE).await?;
            let bitrate: u32 = wifi_proxy.get_property("Bitrate").await.unwrap_or(0);
            let active_ap: OwnedObjectPath = wifi_proxy
                .get_property("ActiveAccessPoint")
                .await
                .unwrap_or_else(|_| OwnedObjectPath::try_from("/").unwrap());
            let last_scan: i64 = wifi_proxy.get_property("LastScan").await.unwrap_or(-1);

            let active_ap_id = if active_ap.as_str() != "/" {
                Some(DeviceId(active_ap.as_str().to_string()))
            } else {
                None
            };

            wifi_device = Some(WifiDevice {
                id: DeviceId(dev_path.as_str().to_string()),
                iface: iface.clone(),
                hw_address,
                state,
                bitrate_kbps: bitrate,
                active_ap: active_ap_id.clone(),
                last_scan_ms: last_scan,
            });

            let ap_paths: Vec<OwnedObjectPath> = wifi_proxy
                .call("GetAllAccessPoints", &())
                .await
                .unwrap_or_default();

            for ap_path in ap_paths {
                let ap_proxy =
                    zbus::Proxy::new(&conn, NM_SERVICE, ap_path.as_str(), NM_AP_IFACE).await?;
                let ssid_raw: Vec<u8> = ap_proxy.get_property("Ssid").await.unwrap_or_default();
                let ssid = String::from_utf8_lossy(&ssid_raw).to_string();
                if ssid.is_empty() {
                    continue;
                }

                let strength: u8 = ap_proxy.get_property("Strength").await.unwrap_or(0);
                let frequency: u32 = ap_proxy.get_property("Frequency").await.unwrap_or(0);
                let bssid: String = ap_proxy.get_property("HwAddress").await.unwrap_or_default();
                let max_bitrate: u32 = ap_proxy.get_property("MaxBitrate").await.unwrap_or(0);
                let flags: u32 = ap_proxy.get_property("Flags").await.unwrap_or(0);
                let wpa_flags: u32 = ap_proxy.get_property("WpaFlags").await.unwrap_or(0);
                let rsn_flags: u32 = ap_proxy.get_property("RsnFlags").await.unwrap_or(0);

                let security = derive_security(flags, wpa_flags, rsn_flags);
                let band = band_of(frequency);

                let is_active = active_ap.as_str() == ap_path.as_str();
                let saved_conn_path = saved_profiles.get(&ssid).cloned();
                let is_saved = saved_conn_path.is_some();

                raw_aps.push(AccessPoint {
                    id: DeviceId(ap_path.as_str().to_string()),
                    ssid,
                    ssid_raw,
                    bssid,
                    strength,
                    frequency,
                    band,
                    max_bitrate_kbps: max_bitrate,
                    security,
                    is_active,
                    is_saved,
                    saved_conn_path,
                });
            }

            if ip4_config_path.as_str() != "/" {
                let ip_proxy =
                    zbus::Proxy::new(&conn, NM_SERVICE, ip4_config_path.as_str(), NM_IP4_IFACE)
                        .await?;
                let gateway: String = ip_proxy.get_property("Gateway").await.unwrap_or_default();
                if !gateway.is_empty() {
                    telemetry.gateway = Some(gateway);
                }

                if let Ok(addr_data) = ip_proxy
                    .get_property::<Vec<HashMap<String, OwnedValue>>>("AddressData")
                    .await
                {
                    if let Some(first) = addr_data.first() {
                        if let Some(addr_val) = first.get("address") {
                            if let Ok(ip_str) = <&str>::try_from(addr_val) {
                                telemetry.ipv4 = Some(ip_str.to_string());
                            }
                        }
                    }
                }
            }

            let (rx, tx_b) = read_sysfs_stats(&iface);
            let (rx_rate, tx_rate) = throughput.update(rx, tx_b);
            telemetry.rx_rate_kbps = rx_rate;
            telemetry.tx_rate_kbps = tx_rate;
            telemetry.total_rx_bytes = rx;
            telemetry.total_tx_bytes = tx_b;

            if active_conn_path.as_str() != "/" {
                let act_proxy = zbus::Proxy::new(
                    &conn,
                    NM_SERVICE,
                    active_conn_path.as_str(),
                    NM_ACTIVE_CONN_IFACE,
                )
                .await?;
                let id_str: String = act_proxy.get_property("Id").await.unwrap_or_default();
                let act_state: u32 = act_proxy.get_property("State").await.unwrap_or(0);
                let conn_path: OwnedObjectPath = act_proxy
                    .get_property("Connection")
                    .await
                    .unwrap_or_else(|_| OwnedObjectPath::try_from("/").unwrap());

                active_conn_info = Some(ActiveConnectionInfo {
                    id: DeviceId(active_conn_path.as_str().to_string()),
                    ssid: id_str,
                    state: act_state,
                    connection_path: if conn_path.as_str() != "/" {
                        Some(conn_path.as_str().to_string())
                    } else {
                        None
                    },
                });
            }

            break;
        }
    }

    let mut deduplicated: HashMap<String, AccessPoint> = HashMap::new();
    for ap in raw_aps {
        match deduplicated.get(&ap.ssid) {
            Some(existing) => {
                if ap.is_active || (!existing.is_active && ap.strength > existing.strength) {
                    deduplicated.insert(ap.ssid.clone(), ap);
                }
            }
            None => {
                deduplicated.insert(ap.ssid.clone(), ap);
            }
        }
    }

    let mut access_points: Vec<AccessPoint> = deduplicated.into_values().collect();
    access_points.sort_by(|a, b| {
        if a.is_active != b.is_active {
            b.is_active.cmp(&a.is_active)
        } else if a.is_saved != b.is_saved {
            b.is_saved.cmp(&a.is_saved)
        } else {
            b.strength
                .cmp(&a.strength)
                .then_with(|| a.ssid.cmp(&b.ssid))
        }
    });

    Ok(NetworkSnapshot {
        nm_available: true,
        networking_enabled,
        wireless_enabled,
        wireless_hw_enabled,
        wifi_device,
        access_points,
        active: active_conn_info,
        telemetry,
    })
}

async fn list_saved_profiles(conn: &Connection) -> anyhow::Result<HashMap<String, String>> {
    let settings_proxy =
        zbus::Proxy::new(conn, NM_SERVICE, NM_SETTINGS_PATH, NM_SETTINGS_IFACE).await?;
    let connections: Vec<OwnedObjectPath> = settings_proxy.call("ListConnections", &()).await?;

    let mut map = HashMap::new();
    for conn_path in connections {
        let Ok(conn_proxy) =
            zbus::Proxy::new(conn, NM_SERVICE, conn_path.as_str(), NM_CONN_IFACE).await
        else {
            continue;
        };
        let res: Result<HashMap<String, HashMap<String, OwnedValue>>, _> =
            conn_proxy.call("GetSettings", &()).await;
        if let Ok(settings) = res {
            if let Some(wireless_map) = settings.get("802-11-wireless") {
                if let Some(ssid_val) = wireless_map.get("ssid") {
                    if let Ok(ssid_bytes) = Vec::<u8>::try_from(ssid_val.clone()) {
                        let ssid = String::from_utf8_lossy(&ssid_bytes).to_string();
                        map.insert(ssid, conn_path.as_str().to_string());
                    }
                }
            } else if let Some(conn_sec) = settings.get("connection") {
                if let Some(id_val) = conn_sec.get("id") {
                    if let Ok(id_str) = String::try_from(id_val.clone()) {
                        map.insert(id_str, conn_path.as_str().to_string());
                    }
                }
            }
        }
    }

    Ok(map)
}

fn read_sysfs_stats(iface: &str) -> (u64, u64) {
    let rx_path = format!("/sys/class/net/{iface}/statistics/rx_bytes");
    let tx_path = format!("/sys/class/net/{iface}/statistics/tx_bytes");

    let rx = std::fs::read_to_string(rx_path)
        .ok()
        .and_then(|s| s.trim().parse::<u64>().ok())
        .unwrap_or(0);

    let tx = std::fs::read_to_string(tx_path)
        .ok()
        .and_then(|s| s.trim().parse::<u64>().ok())
        .unwrap_or(0);

    (rx, tx)
}

async fn trigger_rescan() -> anyhow::Result<()> {
    let conn = Connection::system().await?;
    let nm_proxy = zbus::Proxy::new(&conn, NM_SERVICE, NM_PATH, NM_IFACE).await?;
    let devices: Vec<OwnedObjectPath> = nm_proxy.call("GetDevices", &()).await?;

    for dev_path in devices {
        let dev_proxy =
            zbus::Proxy::new(&conn, NM_SERVICE, dev_path.as_str(), NM_DEVICE_IFACE).await?;
        let dev_type: u32 = dev_proxy.get_property("DeviceType").await.unwrap_or(0);
        if dev_type == NM_DEVICE_TYPE_WIFI {
            let wifi_proxy =
                zbus::Proxy::new(&conn, NM_SERVICE, dev_path.as_str(), NM_WIRELESS_IFACE).await?;
            let empty_opts: HashMap<&str, Value> = HashMap::new();
            let _: () = wifi_proxy.call("RequestScan", &(empty_opts,)).await?;
            break;
        }
    }
    Ok(())
}

async fn toggle_wireless_radio() -> anyhow::Result<bool> {
    let conn = Connection::system().await?;
    let nm_proxy = zbus::Proxy::new(&conn, NM_SERVICE, NM_PATH, NM_IFACE).await?;
    let current: bool = nm_proxy.get_property("WirelessEnabled").await?;
    let next = !current;
    nm_proxy.set_property("WirelessEnabled", next).await?;
    Ok(next)
}

async fn disconnect_network(device_path: &str) -> anyhow::Result<()> {
    let conn = Connection::system().await?;
    let dev_proxy = zbus::Proxy::new(&conn, NM_SERVICE, device_path, NM_DEVICE_IFACE).await?;
    let _: () = dev_proxy.call("Disconnect", &()).await?;
    Ok(())
}

async fn forget_network(conn_path: &str) -> anyhow::Result<()> {
    let conn = Connection::system().await?;
    let conn_proxy = zbus::Proxy::new(&conn, NM_SERVICE, conn_path, NM_CONN_IFACE).await?;
    let _: () = conn_proxy.call("Delete", &()).await?;
    Ok(())
}

async fn connect_network(
    ap_path: &str,
    ssid: &str,
    password: Option<&str>,
    lang: crate::i18n::Language,
) -> anyhow::Result<()> {
    let conn = Connection::system().await?;
    let nm_proxy = zbus::Proxy::new(&conn, NM_SERVICE, NM_PATH, NM_IFACE).await?;
    let ap_obj = ObjectPath::try_from(ap_path)?;

    let saved = list_saved_profiles(&conn).await.unwrap_or_default();
    if let Some(saved_path) = saved.get(ssid) {
        let conn_obj = ObjectPath::try_from(saved_path.as_str())?;

        let devices: Vec<OwnedObjectPath> = nm_proxy.call("GetDevices", &()).await?;
        for dev_path in devices {
            let dev_proxy =
                zbus::Proxy::new(&conn, NM_SERVICE, dev_path.as_str(), NM_DEVICE_IFACE).await?;
            let dev_type: u32 = dev_proxy.get_property("DeviceType").await.unwrap_or(0);
            if dev_type == NM_DEVICE_TYPE_WIFI {
                let _: (OwnedObjectPath, OwnedObjectPath) = nm_proxy
                    .call("ActivateConnection", &(conn_obj, dev_path, ap_obj))
                    .await?;
                return Ok(());
            }
        }
    }

    let mut connection_settings: HashMap<&str, HashMap<&str, Value>> = HashMap::new();

    let mut conn_sec: HashMap<&str, Value> = HashMap::new();
    conn_sec.insert("id", Value::from(ssid));
    conn_sec.insert("type", Value::from("802-11-wireless"));
    connection_settings.insert("connection", conn_sec);

    let mut wireless_sec: HashMap<&str, Value> = HashMap::new();
    wireless_sec.insert("ssid", Value::from(ssid.as_bytes()));
    wireless_sec.insert("mode", Value::from("infrastructure"));
    connection_settings.insert("802-11-wireless", wireless_sec);

    if let Some(pwd) = password {
        let mut sec_sec: HashMap<&str, Value> = HashMap::new();
        sec_sec.insert("key-mgmt", Value::from("wpa-psk"));
        sec_sec.insert("psk", Value::from(pwd));
        connection_settings.insert("802-11-wireless-security", sec_sec);
    }

    let devices: Vec<OwnedObjectPath> = nm_proxy.call("GetDevices", &()).await?;
    for dev_path in devices {
        let dev_proxy =
            zbus::Proxy::new(&conn, NM_SERVICE, dev_path.as_str(), NM_DEVICE_IFACE).await?;
        let dev_type: u32 = dev_proxy.get_property("DeviceType").await.unwrap_or(0);
        if dev_type == NM_DEVICE_TYPE_WIFI {
            let _: (OwnedObjectPath, OwnedObjectPath) = nm_proxy
                .call(
                    "AddAndActivateConnection",
                    &(connection_settings, dev_path, ap_obj),
                )
                .await?;
            return Ok(());
        }
    }

    anyhow::bail!(lang.messages().net_err_device_not_found)
}