From 901d24746e929f5b6d422e63b0865680f96f4925 Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Wed, 27 Aug 2025 19:40:27 +0200 Subject: [PATCH 1/9] breaking(features): add mdns feature + example --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 36efb7dc..e178f5aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["adb_cli", "adb_client", "pyadb_client", "examples/mdns"] +members = ["adb_cli", "adb_client", "examples/mdns", "pyadb_client"] resolver = "2" [workspace.package] From 8fd0f9f1d347aa5d2465f4f2d719ee0dd3d6169b Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Wed, 27 Aug 2025 19:44:59 +0200 Subject: [PATCH 2/9] doc: add section in README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 3aade10d..051f3e10 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,12 @@ Provides a "real-world" usage example of this library. Improved documentation available [here](./adb_cli/README.md). +## examples + +Some examples are available in the `examples` directory: + +- `examples/mdns`: mDNS device discovery example + ## pyadb_client Python wrapper using `adb_client` library to export classes usable directly from a Python environment. From a2a780ac88f88774fec558f192cf2d0e54974d51 Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Wed, 27 Aug 2025 21:00:11 +0200 Subject: [PATCH 3/9] breaking(feature): add usb feature --- adb_cli/Cargo.toml | 2 +- adb_client/Cargo.toml | 13 ++++++++++--- adb_client/README.md | 1 + adb_client/src/device/adb_tcp_device.rs | 3 ++- adb_client/src/device/adb_usb_device.rs | 15 +-------------- adb_client/src/device/mod.rs | 17 +++++++++++++---- adb_client/src/device/models/mod.rs | 4 ++++ adb_client/src/error.rs | 6 ++++++ adb_client/src/lib.rs | 7 ++++++- adb_client/src/transports/mod.rs | 6 ++++++ adb_client/src/transports/tcp_transport.rs | 5 ++--- adb_client/src/utils.rs | 19 ++++++++++++++++++- 12 files changed, 70 insertions(+), 28 deletions(-) diff --git a/adb_cli/Cargo.toml b/adb_cli/Cargo.toml index 4d170b68..f3e1ce18 100644 --- a/adb_cli/Cargo.toml +++ b/adb_cli/Cargo.toml @@ -14,7 +14,7 @@ version.workspace = true workspace = true [dependencies] -adb_client = { version = "^2.1.19", features = ["mdns"] } +adb_client = { version = "^2.1.19", features = ["mdns", "usb"] } clap = { version = "4.5.53", features = ["derive"] } env_logger = { version = "0.11.8" } log = { version = "0.4.29" } diff --git a/adb_client/Cargo.toml b/adb_client/Cargo.toml index def76dd2..d09e11e1 100644 --- a/adb_client/Cargo.toml +++ b/adb_client/Cargo.toml @@ -20,6 +20,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = [] mdns = ["dep:mdns-sd"] +usb = ["dep:rsa", "dep:rusb"] [dependencies] base64 = { version = "0.22.1" } @@ -32,10 +33,11 @@ num-bigint = { version = "0.8.6", package = "num-bigint-dig" } num-traits = { version = "0.2.19" } quick-protobuf = { version = "0.8.1" } rand = { version = "0.9.2" } -rcgen = { version = "0.14.6" } +rcgen = { version = "0.14.6", default-features = false, features = [ + "aws_lc_rs", + "pem", +] } regex = { version = "1.12.2", features = ["perf", "std", "unicode"] } -rsa = { version = "0.9.9" } -rusb = { version = "0.9.4", features = ["vendored"] } rustls = { version = "0.23.35" } rustls-pki-types = { version = "1.13.2" } serde = { version = "1.0.228", features = ["derive"] } @@ -49,6 +51,11 @@ mdns-sd = { version = "0.17.1", default-features = false, features = [ "logging", ], optional = true } ######### +######### +# USB-only dependencies +rsa = { version = "0.9.7", optional = true } +rusb = { version = "0.9.4", features = ["vendored"], optional = true } +######### [dev-dependencies] anyhow = { version = "1.0.100" } diff --git a/adb_client/README.md b/adb_client/README.md index 94116ac0..b459efcd 100644 --- a/adb_client/README.md +++ b/adb_client/README.md @@ -21,6 +21,7 @@ adb_client = "*" | Feature | Description | Default? | | :-----: | :---------------------------------------------: | :------: | | `mdns` | Enables mDNS device discovery on local network. | No | +| `usb` | Enables interactions with USB devices. | No | To deactivate some default features you can use the `default-features = false` option in your `Cargo.toml` file and manually specify the features you want to activate: diff --git a/adb_client/src/device/adb_tcp_device.rs b/adb_client/src/device/adb_tcp_device.rs index 93feec82..a26c70bd 100644 --- a/adb_client/src/device/adb_tcp_device.rs +++ b/adb_client/src/device/adb_tcp_device.rs @@ -4,8 +4,9 @@ use std::{io::Read, net::SocketAddr}; use super::adb_message_device::ADBMessageDevice; use super::models::MessageCommand; -use super::{ADBRsaKey, ADBTransportMessage, get_default_adb_key_path}; +use super::{ADBRsaKey, ADBTransportMessage}; use crate::device::adb_usb_device::read_adb_private_key; +use crate::utils::get_default_adb_key_path; use crate::{ADBDeviceExt, ADBMessageTransport, ADBTransport, Result, TcpTransport}; /// Represent a device reached and available over TCP. diff --git a/adb_client/src/device/adb_usb_device.rs b/adb_client/src/device/adb_usb_device.rs index 0c7d98cb..241de0b8 100644 --- a/adb_client/src/device/adb_usb_device.rs +++ b/adb_client/src/device/adb_usb_device.rs @@ -14,6 +14,7 @@ use super::{ADBRsaKey, ADBTransportMessage}; use crate::ADBDeviceExt; use crate::ADBMessageTransport; use crate::ADBTransport; +use crate::utils::get_default_adb_key_path; use crate::{Result, RustADBError, USBTransport}; pub fn read_adb_private_key>(private_key_path: P) -> Result> { @@ -90,20 +91,6 @@ pub fn is_adb_device(device: &Device, des: &DeviceDescriptor) false } -/// Get the default path to the ADB key file. -/// First checks for the presence of the environment variable `ANDROID_USER_HOME`, defaulting to the user's home directory. -pub fn get_default_adb_key_path() -> Result { - let android_user_home = std::env::var("ANDROID_USER_HOME") - .ok() - .map(|android_user_home| PathBuf::from(android_user_home).join("android")); - let default_dot_android = std::env::home_dir().map(|home| home.join(".android")); - - Ok(android_user_home - .or(default_dot_android) - .ok_or(RustADBError::NoHomeDirectory)? - .join("adbkey")) -} - /// Represent a device reached and available over USB. #[derive(Debug)] pub struct ADBUSBDevice { diff --git a/adb_client/src/device/mod.rs b/adb_client/src/device/mod.rs index a34b6a18..d9bbf067 100644 --- a/adb_client/src/device/mod.rs +++ b/adb_client/src/device/mod.rs @@ -2,6 +2,9 @@ mod adb_message_device; mod adb_message_device_commands; mod adb_tcp_device; mod adb_transport_message; + +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] mod adb_usb_device; mod commands; mod message_writer; @@ -11,9 +14,15 @@ 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, is_adb_device, search_adb_devices, -}; + +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] +pub use adb_usb_device::{ADBUSBDevice, is_adb_device, search_adb_devices}; + pub use message_writer::MessageWriter; -pub use models::{ADBRsaKey, MessageCommand, MessageSubcommand}; +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] +pub use models::ADBRsaKey; + +pub use models::{MessageCommand, MessageSubcommand}; pub use shell_message_writer::ShellMessageWriter; diff --git a/adb_client/src/device/models/mod.rs b/adb_client/src/device/models/mod.rs index d4184026..d48d8437 100644 --- a/adb_client/src/device/models/mod.rs +++ b/adb_client/src/device/models/mod.rs @@ -1,7 +1,11 @@ +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] mod adb_rsa_key; mod adb_session; mod message_commands; +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] pub use adb_rsa_key::ADBRsaKey; pub(crate) use adb_session::ADBSession; pub use message_commands::{MessageCommand, MessageSubcommand}; diff --git a/adb_client/src/error.rs b/adb_client/src/error.rs index 618aa2c4..837ac053 100644 --- a/adb_client/src/error.rs +++ b/adb_client/src/error.rs @@ -67,6 +67,8 @@ pub enum RustADBError { #[error("Cannot get home directory")] NoHomeDirectory, /// Generic USB error + #[cfg(feature = "usb")] + #[cfg_attr(docsrs, doc(cfg(feature = "usb")))] #[error("USB Error: {0}")] UsbError(#[from] rusb::Error), /// USB device not found @@ -85,6 +87,8 @@ pub enum RustADBError { #[error(transparent)] Base64EncodeError(#[from] base64::EncodeSliceError), /// An error occurred with RSA engine + #[cfg(feature = "usb")] + #[cfg_attr(docsrs, doc(cfg(feature = "usb")))] #[error(transparent)] RSAError(#[from] rsa::errors::Error), /// Cannot convert given data from slice @@ -94,6 +98,8 @@ pub enum RustADBError { #[error("wrong file extension: {0}")] WrongFileExtension(String), /// An error occurred with PKCS8 data + #[cfg(feature = "usb")] + #[cfg_attr(docsrs, doc(cfg(feature = "usb")))] #[error("error with pkcs8: {0}")] RsaPkcs8Error(#[from] rsa::pkcs8::Error), /// Error during certificate generation diff --git a/adb_client/src/lib.rs b/adb_client/src/lib.rs index c289844b..e7665db5 100644 --- a/adb_client/src/lib.rs +++ b/adb_client/src/lib.rs @@ -27,7 +27,12 @@ mod transports; mod utils; pub use adb_device_ext::ADBDeviceExt; -pub use device::{ADBTcpDevice, ADBUSBDevice, is_adb_device, search_adb_devices}; +pub use device::ADBTcpDevice; + +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] +pub use device::{ADBUSBDevice, is_adb_device, search_adb_devices}; + pub use emulator_device::ADBEmulatorDevice; pub use error::{Result, RustADBError}; pub use models::{ADBListItem, ADBListItemType, AdbStatResponse, RebootType}; diff --git a/adb_client/src/transports/mod.rs b/adb_client/src/transports/mod.rs index 28e89a06..da50c1fa 100644 --- a/adb_client/src/transports/mod.rs +++ b/adb_client/src/transports/mod.rs @@ -2,10 +2,16 @@ mod tcp_emulator_transport; mod tcp_server_transport; mod tcp_transport; mod traits; + +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] mod usb_transport; pub use tcp_emulator_transport::TCPEmulatorTransport; pub use tcp_server_transport::TCPServerTransport; pub use tcp_transport::TcpTransport; pub use traits::{ADBMessageTransport, ADBTransport}; + +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] pub use usb_transport::USBTransport; diff --git a/adb_client/src/transports/tcp_transport.rs b/adb_client/src/transports/tcp_transport.rs index d420b28b..cb572a58 100644 --- a/adb_client/src/transports/tcp_transport.rs +++ b/adb_client/src/transports/tcp_transport.rs @@ -8,9 +8,8 @@ use rustls::{ use super::{ADBMessageTransport, ADBTransport}; use crate::{ Result, RustADBError, - device::{ - ADBTransportMessage, ADBTransportMessageHeader, MessageCommand, get_default_adb_key_path, - }, + device::{ADBTransportMessage, ADBTransportMessageHeader, MessageCommand}, + utils::get_default_adb_key_path, }; use std::{ fs::read_to_string, diff --git a/adb_client/src/utils.rs b/adb_client/src/utils.rs index cda1a586..5551dbe0 100644 --- a/adb_client/src/utils.rs +++ b/adb_client/src/utils.rs @@ -1,4 +1,7 @@ -use std::{ffi::OsStr, path::Path}; +use std::{ + ffi::OsStr, + path::{Path, PathBuf}, +}; use crate::{Result, RustADBError}; @@ -16,3 +19,17 @@ pub fn check_extension_is_apk>(path: P) -> Result<()> { Ok(()) } + +/// Get the default path to the ADB key file. +/// First checks for the presence of the environment variable `ANDROID_USER_HOME`, defaulting to the user's home directory. +pub fn get_default_adb_key_path() -> Result { + let android_user_home = std::env::var("ANDROID_USER_HOME") + .ok() + .map(|android_user_home| PathBuf::from(android_user_home).join("android")); + let default_dot_android = std::env::home_dir().map(|home| home.join(".android")); + + Ok(android_user_home + .or(default_dot_android) + .ok_or(RustADBError::NoHomeDirectory)? + .join("adbkey")) +} From 34912e0e7535c1af2ea0dcb6ffadeab7eaa8eb22 Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Wed, 27 Aug 2025 21:03:45 +0200 Subject: [PATCH 4/9] fix: python package build --- pyadb_client/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyadb_client/Cargo.toml b/pyadb_client/Cargo.toml index 463d0370..845b24a6 100644 --- a/pyadb_client/Cargo.toml +++ b/pyadb_client/Cargo.toml @@ -23,7 +23,7 @@ doc = false name = "stub_gen" [dependencies] -adb_client = { path = "../adb_client" } +adb_client = { path = "../adb_client", features = ["usb"] } anyhow = { version = "1.0.100" } pyo3 = { version = "0.27.2", features = ["abi3-py310", "anyhow"] } pyo3-stub-gen = { version = "0.17.2" } From b72dbf26b661d33ec06f6b57739fe15c29fb3fc2 Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Mon, 1 Sep 2025 21:40:40 +0200 Subject: [PATCH 5/9] feat: improve modules ordering --- .gitignore | 2 +- adb_cli/src/handlers/emulator_commands.rs | 2 +- adb_cli/src/handlers/host_commands.rs | 5 ++- adb_cli/src/handlers/local_commands.rs | 6 +-- adb_cli/src/main.rs | 10 +++-- adb_cli/src/models/host.rs | 2 +- adb_client/README.md | 12 +++--- adb_client/src/adb_device_ext.rs | 6 +-- .../{transports/traits => }/adb_transport.rs | 0 adb_client/src/constants.rs | 1 - adb_client/src/device/mod.rs | 28 -------------- adb_client/src/device/models/mod.rs | 11 ------ .../adb_emulator_device.rs | 5 ++- .../commands/mod.rs | 0 .../commands/rotate.rs | 5 ++- .../commands/sms.rs | 5 ++- .../src/{emulator_device => emulator}/mod.rs | 4 +- .../models/adb_emulator_command.rs | 0 .../models/mod.rs | 0 .../tcp_emulator_transport.rs | 5 ++- adb_client/src/lib.rs | 35 ++++++++--------- adb_client/src/mdns/mdns_discovery.rs | 17 ++++----- .../adb_message_device.rs | 27 +++++++++---- .../adb_message_device_commands.rs | 12 ++++-- .../adb_message_transport.rs | 6 ++- .../models => message_devices}/adb_session.rs | 0 .../adb_transport_message.rs | 12 ++++-- .../commands/framebuffer.rs | 7 +++- .../commands/install.rs | 7 +++- .../commands/list.rs | 21 +++++----- .../commands/mod.rs | 1 + .../commands/pull.rs | 13 ++++--- .../commands/push.rs | 12 +++--- .../commands/reboot.rs | 7 +++- .../commands/shell.rs | 10 +++-- .../commands/stat.rs | 5 ++- .../commands/uninstall.rs | 7 +++- .../commands/utils}/message_writer.rs | 7 ++-- .../src/message_devices/commands/utils/mod.rs | 4 ++ .../commands/utils}/shell_message_writer.rs | 7 ++-- .../message_commands.rs | 0 adb_client/src/message_devices/mod.rs | 18 +++++++++ .../models/adb_rsa_key.rs | 21 ++++++++++ adb_client/src/message_devices/models/mod.rs | 4 ++ .../tcp}/adb_tcp_device.rs | 16 +++++--- adb_client/src/message_devices/tcp/mod.rs | 4 ++ .../tcp}/tcp_transport.rs | 10 +++-- .../usb}/adb_usb_device.rs | 38 +++++++------------ adb_client/src/message_devices/usb/mod.rs | 5 +++ .../usb}/usb_transport.rs | 8 +++- adb_client/src/models/host_features.rs | 3 ++ adb_client/src/models/mod.rs | 6 +-- adb_client/src/server/adb_server.rs | 2 +- .../{models => server}/adb_server_command.rs | 6 ++- adb_client/src/server/commands/connect.rs | 5 ++- adb_client/src/server/commands/devices.rs | 6 ++- adb_client/src/server/commands/disconnect.rs | 5 ++- adb_client/src/server/commands/kill.rs | 5 ++- adb_client/src/server/commands/mdns.rs | 3 +- adb_client/src/server/commands/pair.rs | 6 ++- adb_client/src/server/commands/reconnect.rs | 5 ++- .../src/server/commands/server_status.rs | 5 ++- adb_client/src/server/commands/version.rs | 5 ++- .../src/server/commands/wait_for_device.rs | 3 +- adb_client/src/server/mod.rs | 4 ++ adb_client/src/server/models/device_long.rs | 3 +- adb_client/src/server/models/device_short.rs | 2 +- .../tcp_server_transport.rs | 3 +- .../src/server_device/adb_server_device.rs | 5 ++- .../adb_server_device_commands.rs | 8 ++-- .../src/server_device/commands/forward.rs | 2 +- .../src/server_device/commands/framebuffer.rs | 6 ++- .../server_device/commands/host_features.rs | 3 +- .../src/server_device/commands/install.rs | 2 +- adb_client/src/server_device/commands/list.rs | 6 ++- .../src/server_device/commands/logcat.rs | 2 +- .../src/server_device/commands/reboot.rs | 5 +-- .../src/server_device/commands/reconnect.rs | 2 +- adb_client/src/server_device/commands/recv.rs | 9 +++-- .../src/server_device/commands/reverse.rs | 2 +- adb_client/src/server_device/commands/send.rs | 12 ++++-- adb_client/src/server_device/commands/stat.rs | 4 +- .../src/server_device/commands/tcpip.rs | 2 +- .../src/server_device/commands/transport.rs | 2 +- .../src/server_device/commands/uninstall.rs | 2 +- adb_client/src/server_device/commands/usb.rs | 2 +- adb_client/src/transports/mod.rs | 17 --------- adb_client/src/transports/traits/mod.rs | 5 --- pyadb_client/src/adb_server.rs | 2 +- pyadb_client/src/adb_server_device.rs | 2 +- pyadb_client/src/adb_usb_device.rs | 2 +- pyadb_client/src/models/devices.rs | 2 +- 92 files changed, 364 insertions(+), 269 deletions(-) rename adb_client/src/{transports/traits => }/adb_transport.rs (100%) delete mode 100644 adb_client/src/constants.rs delete mode 100644 adb_client/src/device/mod.rs delete mode 100644 adb_client/src/device/models/mod.rs rename adb_client/src/{emulator_device => emulator}/adb_emulator_device.rs (94%) rename adb_client/src/{emulator_device => emulator}/commands/mod.rs (100%) rename adb_client/src/{emulator_device => emulator}/commands/rotate.rs (72%) rename adb_client/src/{emulator_device => emulator}/commands/sms.rs (80%) rename adb_client/src/{emulator_device => emulator}/mod.rs (61%) rename adb_client/src/{emulator_device => emulator}/models/adb_emulator_command.rs (100%) rename adb_client/src/{emulator_device => emulator}/models/mod.rs (100%) rename adb_client/src/{transports => emulator}/tcp_emulator_transport.rs (97%) rename adb_client/src/{device => message_devices}/adb_message_device.rs (95%) rename adb_client/src/{device => message_devices}/adb_message_device_commands.rs (80%) rename adb_client/src/{transports/traits => message_devices}/adb_message_transport.rs (90%) rename adb_client/src/{device/models => message_devices}/adb_session.rs (100%) rename adb_client/src/{device => message_devices}/adb_transport_message.rs (93%) rename adb_client/src/{device => message_devices}/commands/framebuffer.rs (94%) rename adb_client/src/{device => message_devices}/commands/install.rs (87%) rename adb_client/src/{device => message_devices}/commands/list.rs (93%) rename adb_client/src/{device => message_devices}/commands/mod.rs (90%) rename adb_client/src/{device => message_devices}/commands/pull.rs (80%) rename adb_client/src/{device => message_devices}/commands/push.rs (69%) rename adb_client/src/{device => message_devices}/commands/reboot.rs (66%) rename adb_client/src/{device => message_devices}/commands/shell.rs (91%) rename adb_client/src/{device => message_devices}/commands/stat.rs (71%) rename adb_client/src/{device => message_devices}/commands/uninstall.rs (81%) rename adb_client/src/{device => message_devices/commands/utils}/message_writer.rs (89%) create mode 100644 adb_client/src/message_devices/commands/utils/mod.rs rename adb_client/src/{device => message_devices/commands/utils}/shell_message_writer.rs (86%) rename adb_client/src/{device/models => message_devices}/message_commands.rs (100%) create mode 100644 adb_client/src/message_devices/mod.rs rename adb_client/src/{device => message_devices}/models/adb_rsa_key.rs (90%) create mode 100644 adb_client/src/message_devices/models/mod.rs rename adb_client/src/{device => message_devices/tcp}/adb_tcp_device.rs (88%) create mode 100644 adb_client/src/message_devices/tcp/mod.rs rename adb_client/src/{transports => message_devices/tcp}/tcp_transport.rs (97%) rename adb_client/src/{device => message_devices/usb}/adb_usb_device.rs (88%) create mode 100644 adb_client/src/message_devices/usb/mod.rs rename adb_client/src/{transports => message_devices/usb}/usb_transport.rs (97%) rename adb_client/src/{models => server}/adb_server_command.rs (98%) rename adb_client/src/{transports => server}/tcp_server_transport.rs (98%) delete mode 100644 adb_client/src/transports/mod.rs delete mode 100644 adb_client/src/transports/traits/mod.rs diff --git a/.gitignore b/.gitignore index a7d61ceb..a3ae0cfc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ target /.vscode venv /.mypy_cache -pyadb_client/pyadb_client.pyi \ No newline at end of file +pyadb_client/pyadb_client.pyi diff --git a/adb_cli/src/handlers/emulator_commands.rs b/adb_cli/src/handlers/emulator_commands.rs index ec271a9d..5b40970c 100644 --- a/adb_cli/src/handlers/emulator_commands.rs +++ b/adb_cli/src/handlers/emulator_commands.rs @@ -1,4 +1,4 @@ -use adb_client::ADBEmulatorDevice; +use adb_client::emulator::ADBEmulatorDevice; use crate::models::{ADBCliResult, EmuCommand, EmulatorCommand}; diff --git a/adb_cli/src/handlers/host_commands.rs b/adb_cli/src/handlers/host_commands.rs index 5a7c7ee1..ba6e61c3 100644 --- a/adb_cli/src/handlers/host_commands.rs +++ b/adb_cli/src/handlers/host_commands.rs @@ -1,4 +1,7 @@ -use adb_client::{ADBServer, DeviceShort, MDNSBackend, Result, WaitForDeviceState}; +use adb_client::{ + Result, + server::{ADBServer, DeviceShort, MDNSBackend, WaitForDeviceState}, +}; use crate::models::{HostCommand, MdnsCommand, ServerCommand}; diff --git a/adb_cli/src/handlers/local_commands.rs b/adb_cli/src/handlers/local_commands.rs index a894f68e..823cca03 100644 --- a/adb_cli/src/handlers/local_commands.rs +++ b/adb_cli/src/handlers/local_commands.rs @@ -1,8 +1,8 @@ use std::{fs::File, io::Write}; -use crate::ADBCliResult; -use crate::models::LocalDeviceCommand; -use adb_client::ADBServerDevice; +use adb_client::server_device::ADBServerDevice; + +use crate::models::{ADBCliResult, LocalDeviceCommand}; pub fn handle_local_commands( mut device: ADBServerDevice, diff --git a/adb_cli/src/main.rs b/adb_cli/src/main.rs index d27c8ddf..cd2ab1f7 100644 --- a/adb_cli/src/main.rs +++ b/adb_cli/src/main.rs @@ -7,10 +7,12 @@ mod handlers; mod models; mod utils; -use adb_client::{ - ADBDeviceExt, ADBListItemType, ADBServer, ADBServerDevice, ADBTcpDevice, ADBUSBDevice, - mdns::MDNSDiscoveryService, -}; +use adb_client::mdns::MDNSDiscoveryService; +use adb_client::server::ADBServer; +use adb_client::server_device::ADBServerDevice; +use adb_client::tcp::ADBTcpDevice; +use adb_client::usb::ADBUSBDevice; +use adb_client::{ADBDeviceExt, ADBListItemType}; #[cfg(any(target_os = "linux", target_os = "macos"))] use adb_termios::ADBTermios; diff --git a/adb_cli/src/models/host.rs b/adb_cli/src/models/host.rs index 67741d6a..705e5d97 100644 --- a/adb_cli/src/models/host.rs +++ b/adb_cli/src/models/host.rs @@ -1,6 +1,6 @@ use std::{net::SocketAddrV4, str::FromStr}; -use adb_client::{RustADBError, WaitForDeviceTransport}; +use adb_client::{RustADBError, server::WaitForDeviceTransport}; use clap::Parser; fn parse_wait_for_device_device_transport( diff --git a/adb_client/README.md b/adb_client/README.md index b459efcd..22ef171c 100644 --- a/adb_client/README.md +++ b/adb_client/README.md @@ -49,7 +49,7 @@ Benchmarks run on `v2.0.6`, on a **Samsung S10 SM-G973F** device and an **Intel ### Get available ADB devices ```rust no_run -use adb_client::ADBServer; +use adb_client::server::ADBServer; use std::net::{SocketAddrV4, Ipv4Addr}; // A custom server address can be provided @@ -65,7 +65,7 @@ server.devices(); #### Launch a command on device ```rust no_run -use adb_client::{ADBServer, ADBDeviceExt}; +use adb_client::{server::ADBServer, ADBDeviceExt}; let mut server = ADBServer::default(); let mut device = server.get_device().expect("cannot get device"); @@ -75,7 +75,7 @@ device.shell_command(&["df", "-h"], &mut std::io::stdout()); #### Push a file to the device ```rust no_run -use adb_client::ADBServer; +use adb_client::server::ADBServer; use std::net::Ipv4Addr; use std::fs::File; use std::path::Path; @@ -91,7 +91,7 @@ device.push(&mut input, "/data/local/tmp"); #### (USB) Launch a command on device ```rust no_run -use adb_client::{ADBUSBDevice, ADBDeviceExt}; +use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; let vendor_id = 0x04e8; let product_id = 0x6860; @@ -102,7 +102,7 @@ device.shell_command(&["df", "-h"], &mut std::io::stdout()); #### (USB) Push a file to the device ```rust no_run -use adb_client::{ADBUSBDevice, ADBDeviceExt}; +use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; use std::fs::File; use std::path::Path; @@ -117,7 +117,7 @@ device.push(&mut input, &"/data/local/tmp"); ```rust no_run use std::net::{SocketAddr, IpAddr, Ipv4Addr}; -use adb_client::{ADBTcpDevice, ADBDeviceExt}; +use adb_client::{tcp::ADBTcpDevice, ADBDeviceExt}; let device_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)); let device_port = 43210; diff --git a/adb_client/src/adb_device_ext.rs b/adb_client/src/adb_device_ext.rs index baac0def..c3d70548 100644 --- a/adb_client/src/adb_device_ext.rs +++ b/adb_client/src/adb_device_ext.rs @@ -3,10 +3,10 @@ use std::path::Path; use image::{ImageBuffer, ImageFormat, Rgba}; -use crate::models::AdbStatResponse; -use crate::{ADBListItem, RebootType, Result}; +use crate::models::{ADBListItem, AdbStatResponse}; +use crate::{RebootType, Result}; -/// Trait representing all features available on both [`crate::ADBServerDevice`] and [`crate::ADBUSBDevice`] +/// Trait representing all features available on both [`crate::server_device::ADBServerDevice`] and [`crate::usb::ADBUSBDevice`] pub trait ADBDeviceExt { /// Runs command in a shell on the device, and write its output and error streams into output. fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()>; diff --git a/adb_client/src/transports/traits/adb_transport.rs b/adb_client/src/adb_transport.rs similarity index 100% rename from adb_client/src/transports/traits/adb_transport.rs rename to adb_client/src/adb_transport.rs diff --git a/adb_client/src/constants.rs b/adb_client/src/constants.rs deleted file mode 100644 index c2eb05ee..00000000 --- a/adb_client/src/constants.rs +++ /dev/null @@ -1 +0,0 @@ -pub const BUFFER_SIZE: usize = 65536; diff --git a/adb_client/src/device/mod.rs b/adb_client/src/device/mod.rs deleted file mode 100644 index d9bbf067..00000000 --- a/adb_client/src/device/mod.rs +++ /dev/null @@ -1,28 +0,0 @@ -mod adb_message_device; -mod adb_message_device_commands; -mod adb_tcp_device; -mod adb_transport_message; - -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -mod adb_usb_device; -mod commands; -mod message_writer; -mod models; -mod shell_message_writer; - -use adb_message_device::ADBMessageDevice; -pub use adb_tcp_device::ADBTcpDevice; -pub use adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader}; - -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -pub use adb_usb_device::{ADBUSBDevice, is_adb_device, search_adb_devices}; - -pub use message_writer::MessageWriter; -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -pub use models::ADBRsaKey; - -pub use models::{MessageCommand, MessageSubcommand}; -pub use shell_message_writer::ShellMessageWriter; diff --git a/adb_client/src/device/models/mod.rs b/adb_client/src/device/models/mod.rs deleted file mode 100644 index d48d8437..00000000 --- a/adb_client/src/device/models/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -mod adb_rsa_key; -mod adb_session; -mod message_commands; - -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -pub use adb_rsa_key::ADBRsaKey; -pub(crate) use adb_session::ADBSession; -pub use message_commands::{MessageCommand, MessageSubcommand}; diff --git a/adb_client/src/emulator_device/adb_emulator_device.rs b/adb_client/src/emulator/adb_emulator_device.rs similarity index 94% rename from adb_client/src/emulator_device/adb_emulator_device.rs rename to adb_client/src/emulator/adb_emulator_device.rs index 60cfaa62..d92b5497 100644 --- a/adb_client/src/emulator_device/adb_emulator_device.rs +++ b/adb_client/src/emulator/adb_emulator_device.rs @@ -3,7 +3,10 @@ use std::{ sync::LazyLock, }; -use crate::{ADBServerDevice, ADBTransport, Result, RustADBError, TCPEmulatorTransport}; +use crate::{ + ADBTransport, Result, RustADBError, emulator::tcp_emulator_transport::TCPEmulatorTransport, + server_device::ADBServerDevice, +}; use regex::Regex; static EMULATOR_REGEX: LazyLock = LazyLock::new(|| { diff --git a/adb_client/src/emulator_device/commands/mod.rs b/adb_client/src/emulator/commands/mod.rs similarity index 100% rename from adb_client/src/emulator_device/commands/mod.rs rename to adb_client/src/emulator/commands/mod.rs diff --git a/adb_client/src/emulator_device/commands/rotate.rs b/adb_client/src/emulator/commands/rotate.rs similarity index 72% rename from adb_client/src/emulator_device/commands/rotate.rs rename to adb_client/src/emulator/commands/rotate.rs index eef31954..864b1982 100644 --- a/adb_client/src/emulator_device/commands/rotate.rs +++ b/adb_client/src/emulator/commands/rotate.rs @@ -1,4 +1,7 @@ -use crate::{ADBEmulatorDevice, Result, emulator_device::ADBEmulatorCommand}; +use crate::{ + Result, + emulator::{ADBEmulatorCommand, ADBEmulatorDevice}, +}; impl ADBEmulatorDevice { /// Send a SMS to this emulator with given content with given phone number diff --git a/adb_client/src/emulator_device/commands/sms.rs b/adb_client/src/emulator/commands/sms.rs similarity index 80% rename from adb_client/src/emulator_device/commands/sms.rs rename to adb_client/src/emulator/commands/sms.rs index b7caa326..f6a3ca7d 100644 --- a/adb_client/src/emulator_device/commands/sms.rs +++ b/adb_client/src/emulator/commands/sms.rs @@ -1,4 +1,7 @@ -use crate::{ADBEmulatorDevice, Result, emulator_device::ADBEmulatorCommand}; +use crate::{ + Result, + emulator::{ADBEmulatorCommand, ADBEmulatorDevice}, +}; impl ADBEmulatorDevice { /// Send a SMS to this emulator with given content with given phone number diff --git a/adb_client/src/emulator_device/mod.rs b/adb_client/src/emulator/mod.rs similarity index 61% rename from adb_client/src/emulator_device/mod.rs rename to adb_client/src/emulator/mod.rs index d3f04502..3f799e88 100644 --- a/adb_client/src/emulator_device/mod.rs +++ b/adb_client/src/emulator/mod.rs @@ -1,5 +1,7 @@ mod adb_emulator_device; mod commands; mod models; +mod tcp_emulator_transport; + pub use adb_emulator_device::ADBEmulatorDevice; -pub(crate) use models::ADBEmulatorCommand; +use models::ADBEmulatorCommand; diff --git a/adb_client/src/emulator_device/models/adb_emulator_command.rs b/adb_client/src/emulator/models/adb_emulator_command.rs similarity index 100% rename from adb_client/src/emulator_device/models/adb_emulator_command.rs rename to adb_client/src/emulator/models/adb_emulator_command.rs diff --git a/adb_client/src/emulator_device/models/mod.rs b/adb_client/src/emulator/models/mod.rs similarity index 100% rename from adb_client/src/emulator_device/models/mod.rs rename to adb_client/src/emulator/models/mod.rs diff --git a/adb_client/src/transports/tcp_emulator_transport.rs b/adb_client/src/emulator/tcp_emulator_transport.rs similarity index 97% rename from adb_client/src/transports/tcp_emulator_transport.rs rename to adb_client/src/emulator/tcp_emulator_transport.rs index ae6d390a..a508333a 100644 --- a/adb_client/src/transports/tcp_emulator_transport.rs +++ b/adb_client/src/emulator/tcp_emulator_transport.rs @@ -4,8 +4,9 @@ use std::{ net::{SocketAddrV4, TcpStream}, }; -use super::ADBTransport; -use crate::{Result, RustADBError, emulator_device::ADBEmulatorCommand}; +use crate::{ + Result, RustADBError, adb_transport::ADBTransport, emulator::models::ADBEmulatorCommand, +}; /// Emulator transport running on top on TCP. #[derive(Debug)] diff --git a/adb_client/src/lib.rs b/adb_client/src/lib.rs index e7665db5..04d7f930 100644 --- a/adb_client/src/lib.rs +++ b/adb_client/src/lib.rs @@ -10,32 +10,27 @@ #![cfg_attr(docsrs, feature(doc_cfg))] mod adb_device_ext; -mod constants; -mod device; -mod emulator_device; +mod adb_transport; +/// Emulator-related definitions +pub mod emulator; mod error; +mod message_devices; +mod models; + +/// Server-related definitions +pub mod server; + +/// Device reachable by the server related definitions +pub mod server_device; +mod utils; /// MDNS-related definitions #[cfg(feature = "mdns")] #[cfg_attr(docsrs, doc(cfg(feature = "mdns")))] pub mod mdns; -mod models; -mod server; -mod server_device; -mod transports; -mod utils; - pub use adb_device_ext::ADBDeviceExt; -pub use device::ADBTcpDevice; - -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -pub use device::{ADBUSBDevice, is_adb_device, search_adb_devices}; - -pub use emulator_device::ADBEmulatorDevice; +use adb_transport::ADBTransport; pub use error::{Result, RustADBError}; -pub use models::{ADBListItem, ADBListItemType, AdbStatResponse, RebootType}; -pub use server::*; -pub use server_device::ADBServerDevice; -pub use transports::*; +pub use message_devices::*; +pub use models::{ADBListItem, ADBListItemType, AdbStatResponse, HostFeatures, RebootType}; diff --git a/adb_client/src/mdns/mdns_discovery.rs b/adb_client/src/mdns/mdns_discovery.rs index bc8b5388..96bda94f 100644 --- a/adb_client/src/mdns/mdns_discovery.rs +++ b/adb_client/src/mdns/mdns_discovery.rs @@ -1,8 +1,7 @@ use mdns_sd::{ServiceDaemon, ServiceEvent}; use std::{sync::mpsc::Sender, thread::JoinHandle}; -use super::MDNSDevice; -use crate::{Result, RustADBError}; +use crate::{Result, RustADBError, mdns::MDNSDevice}; const ADB_SERVICE_NAME: &str = "_adb-tls-connect._tcp.local."; @@ -38,19 +37,17 @@ impl MDNSDiscoveryService { loop { while let Ok(event) = receiver.recv() { match event { - ServiceEvent::SearchStarted(_) - | ServiceEvent::ServiceRemoved(_, _) - | ServiceEvent::ServiceFound(_, _) - | ServiceEvent::SearchStopped(_) => { - // Ignoring these events. We are only interesting in found devices - } ServiceEvent::ServiceResolved(service_info) => { sender .send(MDNSDevice::from(service_info)) .map_err(|_| RustADBError::SendError)?; } - e => { - log::warn!("received unknown event type {e:?}"); + ServiceEvent::SearchStarted(_) + | ServiceEvent::ServiceRemoved(_, _) + | ServiceEvent::ServiceFound(_, _) + | ServiceEvent::SearchStopped(_) + | _ => { + // Ignoring these events. We are only interesting in found devices } } } diff --git a/adb_client/src/device/adb_message_device.rs b/adb_client/src/message_devices/adb_message_device.rs similarity index 95% rename from adb_client/src/device/adb_message_device.rs rename to adb_client/src/message_devices/adb_message_device.rs index 7c4e7f82..97878140 100644 --- a/adb_client/src/device/adb_message_device.rs +++ b/adb_client/src/message_devices/adb_message_device.rs @@ -1,14 +1,25 @@ -use super::{ADBRsaKey, ADBTransportMessage, MessageCommand, models::MessageSubcommand}; -use crate::device::adb_transport_message::{AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AUTH_TOKEN}; -use crate::device::models::ADBSession; -use crate::{ADBMessageTransport, AdbStatResponse, Result, RustADBError, constants::BUFFER_SIZE}; use bincode::config::{Configuration, Fixint, LittleEndian, NoLimit}; use byteorder::ReadBytesExt; use rand::Rng; -use serde::Serialize; -use serde::de::DeserializeOwned; -use std::io::{Cursor, Read, Seek}; -use std::time::Duration; +use serde::{Serialize, de::DeserializeOwned}; +use std::{ + io::{Cursor, Read, Seek}, + time::Duration, +}; + +use crate::{ + ADBSession, AdbStatResponse, Result, RustADBError, + message_devices::{ + adb_message_transport::ADBMessageTransport, + adb_transport_message::{ + ADBTransportMessage, AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AUTH_TOKEN, + }, + message_commands::{MessageCommand, MessageSubcommand}, + models::ADBRsaKey, + }, +}; + +const BUFFER_SIZE: usize = 65535; const BINCODE_CONFIG: Configuration = bincode::config::legacy(); diff --git a/adb_client/src/device/adb_message_device_commands.rs b/adb_client/src/message_devices/adb_message_device_commands.rs similarity index 80% rename from adb_client/src/device/adb_message_device_commands.rs rename to adb_client/src/message_devices/adb_message_device_commands.rs index b3c2ee7a..324a344e 100644 --- a/adb_client/src/device/adb_message_device_commands.rs +++ b/adb_client/src/message_devices/adb_message_device_commands.rs @@ -1,11 +1,15 @@ -use crate::{ADBDeviceExt, ADBMessageTransport, RebootType, Result, models::AdbStatResponse}; +use crate::{ + ADBDeviceExt, RebootType, Result, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + }, + models::{ADBListItem, AdbStatResponse}, +}; use std::{ io::{Read, Write}, path::Path, }; -use super::ADBMessageDevice; - impl ADBDeviceExt for ADBMessageDevice { fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { self.shell_command(command, output) @@ -43,7 +47,7 @@ impl ADBDeviceExt for ADBMessageDevice { self.framebuffer_inner() } - fn list(&mut self, path: &dyn AsRef) -> Result> { + fn list(&mut self, path: &dyn AsRef) -> Result> { self.list(path) } } diff --git a/adb_client/src/transports/traits/adb_message_transport.rs b/adb_client/src/message_devices/adb_message_transport.rs similarity index 90% rename from adb_client/src/transports/traits/adb_message_transport.rs rename to adb_client/src/message_devices/adb_message_transport.rs index 67eefd5e..d56b12ad 100644 --- a/adb_client/src/transports/traits/adb_message_transport.rs +++ b/adb_client/src/message_devices/adb_message_transport.rs @@ -1,7 +1,9 @@ use std::time::Duration; -use super::ADBTransport; -use crate::{Result, device::ADBTransportMessage}; +use crate::{ + Result, adb_transport::ADBTransport, + message_devices::adb_transport_message::ADBTransportMessage, +}; const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(u64::MAX); const DEFAULT_WRITE_TIMEOUT: Duration = Duration::from_secs(2); diff --git a/adb_client/src/device/models/adb_session.rs b/adb_client/src/message_devices/adb_session.rs similarity index 100% rename from adb_client/src/device/models/adb_session.rs rename to adb_client/src/message_devices/adb_session.rs diff --git a/adb_client/src/device/adb_transport_message.rs b/adb_client/src/message_devices/adb_transport_message.rs similarity index 93% rename from adb_client/src/device/adb_transport_message.rs rename to adb_client/src/message_devices/adb_transport_message.rs index 7cb34135..2982d93a 100644 --- a/adb_client/src/device/adb_transport_message.rs +++ b/adb_client/src/message_devices/adb_transport_message.rs @@ -1,8 +1,12 @@ use serde::{Deserialize, Serialize}; -use crate::{Result, RustADBError, device::adb_message_device}; - -use super::models::MessageCommand; +use crate::{ + Result, RustADBError, + message_devices::{ + adb_message_device::{self, bincode_serialize_to_vec}, + message_commands::MessageCommand, + }, +}; pub const AUTH_TOKEN: u32 = 1; pub const AUTH_SIGNATURE: u32 = 2; @@ -67,7 +71,7 @@ impl ADBTransportMessageHeader { } pub fn as_bytes(&self) -> Result> { - adb_message_device::bincode_serialize_to_vec(self) + bincode_serialize_to_vec(self) } } diff --git a/adb_client/src/device/commands/framebuffer.rs b/adb_client/src/message_devices/commands/framebuffer.rs similarity index 94% rename from adb_client/src/device/commands/framebuffer.rs rename to adb_client/src/message_devices/commands/framebuffer.rs index 669383d6..34afde4d 100644 --- a/adb_client/src/device/commands/framebuffer.rs +++ b/adb_client/src/message_devices/commands/framebuffer.rs @@ -4,8 +4,11 @@ use byteorder::{LittleEndian, ReadBytesExt}; use image::{ImageBuffer, Rgba}; use crate::{ - ADBMessageTransport, Result, RustADBError, - device::{MessageCommand, adb_message_device::ADBMessageDevice}, + Result, RustADBError, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + message_commands::MessageCommand, + }, models::{FrameBufferInfoV1, FrameBufferInfoV2}, }; diff --git a/adb_client/src/device/commands/install.rs b/adb_client/src/message_devices/commands/install.rs similarity index 87% rename from adb_client/src/device/commands/install.rs rename to adb_client/src/message_devices/commands/install.rs index 9c185ab9..b5fe5333 100644 --- a/adb_client/src/device/commands/install.rs +++ b/adb_client/src/message_devices/commands/install.rs @@ -1,8 +1,11 @@ use std::{fs::File, path::Path}; use crate::{ - ADBMessageTransport, Result, - device::{MessageWriter, adb_message_device::ADBMessageDevice}, + Result, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + commands::utils::MessageWriter, + }, utils::check_extension_is_apk, }; diff --git a/adb_client/src/device/commands/list.rs b/adb_client/src/message_devices/commands/list.rs similarity index 93% rename from adb_client/src/device/commands/list.rs rename to adb_client/src/message_devices/commands/list.rs index f1834287..c0a99bef 100644 --- a/adb_client/src/device/commands/list.rs +++ b/adb_client/src/message_devices/commands/list.rs @@ -1,14 +1,18 @@ -use crate::{ - ADBListItem, ADBListItemType, ADBMessageTransport, Result, RustADBError, - device::{ - ADBTransportMessage, MessageCommand, MessageSubcommand, - adb_message_device::{ADBMessageDevice, bincode_serialize_to_vec}, - }, -}; use byteorder::ByteOrder; use byteorder::LittleEndian; use std::str; +use crate::Result; +use crate::RustADBError; +use crate::message_devices::adb_message_device::ADBMessageDevice; +use crate::message_devices::adb_message_device::bincode_serialize_to_vec; +use crate::message_devices::adb_message_transport::ADBMessageTransport; +use crate::message_devices::adb_transport_message::ADBTransportMessage; +use crate::message_devices::message_commands::MessageCommand; +use crate::message_devices::message_commands::MessageSubcommand; +use crate::models::ADBListItem; +use crate::models::ADBListItemType; + impl ADBMessageDevice { /// List the entries in the given directory on the device. /// note: path uses internal file paths, so Documents is at /storage/emulated/0/Documents @@ -84,8 +88,7 @@ impl ADBMessageDevice { let subcommand_data = MessageSubcommand::List; - let mut serialized_message = bincode_serialize_to_vec(subcommand_data) - .map_err(|_e| RustADBError::ConversionError)?; + let mut serialized_message = bincode_serialize_to_vec(subcommand_data)?; serialized_message.append(&mut len_buf); let mut path_bytes: Vec = Vec::from(path.as_ref().as_bytes()); diff --git a/adb_client/src/device/commands/mod.rs b/adb_client/src/message_devices/commands/mod.rs similarity index 90% rename from adb_client/src/device/commands/mod.rs rename to adb_client/src/message_devices/commands/mod.rs index 2bae4615..24de463e 100644 --- a/adb_client/src/device/commands/mod.rs +++ b/adb_client/src/message_devices/commands/mod.rs @@ -7,3 +7,4 @@ mod reboot; mod shell; mod stat; mod uninstall; +mod utils; diff --git a/adb_client/src/device/commands/pull.rs b/adb_client/src/message_devices/commands/pull.rs similarity index 80% rename from adb_client/src/device/commands/pull.rs rename to adb_client/src/message_devices/commands/pull.rs index b9101449..1c3158c2 100644 --- a/adb_client/src/device/commands/pull.rs +++ b/adb_client/src/message_devices/commands/pull.rs @@ -1,11 +1,12 @@ use std::io::Write; use crate::{ - ADBMessageTransport, Result, RustADBError, - device::{ - ADBTransportMessage, MessageCommand, - adb_message_device::{self, ADBMessageDevice}, - models::MessageSubcommand, + Result, RustADBError, + message_devices::{ + adb_message_device::{ADBMessageDevice, bincode_serialize_to_vec}, + adb_message_transport::ADBMessageTransport, + adb_transport_message::ADBTransportMessage, + message_commands::{MessageCommand, MessageSubcommand}, }, }; @@ -33,7 +34,7 @@ impl ADBMessageDevice { )?; let recv_buffer = MessageSubcommand::Recv.with_arg(u32::try_from(source.len())?); - let recv_buffer = adb_message_device::bincode_serialize_to_vec(&recv_buffer)?; + let recv_buffer = bincode_serialize_to_vec(&recv_buffer)?; self.send_and_expect_okay(ADBTransportMessage::try_new( MessageCommand::Write, session.local_id(), diff --git a/adb_client/src/device/commands/push.rs b/adb_client/src/message_devices/commands/push.rs similarity index 69% rename from adb_client/src/device/commands/push.rs rename to adb_client/src/message_devices/commands/push.rs index b59dd3f6..ec2c8aec 100644 --- a/adb_client/src/device/commands/push.rs +++ b/adb_client/src/message_devices/commands/push.rs @@ -1,10 +1,12 @@ use std::io::Read; use crate::{ - ADBMessageTransport, Result, - device::{ - ADBTransportMessage, MessageCommand, MessageSubcommand, - adb_message_device::{self, ADBMessageDevice}, + Result, + message_devices::{ + adb_message_device::{ADBMessageDevice, bincode_serialize_to_vec}, + adb_message_transport::ADBMessageTransport, + adb_transport_message::ADBTransportMessage, + message_commands::{MessageCommand, MessageSubcommand}, }, }; @@ -15,7 +17,7 @@ impl ADBMessageDevice { let path_header = format!("{},0777", path.as_ref()); let send_buffer = MessageSubcommand::Send.with_arg(u32::try_from(path_header.len())?); - let mut send_buffer = adb_message_device::bincode_serialize_to_vec(&send_buffer)?; + let mut send_buffer = bincode_serialize_to_vec(&send_buffer)?; send_buffer.append(&mut path_header.as_bytes().to_vec()); self.send_and_expect_okay(ADBTransportMessage::try_new( diff --git a/adb_client/src/device/commands/reboot.rs b/adb_client/src/message_devices/commands/reboot.rs similarity index 66% rename from adb_client/src/device/commands/reboot.rs rename to adb_client/src/message_devices/commands/reboot.rs index 58438f2f..ce461174 100644 --- a/adb_client/src/device/commands/reboot.rs +++ b/adb_client/src/message_devices/commands/reboot.rs @@ -1,6 +1,9 @@ use crate::{ - ADBMessageTransport, RebootType, Result, - device::{MessageCommand, adb_message_device::ADBMessageDevice}, + RebootType, Result, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + message_commands::MessageCommand, + }, }; impl ADBMessageDevice { diff --git a/adb_client/src/device/commands/shell.rs b/adb_client/src/message_devices/commands/shell.rs similarity index 91% rename from adb_client/src/device/commands/shell.rs rename to adb_client/src/message_devices/commands/shell.rs index 30e55441..d62803ca 100644 --- a/adb_client/src/device/commands/shell.rs +++ b/adb_client/src/message_devices/commands/shell.rs @@ -1,11 +1,13 @@ use std::io::{ErrorKind, Read, Write}; use std::time::Duration; -use crate::Result; -use crate::device::ShellMessageWriter; use crate::{ - ADBMessageTransport, RustADBError, - device::{ADBMessageDevice, ADBTransportMessage, MessageCommand}, + Result, RustADBError, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + adb_transport_message::ADBTransportMessage, commands::utils::ShellMessageWriter, + message_commands::MessageCommand, + }, }; impl ADBMessageDevice { diff --git a/adb_client/src/device/commands/stat.rs b/adb_client/src/message_devices/commands/stat.rs similarity index 71% rename from adb_client/src/device/commands/stat.rs rename to adb_client/src/message_devices/commands/stat.rs index bc513037..792dc5f5 100644 --- a/adb_client/src/device/commands/stat.rs +++ b/adb_client/src/message_devices/commands/stat.rs @@ -1,5 +1,8 @@ use crate::{ - ADBMessageTransport, AdbStatResponse, Result, device::adb_message_device::ADBMessageDevice, + AdbStatResponse, Result, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + }, }; impl ADBMessageDevice { diff --git a/adb_client/src/device/commands/uninstall.rs b/adb_client/src/message_devices/commands/uninstall.rs similarity index 81% rename from adb_client/src/device/commands/uninstall.rs rename to adb_client/src/message_devices/commands/uninstall.rs index d2304466..1af47fd8 100644 --- a/adb_client/src/device/commands/uninstall.rs +++ b/adb_client/src/message_devices/commands/uninstall.rs @@ -1,4 +1,9 @@ -use crate::{ADBMessageTransport, Result, device::adb_message_device::ADBMessageDevice}; +use crate::{ + Result, + message_devices::{ + adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, + }, +}; impl ADBMessageDevice { pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> { diff --git a/adb_client/src/device/message_writer.rs b/adb_client/src/message_devices/commands/utils/message_writer.rs similarity index 89% rename from adb_client/src/device/message_writer.rs rename to adb_client/src/message_devices/commands/utils/message_writer.rs index 2b9b6279..bcf1d439 100644 --- a/adb_client/src/device/message_writer.rs +++ b/adb_client/src/message_devices/commands/utils/message_writer.rs @@ -1,8 +1,9 @@ use std::io::{Error, ErrorKind, Result, Write}; -use crate::ADBMessageTransport; - -use super::{ADBTransportMessage, MessageCommand}; +use crate::message_devices::{ + adb_message_transport::ADBMessageTransport, adb_transport_message::ADBTransportMessage, + message_commands::MessageCommand, +}; /// [`Write`] trait implementation to hide underlying ADB protocol write logic. /// diff --git a/adb_client/src/message_devices/commands/utils/mod.rs b/adb_client/src/message_devices/commands/utils/mod.rs new file mode 100644 index 00000000..9a7fd27d --- /dev/null +++ b/adb_client/src/message_devices/commands/utils/mod.rs @@ -0,0 +1,4 @@ +mod message_writer; +mod shell_message_writer; +pub use message_writer::MessageWriter; +pub use shell_message_writer::ShellMessageWriter; diff --git a/adb_client/src/device/shell_message_writer.rs b/adb_client/src/message_devices/commands/utils/shell_message_writer.rs similarity index 86% rename from adb_client/src/device/shell_message_writer.rs rename to adb_client/src/message_devices/commands/utils/shell_message_writer.rs index 5969aaac..c99ce068 100644 --- a/adb_client/src/device/shell_message_writer.rs +++ b/adb_client/src/message_devices/commands/utils/shell_message_writer.rs @@ -1,8 +1,9 @@ use std::io::Write; -use crate::ADBMessageTransport; - -use super::{ADBTransportMessage, models::MessageCommand}; +use crate::message_devices::{ + adb_message_transport::ADBMessageTransport, adb_transport_message::ADBTransportMessage, + message_commands::MessageCommand, +}; /// [`Write`] trait implementation to hide underlying ADB protocol write logic for shell commands. pub struct ShellMessageWriter { diff --git a/adb_client/src/device/models/message_commands.rs b/adb_client/src/message_devices/message_commands.rs similarity index 100% rename from adb_client/src/device/models/message_commands.rs rename to adb_client/src/message_devices/message_commands.rs diff --git a/adb_client/src/message_devices/mod.rs b/adb_client/src/message_devices/mod.rs new file mode 100644 index 00000000..0f2d59d6 --- /dev/null +++ b/adb_client/src/message_devices/mod.rs @@ -0,0 +1,18 @@ +/// USB-related definitions +#[cfg(feature = "usb")] +#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] +pub mod usb; + +/// Device reachable over TCP related definition +pub mod tcp; + +mod adb_message_device; +mod adb_message_device_commands; +mod adb_message_transport; +mod adb_session; +mod adb_transport_message; +mod commands; +mod message_commands; +mod models; + +pub(crate) use adb_session::ADBSession; diff --git a/adb_client/src/device/models/adb_rsa_key.rs b/adb_client/src/message_devices/models/adb_rsa_key.rs similarity index 90% rename from adb_client/src/device/models/adb_rsa_key.rs rename to adb_client/src/message_devices/models/adb_rsa_key.rs index 6647ffe3..744a9837 100644 --- a/adb_client/src/device/models/adb_rsa_key.rs +++ b/adb_client/src/message_devices/models/adb_rsa_key.rs @@ -7,6 +7,8 @@ use rsa::pkcs8::DecodePrivateKey; use rsa::traits::PublicKeyParts; use rsa::{Pkcs1v15Sign, RsaPrivateKey}; use std::fmt::Write; +use std::fs::read_to_string; +use std::path::Path; const ADB_PRIVATE_KEY_SIZE: usize = 2048; const ANDROID_PUBKEY_MODULUS_SIZE_WORDS: u32 = 64; @@ -112,6 +114,25 @@ impl ADBRsaKey { } } +pub(crate) fn read_adb_private_key>( + private_key_path: P, +) -> Result> { + // 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), + } +} + fn set_bit(n: usize) -> Result { BigUint::parse_bytes( &{ diff --git a/adb_client/src/message_devices/models/mod.rs b/adb_client/src/message_devices/models/mod.rs new file mode 100644 index 00000000..dff04610 --- /dev/null +++ b/adb_client/src/message_devices/models/mod.rs @@ -0,0 +1,4 @@ +mod adb_rsa_key; + +pub use adb_rsa_key::ADBRsaKey; +pub(crate) use adb_rsa_key::read_adb_private_key; diff --git a/adb_client/src/device/adb_tcp_device.rs b/adb_client/src/message_devices/tcp/adb_tcp_device.rs similarity index 88% rename from adb_client/src/device/adb_tcp_device.rs rename to adb_client/src/message_devices/tcp/adb_tcp_device.rs index a26c70bd..c60dc1cf 100644 --- a/adb_client/src/device/adb_tcp_device.rs +++ b/adb_client/src/message_devices/tcp/adb_tcp_device.rs @@ -2,12 +2,15 @@ use std::io::Write; use std::path::Path; use std::{io::Read, net::SocketAddr}; -use super::adb_message_device::ADBMessageDevice; -use super::models::MessageCommand; -use super::{ADBRsaKey, ADBTransportMessage}; -use crate::device::adb_usb_device::read_adb_private_key; +use crate::message_devices::adb_message_device::ADBMessageDevice; +use crate::message_devices::adb_message_transport::ADBMessageTransport; +use crate::message_devices::adb_transport_message::ADBTransportMessage; +use crate::message_devices::message_commands::MessageCommand; +use crate::message_devices::models::{ADBRsaKey, read_adb_private_key}; +use crate::models::ADBListItem; +use crate::tcp::tcp_transport::TcpTransport; use crate::utils::get_default_adb_key_path; -use crate::{ADBDeviceExt, ADBMessageTransport, ADBTransport, Result, TcpTransport}; +use crate::{ADBDeviceExt, ADBTransport, Result}; /// Represent a device reached and available over TCP. #[derive(Debug)] @@ -143,7 +146,8 @@ impl ADBDeviceExt for ADBTcpDevice { self.inner.framebuffer_inner() } - fn list(&mut self, path: &dyn AsRef) -> Result> { + #[inline] + fn list(&mut self, path: &dyn AsRef) -> Result> { self.inner.list(path) } } diff --git a/adb_client/src/message_devices/tcp/mod.rs b/adb_client/src/message_devices/tcp/mod.rs new file mode 100644 index 00000000..cd9a81dc --- /dev/null +++ b/adb_client/src/message_devices/tcp/mod.rs @@ -0,0 +1,4 @@ +mod adb_tcp_device; +mod tcp_transport; + +pub use adb_tcp_device::ADBTcpDevice; diff --git a/adb_client/src/transports/tcp_transport.rs b/adb_client/src/message_devices/tcp/tcp_transport.rs similarity index 97% rename from adb_client/src/transports/tcp_transport.rs rename to adb_client/src/message_devices/tcp/tcp_transport.rs index cb572a58..ef628617 100644 --- a/adb_client/src/transports/tcp_transport.rs +++ b/adb_client/src/message_devices/tcp/tcp_transport.rs @@ -5,10 +5,14 @@ use rustls::{ pki_types::{CertificateDer, PrivatePkcs8KeyDer, pem::PemObject}, }; -use super::{ADBMessageTransport, ADBTransport}; use crate::{ Result, RustADBError, - device::{ADBTransportMessage, ADBTransportMessageHeader, MessageCommand}, + adb_transport::ADBTransport, + message_devices::{ + adb_message_transport::ADBMessageTransport, + adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader}, + message_commands::MessageCommand, + }, utils::get_default_adb_key_path, }; use std::{ @@ -206,7 +210,7 @@ impl ADBMessageTransport for TcpTransport { fn read_message_with_timeout( &mut self, read_timeout: std::time::Duration, - ) -> Result { + ) -> Result { let raw_connection_lock = self.get_current_connection()?; let mut raw_connection = raw_connection_lock.lock()?; diff --git a/adb_client/src/device/adb_usb_device.rs b/adb_client/src/message_devices/usb/adb_usb_device.rs similarity index 88% rename from adb_client/src/device/adb_usb_device.rs rename to adb_client/src/message_devices/usb/adb_usb_device.rs index 241de0b8..86fa9e16 100644 --- a/adb_client/src/device/adb_usb_device.rs +++ b/adb_client/src/message_devices/usb/adb_usb_device.rs @@ -2,37 +2,24 @@ use rusb::Device; use rusb::DeviceDescriptor; use rusb::UsbContext; use rusb::constants::LIBUSB_CLASS_VENDOR_SPEC; -use std::fs::read_to_string; use std::io::Read; use std::io::Write; use std::path::Path; use std::path::PathBuf; -use super::adb_message_device::ADBMessageDevice; -use super::models::MessageCommand; -use super::{ADBRsaKey, ADBTransportMessage}; use crate::ADBDeviceExt; -use crate::ADBMessageTransport; -use crate::ADBTransport; +use crate::Result; +use crate::RustADBError; +use crate::adb_transport::ADBTransport; +use crate::message_devices::adb_message_device::ADBMessageDevice; +use crate::message_devices::adb_message_transport::ADBMessageTransport; +use crate::message_devices::adb_transport_message::ADBTransportMessage; +use crate::message_devices::message_commands::MessageCommand; +use crate::message_devices::models::ADBRsaKey; +use crate::message_devices::models::read_adb_private_key; +use crate::models::ADBListItem; +use crate::usb::usb_transport::USBTransport; use crate::utils::get_default_adb_key_path; -use crate::{Result, RustADBError, USBTransport}; - -pub fn read_adb_private_key>(private_key_path: P) -> Result> { - // 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 pub fn search_adb_devices() -> Result> { @@ -243,7 +230,8 @@ impl ADBDeviceExt for ADBUSBDevice { self.inner.framebuffer_inner() } - fn list(&mut self, path: &dyn AsRef) -> Result> { + #[inline] + fn list(&mut self, path: &dyn AsRef) -> Result> { self.inner.list(path) } } diff --git a/adb_client/src/message_devices/usb/mod.rs b/adb_client/src/message_devices/usb/mod.rs new file mode 100644 index 00000000..f686cb67 --- /dev/null +++ b/adb_client/src/message_devices/usb/mod.rs @@ -0,0 +1,5 @@ +mod adb_usb_device; +mod usb_transport; + +pub use adb_usb_device::ADBUSBDevice; +pub use usb_transport::USBTransport; diff --git a/adb_client/src/transports/usb_transport.rs b/adb_client/src/message_devices/usb/usb_transport.rs similarity index 97% rename from adb_client/src/transports/usb_transport.rs rename to adb_client/src/message_devices/usb/usb_transport.rs index 033e6dcc..099c8457 100644 --- a/adb_client/src/transports/usb_transport.rs +++ b/adb_client/src/message_devices/usb/usb_transport.rs @@ -5,10 +5,14 @@ use rusb::{ constants::LIBUSB_CLASS_VENDOR_SPEC, }; -use super::{ADBMessageTransport, ADBTransport}; use crate::{ Result, RustADBError, - device::{ADBTransportMessage, ADBTransportMessageHeader, MessageCommand}, + adb_transport::ADBTransport, + message_devices::{ + adb_message_transport::ADBMessageTransport, + adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader}, + message_commands::MessageCommand, + }, }; #[derive(Clone, Debug)] diff --git a/adb_client/src/models/host_features.rs b/adb_client/src/models/host_features.rs index 85fc22d7..25db03ad 100644 --- a/adb_client/src/models/host_features.rs +++ b/adb_client/src/models/host_features.rs @@ -1,8 +1,11 @@ use std::fmt::Display; +/// Available host features. #[derive(Debug, PartialEq)] pub enum HostFeatures { + /// Shell version 2. ShellV2, + /// Command. Cmd, } diff --git a/adb_client/src/models/mod.rs b/adb_client/src/models/mod.rs index 921c67d5..a4fbeec3 100644 --- a/adb_client/src/models/mod.rs +++ b/adb_client/src/models/mod.rs @@ -1,5 +1,4 @@ mod adb_request_status; -mod adb_server_command; mod adb_stat_response; mod framebuffer_info; mod host_features; @@ -7,11 +6,10 @@ mod list_info; mod reboot_type; mod sync_command; -pub use adb_request_status::AdbRequestStatus; -pub(crate) use adb_server_command::AdbServerCommand; +pub(crate) use adb_request_status::AdbRequestStatus; pub use adb_stat_response::AdbStatResponse; pub(crate) use framebuffer_info::{FrameBufferInfoV1, FrameBufferInfoV2}; pub use host_features::HostFeatures; pub use list_info::{ADBListItem, ADBListItemType}; pub use reboot_type::RebootType; -pub use sync_command::SyncCommand; +pub(crate) use sync_command::SyncCommand; diff --git a/adb_client/src/server/adb_server.rs b/adb_client/src/server/adb_server.rs index a21a25a8..fde3c5d7 100644 --- a/adb_client/src/server/adb_server.rs +++ b/adb_client/src/server/adb_server.rs @@ -1,7 +1,7 @@ use crate::ADBTransport; use crate::Result; use crate::RustADBError; -use crate::TCPServerTransport; +use crate::server::tcp_server_transport::TCPServerTransport; use std::collections::HashMap; use std::net::SocketAddrV4; use std::process::Command; diff --git a/adb_client/src/models/adb_server_command.rs b/adb_client/src/server/adb_server_command.rs similarity index 98% rename from adb_client/src/models/adb_server_command.rs rename to adb_client/src/server/adb_server_command.rs index fe4a0b4d..3779d584 100644 --- a/adb_client/src/models/adb_server_command.rs +++ b/adb_client/src/server/adb_server_command.rs @@ -1,8 +1,10 @@ use std::fmt::Display; -use crate::{WaitForDeviceState, WaitForDeviceTransport}; +use crate::{ + RebootType, + server::{WaitForDeviceState, WaitForDeviceTransport}, +}; -use super::RebootType; use std::net::SocketAddrV4; pub(crate) enum AdbServerCommand { diff --git a/adb_client/src/server/commands/connect.rs b/adb_client/src/server/commands/connect.rs index 0a49a1bb..adf56e1b 100644 --- a/adb_client/src/server/commands/connect.rs +++ b/adb_client/src/server/commands/connect.rs @@ -1,4 +1,7 @@ -use crate::{ADBServer, Result, RustADBError, models::AdbServerCommand}; +use crate::{ + Result, RustADBError, + server::{ADBServer, AdbServerCommand}, +}; use std::net::SocketAddrV4; impl ADBServer { diff --git a/adb_client/src/server/commands/devices.rs b/adb_client/src/server/commands/devices.rs index 8793463e..ad3a449e 100644 --- a/adb_client/src/server/commands/devices.rs +++ b/adb_client/src/server/commands/devices.rs @@ -1,8 +1,10 @@ use std::io::Read; use crate::{ - ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong, DeviceShort, Result, RustADBError, - models::AdbServerCommand, + Result, RustADBError, + emulator::ADBEmulatorDevice, + server::{ADBServer, AdbServerCommand, DeviceLong, DeviceShort}, + server_device::ADBServerDevice, }; impl ADBServer { diff --git a/adb_client/src/server/commands/disconnect.rs b/adb_client/src/server/commands/disconnect.rs index 5819340a..2684c67c 100644 --- a/adb_client/src/server/commands/disconnect.rs +++ b/adb_client/src/server/commands/disconnect.rs @@ -1,4 +1,7 @@ -use crate::{ADBServer, Result, RustADBError, models::AdbServerCommand}; +use crate::{ + Result, RustADBError, + server::{ADBServer, AdbServerCommand}, +}; use std::net::SocketAddrV4; impl ADBServer { diff --git a/adb_client/src/server/commands/kill.rs b/adb_client/src/server/commands/kill.rs index 38051131..e7c369f1 100644 --- a/adb_client/src/server/commands/kill.rs +++ b/adb_client/src/server/commands/kill.rs @@ -1,4 +1,7 @@ -use crate::{ADBServer, Result, models::AdbServerCommand}; +use crate::{ + Result, + server::{ADBServer, AdbServerCommand}, +}; impl ADBServer { /// Asks the ADB server to quit immediately. diff --git a/adb_client/src/server/commands/mdns.rs b/adb_client/src/server/commands/mdns.rs index 955f097a..8b0f44ed 100644 --- a/adb_client/src/server/commands/mdns.rs +++ b/adb_client/src/server/commands/mdns.rs @@ -1,7 +1,8 @@ use std::io::BufRead; use crate::{ - ADBServer, MDNSServices, Result, models::AdbServerCommand, server::models::MDNSBackend, + Result, + server::{ADBServer, AdbServerCommand, MDNSServices, models::MDNSBackend}, }; const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN"; diff --git a/adb_client/src/server/commands/pair.rs b/adb_client/src/server/commands/pair.rs index 01db8c0f..ee0bcd3f 100644 --- a/adb_client/src/server/commands/pair.rs +++ b/adb_client/src/server/commands/pair.rs @@ -1,5 +1,7 @@ -use crate::models::AdbServerCommand; -use crate::{ADBServer, Result, RustADBError}; +use crate::{ + Result, RustADBError, + server::{ADBServer, AdbServerCommand}, +}; use std::net::SocketAddrV4; impl ADBServer { diff --git a/adb_client/src/server/commands/reconnect.rs b/adb_client/src/server/commands/reconnect.rs index 1a2a08c0..2d051865 100644 --- a/adb_client/src/server/commands/reconnect.rs +++ b/adb_client/src/server/commands/reconnect.rs @@ -1,4 +1,7 @@ -use crate::{ADBServer, Result, models::AdbServerCommand}; +use crate::{ + Result, + server::{ADBServer, AdbServerCommand}, +}; impl ADBServer { /// Reconnect the device diff --git a/adb_client/src/server/commands/server_status.rs b/adb_client/src/server/commands/server_status.rs index 0d88fb4a..8400584b 100644 --- a/adb_client/src/server/commands/server_status.rs +++ b/adb_client/src/server/commands/server_status.rs @@ -1,4 +1,7 @@ -use crate::{ADBServer, Result, models::AdbServerCommand, server::models::ServerStatus}; +use crate::{ + Result, + server::{ADBServer, AdbServerCommand, models::ServerStatus}, +}; impl ADBServer { /// Check ADB server status diff --git a/adb_client/src/server/commands/version.rs b/adb_client/src/server/commands/version.rs index e49467b6..66611298 100644 --- a/adb_client/src/server/commands/version.rs +++ b/adb_client/src/server/commands/version.rs @@ -1,4 +1,7 @@ -use crate::{ADBServer, AdbVersion, Result, models::AdbServerCommand}; +use crate::{ + Result, + server::{ADBServer, AdbServerCommand, AdbVersion}, +}; impl ADBServer { /// Gets server's internal version number. diff --git a/adb_client/src/server/commands/wait_for_device.rs b/adb_client/src/server/commands/wait_for_device.rs index 25cfe14b..6f1cfa20 100644 --- a/adb_client/src/server/commands/wait_for_device.rs +++ b/adb_client/src/server/commands/wait_for_device.rs @@ -1,5 +1,6 @@ use crate::{ - ADBServer, Result, WaitForDeviceState, WaitForDeviceTransport, models::AdbServerCommand, + Result, + server::{ADBServer, AdbServerCommand, WaitForDeviceState, WaitForDeviceTransport}, }; impl ADBServer { diff --git a/adb_client/src/server/mod.rs b/adb_client/src/server/mod.rs index d55f1310..30d73410 100644 --- a/adb_client/src/server/mod.rs +++ b/adb_client/src/server/mod.rs @@ -1,6 +1,10 @@ mod adb_server; +mod adb_server_command; mod commands; mod models; +mod tcp_server_transport; pub use adb_server::ADBServer; +pub(crate) use adb_server_command::AdbServerCommand; pub use models::*; +pub use tcp_server_transport::TCPServerTransport; diff --git a/adb_client/src/server/models/device_long.rs b/adb_client/src/server/models/device_long.rs index 8f697baa..c0f13ad5 100644 --- a/adb_client/src/server/models/device_long.rs +++ b/adb_client/src/server/models/device_long.rs @@ -2,7 +2,8 @@ use std::str::FromStr; use std::sync::LazyLock; use std::{fmt::Display, str}; -use crate::{DeviceState, RustADBError}; +use crate::RustADBError; +use crate::server::DeviceState; use regex::bytes::Regex; static DEVICES_LONG_REGEX: LazyLock = LazyLock::new(|| { diff --git a/adb_client/src/server/models/device_short.rs b/adb_client/src/server/models/device_short.rs index 493cd6ed..1759df00 100644 --- a/adb_client/src/server/models/device_short.rs +++ b/adb_client/src/server/models/device_short.rs @@ -1,7 +1,7 @@ use regex::bytes::Regex; use std::{fmt::Display, str::FromStr, sync::LazyLock}; -use crate::{DeviceState, RustADBError}; +use crate::{RustADBError, server::DeviceState}; static DEVICES_REGEX: LazyLock = LazyLock::new(|| Regex::new("^(\\S+)\t(\\w+)\n?$").expect("Cannot build devices regex")); diff --git a/adb_client/src/transports/tcp_server_transport.rs b/adb_client/src/server/tcp_server_transport.rs similarity index 98% rename from adb_client/src/transports/tcp_server_transport.rs rename to adb_client/src/server/tcp_server_transport.rs index 9c489ab0..adb190ab 100644 --- a/adb_client/src/transports/tcp_server_transport.rs +++ b/adb_client/src/server/tcp_server_transport.rs @@ -4,8 +4,9 @@ use std::str::FromStr; use byteorder::{ByteOrder, LittleEndian}; +use crate::ADBTransport; use crate::models::{AdbRequestStatus, SyncCommand}; -use crate::{ADBTransport, models::AdbServerCommand}; +use crate::server::AdbServerCommand; use crate::{Result, RustADBError}; const DEFAULT_SERVER_IP: Ipv4Addr = Ipv4Addr::LOCALHOST; diff --git a/adb_client/src/server_device/adb_server_device.rs b/adb_client/src/server_device/adb_server_device.rs index 73a1e0e5..7678621c 100644 --- a/adb_client/src/server_device/adb_server_device.rs +++ b/adb_client/src/server_device/adb_server_device.rs @@ -1,4 +1,7 @@ -use crate::{ADBTransport, Result, TCPServerTransport, models::AdbServerCommand}; +use crate::{ + ADBTransport, Result, + server::{AdbServerCommand, TCPServerTransport}, +}; use std::net::SocketAddrV4; /// Represents a device connected to the ADB server. diff --git a/adb_client/src/server_device/adb_server_device_commands.rs b/adb_client/src/server_device/adb_server_device_commands.rs index bef9d772..f194cfaf 100644 --- a/adb_client/src/server_device/adb_server_device_commands.rs +++ b/adb_client/src/server_device/adb_server_device_commands.rs @@ -5,12 +5,14 @@ use std::{ use crate::{ ADBDeviceExt, Result, RustADBError, - constants::BUFFER_SIZE, - models::{AdbServerCommand, AdbStatResponse, HostFeatures}, + models::{ADBListItem, AdbStatResponse, HostFeatures}, + server::AdbServerCommand, }; use super::ADBServerDevice; +const BUFFER_SIZE: usize = 65535; + impl ADBDeviceExt for ADBServerDevice { fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { let supported_features = self.host_features()?; @@ -136,7 +138,7 @@ impl ADBDeviceExt for ADBServerDevice { self.framebuffer_inner() } - fn list(&mut self, path: &dyn AsRef) -> Result> { + fn list(&mut self, path: &dyn AsRef) -> Result> { self.list(path) } } diff --git a/adb_client/src/server_device/commands/forward.rs b/adb_client/src/server_device/commands/forward.rs index 15505d6b..18a2dc31 100644 --- a/adb_client/src/server_device/commands/forward.rs +++ b/adb_client/src/server_device/commands/forward.rs @@ -1,4 +1,4 @@ -use crate::{ADBServerDevice, Result, models::AdbServerCommand}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Forward socket connection diff --git a/adb_client/src/server_device/commands/framebuffer.rs b/adb_client/src/server_device/commands/framebuffer.rs index e99949b4..c67b0daa 100644 --- a/adb_client/src/server_device/commands/framebuffer.rs +++ b/adb_client/src/server_device/commands/framebuffer.rs @@ -4,8 +4,10 @@ use byteorder::{LittleEndian, ReadBytesExt}; use image::{ImageBuffer, Rgba}; use crate::{ - ADBServerDevice, Result, RustADBError, - models::{AdbServerCommand, FrameBufferInfoV1, FrameBufferInfoV2}, + Result, RustADBError, + models::{FrameBufferInfoV1, FrameBufferInfoV2}, + server::AdbServerCommand, + server_device::ADBServerDevice, }; impl ADBServerDevice { diff --git a/adb_client/src/server_device/commands/host_features.rs b/adb_client/src/server_device/commands/host_features.rs index 3ffe1a91..8c3f757b 100644 --- a/adb_client/src/server_device/commands/host_features.rs +++ b/adb_client/src/server_device/commands/host_features.rs @@ -1,6 +1,5 @@ use crate::{ - ADBServerDevice, Result, - models::{AdbServerCommand, HostFeatures}, + Result, models::HostFeatures, server::AdbServerCommand, server_device::ADBServerDevice, }; impl ADBServerDevice { diff --git a/adb_client/src/server_device/commands/install.rs b/adb_client/src/server_device/commands/install.rs index 10381814..b29bff5d 100644 --- a/adb_client/src/server_device/commands/install.rs +++ b/adb_client/src/server_device/commands/install.rs @@ -1,7 +1,7 @@ use std::{fs::File, io::Read, path::Path}; use crate::{ - Result, models::AdbServerCommand, server_device::ADBServerDevice, utils::check_extension_is_apk, + Result, server::AdbServerCommand, server_device::ADBServerDevice, utils::check_extension_is_apk, }; impl ADBServerDevice { diff --git a/adb_client/src/server_device/commands/list.rs b/adb_client/src/server_device/commands/list.rs index 5ae54e63..30aaec70 100644 --- a/adb_client/src/server_device/commands/list.rs +++ b/adb_client/src/server_device/commands/list.rs @@ -1,6 +1,8 @@ use crate::{ - ADBServerDevice, Result, RustADBError, - models::{ADBListItem, ADBListItemType, AdbServerCommand, SyncCommand}, + Result, RustADBError, + models::{ADBListItem, ADBListItemType, SyncCommand}, + server::AdbServerCommand, + server_device::ADBServerDevice, }; use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; use std::{ diff --git a/adb_client/src/server_device/commands/logcat.rs b/adb_client/src/server_device/commands/logcat.rs index b0107852..be4f5f79 100644 --- a/adb_client/src/server_device/commands/logcat.rs +++ b/adb_client/src/server_device/commands/logcat.rs @@ -1,6 +1,6 @@ use std::io::{self, Write}; -use crate::{ADBDeviceExt, ADBServerDevice, Result}; +use crate::{ADBDeviceExt, Result, server_device::ADBServerDevice}; struct LogFilter { writer: W, diff --git a/adb_client/src/server_device/commands/reboot.rs b/adb_client/src/server_device/commands/reboot.rs index 8c41db28..a4935226 100644 --- a/adb_client/src/server_device/commands/reboot.rs +++ b/adb_client/src/server_device/commands/reboot.rs @@ -1,7 +1,4 @@ -use crate::{ - ADBServerDevice, Result, - models::{AdbServerCommand, RebootType}, -}; +use crate::{Result, models::RebootType, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Reboots the device diff --git a/adb_client/src/server_device/commands/reconnect.rs b/adb_client/src/server_device/commands/reconnect.rs index 3dce5d8e..a3501f03 100644 --- a/adb_client/src/server_device/commands/reconnect.rs +++ b/adb_client/src/server_device/commands/reconnect.rs @@ -1,4 +1,4 @@ -use crate::{ADBServerDevice, Result, models::AdbServerCommand}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Reconnect device diff --git a/adb_client/src/server_device/commands/recv.rs b/adb_client/src/server_device/commands/recv.rs index 9631e07e..1f4466e0 100644 --- a/adb_client/src/server_device/commands/recv.rs +++ b/adb_client/src/server_device/commands/recv.rs @@ -1,6 +1,5 @@ use crate::{ - ADBServerDevice, Result, constants, - models::{AdbServerCommand, SyncCommand}, + Result, models::SyncCommand, server::AdbServerCommand, server_device::ADBServerDevice, }; use byteorder::{LittleEndian, ReadBytesExt}; use std::io::{BufReader, BufWriter, Read, Write}; @@ -63,6 +62,8 @@ impl Read for ADBRecvCommandReader { } } +const BUFFER_SIZE: usize = 65535; + impl ADBServerDevice { /// Receives path to stream from the device. pub fn pull(&mut self, path: &dyn AsRef, stream: &mut dyn Write) -> Result<()> { @@ -92,8 +93,8 @@ impl ADBServerDevice { let reader = ADBRecvCommandReader::new(raw_connection); std::io::copy( - &mut BufReader::with_capacity(constants::BUFFER_SIZE, reader), - &mut BufWriter::with_capacity(constants::BUFFER_SIZE, output), + &mut BufReader::with_capacity(BUFFER_SIZE, reader), + &mut BufWriter::with_capacity(BUFFER_SIZE, output), )?; // Connection should've been left in SYNC mode by now diff --git a/adb_client/src/server_device/commands/reverse.rs b/adb_client/src/server_device/commands/reverse.rs index 00c36d85..cabc41ab 100644 --- a/adb_client/src/server_device/commands/reverse.rs +++ b/adb_client/src/server_device/commands/reverse.rs @@ -1,4 +1,4 @@ -use crate::{ADBServerDevice, Result, models::AdbServerCommand}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Reverse socket connection diff --git a/adb_client/src/server_device/commands/send.rs b/adb_client/src/server_device/commands/send.rs index e09d8408..42551542 100644 --- a/adb_client/src/server_device/commands/send.rs +++ b/adb_client/src/server_device/commands/send.rs @@ -1,6 +1,8 @@ use crate::{ - ADBServerDevice, Result, RustADBError, constants, - models::{AdbRequestStatus, AdbServerCommand, SyncCommand}, + Result, RustADBError, + models::{AdbRequestStatus, SyncCommand}, + server::AdbServerCommand, + server_device::ADBServerDevice, }; use std::{ convert::TryInto, @@ -40,6 +42,8 @@ impl Write for ADBSendCommandWriter { } } +const BUFFER_SIZE: usize = 65535; + impl ADBServerDevice { /// Send stream to path on the device. pub fn push>(&mut self, stream: R, path: A) -> Result<()> { @@ -71,8 +75,8 @@ impl ADBServerDevice { let writer = ADBSendCommandWriter::new(raw_connection); std::io::copy( - &mut BufReader::with_capacity(constants::BUFFER_SIZE, input), - &mut BufWriter::with_capacity(constants::BUFFER_SIZE, writer), + &mut BufReader::with_capacity(BUFFER_SIZE, input), + &mut BufWriter::with_capacity(BUFFER_SIZE, writer), )?; // Copy is finished, we can now notify as finished diff --git a/adb_client/src/server_device/commands/stat.rs b/adb_client/src/server_device/commands/stat.rs index db73bff1..0aa6fac5 100644 --- a/adb_client/src/server_device/commands/stat.rs +++ b/adb_client/src/server_device/commands/stat.rs @@ -3,8 +3,8 @@ use std::io::{Read, Write}; use byteorder::{ByteOrder, LittleEndian}; use crate::{ - ADBServerDevice, Result, RustADBError, - models::{AdbServerCommand, AdbStatResponse, SyncCommand}, + AdbStatResponse, Result, RustADBError, models::SyncCommand, server::AdbServerCommand, + server_device::ADBServerDevice, }; impl ADBServerDevice { diff --git a/adb_client/src/server_device/commands/tcpip.rs b/adb_client/src/server_device/commands/tcpip.rs index 601db1b8..cd8c222c 100644 --- a/adb_client/src/server_device/commands/tcpip.rs +++ b/adb_client/src/server_device/commands/tcpip.rs @@ -1,4 +1,4 @@ -use crate::{ADBServerDevice, Result, models::AdbServerCommand}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Set adb daemon to tcp/ip mode diff --git a/adb_client/src/server_device/commands/transport.rs b/adb_client/src/server_device/commands/transport.rs index da587421..81e88e87 100644 --- a/adb_client/src/server_device/commands/transport.rs +++ b/adb_client/src/server_device/commands/transport.rs @@ -1,4 +1,4 @@ -use crate::{ADBServerDevice, Result, models::AdbServerCommand}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Asks ADB server to switch the connection to either the device or emulator connect to/running on the host. Will fail if there is more than one such device/emulator available. diff --git a/adb_client/src/server_device/commands/uninstall.rs b/adb_client/src/server_device/commands/uninstall.rs index 324f894c..14791797 100644 --- a/adb_client/src/server_device/commands/uninstall.rs +++ b/adb_client/src/server_device/commands/uninstall.rs @@ -1,6 +1,6 @@ use std::io::Read; -use crate::{Result, models::AdbServerCommand, server_device::ADBServerDevice}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Uninstall a package from device diff --git a/adb_client/src/server_device/commands/usb.rs b/adb_client/src/server_device/commands/usb.rs index a68cadc5..da50a637 100644 --- a/adb_client/src/server_device/commands/usb.rs +++ b/adb_client/src/server_device/commands/usb.rs @@ -1,4 +1,4 @@ -use crate::{ADBServerDevice, Result, models::AdbServerCommand}; +use crate::{Result, server::AdbServerCommand, server_device::ADBServerDevice}; impl ADBServerDevice { /// Set adb daemon to usb mode diff --git a/adb_client/src/transports/mod.rs b/adb_client/src/transports/mod.rs deleted file mode 100644 index da50c1fa..00000000 --- a/adb_client/src/transports/mod.rs +++ /dev/null @@ -1,17 +0,0 @@ -mod tcp_emulator_transport; -mod tcp_server_transport; -mod tcp_transport; -mod traits; - -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -mod usb_transport; - -pub use tcp_emulator_transport::TCPEmulatorTransport; -pub use tcp_server_transport::TCPServerTransport; -pub use tcp_transport::TcpTransport; -pub use traits::{ADBMessageTransport, ADBTransport}; - -#[cfg(feature = "usb")] -#[cfg_attr(docsrs, doc(cfg(feature = "usb")))] -pub use usb_transport::USBTransport; diff --git a/adb_client/src/transports/traits/mod.rs b/adb_client/src/transports/traits/mod.rs deleted file mode 100644 index e655c814..00000000 --- a/adb_client/src/transports/traits/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod adb_message_transport; -mod adb_transport; - -pub use adb_message_transport::ADBMessageTransport; -pub use adb_transport::ADBTransport; diff --git a/pyadb_client/src/adb_server.rs b/pyadb_client/src/adb_server.rs index d5bddbbc..c1a17054 100644 --- a/pyadb_client/src/adb_server.rs +++ b/pyadb_client/src/adb_server.rs @@ -1,6 +1,6 @@ use std::net::SocketAddrV4; -use adb_client::ADBServer; +use adb_client::server::ADBServer; use anyhow::Result; use pyo3::{PyResult, pyclass, pymethods}; use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods}; diff --git a/pyadb_client/src/adb_server_device.rs b/pyadb_client/src/adb_server_device.rs index 9897d40e..f4d2b0a7 100644 --- a/pyadb_client/src/adb_server_device.rs +++ b/pyadb_client/src/adb_server_device.rs @@ -1,4 +1,4 @@ -use adb_client::{ADBDeviceExt, ADBServerDevice}; +use adb_client::{ADBDeviceExt, server_device::ADBServerDevice}; use anyhow::Result; use pyo3::{pyclass, pymethods}; use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods}; diff --git a/pyadb_client/src/adb_usb_device.rs b/pyadb_client/src/adb_usb_device.rs index 79433bdc..ced6db9e 100644 --- a/pyadb_client/src/adb_usb_device.rs +++ b/pyadb_client/src/adb_usb_device.rs @@ -1,6 +1,6 @@ use std::{fs::File, path::PathBuf}; -use adb_client::{ADBDeviceExt, ADBUSBDevice}; +use adb_client::{ADBDeviceExt, usb::ADBUSBDevice}; use anyhow::Result; use pyo3::{pyclass, pymethods}; use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods}; diff --git a/pyadb_client/src/models/devices.rs b/pyadb_client/src/models/devices.rs index 597f297c..cc26d207 100644 --- a/pyadb_client/src/models/devices.rs +++ b/pyadb_client/src/models/devices.rs @@ -1,4 +1,4 @@ -use adb_client::DeviceShort; +use adb_client::server::DeviceShort; use pyo3::{pyclass, pymethods}; use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods}; From d598555dc87236000503ae9a59a706c6f9c9f11b Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Wed, 3 Sep 2025 20:53:57 +0200 Subject: [PATCH 6/9] feat: improve README.md --- adb_client/README.md | 89 ++------------------ adb_client/src/message_devices/tcp/README.md | 13 +++ adb_client/src/message_devices/tcp/mod.rs | 2 + adb_client/src/message_devices/usb/README.md | 26 ++++++ adb_client/src/server/README.md | 15 ++++ adb_client/src/server/mod.rs | 2 + adb_client/src/server_device/README.md | 25 ++++++ adb_client/src/server_device/mod.rs | 2 + 8 files changed, 92 insertions(+), 82 deletions(-) create mode 100644 adb_client/src/message_devices/tcp/README.md create mode 100644 adb_client/src/message_devices/usb/README.md create mode 100644 adb_client/src/server/README.md create mode 100644 adb_client/src/server_device/README.md diff --git a/adb_client/README.md b/adb_client/README.md index 22ef171c..a8757164 100644 --- a/adb_client/README.md +++ b/adb_client/README.md @@ -27,9 +27,15 @@ To deactivate some default features you can use the `default-features = false` o ```toml [dependencies] -adb_client = { version = "*", default-features = false, features = ["mdns"] } +adb_client = { version = "*", default-features = false, features = ["mdns", "usb"] } ``` +## Examples + +Usage examples can be found in the `examples/` directory of this repository. + +Some example are also provided in the various `README.md` files of modules. + ## Benchmarks Benchmarks run on `v2.0.6`, on a **Samsung S10 SM-G973F** device and an **Intel i7-1265U** CPU laptop @@ -43,84 +49,3 @@ Benchmarks run on `v2.0.6`, on a **Samsung S10 SM-G973F** device and an **Intel | 10 MB | 100 | 350,79 ms | 356,30 ms |
-1,57 %
| | 500 MB | 50 | 15,60 s | 15,64 s |
-0,25 %
| | 1 GB | 20 | 31,09 s | 31,12 s |
-0,10 %
| - -## Examples - -### Get available ADB devices - -```rust no_run -use adb_client::server::ADBServer; -use std::net::{SocketAddrV4, Ipv4Addr}; - -// A custom server address can be provided -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(); -``` - -### Using ADB server as bridge - -#### Launch a command on device - -```rust no_run -use adb_client::{server::ADBServer, ADBDeviceExt}; - -let mut server = ADBServer::default(); -let mut device = server.get_device().expect("cannot get device"); -device.shell_command(&["df", "-h"], &mut std::io::stdout()); -``` - -#### Push a file to the device - -```rust no_run -use adb_client::server::ADBServer; -use std::net::Ipv4Addr; -use std::fs::File; -use std::path::Path; - -let mut server = ADBServer::default(); -let mut device = server.get_device().expect("cannot get device"); -let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file"); -device.push(&mut input, "/data/local/tmp"); -``` - -### Interact directly with end devices - -#### (USB) Launch a command on device - -```rust no_run -use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; - -let vendor_id = 0x04e8; -let product_id = 0x6860; -let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); -device.shell_command(&["df", "-h"], &mut std::io::stdout()); -``` - -#### (USB) Push a file to the device - -```rust no_run -use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; -use std::fs::File; -use std::path::Path; - -let vendor_id = 0x04e8; -let product_id = 0x6860; -let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); -let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file"); -device.push(&mut input, &"/data/local/tmp"); -``` - -#### (TCP) Get a shell from device - -```rust no_run -use std::net::{SocketAddr, IpAddr, Ipv4Addr}; -use adb_client::{tcp::ADBTcpDevice, ADBDeviceExt}; - -let device_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)); -let device_port = 43210; -let mut device = ADBTcpDevice::new(SocketAddr::new(device_ip, device_port)).expect("cannot find device"); -device.shell(&mut std::io::stdin(), Box::new(std::io::stdout())); -``` diff --git a/adb_client/src/message_devices/tcp/README.md b/adb_client/src/message_devices/tcp/README.md new file mode 100644 index 00000000..cc24b1fd --- /dev/null +++ b/adb_client/src/message_devices/tcp/README.md @@ -0,0 +1,13 @@ +# Examples + +## Get a shell from device + +```rust no_run +use std::net::{SocketAddr, IpAddr, Ipv4Addr}; +use adb_client::{tcp::ADBTcpDevice, ADBDeviceExt}; + +let device_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)); +let device_port = 43210; +let mut device = ADBTcpDevice::new(SocketAddr::new(device_ip, device_port)).expect("cannot find device"); +device.shell(&mut std::io::stdin(), Box::new(std::io::stdout())); +``` diff --git a/adb_client/src/message_devices/tcp/mod.rs b/adb_client/src/message_devices/tcp/mod.rs index cd9a81dc..17b12674 100644 --- a/adb_client/src/message_devices/tcp/mod.rs +++ b/adb_client/src/message_devices/tcp/mod.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("./README.md")] + mod adb_tcp_device; mod tcp_transport; diff --git a/adb_client/src/message_devices/usb/README.md b/adb_client/src/message_devices/usb/README.md new file mode 100644 index 00000000..bc9bfd9b --- /dev/null +++ b/adb_client/src/message_devices/usb/README.md @@ -0,0 +1,26 @@ +# Examples + +## Launch a command on device + +```rust no_run +use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; + +let vendor_id = 0x04e8; +let product_id = 0x6860; +let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); +device.shell_command(&["df", "-h"], &mut std::io::stdout()); +``` + +## Push a file to the device + +```rust no_run +use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; +use std::fs::File; +use std::path::Path; + +let vendor_id = 0x04e8; +let product_id = 0x6860; +let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); +let mut input = File::open(Path::new("/tmp/file.txt")).expect("Cannot open file"); +device.push(&mut input, &"/data/local/tmp"); +``` diff --git a/adb_client/src/server/README.md b/adb_client/src/server/README.md new file mode 100644 index 00000000..845da978 --- /dev/null +++ b/adb_client/src/server/README.md @@ -0,0 +1,15 @@ +# Examples + +## Get available ADB devices + +```rust no_run +use adb_client::server::ADBServer; +use std::net::{SocketAddrV4, Ipv4Addr}; + +// A custom server address can be provided +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(); +``` diff --git a/adb_client/src/server/mod.rs b/adb_client/src/server/mod.rs index 30d73410..a47dfe27 100644 --- a/adb_client/src/server/mod.rs +++ b/adb_client/src/server/mod.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("./README.md")] + mod adb_server; mod adb_server_command; mod commands; diff --git a/adb_client/src/server_device/README.md b/adb_client/src/server_device/README.md new file mode 100644 index 00000000..27bf6216 --- /dev/null +++ b/adb_client/src/server_device/README.md @@ -0,0 +1,25 @@ +# Examples + +## Launch a command on device + +```rust no_run +use adb_client::{server::ADBServer, ADBDeviceExt}; + +let mut server = ADBServer::default(); +let mut device = server.get_device().expect("cannot get device"); +device.shell_command(&["df", "-h"], &mut std::io::stdout()); +``` + +## Push a file to the device + +```rust no_run +use adb_client::server::ADBServer; +use std::net::Ipv4Addr; +use std::fs::File; +use std::path::Path; + +let mut server = ADBServer::default(); +let mut device = server.get_device().expect("cannot get device"); +let mut input = File::open(Path::new("/tmp/file.txt")).expect("Cannot open file"); +device.push(&mut input, "/data/local/tmp"); +``` diff --git a/adb_client/src/server_device/mod.rs b/adb_client/src/server_device/mod.rs index c7830011..286c69d6 100644 --- a/adb_client/src/server_device/mod.rs +++ b/adb_client/src/server_device/mod.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("./README.md")] + mod adb_server_device; mod adb_server_device_commands; mod commands; From 95f8f83747460aa539386a8cedaa890d7f76fdc0 Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Sun, 28 Dec 2025 12:46:38 +0100 Subject: [PATCH 7/9] fix: post-rebase fixes --- .../src/emulator/tcp_emulator_transport.rs | 28 +++++++++---------- adb_client/src/mdns/mdns_discovery.rs | 17 +++-------- .../src/message_devices/tcp/tcp_transport.rs | 14 +++++----- benches/benchmark_adb_push.rs | 2 +- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/adb_client/src/emulator/tcp_emulator_transport.rs b/adb_client/src/emulator/tcp_emulator_transport.rs index a508333a..e79cbd61 100644 --- a/adb_client/src/emulator/tcp_emulator_transport.rs +++ b/adb_client/src/emulator/tcp_emulator_transport.rs @@ -8,6 +8,19 @@ use crate::{ Result, RustADBError, adb_transport::ADBTransport, emulator::models::ADBEmulatorCommand, }; +/// Return authentication token stored in `$HOME/.emulator_console_auth_token` +fn get_authentication_token() -> Result { + let Some(home) = std::env::home_dir() else { + return Err(RustADBError::NoHomeDirectory); + }; + + let mut f = File::open(home.join(".emulator_console_auth_token"))?; + let mut token = String::new(); + f.read_to_string(&mut token)?; + + Ok(token) +} + /// Emulator transport running on top on TCP. #[derive(Debug)] pub struct TCPEmulatorTransport { @@ -34,22 +47,9 @@ impl TCPEmulatorTransport { ))) } - /// Return authentication token stored in `$HOME/.emulator_console_auth_token` - pub fn get_authentication_token(&mut self) -> Result { - let Some(home) = std::env::home_dir() else { - return Err(RustADBError::NoHomeDirectory); - }; - - let mut f = File::open(home.join(".emulator_console_auth_token"))?; - let mut token = String::new(); - f.read_to_string(&mut token)?; - - Ok(token) - } - /// Send an authenticate request to this emulator pub fn authenticate(&mut self) -> Result<()> { - let token = self.get_authentication_token()?; + let token = get_authentication_token()?; self.send_command(&ADBEmulatorCommand::Authenticate(token)) } diff --git a/adb_client/src/mdns/mdns_discovery.rs b/adb_client/src/mdns/mdns_discovery.rs index 96bda94f..12b63039 100644 --- a/adb_client/src/mdns/mdns_discovery.rs +++ b/adb_client/src/mdns/mdns_discovery.rs @@ -36,19 +36,10 @@ impl MDNSDiscoveryService { let handle: JoinHandle> = std::thread::spawn(move || { loop { while let Ok(event) = receiver.recv() { - match event { - ServiceEvent::ServiceResolved(service_info) => { - sender - .send(MDNSDevice::from(service_info)) - .map_err(|_| RustADBError::SendError)?; - } - ServiceEvent::SearchStarted(_) - | ServiceEvent::ServiceRemoved(_, _) - | ServiceEvent::ServiceFound(_, _) - | ServiceEvent::SearchStopped(_) - | _ => { - // Ignoring these events. We are only interesting in found devices - } + if let ServiceEvent::ServiceResolved(service_info) = event { + sender + .send(MDNSDevice::from(service_info)) + .map_err(|_| RustADBError::SendError)?; } } } diff --git a/adb_client/src/message_devices/tcp/tcp_transport.rs b/adb_client/src/message_devices/tcp/tcp_transport.rs index ef628617..dd619236 100644 --- a/adb_client/src/message_devices/tcp/tcp_transport.rs +++ b/adb_client/src/message_devices/tcp/tcp_transport.rs @@ -96,19 +96,19 @@ fn certificate_from_pk(key_pair: &KeyPair) -> Result impl TcpTransport { /// Instantiate a new [`TcpTransport`] pub fn new(address: SocketAddr) -> Result { - Self::new_with_custom_private_key(address, get_default_adb_key_path()?) + Ok(Self::new_with_custom_private_key( + address, + get_default_adb_key_path()?, + )) } /// Instantiate a new [`TcpTransport`] using a given private key - pub fn new_with_custom_private_key( - address: SocketAddr, - private_key_path: PathBuf, - ) -> Result { - Ok(Self { + pub fn new_with_custom_private_key(address: SocketAddr, private_key_path: PathBuf) -> Self { + Self { address, current_connection: None, private_key_path, - }) + } } fn get_current_connection(&self) -> Result>> { diff --git a/benches/benchmark_adb_push.rs b/benches/benchmark_adb_push.rs index 85444323..a986d7fa 100644 --- a/benches/benchmark_adb_push.rs +++ b/benches/benchmark_adb_push.rs @@ -1,4 +1,4 @@ -use adb_client::ADBServer; +use adb_client::server::ADBServer; use anyhow::Result; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use rand::{Rng, rng}; From 18817c34dbe71db251affca965adc4936d67b36a Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Sun, 28 Dec 2025 13:59:02 +0100 Subject: [PATCH 8/9] feat: improve API --- adb_cli/src/main.rs | 16 +------ adb_client/Cargo.toml | 4 +- adb_client/src/adb_device_ext.rs | 6 +-- .../adb_message_device_commands.rs | 6 +-- .../src/message_devices/commands/list.rs | 27 ++++++------ .../src/message_devices/tcp/adb_tcp_device.rs | 5 +-- .../src/message_devices/usb/adb_usb_device.rs | 4 +- adb_client/src/models/list_info.rs | 44 ++++++++++++++----- .../adb_server_device_commands.rs | 14 ++++-- adb_client/src/server_device/commands/list.rs | 24 +++++----- 10 files changed, 84 insertions(+), 66 deletions(-) diff --git a/adb_cli/src/main.rs b/adb_cli/src/main.rs index cd2ab1f7..97fbf05c 100644 --- a/adb_cli/src/main.rs +++ b/adb_cli/src/main.rs @@ -7,12 +7,12 @@ mod handlers; mod models; mod utils; +use adb_client::ADBDeviceExt; use adb_client::mdns::MDNSDiscoveryService; use adb_client::server::ADBServer; use adb_client::server_device::ADBServerDevice; use adb_client::tcp::ADBTcpDevice; use adb_client::usb::ADBUSBDevice; -use adb_client::{ADBDeviceExt, ADBListItemType}; #[cfg(any(target_os = "linux", target_os = "macos"))] use adb_termios::ADBTermios; @@ -91,19 +91,7 @@ fn run_command(mut device: Box, command: DeviceCommands) -> AD DeviceCommands::List { path } => { let dirs = device.list(&path)?; for dir in dirs { - let list_item_type = match dir.item_type { - ADBListItemType::File => "File".to_string(), - ADBListItemType::Directory => "Directory".to_string(), - ADBListItemType::Symlink => "Symlink".to_string(), - }; - log::info!( - "type: {}, name: {}, time: {}, size: {}, permissions: {:#o}", - list_item_type, - dir.name, - dir.time, - dir.size, - dir.permissions - ); + log::info!("{dir}"); } } } diff --git a/adb_client/Cargo.toml b/adb_client/Cargo.toml index d09e11e1..87cf8780 100644 --- a/adb_client/Cargo.toml +++ b/adb_client/Cargo.toml @@ -20,7 +20,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = [] mdns = ["dep:mdns-sd"] -usb = ["dep:rsa", "dep:rusb"] +usb = ["dep:rusb"] [dependencies] base64 = { version = "0.22.1" } @@ -38,6 +38,7 @@ rcgen = { version = "0.14.6", default-features = false, features = [ "pem", ] } regex = { version = "1.12.2", features = ["perf", "std", "unicode"] } +rsa = { version = "0.9.7" } rustls = { version = "0.23.35" } rustls-pki-types = { version = "1.13.2" } serde = { version = "1.0.228", features = ["derive"] } @@ -53,7 +54,6 @@ mdns-sd = { version = "0.17.1", default-features = false, features = [ ######### ######### # USB-only dependencies -rsa = { version = "0.9.7", optional = true } rusb = { version = "0.9.4", features = ["vendored"], optional = true } ######### diff --git a/adb_client/src/adb_device_ext.rs b/adb_client/src/adb_device_ext.rs index c3d70548..f8a9df90 100644 --- a/adb_client/src/adb_device_ext.rs +++ b/adb_client/src/adb_device_ext.rs @@ -3,10 +3,10 @@ use std::path::Path; use image::{ImageBuffer, ImageFormat, Rgba}; -use crate::models::{ADBListItem, AdbStatResponse}; +use crate::models::{ADBListItemType, AdbStatResponse}; use crate::{RebootType, Result}; -/// Trait representing all features available on both [`crate::server_device::ADBServerDevice`] and [`crate::usb::ADBUSBDevice`] +/// Trait representing all features available on ADB devices. pub trait ADBDeviceExt { /// Runs command in a shell on the device, and write its output and error streams into output. fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()>; @@ -25,7 +25,7 @@ pub trait ADBDeviceExt { fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef) -> Result<()>; /// List the items in a directory on the device - fn list(&mut self, path: &dyn AsRef) -> Result>; + fn list(&mut self, path: &dyn AsRef) -> Result>; /// Reboot the device using given reboot type fn reboot(&mut self, reboot_type: RebootType) -> Result<()>; diff --git a/adb_client/src/message_devices/adb_message_device_commands.rs b/adb_client/src/message_devices/adb_message_device_commands.rs index 324a344e..f37bb2af 100644 --- a/adb_client/src/message_devices/adb_message_device_commands.rs +++ b/adb_client/src/message_devices/adb_message_device_commands.rs @@ -1,9 +1,9 @@ use crate::{ - ADBDeviceExt, RebootType, Result, + ADBDeviceExt, ADBListItemType, RebootType, Result, message_devices::{ adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport, }, - models::{ADBListItem, AdbStatResponse}, + models::AdbStatResponse, }; use std::{ io::{Read, Write}, @@ -47,7 +47,7 @@ impl ADBDeviceExt for ADBMessageDevice { self.framebuffer_inner() } - fn list(&mut self, path: &dyn AsRef) -> Result> { + fn list(&mut self, path: &dyn AsRef) -> Result> { self.list(path) } } diff --git a/adb_client/src/message_devices/commands/list.rs b/adb_client/src/message_devices/commands/list.rs index c0a99bef..2aa462d0 100644 --- a/adb_client/src/message_devices/commands/list.rs +++ b/adb_client/src/message_devices/commands/list.rs @@ -10,13 +10,12 @@ use crate::message_devices::adb_message_transport::ADBMessageTransport; use crate::message_devices::adb_transport_message::ADBTransportMessage; use crate::message_devices::message_commands::MessageCommand; use crate::message_devices::message_commands::MessageSubcommand; -use crate::models::ADBListItem; -use crate::models::ADBListItemType; +use crate::models::{ADBListItem, ADBListItemType}; impl ADBMessageDevice { /// List the entries in the given directory on the device. /// note: path uses internal file paths, so Documents is at /storage/emulated/0/Documents - pub(crate) fn list>(&mut self, path: A) -> Result> { + pub(crate) fn list>(&mut self, path: A) -> Result> { let session = self.begin_synchronization()?; let output = self.handle_list(path, session.local_id(), session.remote_id()); @@ -79,7 +78,7 @@ impl ADBMessageDevice { path: A, local_id: u32, remote_id: u32, - ) -> Result> { + ) -> Result> { // TODO: use LIS2 to support files over 2.14 GB in size. // SEE: https://github.com/cstyan/adbDocumentation?tab=readme-ov-file#adb-list { @@ -154,21 +153,23 @@ impl ADBMessageDevice { // First 9 bits are the file permissions let permissions = mode & 0b1_1111_1111; - // Bits 14 to 16 are the file type - let item_type = match (mode >> 13) & 0b111 { - 0b010 => ADBListItemType::Directory, - 0b100 => ADBListItemType::File, - 0b101 => ADBListItemType::Symlink, - type_code => return Err(RustADBError::UnknownFileMode(type_code)), - }; + let entry = ADBListItem { name, time, permissions, size, - item_type, }; - list_items.push(entry); + + // Bits 14 to 16 are the file type + let item_type = match (mode >> 13) & 0b111 { + 0b010 => ADBListItemType::Directory(entry), + 0b100 => ADBListItemType::File(entry), + 0b101 => ADBListItemType::Symlink(entry), + type_code => return Err(RustADBError::UnknownFileMode(type_code)), + }; + + list_items.push(item_type); } "DONE" => { return Ok(list_items); diff --git a/adb_client/src/message_devices/tcp/adb_tcp_device.rs b/adb_client/src/message_devices/tcp/adb_tcp_device.rs index c60dc1cf..a8850edd 100644 --- a/adb_client/src/message_devices/tcp/adb_tcp_device.rs +++ b/adb_client/src/message_devices/tcp/adb_tcp_device.rs @@ -7,10 +7,9 @@ use crate::message_devices::adb_message_transport::ADBMessageTransport; use crate::message_devices::adb_transport_message::ADBTransportMessage; use crate::message_devices::message_commands::MessageCommand; use crate::message_devices::models::{ADBRsaKey, read_adb_private_key}; -use crate::models::ADBListItem; use crate::tcp::tcp_transport::TcpTransport; use crate::utils::get_default_adb_key_path; -use crate::{ADBDeviceExt, ADBTransport, Result}; +use crate::{ADBDeviceExt, ADBListItemType, ADBTransport, Result}; /// Represent a device reached and available over TCP. #[derive(Debug)] @@ -147,7 +146,7 @@ impl ADBDeviceExt for ADBTcpDevice { } #[inline] - fn list(&mut self, path: &dyn AsRef) -> Result> { + fn list(&mut self, path: &dyn AsRef) -> Result> { self.inner.list(path) } } diff --git a/adb_client/src/message_devices/usb/adb_usb_device.rs b/adb_client/src/message_devices/usb/adb_usb_device.rs index 86fa9e16..6c35957d 100644 --- a/adb_client/src/message_devices/usb/adb_usb_device.rs +++ b/adb_client/src/message_devices/usb/adb_usb_device.rs @@ -8,6 +8,7 @@ use std::path::Path; use std::path::PathBuf; use crate::ADBDeviceExt; +use crate::ADBListItemType; use crate::Result; use crate::RustADBError; use crate::adb_transport::ADBTransport; @@ -17,7 +18,6 @@ use crate::message_devices::adb_transport_message::ADBTransportMessage; use crate::message_devices::message_commands::MessageCommand; use crate::message_devices::models::ADBRsaKey; use crate::message_devices::models::read_adb_private_key; -use crate::models::ADBListItem; use crate::usb::usb_transport::USBTransport; use crate::utils::get_default_adb_key_path; @@ -231,7 +231,7 @@ impl ADBDeviceExt for ADBUSBDevice { } #[inline] - fn list(&mut self, path: &dyn AsRef) -> Result> { + fn list(&mut self, path: &dyn AsRef) -> Result> { self.inner.list(path) } } diff --git a/adb_client/src/models/list_info.rs b/adb_client/src/models/list_info.rs index d99e6946..fbfbbf7a 100644 --- a/adb_client/src/models/list_info.rs +++ b/adb_client/src/models/list_info.rs @@ -1,5 +1,28 @@ +use std::fmt::Display; + +/// The different types of item that the `ADBListItem` can represent. +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] +pub enum ADBListItemType { + /// The entry is a file + File(ADBListItem), + /// The entry is a directory + Directory(ADBListItem), + /// The entry is a symlink + Symlink(ADBListItem), +} + +impl Display for ADBListItemType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ADBListItemType::File(item) => write!(f, "file: {item}"), + ADBListItemType::Directory(item) => write!(f, "directory: {item}"), + ADBListItemType::Symlink(item) => write!(f, "symlink: {item}"), + } + } +} + +/// An item list entry on the device. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] -/// A list entry on the remote device pub struct ADBListItem { /// The name of the file, not the path pub name: String, @@ -9,17 +32,14 @@ pub struct ADBListItem { pub permissions: u32, /// The size of the file pub size: u32, - /// The type of item this is, file, directory or symlink - pub item_type: ADBListItemType, } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] -/// The different types of item that the list item can be -pub enum ADBListItemType { - /// The entry is a file - File, - /// The entry is a directory - Directory, - /// The entry is a symlink - Symlink, +impl Display for ADBListItem { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "name: {}, time: {}, size: {}, permissions: {:#o}", + self.name, self.time, self.size, self.permissions + ) + } } diff --git a/adb_client/src/server_device/adb_server_device_commands.rs b/adb_client/src/server_device/adb_server_device_commands.rs index f194cfaf..b9378fae 100644 --- a/adb_client/src/server_device/adb_server_device_commands.rs +++ b/adb_client/src/server_device/adb_server_device_commands.rs @@ -4,8 +4,8 @@ use std::{ }; use crate::{ - ADBDeviceExt, Result, RustADBError, - models::{ADBListItem, AdbStatResponse, HostFeatures}, + ADBDeviceExt, ADBListItemType, Result, RustADBError, + models::{AdbStatResponse, HostFeatures}, server::AdbServerCommand, }; @@ -59,6 +59,7 @@ impl ADBDeviceExt for ADBServerDevice { } } + #[inline] fn stat(&mut self, remote_path: &str) -> Result { self.stat(remote_path) } @@ -114,31 +115,38 @@ impl ADBDeviceExt for ADBServerDevice { Ok(()) } + #[inline] fn pull(&mut self, source: &dyn AsRef, mut output: &mut dyn Write) -> Result<()> { self.pull(source, &mut output) } + #[inline] fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> { self.reboot(reboot_type) } + #[inline] fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef) -> Result<()> { self.push(stream, path) } + #[inline] fn install(&mut self, apk_path: &dyn AsRef) -> Result<()> { self.install(apk_path) } + #[inline] fn uninstall(&mut self, package: &str) -> Result<()> { self.uninstall(package) } + #[inline] fn framebuffer_inner(&mut self) -> Result, Vec>> { self.framebuffer_inner() } - fn list(&mut self, path: &dyn AsRef) -> Result> { + #[inline] + fn list(&mut self, path: &dyn AsRef) -> Result> { self.list(path) } } diff --git a/adb_client/src/server_device/commands/list.rs b/adb_client/src/server_device/commands/list.rs index 30aaec70..a97e2d8a 100644 --- a/adb_client/src/server_device/commands/list.rs +++ b/adb_client/src/server_device/commands/list.rs @@ -13,7 +13,7 @@ use std::{ impl ADBServerDevice { /// Lists files in path on the device. /// note: path uses internal file paths, so Documents is at /storage/emulated/0/Documents - pub fn list>(&mut self, path: A) -> Result> { + pub fn list>(&mut self, path: A) -> Result> { self.set_serial_transport()?; // Set device in SYNC mode @@ -25,7 +25,7 @@ impl ADBServerDevice { self.handle_list_command(path) } - fn handle_list_command>(&mut self, path: A) -> Result> { + fn handle_list_command>(&mut self, path: A) -> Result> { // TODO: use LIS2 to support files over 2.14 GB in size. // SEE: https://github.com/cstyan/adbDocumentation?tab=readme-ov-file#adb-list let mut len_buf = [0_u8; 4]; @@ -61,21 +61,23 @@ impl ADBServerDevice { // First 9 bits are the file permissions let permissions = mode & 0b1_1111_1111; - // Bits 14 to 16 are the file type - let item_type = match (mode >> 13) & 0b111 { - 0b010 => ADBListItemType::Directory, - 0b100 => ADBListItemType::File, - 0b101 => ADBListItemType::Symlink, - type_code => return Err(RustADBError::UnknownFileMode(type_code)), - }; + let entry = ADBListItem { name, time, permissions, size, - item_type, }; - list_items.push(entry); + + // Bits 14 to 16 are the file type + let item_type = match (mode >> 13) & 0b111 { + 0b010 => ADBListItemType::Directory(entry), + 0b100 => ADBListItemType::File(entry), + 0b101 => ADBListItemType::Symlink(entry), + type_code => return Err(RustADBError::UnknownFileMode(type_code)), + }; + + list_items.push(item_type); } "DONE" => { return Ok(list_items); From 8bc8222bddd2b7efb74abdc49c31fa31fa90e507 Mon Sep 17 00:00:00 2001 From: Corentin LIAUD Date: Sun, 28 Dec 2025 14:09:39 +0100 Subject: [PATCH 9/9] breaking(api): dyn in traits --- adb_client/src/adb_device_ext.rs | 21 +++++++++++++++---- .../src/message_devices/adb_message_device.rs | 4 ++-- .../adb_message_device_commands.rs | 14 +++++++++++-- .../src/message_devices/commands/stat.rs | 4 ++-- .../src/message_devices/commands/uninstall.rs | 13 +++++++++--- adb_client/src/message_devices/mod.rs | 3 --- .../{ => models}/adb_session.rs | 0 adb_client/src/message_devices/models/mod.rs | 2 ++ .../src/message_devices/tcp/adb_tcp_device.rs | 4 ++-- .../src/message_devices/usb/adb_usb_device.rs | 4 ++-- .../adb_server_device_commands.rs | 8 +++---- pyadb_client/src/adb_usb_device.rs | 2 +- 12 files changed, 54 insertions(+), 25 deletions(-) rename adb_client/src/message_devices/{ => models}/adb_session.rs (100%) diff --git a/adb_client/src/adb_device_ext.rs b/adb_client/src/adb_device_ext.rs index f8a9df90..0515be32 100644 --- a/adb_client/src/adb_device_ext.rs +++ b/adb_client/src/adb_device_ext.rs @@ -16,7 +16,7 @@ pub trait ADBDeviceExt { fn shell(&mut self, reader: &mut dyn Read, writer: Box) -> Result<()>; /// Display the stat information for a remote file - fn stat(&mut self, remote_path: &str) -> Result; + fn stat(&mut self, remote_path: &dyn AsRef) -> Result; /// Pull the remote file pointed to by `source` and write its contents into `output` fn pull(&mut self, source: &dyn AsRef, output: &mut dyn Write) -> Result<()>; @@ -31,10 +31,23 @@ pub trait ADBDeviceExt { fn reboot(&mut self, reboot_type: RebootType) -> Result<()>; /// Run `activity` from `package` on device. Return the command output. - fn run_activity(&mut self, package: &str, activity: &str) -> Result> { + fn run_activity( + &mut self, + package: &dyn AsRef, + activity: &dyn AsRef, + ) -> Result> { let mut output = Vec::new(); self.shell_command( - &["am", "start", &format!("{package}/{package}.{activity}")], + &[ + "am", + "start", + &format!( + "{}/{}.{}", + package.as_ref(), + package.as_ref(), + activity.as_ref() + ), + ], &mut output, )?; @@ -45,7 +58,7 @@ pub trait ADBDeviceExt { fn install(&mut self, apk_path: &dyn AsRef) -> Result<()>; /// Uninstall the package `package` from device. - fn uninstall(&mut self, package: &str) -> Result<()>; + fn uninstall(&mut self, package: &dyn AsRef) -> Result<()>; /// Inner method requesting framebuffer from an Android device fn framebuffer_inner(&mut self) -> Result, Vec>>; diff --git a/adb_client/src/message_devices/adb_message_device.rs b/adb_client/src/message_devices/adb_message_device.rs index 97878140..bc09d1bc 100644 --- a/adb_client/src/message_devices/adb_message_device.rs +++ b/adb_client/src/message_devices/adb_message_device.rs @@ -8,14 +8,14 @@ use std::{ }; use crate::{ - ADBSession, AdbStatResponse, Result, RustADBError, + AdbStatResponse, Result, RustADBError, message_devices::{ adb_message_transport::ADBMessageTransport, adb_transport_message::{ ADBTransportMessage, AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AUTH_TOKEN, }, message_commands::{MessageCommand, MessageSubcommand}, - models::ADBRsaKey, + models::{ADBRsaKey, ADBSession}, }, }; diff --git a/adb_client/src/message_devices/adb_message_device_commands.rs b/adb_client/src/message_devices/adb_message_device_commands.rs index f37bb2af..0a9065dd 100644 --- a/adb_client/src/message_devices/adb_message_device_commands.rs +++ b/adb_client/src/message_devices/adb_message_device_commands.rs @@ -11,42 +11,52 @@ use std::{ }; impl ADBDeviceExt for ADBMessageDevice { + #[inline] fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { self.shell_command(command, output) } + #[inline] fn shell(&mut self, reader: &mut dyn Read, writer: Box) -> Result<()> { self.shell(reader, writer) } - fn stat(&mut self, remote_path: &str) -> Result { + #[inline] + fn stat(&mut self, remote_path: &dyn AsRef) -> Result { self.stat(remote_path) } + #[inline] fn pull(&mut self, source: &dyn AsRef, output: &mut dyn Write) -> Result<()> { self.pull(source, output) } + #[inline] fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef) -> Result<()> { self.push(stream, path) } + #[inline] fn reboot(&mut self, reboot_type: RebootType) -> Result<()> { self.reboot(reboot_type) } + #[inline] fn install(&mut self, apk_path: &dyn AsRef) -> Result<()> { self.install(apk_path) } - fn uninstall(&mut self, package: &str) -> Result<()> { + #[inline] + fn uninstall(&mut self, package: &dyn AsRef) -> Result<()> { self.uninstall(package) } + #[inline] fn framebuffer_inner(&mut self) -> Result, Vec>> { self.framebuffer_inner() } + #[inline] fn list(&mut self, path: &dyn AsRef) -> Result> { self.list(path) } diff --git a/adb_client/src/message_devices/commands/stat.rs b/adb_client/src/message_devices/commands/stat.rs index 792dc5f5..87fd6b06 100644 --- a/adb_client/src/message_devices/commands/stat.rs +++ b/adb_client/src/message_devices/commands/stat.rs @@ -6,9 +6,9 @@ use crate::{ }; impl ADBMessageDevice { - pub(crate) fn stat(&mut self, remote_path: &str) -> Result { + pub(crate) fn stat(&mut self, remote_path: &dyn AsRef) -> Result { let session = self.begin_synchronization()?; - let adb_stat_response = self.stat_with_explicit_ids(session, remote_path)?; + let adb_stat_response = self.stat_with_explicit_ids(session, remote_path.as_ref())?; self.end_transaction(session)?; Ok(adb_stat_response) } diff --git a/adb_client/src/message_devices/commands/uninstall.rs b/adb_client/src/message_devices/commands/uninstall.rs index 1af47fd8..49fb5916 100644 --- a/adb_client/src/message_devices/commands/uninstall.rs +++ b/adb_client/src/message_devices/commands/uninstall.rs @@ -6,14 +6,21 @@ use crate::{ }; impl ADBMessageDevice { - pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> { - self.open_session(format!("exec:cmd package 'uninstall' {package_name}\0").as_bytes())?; + pub(crate) fn uninstall(&mut self, package_name: &dyn AsRef) -> Result<()> { + self.open_session( + format!( + "exec:cmd package 'uninstall' {}{}", + package_name.as_ref(), + "\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 {package_name} successfully uninstalled"); + log::info!("Package {} successfully uninstalled", package_name.as_ref()); Ok(()) } d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8( diff --git a/adb_client/src/message_devices/mod.rs b/adb_client/src/message_devices/mod.rs index 0f2d59d6..7017ab20 100644 --- a/adb_client/src/message_devices/mod.rs +++ b/adb_client/src/message_devices/mod.rs @@ -9,10 +9,7 @@ pub mod tcp; mod adb_message_device; mod adb_message_device_commands; mod adb_message_transport; -mod adb_session; mod adb_transport_message; mod commands; mod message_commands; mod models; - -pub(crate) use adb_session::ADBSession; diff --git a/adb_client/src/message_devices/adb_session.rs b/adb_client/src/message_devices/models/adb_session.rs similarity index 100% rename from adb_client/src/message_devices/adb_session.rs rename to adb_client/src/message_devices/models/adb_session.rs diff --git a/adb_client/src/message_devices/models/mod.rs b/adb_client/src/message_devices/models/mod.rs index dff04610..abc1da75 100644 --- a/adb_client/src/message_devices/models/mod.rs +++ b/adb_client/src/message_devices/models/mod.rs @@ -1,4 +1,6 @@ mod adb_rsa_key; +mod adb_session; pub use adb_rsa_key::ADBRsaKey; pub(crate) use adb_rsa_key::read_adb_private_key; +pub(crate) use adb_session::ADBSession; diff --git a/adb_client/src/message_devices/tcp/adb_tcp_device.rs b/adb_client/src/message_devices/tcp/adb_tcp_device.rs index a8850edd..04edba7b 100644 --- a/adb_client/src/message_devices/tcp/adb_tcp_device.rs +++ b/adb_client/src/message_devices/tcp/adb_tcp_device.rs @@ -111,7 +111,7 @@ impl ADBDeviceExt for ADBTcpDevice { } #[inline] - fn stat(&mut self, remote_path: &str) -> Result { + fn stat(&mut self, remote_path: &dyn AsRef) -> Result { self.inner.stat(remote_path) } @@ -136,7 +136,7 @@ impl ADBDeviceExt for ADBTcpDevice { } #[inline] - fn uninstall(&mut self, package: &str) -> Result<()> { + fn uninstall(&mut self, package: &dyn AsRef) -> Result<()> { self.inner.uninstall(package) } diff --git a/adb_client/src/message_devices/usb/adb_usb_device.rs b/adb_client/src/message_devices/usb/adb_usb_device.rs index 6c35957d..f0174e1d 100644 --- a/adb_client/src/message_devices/usb/adb_usb_device.rs +++ b/adb_client/src/message_devices/usb/adb_usb_device.rs @@ -196,7 +196,7 @@ impl ADBDeviceExt for ADBUSBDevice { } #[inline] - fn stat(&mut self, remote_path: &str) -> Result { + fn stat(&mut self, remote_path: &dyn AsRef) -> Result { self.inner.stat(remote_path) } @@ -221,7 +221,7 @@ impl ADBDeviceExt for ADBUSBDevice { } #[inline] - fn uninstall(&mut self, package: &str) -> Result<()> { + fn uninstall(&mut self, package: &dyn AsRef) -> Result<()> { self.inner.uninstall(package) } diff --git a/adb_client/src/server_device/adb_server_device_commands.rs b/adb_client/src/server_device/adb_server_device_commands.rs index b9378fae..286ef659 100644 --- a/adb_client/src/server_device/adb_server_device_commands.rs +++ b/adb_client/src/server_device/adb_server_device_commands.rs @@ -60,8 +60,8 @@ impl ADBDeviceExt for ADBServerDevice { } #[inline] - fn stat(&mut self, remote_path: &str) -> Result { - self.stat(remote_path) + fn stat(&mut self, remote_path: &dyn AsRef) -> Result { + self.stat(remote_path.as_ref()) } fn shell( @@ -136,8 +136,8 @@ impl ADBDeviceExt for ADBServerDevice { } #[inline] - fn uninstall(&mut self, package: &str) -> Result<()> { - self.uninstall(package) + fn uninstall(&mut self, package: &dyn AsRef) -> Result<()> { + self.uninstall(package.as_ref()) } #[inline] diff --git a/pyadb_client/src/adb_usb_device.rs b/pyadb_client/src/adb_usb_device.rs index ced6db9e..e429be0f 100644 --- a/pyadb_client/src/adb_usb_device.rs +++ b/pyadb_client/src/adb_usb_device.rs @@ -51,7 +51,7 @@ impl PyADBUSBDevice { /// Uninstall a package installed on the device pub fn uninstall(&mut self, package: &str) -> Result<()> { - Ok(self.0.uninstall(package)?) + Ok(self.0.uninstall(&package)?) } }