13 Commits

Author SHA1 Message Date
cocool97
39591b6a0a ci: build python wheels (#84) 2025-01-24 10:11:51 +01:00
LIAUD Corentin
775b2421ec chore(ci): improve python build 2025-01-22 15:57:02 +01:00
LIAUD Corentin
62d16b70fb chore: version 2.1.0 2025-01-22 15:25:03 +01:00
cli
466d00e68a feat: Python package (#80)
* feat: create pyadb_client python package

* feat: add push / pull methods

* feat: add shell_command for USB

* feat(ci): add python package build
2025-01-22 15:22:36 +01:00
KFBI1706
144072ba1b fix: track_devices support multiple devices (#83) 2025-01-21 07:31:39 +01:00
LIAUD Corentin
331ef95530 chore: fix deps for adb_cli 2024-12-18 11:24:52 +01:00
LIAUD Corentin
61408cb470 chore: version 2.1.0 2024-12-18 11:12:56 +01:00
cli
c54942f25d feat: improve USB performances by storing endpoints (#79) 2024-12-18 10:55:40 +01:00
cli
3feda38cc3 core: bump deps + update README.md (#78) 2024-12-18 08:34:31 +01:00
cli
9002ecc0c8 chore: internal updates (#74)
* chore: update adb_client README.md

* chore(internal): add assert_command() method on ADBTransportMessage

* chore: minor internal changes

* chore: add server-only models
2024-12-13 14:08:48 +01:00
Stone
5a3ac68fce feat: do not detach kernel driver (#72) 2024-12-10 17:17:41 +01:00
cli
5dfd30cc5b breaking: make ADBDeviceExt dyn-compatible (#70)
* feat: make ADBDeviceExt dyn-compatible

* feat: clean CLI code
2024-12-06 17:47:38 +01:00
cli
66d124475d feat: add framebuffer method to USB/TCP direct devices (#69)
* feat: store local_id and remote_id in ADBMessageDevice
2024-12-03 13:55:12 +01:00
96 changed files with 1399 additions and 1186 deletions

49
.github/workflows/python-build.yml vendored Normal file
View File

@@ -0,0 +1,49 @@
name: Python - Build packages & Release
on:
push: {}
pull_request: {}
release:
types: [created]
jobs:
build-python-packages:
runs-on: ubuntu-latest
defaults:
run:
working-directory: pyadb_client
steps:
- uses: actions/checkout@v4
- name: Install Python dependencies
run: pip install .
- name: Build Python packages
run: maturin build --release --interpreter "3.8" --interpreter "3.9" --interpreter "3.10" --interpreter "3.11" --interpreter "3.12" --interpreter "3.13"
publish-python-packages:
runs-on: ubuntu-latest
defaults:
run:
working-directory: pyadb_client
needs: [build-python-packages]
if: github.event_name == 'release' && github.event.action == 'created'
steps:
- uses: actions/checkout@v4
- name: Install Python dependencies
run: pip install .
- name: Publish Python packages
run: maturin publish --no-interactive
env:
MATURIN_PYPI_TOKEN: ${{ secrets.MATURIN_PYPI_TOKEN }}
- name: "Publish GitHub artefacts"
uses: softprops/action-gh-release@v2
with:
files: |
target/wheels/pyadb_client*.whl
target/wheels/pyadb_client*.tar.gz

View File

@@ -9,8 +9,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: "Checkout repository" - uses: actions/checkout@v4
uses: actions/checkout@v4
- name: "Set up Rust" - name: "Set up Rust"
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
@@ -25,6 +24,13 @@ jobs:
cargo install cargo-deb cargo install cargo-deb
cargo install cargo-generate-rpm cargo install cargo-generate-rpm
- name: "Publish crates"
run: |
cargo publish -p adb_client --token ${CRATES_IO_TOKEN}
cargo publish -p adb_cli --token ${CRATES_IO_TOKEN}
env:
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
- name: "build-release" - name: "build-release"
run: cargo build --all-features --release run: cargo build --all-features --release
@@ -40,11 +46,4 @@ jobs:
files: | files: |
target/debian/*.deb target/debian/*.deb
target/generate-rpm/*.rpm target/generate-rpm/*.rpm
target/release/adb_cli target/release/adb_cli
- name: "Publish crates"
run: |
cargo publish -p adb_client --token ${CRATES_IO_TOKEN}
cargo publish -p adb_cli --token ${CRATES_IO_TOKEN}
env:
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

6
.gitignore vendored
View File

@@ -1,3 +1,5 @@
target target
Cargo.lock /Cargo.lock
.vscode /.vscode
venv
/.mypy_cache

View File

@@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["adb_cli", "adb_client"] members = ["adb_cli", "adb_client", "pyadb_client"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
@@ -9,7 +9,7 @@ homepage = "https://github.com/cocool97/adb_client"
keywords = ["adb", "android", "tcp", "usb"] keywords = ["adb", "android", "tcp", "usb"]
license = "MIT" license = "MIT"
repository = "https://github.com/cocool97/adb_client" repository = "https://github.com/cocool97/adb_client"
version = "2.0.6" version = "2.1.3"
# To build locally when working on a new release # To build locally when working on a new release
[patch.crates-io] [patch.crates-io]

View File

@@ -30,6 +30,7 @@ Main features of this library:
- Over **TCP/IP** - Over **TCP/IP**
- Implements hidden `adb` features, like `framebuffer` - Implements hidden `adb` features, like `framebuffer`
- Highly configurable - Highly configurable
- Provides wrappers to use directly from Python code
- Easy to use ! - Easy to use !
## adb_client ## adb_client
@@ -41,12 +42,19 @@ Improved documentation available [here](./adb_client/README.md).
## adb_cli ## adb_cli
Rust binary providing an improved version of Google's official `adb` CLI, by using `adb_client` library. Rust binary providing an improved version of Google's official `adb` CLI, by using `adb_client` library.
Provides an usage example of the library. Provides a "real-world" usage example of this library.
Improved documentation available [here](./adb_cli/README.md). Improved documentation available [here](./adb_cli/README.md).
## pyadb_client
Python wrapper using `adb_client` library to export classes usable directly from a Python environment.
Improved documentation available [here](./pyadb_client/README.md)
## Related publications ## Related publications
- [Diving into ADB protocol internals (1/2)](https://www.synacktiv.com/publications/diving-into-adb-protocol-internals-12) - [Diving into ADB protocol internals (1/2)](https://www.synacktiv.com/publications/diving-into-adb-protocol-internals-12)
- [Diving into ADB protocol internals (2/2)](https://www.synacktiv.com/publications/diving-into-adb-protocol-internals-22)
Some features may still be missing, all pull requests are welcome ! Some features may still be missing, all pull requests are welcome !

View File

@@ -10,9 +10,9 @@ repository.workspace = true
version.workspace = true version.workspace = true
[dependencies] [dependencies]
adb_client = { version = "2.0.5" } adb_client = { version = "^2.0.0" }
anyhow = { version = "1.0.89" } anyhow = { version = "1.0.94" }
clap = { version = "4.5.18", features = ["derive"] } clap = { version = "4.5.23", features = ["derive"] }
env_logger = { version = "0.11.5" } env_logger = { version = "0.11.5" }
log = { version = "0.4.22" } log = { version = "0.4.22" }

View File

@@ -18,38 +18,29 @@ Usage is quite simple, and tends to look like `adb`:
- To use ADB server as a proxy: - To use ADB server as a proxy:
```bash ```bash
user@laptop ~/adb_client (main)> adb_cli --help user@laptop ~/adb_client (main)> adb_cli local --help
Rust ADB (Android Debug Bridge) CLI Device related commands using server
Usage: adb_cli [OPTIONS] <COMMAND> Usage: adb_cli local [OPTIONS] <COMMAND>
Commands: Commands:
host-features List available server features
push Push a file on device
pull Pull a file from device
list List a directory on device
stat Stat a file specified on device
shell Spawn an interactive shell or run a list of commands on the device shell Spawn an interactive shell or run a list of commands on the device
pull Pull a file from device
push Push a file on device
stat Stat a file on device
run Run an activity on device specified by the intent
reboot Reboot the device reboot Reboot the device
install Install an APK on device
framebuffer Dump framebuffer of device framebuffer Dump framebuffer of device
host-features List available server features
list List a directory on device
logcat Get logs of device logcat Get logs of device
version Print current ADB version
kill Ask ADB server to quit immediately
devices List connected devices
track-devices Track new devices showing up
pair Pair device with a given code
connect Connect device over WI-FI
disconnect Disconnect device over WI-FI
sms Send a SMS with given phone number and given content
rotate Rotate device screen from 90°
help Print this message or the help of the given subcommand(s) help Print this message or the help of the given subcommand(s)
Options: Options:
-d, --debug
-a, --address <ADDRESS> [default: 127.0.0.1:5037] -a, --address <ADDRESS> [default: 127.0.0.1:5037]
-s, --serial <SERIAL> Serial id of a specific device. Every request will be sent to this device -s, --serial <SERIAL> Serial id of a specific device. Every request will be sent to this device
-h, --help Print help -h, --help Print help
-V, --version Print version
``` ```
- To interact directly with end devices - To interact directly with end devices

View File

@@ -1,12 +0,0 @@
use clap::Parser;
#[derive(Parser, Debug)]
pub enum EmuCommand {
/// Send a SMS with given phone number and given content
Sms {
phone_number: String,
content: String,
},
/// Rotate device screen from 90°
Rotate,
}

View File

@@ -1,46 +0,0 @@
use clap::Parser;
use std::path::PathBuf;
use crate::models::RebootTypeCommand;
#[derive(Parser, Debug)]
pub enum LocalCommand {
/// List available server features.
HostFeatures,
/// Push a file on device
Push { filename: String, path: String },
/// Pull a file from device
Pull { path: String, filename: String },
/// List a directory on device
List { path: String },
/// Stat a file specified on device
Stat { path: String },
/// Spawn an interactive shell or run a list of commands on the device
Shell { commands: Vec<String> },
/// Run an activity on device specified by the intent
Run {
/// The package whose activity is to be invoked
#[clap(short = 'p', long = "package")]
package: String,
/// The activity to be invoked itself, Usually it is MainActivity
#[clap(short = 'a', long = "activity")]
activity: String,
},
/// Reboot the device
Reboot {
#[clap(subcommand)]
reboot_type: RebootTypeCommand,
},
/// Dump framebuffer of device
Framebuffer { path: String },
/// Get logs of device
Logcat {
/// Path to output file (created if not exists)
path: Option<String>,
},
/// Install an APK on device
Install {
/// Path to APK file. Extension must be ".apk"
path: PathBuf,
},
}

View File

@@ -1,11 +0,0 @@
mod emu;
mod host;
mod local;
mod tcp;
mod usb;
pub use emu::EmuCommand;
pub use host::{HostCommand, MdnsCommand};
pub use local::LocalCommand;
pub use tcp::{TcpCommand, TcpCommands};
pub use usb::{UsbCommand, UsbCommands};

View File

@@ -1,56 +0,0 @@
use std::num::ParseIntError;
use std::path::PathBuf;
use clap::Parser;
use crate::models::RebootTypeCommand;
fn parse_hex_id(id: &str) -> Result<u16, ParseIntError> {
u16::from_str_radix(id, 16)
}
#[derive(Parser, Debug)]
pub struct UsbCommand {
/// Hexadecimal vendor id of this USB device
#[clap(short = 'v', long = "vendor-id", value_parser=parse_hex_id, value_name="VID")]
pub vendor_id: Option<u16>,
/// Hexadecimal product id of this USB device
#[clap(short = 'p', long = "product-id", value_parser=parse_hex_id, value_name="PID")]
pub product_id: Option<u16>,
/// Path to a custom private key to use for authentication
#[clap(short = 'k', long = "private-key")]
pub path_to_private_key: Option<PathBuf>,
#[clap(subcommand)]
pub commands: UsbCommands,
}
#[derive(Parser, Debug)]
pub enum UsbCommands {
/// Spawn an interactive shell or run a list of commands on the device
Shell { commands: Vec<String> },
/// Pull a file from device
Pull { source: String, destination: String },
/// Push a file on device
Push { filename: String, path: String },
/// Stat a file on device
Stat { path: String },
/// Run an activity on device specified by the intent
Run {
/// The package whose activity is to be invoked
#[clap(short = 'p', long = "package")]
package: String,
/// The activity to be invoked itself, Usually it is MainActivity
#[clap(short = 'a', long = "activity")]
activity: String,
},
/// Reboot the device
Reboot {
#[clap(subcommand)]
reboot_type: RebootTypeCommand,
},
/// Install an APK on device
Install {
/// Path to APK file. Extension must be ".apk"
path: PathBuf,
},
}

View File

@@ -0,0 +1,20 @@
use adb_client::ADBEmulatorDevice;
use crate::models::{EmuCommand, EmulatorCommand};
pub fn handle_emulator_commands(emulator_command: EmulatorCommand) -> anyhow::Result<()> {
let mut emulator = ADBEmulatorDevice::new(emulator_command.serial, None)?;
match emulator_command.command {
EmuCommand::Sms {
phone_number,
content,
} => {
emulator.send_sms(&phone_number, &content)?;
log::info!("SMS sent to {phone_number}");
}
EmuCommand::Rotate => emulator.rotate()?,
}
Ok(())
}

View File

@@ -0,0 +1,78 @@
use adb_client::{ADBServer, DeviceShort, MDNSBackend, Result};
use crate::models::{HostCommand, MdnsCommand, ServerCommand};
pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Result<()> {
let mut adb_server = ADBServer::new(server_command.address);
match server_command.command {
HostCommand::Version => {
let version = adb_server.version()?;
log::info!("Android Debug Bridge version {}", version);
log::info!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
}
HostCommand::Kill => {
adb_server.kill()?;
}
HostCommand::Devices { long } => {
if long {
log::info!("List of devices attached (extended)");
for device in adb_server.devices_long()? {
log::info!("{}", device);
}
} else {
log::info!("List of devices attached");
for device in adb_server.devices()? {
log::info!("{}", device);
}
}
}
HostCommand::TrackDevices => {
let callback = |device: DeviceShort| {
log::info!("{}", device);
Ok(())
};
log::info!("Live list of devices attached");
adb_server.track_devices(callback)?;
}
HostCommand::Pair { address, code } => {
adb_server.pair(address, code)?;
log::info!("Paired device {address}");
}
HostCommand::Connect { address } => {
adb_server.connect_device(address)?;
log::info!("Connected to {address}");
}
HostCommand::Disconnect { address } => {
adb_server.disconnect_device(address)?;
log::info!("Disconnected {address}");
}
HostCommand::Mdns { subcommand } => match subcommand {
MdnsCommand::Check => {
let check = adb_server.mdns_check()?;
let server_status = adb_server.server_status()?;
match server_status.mdns_backend {
MDNSBackend::Unknown => log::info!("unknown mdns backend..."),
MDNSBackend::Bonjour => match check {
true => log::info!("mdns daemon version [Bonjour]"),
false => log::info!("ERROR: mdns daemon unavailable"),
},
MDNSBackend::OpenScreen => {
log::info!("mdns daemon version [Openscreen discovery 0.0.0]")
}
}
}
MdnsCommand::Services => {
log::info!("List of discovered mdns services");
for service in adb_server.mdns_services()? {
log::info!("{}", service);
}
}
},
HostCommand::ServerStatus => {
log::info!("{}", adb_server.server_status()?);
}
}
Ok(())
}

View File

@@ -0,0 +1,35 @@
use std::{fs::File, io::Write};
use adb_client::ADBServerDevice;
use anyhow::{anyhow, Result};
use crate::models::LocalDeviceCommand;
pub fn handle_local_commands(
mut device: ADBServerDevice,
local_device_commands: LocalDeviceCommand,
) -> Result<()> {
match local_device_commands {
LocalDeviceCommand::HostFeatures => {
let features = device
.host_features()?
.iter()
.map(|v| v.to_string())
.reduce(|a, b| format!("{a},{b}"))
.ok_or(anyhow!("cannot list features"))?;
log::info!("Available host features: {features}");
Ok(())
}
LocalDeviceCommand::List { path } => Ok(device.list(path)?),
LocalDeviceCommand::Logcat { path } => {
let writer: Box<dyn Write> = if let Some(path) = path {
let f = File::create(path)?;
Box::new(f)
} else {
Box::new(std::io::stdout())
};
Ok(device.get_logs(writer)?)
}
}
}

View File

@@ -0,0 +1,7 @@
mod emulator_commands;
mod host_commands;
mod local_commands;
pub use emulator_commands::handle_emulator_commands;
pub use host_commands::handle_host_commands;
pub use local_commands::handle_local_commands;

View File

@@ -3,334 +3,68 @@
#[cfg(any(target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "linux", target_os = "macos"))]
mod adb_termios; mod adb_termios;
mod commands; mod handlers;
mod models; mod models;
mod utils;
use adb_client::{ use adb_client::{ADBDeviceExt, ADBServer, ADBTcpDevice, ADBUSBDevice, MDNSDiscoveryService};
ADBDeviceExt, ADBEmulatorDevice, ADBServer, ADBTcpDevice, ADBUSBDevice, DeviceShort, use adb_termios::ADBTermios;
MDNSBackend, MDNSDiscoveryService, use anyhow::Result;
};
use anyhow::{anyhow, Result};
use clap::Parser; use clap::Parser;
use commands::{EmuCommand, HostCommand, LocalCommand, MdnsCommand, TcpCommands, UsbCommands}; use handlers::{handle_emulator_commands, handle_host_commands, handle_local_commands};
use models::{Command, Opts}; use models::{DeviceCommands, LocalCommand, MainCommand, Opts};
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use utils::setup_logger;
fn main() -> Result<()> { fn main() -> Result<()> {
let opts = Opts::parse(); let opts = Opts::parse();
// RUST_LOG variable has more priority then "--debug" flag setup_logger(opts.debug);
if std::env::var("RUST_LOG").is_err() {
let level = match opts.debug {
true => "trace",
false => "info",
};
std::env::set_var("RUST_LOG", level); // Directly handling methods / commands that aren't linked to [`ADBDeviceExt`] trait.
} // Other methods just have to create a concrete [`ADBDeviceExt`] instance, and return it.
// This instance will then be used to execute desired command.
let (mut device, commands) = match opts.command {
MainCommand::Host(server_command) => return Ok(handle_host_commands(server_command)?),
MainCommand::Emu(emulator_command) => return handle_emulator_commands(emulator_command),
MainCommand::Local(server_command) => {
let mut adb_server = ADBServer::new(server_command.address);
// Setting default log level as "info" if not set let device = match server_command.serial {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "info");
}
env_logger::init();
match opts.command {
Command::Local(local) => {
let mut adb_server = ADBServer::new(opts.address);
let mut device = match opts.serial {
Some(serial) => adb_server.get_device_by_name(&serial)?, Some(serial) => adb_server.get_device_by_name(&serial)?,
None => adb_server.get_device()?, None => adb_server.get_device()?,
}; };
match local { match server_command.command {
LocalCommand::Pull { path, filename } => { LocalCommand::DeviceCommands(device_commands) => (device.boxed(), device_commands),
let mut output = File::create(Path::new(&filename))?; LocalCommand::LocalDeviceCommand(local_device_command) => {
device.pull(&path, &mut output)?; return handle_local_commands(device, local_device_command)
log::info!("Downloaded {path} as {filename}");
}
LocalCommand::Push { filename, path } => {
let mut input = File::open(Path::new(&filename))?;
device.push(&mut input, &path)?;
log::info!("Uploaded {filename} to {path}");
}
LocalCommand::List { path } => {
device.list(path)?;
}
LocalCommand::Stat { path } => {
let stat_response = device.stat(path)?;
println!("{}", stat_response);
}
LocalCommand::Shell { commands } => {
if commands.is_empty() {
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = adb_termios::ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(std::io::stdin(), std::io::stdout())?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
device.shell_command(commands, std::io::stdout())?;
}
}
LocalCommand::Run { package, activity } => {
let output = device.run_activity(&package, &activity)?;
std::io::stdout().write_all(&output)?;
}
LocalCommand::HostFeatures => {
let features = device
.host_features()?
.iter()
.map(|v| v.to_string())
.reduce(|a, b| format!("{a},{b}"))
.ok_or(anyhow!("cannot list features"))?;
log::info!("Available host features: {features}");
}
LocalCommand::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
LocalCommand::Framebuffer { path } => {
device.framebuffer(&path)?;
log::info!("Framebuffer dropped: {path}");
}
LocalCommand::Logcat { path } => {
let writer: Box<dyn Write> = if let Some(path) = path {
let f = File::create(path)?;
Box::new(f)
} else {
Box::new(std::io::stdout())
};
device.get_logs(writer)?;
}
LocalCommand::Install { path } => {
log::info!("Starting installation of APK {}...", path.display());
device.install(path)?;
} }
} }
} }
Command::Host(host) => { MainCommand::Usb(usb_command) => {
let mut adb_server = ADBServer::new(opts.address); let device = match (usb_command.vendor_id, usb_command.product_id) {
(Some(vid), Some(pid)) => match usb_command.path_to_private_key {
match host {
HostCommand::Version => {
let version = adb_server.version()?;
log::info!("Android Debug Bridge version {}", version);
log::info!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
}
HostCommand::Kill => {
adb_server.kill()?;
}
HostCommand::Devices { long } => {
if long {
log::info!("List of devices attached (extended)");
for device in adb_server.devices_long()? {
log::info!("{}", device);
}
} else {
log::info!("List of devices attached");
for device in adb_server.devices()? {
log::info!("{}", device);
}
}
}
HostCommand::TrackDevices => {
let callback = |device: DeviceShort| {
log::info!("{}", device);
Ok(())
};
log::info!("Live list of devices attached");
adb_server.track_devices(callback)?;
}
HostCommand::Pair { address, code } => {
adb_server.pair(address, code)?;
log::info!("Paired device {address}");
}
HostCommand::Connect { address } => {
adb_server.connect_device(address)?;
log::info!("Connected to {address}");
}
HostCommand::Disconnect { address } => {
adb_server.disconnect_device(address)?;
log::info!("Disconnected {address}");
}
HostCommand::Mdns { subcommand } => match subcommand {
MdnsCommand::Check => {
let check = adb_server.mdns_check()?;
let server_status = adb_server.server_status()?;
match server_status.mdns_backend {
MDNSBackend::Unknown => log::info!("unknown mdns backend..."),
MDNSBackend::Bonjour => match check {
true => log::info!("mdns daemon version [Bonjour]"),
false => log::info!("ERROR: mdns daemon unavailable"),
},
MDNSBackend::OpenScreen => {
log::info!("mdns daemon version [Openscreen discovery 0.0.0]")
}
}
}
MdnsCommand::Services => {
log::info!("List of discovered mdns services");
for service in adb_server.mdns_services()? {
log::info!("{}", service);
}
}
},
HostCommand::ServerStatus => {
log::info!("{}", adb_server.server_status()?);
}
}
}
Command::Emu(emu) => {
let mut emulator = match opts.serial {
Some(serial) => ADBEmulatorDevice::new(serial, None)?,
None => return Err(anyhow!("Serial must be set to use emulators !")),
};
match emu {
EmuCommand::Sms {
phone_number,
content,
} => {
emulator.send_sms(&phone_number, &content)?;
log::info!("SMS sent to {phone_number}");
}
EmuCommand::Rotate => emulator.rotate()?,
}
}
Command::Usb(usb) => {
let mut device = match (usb.vendor_id, usb.product_id) {
(Some(vid), Some(pid)) => match usb.path_to_private_key {
Some(pk) => ADBUSBDevice::new_with_custom_private_key(vid, pid, pk)?, Some(pk) => ADBUSBDevice::new_with_custom_private_key(vid, pid, pk)?,
None => ADBUSBDevice::new(vid, pid)?, None => ADBUSBDevice::new(vid, pid)?,
}, },
(None, None) => match usb_command.path_to_private_key {
(None, None) => match usb.path_to_private_key {
Some(pk) => ADBUSBDevice::autodetect_with_custom_private_key(pk)?, Some(pk) => ADBUSBDevice::autodetect_with_custom_private_key(pk)?,
None => ADBUSBDevice::autodetect()?, None => ADBUSBDevice::autodetect()?,
}, },
_ => { _ => {
anyhow::bail!("please either supply values for both the --vendor-id and --product-id flags or none."); anyhow::bail!("please either supply values for both the --vendor-id and --product-id flags or none.");
} }
}; };
(device.boxed(), usb_command.commands)
match usb.commands {
UsbCommands::Shell { commands } => {
if commands.is_empty() {
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = adb_termios::ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(std::io::stdin(), std::io::stdout())?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
device.shell_command(commands, std::io::stdout())?;
}
}
UsbCommands::Pull {
source,
destination,
} => {
let mut output = File::create(Path::new(&destination))?;
device.pull(&source, &mut output)?;
log::info!("Downloaded {source} as {destination}");
}
UsbCommands::Stat { path } => {
let stat_response = device.stat(&path)?;
println!("{}", stat_response);
}
UsbCommands::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
UsbCommands::Push { filename, path } => {
let mut input = File::open(Path::new(&filename))?;
device.push(&mut input, &path)?;
log::info!("Uploaded {filename} to {path}");
}
UsbCommands::Run { package, activity } => {
let output = device.run_activity(&package, &activity)?;
std::io::stdout().write_all(&output)?;
}
UsbCommands::Install { path } => {
log::info!("Starting installation of APK {}...", path.display());
device.install(path)?;
}
}
} }
Command::Tcp(tcp) => { MainCommand::Tcp(tcp_command) => {
let mut device = ADBTcpDevice::new(tcp.address)?; let device = ADBTcpDevice::new(tcp_command.address)?;
(device.boxed(), tcp_command.commands)
match tcp.commands {
TcpCommands::Shell { commands } => {
if commands.is_empty() {
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = adb_termios::ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(std::io::stdin(), std::io::stdout())?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
device.shell_command(commands, std::io::stdout())?;
}
}
TcpCommands::Pull {
source,
destination,
} => {
let mut output = File::create(Path::new(&destination))?;
device.pull(&source, &mut output)?;
log::info!("Downloaded {source} as {destination}");
}
TcpCommands::Stat { path } => {
let stat_response = device.stat(&path)?;
println!("{}", stat_response);
}
TcpCommands::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
TcpCommands::Push { filename, path } => {
let mut input = File::open(Path::new(&filename))?;
device.push(&mut input, &path)?;
log::info!("Uploaded {filename} to {path}");
}
TcpCommands::Run { package, activity } => {
let output = device.run_activity(&package, &activity)?;
std::io::stdout().write_all(&output)?;
}
TcpCommands::Install { path } => {
log::info!("Starting installation of APK {}...", path.display());
device.install(path)?;
}
}
} }
Command::MdnsDiscovery => { MainCommand::Mdns => {
let mut service = MDNSDiscoveryService::new()?; let mut service = MDNSDiscoveryService::new()?;
let (tx, rx) = std::sync::mpsc::channel(); let (tx, rx) = std::sync::mpsc::channel();
@@ -345,7 +79,63 @@ fn main() -> Result<()> {
) )
} }
service.shutdown()?; return Ok(service.shutdown()?);
}
};
match commands {
DeviceCommands::Shell { commands } => {
if commands.is_empty() {
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(&mut std::io::stdin(), Box::new(std::io::stdout()))?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
let commands: Vec<&str> = commands.iter().map(|v| v.as_str()).collect();
device.shell_command(&commands, &mut std::io::stdout())?;
}
}
DeviceCommands::Pull {
source,
destination,
} => {
let mut output = File::create(Path::new(&destination))?;
device.pull(&source, &mut output)?;
log::info!("Downloaded {source} as {destination}");
}
DeviceCommands::Stat { path } => {
let stat_response = device.stat(&path)?;
println!("{}", stat_response);
}
DeviceCommands::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
DeviceCommands::Push { filename, path } => {
let mut input = File::open(Path::new(&filename))?;
device.push(&mut input, &path)?;
log::info!("Uploaded {filename} to {path}");
}
DeviceCommands::Run { package, activity } => {
let output = device.run_activity(&package, &activity)?;
std::io::stdout().write_all(&output)?;
}
DeviceCommands::Install { path } => {
log::info!("Starting installation of APK {}...", path.display());
device.install(&path)?;
}
DeviceCommands::Framebuffer { path } => {
device.framebuffer(&path)?;
log::info!("Successfully dumped framebuffer at path {path}");
} }
} }

View File

@@ -1,19 +1,11 @@
use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use clap::Parser; use clap::Parser;
use crate::models::RebootTypeCommand; use super::RebootTypeCommand;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct TcpCommand { pub enum DeviceCommands {
pub address: SocketAddr,
#[clap(subcommand)]
pub commands: TcpCommands,
}
#[derive(Parser, Debug)]
pub enum TcpCommands {
/// Spawn an interactive shell or run a list of commands on the device /// Spawn an interactive shell or run a list of commands on the device
Shell { commands: Vec<String> }, Shell { commands: Vec<String> },
/// Pull a file from device /// Pull a file from device
@@ -41,4 +33,9 @@ pub enum TcpCommands {
/// Path to APK file. Extension must be ".apk" /// Path to APK file. Extension must be ".apk"
path: PathBuf, path: PathBuf,
}, },
/// Dump framebuffer of device
Framebuffer {
/// Framebuffer image destination path
path: String,
},
} }

20
adb_cli/src/models/emu.rs Normal file
View File

@@ -0,0 +1,20 @@
use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
pub struct EmulatorCommand {
#[clap(short = 's', long = "serial")]
pub serial: String,
#[clap(subcommand)]
pub command: EmuCommand,
}
#[derive(Debug, Subcommand)]
pub enum EmuCommand {
/// Send a SMS with given phone number and given content
Sms {
phone_number: String,
content: String,
},
/// Rotate device screen from 90°
Rotate,
}

View File

@@ -0,0 +1,24 @@
use clap::Parser;
use super::DeviceCommands;
#[derive(Parser, Debug)]
pub enum LocalCommand {
#[clap(flatten)]
DeviceCommands(DeviceCommands),
#[clap(flatten)]
LocalDeviceCommand(LocalDeviceCommand),
}
#[derive(Parser, Debug)]
pub enum LocalDeviceCommand {
/// List available server features.
HostFeatures,
/// List a directory on device
List { path: String },
/// Get logs of device
Logcat {
/// Path to output file (created if not exists)
path: Option<String>,
},
}

View File

@@ -1,5 +1,17 @@
mod device;
mod emu;
mod host;
mod local;
mod opts; mod opts;
mod reboot_type; mod reboot_type;
mod tcp;
mod usb;
pub use opts::{Command, Opts}; pub use device::DeviceCommands;
pub use emu::{EmuCommand, EmulatorCommand};
pub use host::{HostCommand, MdnsCommand};
pub use local::{LocalCommand, LocalDeviceCommand};
pub use opts::{MainCommand, Opts, ServerCommand};
pub use reboot_type::RebootTypeCommand; pub use reboot_type::RebootTypeCommand;
pub use tcp::TcpCommand;
pub use usb::UsbCommand;

View File

@@ -1,36 +1,41 @@
use std::net::SocketAddrV4; use std::net::SocketAddrV4;
use clap::Parser; use clap::{Parser, Subcommand};
use crate::commands::{EmuCommand, HostCommand, LocalCommand, TcpCommand, UsbCommand}; use super::{EmulatorCommand, HostCommand, LocalCommand, TcpCommand, UsbCommand};
#[derive(Parser, Debug)] #[derive(Debug, Parser)]
#[clap(about, version, author)] #[clap(about, version, author)]
pub struct Opts { pub struct Opts {
#[clap(long = "debug")] #[clap(long = "debug")]
pub debug: bool, pub debug: bool,
#[clap(subcommand)]
pub command: MainCommand,
}
#[derive(Debug, Parser)]
pub enum MainCommand {
/// Server related commands
Host(ServerCommand<HostCommand>),
/// Device related commands using server
Local(ServerCommand<LocalCommand>),
/// Emulator related commands
Emu(EmulatorCommand),
/// USB device related commands
Usb(UsbCommand),
/// TCP device related commands
Tcp(TcpCommand),
/// MDNS discovery related commands
Mdns,
}
#[derive(Debug, Parser)]
pub struct ServerCommand<T: Subcommand> {
#[clap(short = 'a', long = "address", default_value = "127.0.0.1:5037")] #[clap(short = 'a', long = "address", default_value = "127.0.0.1:5037")]
pub address: SocketAddrV4, pub address: SocketAddrV4,
/// Serial id of a specific device. Every request will be sent to this device. /// Serial id of a specific device. Every request will be sent to this device.
#[clap(short = 's', long = "serial")] #[clap(short = 's', long = "serial")]
pub serial: Option<String>, pub serial: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
pub command: Command, pub command: T,
}
#[derive(Parser, Debug)]
pub enum Command {
#[clap(flatten)]
Local(LocalCommand),
#[clap(flatten)]
Host(HostCommand),
/// Emulator specific commands
#[clap(subcommand)]
Emu(EmuCommand),
/// Device commands via USB, no server needed
Usb(UsbCommand),
/// Device commands via TCP, no server needed
Tcp(TcpCommand),
/// Discover devices over MDNS without using adb-server
MdnsDiscovery,
} }

11
adb_cli/src/models/tcp.rs Normal file
View File

@@ -0,0 +1,11 @@
use clap::Parser;
use std::net::SocketAddr;
use super::DeviceCommands;
#[derive(Parser, Debug)]
pub struct TcpCommand {
pub address: SocketAddr,
#[clap(subcommand)]
pub commands: DeviceCommands,
}

25
adb_cli/src/models/usb.rs Normal file
View File

@@ -0,0 +1,25 @@
use std::num::ParseIntError;
use std::path::PathBuf;
use clap::Parser;
use super::DeviceCommands;
fn parse_hex_id(id: &str) -> Result<u16, ParseIntError> {
u16::from_str_radix(id, 16)
}
#[derive(Parser, Debug)]
pub struct UsbCommand {
/// Hexadecimal vendor id of this USB device
#[clap(short = 'v', long = "vendor-id", value_parser=parse_hex_id, value_name="VID")]
pub vendor_id: Option<u16>,
/// Hexadecimal product id of this USB device
#[clap(short = 'p', long = "product-id", value_parser=parse_hex_id, value_name="PID")]
pub product_id: Option<u16>,
/// Path to a custom private key to use for authentication
#[clap(short = 'k', long = "private-key")]
pub path_to_private_key: Option<PathBuf>,
#[clap(subcommand)]
pub commands: DeviceCommands,
}

13
adb_cli/src/utils.rs Normal file
View File

@@ -0,0 +1,13 @@
pub fn setup_logger(debug: bool) {
// RUST_LOG variable has more priority then "--debug" flag
if std::env::var("RUST_LOG").is_err() {
let level = match debug {
true => "trace",
false => "info",
};
std::env::set_var("RUST_LOG", level);
}
env_logger::init();
}

View File

@@ -13,26 +13,26 @@ version.workspace = true
base64 = { version = "0.22.1" } base64 = { version = "0.22.1" }
bincode = { version = "1.3.3" } bincode = { version = "1.3.3" }
byteorder = { version = "1.5.0" } byteorder = { version = "1.5.0" }
chrono = { version = "0.4.38" } chrono = { version = "0.4.39" }
homedir = { version = "0.3.4" } homedir = { version = "0.3.4" }
image = { version = "0.25.5" } image = { version = "0.25.5" }
lazy_static = { version = "1.5.0" } lazy_static = { version = "1.5.0" }
log = { version = "0.4.22" } log = { version = "0.4.22" }
mdns-sd = { version = "0.12.0" } mdns-sd = { version = "0.13.1" }
num-bigint = { version = "0.8.4", package = "num-bigint-dig" } num-bigint = { version = "0.8.4", package = "num-bigint-dig" }
num-traits = { version = "0.2.19" } num-traits = { version = "0.2.19" }
quick-protobuf = { version = "0.8.1" } quick-protobuf = { version = "0.8.1" }
rand = { version = "0.8.5" } rand = { version = "0.8.5" }
rcgen = { version = "0.13.1" } rcgen = { version = "0.13.1" }
regex = { version = "1.11.0", features = ["perf", "std", "unicode"] } regex = { version = "1.11.1", features = ["perf", "std", "unicode"] }
rsa = { version = "0.9.7" } rsa = { version = "0.9.7" }
rusb = { version = "0.9.4", features = ["vendored"] } rusb = { version = "0.9.4", features = ["vendored"] }
rustls = { version = "0.23.18" } rustls = { version = "0.23.18" }
rustls-pki-types = "1.10.0" rustls-pki-types = "1.10.0"
serde = { version = "1.0.210", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
serde_repr = { version = "0.1.19" } serde_repr = { version = "0.1.19" }
sha1 = { version = "0.10.6", features = ["oid"] } sha1 = { version = "0.10.6", features = ["oid"] }
thiserror = { version = "2.0.1" } thiserror = { version = "2.0.7" }
[dev-dependencies] [dev-dependencies]
anyhow = { version = "1.0.93" } anyhow = { version = "1.0.93" }

View File

@@ -2,7 +2,7 @@
[![MIT licensed](https://img.shields.io/crates/l/adb_client.svg)](./LICENSE-MIT) [![MIT licensed](https://img.shields.io/crates/l/adb_client.svg)](./LICENSE-MIT)
[![Documentation](https://docs.rs/adb_client/badge.svg)](https://docs.rs/adb_client) [![Documentation](https://docs.rs/adb_client/badge.svg)](https://docs.rs/adb_client)
![Crates.io Total Downloads](https://img.shields.io/crates/d/adb_client) [![Crates.io Total Downloads](https://img.shields.io/crates/d/adb_client)](https://crates.io/crates/adb_client)
Rust library implementing ADB protocol. Rust library implementing ADB protocol.
@@ -15,6 +15,20 @@ Add `adb_client` crate as a dependency by simply adding it to your `Cargo.toml`:
adb_client = "*" adb_client = "*"
``` ```
## Benchmarks
Benchmarks run on `v2.0.6`, on a **Samsung S10 SM-G973F** device and an **Intel i7-1265U** CPU laptop
### `ADBServerDevice` push vs `adb push`
`ADBServerDevice` performs all operations by using adb server as a bridge.
|File size|Sample size|`ADBServerDevice`|`adb`|Difference|
|:-------:|:---------:|:----------:|:---:|:-----:|
|10 MB|100|350,79 ms|356,30 ms|<div style="color:green">-1,57 %</div>|
|500 MB|50|15,60 s|15,64 s|<div style="color:green">-0,25 %</div>|
|1 GB|20|31,09 s|31,12 s|<div style="color:green">-0,10 %</div>|
## Examples ## Examples
### Get available ADB devices ### Get available ADB devices
@@ -40,7 +54,7 @@ use adb_client::{ADBServer, ADBDeviceExt};
let mut server = ADBServer::default(); let mut server = ADBServer::default();
let mut device = server.get_device().expect("cannot get device"); let mut device = server.get_device().expect("cannot get device");
device.shell_command(["df", "-h"],std::io::stdout()); device.shell_command(&["df", "-h"], &mut std::io::stdout());
``` ```
#### Push a file to the device #### Push a file to the device
@@ -67,7 +81,7 @@ use adb_client::{ADBUSBDevice, ADBDeviceExt};
let vendor_id = 0x04e8; let vendor_id = 0x04e8;
let product_id = 0x6860; let product_id = 0x6860;
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
device.shell_command(["df", "-h"],std::io::stdout()); device.shell_command(&["df", "-h"], &mut std::io::stdout());
``` ```
#### (USB) Push a file to the device #### (USB) Push a file to the device
@@ -81,10 +95,10 @@ let vendor_id = 0x04e8;
let product_id = 0x6860; let product_id = 0x6860;
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); 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"); let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file");
device.push(&mut input, "/data/local/tmp"); device.push(&mut input, &"/data/local/tmp");
``` ```
### (TCP) Get a shell from device #### (TCP) Get a shell from device
```rust no_run ```rust no_run
use std::net::{SocketAddr, IpAddr, Ipv4Addr}; use std::net::{SocketAddr, IpAddr, Ipv4Addr};
@@ -93,5 +107,5 @@ use adb_client::{ADBTcpDevice, ADBDeviceExt};
let device_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10)); let device_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10));
let device_port = 43210; let device_port = 43210;
let mut device = ADBTcpDevice::new(SocketAddr::new(device_ip, device_port)).expect("cannot find device"); let mut device = ADBTcpDevice::new(SocketAddr::new(device_ip, device_port)).expect("cannot find device");
device.shell(std::io::stdin(), std::io::stdout()); device.shell(&mut std::io::stdin(), Box::new(std::io::stdout()));
``` ```

View File

@@ -1,31 +1,28 @@
use std::io::{Read, Write}; use std::io::{Cursor, Read, Write};
use std::path::Path; use std::path::Path;
use image::{ImageBuffer, ImageFormat, Rgba};
use crate::models::AdbStatResponse; use crate::models::AdbStatResponse;
use crate::{RebootType, Result}; use crate::{RebootType, Result};
/// Trait representing all features available on both [`crate::ADBServerDevice`] and [`crate::ADBUSBDevice`] /// Trait representing all features available on both [`crate::ADBServerDevice`] and [`crate::ADBUSBDevice`]
pub trait ADBDeviceExt { pub trait ADBDeviceExt {
/// Runs command in a shell on the device, and write its output and error streams into output. /// Runs command in a shell on the device, and write its output and error streams into output.
fn shell_command<S: ToString, W: Write>( fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()>;
&mut self,
command: impl IntoIterator<Item = S>,
output: W,
) -> Result<()>;
/// Starts an interactive shell session on the device. /// Starts an interactive shell session on the device.
/// Input data is read from reader and write to writer. /// Input data is read from reader and write to writer.
/// W has a 'static bound as it is internally used in a thread. fn shell(&mut self, reader: &mut dyn Read, writer: Box<(dyn Write + Send)>) -> Result<()>;
fn shell<R: Read, W: Write + Send + 'static>(&mut self, reader: R, writer: W) -> Result<()>;
/// Display the stat information for a remote file /// Display the stat information for a remote file
fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse>; fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse>;
/// Pull the remote file pointed to by `source` and write its contents into `output` /// Pull the remote file pointed to by `source` and write its contents into `output`
fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()>; fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()>;
/// Push `stream` to `path` on the device. /// Push `stream` to `path` on the device.
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()>; fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()>;
/// Reboot the device using given reboot type /// Reboot the device using given reboot type
fn reboot(&mut self, reboot_type: RebootType) -> Result<()>; fn reboot(&mut self, reboot_type: RebootType) -> Result<()>;
@@ -34,7 +31,7 @@ pub trait ADBDeviceExt {
fn run_activity(&mut self, package: &str, activity: &str) -> Result<Vec<u8>> { fn run_activity(&mut self, package: &str, activity: &str) -> Result<Vec<u8>> {
let mut output = Vec::new(); let mut output = Vec::new();
self.shell_command( self.shell_command(
["am", "start", &format!("{package}/{package}.{activity}")], &["am", "start", &format!("{package}/{package}.{activity}")],
&mut output, &mut output,
)?; )?;
@@ -42,5 +39,35 @@ pub trait ADBDeviceExt {
} }
/// Install an APK pointed to by `apk_path` on device. /// Install an APK pointed to by `apk_path` on device.
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()>; fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()>;
/// Inner method requesting framebuffer from an Android device
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>>;
/// Dump framebuffer of this device into given path
fn framebuffer(&mut self, path: &dyn AsRef<Path>) -> Result<()> {
// Big help from AOSP source code (<https://android.googlesource.com/platform/system/adb/+/refs/heads/main/framebuffer_service.cpp>)
let img = self.framebuffer_inner()?;
Ok(img.save(path.as_ref())?)
}
/// Dump framebuffer of this device and return corresponding bytes.
///
/// Output data format is currently only `PNG`.
fn framebuffer_bytes(&mut self) -> Result<Vec<u8>> {
let img = self.framebuffer_inner()?;
let mut vec = Cursor::new(Vec::new());
img.write_to(&mut vec, ImageFormat::Png)?;
Ok(vec.into_inner())
}
/// Return a boxed instance representing this trait
fn boxed(self) -> Box<dyn ADBDeviceExt>
where
Self: Sized,
Self: 'static,
{
Box::new(self)
}
} }

View File

@@ -11,12 +11,18 @@ use super::{models::MessageSubcommand, ADBTransportMessage, MessageCommand};
#[derive(Debug)] #[derive(Debug)]
pub struct ADBMessageDevice<T: ADBMessageTransport> { pub struct ADBMessageDevice<T: ADBMessageTransport> {
transport: T, transport: T,
local_id: Option<u32>,
remote_id: Option<u32>,
} }
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
/// Instantiate a new [`ADBMessageTransport`] /// Instantiate a new [`ADBMessageTransport`]
pub fn new(transport: T) -> Self { pub fn new(transport: T) -> Self {
Self { transport } Self {
transport,
local_id: None,
remote_id: None,
}
} }
pub(crate) fn get_transport(&mut self) -> &T { pub(crate) fn get_transport(&mut self) -> &T {
@@ -28,17 +34,13 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
} }
/// Receive a message and acknowledge it by replying with an `OKAY` command /// Receive a message and acknowledge it by replying with an `OKAY` command
pub(crate) fn recv_and_reply_okay( pub(crate) fn recv_and_reply_okay(&mut self) -> Result<ADBTransportMessage> {
&mut self,
local_id: u32,
remote_id: u32,
) -> Result<ADBTransportMessage> {
let message = self.transport.read_message()?; let message = self.transport.read_message()?;
self.transport.write_message(ADBTransportMessage::new( self.transport.write_message(ADBTransportMessage::new(
MessageCommand::Okay, MessageCommand::Okay,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
"".into(), &[],
))?; ))?;
Ok(message) Ok(message)
} }
@@ -49,28 +51,20 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
message: ADBTransportMessage, message: ADBTransportMessage,
) -> Result<ADBTransportMessage> { ) -> Result<ADBTransportMessage> {
self.transport.write_message(message)?; self.transport.write_message(message)?;
let message = self.transport.read_message()?;
let received_command = message.header().command(); self.transport.read_message().and_then(|message| {
if received_command != MessageCommand::Okay { message.assert_command(MessageCommand::Okay)?;
return Err(RustADBError::ADBRequestFailed(format!( Ok(message)
"expected command OKAY after message, got {}", })
received_command
)));
}
Ok(message)
} }
pub(crate) fn recv_file<W: std::io::Write>( pub(crate) fn recv_file<W: std::io::Write>(
&mut self, &mut self,
local_id: u32,
remote_id: u32,
mut output: W, mut output: W,
) -> std::result::Result<(), RustADBError> { ) -> std::result::Result<(), RustADBError> {
let mut len: Option<u64> = None; let mut len: Option<u64> = None;
loop { loop {
let payload = self let payload = self.recv_and_reply_okay()?.into_payload();
.recv_and_reply_okay(local_id, remote_id)?
.into_payload();
let mut rdr = Cursor::new(&payload); let mut rdr = Cursor::new(&payload);
while rdr.position() != payload.len() as u64 { while rdr.position() != payload.len() as u64 {
match len.take() { match len.take() {
@@ -119,7 +113,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
MessageCommand::Write, MessageCommand::Write,
local_id, local_id,
remote_id, remote_id,
serialized_message, &serialized_message,
); );
self.send_and_expect_okay(message)?; self.send_and_expect_okay(message)?;
@@ -139,7 +133,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
MessageCommand::Write, MessageCommand::Write,
local_id, local_id,
remote_id, remote_id,
serialized_message, &serialized_message,
); );
self.send_and_expect_okay(message)?; self.send_and_expect_okay(message)?;
@@ -167,7 +161,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
MessageCommand::Write, MessageCommand::Write,
local_id, local_id,
remote_id, remote_id,
serialized_message, &serialized_message,
); );
self.send_and_expect_okay(message)?; self.send_and_expect_okay(message)?;
@@ -179,41 +173,25 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
} }
} }
pub(crate) fn begin_synchronization(&mut self) -> Result<(u32, u32)> { pub(crate) fn begin_synchronization(&mut self) -> Result<()> {
let sync_directive = "sync:\0"; self.open_session(b"sync:\0")?;
Ok(())
let mut rng = rand::thread_rng();
let message = ADBTransportMessage::new(
MessageCommand::Open,
rng.gen(), /* Our 'local-id' */
0,
sync_directive.into(),
);
let message = self.send_and_expect_okay(message)?;
let local_id = message.header().arg1();
let remote_id = message.header().arg0();
Ok((local_id, remote_id))
} }
pub(crate) fn stat_with_explicit_ids( pub(crate) fn stat_with_explicit_ids(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
&mut self,
remote_path: &str,
local_id: u32,
remote_id: u32,
) -> Result<AdbStatResponse> {
let stat_buffer = MessageSubcommand::Stat.with_arg(remote_path.len() as u32); let stat_buffer = MessageSubcommand::Stat.with_arg(remote_path.len() as u32);
let message = ADBTransportMessage::new( let message = ADBTransportMessage::new(
MessageCommand::Write, MessageCommand::Write,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
bincode::serialize(&stat_buffer).map_err(|_e| RustADBError::ConversionError)?, &bincode::serialize(&stat_buffer).map_err(|_e| RustADBError::ConversionError)?,
); );
self.send_and_expect_okay(message)?; self.send_and_expect_okay(message)?;
self.send_and_expect_okay(ADBTransportMessage::new( self.send_and_expect_okay(ADBTransportMessage::new(
MessageCommand::Write, MessageCommand::Write,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
remote_path.into(), remote_path.as_bytes(),
))?; ))?;
let response = self.transport.read_message()?; let response = self.transport.read_message()?;
// Skip first 4 bytes as this is the literal "STAT". // Skip first 4 bytes as this is the literal "STAT".
@@ -222,15 +200,46 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
.map_err(|_e| RustADBError::ConversionError) .map_err(|_e| RustADBError::ConversionError)
} }
pub(crate) fn end_transaction(&mut self, local_id: u32, remote_id: u32) -> Result<()> { pub(crate) fn end_transaction(&mut self) -> Result<()> {
let quit_buffer = MessageSubcommand::Quit.with_arg(0u32); let quit_buffer = MessageSubcommand::Quit.with_arg(0u32);
self.send_and_expect_okay(ADBTransportMessage::new( self.send_and_expect_okay(ADBTransportMessage::new(
MessageCommand::Write, MessageCommand::Write,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
bincode::serialize(&quit_buffer).map_err(|_e| RustADBError::ConversionError)?, &bincode::serialize(&quit_buffer).map_err(|_e| RustADBError::ConversionError)?,
))?; ))?;
let _discard_close = self.transport.read_message()?; let _discard_close = self.transport.read_message()?;
Ok(()) Ok(())
} }
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBTransportMessage> {
let mut rng = rand::thread_rng();
let message = ADBTransportMessage::new(
MessageCommand::Open,
rng.gen(), // Our 'local-id'
0,
data,
);
self.get_transport_mut().write_message(message)?;
let response = self.get_transport_mut().read_message()?;
self.local_id = Some(response.header().arg1());
self.remote_id = Some(response.header().arg0());
Ok(response)
}
pub(crate) fn get_local_id(&self) -> Result<u32> {
self.local_id.ok_or(RustADBError::ADBRequestFailed(
"connection not opened, no local_id".into(),
))
}
pub(crate) fn get_remote_id(&self) -> Result<u32> {
self.remote_id.ok_or(RustADBError::ADBRequestFailed(
"connection not opened, no remote_id".into(),
))
}
} }

View File

@@ -1,18 +1,17 @@
use crate::{models::AdbStatResponse, ADBDeviceExt, ADBMessageTransport, RebootType, Result}; use crate::{models::AdbStatResponse, ADBDeviceExt, ADBMessageTransport, RebootType, Result};
use std::io::{Read, Write}; use std::{
io::{Read, Write},
path::Path,
};
use super::ADBMessageDevice; use super::ADBMessageDevice;
impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
fn shell_command<S: ToString, W: Write>( fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
&mut self,
command: impl IntoIterator<Item = S>,
output: W,
) -> Result<()> {
self.shell_command(command, output) self.shell_command(command, output)
} }
fn shell<R: Read, W: Write + Send + 'static>(&mut self, reader: R, writer: W) -> Result<()> { fn shell(&mut self, reader: &mut dyn Read, writer: Box<(dyn Write + Send)>) -> Result<()> {
self.shell(reader, writer) self.shell(reader, writer)
} }
@@ -20,11 +19,11 @@ impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
self.stat(remote_path) self.stat(remote_path)
} }
fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> { fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()> {
self.pull(source, output) self.pull(source, output)
} }
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> { fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
self.push(stream, path) self.push(stream, path)
} }
@@ -32,7 +31,11 @@ impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
self.reboot(reboot_type) self.reboot(reboot_type)
} }
fn install<P: AsRef<std::path::Path>>(&mut self, apk_path: P) -> Result<()> { fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
self.install(apk_path) self.install(apk_path)
} }
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.framebuffer_inner()
}
} }

View File

@@ -1,10 +1,11 @@
use std::net::SocketAddr; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::{io::Read, net::SocketAddr};
use super::adb_message_device::ADBMessageDevice; use super::adb_message_device::ADBMessageDevice;
use super::models::MessageCommand; use super::models::MessageCommand;
use super::ADBTransportMessage; use super::ADBTransportMessage;
use crate::{ADBDeviceExt, ADBMessageTransport, ADBTransport, Result, RustADBError, TcpTransport}; use crate::{ADBDeviceExt, ADBMessageTransport, ADBTransport, Result, TcpTransport};
/// Represent a device reached and available over USB. /// Represent a device reached and available over USB.
#[derive(Debug)] #[derive(Debug)]
@@ -32,26 +33,18 @@ impl ADBTcpDevice {
MessageCommand::Cnxn, MessageCommand::Cnxn,
0x01000000, 0x01000000,
1048576, 1048576,
format!("host::{}\0", env!("CARGO_PKG_NAME")) format!("host::{}\0", env!("CARGO_PKG_NAME")).as_bytes(),
.as_bytes()
.to_vec(),
); );
self.get_transport_mut().write_message(message)?; self.get_transport_mut().write_message(message)?;
let message = self.get_transport_mut().read_message()?; // At this point, we should have received a STLS command indicating that the device wants to upgrade connection with TLS
self.get_transport_mut()
.read_message()
.and_then(|message| message.assert_command(MessageCommand::Stls))?;
// At this point, we should have received a STLS message self.get_transport_mut()
if message.header().command() != MessageCommand::Stls { .write_message(ADBTransportMessage::new(MessageCommand::Stls, 1, 0, &[]))?;
return Err(RustADBError::ADBRequestFailed(format!(
"Wrong command received {}",
message.header().command()
)));
};
let message = ADBTransportMessage::new(MessageCommand::Stls, 1, 0, vec![]);
self.get_transport_mut().write_message(message)?;
// Upgrade TCP connection to TLS // Upgrade TCP connection to TLS
self.get_transport_mut().upgrade_connection()?; self.get_transport_mut().upgrade_connection()?;
@@ -61,47 +54,52 @@ impl ADBTcpDevice {
Ok(()) Ok(())
} }
#[inline]
fn get_transport_mut(&mut self) -> &mut TcpTransport { fn get_transport_mut(&mut self) -> &mut TcpTransport {
self.inner.get_transport_mut() self.inner.get_transport_mut()
} }
} }
impl ADBDeviceExt for ADBTcpDevice { impl ADBDeviceExt for ADBTcpDevice {
fn shell_command<S: ToString, W: std::io::Write>( #[inline]
&mut self, fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
command: impl IntoIterator<Item = S>,
output: W,
) -> Result<()> {
self.inner.shell_command(command, output) self.inner.shell_command(command, output)
} }
fn shell<R: std::io::Read, W: std::io::Write + Send + 'static>( #[inline]
&mut self, fn shell(&mut self, reader: &mut dyn Read, writer: Box<(dyn Write + Send)>) -> Result<()> {
reader: R,
writer: W,
) -> Result<()> {
self.inner.shell(reader, writer) self.inner.shell(reader, writer)
} }
#[inline]
fn stat(&mut self, remote_path: &str) -> Result<crate::AdbStatResponse> { fn stat(&mut self, remote_path: &str) -> Result<crate::AdbStatResponse> {
self.inner.stat(remote_path) self.inner.stat(remote_path)
} }
fn pull<A: AsRef<str>, W: std::io::Write>(&mut self, source: A, output: W) -> Result<()> { #[inline]
fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()> {
self.inner.pull(source, output) self.inner.pull(source, output)
} }
fn push<R: std::io::Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> { #[inline]
fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
self.inner.push(stream, path) self.inner.push(stream, path)
} }
#[inline]
fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> { fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> {
self.inner.reboot(reboot_type) self.inner.reboot(reboot_type)
} }
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> { #[inline]
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
self.inner.install(apk_path) self.inner.install(apk_path)
} }
#[inline]
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.inner.framebuffer_inner()
}
} }
impl Drop for ADBTcpDevice { impl Drop for ADBTcpDevice {

View File

@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::RustADBError; use crate::{Result, RustADBError};
use super::models::MessageCommand; use super::models::MessageCommand;
@@ -66,16 +66,16 @@ impl ADBTransportMessageHeader {
command_u32 ^ 0xFFFFFFFF command_u32 ^ 0xFFFFFFFF
} }
pub fn as_bytes(&self) -> Result<Vec<u8>, RustADBError> { pub fn as_bytes(&self) -> Result<Vec<u8>> {
bincode::serialize(&self).map_err(|_e| RustADBError::ConversionError) bincode::serialize(&self).map_err(|_e| RustADBError::ConversionError)
} }
} }
impl ADBTransportMessage { impl ADBTransportMessage {
pub fn new(command: MessageCommand, arg0: u32, arg1: u32, data: Vec<u8>) -> Self { pub fn new(command: MessageCommand, arg0: u32, arg1: u32, data: &[u8]) -> Self {
Self { Self {
header: ADBTransportMessageHeader::new(command, arg0, arg1, &data), header: ADBTransportMessageHeader::new(command, arg0, arg1, data),
payload: data, payload: data.to_vec(),
} }
} }
@@ -88,6 +88,18 @@ impl ADBTransportMessage {
&& ADBTransportMessageHeader::compute_crc32(&self.payload) == self.header.data_crc32 && ADBTransportMessageHeader::compute_crc32(&self.payload) == self.header.data_crc32
} }
pub fn assert_command(&self, expected_command: MessageCommand) -> Result<()> {
let our_command = self.header().command();
if expected_command == our_command {
return Ok(());
}
Err(RustADBError::WrongResponseReceived(
our_command.to_string(),
expected_command.to_string(),
))
}
pub fn header(&self) -> &ADBTransportMessageHeader { pub fn header(&self) -> &ADBTransportMessageHeader {
&self.header &self.header
} }
@@ -104,7 +116,7 @@ impl ADBTransportMessage {
impl TryFrom<[u8; 24]> for ADBTransportMessageHeader { impl TryFrom<[u8; 24]> for ADBTransportMessageHeader {
type Error = RustADBError; type Error = RustADBError;
fn try_from(value: [u8; 24]) -> Result<Self, Self::Error> { fn try_from(value: [u8; 24]) -> Result<Self> {
bincode::deserialize(&value).map_err(|_e| RustADBError::ConversionError) bincode::deserialize(&value).map_err(|_e| RustADBError::ConversionError)
} }
} }

View File

@@ -1,7 +1,10 @@
use rusb::constants::LIBUSB_CLASS_VENDOR_SPEC;
use rusb::Device; use rusb::Device;
use rusb::DeviceDescriptor; use rusb::DeviceDescriptor;
use rusb::UsbContext; use rusb::UsbContext;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::io::Read;
use std::io::Write;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Duration; use std::time::Duration;
@@ -55,8 +58,6 @@ fn search_adb_devices() -> Result<Option<(u16, u16)>> {
} }
fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool { fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
const ADB_CLASS: u8 = 0xff;
const ADB_SUBCLASS: u8 = 0x42; const ADB_SUBCLASS: u8 = 0x42;
const ADB_PROTOCOL: u8 = 0x1; const ADB_PROTOCOL: u8 = 0x1;
@@ -75,7 +76,7 @@ fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> b
let class = interface_des.class_code(); let class = interface_des.class_code();
let subcl = interface_des.sub_class_code(); let subcl = interface_des.sub_class_code();
if proto == ADB_PROTOCOL if proto == ADB_PROTOCOL
&& ((class == ADB_CLASS && subcl == ADB_SUBCLASS) && ((class == LIBUSB_CLASS_VENDOR_SPEC && subcl == ADB_SUBCLASS)
|| (class == BULK_CLASS && subcl == BULK_ADB_SUBCLASS)) || (class == BULK_CLASS && subcl == BULK_ADB_SUBCLASS))
{ {
return true; return true;
@@ -173,36 +174,27 @@ impl ADBUSBDevice {
MessageCommand::Cnxn, MessageCommand::Cnxn,
0x01000000, 0x01000000,
1048576, 1048576,
format!("host::{}\0", env!("CARGO_PKG_NAME")) format!("host::{}\0", env!("CARGO_PKG_NAME")).as_bytes(),
.as_bytes()
.to_vec(),
); );
self.get_transport_mut().write_message(message)?; self.get_transport_mut().write_message(message)?;
let message = self.get_transport_mut().read_message()?; let message = self.get_transport_mut().read_message()?;
message.assert_command(MessageCommand::Auth)?;
// At this point, we should have received either: // At this point, we should have receive an AUTH message with arg0 == 1
// - an AUTH message with arg0 == 1 let auth_message = match message.header().arg0() {
// - a CNXN message AUTH_TOKEN => message,
let auth_message = match message.header().command() { v => {
MessageCommand::Auth if message.header().arg0() == AUTH_TOKEN => message,
MessageCommand::Auth if message.header().arg0() != AUTH_TOKEN => {
return Err(RustADBError::ADBRequestFailed(
"Received AUTH message with type != 1".into(),
))
}
c => {
return Err(RustADBError::ADBRequestFailed(format!( return Err(RustADBError::ADBRequestFailed(format!(
"Wrong command received {}", "Received AUTH message with type != 1 ({v})"
c
))) )))
} }
}; };
let sign = self.private_key.sign(auth_message.into_payload())?; let sign = self.private_key.sign(auth_message.into_payload())?;
let message = ADBTransportMessage::new(MessageCommand::Auth, AUTH_SIGNATURE, 0, sign); let message = ADBTransportMessage::new(MessageCommand::Auth, AUTH_SIGNATURE, 0, &sign);
self.get_transport_mut().write_message(message)?; self.get_transport_mut().write_message(message)?;
@@ -219,71 +211,72 @@ impl ADBUSBDevice {
let mut pubkey = self.private_key.android_pubkey_encode()?.into_bytes(); let mut pubkey = self.private_key.android_pubkey_encode()?.into_bytes();
pubkey.push(b'\0'); pubkey.push(b'\0');
let message = ADBTransportMessage::new(MessageCommand::Auth, AUTH_RSAPUBLICKEY, 0, pubkey); let message = ADBTransportMessage::new(MessageCommand::Auth, AUTH_RSAPUBLICKEY, 0, &pubkey);
self.get_transport_mut().write_message(message)?; self.get_transport_mut().write_message(message)?;
let response = self let response = self
.get_transport_mut() .get_transport_mut()
.read_message_with_timeout(Duration::from_secs(10))?; .read_message_with_timeout(Duration::from_secs(10))
.and_then(|message| {
message.assert_command(MessageCommand::Cnxn)?;
Ok(message)
})?;
match response.header().command() { log::info!(
MessageCommand::Cnxn => log::info!( "Authentication OK, device info {}",
"Authentication OK, device info {}", String::from_utf8(response.into_payload())?
String::from_utf8(response.into_payload())? );
),
_ => {
return Err(RustADBError::ADBRequestFailed(format!(
"wrong response {}",
response.header().command()
)))
}
}
Ok(()) Ok(())
} }
#[inline]
fn get_transport_mut(&mut self) -> &mut USBTransport { fn get_transport_mut(&mut self) -> &mut USBTransport {
self.inner.get_transport_mut() self.inner.get_transport_mut()
} }
} }
impl ADBDeviceExt for ADBUSBDevice { impl ADBDeviceExt for ADBUSBDevice {
fn shell_command<S: ToString, W: std::io::Write>( #[inline]
&mut self, fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
command: impl IntoIterator<Item = S>,
output: W,
) -> Result<()> {
self.inner.shell_command(command, output) self.inner.shell_command(command, output)
} }
fn shell<R: std::io::Read, W: std::io::Write + Send + 'static>( #[inline]
&mut self, fn shell<'a>(&mut self, reader: &mut dyn Read, writer: Box<(dyn Write + Send)>) -> Result<()> {
reader: R,
writer: W,
) -> Result<()> {
self.inner.shell(reader, writer) self.inner.shell(reader, writer)
} }
#[inline]
fn stat(&mut self, remote_path: &str) -> Result<crate::AdbStatResponse> { fn stat(&mut self, remote_path: &str) -> Result<crate::AdbStatResponse> {
self.inner.stat(remote_path) self.inner.stat(remote_path)
} }
fn pull<A: AsRef<str>, W: std::io::Write>(&mut self, source: A, output: W) -> Result<()> { #[inline]
fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()> {
self.inner.pull(source, output) self.inner.pull(source, output)
} }
fn push<R: std::io::Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> { #[inline]
fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
self.inner.push(stream, path) self.inner.push(stream, path)
} }
#[inline]
fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> { fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> {
self.inner.reboot(reboot_type) self.inner.reboot(reboot_type)
} }
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> { #[inline]
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
self.inner.install(apk_path) self.inner.install(apk_path)
} }
#[inline]
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.inner.framebuffer_inner()
}
} }
impl Drop for ADBUSBDevice { impl Drop for ADBUSBDevice {

View File

@@ -0,0 +1,98 @@
use std::io::{Cursor, Read};
use byteorder::{LittleEndian, ReadBytesExt};
use image::{ImageBuffer, Rgba};
use crate::{
device::{adb_message_device::ADBMessageDevice, MessageCommand},
models::{FrameBufferInfoV1, FrameBufferInfoV2},
ADBMessageTransport, Result, RustADBError,
};
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
self.open_session(b"framebuffer:\0")?;
let response = self.recv_and_reply_okay()?;
let mut payload_cursor = Cursor::new(response.payload());
let version = payload_cursor.read_u32::<LittleEndian>()?;
let img = match version {
// RGBA_8888
1 => {
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV1>()];
payload_cursor.read_exact(&mut buf)?;
let framebuffer_info: FrameBufferInfoV1 = buf.try_into()?;
let mut framebuffer_data = Vec::new();
payload_cursor.read_to_end(&mut framebuffer_data)?;
loop {
if framebuffer_data.len() as u32 == framebuffer_info.size {
break;
}
let response = self.recv_and_reply_okay()?;
framebuffer_data.extend_from_slice(&response.into_payload());
log::debug!(
"received framebuffer data. new size {}",
framebuffer_data.len()
);
}
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(
framebuffer_info.width,
framebuffer_info.height,
framebuffer_data,
)
.ok_or_else(|| RustADBError::FramebufferConversionError)?
}
// RGBX_8888
2 => {
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV2>()];
payload_cursor.read_exact(&mut buf)?;
let framebuffer_info: FrameBufferInfoV2 = buf.try_into()?;
let mut framebuffer_data = Vec::new();
payload_cursor.read_to_end(&mut framebuffer_data)?;
loop {
if framebuffer_data.len() as u32 == framebuffer_info.size {
break;
}
let response = self.recv_and_reply_okay()?;
framebuffer_data.extend_from_slice(&response.into_payload());
log::debug!(
"received framebuffer data. new size {}",
framebuffer_data.len()
);
}
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(
framebuffer_info.width,
framebuffer_info.height,
framebuffer_data,
)
.ok_or_else(|| RustADBError::FramebufferConversionError)?
}
v => return Err(RustADBError::UnimplementedFramebufferImageVersion(v)),
};
self.get_transport_mut()
.read_message()
.and_then(|message| message.assert_command(MessageCommand::Clse))?;
Ok(img)
}
}

View File

@@ -1,20 +1,18 @@
use std::fs::File; use std::{fs::File, path::Path};
use rand::Rng; use rand::Rng;
use crate::{ use crate::{
device::{ device::{adb_message_device::ADBMessageDevice, MessageWriter},
adb_message_device::ADBMessageDevice, ADBTransportMessage, MessageCommand, MessageWriter,
},
utils::check_extension_is_apk, utils::check_extension_is_apk,
ADBMessageTransport, Result, ADBMessageTransport, Result,
}; };
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn install<P: AsRef<std::path::Path>>(&mut self, apk_path: P) -> Result<()> { pub(crate) fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
let mut apk_file = File::open(&apk_path)?; let mut apk_file = File::open(apk_path)?;
check_extension_is_apk(&apk_path)?; check_extension_is_apk(apk_path)?;
let file_size = apk_file.metadata()?.len(); let file_size = apk_file.metadata()?.len();
@@ -22,22 +20,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
let local_id = rng.gen(); let local_id = rng.gen();
let message = ADBTransportMessage::new( self.open_session(format!("exec:cmd package 'install' -S {}\0", file_size).as_bytes())?;
MessageCommand::Open,
local_id,
0,
format!("exec:cmd package 'install' -S {}\0", file_size)
.as_bytes()
.to_vec(),
);
self.get_transport_mut().write_message(message)?;
let response = self.get_transport_mut().read_message()?;
let remote_id = response.header().arg0();
let transport = self.get_transport().clone(); let transport = self.get_transport().clone();
let mut writer = MessageWriter::new(transport, local_id, remote_id); let mut writer = MessageWriter::new(transport, local_id, self.get_remote_id()?);
std::io::copy(&mut apk_file, &mut writer)?; std::io::copy(&mut apk_file, &mut writer)?;

View File

@@ -1,3 +1,4 @@
mod framebuffer;
mod install; mod install;
mod pull; mod pull;
mod push; mod push;

View File

@@ -10,10 +10,10 @@ use crate::{
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> { pub(crate) fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> {
let (local_id, remote_id) = self.begin_synchronization()?; self.begin_synchronization()?;
let source = source.as_ref(); let source = source.as_ref();
let adb_stat_response = self.stat_with_explicit_ids(source, local_id, remote_id)?; let adb_stat_response = self.stat_with_explicit_ids(source)?;
if adb_stat_response.file_perm == 0 { if adb_stat_response.file_perm == 0 {
return Err(RustADBError::UnknownResponseType( return Err(RustADBError::UnknownResponseType(
@@ -21,8 +21,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
)); ));
} }
let local_id = self.get_local_id()?;
let remote_id = self.get_remote_id()?;
self.get_transport_mut().write_message_with_timeout( self.get_transport_mut().write_message_with_timeout(
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, "".into()), ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]),
std::time::Duration::from_secs(4), std::time::Duration::from_secs(4),
)?; )?;
@@ -31,19 +34,19 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?; bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?;
self.send_and_expect_okay(ADBTransportMessage::new( self.send_and_expect_okay(ADBTransportMessage::new(
MessageCommand::Write, MessageCommand::Write,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
recv_buffer, &recv_buffer,
))?; ))?;
self.send_and_expect_okay(ADBTransportMessage::new( self.send_and_expect_okay(ADBTransportMessage::new(
MessageCommand::Write, MessageCommand::Write,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
source.into(), source.as_bytes(),
))?; ))?;
self.recv_file(local_id, remote_id, output)?; self.recv_file(output)?;
self.end_transaction(local_id, remote_id)?; self.end_transaction()?;
Ok(()) Ok(())
} }
} }

View File

@@ -10,7 +10,7 @@ use crate::{
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> { pub(crate) fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
let (local_id, remote_id) = self.begin_synchronization()?; self.begin_synchronization()?;
let path_header = format!("{},0777", path.as_ref()); let path_header = format!("{},0777", path.as_ref());
@@ -21,14 +21,14 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
self.send_and_expect_okay(ADBTransportMessage::new( self.send_and_expect_okay(ADBTransportMessage::new(
MessageCommand::Write, MessageCommand::Write,
local_id, self.get_local_id()?,
remote_id, self.get_remote_id()?,
send_buffer, &send_buffer,
))?; ))?;
self.push_file(local_id, remote_id, stream)?; self.push_file(self.get_local_id()?, self.get_remote_id()?, stream)?;
self.end_transaction(local_id, remote_id)?; self.end_transaction()?;
Ok(()) Ok(())
} }

View File

@@ -1,28 +1,14 @@
use rand::Rng;
use crate::{ use crate::{
device::{adb_message_device::ADBMessageDevice, ADBTransportMessage, MessageCommand}, device::{adb_message_device::ADBMessageDevice, MessageCommand},
ADBMessageTransport, RebootType, Result, RustADBError, ADBMessageTransport, RebootType, Result,
}; };
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn reboot(&mut self, reboot_type: RebootType) -> Result<()> { pub(crate) fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
let mut rng = rand::thread_rng(); self.open_session(format!("reboot:{}\0", reboot_type).as_bytes())?;
let message = ADBTransportMessage::new( self.get_transport_mut()
MessageCommand::Open, .read_message()
rng.gen(), // Our 'local-id' .and_then(|message| message.assert_command(MessageCommand::Okay))
0,
format!("reboot:{}\0", reboot_type).as_bytes().to_vec(),
);
self.get_transport_mut().write_message(message)?;
let message = self.get_transport_mut().read_message()?;
if message.header().command() != MessageCommand::Okay {
return Err(RustADBError::ADBShellNotSupported);
}
Ok(())
} }
} }

View File

@@ -1,5 +1,4 @@
use rand::Rng; use std::io::{ErrorKind, Read, Write};
use std::io::{Read, Write};
use crate::device::ShellMessageWriter; use crate::device::ShellMessageWriter;
use crate::Result; use crate::Result;
@@ -9,30 +8,10 @@ use crate::{
}; };
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
/// Runs 'command' in a shell on the device, and write its output and error streams into [`output`]. /// Runs 'command' in a shell on the device, and write its output and error streams into output.
pub(crate) fn shell_command<S: ToString, W: Write>( pub(crate) fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
&mut self, let response = self.open_session(format!("shell:{}\0", command.join(" "),).as_bytes())?;
command: impl IntoIterator<Item = S>,
mut output: W,
) -> Result<()> {
let message = ADBTransportMessage::new(
MessageCommand::Open,
1,
0,
format!(
"shell:{}\0",
command
.into_iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" "),
)
.as_bytes()
.to_vec(),
);
self.get_transport_mut().write_message(message)?;
let response = self.get_transport_mut().read_message()?;
if response.header().command() != MessageCommand::Okay { if response.header().command() != MessageCommand::Okay {
return Err(RustADBError::ADBRequestFailed(format!( return Err(RustADBError::ADBRequestFailed(format!(
"wrong command {}", "wrong command {}",
@@ -54,27 +33,18 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
/// Starts an interactive shell session on the device. /// Starts an interactive shell session on the device.
/// Input data is read from [reader] and write to [writer]. /// Input data is read from [reader] and write to [writer].
/// [W] has a 'static bound as it is internally used in a thread. pub(crate) fn shell(
pub(crate) fn shell<R: Read, W: Write + Send + 'static>(
&mut self, &mut self,
mut reader: R, mut reader: &mut dyn Read,
mut writer: W, mut writer: Box<(dyn Write + Send)>,
) -> Result<()> { ) -> Result<()> {
let sync_directive = "shell:\0"; self.open_session(b"shell:\0")?;
let mut rng = rand::thread_rng();
let message = ADBTransportMessage::new(
MessageCommand::Open,
rng.gen(), /* Our 'local-id' */
0,
sync_directive.into(),
);
let message = self.send_and_expect_okay(message)?;
let local_id = message.header().arg1();
let remote_id = message.header().arg0();
let mut transport = self.get_transport().clone(); let mut transport = self.get_transport().clone();
let local_id = self.get_local_id()?;
let remote_id = self.get_remote_id()?;
// Reading thread, reads response from adbd // Reading thread, reads response from adbd
std::thread::spawn(move || -> Result<()> { std::thread::spawn(move || -> Result<()> {
loop { loop {
@@ -82,17 +52,17 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
// Acknowledge for more data // Acknowledge for more data
let response = let response =
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, vec![]); ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]);
transport.write_message(response)?; transport.write_message(response)?;
match message.header().command() { match message.header().command() {
MessageCommand::Write => {} MessageCommand::Write => {
writer.write_all(&message.into_payload())?;
writer.flush()?;
}
MessageCommand::Okay => continue, MessageCommand::Okay => continue,
_ => return Err(RustADBError::ADBShellNotSupported), _ => return Err(RustADBError::ADBShellNotSupported),
} }
writer.write_all(&message.into_payload())?;
writer.flush()?;
} }
}); });
@@ -102,7 +72,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
// Read from given reader (that could be stdin e.g), and write content to device adbd // Read from given reader (that could be stdin e.g), and write content to device adbd
if let Err(e) = std::io::copy(&mut reader, &mut shell_writer) { if let Err(e) = std::io::copy(&mut reader, &mut shell_writer) {
match e.kind() { match e.kind() {
std::io::ErrorKind::BrokenPipe => return Ok(()), ErrorKind::BrokenPipe => return Ok(()),
_ => return Err(RustADBError::IOError(e)), _ => return Err(RustADBError::IOError(e)),
} }
} }

View File

@@ -4,9 +4,9 @@ use crate::{
impl<T: ADBMessageTransport> ADBMessageDevice<T> { impl<T: ADBMessageTransport> ADBMessageDevice<T> {
pub(crate) fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> { pub(crate) fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
let (local_id, remote_id) = self.begin_synchronization()?; self.begin_synchronization()?;
let adb_stat_response = self.stat_with_explicit_ids(remote_path, local_id, remote_id)?; let adb_stat_response = self.stat_with_explicit_ids(remote_path)?;
self.end_transaction(local_id, remote_id)?; self.end_transaction()?;
Ok(adb_stat_response) Ok(adb_stat_response)
} }
} }

View File

@@ -1,4 +1,4 @@
use std::io::{ErrorKind, Write}; use std::io::{Error, ErrorKind, Result, Write};
use crate::ADBMessageTransport; use crate::ADBMessageTransport;
@@ -24,30 +24,25 @@ impl<T: ADBMessageTransport> MessageWriter<T> {
} }
impl<T: ADBMessageTransport> Write for MessageWriter<T> { impl<T: ADBMessageTransport> Write for MessageWriter<T> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> Result<usize> {
let message = ADBTransportMessage::new( let message =
MessageCommand::Write, ADBTransportMessage::new(MessageCommand::Write, self.local_id, self.remote_id, buf);
self.local_id,
self.remote_id,
buf.to_vec(),
);
self.transport self.transport
.write_message(message) .write_message(message)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; .map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
match self.transport.read_message() { match self.transport.read_message() {
Ok(response) => match response.header().command() { Ok(response) => {
MessageCommand::Okay => Ok(buf.len()), response
c => Err(std::io::Error::new( .assert_command(MessageCommand::Okay)
ErrorKind::Other, .map_err(|e| Error::new(ErrorKind::Other, e))?;
format!("wrong response received: {c}"), Ok(buf.len())
)), }
}, Err(e) => Err(Error::new(ErrorKind::Other, e)),
Err(e) => Err(std::io::Error::new(ErrorKind::Other, e)),
} }
} }
fn flush(&mut self) -> std::io::Result<()> { fn flush(&mut self) -> Result<()> {
Ok(()) Ok(())
} }
} }

View File

@@ -6,18 +6,17 @@ use std::fmt::Display;
#[repr(u32)] #[repr(u32)]
pub enum MessageCommand { pub enum MessageCommand {
/// Connect to a device /// Connect to a device
Cnxn = 0x4e584e43, Cnxn = 0x4E584E43,
/// Close connection to a device /// Close connection to a device
Clse = 0x45534c43, Clse = 0x45534C43,
/// Device ask for authentication /// Device ask for authentication
Auth = 0x48545541, Auth = 0x48545541,
/// Open a data connection /// Open a data connection
Open = 0x4e45504f, Open = 0x4E45504F,
/// Write data to connection /// Write data to connection
Write = 0x45545257, Write = 0x45545257,
/// Server understood the message /// Server understood the message
Okay = 0x59414b4f, Okay = 0x59414B4F,
// Sync 0x434e5953
/// Start a connection using TLS /// Start a connection using TLS
Stls = 0x534C5453, Stls = 0x534C5453,
} }
@@ -29,10 +28,10 @@ pub enum MessageSubcommand {
Send = 0x444E4553, Send = 0x444E4553,
Recv = 0x56434552, Recv = 0x56434552,
Quit = 0x54495551, Quit = 0x54495551,
Fail = 0x4c494146, Fail = 0x4C494146,
Done = 0x454e4f44, Done = 0x454E4F44,
Data = 0x41544144, Data = 0x41544144,
List = 0x5453494c, List = 0x5453494C,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]

View File

@@ -23,12 +23,8 @@ impl<T: ADBMessageTransport> ShellMessageWriter<T> {
impl<T: ADBMessageTransport> Write for ShellMessageWriter<T> { impl<T: ADBMessageTransport> Write for ShellMessageWriter<T> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let message = ADBTransportMessage::new( let message =
MessageCommand::Write, ADBTransportMessage::new(MessageCommand::Write, self.local_id, self.remote_id, buf);
self.local_id,
self.remote_id,
buf.to_vec(),
);
self.transport self.transport
.write_message(message) .write_message(message)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;

View File

@@ -67,7 +67,7 @@ impl TryFrom<ADBServerDevice> for ADBEmulatorDevice {
fn try_from(value: ADBServerDevice) -> std::result::Result<Self, Self::Error> { fn try_from(value: ADBServerDevice) -> std::result::Result<Self, Self::Error> {
ADBEmulatorDevice::new( ADBEmulatorDevice::new(
value.identifier.clone(), value.identifier.clone(),
Some(*value.get_transport().get_socketaddr().ip()), Some(*value.transport.get_socketaddr().ip()),
) )
} }
} }

View File

@@ -1,10 +1,8 @@
use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result}; use crate::{emulator_device::ADBEmulatorCommand, ADBEmulatorDevice, Result};
impl ADBEmulatorDevice { impl ADBEmulatorDevice {
/// Send a SMS to this emulator with given content with given phone number /// Send a SMS to this emulator with given content with given phone number
pub fn rotate(&mut self) -> Result<()> { pub fn rotate(&mut self) -> Result<()> {
let transport = self.connect()?; self.connect()?.send_command(ADBEmulatorCommand::Rotate)
transport.send_command(ADBEmulatorCommand::Rotate)?;
Ok(())
} }
} }

View File

@@ -1,13 +1,11 @@
use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result}; use crate::{emulator_device::ADBEmulatorCommand, ADBEmulatorDevice, Result};
impl ADBEmulatorDevice { impl ADBEmulatorDevice {
/// Send a SMS to this emulator with given content with given phone number /// Send a SMS to this emulator with given content with given phone number
pub fn send_sms(&mut self, phone_number: &str, content: &str) -> Result<()> { pub fn send_sms(&mut self, phone_number: &str, content: &str) -> Result<()> {
let transport = self.connect()?; self.connect()?.send_command(ADBEmulatorCommand::Sms(
transport.send_command(ADBEmulatorCommand::Sms(
phone_number.to_string(), phone_number.to_string(),
content.to_string(), content.to_string(),
))?; ))
Ok(())
} }
} }

View File

@@ -1,3 +1,5 @@
mod adb_emulator_device; mod adb_emulator_device;
mod commands; mod commands;
mod models;
pub use adb_emulator_device::ADBEmulatorDevice; pub use adb_emulator_device::ADBEmulatorDevice;
pub(crate) use models::ADBEmulatorCommand;

View File

@@ -1,6 +1,6 @@
use std::fmt::Display; use std::fmt::Display;
pub(crate) enum ADBEmulatorCommand { pub enum ADBEmulatorCommand {
Authenticate(String), Authenticate(String),
Sms(String, String), Sms(String, String),
Rotate, Rotate,

View File

@@ -0,0 +1,2 @@
mod adb_emulator_command;
pub use adb_emulator_command::ADBEmulatorCommand;

View File

@@ -15,6 +15,9 @@ pub enum RustADBError {
/// Indicates that ADB server responded an unknown response type. /// Indicates that ADB server responded an unknown response type.
#[error("Unknown response type {0}")] #[error("Unknown response type {0}")]
UnknownResponseType(String), UnknownResponseType(String),
/// Indicated that an unexpected command has been received
#[error("Wrong response command received: {0}. Expected {1}")]
WrongResponseReceived(String, String),
/// Indicates that ADB server responses an unknown device state. /// Indicates that ADB server responses an unknown device state.
#[error("Unknown device state {0}")] #[error("Unknown device state {0}")]
UnknownDeviceState(String), UnknownDeviceState(String),
@@ -64,7 +67,7 @@ pub enum RustADBError {
#[error("Cannot get home directory")] #[error("Cannot get home directory")]
NoHomeDirectory, NoHomeDirectory,
/// Generic USB error /// Generic USB error
#[error(transparent)] #[error("USB Error: {0}")]
UsbError(#[from] rusb::Error), UsbError(#[from] rusb::Error),
/// USB device not found /// USB device not found
#[error("USB Device not found: {0} {1}")] #[error("USB Device not found: {0} {1}")]

View File

@@ -21,9 +21,7 @@ pub use device::{ADBTcpDevice, ADBUSBDevice};
pub use emulator_device::ADBEmulatorDevice; pub use emulator_device::ADBEmulatorDevice;
pub use error::{Result, RustADBError}; pub use error::{Result, RustADBError};
pub use mdns::*; pub use mdns::*;
pub use models::{ pub use models::{AdbStatResponse, RebootType};
AdbStatResponse, AdbVersion, DeviceLong, DeviceShort, DeviceState, MDNSBackend, RebootType,
};
pub use server::*; pub use server::*;
pub use server_device::ADBServerDevice; pub use server_device::ADBServerDevice;
pub use transports::*; pub use transports::*;

View File

@@ -0,0 +1,97 @@
use std::{iter::Map, slice::ChunksExact};
use byteorder::{ByteOrder, LittleEndian};
use crate::{Result, RustADBError};
type U32ChunkIter<'a> = Map<ChunksExact<'a, u8>, fn(&[u8]) -> Result<u32>>;
fn read_next(chunks: &mut U32ChunkIter) -> Result<u32> {
chunks
.next()
.ok_or(RustADBError::FramebufferConversionError)?
}
#[derive(Debug)]
pub(crate) struct FrameBufferInfoV1 {
pub _bpp: u32,
pub size: u32,
pub width: u32,
pub height: u32,
pub _red_offset: u32,
pub _red_length: u32,
pub _blue_offset: u32,
pub _blue_length: u32,
pub _green_offset: u32,
pub _green_length: u32,
pub _alpha_offset: u32,
pub _alpha_length: u32,
}
impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV1 {
type Error = RustADBError;
fn try_from(
value: [u8; std::mem::size_of::<Self>()],
) -> std::result::Result<Self, Self::Error> {
let mut chunks: U32ChunkIter = value.chunks_exact(4).map(|v| Ok(LittleEndian::read_u32(v)));
Ok(Self {
_bpp: read_next(&mut chunks)?,
size: read_next(&mut chunks)?,
width: read_next(&mut chunks)?,
height: read_next(&mut chunks)?,
_red_offset: read_next(&mut chunks)?,
_red_length: read_next(&mut chunks)?,
_blue_offset: read_next(&mut chunks)?,
_blue_length: read_next(&mut chunks)?,
_green_offset: read_next(&mut chunks)?,
_green_length: read_next(&mut chunks)?,
_alpha_offset: read_next(&mut chunks)?,
_alpha_length: read_next(&mut chunks)?,
})
}
}
#[derive(Debug)]
pub(crate) struct FrameBufferInfoV2 {
pub _bpp: u32,
pub _color_space: u32,
pub size: u32,
pub width: u32,
pub height: u32,
pub _red_offset: u32,
pub _red_length: u32,
pub _blue_offset: u32,
pub _blue_length: u32,
pub _green_offset: u32,
pub _green_length: u32,
pub _alpha_offset: u32,
pub _alpha_length: u32,
}
impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV2 {
type Error = RustADBError;
fn try_from(
value: [u8; std::mem::size_of::<Self>()],
) -> std::result::Result<Self, Self::Error> {
let mut chunks: U32ChunkIter = value.chunks_exact(4).map(|v| Ok(LittleEndian::read_u32(v)));
Ok(Self {
_bpp: read_next(&mut chunks)?,
_color_space: read_next(&mut chunks)?,
size: read_next(&mut chunks)?,
width: read_next(&mut chunks)?,
height: read_next(&mut chunks)?,
_red_offset: read_next(&mut chunks)?,
_red_length: read_next(&mut chunks)?,
_blue_offset: read_next(&mut chunks)?,
_blue_length: read_next(&mut chunks)?,
_green_offset: read_next(&mut chunks)?,
_green_length: read_next(&mut chunks)?,
_alpha_offset: read_next(&mut chunks)?,
_alpha_length: read_next(&mut chunks)?,
})
}
}

View File

@@ -1,28 +1,15 @@
mod adb_emulator_command;
mod adb_request_status; mod adb_request_status;
mod adb_server_command; mod adb_server_command;
mod adb_stat_response; mod adb_stat_response;
mod adb_version; mod framebuffer_info;
mod device_long;
mod device_short;
mod device_state;
mod host_features; mod host_features;
mod mdns_services;
mod reboot_type; mod reboot_type;
mod server_status;
mod sync_command; mod sync_command;
pub(crate) use adb_emulator_command::ADBEmulatorCommand;
pub use adb_request_status::AdbRequestStatus; pub use adb_request_status::AdbRequestStatus;
pub(crate) use adb_server_command::AdbServerCommand; pub(crate) use adb_server_command::AdbServerCommand;
pub use adb_stat_response::AdbStatResponse; pub use adb_stat_response::AdbStatResponse;
pub use adb_version::AdbVersion; pub(crate) use framebuffer_info::{FrameBufferInfoV1, FrameBufferInfoV2};
pub use device_long::DeviceLong;
pub use device_short::DeviceShort;
pub use device_state::DeviceState;
pub use host_features::HostFeatures; pub use host_features::HostFeatures;
pub use mdns_services::MDNSServices;
pub use reboot_type::RebootType; pub use reboot_type::RebootType;
pub use server_status::MDNSBackend;
pub use server_status::ServerStatus;
pub use sync_command::SyncCommand; pub use sync_command::SyncCommand;

View File

@@ -1,8 +1,8 @@
use std::io::Read; use std::io::Read;
use crate::{ use crate::{
models::{AdbServerCommand, DeviceShort}, models::AdbServerCommand, ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong,
ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong, Result, RustADBError, DeviceShort, Result, RustADBError,
}; };
impl ADBServer { impl ADBServer {
@@ -97,7 +97,12 @@ impl ADBServer {
.get_raw_connection()? .get_raw_connection()?
.read_exact(&mut body)?; .read_exact(&mut body)?;
callback(DeviceShort::try_from(body)?)?; for device in body.split(|x| x.eq(&b'\n')) {
if device.is_empty() {
break;
}
callback(DeviceShort::try_from(device.to_vec())?)?;
}
} }
} }
} }

View File

@@ -1,8 +1,7 @@
use std::io::BufRead; use std::io::BufRead;
use crate::{ use crate::{
models::{AdbServerCommand, MDNSBackend, MDNSServices}, models::AdbServerCommand, server::models::MDNSBackend, ADBServer, MDNSServices, Result,
ADBServer, Result,
}; };
const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN"; const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN";

View File

@@ -1,7 +1,4 @@
use crate::{ use crate::{models::AdbServerCommand, server::models::ServerStatus, ADBServer, Result};
models::{AdbServerCommand, ServerStatus},
ADBServer, Result,
};
impl ADBServer { impl ADBServer {
/// Check ADB server status /// Check ADB server status

View File

@@ -1,4 +1,6 @@
mod adb_server; mod adb_server;
mod commands; mod commands;
mod models;
pub use adb_server::ADBServer; pub use adb_server::ADBServer;
pub use models::*;

View File

@@ -0,0 +1,13 @@
mod adb_version;
mod device_long;
mod device_short;
mod device_state;
mod mdns_services;
mod server_status;
pub use adb_version::AdbVersion;
pub use device_long::DeviceLong;
pub use device_short::DeviceShort;
pub use device_state::DeviceState;
pub use mdns_services::MDNSServices;
pub use server_status::{MDNSBackend, ServerStatus};

View File

@@ -91,14 +91,23 @@ impl Display for MDNSBackend {
/// Structure representing current server status /// Structure representing current server status
#[derive(Debug, Clone, Default, PartialEq)] #[derive(Debug, Clone, Default, PartialEq)]
pub struct ServerStatus { pub struct ServerStatus {
/// Currently active USB backend
pub usb_backend: UsbBackend, pub usb_backend: UsbBackend,
/// Is USB backend forced ?
pub usb_backend_forced: bool, pub usb_backend_forced: bool,
/// Currently active MDNS backend
pub mdns_backend: MDNSBackend, pub mdns_backend: MDNSBackend,
/// Is MDNS backend forced ?
pub mdns_backend_forced: bool, pub mdns_backend_forced: bool,
/// Server version
pub version: String, pub version: String,
/// Server build information
pub build: String, pub build: String,
/// Server executable absolute path
pub executable_absolute_path: String, pub executable_absolute_path: String,
/// Server logs absolute path
pub log_absolute_path: String, pub log_absolute_path: String,
/// OS server is running on
pub os: String, pub os: String,
} }

View File

@@ -7,7 +7,7 @@ pub struct ADBServerDevice {
/// Unique device identifier. /// Unique device identifier.
pub identifier: String, pub identifier: String,
/// Internal [TCPServerTransport] /// Internal [TCPServerTransport]
transport: TCPServerTransport, pub(crate) transport: TCPServerTransport,
} }
impl ADBServerDevice { impl ADBServerDevice {
@@ -25,19 +25,11 @@ impl ADBServerDevice {
} }
} }
pub(crate) fn get_transport(&self) -> &TCPServerTransport {
&self.transport
}
pub(crate) fn get_transport_mut(&mut self) -> &mut TCPServerTransport {
&mut self.transport
}
/// Connect to underlying transport /// Connect to underlying transport
pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> { pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> {
self.transport.connect()?; self.transport.connect()?;
Ok(self.get_transport_mut()) Ok(&mut self.transport)
} }
} }

View File

@@ -1,5 +1,5 @@
use std::{ use std::{
io::{Read, Write}, io::{ErrorKind, Read, Write},
path::Path, path::Path,
}; };
@@ -12,11 +12,7 @@ use crate::{
use super::ADBServerDevice; use super::ADBServerDevice;
impl ADBDeviceExt for ADBServerDevice { impl ADBDeviceExt for ADBServerDevice {
fn shell_command<S: ToString, W: Write>( fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
&mut self,
command: impl IntoIterator<Item = S>,
mut output: W,
) -> Result<()> {
let supported_features = self.host_features()?; let supported_features = self.host_features()?;
if !supported_features.contains(&HostFeatures::ShellV2) if !supported_features.contains(&HostFeatures::ShellV2)
&& !supported_features.contains(&HostFeatures::Cmd) && !supported_features.contains(&HostFeatures::Cmd)
@@ -27,23 +23,12 @@ impl ADBDeviceExt for ADBServerDevice {
let serial = self.identifier.clone(); let serial = self.identifier.clone();
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.send_adb_request(AdbServerCommand::ShellCommand( .send_adb_request(AdbServerCommand::ShellCommand(command.join(" ")))?;
command
.into_iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" "),
))?;
const BUFFER_SIZE: usize = 4096;
loop { loop {
let mut buffer = [0; BUFFER_SIZE]; let mut buffer = [0; BUFFER_SIZE];
match self match self.transport.get_raw_connection()?.read(&mut buffer) {
.get_transport_mut()
.get_raw_connection()?
.read(&mut buffer)
{
Ok(size) => { Ok(size) => {
if size == 0 { if size == 0 {
return Ok(()); return Ok(());
@@ -62,10 +47,10 @@ impl ADBDeviceExt for ADBServerDevice {
self.stat(remote_path) self.stat(remote_path)
} }
fn shell<R: Read, W: Write + Send + 'static>( fn shell(
&mut self, &mut self,
mut reader: R, mut reader: &mut dyn Read,
mut writer: W, mut writer: Box<(dyn Write + Send)>,
) -> Result<()> { ) -> Result<()> {
let supported_features = self.host_features()?; let supported_features = self.host_features()?;
if !supported_features.contains(&HostFeatures::ShellV2) if !supported_features.contains(&HostFeatures::ShellV2)
@@ -77,10 +62,9 @@ impl ADBDeviceExt for ADBServerDevice {
let serial = self.identifier.clone(); let serial = self.identifier.clone();
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport.send_adb_request(AdbServerCommand::Shell)?;
.send_adb_request(AdbServerCommand::Shell)?;
let mut read_stream = self.get_transport_mut().get_raw_connection()?.try_clone()?; let mut read_stream = self.transport.get_raw_connection()?.try_clone()?;
let mut write_stream = read_stream.try_clone()?; let mut write_stream = read_stream.try_clone()?;
@@ -107,7 +91,7 @@ impl ADBDeviceExt for ADBServerDevice {
// Read from given reader (that could be stdin e.g), and write content to server socket // Read from given reader (that could be stdin e.g), and write content to server socket
if let Err(e) = std::io::copy(&mut reader, &mut write_stream) { if let Err(e) = std::io::copy(&mut reader, &mut write_stream) {
match e.kind() { match e.kind() {
std::io::ErrorKind::BrokenPipe => return Ok(()), ErrorKind::BrokenPipe => return Ok(()),
_ => return Err(RustADBError::IOError(e)), _ => return Err(RustADBError::IOError(e)),
} }
} }
@@ -115,7 +99,7 @@ impl ADBDeviceExt for ADBServerDevice {
Ok(()) Ok(())
} }
fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, mut output: W) -> Result<()> { fn pull(&mut self, source: &dyn AsRef<str>, mut output: &mut dyn Write) -> Result<()> {
self.pull(source, &mut output) self.pull(source, &mut output)
} }
@@ -123,11 +107,15 @@ impl ADBDeviceExt for ADBServerDevice {
self.reboot(reboot_type) self.reboot(reboot_type)
} }
fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> { fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
self.push(stream, path) self.push(stream, path)
} }
fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> { fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
self.install(apk_path) self.install(apk_path)
} }
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
self.framebuffer_inner()
}
} }

View File

@@ -7,7 +7,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?; .send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::Forward(remote, local), false) .proxy_connection(AdbServerCommand::Forward(remote, local), false)
.map(|_| ()) .map(|_| ())
} }
@@ -18,7 +18,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?; .send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::ForwardRemoveAll, false) .proxy_connection(AdbServerCommand::ForwardRemoveAll, false)
.map(|_| ()) .map(|_| ())
} }

View File

@@ -1,134 +1,25 @@
use std::{ use std::io::Read;
io::{Read, Seek, Write},
iter::Map,
path::Path,
slice::ChunksExact,
};
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use image::{ImageBuffer, ImageFormat, Rgba}; use image::{ImageBuffer, Rgba};
use crate::{models::AdbServerCommand, utils, ADBServerDevice, Result, RustADBError}; use crate::{
models::{AdbServerCommand, FrameBufferInfoV1, FrameBufferInfoV2},
type U32ChunkIter<'a> = Map<ChunksExact<'a, u8>, fn(&[u8]) -> Result<u32>>; ADBServerDevice, Result, RustADBError,
};
fn read_next(chunks: &mut U32ChunkIter) -> Result<u32> {
chunks
.next()
.ok_or(RustADBError::FramebufferConversionError)?
}
#[derive(Debug)]
struct FrameBufferInfoV1 {
pub _bpp: u32,
pub size: u32,
pub width: u32,
pub height: u32,
pub _red_offset: u32,
pub _red_length: u32,
pub _blue_offset: u32,
pub _blue_length: u32,
pub _green_offset: u32,
pub _green_length: u32,
pub _alpha_offset: u32,
pub _alpha_length: u32,
}
impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV1 {
type Error = RustADBError;
fn try_from(
value: [u8; std::mem::size_of::<Self>()],
) -> std::result::Result<Self, Self::Error> {
let mut chunks: U32ChunkIter = value.chunks_exact(4).map(utils::u32_from_le);
Ok(Self {
_bpp: read_next(&mut chunks)?,
size: read_next(&mut chunks)?,
width: read_next(&mut chunks)?,
height: read_next(&mut chunks)?,
_red_offset: read_next(&mut chunks)?,
_red_length: read_next(&mut chunks)?,
_blue_offset: read_next(&mut chunks)?,
_blue_length: read_next(&mut chunks)?,
_green_offset: read_next(&mut chunks)?,
_green_length: read_next(&mut chunks)?,
_alpha_offset: read_next(&mut chunks)?,
_alpha_length: read_next(&mut chunks)?,
})
}
}
#[derive(Debug)]
struct FrameBufferInfoV2 {
pub _bpp: u32,
pub _color_space: u32,
pub size: u32,
pub width: u32,
pub height: u32,
pub _red_offset: u32,
pub _red_length: u32,
pub _blue_offset: u32,
pub _blue_length: u32,
pub _green_offset: u32,
pub _green_length: u32,
pub _alpha_offset: u32,
pub _alpha_length: u32,
}
impl TryFrom<[u8; std::mem::size_of::<Self>()]> for FrameBufferInfoV2 {
type Error = RustADBError;
fn try_from(
value: [u8; std::mem::size_of::<Self>()],
) -> std::result::Result<Self, Self::Error> {
let mut chunks: U32ChunkIter = value.chunks_exact(4).map(utils::u32_from_le);
Ok(Self {
_bpp: read_next(&mut chunks)?,
_color_space: read_next(&mut chunks)?,
size: read_next(&mut chunks)?,
width: read_next(&mut chunks)?,
height: read_next(&mut chunks)?,
_red_offset: read_next(&mut chunks)?,
_red_length: read_next(&mut chunks)?,
_blue_offset: read_next(&mut chunks)?,
_blue_length: read_next(&mut chunks)?,
_green_offset: read_next(&mut chunks)?,
_green_length: read_next(&mut chunks)?,
_alpha_offset: read_next(&mut chunks)?,
_alpha_length: read_next(&mut chunks)?,
})
}
}
impl ADBServerDevice { impl ADBServerDevice {
/// Dump framebuffer of this device into given ['path']
/// Big help from source code (<https://android.googlesource.com/platform/system/adb/+/refs/heads/main/framebuffer_service.cpp>)
pub fn framebuffer<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let img = self.framebuffer_inner()?;
Ok(img.save(path.as_ref())?)
}
/// Dump framebuffer of this device and return corresponding bytes.
///
/// Output data format is currently only `PNG`.
pub fn framebuffer_bytes<W: Write + Seek>(&mut self, mut writer: W) -> Result<()> {
let img = self.framebuffer_inner()?;
Ok(img.write_to(&mut writer, ImageFormat::Png)?)
}
/// Inner method requesting framebuffer from Android device /// Inner method requesting framebuffer from Android device
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> { pub(crate) fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
let serial: String = self.identifier.clone(); let serial: String = self.identifier.clone();
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.send_adb_request(AdbServerCommand::FrameBuffer)?; .send_adb_request(AdbServerCommand::FrameBuffer)?;
let version = self let version = self
.get_transport_mut() .transport
.get_raw_connection()? .get_raw_connection()?
.read_u32::<LittleEndian>()?; .read_u32::<LittleEndian>()?;
@@ -137,51 +28,49 @@ impl ADBServerDevice {
1 => { 1 => {
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV1>()]; let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV1>()];
self.get_transport_mut() self.transport.get_raw_connection()?.read_exact(&mut buf)?;
.get_raw_connection()?
.read_exact(&mut buf)?;
let h: FrameBufferInfoV1 = buf.try_into()?; let framebuffer_info: FrameBufferInfoV1 = buf.try_into()?;
let mut data = vec![ let mut data = vec![
0_u8; 0_u8;
h.size framebuffer_info
.size
.try_into() .try_into()
.map_err(|_| RustADBError::ConversionError)? .map_err(|_| RustADBError::ConversionError)?
]; ];
self.get_transport_mut() self.transport.get_raw_connection()?.read_exact(&mut data)?;
.get_raw_connection()?
.read_exact(&mut data)?;
Ok( Ok(ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(h.width, h.height, data) framebuffer_info.width,
.ok_or_else(|| RustADBError::FramebufferConversionError)?, framebuffer_info.height,
data,
) )
.ok_or_else(|| RustADBError::FramebufferConversionError)?)
} }
// RGBX_8888 // RGBX_8888
2 => { 2 => {
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV2>()]; let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV2>()];
self.get_transport_mut() self.transport.get_raw_connection()?.read_exact(&mut buf)?;
.get_raw_connection()?
.read_exact(&mut buf)?;
let h: FrameBufferInfoV2 = buf.try_into()?; let framebuffer_info: FrameBufferInfoV2 = buf.try_into()?;
let mut data = vec![ let mut data = vec![
0_u8; 0_u8;
h.size framebuffer_info
.size
.try_into() .try_into()
.map_err(|_| RustADBError::ConversionError)? .map_err(|_| RustADBError::ConversionError)?
]; ];
self.get_transport_mut() self.transport.get_raw_connection()?.read_exact(&mut data)?;
.get_raw_connection()?
.read_exact(&mut data)?;
Ok( Ok(ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(h.width, h.height, data) framebuffer_info.width,
.ok_or_else(|| RustADBError::FramebufferConversionError)?, framebuffer_info.height,
data,
) )
.ok_or_else(|| RustADBError::FramebufferConversionError)?)
} }
v => Err(RustADBError::UnimplementedFramebufferImageVersion(v)), v => Err(RustADBError::UnimplementedFramebufferImageVersion(v)),
} }

View File

@@ -11,7 +11,7 @@ impl ADBServerDevice {
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
let features = self let features = self
.get_transport_mut() .transport
.proxy_connection(AdbServerCommand::HostFeatures, true)?; .proxy_connection(AdbServerCommand::HostFeatures, true)?;
Ok(features Ok(features

View File

@@ -17,15 +17,15 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.send_adb_request(AdbServerCommand::Install(file_size))?; .send_adb_request(AdbServerCommand::Install(file_size))?;
let mut raw_connection = self.get_transport_mut().get_raw_connection()?; let mut raw_connection = self.transport.get_raw_connection()?;
std::io::copy(&mut apk_file, &mut raw_connection)?; std::io::copy(&mut apk_file, &mut raw_connection)?;
let mut data = [0; 1024]; let mut data = [0; 1024];
let read_amount = self.get_transport().get_raw_connection()?.read(&mut data)?; let read_amount = self.transport.get_raw_connection()?.read(&mut data)?;
match &data[0..read_amount] { match &data[0..read_amount] {
b"Success\n" => { b"Success\n" => {

View File

@@ -16,12 +16,10 @@ impl ADBServerDevice {
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode // Set device in SYNC mode
self.get_transport_mut() self.transport.send_adb_request(AdbServerCommand::Sync)?;
.send_adb_request(AdbServerCommand::Sync)?;
// Send a list command // Send a list command
self.get_transport_mut() self.transport.send_sync_request(SyncCommand::List)?;
.send_sync_request(SyncCommand::List)?;
self.handle_list_command(path) self.handle_list_command(path)
} }
@@ -33,19 +31,17 @@ impl ADBServerDevice {
LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32); LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32);
// 4 bytes of command name is already sent by send_sync_request // 4 bytes of command name is already sent by send_sync_request
self.get_transport_mut() self.transport.get_raw_connection()?.write_all(&len_buf)?;
.get_raw_connection()?
.write_all(&len_buf)?;
// List send the string of the directory to list, and then the server send a list of files // List send the string of the directory to list, and then the server send a list of files
self.get_transport_mut() self.transport
.get_raw_connection()? .get_raw_connection()?
.write_all(path.as_ref().to_string().as_bytes())?; .write_all(path.as_ref().to_string().as_bytes())?;
// Reads returned status code from ADB server // Reads returned status code from ADB server
let mut response = [0_u8; 4]; let mut response = [0_u8; 4];
loop { loop {
self.get_transport_mut() self.transport
.get_raw_connection()? .get_raw_connection()?
.read_exact(&mut response)?; .read_exact(&mut response)?;
match str::from_utf8(response.as_ref())? { match str::from_utf8(response.as_ref())? {
@@ -57,7 +53,7 @@ impl ADBServerDevice {
let mut mod_time = [0_u8; 4]; let mut mod_time = [0_u8; 4];
let mut name_len = [0_u8; 4]; let mut name_len = [0_u8; 4];
let mut connection = self.get_transport_mut().get_raw_connection()?; let mut connection = self.transport.get_raw_connection()?;
connection.read_exact(&mut file_mod)?; connection.read_exact(&mut file_mod)?;
connection.read_exact(&mut file_size)?; connection.read_exact(&mut file_size)?;
connection.read_exact(&mut mod_time)?; connection.read_exact(&mut mod_time)?;

View File

@@ -51,6 +51,6 @@ impl<W: Write> Write for LogFilter<W> {
impl ADBServerDevice { impl ADBServerDevice {
/// Get logs from device /// Get logs from device
pub fn get_logs<W: Write>(&mut self, output: W) -> Result<()> { pub fn get_logs<W: Write>(&mut self, output: W) -> Result<()> {
self.shell_command(["exec logcat"], LogFilter::new(output)) self.shell_command(&["exec logcat"], &mut LogFilter::new(output))
} }
} }

View File

@@ -10,7 +10,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::Reboot(reboot_type), false) .proxy_connection(AdbServerCommand::Reboot(reboot_type), false)
.map(|_| ()) .map(|_| ())
} }

View File

@@ -7,7 +7,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::Reconnect, false) .proxy_connection(AdbServerCommand::Reconnect, false)
.map(|_| ()) .map(|_| ())
} }

View File

@@ -70,18 +70,16 @@ impl<R: Read> Read for ADBRecvCommandReader<R> {
impl ADBServerDevice { impl ADBServerDevice {
/// Receives path to stream from the device. /// Receives path to stream from the device.
pub fn pull<A: AsRef<str>>(&mut self, path: A, stream: &mut dyn Write) -> Result<()> { pub fn pull(&mut self, path: &dyn AsRef<str>, stream: &mut dyn Write) -> Result<()> {
let serial = self.identifier.clone(); let serial = self.identifier.clone();
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode // Set device in SYNC mode
self.get_transport_mut() self.transport.send_adb_request(AdbServerCommand::Sync)?;
.send_adb_request(AdbServerCommand::Sync)?;
// Send a recv command // Send a recv command
self.get_transport_mut() self.transport.send_sync_request(SyncCommand::Recv)?;
.send_sync_request(SyncCommand::Recv)?;
self.handle_recv_command(path, stream) self.handle_recv_command(path, stream)
} }
@@ -91,7 +89,7 @@ impl ADBServerDevice {
from: S, from: S,
output: &mut dyn Write, output: &mut dyn Write,
) -> Result<()> { ) -> Result<()> {
let mut raw_connection = self.get_transport().get_raw_connection()?; let mut raw_connection = self.transport.get_raw_connection()?;
let from_as_bytes = from.as_ref().as_bytes(); let from_as_bytes = from.as_ref().as_bytes();
let mut buffer = Vec::with_capacity(4 + from_as_bytes.len()); let mut buffer = Vec::with_capacity(4 + from_as_bytes.len());

View File

@@ -7,7 +7,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::Reverse(remote, local), false) .proxy_connection(AdbServerCommand::Reverse(remote, local), false)
.map(|_| ()) .map(|_| ())
} }
@@ -18,7 +18,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?; .send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::ReverseRemoveAll, false) .proxy_connection(AdbServerCommand::ReverseRemoveAll, false)
.map(|_| ()) .map(|_| ())
} }

View File

@@ -50,12 +50,10 @@ impl ADBServerDevice {
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode // Set device in SYNC mode
self.get_transport_mut() self.transport.send_adb_request(AdbServerCommand::Sync)?;
.send_adb_request(AdbServerCommand::Sync)?;
// Send a send command // Send a send command
self.get_transport_mut() self.transport.send_sync_request(SyncCommand::Send)?;
.send_sync_request(SyncCommand::Send)?;
self.handle_send_command(stream, path) self.handle_send_command(stream, path)
} }
@@ -64,7 +62,7 @@ impl ADBServerDevice {
// Append the permission flags to the filename // Append the permission flags to the filename
let to = to.as_ref().to_string() + ",0777"; let to = to.as_ref().to_string() + ",0777";
let mut raw_connection = self.get_transport_mut().get_raw_connection()?; let mut raw_connection = self.transport.get_raw_connection()?;
// The name of the command is already sent by get_transport()?.send_sync_request // The name of the command is already sent by get_transport()?.send_sync_request
let to_as_bytes = to.as_bytes(); let to_as_bytes = to.as_bytes();
@@ -99,7 +97,7 @@ impl ADBServerDevice {
match AdbRequestStatus::from_str(str::from_utf8(&request_status)?)? { match AdbRequestStatus::from_str(str::from_utf8(&request_status)?)? {
AdbRequestStatus::Fail => { AdbRequestStatus::Fail => {
// We can keep reading to get further details // We can keep reading to get further details
let length = self.get_transport_mut().get_body_length()?; let length = self.transport.get_body_length()?;
let mut body = vec![ let mut body = vec![
0; 0;
@@ -108,9 +106,7 @@ impl ADBServerDevice {
.map_err(|_| RustADBError::ConversionError)? .map_err(|_| RustADBError::ConversionError)?
]; ];
if length > 0 { if length > 0 {
self.get_transport() self.transport.get_raw_connection()?.read_exact(&mut body)?;
.get_raw_connection()?
.read_exact(&mut body)?;
} }
Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?)) Err(RustADBError::ADBRequestFailed(String::from_utf8(body)?))

View File

@@ -13,24 +13,20 @@ impl ADBServerDevice {
LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32); LittleEndian::write_u32(&mut len_buf, path.as_ref().len() as u32);
// 4 bytes of command name is already sent by send_sync_request // 4 bytes of command name is already sent by send_sync_request
self.get_transport_mut() self.transport.get_raw_connection()?.write_all(&len_buf)?;
.get_raw_connection()? self.transport
.write_all(&len_buf)?;
self.get_transport_mut()
.get_raw_connection()? .get_raw_connection()?
.write_all(path.as_ref().to_string().as_bytes())?; .write_all(path.as_ref().to_string().as_bytes())?;
// Reads returned status code from ADB server // Reads returned status code from ADB server
let mut response = [0_u8; 4]; let mut response = [0_u8; 4];
self.get_transport_mut() self.transport
.get_raw_connection()? .get_raw_connection()?
.read_exact(&mut response)?; .read_exact(&mut response)?;
match std::str::from_utf8(response.as_ref())? { match std::str::from_utf8(response.as_ref())? {
"STAT" => { "STAT" => {
let mut data = [0_u8; 12]; let mut data = [0_u8; 12];
self.get_transport_mut() self.transport.get_raw_connection()?.read_exact(&mut data)?;
.get_raw_connection()?
.read_exact(&mut data)?;
Ok(data.into()) Ok(data.into())
} }
@@ -48,12 +44,10 @@ impl ADBServerDevice {
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
// Set device in SYNC mode // Set device in SYNC mode
self.get_transport_mut() self.transport.send_adb_request(AdbServerCommand::Sync)?;
.send_adb_request(AdbServerCommand::Sync)?;
// Send a "Stat" command // Send a "Stat" command
self.get_transport_mut() self.transport.send_sync_request(SyncCommand::Stat)?;
.send_sync_request(SyncCommand::Stat)?;
self.handle_stat_command(path) self.handle_stat_command(path)
} }

View File

@@ -7,7 +7,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::TcpIp(port), false) .proxy_connection(AdbServerCommand::TcpIp(port), false)
.map(|_| ()) .map(|_| ())
} }

View File

@@ -7,7 +7,7 @@ impl ADBServerDevice {
self.connect()? self.connect()?
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; .send_adb_request(AdbServerCommand::TransportSerial(serial))?;
self.get_transport_mut() self.transport
.proxy_connection(AdbServerCommand::Usb, false) .proxy_connection(AdbServerCommand::Usb, false)
.map(|_| ()) .map(|_| ())
} }

View File

@@ -1,13 +1,13 @@
use std::{ use std::{
fs::File, fs::File,
io::{BufRead, BufReader, Read, Write}, io::{BufRead, BufReader, Error, ErrorKind, Read, Write},
net::{SocketAddrV4, TcpStream}, net::{SocketAddrV4, TcpStream},
}; };
use homedir::my_home; use homedir::my_home;
use super::ADBTransport; use super::ADBTransport;
use crate::{models::ADBEmulatorCommand, Result, RustADBError}; use crate::{emulator_device::ADBEmulatorCommand, Result, RustADBError};
/// Emulator transport running on top on TCP. /// Emulator transport running on top on TCP.
#[derive(Debug)] #[derive(Debug)]
@@ -28,8 +28,8 @@ impl TCPEmulatorTransport {
pub(crate) fn get_raw_connection(&self) -> Result<&TcpStream> { pub(crate) fn get_raw_connection(&self) -> Result<&TcpStream> {
self.tcp_stream self.tcp_stream
.as_ref() .as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new( .ok_or(RustADBError::IOError(Error::new(
std::io::ErrorKind::NotConnected, ErrorKind::NotConnected,
"not connected", "not connected",
))) )))
} }

View File

@@ -1,4 +1,4 @@
use std::io::{Read, Write}; use std::io::{Error, ErrorKind, Read, Write};
use std::net::{Ipv4Addr, SocketAddrV4, TcpStream}; use std::net::{Ipv4Addr, SocketAddrV4, TcpStream};
use std::str::FromStr; use std::str::FromStr;
@@ -66,8 +66,8 @@ impl TCPServerTransport {
pub(crate) fn get_raw_connection(&self) -> Result<&TcpStream> { pub(crate) fn get_raw_connection(&self) -> Result<&TcpStream> {
self.tcp_stream self.tcp_stream
.as_ref() .as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new( .ok_or(RustADBError::IOError(Error::new(
std::io::ErrorKind::NotConnected, ErrorKind::NotConnected,
"not connected", "not connected",
))) )))
} }

View File

@@ -151,8 +151,8 @@ impl TcpTransport {
client_config.key_log = Arc::new(KeyLogFile::new()); client_config.key_log = Arc::new(KeyLogFile::new());
let rc_config = Arc::new(client_config); let rc_config = Arc::new(client_config);
let example_com = self.address.ip().into(); let server_name = self.address.ip().into();
let conn = ClientConnection::new(rc_config, example_com)?; let conn = ClientConnection::new(rc_config, server_name)?;
let owned = tcp_stream.try_clone()?; let owned = tcp_stream.try_clone()?;
let client = StreamOwned::new(conn, owned); let client = StreamOwned::new(conn, owned);

View File

@@ -11,7 +11,7 @@ use crate::{
Result, RustADBError, Result, RustADBError,
}; };
#[derive(Debug)] #[derive(Clone, Debug)]
struct Endpoint { struct Endpoint {
iface: u8, iface: u8,
address: u8, address: u8,
@@ -22,6 +22,8 @@ struct Endpoint {
pub struct USBTransport { pub struct USBTransport {
device: Device<GlobalContext>, device: Device<GlobalContext>,
handle: Option<Arc<DeviceHandle<GlobalContext>>>, handle: Option<Arc<DeviceHandle<GlobalContext>>>,
read_endpoint: Option<Endpoint>,
write_endpoint: Option<Endpoint>,
} }
impl USBTransport { impl USBTransport {
@@ -49,6 +51,8 @@ impl USBTransport {
Self { Self {
device: rusb_device, device: rusb_device,
handle: None, handle: None,
read_endpoint: None,
write_endpoint: None,
} }
} }
@@ -62,13 +66,34 @@ impl USBTransport {
.cloned() .cloned()
} }
fn get_read_endpoint(&self) -> Result<Endpoint> {
self.read_endpoint
.as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"no read endpoint setup",
)))
.cloned()
}
fn get_write_endpoint(&self) -> Result<&Endpoint> {
self.write_endpoint
.as_ref()
.ok_or(RustADBError::IOError(std::io::Error::new(
std::io::ErrorKind::NotConnected,
"no write endpoint setup",
)))
}
fn configure_endpoint(handle: &DeviceHandle<GlobalContext>, endpoint: &Endpoint) -> Result<()> { fn configure_endpoint(handle: &DeviceHandle<GlobalContext>, endpoint: &Endpoint) -> Result<()> {
handle.claim_interface(endpoint.iface)?; handle.claim_interface(endpoint.iface)?;
Ok(()) Ok(())
} }
fn find_readable_endpoint(&self) -> Result<Endpoint> { fn find_endpoints(&self, handle: &DeviceHandle<GlobalContext>) -> Result<(Endpoint, Endpoint)> {
let handle = self.get_raw_connection()?; let mut read_endpoint: Option<Endpoint> = None;
let mut write_endpoint: Option<Endpoint> = None;
for n in 0..handle.device().device_descriptor()?.num_configurations() { for n in 0..handle.device().device_descriptor()?.num_configurations() {
let config_desc = match handle.device().config_descriptor(n) { let config_desc = match handle.device().config_descriptor(n) {
Ok(c) => c, Ok(c) => c,
@@ -78,46 +103,31 @@ impl USBTransport {
for interface in config_desc.interfaces() { for interface in config_desc.interfaces() {
for interface_desc in interface.descriptors() { for interface_desc in interface.descriptors() {
for endpoint_desc in interface_desc.endpoint_descriptors() { for endpoint_desc in interface_desc.endpoint_descriptors() {
if endpoint_desc.direction() == Direction::In if endpoint_desc.transfer_type() == TransferType::Bulk
&& endpoint_desc.transfer_type() == TransferType::Bulk
&& interface_desc.class_code() == LIBUSB_CLASS_VENDOR_SPEC && interface_desc.class_code() == LIBUSB_CLASS_VENDOR_SPEC
&& interface_desc.sub_class_code() == 0x42 && interface_desc.sub_class_code() == 0x42
&& interface_desc.protocol_code() == 0x01 && interface_desc.protocol_code() == 0x01
{ {
return Ok(Endpoint { let endpoint = Endpoint {
iface: interface_desc.interface_number(), iface: interface_desc.interface_number(),
address: endpoint_desc.address(), address: endpoint_desc.address(),
}); };
} match endpoint_desc.direction() {
} Direction::In => {
} if let Some(write_endpoint) = write_endpoint {
} return Ok((endpoint, write_endpoint));
} } else {
read_endpoint = Some(endpoint);
Err(RustADBError::USBNoDescriptorFound) }
} }
Direction::Out => {
fn find_writable_endpoint(&self) -> Result<Endpoint> { if let Some(read_endpoint) = read_endpoint {
let handle = self.get_raw_connection()?; return Ok((read_endpoint, endpoint));
for n in 0..handle.device().device_descriptor()?.num_configurations() { } else {
let config_desc = match handle.device().config_descriptor(n) { write_endpoint = Some(endpoint);
Ok(c) => c, }
Err(_) => continue, }
}; }
for interface in config_desc.interfaces() {
for interface_desc in interface.descriptors() {
for endpoint_desc in interface_desc.endpoint_descriptors() {
if endpoint_desc.direction() == Direction::Out
&& endpoint_desc.transfer_type() == TransferType::Bulk
&& interface_desc.class_code() == LIBUSB_CLASS_VENDOR_SPEC
&& interface_desc.sub_class_code() == 0x42
&& interface_desc.protocol_code() == 0x01
{
return Ok(Endpoint {
iface: interface_desc.interface_number(),
address: endpoint_desc.address(),
});
} }
} }
} }
@@ -130,12 +140,23 @@ impl USBTransport {
impl ADBTransport for USBTransport { impl ADBTransport for USBTransport {
fn connect(&mut self) -> crate::Result<()> { fn connect(&mut self) -> crate::Result<()> {
self.handle = Some(Arc::new(self.device.open()?)); let device = self.device.open()?;
let (read_endpoint, write_endpoint) = self.find_endpoints(&device)?;
Self::configure_endpoint(&device, &read_endpoint)?;
self.read_endpoint = Some(read_endpoint);
Self::configure_endpoint(&device, &write_endpoint)?;
self.write_endpoint = Some(write_endpoint);
self.handle = Some(Arc::new(device));
Ok(()) Ok(())
} }
fn disconnect(&mut self) -> crate::Result<()> { fn disconnect(&mut self) -> crate::Result<()> {
let message = ADBTransportMessage::new(MessageCommand::Clse, 0, 0, "".into()); let message = ADBTransportMessage::new(MessageCommand::Clse, 0, 0, &[]);
self.write_message(message) self.write_message(message)
} }
} }
@@ -146,15 +167,9 @@ impl ADBMessageTransport for USBTransport {
message: ADBTransportMessage, message: ADBTransportMessage,
timeout: Duration, timeout: Duration,
) -> Result<()> { ) -> Result<()> {
let endpoint = self.find_writable_endpoint()?; let endpoint = self.get_write_endpoint()?;
let handle = self.get_raw_connection()?; let handle = self.get_raw_connection()?;
if let Ok(true) = handle.kernel_driver_active(endpoint.iface) {
handle.detach_kernel_driver(endpoint.iface)?;
}
Self::configure_endpoint(&handle, &endpoint)?;
let message_bytes = message.header().as_bytes()?; let message_bytes = message.header().as_bytes()?;
let mut total_written = 0; let mut total_written = 0;
loop { loop {
@@ -181,15 +196,9 @@ impl ADBMessageTransport for USBTransport {
} }
fn read_message_with_timeout(&mut self, timeout: Duration) -> Result<ADBTransportMessage> { fn read_message_with_timeout(&mut self, timeout: Duration) -> Result<ADBTransportMessage> {
let endpoint = self.find_readable_endpoint()?; let endpoint = self.get_read_endpoint()?;
let handle = self.get_raw_connection()?; let handle = self.get_raw_connection()?;
if let Ok(true) = handle.kernel_driver_active(endpoint.iface) {
handle.detach_kernel_driver(endpoint.iface)?;
}
Self::configure_endpoint(&handle, &endpoint)?;
let mut data = [0; 24]; let mut data = [0; 24];
let mut total_read = 0; let mut total_read = 0;
loop { loop {

View File

@@ -2,14 +2,6 @@ use std::{ffi::OsStr, path::Path};
use crate::{Result, RustADBError}; use crate::{Result, RustADBError};
pub fn u32_from_le(value: &[u8]) -> Result<u32> {
Ok(u32::from_le_bytes(
value
.try_into()
.map_err(|_| RustADBError::ConversionError)?,
))
}
pub fn check_extension_is_apk<P: AsRef<Path>>(path: P) -> Result<()> { pub fn check_extension_is_apk<P: AsRef<Path>>(path: P) -> Result<()> {
if let Some(extension) = path.as_ref().extension() { if let Some(extension) = path.as_ref().extension() {
if ![OsStr::new("apk")].contains(&extension) { if ![OsStr::new("apk")].contains(&extension) {

View File

@@ -1,4 +1,4 @@
use adb_client::{ADBDeviceExt, ADBServer, ADBUSBDevice}; use adb_client::ADBServer;
use anyhow::Result; use anyhow::Result;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
@@ -38,14 +38,6 @@ fn bench_adb_client_push() -> Result<()> {
Ok(device.push(f, REMOTE_TEST_FILE_PATH)?) Ok(device.push(f, REMOTE_TEST_FILE_PATH)?)
} }
/// Use `adb_client` crate to push a file on device using an USB device.
/// Only one android device must be connected when launching this benchmark as we're using `autodetect()` method.
fn bench_adb_client_push_over_usb() -> Result<()> {
let mut device = ADBUSBDevice::autodetect()?;
let f = File::open(LOCAL_TEST_FILE_PATH)?;
Ok(device.push(f, REMOTE_TEST_FILE_PATH)?)
}
/// Use standard `adb` command ti push a file on device /// Use standard `adb` command ti push a file on device
fn bench_adb_push_command() -> Result<()> { fn bench_adb_push_command() -> Result<()> {
let output = Command::new("adb") let output = Command::new("adb")
@@ -93,43 +85,9 @@ fn benchmark_adb_push(c: &mut Criterion) {
} }
} }
/// benchmarking `adb push INPUT DEST` and adb_client `ADBUSBDevice.push(INPUT, DEST)`
fn benchmark_adb_push_over_usb(c: &mut Criterion) {
for (file_size, sample_size) in [
// (10 * 1024 * 1024, 100), // 10MB -> 100 iterations
// (500 * 1024 * 1024, 50), // 500MB -> 50 iterations
(1000 * 1024 * 1024, 20), // 1GB -> 20 iterations
] {
eprintln!(
"Benchmarking file_size={} and sample_size={}",
file_size, sample_size
);
generate_test_file(file_size).expect("Cannot generate test file");
let mut group = c.benchmark_group("ADB Push Benchmark");
group.sample_size(sample_size);
group.bench_function(BenchmarkId::new("adb_client", "push"), |b| {
b.iter(|| {
bench_adb_client_push_over_usb()
.expect("Error while benchmarking adb_client push over USB");
});
});
group.bench_function(BenchmarkId::new("adb", "push"), |b| {
b.iter(|| {
bench_adb_push_command().expect("Error while benchmarking adb push command");
});
});
group.finish();
}
}
criterion_group!( criterion_group!(
name = benches; name = benches;
config = Criterion::default().measurement_time(Duration::from_secs(1000)); config = Criterion::default().measurement_time(Duration::from_secs(1000));
targets = benchmark_adb_push, benchmark_adb_push_over_usb targets = benchmark_adb_push
); );
criterion_main!(benches); criterion_main!(benches);

20
pyadb_client/Cargo.toml Normal file
View File

@@ -0,0 +1,20 @@
[package]
name = "pyadb_client"
description = "Python wrapper for adb_client library"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true
readme = "README.md"
[lib]
name = "pyadb_client"
crate-type = ["cdylib"]
[dependencies]
anyhow = { version = "1.0.94" }
adb_client = { version = "2.0.6" }
pyo3 = { version = "0.23.3", features = ["extension-module", "anyhow"] }

44
pyadb_client/README.md Normal file
View File

@@ -0,0 +1,44 @@
# pyadb_client
Python library to communicate with ADB devices. Built on top of Rust `adb_client` library.
## Examples
### Use ADB server
```python
server = pyadb_client.PyADBServer("127.0.0.1:5037")
for i, device in enumerate(server.devices()):
print(i, device.identifier, device.state)
# Get only connected device
device = server.get_device()
print(device, device.identifier)
```
### Push a file on device
```python
usb_device = PyADBUSBDevice.autodetect()
usb_device.push("file.txt", "/data/local/tmp/file.txt")
```
## Local development
```bash
# Create Python virtual environment
cd pyadb_client
python3 -m venv .venv
source .venv/bin/activate
# Install needed dependencies
pip install -e .
# Build development package
maturin develop
# Build release Python package
maturin build --release
# Publish Python package
```

View File

@@ -0,0 +1,13 @@
[build-system]
requires = ["maturin>=1,<2"]
build-backend = "maturin"
[project]
name = "pyadb_client"
dependencies = ["maturin", "patchelf"]
dynamic = ["version"] # Let the build system automatically set package version
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]

View File

@@ -0,0 +1,37 @@
use std::net::SocketAddrV4;
use adb_client::ADBServer;
use anyhow::Result;
use pyo3::{pyclass, pymethods, PyResult};
use crate::{PyADBServerDevice, PyDeviceShort};
#[pyclass]
pub struct PyADBServer(ADBServer);
#[pymethods]
impl PyADBServer {
#[new]
pub fn new(address: String) -> PyResult<Self> {
let address = address.parse::<SocketAddrV4>()?;
Ok(ADBServer::new(address).into())
}
pub fn devices(&mut self) -> Result<Vec<PyDeviceShort>> {
Ok(self.0.devices()?.into_iter().map(|v| v.into()).collect())
}
pub fn get_device(&mut self) -> Result<PyADBServerDevice> {
Ok(self.0.get_device()?.into())
}
pub fn get_device_by_name(&mut self, name: String) -> Result<PyADBServerDevice> {
Ok(self.0.get_device_by_name(&name)?.into())
}
}
impl From<ADBServer> for PyADBServer {
fn from(value: ADBServer) -> Self {
Self(value)
}
}

View File

@@ -0,0 +1,38 @@
use adb_client::{ADBDeviceExt, ADBServerDevice};
use anyhow::Result;
use pyo3::{pyclass, pymethods};
use std::{fs::File, path::PathBuf};
#[pyclass]
pub struct PyADBServerDevice(pub ADBServerDevice);
#[pymethods]
impl PyADBServerDevice {
#[getter]
pub fn identifier(&self) -> String {
self.0.identifier.clone()
}
pub fn shell_command(&mut self, commands: Vec<String>) -> Result<Vec<u8>> {
let mut output = Vec::new();
let commands: Vec<&str> = commands.iter().map(|x| &**x).collect();
self.0.shell_command(&commands, &mut output)?;
Ok(output)
}
pub fn push(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
let mut reader = File::open(input)?;
Ok(self.0.push(&mut reader, dest.to_string_lossy())?)
}
pub fn pull(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
let mut writer = File::create(dest)?;
Ok(self.0.pull(&input.to_string_lossy(), &mut writer)?)
}
}
impl From<ADBServerDevice> for PyADBServerDevice {
fn from(value: ADBServerDevice) -> Self {
Self(value)
}
}

View File

@@ -0,0 +1,39 @@
use std::{fs::File, path::PathBuf};
use adb_client::{ADBDeviceExt, ADBUSBDevice};
use anyhow::Result;
use pyo3::{pyclass, pymethods};
#[pyclass]
pub struct PyADBUSBDevice(ADBUSBDevice);
#[pymethods]
impl PyADBUSBDevice {
#[staticmethod]
pub fn autodetect() -> Result<Self> {
Ok(ADBUSBDevice::autodetect()?.into())
}
pub fn shell_command(&mut self, commands: Vec<String>) -> Result<Vec<u8>> {
let mut output = Vec::new();
let commands: Vec<&str> = commands.iter().map(|x| &**x).collect();
self.0.shell_command(&commands, &mut output)?;
Ok(output)
}
pub fn push(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
let mut reader = File::open(input)?;
Ok(self.0.push(&mut reader, &dest.to_string_lossy())?)
}
pub fn pull(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
let mut writer = File::create(dest)?;
Ok(self.0.pull(&input.to_string_lossy(), &mut writer)?)
}
}
impl From<ADBUSBDevice> for PyADBUSBDevice {
fn from(value: ADBUSBDevice) -> Self {
Self(value)
}
}

20
pyadb_client/src/lib.rs Normal file
View File

@@ -0,0 +1,20 @@
mod adb_server;
mod adb_server_device;
mod adb_usb_device;
mod models;
pub use adb_server::*;
pub use adb_server_device::*;
pub use adb_usb_device::*;
pub use models::*;
use pyo3::prelude::*;
#[pymodule]
fn pyadb_client(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyADBServer>()?;
m.add_class::<PyDeviceShort>()?;
m.add_class::<PyADBServerDevice>()?;
m.add_class::<PyADBUSBDevice>()?;
Ok(())
}

View File

@@ -0,0 +1,26 @@
use adb_client::DeviceShort;
use pyo3::{pyclass, pymethods};
// Check https://docs.rs/rigetti-pyo3/latest/rigetti_pyo3 to automatically build this code
#[pyclass]
pub struct PyDeviceShort(DeviceShort);
#[pymethods]
impl PyDeviceShort {
#[getter]
pub fn identifier(&self) -> String {
self.0.identifier.clone()
}
#[getter]
pub fn state(&self) -> String {
self.0.state.to_string()
}
}
impl From<DeviceShort> for PyDeviceShort {
fn from(value: DeviceShort) -> Self {
Self(value)
}
}

View File

@@ -0,0 +1,2 @@
mod devices;
pub use devices::PyDeviceShort;