sl4f_lib/hardware_power_statecontrol/
facade.rs1use crate::common_utils::common::macros::{fx_err_and_bail, with_line};
6use anyhow::Error;
7use fidl_fuchsia_hardware_power_statecontrol::{
8 AdminMarker, AdminProxy, RebootOptions, RebootReason2,
9};
10use fuchsia_component as app;
11use log::info;
12
13#[derive(Debug)]
18pub struct HardwarePowerStatecontrolFacade {}
19
20impl HardwarePowerStatecontrolFacade {
21 pub fn new() -> HardwarePowerStatecontrolFacade {
22 HardwarePowerStatecontrolFacade {}
23 }
24
25 fn get_admin_proxy(&self) -> Result<AdminProxy, Error> {
26 let tag = "HardwarePowerStatecontrolFacade";
27 match app::client::connect_to_protocol::<AdminMarker>() {
28 Ok(p) => Ok(p),
29 Err(err) => fx_err_and_bail!(
30 &with_line!(tag),
31 format_err!("Failed to create device admin proxy: {:?}", err)
32 ),
33 }
34 }
35
36 pub async fn suspend_reboot(&self) -> Result<(), Error> {
38 let tag = "HardwarePowerStatecontrolFacade::suspend_reboot";
39 info!("Executing Suspend: REBOOT");
40 if let Err(err) = self
41 .get_admin_proxy()?
42 .perform_reboot(&RebootOptions {
43 reasons: Some(vec![RebootReason2::UserRequest]),
44 ..Default::default()
45 })
46 .await?
47 {
48 fx_err_and_bail!(
49 &with_line!(tag),
50 format_err!("Failed to change power control state: {:?}", err)
51 )
52 }
53 Ok(())
54 }
55
56 pub async fn suspend_reboot_bootloader(&self) -> Result<(), Error> {
58 let tag = "HardwarePowerStatecontrolFacade::suspend_reboot_bootloader";
59 info!("Executing Suspend: REBOOT_BOOTLOADER");
60
61 if let Err(err) = self.get_admin_proxy()?.reboot_to_bootloader().await? {
62 fx_err_and_bail!(
63 &with_line!(tag),
64 format_err!("Failed to change power control state: {:?}", err)
65 )
66 }
67 Ok(())
68 }
69
70 pub async fn suspend_reboot_recovery(&self) -> Result<(), Error> {
72 let tag = "HardwarePowerStatecontrolFacade::suspend_reboot_recovery";
73 info!("Executing Suspend: REBOOT_RECOVERY");
74 if let Err(err) = self.get_admin_proxy()?.reboot_to_recovery().await? {
75 fx_err_and_bail!(
76 &with_line!(tag),
77 format_err!("Failed to change power control state: {:?}", err)
78 )
79 }
80 Ok(())
81 }
82
83 pub async fn suspend_poweroff(&self) -> Result<(), Error> {
85 let tag = "HardwarePowerStatecontrolFacade::suspend_poweroff";
86 info!("Executing Suspend: POWEROFF");
87
88 if let Err(err) = self.get_admin_proxy()?.poweroff().await? {
89 fx_err_and_bail!(
90 &with_line!(tag),
91 format_err!("Failed to change power control state: {:?}", err)
92 )
93 }
94 Ok(())
95 }
96
97 pub async fn suspend_mexec(&self) -> Result<(), Error> {
99 let tag = "HardwarePowerStatecontrolFacade::suspend_mexec";
100 fx_err_and_bail!(&with_line!(tag), format!("Unsupported"))
102 }
103
104 pub async fn suspend_ram(&self) -> Result<(), Error> {
106 let tag = "HardwarePowerStatecontrolFacade::suspend_ram";
107 info!("Executing Suspend: SUSPEND_RAM");
108 if let Err(err) = self.get_admin_proxy()?.suspend_to_ram().await? {
109 fx_err_and_bail!(
110 &with_line!(tag),
111 format_err!("Failed to change power control state: {:?}", err)
112 )
113 }
114 Ok(())
115 }
116}