garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /aml/src/battery.rs

raw

//! A control-method battery (ACPI 6.5, 10.2): what `_BIF` or `_BIX`
//! (what it is) and `_BST` (how it is now) answer, decoded.

use crate::Value;
use alloc::string::String;

/// What the firmware says when it does not know.
pub const UNKNOWN: u32 = 0xffff_ffff;

/// What a battery is.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct Info {
    /// Capacities in mWh (true) or mAh (false).
    pub milliwatts: bool,
    pub design_capacity: u32,
    pub full_capacity: u32,
    pub rechargeable: bool,
    /// mV.
    pub design_voltage: u32,
    pub warning: u32,
    pub low: u32,
    /// Charge cycles, where `_BIX` says ([`UNKNOWN`] else).
    pub cycles: u32,
    pub model: String,
    pub serial: String,
    pub kind: String,
    pub maker: String,
}

/// How a battery is now.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct Status {
    pub discharging: bool,
    pub charging: bool,
    pub critical: bool,
    /// mW or mA, as [`Info::milliwatts`] says.
    pub rate: u32,
    /// mWh or mAh.
    pub remaining: u32,
    /// mV.
    pub voltage: u32,
}

fn int(p: &[Value], i: usize) -> u32 {
    p.get(i).and_then(Value::integer).map_or(UNKNOWN, |v| v as u32)
}

fn text(p: &[Value], i: usize) -> String {
    match p.get(i) {
        Some(Value::String(s)) => String::from(s.trim_end_matches('\0').trim()),
        Some(Value::Buffer(b)) => {
            let end = b.iter().position(|&x| x == 0).unwrap_or(b.len());
            String::from_utf8_lossy(&b[..end]).trim().into()
        }
        _ => String::new(),
    }
}

impl Info {
    /// From `_BIF`'s package.
    pub fn from_bif(v: &Value) -> Option<Info> {
        let p = v.package()?;
        Some(Info {
            milliwatts: int(p, 0) == 0,
            design_capacity: int(p, 1),
            full_capacity: int(p, 2),
            rechargeable: int(p, 3) == 1,
            design_voltage: int(p, 4),
            warning: int(p, 5),
            low: int(p, 6),
            cycles: UNKNOWN,
            model: text(p, 9),
            serial: text(p, 10),
            kind: text(p, 11),
            maker: text(p, 12),
        })
    }

    /// From `_BIX`'s package (its revision first).
    pub fn from_bix(v: &Value) -> Option<Info> {
        let p = v.package()?;
        Some(Info {
            milliwatts: int(p, 1) == 0,
            design_capacity: int(p, 2),
            full_capacity: int(p, 3),
            rechargeable: int(p, 4) == 1,
            design_voltage: int(p, 5),
            warning: int(p, 6),
            low: int(p, 7),
            cycles: int(p, 8),
            model: text(p, 16),
            serial: text(p, 17),
            kind: text(p, 18),
            maker: text(p, 19),
        })
    }
}

impl Status {
    /// From `_BST`'s package.
    pub fn from_bst(v: &Value) -> Option<Status> {
        let p = v.package()?;
        let state = int(p, 0);
        Some(Status {
            discharging: state & 1 != 0,
            charging: state & 2 != 0,
            critical: state & 4 != 0,
            rate: int(p, 1),
            remaining: int(p, 2),
            voltage: int(p, 3),
        })
    }

    /// How full, in percent of the last full charge, where both are known.
    pub fn percent(&self, info: &Info) -> Option<u8> {
        let full = if info.full_capacity == UNKNOWN || info.full_capacity == 0 {
            info.design_capacity
        } else {
            info.full_capacity
        };
        if self.remaining == UNKNOWN || full == UNKNOWN || full == 0 {
            return None;
        }
        Some((u64::from(self.remaining) * 100 / u64::from(full)).min(100) as u8)
    }

    /// Minutes left at the present rate, discharging; to full, charging.
    pub fn minutes(&self, info: &Info) -> Option<u32> {
        if self.rate == UNKNOWN || self.rate == 0 || self.remaining == UNKNOWN {
            return None;
        }
        let left = if self.charging {
            info.full_capacity.checked_sub(self.remaining)?
        } else if self.discharging {
            self.remaining
        } else {
            return None;
        };
        Some((u64::from(left) * 60 / u64::from(self.rate)) as u32)
    }
}