sl4f_lib/common_utils/
lowpan_context.rsuse anyhow::{Context as _, Error};
use fidl::endpoints::create_endpoints;
use fidl_fuchsia_factory_lowpan::{FactoryDeviceMarker, FactoryDeviceProxy, FactoryLookupMarker};
use fidl_fuchsia_lowpan::{DeviceWatcherMarker, DeviceWatcherProxy};
use fidl_fuchsia_lowpan_device::{
DeviceConnectorMarker, DeviceExtraConnectorMarker, DeviceExtraMarker, DeviceExtraProxy,
DeviceMarker, DeviceProxy,
};
use fidl_fuchsia_lowpan_test::{DeviceTestConnectorMarker, DeviceTestMarker, DeviceTestProxy};
use fuchsia_component::client::connect_to_protocol;
pub struct LowpanContext {
pub device_watcher: DeviceWatcherProxy,
pub device_name: String,
}
impl LowpanContext {
const DEFAULT_DEVICE_NAME: &'static str = "lowpan0";
pub fn new(device_name: Option<String>) -> Result<LowpanContext, Error> {
let device_watcher = connect_to_protocol::<DeviceWatcherMarker>()
.context("Failed to connect to Lowpan DeviceWatcher service")?;
Ok(LowpanContext {
device_watcher,
device_name: device_name
.unwrap_or_else(|| LowpanContext::DEFAULT_DEVICE_NAME.to_string()),
})
}
pub async fn get_default_device(&self) -> Result<DeviceProxy, Error> {
let (client, server) = create_endpoints::<DeviceMarker>();
connect_to_protocol::<DeviceConnectorMarker>()
.context("Failed to connect to DeviceConnector")?
.connect(&self.device_name, server)?;
Ok(client.into_proxy())
}
pub async fn get_default_device_factory(&self) -> Result<FactoryDeviceProxy, Error> {
let lookup = connect_to_protocol::<FactoryLookupMarker>()
.context("Failed to connect to Lowpan FactoryLookup service")?;
let (client, server) = create_endpoints::<FactoryDeviceMarker>();
lookup.lookup(&self.device_name, server)?;
Ok(client.into_proxy())
}
pub async fn get_default_device_proxies(
&self,
) -> Result<(DeviceProxy, DeviceExtraProxy, DeviceTestProxy), Error> {
let (client, server) = create_endpoints::<DeviceMarker>();
let (client_extra, server_extra) = create_endpoints::<DeviceExtraMarker>();
let (client_test, server_test) = create_endpoints::<DeviceTestMarker>();
connect_to_protocol::<DeviceConnectorMarker>()
.context("Failed to connect to DeviceConnector")?
.connect(&self.device_name, server)?;
connect_to_protocol::<DeviceExtraConnectorMarker>()
.context("Failed to connect to DeviceExtraConnector")?
.connect(&self.device_name, server_extra)?;
connect_to_protocol::<DeviceTestConnectorMarker>()
.context("Failed to connect to DeviceTestConnector")?
.connect(&self.device_name, server_test)?;
Ok((client.into_proxy(), client_extra.into_proxy(), client_test.into_proxy()))
}
}