Skip to content

Commit

Permalink
feat: NetBSD support with envsys ioctl and plist (#69)
Browse files Browse the repository at this point in the history
* NetBSD support with envsys ioctl and plist

New examples, simple.rs only refreshed the first battery, and this does not
help checking for iterator validity for development, all.rs solves the
problem. The example oneshot.rs is just for comfort avoiding loop.

NetBSD exposes battery information through one ioctl so for now we have
to "waste" resource getting all sensors info for each single battery
refresh. We can't assume the library user will want to refresh all
bateries at the same time even if 90% of time it will probably be.

The result of the ioctl is a plist (apple format) that needs to be
parsed and accessed.

The plist library only uses options, so this is the reason for utils.rs.
These are all oneline functions to reduce the lenght of chains in the
netbsd platform device.rs file.

NetBSD does not expose battery vendor, model, technology, cycle among
other things for now, but these are optional.

Got inspiration from the FreeBSD port regarding having a better understanding on
how the library works.

Thanks to unitedbsd.com community for the guidance, and shout out to
https://github.com/distatus/battery/blob/master/battery_netbsd.go as
well as the NetBSD devs for the great man pages and the clear code
allowing to read what happens for undocumented things.

* Correcting rustfmt formatting on netbsd/device.rs

* Maybe crossbuild only as on FreeBSD ? (Already tried manually on Linux and it works)

* Revert "Maybe crossbuild only as on FreeBSD ? (Already tried manually on Linux and it works)"

Well the naive approach did not work

This reverts commit 693c882.

* Trying CICD regarding cross-rs

* Installing some prerequisites for NetBSD CI

* Correct path for libexecinfo.so in NetBSD CICD

* Clippy fixes for NetBSD platform

* Update src/platform/netbsd/iterator.rs

Remove default

Co-authored-by: David Knaack <[email protected]>

* Update Cross.toml

Co-authored-by: David Knaack <[email protected]>

* We support acpibat only in the end, but now we relyin on NetBSD kernel enum to ensure the data is correct even if someonechange envsys description fields. Extending the code for other battery driver is possible

* Updated the branch Cargo.toml to match updated version

---------

Co-authored-by: David Knaack <[email protected]>
  • Loading branch information
naguam and davidkna committed Jun 8, 2024
1 parent 00185a9 commit 87b8cc4
Show file tree
Hide file tree
Showing 15 changed files with 639 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ jobs:
- 1.69.0 # MSRV
target:
- x86_64-unknown-freebsd
- x86_64-unknown-netbsd
- aarch64-unknown-linux-gnu
- aarch64-unknown-linux-musl
- aarch64-pc-windows-msvc
- aarch64-apple-darwin
include:
- target: x86_64-unknown-freebsd
os: ubuntu-latest
- target: x86_64-unknown-netbsd
os: ubuntu-latest
- target: aarch64-unknown-linux-gnu
os: ubuntu-latest
- target: aarch64-unknown-linux-musl
Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Starship Contributors"]
build = "build.rs"
categories = ["os"]
edition = "2021"
keywords = ["battery", "linux", "macos", "windows", "freebsd"]
keywords = ["battery", "linux", "macos", "windows", "freebsd", "netbsd"]
license = "ISC"
readme = "README.md"
repository = "https://github.com/starship/rust-battery"
Expand Down Expand Up @@ -37,6 +37,11 @@ winapi = { version = "~0.3.9", features = ["impl-default", "devguid", "winbase",
libc = "~0.2.154"
nix = { version = "~0.29.0", default-features = false, features = ["ioctl"] }

[target.'cfg(target_os = "netbsd")'.dependencies]
libc = "~0.2.154"
nix = { version = "~0.29.0", default-features = false, features = ["ioctl", "mman"] }
plist = "~1.6"

[dev-dependencies]
approx = "0.5.1"
tempfile = "^3.10.1"
6 changes: 6 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ passthrough = [
"RUST_BACKTRACE",
"RUST_LOG",
]

# Fix already here https://github.com/cross-rs/cross/blob/main/docker/netbsd.sh
# But no new release so temporary fix here instead
# Remove once next cross release is out
[target.x86_64-unknown-netbsd]
image = "ghcr.io/cross-rs/x86_64-unknown-netbsd:main"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ as a typed values, recalculated as necessary to be returned as a [SI measurement
- Windows 7+
- FreeBSD
- DragonFlyBSD
- NetBSD

Do note that iOS implementation uses IOKit bindings, your application
might be automatically rejected by Apple based on that fact. Use it on your own risk.
Expand All @@ -50,7 +51,7 @@ Add the following line into a `Cargo.toml`:

```toml
[dependencies]
battery = "0.7.8"
battery = "0.8.3"
```

## Examples
Expand Down
33 changes: 33 additions & 0 deletions examples/all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extern crate starship_battery as battery;

use std::thread;
use std::time::Duration;
use std::vec::Vec;

use battery::Battery;

fn main() -> battery::Result<()> {
let manager = battery::Manager::new()?;
let mut vc: Vec<Battery> = Vec::<Battery>::new();

let iter = manager.batteries()?;

for bat in iter {
vc.push(match bat {
Ok(battery) => battery,
Err(e) => {
eprintln!("Unable to access battery information");
return Err(e);
}
})
}

loop {
for bat in &mut vc {
println!("{:?}", bat);
thread::sleep(Duration::from_secs(1));
manager.refresh(bat)?;
}
println!("Back to the beginning")
}
}
21 changes: 21 additions & 0 deletions examples/oneshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate starship_battery as battery;

use std::io;

fn main() -> battery::Result<()> {
let manager = battery::Manager::new()?;
let battery = match manager.batteries()?.next() {
Some(Ok(battery)) => battery,
Some(Err(e)) => {
eprintln!("Unable to access battery information");
return Err(e);
}
None => {
eprintln!("Unable to find any batteries");
return Err(io::Error::from(io::ErrorKind::NotFound).into());
}
};

println!("{:?}", battery);
Ok(())
}
13 changes: 12 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl From<io::Error> for Error {
}
}

#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))]
mod nix_impl {
use std::io;

Expand All @@ -93,3 +93,14 @@ mod nix_impl {
}
}
}

#[cfg(target_os = "netbsd")]
mod plist_impl {
use super::Error;

impl From<plist::Error> for Error {
fn from(e: plist::Error) -> Self {
e.into()
}
}
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! * Windows 7+
//! * FreeBSD
//! * DragonFlyBSD
//! * NetBSD
//!
//! ## Examples
//!
Expand All @@ -32,10 +33,13 @@ extern crate cfg_if;
#[macro_use]
extern crate winapi;

#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))]
#[macro_use]
extern crate nix;

#[cfg(target_os = "netbsd")]
extern crate plist;

mod types;
#[macro_use]
pub mod units;
Expand Down
6 changes: 6 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ cfg_if! {
pub type Manager = freebsd::IoCtlManager;
pub type Iterator = freebsd::IoCtlIterator;
pub type Device = freebsd::IoCtlDevice;
} else if #[cfg(target_os = "netbsd")] {
mod netbsd;

pub type Manager = netbsd::SysMonManager;
pub type Iterator = netbsd::SysMonIterator;
pub type Device = netbsd::SysMonDevice;
} else {
compile_error!("Support for this target OS is not implemented yet!\n \
You may want to create an issue: https://github.com/starship/rust-battery/issues/new");
Expand Down
Loading

0 comments on commit 87b8cc4

Please sign in to comment.