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 use std::time::SystemTime; 9 10 use base::ReadNotifier; 11 12 pub trait PowerMonitor: ReadNotifier { read_message(&mut self) -> std::result::Result<Option<PowerData>, Box<dyn Error>>13 fn read_message(&mut self) -> std::result::Result<Option<PowerData>, Box<dyn Error>>; 14 } 15 16 pub trait PowerClient { get_power_data(&mut self) -> std::result::Result<PowerData, Box<dyn Error>>17 fn get_power_data(&mut self) -> std::result::Result<PowerData, Box<dyn Error>>; 18 19 /// Returns timestamp that this client sends a DBus request. last_request_timestamp(&self) -> Option<SystemTime>20 fn last_request_timestamp(&self) -> Option<SystemTime>; 21 } 22 23 #[derive(Debug)] 24 pub struct PowerData { 25 pub ac_online: bool, 26 pub battery: Option<BatteryData>, 27 } 28 29 #[derive(Clone, Copy, Debug)] 30 pub struct BatteryData { 31 pub status: BatteryStatus, 32 pub percent: u32, 33 /// Battery voltage in microvolts. 34 pub voltage: u32, 35 /// Battery current in microamps. 36 pub current: u32, 37 /// Battery charge counter in microampere hours. 38 pub charge_counter: u32, 39 /// Battery full charge counter in microampere hours. 40 pub charge_full: u32, 41 } 42 43 #[derive(Clone, Copy, Debug)] 44 pub enum BatteryStatus { 45 Unknown, 46 Charging, 47 Discharging, 48 NotCharging, 49 } 50 51 pub trait CreatePowerMonitorFn: 52 Send + Fn() -> std::result::Result<Box<dyn PowerMonitor>, Box<dyn Error>> 53 { 54 } 55 56 impl<T> CreatePowerMonitorFn for T where 57 T: Send + Fn() -> std::result::Result<Box<dyn PowerMonitor>, Box<dyn Error>> 58 { 59 } 60 61 pub trait CreatePowerClientFn: 62 Send + Fn() -> std::result::Result<Box<dyn PowerClient>, Box<dyn Error>> 63 { 64 } 65 66 impl<T> CreatePowerClientFn for T where 67 T: Send + Fn() -> std::result::Result<Box<dyn PowerClient>, Box<dyn Error>> 68 { 69 } 70 71 #[cfg(feature = "powerd")] 72 pub mod powerd; 73 74 #[cfg(feature = "powerd")] 75 mod protos { 76 // ANDROID: b/259142784 - we remove protos subdir b/c cargo2android 77 include!(concat!(env!("OUT_DIR"), "/generated.rs")); 78 } 79