Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover battery from sysfs #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions src/battery/kobo.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::env;
use std::fs::File;
use std::path::Path;
use std::fs::{File, read_dir, read_to_string};
use std::io::{Read, Seek, SeekFrom};
use super::{Battery, Status};
use anyhow::{Error, format_err};

const BATTERY_INTERFACE_A: &str = "/sys/class/power_supply/mc13892_bat";
const BATTERY_INTERFACE_B: &str = "/sys/class/power_supply/battery";
const BATTERY_INTERFACE: &str = "/sys/class/power_supply";

const BATTERY_CAPACITY: &str = "capacity";
const BATTERY_STATUS: &str = "status";
Expand All @@ -19,17 +16,13 @@ pub struct KoboBattery {

impl KoboBattery {
pub fn new() -> Result<KoboBattery, Error> {
let mut firmware_version = [0u16; 3];
env::var("FIRMWARE_VERSION").ok()
.map(|s| s.split('.')
.filter_map(|v| v.parse::<u16>().ok())
.zip(firmware_version.iter_mut())
.for_each(|(a, b)| *b = a));
let base = if firmware_version < [4, 28, 17623] {
Path::new(BATTERY_INTERFACE_A)
} else {
Path::new(BATTERY_INTERFACE_B)
};
let base = read_dir(BATTERY_INTERFACE)?
.filter_map(Result::ok)
.map(|dir| dir.path())
.find(|path| {
let kind = read_to_string(path.join("type")).unwrap_or_default();
kind.trim_end() == "Battery"
}).expect("Could not find battery");
let capacity = File::open(base.join(BATTERY_CAPACITY))?;
let status = File::open(base.join(BATTERY_STATUS))?;
Ok(KoboBattery { capacity, status })
Expand Down