Skip to content

Commit dccf403

Browse files
committed
smart_battery: Allow loading battery dump from file
The --smartbattery flag now accepts an optional file path to load previously dumped battery data instead of reading from the actual battery. This enables offline analysis and debugging. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 9ab9c1f commit dccf403

File tree

4 files changed

+99
-9
lines changed

4 files changed

+99
-9
lines changed

Cargo.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework_lib/src/commandline/clap_std.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Module to factor out commandline interaction
22
//! This way we can use it in the regular OS commandline tool on Linux and Windows,
33
//! as well as on the UEFI shell tool.
4+
use std::path::PathBuf;
5+
46
use clap::error::ErrorKind;
57
use clap::Parser;
68
use clap::{command, Arg, Args, FromArgMatches};
@@ -49,9 +51,9 @@ struct ClapCli {
4951
#[arg(long)]
5052
power: bool,
5153

52-
/// Show detailed smart battery information
53-
#[arg(long)]
54-
smartbattery: bool,
54+
/// Show detailed smart battery information, or load from dump file
55+
#[arg(long, value_name = "FILE")]
56+
smartbattery: Option<Option<PathBuf>>,
5557

5658
/// Authenticate smart battery (requires unseal and auth keys)
5759
#[arg(long)]
@@ -415,7 +417,9 @@ pub fn parse(args: &[String]) -> Cli {
415417
device: args.device,
416418
compare_version: args.compare_version,
417419
power: args.power,
418-
smartbattery: args.smartbattery,
420+
smartbattery: args
421+
.smartbattery
422+
.map(|opt| opt.map(|x| x.into_os_string().into_string().unwrap())),
419423
smartbattery_auth: args.smartbattery_auth,
420424
thermal: args.thermal,
421425
sensors: args.sensors,

framework_lib/src/commandline/mod.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub struct Cli {
169169
pub device: Option<HardwareDeviceType>,
170170
pub compare_version: Option<String>,
171171
pub power: bool,
172-
pub smartbattery: bool,
172+
pub smartbattery: Option<Option<String>>,
173173
pub smartbattery_auth: bool,
174174
pub thermal: bool,
175175
pub sensors: bool,
@@ -1502,11 +1502,26 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
15021502
print_board_ids(&ec);
15031503
} else if args.power {
15041504
return power::get_and_print_power_info(&ec);
1505-
} else if args.smartbattery {
1505+
} else if let Some(smartbattery_arg) = &args.smartbattery {
15061506
#[cfg(not(feature = "uefi"))]
15071507
{
1508-
let bat = SmartBattery::new();
1509-
print_err(bat.dump_data(&ec));
1508+
use crate::smart_battery::{display_battery_data, BatteryData};
1509+
use std::path::Path;
1510+
1511+
match smartbattery_arg {
1512+
Some(file_path) => {
1513+
// Load from file
1514+
match BatteryData::read_from_file(Path::new(file_path)) {
1515+
Ok(data) => display_battery_data(&data),
1516+
Err(e) => eprintln!("Failed to read battery dump: {}", e),
1517+
}
1518+
}
1519+
None => {
1520+
// Read from actual battery
1521+
let bat = SmartBattery::new();
1522+
print_err(bat.dump_data(&ec));
1523+
}
1524+
}
15101525
}
15111526
} else if args.smartbattery_auth {
15121527
#[cfg(not(feature = "uefi"))]

framework_lib/src/commandline/uefi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn parse(args: &[String]) -> Cli {
3535
device: None,
3636
compare_version: None,
3737
power: false,
38-
smartbattery: false,
38+
smartbattery: None,
3939
smartbattery_auth: false,
4040
thermal: false,
4141
sensors: false,

0 commit comments

Comments
 (0)