Device can now be downcasted to a specific type of device (and upcasted back)
This commit is contained in:
parent
2acdb272fb
commit
e40c3e7787
7 changed files with 106 additions and 6 deletions
|
|
@ -10,7 +10,10 @@ use crate::{
|
||||||
interface::{
|
interface::{
|
||||||
config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy},
|
config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy},
|
||||||
connection::ActiveConnectionProxy,
|
connection::ActiveConnectionProxy,
|
||||||
device::DeviceProxy,
|
device::{
|
||||||
|
bluetooth::BluetoothDeviceProxy, wired::WiredDeviceProxy,
|
||||||
|
wireless::WirelessDeviceProxy, DeviceProxy,
|
||||||
|
},
|
||||||
enums::{DeviceCapabilities, DeviceState, DeviceType},
|
enums::{DeviceCapabilities, DeviceState, DeviceType},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -53,6 +56,33 @@ impl<'a> Device<'a> {
|
||||||
self.0.device_type().await.map(DeviceType::from)
|
self.0.device_type().await.map(DeviceType::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn downcast_to_device(&'a self) -> Result<SpecificDevice<'a>> {
|
||||||
|
match self.device_type().await? {
|
||||||
|
DeviceType::Bluetooth => Ok(SpecificDevice::Bluetooth(
|
||||||
|
BluetoothDeviceProxy::builder(self.0.connection())
|
||||||
|
.path(self.0.path())?
|
||||||
|
.build()
|
||||||
|
.await?
|
||||||
|
.into(),
|
||||||
|
)),
|
||||||
|
DeviceType::Ethernet => Ok(SpecificDevice::Wired(
|
||||||
|
WiredDeviceProxy::builder(self.0.connection())
|
||||||
|
.path(self.0.path())?
|
||||||
|
.build()
|
||||||
|
.await?
|
||||||
|
.into(),
|
||||||
|
)),
|
||||||
|
DeviceType::Wifi => Ok(SpecificDevice::Wireless(
|
||||||
|
WirelessDeviceProxy::builder(self.0.connection())
|
||||||
|
.path(self.0.path())?
|
||||||
|
.build()
|
||||||
|
.await?
|
||||||
|
.into(),
|
||||||
|
)),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn ip4_address(&self) -> Result<Ipv4Addr> {
|
pub async fn ip4_address(&self) -> Result<Ipv4Addr> {
|
||||||
self.0.ip4_address().await.map(Ipv4Addr::from)
|
self.0.ip4_address().await.map(Ipv4Addr::from)
|
||||||
}
|
}
|
||||||
|
|
@ -91,3 +121,32 @@ impl<'a> From<DeviceProxy<'a>> for Device<'a> {
|
||||||
Device(device)
|
Device(device)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum SpecificDevice<'a> {
|
||||||
|
Bluetooth(bluetooth::BluetoothDevice<'a>),
|
||||||
|
Wired(wired::WiredDevice<'a>),
|
||||||
|
Wireless(wireless::WirelessDevice<'a>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> SpecificDevice<'a> {
|
||||||
|
pub fn into_bluetooth(self) -> Option<bluetooth::BluetoothDevice<'a>> {
|
||||||
|
match self {
|
||||||
|
SpecificDevice::Bluetooth(device) => Some(device),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_wired(self) -> Option<wired::WiredDevice<'a>> {
|
||||||
|
match self {
|
||||||
|
SpecificDevice::Wired(device) => Some(device),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_wireless(self) -> Option<wireless::WirelessDevice<'a>> {
|
||||||
|
match self {
|
||||||
|
SpecificDevice::Wireless(device) => Some(device),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,22 @@
|
||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
use crate::interface::device::bluetooth::BluetoothDeviceProxy;
|
use super::Device;
|
||||||
|
use crate::interface::device::{bluetooth::BluetoothDeviceProxy, DeviceProxy};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
use zbus::Result;
|
||||||
|
|
||||||
pub struct BluetoothDevice<'a>(BluetoothDeviceProxy<'a>);
|
pub struct BluetoothDevice<'a>(BluetoothDeviceProxy<'a>);
|
||||||
|
|
||||||
|
impl<'a> BluetoothDevice<'a> {
|
||||||
|
pub async fn upcast(&'a self) -> Result<Device<'a>> {
|
||||||
|
DeviceProxy::builder(self.0.connection())
|
||||||
|
.path(self.0.path())?
|
||||||
|
.build()
|
||||||
|
.await
|
||||||
|
.map(Device::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Deref for BluetoothDevice<'a> {
|
impl<'a> Deref for BluetoothDevice<'a> {
|
||||||
type Target = BluetoothDeviceProxy<'a>;
|
type Target = BluetoothDeviceProxy<'a>;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,22 @@
|
||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
use crate::interface::device::wired::WiredDeviceProxy;
|
use super::Device;
|
||||||
|
use crate::interface::device::{wired::WiredDeviceProxy, DeviceProxy};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
use zbus::Result;
|
||||||
|
|
||||||
pub struct WiredDevice<'a>(WiredDeviceProxy<'a>);
|
pub struct WiredDevice<'a>(WiredDeviceProxy<'a>);
|
||||||
|
|
||||||
|
impl<'a> WiredDevice<'a> {
|
||||||
|
pub async fn upcast(&'a self) -> Result<Device<'a>> {
|
||||||
|
DeviceProxy::builder(self.0.connection())
|
||||||
|
.path(self.0.path())?
|
||||||
|
.build()
|
||||||
|
.await
|
||||||
|
.map(Device::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Deref for WiredDevice<'a> {
|
impl<'a> Deref for WiredDevice<'a> {
|
||||||
type Target = WiredDeviceProxy<'a>;
|
type Target = WiredDeviceProxy<'a>;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,25 @@
|
||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
use crate::interface::{device::wireless::WirelessDeviceProxy, enums::WifiCapabilities};
|
use super::Device;
|
||||||
|
use crate::interface::{
|
||||||
|
device::{wireless::WirelessDeviceProxy, DeviceProxy},
|
||||||
|
enums::WifiCapabilities,
|
||||||
|
};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use zbus::Result;
|
use zbus::Result;
|
||||||
|
|
||||||
pub struct WirelessDevice<'a>(WirelessDeviceProxy<'a>);
|
pub struct WirelessDevice<'a>(WirelessDeviceProxy<'a>);
|
||||||
|
|
||||||
|
impl<'a> WirelessDevice<'a> {
|
||||||
|
pub async fn upcast(&'a self) -> Result<Device<'a>> {
|
||||||
|
DeviceProxy::builder(self.0.connection())
|
||||||
|
.path(self.0.path())?
|
||||||
|
.build()
|
||||||
|
.await
|
||||||
|
.map(Device::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> WirelessDevice<'a> {
|
impl<'a> WirelessDevice<'a> {
|
||||||
pub async fn wireless_capabilities(&self) -> Result<WifiCapabilities> {
|
pub async fn wireless_capabilities(&self) -> Result<WifiCapabilities> {
|
||||||
self.0
|
self.0
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ pub trait Device {
|
||||||
|
|
||||||
/// Path property
|
/// Path property
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
fn path(&self) -> zbus::Result<String>;
|
fn path_(&self) -> zbus::Result<String>;
|
||||||
|
|
||||||
/// PhysicalPortId property
|
/// PhysicalPortId property
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ pub enum DeviceType {
|
||||||
Ethernet,
|
Ethernet,
|
||||||
Wifi,
|
Wifi,
|
||||||
Bluetooth,
|
Bluetooth,
|
||||||
|
Generic,
|
||||||
Other,
|
Other,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
@ -61,7 +62,8 @@ impl From<u32> for DeviceType {
|
||||||
1 => DeviceType::Ethernet,
|
1 => DeviceType::Ethernet,
|
||||||
2 => DeviceType::Wifi,
|
2 => DeviceType::Wifi,
|
||||||
5 => DeviceType::Bluetooth,
|
5 => DeviceType::Bluetooth,
|
||||||
3..=4 | 6..=32 => DeviceType::Other,
|
14 => DeviceType::Generic,
|
||||||
|
3..=32 => DeviceType::Other,
|
||||||
_ => DeviceType::Unknown,
|
_ => DeviceType::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
//! * [`zbus::fdo::PeerProxy`]
|
//! * [`zbus::fdo::PeerProxy`]
|
||||||
//!
|
//!
|
||||||
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
|
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
|
||||||
|
#![allow(clippy::type_complexity)]
|
||||||
|
|
||||||
pub mod access_point;
|
pub mod access_point;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue