From 3c466caf07c4c5ca58150e5b190e7a636b9c6f8c Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Wed, 15 May 2024 17:55:52 -0400 Subject: [PATCH] Initial commit --- .gitignore | 4 ++ Cargo.toml | 21 +++++++++ LICENSE | 21 +++++++++ README.md | 4 ++ crates/batbox/Cargo.toml | 9 ++++ crates/batbox/src/lib.rs | 16 +++++++ crates/batbox/src/supply_trait.rs | 5 +++ crates/core/Cargo.toml | 11 +++++ crates/core/src/lib.rs | 22 ++++++++++ crates/core/src/power_error.rs | 4 ++ crates/core/src/power_trait.rs | 21 +++++++++ crates/core/src/support.rs | 7 +++ crates/core/src/value.rs | 67 +++++++++++++++++++++++++++++ crates/procbox/Cargo.toml | 9 ++++ crates/procbox/src/cpu/cpu_trait.rs | 5 +++ crates/procbox/src/cpu/mod.rs | 3 ++ crates/procbox/src/gpu/gpu_trait.rs | 5 +++ crates/procbox/src/gpu/mod.rs | 3 ++ crates/procbox/src/lib.rs | 16 +++++++ src/lib.rs | 14 ++++++ 20 files changed, 267 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 crates/batbox/Cargo.toml create mode 100644 crates/batbox/src/lib.rs create mode 100644 crates/batbox/src/supply_trait.rs create mode 100644 crates/core/Cargo.toml create mode 100644 crates/core/src/lib.rs create mode 100644 crates/core/src/power_error.rs create mode 100644 crates/core/src/power_trait.rs create mode 100644 crates/core/src/support.rs create mode 100644 crates/core/src/value.rs create mode 100644 crates/procbox/Cargo.toml create mode 100644 crates/procbox/src/cpu/cpu_trait.rs create mode 100644 crates/procbox/src/cpu/mod.rs create mode 100644 crates/procbox/src/gpu/gpu_trait.rs create mode 100644 crates/procbox/src/gpu/mod.rs create mode 100644 crates/procbox/src/lib.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c934a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +/crates/**/target + +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d76d068 --- /dev/null +++ b/Cargo.toml @@ -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) "] +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" + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7a235e1 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..13e5583 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# powerbox + +Power toolbox for Linux devices + diff --git a/crates/batbox/Cargo.toml b/crates/batbox/Cargo.toml new file mode 100644 index 0000000..b18b02e --- /dev/null +++ b/crates/batbox/Cargo.toml @@ -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" } diff --git a/crates/batbox/src/lib.rs b/crates/batbox/src/lib.rs new file mode 100644 index 0000000..a0d2855 --- /dev/null +++ b/crates/batbox/src/lib.rs @@ -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); + } +} diff --git a/crates/batbox/src/supply_trait.rs b/crates/batbox/src/supply_trait.rs new file mode 100644 index 0000000..1f5b322 --- /dev/null +++ b/crates/batbox/src/supply_trait.rs @@ -0,0 +1,5 @@ +/// Power control interface for a power supply +pub trait SupplyPower: powerbox::Power {} + +/// Power control operation(s) of a power supply +pub trait SupplyPowerOp: powerbox::PowerOp {} diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml new file mode 100644 index 0000000..3be74da --- /dev/null +++ b/crates/core/Cargo.toml @@ -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" } diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs new file mode 100644 index 0000000..c509521 --- /dev/null +++ b/crates/core/src/lib.rs @@ -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); + } +} diff --git a/crates/core/src/power_error.rs b/crates/core/src/power_error.rs new file mode 100644 index 0000000..dc81cb3 --- /dev/null +++ b/crates/core/src/power_error.rs @@ -0,0 +1,4 @@ +/// All possible errors returned by the [Power] trait. +pub enum PowerError { + +} diff --git a/crates/core/src/power_trait.rs b/crates/core/src/power_trait.rs new file mode 100644 index 0000000..be84514 --- /dev/null +++ b/crates/core/src/power_trait.rs @@ -0,0 +1,21 @@ +use super::PowerError; +use super::Value; + +/// Power control interface for a device +pub trait Power { + /// 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>; + /// Perform a power operation + fn act(&self, op: OP) -> Result; +} + +/// 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; +} diff --git a/crates/core/src/support.rs b/crates/core/src/support.rs new file mode 100644 index 0000000..dee4c9f --- /dev/null +++ b/crates/core/src/support.rs @@ -0,0 +1,7 @@ +/// Support status of a power interface +pub enum Support { + Get, + Set, + GetSet, + Unsupported, +} diff --git a/crates/core/src/value.rs b/crates/core/src/value.rs new file mode 100644 index 0000000..2be1e22 --- /dev/null +++ b/crates/core/src/value.rs @@ -0,0 +1,67 @@ +/// Value of a power reading +pub enum Value> { + /// 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 { + 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 { + 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 { + 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(_)) + } +} diff --git a/crates/procbox/Cargo.toml b/crates/procbox/Cargo.toml new file mode 100644 index 0000000..73e0d6a --- /dev/null +++ b/crates/procbox/Cargo.toml @@ -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" } diff --git a/crates/procbox/src/cpu/cpu_trait.rs b/crates/procbox/src/cpu/cpu_trait.rs new file mode 100644 index 0000000..1a85075 --- /dev/null +++ b/crates/procbox/src/cpu/cpu_trait.rs @@ -0,0 +1,5 @@ +/// Power control interface for a CPU +pub trait CpuPower: powerbox::Power {} + +/// Power control operation(s) of a CPU +pub trait CpuPowerOp: powerbox::PowerOp {} diff --git a/crates/procbox/src/cpu/mod.rs b/crates/procbox/src/cpu/mod.rs new file mode 100644 index 0000000..77e2dbd --- /dev/null +++ b/crates/procbox/src/cpu/mod.rs @@ -0,0 +1,3 @@ +//! CPU power management interface +mod cpu_trait; +pub use cpu_trait::{CpuPower, CpuPowerOp}; diff --git a/crates/procbox/src/gpu/gpu_trait.rs b/crates/procbox/src/gpu/gpu_trait.rs new file mode 100644 index 0000000..02545f1 --- /dev/null +++ b/crates/procbox/src/gpu/gpu_trait.rs @@ -0,0 +1,5 @@ +/// Power control interface for a GPU +pub trait GpuPower: powerbox::Power {} + +/// Power control operation(s) of a GPU +pub trait GpuPowerOp: powerbox::PowerOp {} diff --git a/crates/procbox/src/gpu/mod.rs b/crates/procbox/src/gpu/mod.rs new file mode 100644 index 0000000..ef0dd04 --- /dev/null +++ b/crates/procbox/src/gpu/mod.rs @@ -0,0 +1,3 @@ +//! GPU power management interface +mod gpu_trait; +pub use gpu_trait::{GpuPower, GpuPowerOp}; diff --git a/crates/procbox/src/lib.rs b/crates/procbox/src/lib.rs new file mode 100644 index 0000000..df886b3 --- /dev/null +++ b/crates/procbox/src/lib.rs @@ -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); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/src/lib.rs @@ -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); + } +}