Skip to content

Commit

Permalink
try find & open interface on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Mar 27, 2024
1 parent 4d6186f commit ba97d9f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 51 deletions.
114 changes: 88 additions & 26 deletions support/device/src/usb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use object_pool::Reusable;
use crate::device::command::Command;
use crate::device::Device;
use crate::error::Error;
use crate::interface::blocking::Out;

use self::mode::DeviceMode;
use self::mode::Mode;
Expand Down Expand Up @@ -231,43 +232,53 @@ pub fn inspect_device(dev: &DeviceInfo) {
dev.product_string().unwrap_or("")
);

println!(" {dev:#?}");

{
let bulk = dev.data_interface();
println!("bulk interface: {:#?}", bulk);
}

let bulk = dev.data_interface();
let mut has_data_interface = bulk.is_some();
let mut bulk_interface_number = None;
println!("bulk interface: {:#?}", bulk);
println!("---");


let describe_class = |class: u8, subclass: u8, protocol: u8, indent: &'static str| {
use usb_ids::FromId;

let class = usb_ids::Class::from_id(class);

let subs = class.unwrap()
.sub_classes()
.filter(|sub| sub.id() == subclass)
.collect::<Vec<_>>();


for sub in subs {
println!("{indent}sub: ({:#02x}) {}", sub.id(), sub.name());
let protocols = sub.protocols().filter(|p| p.id() == protocol).collect::<Vec<_>>();
if protocols.is_empty() {
println!("{indent}{indent}unknown protocol: {protocol:#02x}");
}
for p in protocols {
println!("{indent}{indent}protocol: ({:#02x}) {}", p.id(), p.name());
}
}
};

{
use usb_ids::FromId;

println!("interfaces:");
for i in dev.interfaces() {
let interfaces = dev.interfaces().collect::<Vec<_>>();
println!("interfaces: ({})", interfaces.len());
for i in interfaces {
let class = usb_ids::Class::from_id(i.class());

let subclass_id = i.subclass();
let subs = class.unwrap()
.sub_classes()
.filter(|sub| sub.id() == subclass_id)
.collect::<Vec<_>>();
let n = i.interface_number();
let name = i.interface_string().unwrap_or("_");
let protocol_id = i.protocol();

println!("{n}: {name}");
println!("class: ({:#02x}) {:?}", i.class(), class.map(|c| c.name()));
for sub in subs {
println!(" sub: ({:#02x}) {}", sub.id(), sub.name());
let protocols = sub.protocols()
.filter(|p| p.id() == protocol_id)
.collect::<Vec<_>>();
if protocols.is_empty() {
println!(" unknown protocol: {protocol_id:#02x}");
}
for p in protocols {
println!(" protocol: ({:#02x}) {}", p.id(), p.name());
}
}
describe_class(i.class(), i.subclass(), i.protocol(), " ");
}
}
println!("---");
Expand All @@ -281,13 +292,64 @@ pub fn inspect_device(dev: &DeviceInfo) {
},
};

match dev.active_configuration() {
let active_configuration = dev.active_configuration();
match &active_configuration {
Ok(config) => println!("Active configuration is {}", config.configuration_value()),
Err(e) => println!("Unknown active configuration: {e}"),
}


let mut bulk_found = false;
for config in dev.configurations() {
println!("{config:#?}");
let active = if let Ok(ref cfg) = active_configuration {
if config.configuration_value() == cfg.configuration_value() {
"[ACTIVE] "
} else {
""
}
} else {
""
};

println!(" {active}{config:#?}");

if !has_data_interface {
println!(" |");
println!(" |");
println!(" \\---");
for i in config.interfaces() {
let bulk = i.alt_settings().find(|i| i.class() == 0xA | 2);
if let Some(bulk) = bulk {
println!("I JUST FOUND BULK INTERFACE!");
println!("{bulk:#?}");
has_data_interface = true;
bulk_interface_number = Some(bulk.interface_number());
} else {
for i in i.alt_settings() {
describe_class(i.class(), i.subclass(), i.protocol(), " ");
println!(" endpoints: ({})", i.num_endpoints());
for endpoint in i.endpoints() {
println!(" {endpoint:#?}");
}
}
}
}
}
}

println!("---");

if has_data_interface && bulk_interface_number.is_some() {
let i = bulk_interface_number.unwrap();
println!("trying to open interface: {i}");
let interf = Interface::new(dev.claim_interface(i).unwrap());
{
use crate::device::command::SystemPath;
use crate::device::command::Switch;

interf.send_cmd(Command::Echo { value: Switch::On }).unwrap();
interf.send_cmd(Command::RunSystem { path: SystemPath::Settings }).unwrap();
}
}

println!("----------------\n");
Expand Down
2 changes: 1 addition & 1 deletion support/tool2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ name = "pdtool"

[dependencies]
# RT, async:
tokio = { version = "1.36", features = ["full"] }
tokio = { version = "1.36", features = ["full", "rt-multi-thread"] }
futures = { version = "0.3" }
futures-lite.workspace = true

Expand Down
46 changes: 22 additions & 24 deletions support/tool2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,23 @@ async fn main() -> miette::Result<()> {
debug!("cmd: {:?}", cfg.cmd);

match cfg.cmd {
cli::Command::List { kind } => discover(cfg.format, kind).await,
cli::Command::List { kind } => list(cfg.format, kind).await,
cli::Command::Read(query) => read(query).await,
cli::Command::Mount { query, wait } => mount_and(query, wait, cfg.format).await,
cli::Command::Unmount { query, wait } => unmount_and(query, wait, cfg.format).await,
cli::Command::Install(cli::Install { pdx, query, force }) => {
install_and(query, pdx, force, cfg.format).await
},
cli::Command::Mount { query, wait } => mount(query, wait, cfg.format).await,
cli::Command::Unmount { query, wait } => unmount(query, wait, cfg.format).await,
cli::Command::Install(cli::Install { pdx, query, force }) => install(query, pdx, force, cfg.format).await,
cli::Command::Run(cli::Run { destination:
cli::Destination::Device(cli::DeviceDestination { install:
cli::Install { pdx,
query,
force, },
no_install,
no_read, }), }) => {
run_on_device(query, pdx, no_install, no_read, force, cfg.format).await
run_dev(query, pdx, no_install, no_read, force, cfg.format).await
},
cli::Command::Run(cli::Run { destination:
cli::Destination::Simulator(cli::SimDestination { pdx, sdk }), }) => {
run_with_sim(pdx, sdk, cfg.format).await
run_sim(pdx, sdk, cfg.format).await
},
cli::Command::Send(cli::Send { command, query, read }) => send(query, command, read, cfg.format).await,

Expand All @@ -127,13 +125,13 @@ async fn main() -> miette::Result<()> {


#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn run_on_device(query: DeviceQuery,
pdx: PathBuf,
no_install: bool,
no_read: bool,
force: bool,
format: cli::Format)
-> Result<(), error::Error> {
async fn run_dev(query: DeviceQuery,
pdx: PathBuf,
no_install: bool,
no_read: bool,
force: bool,
format: cli::Format)
-> Result<(), error::Error> {
let devs = run::run_on_device(query, pdx, no_install, no_read, force).await?
.into_iter()
.enumerate();
Expand Down Expand Up @@ -165,19 +163,19 @@ async fn run_on_device(query: DeviceQuery,


#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn run_with_sim(pdx: PathBuf, sdk: Option<PathBuf>, _format: cli::Format) -> Result<(), error::Error> {
async fn run_sim(pdx: PathBuf, sdk: Option<PathBuf>, _format: cli::Format) -> Result<(), error::Error> {
run::run_with_sim(pdx, sdk).await
.inspect(|_| trace!("sim execution is done"))
}


/// `mount_and_install` with report.
#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn install_and(query: DeviceQuery,
path: PathBuf,
force: bool,
format: cli::Format)
-> Result<(), error::Error> {
async fn install(query: DeviceQuery,
path: PathBuf,
force: bool,
format: cli::Format)
-> Result<(), error::Error> {
if matches!(format, cli::Format::Json) {
print!("[");
}
Expand Down Expand Up @@ -216,7 +214,7 @@ async fn install_and(query: DeviceQuery,

/// [[mount::mount_and]] with report.
#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn mount_and(query: DeviceQuery, wait: bool, format: cli::Format) -> Result<(), error::Error> {
async fn mount(query: DeviceQuery, wait: bool, format: cli::Format) -> Result<(), error::Error> {
if matches!(format, cli::Format::Json) {
print!("[");
}
Expand Down Expand Up @@ -253,7 +251,7 @@ async fn mount_and(query: DeviceQuery, wait: bool, format: cli::Format) -> Resul

/// [[mount::unmount_and]] with report.
#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn unmount_and(query: DeviceQuery, wait: bool, format: cli::Format) -> Result<(), error::Error> {
async fn unmount(query: DeviceQuery, wait: bool, format: cli::Format) -> Result<(), error::Error> {
let results: Vec<_> = mount::unmount_and(query, wait).await?.collect().await;
for (i, res) in results.into_iter().enumerate() {
match res {
Expand Down Expand Up @@ -350,7 +348,7 @@ async fn read(query: DeviceQuery) -> Result<(), error::Error> {


#[cfg_attr(feature = "tracing", tracing::instrument())]
async fn discover(format: cli::Format, kind: cli::DeviceKind) -> Result<(), error::Error> {
async fn list(format: cli::Format, kind: cli::DeviceKind) -> Result<(), error::Error> {
use mount::volume::volumes_for_map;

let devices = match kind {
Expand Down

0 comments on commit ba97d9f

Please sign in to comment.