sl4f_lib/device/
facade.rsuse crate::common_utils::common::macros::{fx_err_and_bail, with_line};
use anyhow::Error;
use fidl_fuchsia_buildinfo::ProviderMarker;
use fidl_fuchsia_device::{NameProviderMarker, DEFAULT_DEVICE_NAME};
use fuchsia_component::client;
#[derive(Debug)]
pub struct DeviceFacade {}
impl DeviceFacade {
pub fn new() -> DeviceFacade {
DeviceFacade {}
}
pub async fn get_device_name(&self) -> Result<String, Error> {
let tag = "DeviceFacade::get_device_name";
let proxy = match client::connect_to_protocol::<NameProviderMarker>() {
Ok(p) => p,
Err(err) => fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to connect to NameProvider proxy: {:?}", err)
),
};
let name = proxy
.get_device_name()
.await?
.map_err(|e| format_err!("failed to obtain device name: {:?}", e));
let device_name = name.unwrap_or_else(|_| DEFAULT_DEVICE_NAME.to_string());
Ok(device_name)
}
pub async fn get_product(&self) -> Result<String, Error> {
let tag = "DeviceFacade::get_product";
let proxy = match client::connect_to_protocol::<ProviderMarker>() {
Ok(p) => p,
Err(err) => fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to connect to fuchsia.buildinfo.Provider proxy: {:?}", err)
),
};
let buildinfo = proxy.get_build_info().await?;
let product = buildinfo.product_config.unwrap_or_else(|| "unknown".to_string());
Ok(product)
}
pub async fn get_version(&self) -> Result<String, Error> {
let tag = "DeviceFacade::get_version";
let proxy = match client::connect_to_protocol::<ProviderMarker>() {
Ok(p) => p,
Err(err) => fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to connect to fuchsia.buildinfo.Provider proxy: {:?}", err)
),
};
let buildinfo = proxy.get_build_info().await?;
let device_name = buildinfo.version.unwrap_or_else(|| "unknown".to_string());
Ok(device_name)
}
}