Merge pull request #2 from r-e-l-z/multiple_device_support

Multiple device support
This commit is contained in:
cocool97
2022-05-16 18:10:08 +02:00
committed by GitHub
3 changed files with 24 additions and 14 deletions

View File

@@ -12,6 +12,9 @@ struct Args {
/// Sets the listening port of ADB server
#[clap(short = 'p', long = "port", default_value = "5037")]
pub port: u16,
/// Serial id of a specific device. Every request will be sent to this device.
#[clap(short = 's', long = "serial")]
pub serial: Option<String>,
#[clap(subcommand)]
pub command: Command,
}
@@ -30,7 +33,7 @@ enum Command {
/// Tracks new devices showing up
TrackDevices,
/// Run 'command' in a shell on the device, and return its output and error streams.
Shell { command: Option<String> },
Shell { command: Vec<String> },
}
fn main() -> Result<()> {
@@ -69,10 +72,10 @@ fn main() -> Result<()> {
connexion.track_devices(callback)?;
}
Command::Shell { command } => {
if let Some(command) = command {
connexion.shell_command(command)?;
if command.is_empty() {
connexion.shell(opt.serial)?;
} else {
connexion.shell()?;
connexion.shell_command(opt.serial, command)?;
}
}
}

View File

@@ -192,15 +192,15 @@ impl AdbTcpConnexion {
}
/// Runs 'command' in a shell on the device, and return its output and error streams.
pub fn shell_command<S: ToString>(&self, command: S) -> Result<()> {
pub fn shell_command(&self, serial: Option<String>, command: Vec<String>) -> Result<()> {
let mut tcp_stream = TcpStream::connect(self.socket_addr)?;
Self::send_adb_request(&mut tcp_stream, AdbCommand::TransportAny)?;
Self::send_adb_request(
&mut tcp_stream,
AdbCommand::ShellCommand(command.to_string()),
)?;
match serial {
None => Self::send_adb_request(&mut tcp_stream, AdbCommand::TransportAny)?,
Some(serial) => {
Self::send_adb_request(&mut tcp_stream, AdbCommand::TransportSerial(serial))?
}
}
Self::send_adb_request(&mut tcp_stream, AdbCommand::ShellCommand(command.join(" ")))?;
let buffer_size = 512;
loop {
@@ -222,14 +222,19 @@ impl AdbTcpConnexion {
}
/// Starts an interactive shell session on the device. Redirects stdin/stdout/stderr as appropriate.
pub fn shell(&self) -> Result<()> {
pub fn shell(&self, serial: Option<String>) -> Result<()> {
let mut adb_termios = ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
let mut tcp_stream = TcpStream::connect(self.socket_addr)?;
tcp_stream.set_nodelay(true)?;
Self::send_adb_request(&mut tcp_stream, AdbCommand::TransportAny)?;
match serial {
None => Self::send_adb_request(&mut tcp_stream, AdbCommand::TransportAny)?,
Some(serial) => {
Self::send_adb_request(&mut tcp_stream, AdbCommand::TransportSerial(serial))?
}
}
Self::send_adb_request(&mut tcp_stream, AdbCommand::Shell)?;
let read_stream = Arc::new(tcp_stream);

View File

@@ -10,6 +10,7 @@ pub enum AdbCommand {
// TransportUSB,
// TransportLocal,
TransportAny,
TransportSerial(String),
// Serial((String, String)),
// USB(String),
// Local(String),
@@ -49,6 +50,7 @@ impl ToString for AdbCommand {
AdbCommand::DevicesLong => "host:devices-l".into(),
AdbCommand::TrackDevices => "host:track-devices".into(),
AdbCommand::TransportAny => "host:transport-any".into(),
AdbCommand::TransportSerial(serial) => format!("host:transport:{}", serial),
AdbCommand::ShellCommand(command) => format!("shell:{}", command),
AdbCommand::Shell => "shell:".into(),
}