xref: /aosp_15_r20/external/crosvm/power_monitor/src/lib.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2020 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Power monitoring abstraction layer.
6 
7 use std::error::Error;
8 
9 use base::ReadNotifier;
10 
11 pub trait PowerMonitor: ReadNotifier {
read_message(&mut self) -> std::result::Result<Option<PowerData>, Box<dyn Error>>12     fn read_message(&mut self) -> std::result::Result<Option<PowerData>, Box<dyn Error>>;
13 }
14 
15 pub trait PowerClient {
get_power_data(&mut self) -> std::result::Result<PowerData, Box<dyn Error>>16     fn get_power_data(&mut self) -> std::result::Result<PowerData, Box<dyn Error>>;
17 }
18 
19 #[derive(Debug)]
20 pub struct PowerData {
21     pub ac_online: bool,
22     pub battery: Option<BatteryData>,
23 }
24 
25 #[derive(Clone, Copy, Debug)]
26 pub struct BatteryData {
27     pub status: BatteryStatus,
28     pub percent: u32,
29     /// Battery voltage in microvolts.
30     pub voltage: u32,
31     /// Battery current in microamps.
32     pub current: u32,
33     /// Battery charge counter in microampere hours.
34     pub charge_counter: u32,
35     /// Battery full charge counter in microampere hours.
36     pub charge_full: u32,
37 }
38 
39 #[derive(Clone, Copy, Debug)]
40 pub enum BatteryStatus {
41     Unknown,
42     Charging,
43     Discharging,
44     NotCharging,
45 }
46 
47 pub trait CreatePowerMonitorFn:
48     Send + Fn() -> std::result::Result<Box<dyn PowerMonitor>, Box<dyn Error>>
49 {
50 }
51 
52 impl<T> CreatePowerMonitorFn for T where
53     T: Send + Fn() -> std::result::Result<Box<dyn PowerMonitor>, Box<dyn Error>>
54 {
55 }
56 
57 pub trait CreatePowerClientFn:
58     Send + Fn() -> std::result::Result<Box<dyn PowerClient>, Box<dyn Error>>
59 {
60 }
61 
62 impl<T> CreatePowerClientFn for T where
63     T: Send + Fn() -> std::result::Result<Box<dyn PowerClient>, Box<dyn Error>>
64 {
65 }
66 
67 #[cfg(feature = "powerd")]
68 pub mod powerd;
69 
70 mod protos {
71     // ANDROID: b/259142784 - we remove protos subdir b/c cargo2android
72     include!(concat!(env!("OUT_DIR"), "/generated.rs"));
73 }
74