NetBSD Implementation (Sysmon)
Relevant source files
The following files were used as context for generating this wiki page:
The NetBSD implementation of the battery crate provides access to battery information via the NetBSD envsys (Environmental Systems) framework and the sysmon device. It specifically supports the acpibat driver, which exposes battery metrics such as charge state, voltage, and capacity through ioctl calls and Property List (plist) data structures.
This module translates platform-specific dictionary values into a unified SI measurement units format. It enables the crate to support NetBSD alongside other Unix-like systems such as FreeBSD and Linux.
Sources: src/platform/netbsd/device.rs:18-20, src/lib.rs:8-13, CHANGELOG.md:46-48
Architecture and Data Flow
The NetBSD implementation follows a two-stage data processing pipeline. First, it extracts raw data from the system using the envsys interface into an intermediate EnvSysDevice. Second, it transforms that raw data into a SysMonDevice that implements the standard BatteryDevice trait used throughout the library.
Data Extraction Pipeline
The implementation relies on the plist crate to parse dictionary-based responses from the NetBSD kernel. The EnvSysDevice struct maps specific ACPI battery (acpibat) sensor indices to battery properties.
Key Components
EnvSysDevice
This internal struct acts as the raw data container. It validates that the hardware device is an acpibat driver and that the device class is indeed a battery. It handles the extraction of current, full, and design capacities, as well as charge/discharge rates.
| Field | Description | Source Index |
|---|---|---|
name | The device name (must start with acpibat) | N/A |
energy | Current charge level | ACPIBAT_CAPACITY |
energy_full | Last full capacity | ACPIBAT_LFCCAPACITY |
energy_full_design | Design capacity | ACPIBAT_DCAPACITY |
charge_rate | Current charging rate | ACPIBAT_CHARGERATE |
discharge_rate | Current discharging rate | ACPIBAT_DISCHARGERATE |
charging | Charging state flag | ACPIBAT_CHARGING |
Sources: src/platform/netbsd/device.rs:25-46, src/platform/netbsd/device.rs:51-100
SysMonDevice
This is the primary implementation of the BatteryDevice trait for NetBSD. It stores converted SI units using the uom crate.
Unit Conversion Logic
NetBSD can report battery metrics in different units depending on the hardware. The implementation specifically handles "Ampere hour" and "Watt hour" for capacity, and "Ampere" or "Watts" for power rates.
- Capacity Conversion: If units are in Ampere hours, the value is multiplied by the design voltage to derive Energy (Watt-hours).
- Rate Conversion: If power is reported in Amperes, it is multiplied by the current voltage to determine the Energy Rate in Watts.
- State Determination: The state is derived by comparing current energy to full energy (setting
State::Fullif equal or greater) or checking thechargingflag (1 forCharging, 0 forDischarging).
Sources: src/platform/netbsd/device.rs:141-175
Platform Integration
The NetBSD implementation is conditionally compiled and aliased to the standard Manager, Iterator, and Device types when the target OS is netbsd.
// src/platform/mod.rs:31-35
} else if #[cfg(target_os = "netbsd")] {
mod netbsd;
pub type Manager = netbsd::SysMonManager;
pub type Iterator = netbsd::SysMonIterator;
pub type Device = netbsd::SysMonDevice;
}
The Cargo.toml specifies required dependencies for this platform, including nix with ioctl and mman features, and the plist crate for data parsing.
Sources: src/platform/mod.rs:31-35, Cargo.toml:53-56
Implementation Limitations
As noted in the source code, the NetBSD implementation does not provide all possible battery metadata. The following fields are currently unsupported and return None or Unknown:
- Technology: Always returns
Technology::Unknown. - Temperature: Not available (returns
None). - Vendor/Model/Serial: Not exposed by the current
acpibatsysmon interface (returnsNone). - Cycle Count: Not available (returns
None).
Sources: src/platform/netbsd/device.rs:188-251
Summary
The NetBSD Implementation (Sysmon) provides a robust bridge between the NetBSD envsys kernel interface and the rust-battery unified API. By parsing kernel plists and performing unit conversions from Amperes/Ampere-hours to SI Watts/Joules, it ensures consistent battery monitoring capabilities for NetBSD users.