power_monitor/
lib.rs

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
7use std::error::Error;
8use std::time::SystemTime;
9
10use base::ReadNotifier;
11
12pub trait PowerMonitor: ReadNotifier {
13    fn read_message(&mut self) -> std::result::Result<Option<PowerData>, Box<dyn Error>>;
14}
15
16pub trait PowerClient: Send {
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.
20    fn last_request_timestamp(&self) -> Option<SystemTime>;
21}
22
23#[derive(Debug)]
24pub struct PowerData {
25    pub ac_online: bool,
26    pub battery: Option<BatteryData>,
27}
28
29#[derive(Clone, Copy, Debug)]
30pub 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)]
44pub enum BatteryStatus {
45    Unknown,
46    Charging,
47    Discharging,
48    NotCharging,
49}
50
51pub trait CreatePowerMonitorFn:
52    Send + Fn() -> std::result::Result<Box<dyn PowerMonitor>, Box<dyn Error>>
53{
54}
55
56impl<T> CreatePowerMonitorFn for T where
57    T: Send + Fn() -> std::result::Result<Box<dyn PowerMonitor>, Box<dyn Error>>
58{
59}
60
61pub trait CreatePowerClientFn:
62    Send + Fn() -> std::result::Result<Box<dyn PowerClient>, Box<dyn Error>>
63{
64}
65
66impl<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")]
72pub mod powerd;
73
74#[cfg(feature = "powerd")]
75mod protos {
76    include!(concat!(env!("OUT_DIR"), "/protos/generated.rs"));
77}