Skip to content

Commit

Permalink
Merge branch 'master' into eliza/diagnose-good-tcbs
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw authored Apr 24, 2024
2 parents f6b13cd + fdf2939 commit d3dde7a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
49 changes: 44 additions & 5 deletions cmd/lsusb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
//! `humility lsusb` will show you Humility's view of the USB devices available
//! on the system, to help you choose probes and/or diagnose permissions issues.

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use clap::{CommandFactory, Parser};
use humility_cli::{ExecutionContext, Subcommand};
use humility_cmd::{Archive, Command, CommandKind};
use std::collections::HashMap;
use std::time::Duration;

#[derive(Parser, Debug)]
Expand All @@ -22,6 +23,17 @@ struct Args {
fn lsusb(context: &mut ExecutionContext) -> Result<()> {
let Subcommand::Other(subargs) = context.cli.cmd.as_ref().unwrap();
let _subargs = Args::try_parse_from(subargs)?;
let mut targets = if let Some(ref env) = context.cli.environment {
humility_cli::env::Environment::read(env)
.with_context(|| {
format!("failed to read environment from '{env}'")
})?
.into_iter()
.map(|(name, target)| (target.probe, name))
.collect::<HashMap<_, _>>()
} else {
HashMap::new()
};

let devices = rusb::devices()?;
let mut successes = vec![];
Expand Down Expand Up @@ -53,8 +65,12 @@ fn lsusb(context: &mut ExecutionContext) -> Result<()> {
"format: VID:PID:SERIAL, then manufacturer name, \
then product name"
);
for summary in successes {
humility::msg!("{}", summary);
for (ident, desc) in successes {
if let Some(target) = targets.remove(&ident) {
humility::msg!("{ident}\t{desc} (target: {target})");
} else {
humility::msg!("{ident}\t{desc}");
}
}
}

Expand All @@ -67,10 +83,33 @@ fn lsusb(context: &mut ExecutionContext) -> Result<()> {
}
}

if !targets.is_empty() {
let env =
context.cli.environment.as_ref().expect(
"if `targets` is non-empty, `environment` must be Some",
);
humility::warn!(
"--- could not find {} probes declared in HUMILITY_ENVIRONMENT ---",
targets.len()
);
let target_len = targets
.values()
.map(String::len)
.max()
.expect("must be `Some`, as `targets` is not empty");
humility::warn!("HUMILITY_ENVIRONMENT={env}");
humility::warn!("{:<target_len$} PROBE", "TARGET");
for (probe, target) in targets {
humility::warn!("{target:<target_len$} {probe}");
}
}

Ok(())
}

fn list1(dev: &rusb::Device<impl rusb::UsbContext>) -> Result<String> {
fn list1(
dev: &rusb::Device<impl rusb::UsbContext>,
) -> Result<(String, String)> {
const TIMEOUT: Duration = Duration::from_secs(1);

let desc = dev.device_descriptor()?;
Expand Down Expand Up @@ -99,7 +138,7 @@ fn list1(dev: &rusb::Device<impl rusb::UsbContext>) -> Result<String> {
.read_serial_number_string(lang, &desc, TIMEOUT)
.unwrap_or_else(|_| "(serial unknown)".to_string());

Ok(format!("{vid:04x}:{pid:04x}:{serial}\t{man}\t{prod}"))
Ok((format!("{vid:04x}:{pid:04x}:{serial}"), format!("{man}\t{prod}")))
}

pub fn init() -> Command {
Expand Down
2 changes: 1 addition & 1 deletion humility-cli/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Environment {
}
}

fn read(filename: &str) -> Result<IndexMap<String, Environment>> {
pub fn read(filename: &str) -> Result<IndexMap<String, Environment>> {
let path = PathBuf::from(filename);
let input = fs::read_to_string(path)?;
Ok(serde_json::from_str(&input)?)
Expand Down
2 changes: 1 addition & 1 deletion humility-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

mod env;
pub mod env;

use anyhow::Result;
use clap::{AppSettings, ArgGroup, ArgMatches, Parser};
Expand Down

0 comments on commit d3dde7a

Please sign in to comment.