Skip to content

Commit c2e8bed

Browse files
Merge pull request #142 from FrameworkComputer/gpioget-all
--get-gpio: Return all GPIOs if no name was passed
2 parents ba01028 + e4832fe commit c2e8bed

File tree

6 files changed

+106
-12
lines changed

6 files changed

+106
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ Options:
171171
--expansion-bay Show status of the expansion bay (Framework 16 only)
172172
--charge-limit [<CHARGE_LIMIT>]
173173
Get or set max charge limit
174-
--get-gpio <GET_GPIO>
175-
Get GPIO value by name
174+
--get-gpio [<GET_GPIO>]
175+
Get GPIO value by name or all, if no name provided
176176
--fp-led-level [<FP_LED_LEVEL>]
177177
Get or set fingerprint LED brightness level [possible values: high, medium, low, ultra-low, auto]
178178
--fp-brightness [<FP_BRIGHTNESS>]

framework_lib/src/chromium_ec/commands.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,61 @@ impl EcRequest<EcResponseGpioGetV0> for EcRequestGpioGetV0 {
343343
}
344344
}
345345

346+
pub enum GpioGetSubCommand {
347+
ByName = 0,
348+
Count = 1,
349+
Info = 2,
350+
}
351+
352+
#[repr(C, packed)]
353+
pub struct EcRequestGpioGetV1Count {
354+
pub subcmd: u8,
355+
}
356+
357+
#[repr(C, packed)]
358+
pub struct EcRequestGpioGetV1ByName {
359+
pub subcmd: u8,
360+
pub name: [u8; 32],
361+
}
362+
363+
#[repr(C, packed)]
364+
pub struct EcRequestGpioGetV1Info {
365+
pub subcmd: u8,
366+
pub index: u8,
367+
}
368+
369+
#[repr(C)]
370+
pub struct EcResponseGpioGetV1Info {
371+
pub val: u8,
372+
pub name: [u8; 32],
373+
pub flags: u32,
374+
}
375+
376+
impl EcRequest<EcResponseGpioGetV0> for EcRequestGpioGetV1Count {
377+
fn command_id() -> EcCommands {
378+
EcCommands::GpioGet
379+
}
380+
fn command_version() -> u8 {
381+
1
382+
}
383+
}
384+
impl EcRequest<EcResponseGpioGetV0> for EcRequestGpioGetV1ByName {
385+
fn command_id() -> EcCommands {
386+
EcCommands::GpioGet
387+
}
388+
fn command_version() -> u8 {
389+
1
390+
}
391+
}
392+
impl EcRequest<EcResponseGpioGetV1Info> for EcRequestGpioGetV1Info {
393+
fn command_id() -> EcCommands {
394+
EcCommands::GpioGet
395+
}
396+
fn command_version() -> u8 {
397+
1
398+
}
399+
}
400+
346401
#[repr(C, packed)]
347402
pub struct EcRequestReboot {}
348403

framework_lib/src/chromium_ec/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,41 @@ impl CrosEc {
12421242
Ok(res.val == 1)
12431243
}
12441244

1245+
pub fn get_all_gpios(&self) -> EcResult<u8> {
1246+
let res = EcRequestGpioGetV1Count {
1247+
subcmd: GpioGetSubCommand::Count as u8,
1248+
}
1249+
.send_command(self)?;
1250+
let gpio_count = res.val;
1251+
1252+
debug!("Found {} GPIOs", gpio_count);
1253+
1254+
for index in 0..res.val {
1255+
let res = EcRequestGpioGetV1Info {
1256+
subcmd: GpioGetSubCommand::Info as u8,
1257+
index,
1258+
}
1259+
.send_command(self)?;
1260+
1261+
let name = std::str::from_utf8(&res.name)
1262+
.map_err(|utf8_err| {
1263+
EcError::DeviceError(format!("Failed to decode GPIO name: {:?}", utf8_err))
1264+
})?
1265+
.trim_end_matches(char::from(0))
1266+
.to_string();
1267+
1268+
if log_enabled!(Level::Info) {
1269+
// Same output as ectool
1270+
println!("{:>32}: {:>2} 0x{:04X}", res.val, name, { res.flags });
1271+
} else {
1272+
// Simple output, just name and level high/low
1273+
println!("{:<32} {}", name, res.val);
1274+
}
1275+
}
1276+
1277+
Ok(gpio_count)
1278+
}
1279+
12451280
pub fn adc_read(&self, adc_channel: u8) -> EcResult<i32> {
12461281
let res = EcRequestAdcRead { adc_channel }.send_command(self)?;
12471282
Ok(res.adc_value)

framework_lib/src/commandline/clap_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ struct ClapCli {
165165
#[clap(num_args = ..=2)]
166166
charge_rate_limit: Vec<f32>,
167167

168-
/// Get GPIO value by name
168+
/// Get GPIO value by name or all, if no name provided
169169
#[arg(long)]
170-
get_gpio: Option<String>,
170+
get_gpio: Option<Option<String>>,
171171

172172
/// Get or set fingerprint LED brightness level
173173
#[arg(long)]

framework_lib/src/commandline/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub struct Cli {
177177
pub charge_limit: Option<Option<u8>>,
178178
pub charge_current_limit: Option<(u32, Option<u32>)>,
179179
pub charge_rate_limit: Option<(f32, Option<f32>)>,
180-
pub get_gpio: Option<String>,
180+
pub get_gpio: Option<Option<String>>,
181181
pub fp_led_level: Option<Option<FpBrightnessArg>>,
182182
pub fp_brightness: Option<Option<u8>>,
183183
pub kblight: Option<Option<u8>>,
@@ -791,11 +791,15 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
791791
} else if let Some((limit, soc)) = args.charge_rate_limit {
792792
print_err(ec.set_charge_rate_limit(limit, soc));
793793
} else if let Some(gpio_name) = &args.get_gpio {
794-
print!("Getting GPIO value {}: ", gpio_name);
795-
if let Ok(value) = ec.get_gpio(gpio_name) {
796-
println!("{:?}", value);
794+
if let Some(gpio_name) = gpio_name {
795+
print!("GPIO {}: ", gpio_name);
796+
if let Ok(value) = ec.get_gpio(gpio_name) {
797+
println!("{:?}", value);
798+
} else {
799+
println!("Not found");
800+
}
797801
} else {
798-
println!("Not found");
802+
print_err(ec.get_all_gpios());
799803
}
800804
} else if let Some(maybe_led_level) = &args.fp_led_level {
801805
print_err(handle_fp_led_level(&ec, *maybe_led_level));
@@ -1120,7 +1124,7 @@ Options:
11201124
--expansion-bay Show status of the expansion bay (Framework 16 only)
11211125
--charge-limit [<VAL>] Get or set battery charge limit (Percentage number as arg, e.g. '100')
11221126
--charge-current-limit [<VAL>] Get or set battery current charge limit (Percentage number as arg, e.g. '100')
1123-
--get-gpio <GET_GPIO> Get GPIO value by name
1127+
--get-gpio <GET_GPIO> Get GPIO value by name or all, if no name provided
11241128
--fp-led-level [<VAL>] Get or set fingerprint LED brightness level [possible values: high, medium, low]
11251129
--fp-brightness [<VAL>]Get or set fingerprint LED brightness percentage
11261130
--kblight [<KBLIGHT>] Set keyboard backlight percentage or get, if no value provided

framework_lib/src/commandline/uefi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ pub fn parse(args: &[String]) -> Cli {
329329
found_an_option = true;
330330
} else if arg == "--get-gpio" {
331331
cli.get_gpio = if args.len() > i + 1 {
332-
Some(args[i + 1].clone())
332+
Some(Some(args[i + 1].clone()))
333333
} else {
334-
None
334+
Some(None)
335335
};
336336
found_an_option = true;
337337
} else if arg == "--kblight" {

0 commit comments

Comments
 (0)