Initial commit
This commit is contained in:
commit
3c466caf07
20 changed files with 267 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
/target
|
||||||
|
/crates/**/target
|
||||||
|
|
||||||
|
Cargo.lock
|
21
Cargo.toml
Normal file
21
Cargo.toml
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
[package]
|
||||||
|
name = "powerbox-cli"
|
||||||
|
|
||||||
|
[workspace.package]
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
description = "Power toolbox for Linux devices"
|
||||||
|
authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
|
||||||
|
license = "MIT"
|
||||||
|
repository = "https://git.ngni.us/NGnius/powerbox"
|
||||||
|
keywords = ["utility", "linux", "power-management"]
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[workspace.dependencies]
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
members = [ "crates/batbox", "crates/core", "crates/procbox" ]
|
||||||
|
resolver = "2"
|
||||||
|
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 NGnius
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
4
README.md
Normal file
4
README.md
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# powerbox
|
||||||
|
|
||||||
|
Power toolbox for Linux devices
|
||||||
|
|
9
crates/batbox/Cargo.toml
Normal file
9
crates/batbox/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "batbox"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
readme = "../../README.md"
|
||||||
|
description = "Power toolbox for power supply devices"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
powerbox = { version = "0.1", path = "../core" }
|
16
crates/batbox/src/lib.rs
Normal file
16
crates/batbox/src/lib.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
//! Userspace library for Linux power supply management.
|
||||||
|
|
||||||
|
mod supply_trait;
|
||||||
|
pub use supply_trait::{SupplyPower, SupplyPowerOp};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = 2 + 2;
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
5
crates/batbox/src/supply_trait.rs
Normal file
5
crates/batbox/src/supply_trait.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
/// Power control interface for a power supply
|
||||||
|
pub trait SupplyPower<OP: SupplyPowerOp, VAL = powerbox::Value>: powerbox::Power<OP, VAL> {}
|
||||||
|
|
||||||
|
/// Power control operation(s) of a power supply
|
||||||
|
pub trait SupplyPowerOp: powerbox::PowerOp {}
|
11
crates/core/Cargo.toml
Normal file
11
crates/core/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "powerbox"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
readme = "../../README.md"
|
||||||
|
description = "Power toolbox for Linux devices"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
sysfuss = { version = "0.3", path = "../../../sysfs-nav" }
|
22
crates/core/src/lib.rs
Normal file
22
crates/core/src/lib.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
//! Userspace library for Linux power management.
|
||||||
|
|
||||||
|
mod power_error;
|
||||||
|
pub use power_error::PowerError;
|
||||||
|
|
||||||
|
mod power_trait;
|
||||||
|
pub use power_trait::{Power, PowerOp};
|
||||||
|
|
||||||
|
mod value;
|
||||||
|
pub use value::Value;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = 2 + 2;
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
4
crates/core/src/power_error.rs
Normal file
4
crates/core/src/power_error.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
/// All possible errors returned by the [Power] trait.
|
||||||
|
pub enum PowerError {
|
||||||
|
|
||||||
|
}
|
21
crates/core/src/power_trait.rs
Normal file
21
crates/core/src/power_trait.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
use super::PowerError;
|
||||||
|
use super::Value;
|
||||||
|
|
||||||
|
/// Power control interface for a device
|
||||||
|
pub trait Power<OP: PowerOp, VAL = Value> {
|
||||||
|
/// Is this device online?
|
||||||
|
fn is_on(&self) -> bool;
|
||||||
|
/// Is this device connected?
|
||||||
|
fn is_available(&self) -> bool;
|
||||||
|
/// List supported functionality
|
||||||
|
fn supported_operations(&self) -> Box<dyn core::iter::Iterator<Item=OP>>;
|
||||||
|
/// Perform a power operation
|
||||||
|
fn act(&self, op: OP) -> Result<VAL, PowerError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Power control operation(s) of a device
|
||||||
|
pub trait PowerOp {
|
||||||
|
/// Do the operations match?
|
||||||
|
/// This should ignore values of the operation.
|
||||||
|
fn is_eq_op(&self, other: Self) -> bool;
|
||||||
|
}
|
7
crates/core/src/support.rs
Normal file
7
crates/core/src/support.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
/// Support status of a power interface
|
||||||
|
pub enum Support {
|
||||||
|
Get,
|
||||||
|
Set,
|
||||||
|
GetSet,
|
||||||
|
Unsupported,
|
||||||
|
}
|
67
crates/core/src/value.rs
Normal file
67
crates/core/src/value.rs
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
/// Value of a power reading
|
||||||
|
pub enum Value<C = Box<dyn std::any::Any>> {
|
||||||
|
/// floating point value
|
||||||
|
Float(f64),
|
||||||
|
/// unsigned integer value
|
||||||
|
UInt(u64),
|
||||||
|
/// signed integer value
|
||||||
|
Int(i64),
|
||||||
|
/// boolean value
|
||||||
|
Bool(bool),
|
||||||
|
/// custom value
|
||||||
|
Custom(C),
|
||||||
|
/// unknown value
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Value {
|
||||||
|
/// Convert to f64, if numeric type
|
||||||
|
pub fn as_f64(&self) -> Option<f64> {
|
||||||
|
match self {
|
||||||
|
Self::Float(f) => Some(*f),
|
||||||
|
Self::UInt(u) => Some(*u as _),
|
||||||
|
Self::Int(i) => Some(*i as _),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert to u64, if numeric type
|
||||||
|
pub fn as_u64(&self) -> Option<u64> {
|
||||||
|
match self {
|
||||||
|
Self::Float(f) => Some(*f as _),
|
||||||
|
Self::UInt(u) => Some(*u),
|
||||||
|
Self::Int(i) => Some(*i as _),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert to u64, if numeric type
|
||||||
|
pub fn as_i64(&self) -> Option<i64> {
|
||||||
|
match self {
|
||||||
|
Self::Float(f) => Some(*f as _),
|
||||||
|
Self::UInt(u) => Some(*u as _),
|
||||||
|
Self::Int(i) => Some(*i),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is value some sort of number?
|
||||||
|
pub fn is_numeric(&self) -> bool {
|
||||||
|
matches!(self, Self::Float(_) | Self::Int(_) | Self::UInt(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is value unknown?
|
||||||
|
pub fn is_unknown(&self) -> bool {
|
||||||
|
matches!(self, Self::Unknown)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is value a boolean?
|
||||||
|
pub fn is_bool(&self) -> bool {
|
||||||
|
matches!(self, Self::Bool(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is value a custom type?
|
||||||
|
pub fn is_custom(&self) -> bool {
|
||||||
|
matches!(self, Self::Custom(_))
|
||||||
|
}
|
||||||
|
}
|
9
crates/procbox/Cargo.toml
Normal file
9
crates/procbox/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "procbox"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
readme = "../../README.md"
|
||||||
|
description = "Power toolbox for processors"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
powerbox = { version = "0.1", path = "../core" }
|
5
crates/procbox/src/cpu/cpu_trait.rs
Normal file
5
crates/procbox/src/cpu/cpu_trait.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
/// Power control interface for a CPU
|
||||||
|
pub trait CpuPower<OP: CpuPowerOp, VAL = powerbox::Value>: powerbox::Power<OP, VAL> {}
|
||||||
|
|
||||||
|
/// Power control operation(s) of a CPU
|
||||||
|
pub trait CpuPowerOp: powerbox::PowerOp {}
|
3
crates/procbox/src/cpu/mod.rs
Normal file
3
crates/procbox/src/cpu/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
//! CPU power management interface
|
||||||
|
mod cpu_trait;
|
||||||
|
pub use cpu_trait::{CpuPower, CpuPowerOp};
|
5
crates/procbox/src/gpu/gpu_trait.rs
Normal file
5
crates/procbox/src/gpu/gpu_trait.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
/// Power control interface for a GPU
|
||||||
|
pub trait GpuPower<OP: GpuPowerOp, VAL = powerbox::Value>: powerbox::Power<OP, VAL> {}
|
||||||
|
|
||||||
|
/// Power control operation(s) of a GPU
|
||||||
|
pub trait GpuPowerOp: powerbox::PowerOp {}
|
3
crates/procbox/src/gpu/mod.rs
Normal file
3
crates/procbox/src/gpu/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
//! GPU power management interface
|
||||||
|
mod gpu_trait;
|
||||||
|
pub use gpu_trait::{GpuPower, GpuPowerOp};
|
16
crates/procbox/src/lib.rs
Normal file
16
crates/procbox/src/lib.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
//! Userspace library for Linux processor power management.
|
||||||
|
|
||||||
|
pub mod cpu;
|
||||||
|
pub mod gpu;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = 2 + 2;
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
14
src/lib.rs
Normal file
14
src/lib.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
pub fn add(left: usize, right: usize) -> usize {
|
||||||
|
left + right
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = add(2, 2);
|
||||||
|
assert_eq!(result, 4);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue