Skip to content

Commit

Permalink
feat: rework USB device loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cocool97 committed Oct 24, 2024
1 parent 34d8688 commit 8d608f6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
12 changes: 1 addition & 11 deletions adb_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ use adb_client::{ADBDeviceExt, ADBEmulatorDevice, ADBServer, ADBUSBDevice, Devic
use anyhow::{anyhow, Result};
use clap::Parser;
use commands::{EmuCommand, HostCommand, LocalCommand, UsbCommands};
use env_logger::Builder;
use log::LevelFilter;
use models::{Command, Opts};
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn main() -> Result<()> {
let opt = Opts::parse();

let max_level = if opt.verbose {
LevelFilter::Trace
} else {
LevelFilter::Info
};
let mut builder = Builder::default();
builder.filter_level(max_level);
builder.init();
env_logger::init();

match opt.command {
Command::Local(local) => {
Expand Down
2 changes: 0 additions & 2 deletions adb_cli/src/models/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use crate::commands::{EmuCommand, HostCommand, LocalCommand, UsbCommand};
#[derive(Parser, Debug)]
#[clap(about, version, author)]
pub struct Opts {
#[clap(short = 'v', long = "verbose")]
pub verbose: bool,
#[clap(short = 'a', long = "address", default_value = "127.0.0.1:5037")]
pub address: SocketAddrV4,
/// Serial id of a specific device. Every request will be sent to this device.
Expand Down
2 changes: 1 addition & 1 deletion adb_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ image = { version = "0.25.4" }
lazy_static = { version = "1.5.0" }
log = { version = "0.4.22" }
num-bigint = { version = "0.6", package = "num-bigint-dig" }
rand = "0.7.0"
rand = { version = "0.7.0" }
regex = { version = "1.11.0", features = ["perf", "std", "unicode"] }
rsa = { version = "0.3.0" }
rusb = { version = "0.9.4", features = ["vendored"] }
Expand Down
22 changes: 15 additions & 7 deletions adb_client/src/transports/usb_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,22 @@ impl USBTransport {

impl ADBTransport for USBTransport {
fn connect(&mut self) -> crate::Result<()> {
// Remove in production
let handle = rusb::open_device_with_vid_pid(self.vendor_id, self.product_id).ok_or(
RustADBError::USBDeviceNotFound(self.vendor_id, self.product_id),
)?;

self.handle = Some(Arc::new(handle));
for d in rusb::devices()?.iter() {
if let Ok(descriptor) = d.device_descriptor() {
if descriptor.vendor_id() == self.vendor_id
&& descriptor.product_id() == self.product_id
{
self.handle = Some(Arc::new(d.open()?));

return Ok(());
}
}
}

Ok(())
Err(RustADBError::DeviceNotFound(format!(
"Cannot find device with vendor id {} and product id {}",
self.vendor_id, self.product_id
)))
}

fn disconnect(&mut self) -> crate::Result<()> {
Expand Down

0 comments on commit 8d608f6

Please sign in to comment.