1 Commits

Author SHA1 Message Date
LIAUD Corentin
4126c856d9 wip: feature-based 2025-08-17 10:46:40 +02:00
12 changed files with 42 additions and 121 deletions

View File

@@ -16,7 +16,6 @@ anyhow = { version = "1.0.94" }
clap = { version = "4.5.23", features = ["derive"] }
env_logger = { version = "0.11.5" }
log = { version = "0.4.26" }
tabwriter = { version = "1.4.1" }
[target.'cfg(unix)'.dependencies]
termios = { version = "0.3.3" }

View File

@@ -8,22 +8,20 @@ mod models;
mod utils;
use adb_client::{
ADBDeviceExt, ADBDeviceInfo, ADBServer, ADBServerDevice, ADBTcpDevice, ADBUSBDevice,
MDNSDiscoveryService, find_all_connected_adb_devices,
ADBDeviceExt, ADBServer, ADBServerDevice, ADBTcpDevice, ADBUSBDevice, MDNSDiscoveryService,
};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use adb_termios::ADBTermios;
use anyhow::{Result, bail};
use anyhow::Result;
use clap::Parser;
use handlers::{handle_emulator_commands, handle_host_commands, handle_local_commands};
use models::{DeviceCommands, LocalCommand, MainCommand, Opts};
use std::collections::HashMap;
use std::fs::File;
use std::io::{Write, stdout};
use std::io::Write;
use std::path::Path;
use tabwriter::TabWriter;
use utils::setup_logger;
fn main() -> Result<()> {
@@ -63,34 +61,6 @@ fn main() -> Result<()> {
}
}
MainCommand::Usb(usb_command) => {
if usb_command.list_devices {
let devices = find_all_connected_adb_devices()?;
let mut writer = TabWriter::new(stdout()).alignment(tabwriter::Alignment::Center);
writeln!(writer, "Index\tVendor ID\tProduct ID\tDevice Description")?;
writeln!(writer, "-----\t---------\t----------\t----------------")?;
for (
index,
ADBDeviceInfo {
vendor_id,
product_id,
device_description,
},
) in devices.iter().enumerate()
{
writeln!(
writer,
"#{}\t{:04x}\t{:04x}\t{}",
index, vendor_id, product_id, device_description
)?;
}
writer.flush()?;
return Ok(());
}
let device = match (usb_command.vendor_id, usb_command.product_id) {
(Some(vid), Some(pid)) => match usb_command.path_to_private_key {
Some(pk) => ADBUSBDevice::new_with_custom_private_key(vid, pid, pk)?,
@@ -106,11 +76,7 @@ fn main() -> Result<()> {
);
}
};
match usb_command.commands {
Some(commands) => (device.boxed(), commands),
None => bail!("no command provided"),
}
(device.boxed(), usb_command.commands)
}
MainCommand::Tcp(tcp_command) => {
let device = ADBTcpDevice::new(tcp_command.address)?;

View File

@@ -20,9 +20,6 @@ pub struct UsbCommand {
/// Path to a custom private key to use for authentication
#[clap(short = 'k', long = "private-key")]
pub path_to_private_key: Option<PathBuf>,
/// List all connected Android devices
#[clap(short = 'l', long = "list")]
pub list_devices: bool,
#[clap(subcommand)]
pub commands: Option<DeviceCommands>,
pub commands: DeviceCommands,
}

View File

@@ -10,6 +10,10 @@ repository.workspace = true
rust-version.workspace = true
version.workspace = true
[features]
default = ["mdns"]
mdns = ["dep:mdns-sd"]
[dependencies]
base64 = { version = "0.22.1" }
bincode = { version = "1.3.3" }
@@ -18,9 +22,6 @@ chrono = { version = "0.4.40", default-features = false, features = ["std"] }
homedir = { version = "= 0.3.4" }
image = { version = "0.25.5", default-features = false }
log = { version = "0.4.26" }
mdns-sd = { version = "0.13.9", default-features = false, features = [
"logging",
] }
num-bigint = { version = "0.8.4", package = "num-bigint-dig" }
num-traits = { version = "0.2.19" }
quick-protobuf = { version = "0.8.1" }
@@ -39,6 +40,11 @@ serde_repr = { version = "0.1.19" }
sha1 = { version = "0.10.6", features = ["oid"] }
thiserror = { version = "2.0.7" }
# MDNS
mdns-sd = { version = "0.13.9", default-features = false, optional = true, features = [
"logging",
] }
[dev-dependencies]
anyhow = { version = "1.0.93" }
criterion = { version = "0.6.0" } # Used for benchmarks

View File

@@ -35,80 +35,28 @@ pub fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Optio
}
}
/// Represents an Android device connected via USB
#[derive(Clone, Debug)]
pub struct ADBDeviceInfo {
/// Vendor ID of the device
pub vendor_id: u16,
/// Product ID of the device
pub product_id: u16,
/// Textual description of the device
pub device_description: String,
}
/// Find and return a list of all connected Android devices with known interface class and subclass values
pub fn find_all_connected_adb_devices() -> Result<Vec<ADBDeviceInfo>> {
/// Search for adb devices with known interface class and subclass values
pub fn search_adb_devices() -> Result<Option<(u16, u16)>> {
let mut found_devices = vec![];
for device in rusb::devices()?.iter() {
let Ok(des) = device.device_descriptor() else {
continue;
};
if is_adb_device(&device, &des) {
let device_handle = match device.open() {
Ok(h) => h,
Err(_) => {
found_devices.push(ADBDeviceInfo {
vendor_id: des.vendor_id(),
product_id: des.product_id(),
device_description: "Unknown device".to_string(),
});
continue;
}
};
let manufacturer = device_handle
.read_manufacturer_string_ascii(&des)
.unwrap_or_else(|_| "Unknown".to_string());
let product = device_handle
.read_product_string_ascii(&des)
.unwrap_or_else(|_| "Unknown".to_string());
let device_description = format!("{} {}", manufacturer, product);
found_devices.push(ADBDeviceInfo {
vendor_id: des.vendor_id(),
product_id: des.product_id(),
device_description,
});
log::debug!(
"Autodetect device {:04x}:{:04x}",
des.vendor_id(),
des.product_id()
);
found_devices.push((des.vendor_id(), des.product_id()));
}
}
Ok(found_devices)
}
/// Find and return an USB-connected Android device with known interface class and subclass values.
///
/// Returns the first device found or None if no device is found.
/// If multiple devices are found, an error is returned.
pub fn get_single_connected_adb_device() -> Result<Option<ADBDeviceInfo>> {
let found_devices = find_all_connected_adb_devices()?;
match (found_devices.first(), found_devices.get(1)) {
(None, _) => Ok(None),
(Some(device_info), None) => {
log::debug!(
"Autodetect device {:04x}:{:04x} - {}",
device_info.vendor_id,
device_info.product_id,
device_info.device_description
);
Ok(Some(device_info.clone()))
}
(Some(device_1), Some(device_2)) => Err(RustADBError::DeviceNotFound(format!(
"Found two Android devices {:04x}:{:04x} and {:04x}:{:04x}",
device_1.vendor_id, device_1.product_id, device_2.vendor_id, device_2.product_id
(Some(identifiers), None) => Ok(Some(*identifiers)),
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
"Found two Android devices {vid1:04x}:{pid1:04x} and {vid2:04x}:{pid2:04x}",
))),
}
}
@@ -219,12 +167,10 @@ impl ADBUSBDevice {
/// autodetect connected ADB devices and establish a connection with the first device found using a custom private key path
pub fn autodetect_with_custom_private_key(private_key_path: PathBuf) -> Result<Self> {
match get_single_connected_adb_device()? {
Some(device_info) => ADBUSBDevice::new_with_custom_private_key(
device_info.vendor_id,
device_info.product_id,
private_key_path,
),
match search_adb_devices()? {
Some((vendor_id, product_id)) => {
ADBUSBDevice::new_with_custom_private_key(vendor_id, product_id, private_key_path)
}
_ => Err(RustADBError::DeviceNotFound(
"cannot find USB devices matching the signature of an ADB device".into(),
)),

View File

@@ -12,8 +12,7 @@ use adb_message_device::ADBMessageDevice;
pub use adb_tcp_device::ADBTcpDevice;
pub use adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader};
pub use adb_usb_device::{
ADBDeviceInfo, ADBUSBDevice, find_all_connected_adb_devices, get_default_adb_key_path,
get_single_connected_adb_device, is_adb_device,
ADBUSBDevice, get_default_adb_key_path, is_adb_device, search_adb_devices,
};
pub use message_writer::MessageWriter;
pub use models::{ADBRsaKey, MessageCommand, MessageSubcommand};

View File

@@ -112,6 +112,7 @@ pub enum RustADBError {
#[error("upgrade error: {0}")]
UpgradeError(String),
/// An error occurred while getting mdns devices
#[cfg(feature = "mdns")]
#[error(transparent)]
MDNSError(#[from] mdns_sd::Error),
/// An error occurred while sending data to channel

View File

@@ -9,6 +9,7 @@ mod constants;
mod device;
mod emulator_device;
mod error;
#[cfg(feature = "mdns")]
mod mdns;
mod models;
mod server;
@@ -17,12 +18,10 @@ mod transports;
mod utils;
pub use adb_device_ext::ADBDeviceExt;
pub use device::{
ADBDeviceInfo, ADBTcpDevice, ADBUSBDevice, find_all_connected_adb_devices,
get_single_connected_adb_device, is_adb_device,
};
pub use device::{ADBTcpDevice, ADBUSBDevice, is_adb_device, search_adb_devices};
pub use emulator_device::ADBEmulatorDevice;
pub use error::{Result, RustADBError};
#[cfg(feature = "mdns")]
pub use mdns::*;
pub use models::{AdbStatResponse, RebootType};
pub use server::*;

View File

@@ -18,7 +18,9 @@ pub(crate) enum AdbServerCommand {
Pair(SocketAddrV4, String),
TransportAny,
TransportSerial(String),
#[cfg(feature = "mdns")]
MDNSCheck,
#[cfg(feature = "mdns")]
MDNSServices,
ServerStatus,
ReconnectOffline,
@@ -77,7 +79,9 @@ impl Display for AdbServerCommand {
write!(f, "reverse:forward:{remote};{local}")
}
AdbServerCommand::ReverseRemoveAll => write!(f, "reverse:killforward-all"),
#[cfg(feature = "mdns")]
AdbServerCommand::MDNSCheck => write!(f, "host:mdns:check"),
#[cfg(feature = "mdns")]
AdbServerCommand::MDNSServices => write!(f, "host:mdns:services"),
AdbServerCommand::ServerStatus => write!(f, "host:server-status"),
AdbServerCommand::Reconnect => write!(f, "reconnect"),

View File

@@ -6,6 +6,7 @@ use crate::{
const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN";
#[cfg(feature = "mdns")]
impl ADBServer {
/// Check if mdns discovery is available
pub fn mdns_check(&mut self) -> Result<bool> {

View File

@@ -2,6 +2,7 @@ mod connect;
mod devices;
mod disconnect;
mod kill;
#[cfg(feature = "mdns")]
mod mdns;
mod pair;
mod reconnect;

View File

@@ -2,6 +2,7 @@ mod adb_version;
mod device_long;
mod device_short;
mod device_state;
#[cfg(feature = "mdns")]
mod mdns_services;
mod server_status;
mod wait_for_device;
@@ -10,6 +11,7 @@ pub use adb_version::AdbVersion;
pub use device_long::DeviceLong;
pub use device_short::DeviceShort;
pub use device_state::DeviceState;
#[cfg(feature = "mdns")]
pub use mdns_services::MDNSServices;
pub use server_status::{MDNSBackend, ServerStatus};
pub use wait_for_device::{WaitForDeviceState, WaitForDeviceTransport};