sl4f_lib/hardware_power_statecontrol/
facade.rsuse crate::common_utils::common::macros::{fx_err_and_bail, with_line};
use anyhow::Error;
use fidl_fuchsia_hardware_power_statecontrol::{AdminMarker, AdminProxy, RebootReason};
use fuchsia_component as app;
use tracing::info;
#[derive(Debug)]
pub struct HardwarePowerStatecontrolFacade {}
impl HardwarePowerStatecontrolFacade {
pub fn new() -> HardwarePowerStatecontrolFacade {
HardwarePowerStatecontrolFacade {}
}
fn get_admin_proxy(&self) -> Result<AdminProxy, Error> {
let tag = "HardwarePowerStatecontrolFacade";
match app::client::connect_to_protocol::<AdminMarker>() {
Ok(p) => Ok(p),
Err(err) => fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to create device admin proxy: {:?}", err)
),
}
}
pub async fn suspend_reboot(&self) -> Result<(), Error> {
let tag = "HardwarePowerStatecontrolFacade::suspend_reboot";
info!("Executing Suspend: REBOOT");
if let Err(err) = self.get_admin_proxy()?.reboot(RebootReason::UserRequest).await? {
fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to change power control state: {:?}", err)
)
}
Ok(())
}
pub async fn suspend_reboot_bootloader(&self) -> Result<(), Error> {
let tag = "HardwarePowerStatecontrolFacade::suspend_reboot_bootloader";
info!("Executing Suspend: REBOOT_BOOTLOADER");
if let Err(err) = self.get_admin_proxy()?.reboot_to_bootloader().await? {
fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to change power control state: {:?}", err)
)
}
Ok(())
}
pub async fn suspend_reboot_recovery(&self) -> Result<(), Error> {
let tag = "HardwarePowerStatecontrolFacade::suspend_reboot_recovery";
info!("Executing Suspend: REBOOT_RECOVERY");
if let Err(err) = self.get_admin_proxy()?.reboot_to_recovery().await? {
fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to change power control state: {:?}", err)
)
}
Ok(())
}
pub async fn suspend_poweroff(&self) -> Result<(), Error> {
let tag = "HardwarePowerStatecontrolFacade::suspend_poweroff";
info!("Executing Suspend: POWEROFF");
if let Err(err) = self.get_admin_proxy()?.poweroff().await? {
fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to change power control state: {:?}", err)
)
}
Ok(())
}
pub async fn suspend_mexec(&self) -> Result<(), Error> {
let tag = "HardwarePowerStatecontrolFacade::suspend_mexec";
fx_err_and_bail!(&with_line!(tag), format!("Unsupported"))
}
pub async fn suspend_ram(&self) -> Result<(), Error> {
let tag = "HardwarePowerStatecontrolFacade::suspend_ram";
info!("Executing Suspend: SUSPEND_RAM");
if let Err(err) = self.get_admin_proxy()?.suspend_to_ram().await? {
fx_err_and_bail!(
&with_line!(tag),
format_err!("Failed to change power control state: {:?}", err)
)
}
Ok(())
}
}