feat: remove serial param from functions
This commit is contained in:
10
README.md
10
README.md
@@ -25,16 +25,18 @@ use adb_client::ADBServer;
|
||||
|
||||
let mut server = ADBServer::default();
|
||||
let mut device = server.get_device(None).expect("cannot get device");
|
||||
device.shell_command(None, ["df", "-h"]);
|
||||
device.shell_command(["df", "-h"]);
|
||||
```
|
||||
|
||||
### Get available ADB devices
|
||||
|
||||
```rust
|
||||
use adb_client::ADBServer;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::net::{SocketAddrV4, Ipv4Addr};
|
||||
|
||||
let mut server = ADBServer::new(Ipv4Addr::from([127,0,0,1]), 5037);
|
||||
let server_ip = Ipv4Addr::new(127, 0, 0, 1);
|
||||
let server_port = 5037;
|
||||
let mut server = ADBServer::new(SocketAddrV4::new(server_ip, server_port));
|
||||
server.devices();
|
||||
```
|
||||
|
||||
@@ -49,7 +51,7 @@ use std::path::Path;
|
||||
let mut server = ADBServer::default();
|
||||
let mut device = server.get_device(None).expect("cannot get device");
|
||||
let mut input = File::open(Path::new("/tmp")).unwrap();
|
||||
device.send::<&str,&str>(None, &mut input, "/data/local/tmp");
|
||||
device.send(&mut input, "/data/local/tmp");
|
||||
```
|
||||
|
||||
## Rust binary
|
||||
|
||||
@@ -100,38 +100,38 @@ fn main() -> Result<()> {
|
||||
match local {
|
||||
LocalCommand::Pull { path, filename } => {
|
||||
let mut output = File::create(Path::new(&filename))?;
|
||||
device.recv(opt.serial.as_ref(), &path, &mut output)?;
|
||||
device.recv(&path, &mut output)?;
|
||||
println!("Downloaded {path} as {filename}");
|
||||
}
|
||||
LocalCommand::Push { filename, path } => {
|
||||
let mut input = File::open(Path::new(&filename))?;
|
||||
device.send(opt.serial.as_ref(), &mut input, &path)?;
|
||||
device.send(&mut input, &path)?;
|
||||
println!("Uploaded {filename} to {path}");
|
||||
}
|
||||
LocalCommand::List { path } => {
|
||||
device.list(opt.serial.as_ref(), path)?;
|
||||
device.list(path)?;
|
||||
}
|
||||
LocalCommand::Stat { path } => {
|
||||
let stat_response = device.stat(opt.serial, path)?;
|
||||
let stat_response = device.stat(path)?;
|
||||
println!("{}", stat_response);
|
||||
}
|
||||
LocalCommand::Shell { command } => {
|
||||
if command.is_empty() {
|
||||
device.shell(opt.serial.as_ref())?;
|
||||
device.shell()?;
|
||||
} else {
|
||||
let stdout = device.shell_command(opt.serial.as_ref(), command)?;
|
||||
let stdout = device.shell_command(command)?;
|
||||
io::stdout().write_all(&stdout)?;
|
||||
}
|
||||
}
|
||||
LocalCommand::HostFeatures => {
|
||||
println!("Available host features");
|
||||
for feature in device.host_features(opt.serial.as_ref())? {
|
||||
for feature in device.host_features()? {
|
||||
println!("- {}", feature);
|
||||
}
|
||||
}
|
||||
LocalCommand::Reboot { sub_command } => {
|
||||
println!("Reboots device");
|
||||
device.reboot(opt.serial.as_ref(), sub_command.into())?
|
||||
device.reboot(sub_command.into())?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,10 @@ use crate::{
|
||||
|
||||
impl ADBServerDevice {
|
||||
/// Lists available ADB server features.
|
||||
pub fn host_features<S: ToString>(&mut self, serial: Option<&S>) -> Result<Vec<HostFeatures>> {
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
pub fn host_features(&mut self) -> Result<Vec<HostFeatures>> {
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
|
||||
let features = self
|
||||
.get_transport()?
|
||||
|
||||
@@ -10,13 +10,10 @@ use std::{
|
||||
|
||||
impl ADBServerDevice {
|
||||
/// Lists files in [path] on the device.
|
||||
pub fn list<S: ToString, A: AsRef<str>>(&mut self, serial: Option<&S>, path: A) -> Result<()> {
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
pub fn list<A: AsRef<str>>(&mut self, path: A) -> Result<()> {
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
|
||||
// Set device in SYNC mode
|
||||
self.get_transport()?.send_adb_request(AdbCommand::Sync)?;
|
||||
|
||||
@@ -5,17 +5,10 @@ use crate::{
|
||||
|
||||
impl ADBServerDevice {
|
||||
/// Reboots the device
|
||||
pub fn reboot<S: ToString>(
|
||||
&mut self,
|
||||
serial: Option<&S>,
|
||||
reboot_type: RebootType,
|
||||
) -> Result<()> {
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
pub fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
|
||||
self.get_transport()?
|
||||
.proxy_connection(AdbCommand::Reboot(reboot_type), false)
|
||||
|
||||
@@ -7,18 +7,10 @@ use std::io::{Read, Write};
|
||||
|
||||
impl ADBServerDevice {
|
||||
/// Receives [path] to [stream] from the device.
|
||||
pub fn recv<S: ToString, A: AsRef<str>>(
|
||||
&mut self,
|
||||
serial: Option<&S>,
|
||||
path: A,
|
||||
stream: &mut dyn Write,
|
||||
) -> Result<()> {
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
pub fn recv<A: AsRef<str>>(&mut self, path: A, stream: &mut dyn Write) -> Result<()> {
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
|
||||
// Set device in SYNC mode
|
||||
self.get_transport()?.send_adb_request(AdbCommand::Sync)?;
|
||||
|
||||
@@ -12,18 +12,10 @@ use std::{
|
||||
|
||||
impl ADBServerDevice {
|
||||
/// Sends [stream] to [path] on the device.
|
||||
pub fn send<S: ToString, A: AsRef<str>>(
|
||||
&mut self,
|
||||
serial: Option<&S>,
|
||||
stream: &mut dyn Read,
|
||||
path: A,
|
||||
) -> Result<()> {
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
pub fn send<A: AsRef<str>>(&mut self, stream: &mut dyn Read, path: A) -> Result<()> {
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
|
||||
// Set device in SYNC mode
|
||||
self.get_transport()?.send_adb_request(AdbCommand::Sync)?;
|
||||
|
||||
@@ -29,22 +29,18 @@ impl ADBServerDevice {
|
||||
/// Runs 'command' in a shell on the device, and return its output and error streams.
|
||||
pub fn shell_command<S: ToString>(
|
||||
&mut self,
|
||||
serial: Option<&S>,
|
||||
command: impl IntoIterator<Item = S>,
|
||||
) -> Result<Vec<u8>> {
|
||||
let supported_features = self.host_features(serial)?;
|
||||
let supported_features = self.host_features()?;
|
||||
if !supported_features.contains(&HostFeatures::ShellV2)
|
||||
&& !supported_features.contains(&HostFeatures::Cmd)
|
||||
{
|
||||
return Err(RustADBError::ADBShellNotSupported);
|
||||
}
|
||||
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
self.get_transport()?
|
||||
.send_adb_request(AdbCommand::ShellCommand(
|
||||
command
|
||||
@@ -74,7 +70,7 @@ impl ADBServerDevice {
|
||||
}
|
||||
|
||||
/// Starts an interactive shell session on the device. Redirects stdin/stdout/stderr as appropriate.
|
||||
pub fn shell<S: ToString>(&mut self, serial: Option<&S>) -> Result<()> {
|
||||
pub fn shell(&mut self) -> Result<()> {
|
||||
let mut adb_termios = ADBTermios::new(std::io::stdin())?;
|
||||
adb_termios.set_adb_termios()?;
|
||||
|
||||
@@ -82,19 +78,16 @@ impl ADBServerDevice {
|
||||
|
||||
// TODO: FORWARD CTRL+C !!
|
||||
|
||||
let supported_features = self.host_features(serial)?;
|
||||
let supported_features = self.host_features()?;
|
||||
if !supported_features.contains(&HostFeatures::ShellV2)
|
||||
&& !supported_features.contains(&HostFeatures::Cmd)
|
||||
{
|
||||
return Err(RustADBError::ADBShellNotSupported);
|
||||
}
|
||||
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
self.get_transport()?.send_adb_request(AdbCommand::Shell)?;
|
||||
|
||||
// let read_stream = Arc::new(self.tcp_stream);
|
||||
|
||||
@@ -81,17 +81,10 @@ impl ADBServerDevice {
|
||||
}
|
||||
|
||||
/// Stat file given as [path] on the device.
|
||||
pub fn stat<S: ToString, A: AsRef<str>>(
|
||||
&mut self,
|
||||
serial: Option<S>,
|
||||
path: A,
|
||||
) -> Result<AdbStatResponse> {
|
||||
match serial {
|
||||
None => self.connect()?.send_adb_request(AdbCommand::TransportAny)?,
|
||||
Some(serial) => self
|
||||
.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial.to_string()))?,
|
||||
}
|
||||
pub fn stat<A: AsRef<str>>(&mut self, path: A) -> Result<AdbStatResponse> {
|
||||
let serial = self.identifier.clone();
|
||||
self.connect()?
|
||||
.send_adb_request(AdbCommand::TransportSerial(serial))?;
|
||||
|
||||
// Set device in SYNC mode
|
||||
self.get_transport()?.send_adb_request(AdbCommand::Sync)?;
|
||||
|
||||
@@ -24,8 +24,8 @@ mod tests {
|
||||
fn test_shell() {
|
||||
let mut device = new_device();
|
||||
|
||||
device.shell_command(None, vec!["ls"]).unwrap();
|
||||
device.shell_command(None, vec!["pwd"]).unwrap();
|
||||
device.shell_command(vec!["ls"]).unwrap();
|
||||
device.shell_command(vec!["pwd"]).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -61,20 +61,20 @@ mod tests {
|
||||
const TEST_FILENAME: &'static str = "/data/local/tmp/test_file";
|
||||
// Send it
|
||||
device
|
||||
.send::<&str, &str>(None, &mut c, TEST_FILENAME)
|
||||
.send(&mut c, TEST_FILENAME)
|
||||
.expect("cannot send file");
|
||||
|
||||
// Pull it to memory
|
||||
let mut res = vec![];
|
||||
device
|
||||
.recv::<&str, &str>(None, TEST_FILENAME, &mut res)
|
||||
.recv(TEST_FILENAME, &mut res)
|
||||
.expect("cannot recv file");
|
||||
|
||||
// diff
|
||||
assert_eq!(c.get_ref(), &res);
|
||||
|
||||
device
|
||||
.shell_command::<&str>(None, [format!("rm {TEST_FILENAME}").as_str()])
|
||||
.shell_command::<&str>([format!("rm {TEST_FILENAME}").as_str()])
|
||||
.expect("cannot remove test file");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user