Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
373a5265a0 | ||
|
|
060e43590d | ||
|
|
f51ba984ca | ||
|
|
66ebc8a030 | ||
|
|
d39e98695d | ||
|
|
34d5811420 | ||
|
|
86e28a6e25 | ||
|
|
9f113bdb93 | ||
|
|
8670d6db58 | ||
|
|
0732a0bbad | ||
|
|
b5673001ca | ||
|
|
81829c1523 | ||
|
|
b9d2b8374f | ||
|
|
5716784f5d | ||
|
|
b6ddc720d8 | ||
|
|
5438e53361 | ||
|
|
39a7f0a8cf | ||
|
|
4129d9d218 | ||
|
|
c23dca61d7 | ||
|
|
2bc338fdf5 | ||
|
|
728fb7da95 | ||
|
|
2e762400e5 | ||
|
|
c85c76228d | ||
|
|
e91a35b3ff | ||
|
|
8f979eff91 | ||
|
|
af51584736 | ||
|
|
864bde01f6 | ||
|
|
8361a0fe06 | ||
|
|
218ec3d679 | ||
|
|
4b817c0435 | ||
|
|
2dd30931f2 |
@@ -1,2 +0,0 @@
|
||||
[target.aarch64-apple-darwin]
|
||||
rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
|
||||
@@ -9,7 +9,8 @@ homepage = "https://github.com/cocool97/adb_client"
|
||||
keywords = ["adb", "android", "tcp", "usb"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/cocool97/adb_client"
|
||||
version = "2.1.11"
|
||||
version = "2.1.17"
|
||||
rust-version = "1.85.1"
|
||||
|
||||
# To build locally when working on a new release
|
||||
[patch.crates-io]
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
<a href="https://crates.io/crates/adb_client">
|
||||
<img alt="crates.io" src="https://img.shields.io/crates/v/adb_client.svg"/>
|
||||
</a>
|
||||
<a href="https://crates.io/crates/adb_client">
|
||||
<img alt="msrv" src="https://img.shields.io/crates/msrv/adb_client"/>
|
||||
</a>
|
||||
<a href="https://github.com/cocool97/adb_client/actions">
|
||||
<img alt="ci status" src="https://github.com/cocool97/adb_client/actions/workflows/rust-build.yml/badge.svg"/>
|
||||
</a>
|
||||
|
||||
@@ -7,6 +7,7 @@ license.workspace = true
|
||||
name = "adb_cli"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
[](./LICENSE-MIT)
|
||||

|
||||

|
||||
|
||||
Rust binary providing an improved version of `adb` CLI.
|
||||
|
||||
@@ -10,7 +11,7 @@ Rust binary providing an improved version of `adb` CLI.
|
||||
This crate provides a lightweight binary based on the `adb_client` crate. You can install it by running the following command :
|
||||
|
||||
```shell
|
||||
cargo install adb_cli
|
||||
cargo install adb_cli
|
||||
```
|
||||
|
||||
Usage is quite simple, and tends to look like `adb`:
|
||||
@@ -64,4 +65,4 @@ Options:
|
||||
-p, --product-id <PID> Hexadecimal product id of this USB device
|
||||
-k, --private-key <PATH_TO_PRIVATE_KEY> Path to a custom private key to use for authentication
|
||||
-h, --help Print help
|
||||
```
|
||||
```
|
||||
|
||||
@@ -8,7 +8,7 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
match server_command.command {
|
||||
HostCommand::Version => {
|
||||
let version = adb_server.version()?;
|
||||
log::info!("Android Debug Bridge version {}", version);
|
||||
log::info!("Android Debug Bridge version {version}");
|
||||
log::info!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
|
||||
}
|
||||
HostCommand::Kill => {
|
||||
@@ -18,18 +18,18 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
if long {
|
||||
log::info!("List of devices attached (extended)");
|
||||
for device in adb_server.devices_long()? {
|
||||
log::info!("{}", device);
|
||||
log::info!("{device}");
|
||||
}
|
||||
} else {
|
||||
log::info!("List of devices attached");
|
||||
for device in adb_server.devices()? {
|
||||
log::info!("{}", device);
|
||||
log::info!("{device}");
|
||||
}
|
||||
}
|
||||
}
|
||||
HostCommand::TrackDevices => {
|
||||
let callback = |device: DeviceShort| {
|
||||
log::info!("{}", device);
|
||||
log::info!("{device}");
|
||||
Ok(())
|
||||
};
|
||||
log::info!("Live list of devices attached");
|
||||
@@ -65,7 +65,7 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
MdnsCommand::Services => {
|
||||
log::info!("List of discovered mdns services");
|
||||
for service in adb_server.mdns_services()? {
|
||||
log::info!("{}", service);
|
||||
log::info!("{service}");
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -25,9 +25,15 @@ use std::path::Path;
|
||||
use utils::setup_logger;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// This depends on `clap`
|
||||
let opts = Opts::parse();
|
||||
|
||||
setup_logger(opts.debug);
|
||||
// SAFETY:
|
||||
// We are assuming the entire process is single-threaded
|
||||
// at this point.
|
||||
// This seems true for the current version of `clap`,
|
||||
// but there's no guarantee for future updates
|
||||
unsafe { setup_logger(opts.debug) };
|
||||
|
||||
// Directly handling methods / commands that aren't linked to [`ADBDeviceExt`] trait.
|
||||
// Other methods just have to create a concrete [`ADBDeviceExt`] instance, and return it.
|
||||
@@ -126,10 +132,10 @@ fn main() -> Result<()> {
|
||||
}
|
||||
DeviceCommands::Stat { path } => {
|
||||
let stat_response = device.stat(&path)?;
|
||||
println!("{}", stat_response);
|
||||
println!("{stat_response}");
|
||||
}
|
||||
DeviceCommands::Reboot { reboot_type } => {
|
||||
log::info!("Reboots device in mode {:?}", reboot_type);
|
||||
log::info!("Reboots device in mode {reboot_type:?}");
|
||||
device.reboot(reboot_type.into())?
|
||||
}
|
||||
DeviceCommands::Push { filename, path } => {
|
||||
@@ -146,7 +152,7 @@ fn main() -> Result<()> {
|
||||
device.install(&path)?;
|
||||
}
|
||||
DeviceCommands::Uninstall { package } => {
|
||||
log::info!("Uninstalling the package {}...", package);
|
||||
log::info!("Uninstalling the package {package}...");
|
||||
device.uninstall(&package)?;
|
||||
}
|
||||
DeviceCommands::Framebuffer { path } => {
|
||||
|
||||
@@ -8,6 +8,7 @@ pub enum RebootTypeCommand {
|
||||
Recovery,
|
||||
Sideload,
|
||||
SideloadAutoReboot,
|
||||
Fastboot,
|
||||
}
|
||||
|
||||
impl From<RebootTypeCommand> for RebootType {
|
||||
@@ -18,6 +19,7 @@ impl From<RebootTypeCommand> for RebootType {
|
||||
RebootTypeCommand::Recovery => RebootType::Recovery,
|
||||
RebootTypeCommand::Sideload => RebootType::Sideload,
|
||||
RebootTypeCommand::SideloadAutoReboot => RebootType::SideloadAutoReboot,
|
||||
RebootTypeCommand::Fastboot => RebootType::Fastboot,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
pub fn setup_logger(debug: bool) {
|
||||
/// # Safety
|
||||
///
|
||||
/// This conditionally mutates the process' environment.
|
||||
/// See [`std::env::set_var`] for more info.
|
||||
pub unsafe fn setup_logger(debug: bool) {
|
||||
// RUST_LOG variable has more priority then "--debug" flag
|
||||
if std::env::var("RUST_LOG").is_err() {
|
||||
let level = match debug {
|
||||
|
||||
@@ -7,28 +7,33 @@ license.workspace = true
|
||||
name = "adb_client"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
base64 = { version = "0.22.1" }
|
||||
bincode = { version = "1.3.3" }
|
||||
byteorder = { version = "1.5.0" }
|
||||
chrono = { version = "0.4.40" }
|
||||
homedir = { version = "0.3.4" }
|
||||
image = { version = "0.25.5" }
|
||||
lazy_static = { version = "1.5.0" }
|
||||
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.2" }
|
||||
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" }
|
||||
rand = { version = "0.9.0" }
|
||||
rcgen = { version = "0.13.1" }
|
||||
rcgen = { version = "0.13.1", default-features = false, features = [
|
||||
"aws_lc_rs",
|
||||
"pem",
|
||||
] }
|
||||
regex = { version = "1.11.1", features = ["perf", "std", "unicode"] }
|
||||
rsa = { version = "0.9.7" }
|
||||
rusb = { version = "0.9.4", features = ["vendored"] }
|
||||
rustls = { version = "0.23.22" }
|
||||
rustls-pki-types = "1.11.0"
|
||||
rustls = { version = "0.23.27" }
|
||||
rustls-pki-types = { version = "1.11.0" }
|
||||
serde = { version = "1.0.216", features = ["derive"] }
|
||||
serde_repr = { version = "0.1.19" }
|
||||
sha1 = { version = "0.10.6", features = ["oid"] }
|
||||
@@ -36,7 +41,7 @@ thiserror = { version = "2.0.7" }
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = { version = "1.0.93" }
|
||||
criterion = { version = "0.5.1" } # Used for benchmarks
|
||||
criterion = { version = "0.6.0" } # Used for benchmarks
|
||||
|
||||
[[bench]]
|
||||
harness = false
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
[](./LICENSE-MIT)
|
||||
[](https://docs.rs/adb_client)
|
||||
[](https://crates.io/crates/adb_client)
|
||||

|
||||
|
||||
Rust library implementing ADB protocol.
|
||||
|
||||
|
||||
@@ -144,8 +144,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
MessageCommand::Write => return Ok(()),
|
||||
c => {
|
||||
return Err(RustADBError::ADBRequestFailed(format!(
|
||||
"Wrong command received {}",
|
||||
c
|
||||
"Wrong command received {c}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,19 +19,24 @@ use crate::device::adb_transport_message::{AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AU
|
||||
use crate::{Result, RustADBError, USBTransport};
|
||||
|
||||
pub fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Option<ADBRsaKey>> {
|
||||
Ok(read_to_string(private_key_path.as_ref()).map(|pk| {
|
||||
match ADBRsaKey::new_from_pkcs8(&pk) {
|
||||
Ok(pk) => Some(pk),
|
||||
Err(e) => {
|
||||
log::error!("Error while create RSA private key: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
})?)
|
||||
// Try to read the private key file from given path
|
||||
// If the file is not found, return None
|
||||
// If there is another error while reading the file, return this error
|
||||
// Else, return the private key content
|
||||
let pk = match read_to_string(private_key_path.as_ref()) {
|
||||
Ok(pk) => pk,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
|
||||
match ADBRsaKey::new_from_pkcs8(&pk) {
|
||||
Ok(pk) => Ok(Some(pk)),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Search for adb devices with known interface class and subclass values
|
||||
fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||
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 {
|
||||
@@ -51,13 +56,13 @@ fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||
(None, _) => Ok(None),
|
||||
(Some(identifiers), None) => Ok(Some(*identifiers)),
|
||||
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
|
||||
"Found two Android devices {:04x}:{:04x} and {:04x}:{:04x}",
|
||||
vid1, pid1, vid2, pid2
|
||||
"Found two Android devices {vid1:04x}:{pid1:04x} and {vid2:04x}:{pid2:04x}",
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
||||
/// Check whether a device with given descriptor is an ADB device
|
||||
pub fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
||||
const ADB_SUBCLASS: u8 = 0x42;
|
||||
const ADB_PROTOCOL: u8 = 0x1;
|
||||
|
||||
@@ -134,9 +139,15 @@ impl ADBUSBDevice {
|
||||
transport: USBTransport,
|
||||
private_key_path: PathBuf,
|
||||
) -> Result<Self> {
|
||||
let private_key = match read_adb_private_key(private_key_path)? {
|
||||
let private_key = match read_adb_private_key(&private_key_path)? {
|
||||
Some(pk) => pk,
|
||||
None => ADBRsaKey::new_random()?,
|
||||
None => {
|
||||
log::warn!(
|
||||
"No private key found at path {}. Using a temporary random one.",
|
||||
private_key_path.display()
|
||||
);
|
||||
ADBRsaKey::new_random()?
|
||||
}
|
||||
};
|
||||
|
||||
let mut s = Self {
|
||||
@@ -180,6 +191,11 @@ impl ADBUSBDevice {
|
||||
self.get_transport_mut().write_message(message)?;
|
||||
|
||||
let message = self.get_transport_mut().read_message()?;
|
||||
// If the device returned CNXN instead of AUTH it does not require authentication,
|
||||
// so we can skip the auth steps.
|
||||
if message.header().command() == MessageCommand::Cnxn {
|
||||
return Ok(());
|
||||
}
|
||||
message.assert_command(MessageCommand::Auth)?;
|
||||
|
||||
// At this point, we should have receive an AUTH message with arg0 == 1
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use std::{fs::File, path::Path};
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{
|
||||
ADBMessageTransport, Result,
|
||||
device::{MessageWriter, adb_message_device::ADBMessageDevice},
|
||||
@@ -16,15 +14,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
let file_size = apk_file.metadata()?.len();
|
||||
|
||||
let mut rng = rand::rng();
|
||||
|
||||
let local_id = rng.random();
|
||||
|
||||
self.open_session(format!("exec:cmd package 'install' -S {}\0", file_size).as_bytes())?;
|
||||
self.open_session(format!("exec:cmd package 'install' -S {file_size}\0").as_bytes())?;
|
||||
|
||||
let transport = self.get_transport().clone();
|
||||
|
||||
let mut writer = MessageWriter::new(transport, local_id, self.get_remote_id()?);
|
||||
let mut writer = MessageWriter::new(transport, self.get_local_id()?, self.get_remote_id()?);
|
||||
|
||||
std::io::copy(&mut apk_file, &mut writer)?;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::{
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
|
||||
self.open_session(format!("reboot:{}\0", reboot_type).as_bytes())?;
|
||||
self.open_session(format!("reboot:{reboot_type}\0").as_bytes())?;
|
||||
|
||||
self.get_transport_mut()
|
||||
.read_message()
|
||||
|
||||
@@ -2,13 +2,13 @@ use crate::{ADBMessageTransport, Result, device::adb_message_device::ADBMessageD
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> {
|
||||
self.open_session(format!("exec:cmd package 'uninstall' {}\0", package_name).as_bytes())?;
|
||||
self.open_session(format!("exec:cmd package 'uninstall' {package_name}\0").as_bytes())?;
|
||||
|
||||
let final_status = self.get_transport_mut().read_message()?;
|
||||
|
||||
match final_status.into_payload().as_slice() {
|
||||
b"Success\n" => {
|
||||
log::info!("Package {} successfully uninstalled", package_name);
|
||||
log::info!("Package {package_name} successfully uninstalled");
|
||||
Ok(())
|
||||
}
|
||||
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||
|
||||
@@ -35,10 +35,10 @@ impl<T: ADBMessageTransport> Write for MessageWriter<T> {
|
||||
Ok(response) => {
|
||||
response
|
||||
.assert_command(MessageCommand::Okay)
|
||||
.map_err(|e| Error::new(ErrorKind::Other, e))?;
|
||||
.map_err(Error::other)?;
|
||||
Ok(buf.len())
|
||||
}
|
||||
Err(e) => Err(Error::new(ErrorKind::Other, e)),
|
||||
Err(e) => Err(Error::other(e)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ mod shell_message_writer;
|
||||
use adb_message_device::ADBMessageDevice;
|
||||
pub use adb_tcp_device::ADBTcpDevice;
|
||||
pub use adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader};
|
||||
pub use adb_usb_device::{ADBUSBDevice, get_default_adb_key_path};
|
||||
pub use adb_usb_device::{
|
||||
ADBUSBDevice, get_default_adb_key_path, is_adb_device, search_adb_devices,
|
||||
};
|
||||
pub use message_writer::MessageWriter;
|
||||
pub use models::{ADBRsaKey, MessageCommand, MessageSubcommand};
|
||||
pub use shell_message_writer::ShellMessageWriter;
|
||||
|
||||
@@ -30,8 +30,7 @@ impl ADBEmulatorDevice {
|
||||
let groups = EMULATOR_REGEX
|
||||
.captures(&identifier)
|
||||
.ok_or(RustADBError::DeviceNotFound(format!(
|
||||
"Device {} is likely not an emulator",
|
||||
identifier
|
||||
"Device {identifier} is likely not an emulator"
|
||||
)))?;
|
||||
|
||||
let port = groups
|
||||
|
||||
@@ -17,7 +17,7 @@ mod transports;
|
||||
mod utils;
|
||||
|
||||
pub use adb_device_ext::ADBDeviceExt;
|
||||
pub use device::{ADBTcpDevice, ADBUSBDevice};
|
||||
pub use device::{ADBTcpDevice, ADBUSBDevice, is_adb_device, search_adb_devices};
|
||||
pub use emulator_device::ADBEmulatorDevice;
|
||||
pub use error::{Result, RustADBError};
|
||||
pub use mdns::*;
|
||||
|
||||
@@ -63,8 +63,8 @@ impl Display for AdbServerCommand {
|
||||
AdbServerCommand::Reboot(reboot_type) => {
|
||||
write!(f, "reboot:{reboot_type}")
|
||||
}
|
||||
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{}", addr),
|
||||
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{}", addr),
|
||||
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{addr}"),
|
||||
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{addr}"),
|
||||
AdbServerCommand::Pair(addr, code) => {
|
||||
write!(f, "host:pair:{code}:{addr}")
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ pub enum RebootType {
|
||||
Sideload,
|
||||
/// Same as `Sideload` but reboots after sideloading
|
||||
SideloadAutoReboot,
|
||||
/// Reboots to fastboot
|
||||
Fastboot,
|
||||
}
|
||||
|
||||
impl Display for RebootType {
|
||||
@@ -23,6 +25,7 @@ impl Display for RebootType {
|
||||
RebootType::Recovery => write!(f, "recovery"),
|
||||
RebootType::Sideload => write!(f, "sideload"),
|
||||
RebootType::SideloadAutoReboot => write!(f, "sideload-auto-reboot"),
|
||||
RebootType::Fastboot => write!(f, "fastboot"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ impl ADBServer {
|
||||
break;
|
||||
}
|
||||
|
||||
vec_devices.push(DeviceLong::try_from(device.to_vec())?);
|
||||
vec_devices.push(DeviceLong::try_from(device)?);
|
||||
}
|
||||
|
||||
Ok(vec_devices)
|
||||
|
||||
@@ -32,7 +32,7 @@ impl ADBServer {
|
||||
Ok(service) => {
|
||||
vec_services.push(MDNSServices::try_from(service.as_bytes())?);
|
||||
}
|
||||
Err(e) => log::error!("{}", e),
|
||||
Err(e) => log::error!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{DeviceState, RustADBError};
|
||||
use regex::bytes::Regex;
|
||||
|
||||
static DEVICES_LONG_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"^(?P<identifier>\S+)\s+(?P<state>\w+)\s+(usb:(?P<usb1>\S+)|(?P<usb2>\S+))?\s*(product:(?P<product>\w+)\s+model:(?P<model>\w+)\s+device:(?P<device>\w+)\s+)?transport_id:(?P<transport_id>\d+)$").expect("cannot build devices long regex")
|
||||
Regex::new(r"^(?P<identifier>\S+)\s+(?P<state>\w+)\s+(usb:(?P<usb1>\S+)|(?P<usb2>\S+))?\s*(product:(?P<product>\S+)\s+model:(?P<model>\w+)\s+device:(?P<device>\S+)\s+)?transport_id:(?P<transport_id>\d+)$").expect("cannot build devices long regex")
|
||||
});
|
||||
|
||||
/// Represents a new device with more informations.
|
||||
@@ -44,12 +44,12 @@ impl Display for DeviceLong {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for DeviceLong {
|
||||
impl TryFrom<&[u8]> for DeviceLong {
|
||||
type Error = RustADBError;
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
|
||||
let groups = DEVICES_LONG_REGEX
|
||||
.captures(&value)
|
||||
.captures(value)
|
||||
.ok_or(RustADBError::RegexParsingError)?;
|
||||
|
||||
Ok(DeviceLong {
|
||||
@@ -93,8 +93,22 @@ impl TryFrom<Vec<u8>> for DeviceLong {
|
||||
.ok_or(RustADBError::RegexParsingError)?
|
||||
.as_bytes(),
|
||||
)?,
|
||||
16,
|
||||
10,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_static_devices_long() {
|
||||
let inputs = [
|
||||
"7a5158f05122195aa device 1-5 product:gts210vewifixx model:SM_T813 device:gts210vewifi transport_id:4",
|
||||
"n311r05e device usb:0-1.5 product:alioth model:M2012K11AC device:alioth transport_id:58",
|
||||
"192.168.100.192:5555 device product:alioth model:M2012K11AC device:alioth transport_id:97",
|
||||
"emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:101",
|
||||
"QQ20131020250511 device 20-4 product:NOH-AN00 model:NOH_AN00 device:HWNOH transport_id:3",
|
||||
];
|
||||
for input in inputs {
|
||||
DeviceLong::try_from(input.as_bytes()).expect(&format!("cannot parse input: '{input}'"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ impl ADBServerDevice {
|
||||
"DONE" => {
|
||||
return Ok(());
|
||||
}
|
||||
x => log::error!("Got an unknown response {}", x),
|
||||
x => log::error!("Got an unknown response {x}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,21 +23,26 @@ impl<W: Write> LogFilter<W> {
|
||||
|
||||
impl<W: Write> Write for LogFilter<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
// Add newly received bytes to the internal buffer
|
||||
self.buffer.extend_from_slice(buf);
|
||||
|
||||
let buf_clone = self.buffer.clone();
|
||||
let mut lines = buf_clone.split_inclusive(|&byte| byte == b'\n').peekable();
|
||||
let mut processed = 0;
|
||||
while let Some(pos) = self.buffer[processed..].iter().position(|&b| b == b'\n') {
|
||||
// Found a newline, need to process it
|
||||
let end = processed + pos + 1; // +1 to include the '\n'
|
||||
let line = &self.buffer[processed..end];
|
||||
|
||||
while let Some(line) = lines.next() {
|
||||
if lines.peek().is_some() {
|
||||
if self.should_write(line) {
|
||||
self.writer.write_all(line)?;
|
||||
}
|
||||
} else {
|
||||
// This is the last (unfinished) element, we keep it for next round
|
||||
self.buffer = line.to_vec();
|
||||
break;
|
||||
if self.should_write(line) {
|
||||
self.writer.write_all(line)?;
|
||||
}
|
||||
|
||||
processed = end;
|
||||
}
|
||||
|
||||
// Keep only remaining bytes after the last complete line
|
||||
if processed > 0 {
|
||||
self.buffer.copy_within(processed.., 0);
|
||||
self.buffer.truncate(self.buffer.len() - processed);
|
||||
}
|
||||
|
||||
Ok(buf.len())
|
||||
|
||||
@@ -42,18 +42,14 @@ impl<R: Read> Read for ADBRecvCommandReader<R> {
|
||||
let mut error_msg = vec![0; length];
|
||||
self.inner.read_exact(&mut error_msg)?;
|
||||
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!(
|
||||
"ADB request failed: {}",
|
||||
String::from_utf8_lossy(&error_msg)
|
||||
),
|
||||
))
|
||||
Err(std::io::Error::other(format!(
|
||||
"ADB request failed: {}",
|
||||
String::from_utf8_lossy(&error_msg)
|
||||
)))
|
||||
}
|
||||
_ => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("Unknown response from device {:#?}", header),
|
||||
)),
|
||||
_ => Err(std::io::Error::other(format!(
|
||||
"Unknown response from device {header:#?}"
|
||||
))),
|
||||
}
|
||||
} else {
|
||||
// Computing minimum to ensure to stop reading before next header...
|
||||
|
||||
@@ -31,8 +31,7 @@ impl ADBServerDevice {
|
||||
Ok(data.into())
|
||||
}
|
||||
x => Err(RustADBError::UnknownResponseType(format!(
|
||||
"Unknown response {}",
|
||||
x
|
||||
"Unknown response {x}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ impl ADBServerDevice {
|
||||
|
||||
match &data[0..read_amount] {
|
||||
b"Success\n" => {
|
||||
log::info!("Package {} successfully uninstalled", package_name);
|
||||
log::info!("Package {package_name} successfully uninstalled");
|
||||
Ok(())
|
||||
}
|
||||
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||
|
||||
@@ -99,13 +99,13 @@ impl TCPServerTransport {
|
||||
}
|
||||
|
||||
/// Gets the body length from a LittleEndian value
|
||||
pub(crate) fn get_body_length(&mut self) -> Result<u32> {
|
||||
pub(crate) fn get_body_length(&self) -> Result<u32> {
|
||||
let length_buffer = self.read_body_length()?;
|
||||
Ok(LittleEndian::read_u32(&length_buffer))
|
||||
}
|
||||
|
||||
/// Read 4 bytes representing body length
|
||||
fn read_body_length(&mut self) -> Result<[u8; 4]> {
|
||||
fn read_body_length(&self) -> Result<[u8; 4]> {
|
||||
let mut length_buffer = [0; 4];
|
||||
self.get_raw_connection()?.read_exact(&mut length_buffer)?;
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ impl TcpTransport {
|
||||
})
|
||||
}
|
||||
|
||||
fn get_current_connection(&mut self) -> Result<Arc<Mutex<CurrentConnection>>> {
|
||||
fn get_current_connection(&self) -> Result<Arc<Mutex<CurrentConnection>>> {
|
||||
self.current_connection
|
||||
.as_ref()
|
||||
.ok_or(RustADBError::IOError(std::io::Error::new(
|
||||
@@ -175,8 +175,7 @@ impl TcpTransport {
|
||||
Ok(())
|
||||
}
|
||||
c => Err(RustADBError::ADBRequestFailed(format!(
|
||||
"Wrong command received {}",
|
||||
c
|
||||
"Wrong command received {c}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ use crate::{
|
||||
struct Endpoint {
|
||||
iface: u8,
|
||||
address: u8,
|
||||
max_packet_size: usize,
|
||||
}
|
||||
|
||||
/// Transport running on USB
|
||||
@@ -39,8 +40,7 @@ impl USBTransport {
|
||||
}
|
||||
|
||||
Err(RustADBError::DeviceNotFound(format!(
|
||||
"cannot find USB device with vendor_id={} and product_id={}",
|
||||
vendor_id, product_id
|
||||
"cannot find USB device with vendor_id={vendor_id} and product_id={product_id}",
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -111,6 +111,7 @@ impl USBTransport {
|
||||
let endpoint = Endpoint {
|
||||
iface: interface_desc.interface_number(),
|
||||
address: endpoint_desc.address(),
|
||||
max_packet_size: endpoint_desc.max_packet_size() as usize,
|
||||
};
|
||||
match endpoint_desc.direction() {
|
||||
Direction::In => {
|
||||
@@ -136,6 +137,29 @@ impl USBTransport {
|
||||
|
||||
Err(RustADBError::USBNoDescriptorFound)
|
||||
}
|
||||
|
||||
fn write_bulk_data(&self, data: &[u8], timeout: Duration) -> Result<()> {
|
||||
let endpoint = self.get_write_endpoint()?;
|
||||
let handle = self.get_raw_connection()?;
|
||||
let max_packet_size = endpoint.max_packet_size;
|
||||
|
||||
let mut offset = 0;
|
||||
let data_len = data.len();
|
||||
while offset < data_len {
|
||||
let end = (offset + max_packet_size).min(data_len);
|
||||
let write_amount = handle.write_bulk(endpoint.address, &data[offset..end], timeout)?;
|
||||
offset += write_amount;
|
||||
|
||||
log::trace!("wrote chunk of size {write_amount} - {offset}/{data_len}",)
|
||||
}
|
||||
|
||||
if offset % max_packet_size == 0 {
|
||||
log::trace!("must send final zero-length packet");
|
||||
handle.write_bulk(endpoint.address, &[], timeout)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ADBTransport for USBTransport {
|
||||
@@ -145,9 +169,11 @@ impl ADBTransport for USBTransport {
|
||||
let (read_endpoint, write_endpoint) = self.find_endpoints(&device)?;
|
||||
|
||||
Self::configure_endpoint(&device, &read_endpoint)?;
|
||||
log::debug!("got read endpoint: {read_endpoint:?}");
|
||||
self.read_endpoint = Some(read_endpoint);
|
||||
|
||||
Self::configure_endpoint(&device, &write_endpoint)?;
|
||||
log::debug!("got write endpoint: {write_endpoint:?}");
|
||||
self.write_endpoint = Some(write_endpoint);
|
||||
|
||||
self.handle = Some(Arc::new(device));
|
||||
@@ -157,7 +183,21 @@ impl ADBTransport for USBTransport {
|
||||
|
||||
fn disconnect(&mut self) -> crate::Result<()> {
|
||||
let message = ADBTransportMessage::new(MessageCommand::Clse, 0, 0, &[]);
|
||||
self.write_message(message)
|
||||
if let Err(e) = self.write_message(message) {
|
||||
log::error!("error while sending CLSE message: {e}");
|
||||
}
|
||||
|
||||
if let Some(handle) = &self.handle {
|
||||
let endpoint = self.read_endpoint.as_ref().or(self.write_endpoint.as_ref());
|
||||
if let Some(endpoint) = &endpoint {
|
||||
match handle.release_interface(endpoint.iface) {
|
||||
Ok(()) => log::debug!("succesfully released interface"),
|
||||
Err(e) => log::error!("error while release interface: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,29 +207,15 @@ impl ADBMessageTransport for USBTransport {
|
||||
message: ADBTransportMessage,
|
||||
timeout: Duration,
|
||||
) -> Result<()> {
|
||||
let endpoint = self.get_write_endpoint()?;
|
||||
let handle = self.get_raw_connection()?;
|
||||
|
||||
let message_bytes = message.header().as_bytes()?;
|
||||
let mut total_written = 0;
|
||||
loop {
|
||||
total_written +=
|
||||
handle.write_bulk(endpoint.address, &message_bytes[total_written..], timeout)?;
|
||||
if total_written == message_bytes.len() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.write_bulk_data(&message_bytes, timeout)?;
|
||||
|
||||
log::trace!("successfully write header: {} bytes", message_bytes.len());
|
||||
|
||||
let payload = message.into_payload();
|
||||
if !payload.is_empty() {
|
||||
let mut total_written = 0;
|
||||
loop {
|
||||
total_written +=
|
||||
handle.write_bulk(endpoint.address, &payload[total_written..], timeout)?;
|
||||
if total_written == payload.len() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.write_bulk_data(&payload, timeout)?;
|
||||
log::trace!("successfully write payload: {} bytes", payload.len());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -198,29 +224,26 @@ impl ADBMessageTransport for USBTransport {
|
||||
fn read_message_with_timeout(&mut self, timeout: Duration) -> Result<ADBTransportMessage> {
|
||||
let endpoint = self.get_read_endpoint()?;
|
||||
let handle = self.get_raw_connection()?;
|
||||
let max_packet_size = endpoint.max_packet_size;
|
||||
|
||||
let mut data = [0; 24];
|
||||
let mut total_read = 0;
|
||||
loop {
|
||||
total_read += handle.read_bulk(endpoint.address, &mut data[total_read..], timeout)?;
|
||||
if total_read == data.len() {
|
||||
break;
|
||||
}
|
||||
let mut data = [0u8; 24];
|
||||
let mut offset = 0;
|
||||
while offset < data.len() {
|
||||
let end = (offset + max_packet_size).min(data.len());
|
||||
let chunk = &mut data[offset..end];
|
||||
offset += handle.read_bulk(endpoint.address, chunk, timeout)?;
|
||||
}
|
||||
|
||||
let header = ADBTransportMessageHeader::try_from(data)?;
|
||||
|
||||
log::trace!("received header {header:?}");
|
||||
|
||||
if header.data_length() != 0 {
|
||||
let mut msg_data = vec![0_u8; header.data_length() as usize];
|
||||
let mut total_read = 0;
|
||||
loop {
|
||||
total_read +=
|
||||
handle.read_bulk(endpoint.address, &mut msg_data[total_read..], timeout)?;
|
||||
if total_read == msg_data.capacity() {
|
||||
break;
|
||||
}
|
||||
let mut offset = 0;
|
||||
while offset < msg_data.len() {
|
||||
let end = (offset + max_packet_size).min(msg_data.len());
|
||||
let chunk = &mut msg_data[offset..end];
|
||||
offset += handle.read_bulk(endpoint.address, chunk, timeout)?;
|
||||
}
|
||||
|
||||
let message = ADBTransportMessage::from_header_and_payload(header, msg_data);
|
||||
|
||||
@@ -12,5 +12,7 @@ pub fn check_extension_is_apk<P: AsRef<Path>>(path: P) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
log::debug!("Given file is an APK");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ name = "pyadb_client"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
version.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
@@ -21,10 +22,6 @@ name = "stub_gen"
|
||||
[dependencies]
|
||||
adb_client = { path = "../adb_client" }
|
||||
anyhow = { version = "1.0.95" }
|
||||
pyo3 = { version = "0.24.1", features = [
|
||||
"abi3-py37",
|
||||
"anyhow",
|
||||
"extension-module",
|
||||
] }
|
||||
pyo3 = { version = "0.25.0", features = ["abi3-py37", "anyhow"] }
|
||||
pyo3-stub-gen = "0.7.0"
|
||||
pyo3-stub-gen-derive = "0.7.0"
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
# pyadb_client
|
||||
|
||||
Python library to communicate with ADB devices. Built on top of Rust `adb_client` library.
|
||||
<p align="center">
|
||||
<p align="center">Python library to communicate with ADB devices. Built on top of Rust adb_client library.</p>
|
||||
<p align="center">
|
||||
<a href="https://pypi.org/project/pyadb_client">
|
||||
<img alt="pypi.org" src="https://img.shields.io/pypi/v/pyadb_client.svg" />
|
||||
</a>
|
||||
<a href="https://pypi.org/project/pyadb_client">
|
||||
<img alt="downloads" src="https://static.pepy.tech/badge/pyadb_client" />
|
||||
</a>
|
||||
</p>
|
||||
</p>
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -37,7 +47,6 @@ usb_device.push("file.txt", "/data/local/tmp/file.txt")
|
||||
|
||||
```bash
|
||||
# Create Python virtual environment
|
||||
cd pyadb_client
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
|
||||
|
||||
101
tests/tests.rs
101
tests/tests.rs
@@ -1,101 +0,0 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use adb_client::{ADBServer, ADBServerDevice, DeviceLong};
|
||||
use rand::Rng;
|
||||
|
||||
fn new_client() -> ADBServer {
|
||||
ADBServer::default()
|
||||
}
|
||||
|
||||
fn new_device() -> ADBServerDevice {
|
||||
let mut client = new_client();
|
||||
return client.get_device().expect("cannot get device");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_version() {
|
||||
let mut adb = new_client();
|
||||
adb.version().expect("cannot get adb version");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shell_commands() {
|
||||
let mut device = new_device();
|
||||
|
||||
device.shell_command(["ls"]).expect("error while executing `ls` command");
|
||||
device.shell_command(["pwd"]).expect("error while executing `pwd` command");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_devices() {
|
||||
let mut adb = new_client();
|
||||
adb.devices().expect("cannot list devices");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_devices_long() {
|
||||
let mut adb = new_client();
|
||||
adb.devices_long().expect("cannot list devices long");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_static_devices_long() {
|
||||
let inputs = ["7a5158f05122195aa device 1-5 product:gts210vewifixx model:SM_T813 device:gts210vewifi transport_id:4",
|
||||
"n311r05e device usb:0-1.5 product:alioth model:M2012K11AC device:alioth transport_id:58",
|
||||
"192.168.100.192:5555 device product:alioth model:M2012K11AC device:alioth transport_id:97",
|
||||
"emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:101"];
|
||||
for input in inputs {
|
||||
DeviceLong::try_from(input.as_bytes().to_vec())
|
||||
.expect(&format!("cannot parse input: '{input}'"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_recv() {
|
||||
// Create random "Reader" in memory
|
||||
let mut key = [0u8; 1000];
|
||||
rand::thread_rng().fill(&mut key[..]);
|
||||
let mut c: Cursor<Vec<u8>> = Cursor::new(key.to_vec());
|
||||
|
||||
let mut device = new_device();
|
||||
|
||||
const TEST_FILENAME: &'static str = "/data/local/tmp/test_file";
|
||||
// Send it
|
||||
device
|
||||
.send(&mut c, TEST_FILENAME)
|
||||
.expect("cannot send file");
|
||||
|
||||
// Pull it to memory
|
||||
let mut res = vec![];
|
||||
device
|
||||
.recv(TEST_FILENAME, &mut res)
|
||||
.expect("cannot recv file");
|
||||
|
||||
// diff
|
||||
assert_eq!(c.get_ref(), &res);
|
||||
|
||||
device
|
||||
.shell_command::<&str>([format!("rm {TEST_FILENAME}").as_str()])
|
||||
.expect("cannot remove test file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_connexions() {
|
||||
let mut connection = new_client();
|
||||
|
||||
for _ in 0..2 {
|
||||
let _ = connection.devices().expect("cannot get version");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_emulator() {
|
||||
let mut connection = new_client();
|
||||
let mut emulator = connection
|
||||
.get_emulator_device()
|
||||
.expect("no emulator running");
|
||||
emulator.hello().expect("cannot hello");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user