Skip to main content

wayle_network/wired/
mod.rs

1mod monitoring;
2mod types;
3
4use std::sync::Arc;
5
6pub(crate) use types::{LiveWiredParams, WiredParams};
7use wayle_core::Property;
8use wayle_traits::{ModelMonitoring, Reactive};
9
10use super::{
11    core::{
12        config::ip4_config::Ip4Config,
13        device::wired::{DeviceWired, DeviceWiredParams, LiveDeviceWiredParams},
14    },
15    error::Error,
16    types::states::NetworkStatus,
17};
18
19/// Wired (ethernet) device state. See [crate-level docs](crate) for usage.
20#[derive(Clone, Debug)]
21pub struct Wired {
22    /// Underlying device properties.
23    pub device: DeviceWired,
24    /// Current connectivity status.
25    pub connectivity: Property<NetworkStatus>,
26    /// IPv4 address assigned to this device.
27    pub ip4_address: Property<Option<String>>,
28}
29
30impl PartialEq for Wired {
31    fn eq(&self, other: &Self) -> bool {
32        self.device.core.object_path == other.device.core.object_path
33    }
34}
35
36impl Reactive for Wired {
37    type Context<'a> = WiredParams<'a>;
38    type LiveContext<'a> = LiveWiredParams<'a>;
39    type Error = Error;
40
41    async fn get(params: Self::Context<'_>) -> Result<Self, Self::Error> {
42        let device = DeviceWired::get(DeviceWiredParams {
43            connection: params.connection,
44            device_path: params.device_path.clone(),
45        })
46        .await
47        .map_err(|e| Error::ObjectCreationFailed {
48            object_type: String::from("Wired"),
49            object_path: params.device_path.clone(),
50            source: e.into(),
51        })?;
52
53        Self::from_device(device).await
54    }
55
56    async fn get_live(params: Self::LiveContext<'_>) -> Result<Arc<Self>, Self::Error> {
57        let device_arc = DeviceWired::get_live(LiveDeviceWiredParams {
58            connection: params.connection,
59            device_path: params.device_path,
60            cancellation_token: params.cancellation_token,
61        })
62        .await?;
63        let device = DeviceWired::clone(&device_arc);
64
65        let wired = Self::from_device(device.clone()).await?;
66        let wired = Arc::new(wired);
67
68        wired.clone().start_monitoring().await?;
69
70        Ok(wired)
71    }
72}
73
74impl Wired {
75    async fn from_device(device: DeviceWired) -> Result<Self, Error> {
76        let device_state = &device.core.state.get();
77        let connectivity = NetworkStatus::from_device_state(*device_state);
78        let ip4_address =
79            Ip4Config::resolve_address(&device.core.connection, device.core.ip4_config.get()).await;
80
81        Ok(Self {
82            device,
83            connectivity: Property::new(connectivity),
84            ip4_address: Property::new(ip4_address),
85        })
86    }
87}