Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b4be5d5cd | ||
|
|
586964128f | ||
|
|
042ca89f12 | ||
|
|
c6b12008a8 | ||
|
|
2bd0b2ca87 | ||
|
|
1589ee9da5 | ||
|
|
d39e98695d | ||
|
|
34d5811420 | ||
|
|
86e28a6e25 | ||
|
|
9f113bdb93 | ||
|
|
8670d6db58 | ||
|
|
0732a0bbad | ||
|
|
b5673001ca | ||
|
|
81829c1523 | ||
|
|
b9d2b8374f | ||
|
|
5716784f5d | ||
|
|
b6ddc720d8 | ||
|
|
5438e53361 | ||
|
|
39a7f0a8cf | ||
|
|
4129d9d218 | ||
|
|
c23dca61d7 | ||
|
|
2bc338fdf5 | ||
|
|
728fb7da95 | ||
|
|
2e762400e5 | ||
|
|
c85c76228d | ||
|
|
e91a35b3ff | ||
|
|
8f979eff91 | ||
|
|
af51584736 | ||
|
|
864bde01f6 | ||
|
|
8361a0fe06 | ||
|
|
218ec3d679 | ||
|
|
4b817c0435 | ||
|
|
2dd30931f2 | ||
|
|
8b4602c62f | ||
|
|
e917a45670 | ||
|
|
b0303ad544 | ||
|
|
f3f95d92c2 | ||
|
|
886adfa392 | ||
|
|
cdf062c3e1 | ||
|
|
b8e3d02311 | ||
|
|
1b7efc1cc6 | ||
|
|
ad064a9f41 | ||
|
|
d0e0f46571 | ||
|
|
727f3a3eb4 | ||
|
|
ab77db5cc8 | ||
|
|
1255f2b5d6 | ||
|
|
eb04f9064c | ||
|
|
69107e2333 | ||
|
|
17bb77a472 | ||
|
|
f211023b24 | ||
|
|
728d9603dc | ||
|
|
00c387d85c | ||
|
|
79d96d4c76 | ||
|
|
dc909ceda6 | ||
|
|
cbba912483 | ||
|
|
38d8384b98 | ||
|
|
39591b6a0a | ||
|
|
775b2421ec | ||
|
|
62d16b70fb | ||
|
|
466d00e68a | ||
|
|
144072ba1b | ||
|
|
331ef95530 | ||
|
|
61408cb470 | ||
|
|
c54942f25d | ||
|
|
3feda38cc3 | ||
|
|
9002ecc0c8 | ||
|
|
5a3ac68fce | ||
|
|
5dfd30cc5b | ||
|
|
66d124475d | ||
|
|
8c382f0bde | ||
|
|
b933ab083f | ||
|
|
2f4d13bd29 | ||
|
|
9eeb8f7da8 |
38
.github/workflows/python-build.yml
vendored
Normal file
38
.github/workflows/python-build.yml
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
name: Python - Build packages & Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
release:
|
||||||
|
types: [created]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-python-packages:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Build Python stubs
|
||||||
|
run: cargo run --bin stub_gen
|
||||||
|
|
||||||
|
- name: Install Python build dependencies
|
||||||
|
run: pip install maturin==1.8.2
|
||||||
|
|
||||||
|
- name: Build Python packages
|
||||||
|
run: maturin build --sdist --release -m pyadb_client/Cargo.toml
|
||||||
|
|
||||||
|
- name: Publish Python packages
|
||||||
|
if: github.event_name == 'release' && github.event.action == 'created'
|
||||||
|
run: maturin publish -m pyadb_client/Cargo.toml --non-interactive
|
||||||
|
env:
|
||||||
|
MATURIN_PYPI_TOKEN: ${{ secrets.MATURIN_PYPI_TOKEN }}
|
||||||
|
|
||||||
|
- name: "Publish GitHub artefacts"
|
||||||
|
if: github.event_name == 'release' && github.event.action == 'created'
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
target/wheels/pyadb_client*.whl
|
||||||
|
target/wheels/pyadb_client*.tar.gz
|
||||||
22
.github/workflows/rust-build.yml
vendored
22
.github/workflows/rust-build.yml
vendored
@@ -1,15 +1,23 @@
|
|||||||
name: Rust - Build
|
name: Rust - Build
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-release:
|
build:
|
||||||
name: "build-release"
|
name: "Build on ${{ matrix.os }}"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Build project
|
- name: Build project
|
||||||
run: cargo build --release
|
run: cargo build --release --all-features
|
||||||
|
|||||||
22
.github/workflows/rust-quality.yml
vendored
22
.github/workflows/rust-quality.yml
vendored
@@ -1,6 +1,10 @@
|
|||||||
name: Rust - Quality
|
name: Rust - Quality
|
||||||
|
|
||||||
on: [push, pull_request]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
@@ -13,7 +17,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- run: rustup component add clippy
|
- run: rustup component add clippy
|
||||||
- name: Run clippy
|
- name: Run clippy
|
||||||
run : cargo clippy --all-features
|
run: cargo clippy --all-features
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
name: "fmt"
|
name: "fmt"
|
||||||
@@ -21,7 +25,17 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Run formatter
|
- name: Run formatter
|
||||||
run : cargo fmt --all --check
|
run: cargo fmt --all --check
|
||||||
|
|
||||||
|
doc:
|
||||||
|
name: "doc"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Run doc
|
||||||
|
run: cargo doc --all-features --no-deps
|
||||||
|
env:
|
||||||
|
RUSTDOCFLAGS: "-D warnings"
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
name: "tests"
|
name: "tests"
|
||||||
@@ -29,4 +43,4 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: cargo test --verbose
|
run: cargo test --verbose --all-features
|
||||||
|
|||||||
88
.github/workflows/rust-release.yml
vendored
88
.github/workflows/rust-release.yml
vendored
@@ -4,47 +4,101 @@ on:
|
|||||||
release:
|
release:
|
||||||
types: [created]
|
types: [created]
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
create-release:
|
release-linux:
|
||||||
|
name: Linux - Build and Publish
|
||||||
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
|
||||||
with:
|
with:
|
||||||
toolchain: stable
|
toolchain: stable
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
- name: "Install dependencies"
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install -y rpm
|
sudo apt install -y rpm
|
||||||
cargo install cargo-deb
|
cargo install cargo-deb
|
||||||
cargo install cargo-generate-rpm
|
cargo install cargo-generate-rpm
|
||||||
|
|
||||||
- name: "build-release"
|
- name: Publish crates
|
||||||
run: cargo build --release
|
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 DEB package"
|
- name: Build release
|
||||||
|
run: cargo build --all-features --release
|
||||||
|
|
||||||
|
- name: Rename binary
|
||||||
|
run: mv target/release/adb_cli target/release/adb_cli-linux
|
||||||
|
|
||||||
|
- name: Build DEB package
|
||||||
run: cargo deb -p adb_cli
|
run: cargo deb -p adb_cli
|
||||||
|
|
||||||
- name: "Build RPM package"
|
- name: Build RPM package
|
||||||
run: cargo generate-rpm -p adb_cli
|
run: cargo generate-rpm -p adb_cli
|
||||||
|
|
||||||
- name: "Publish GitHub artefacts"
|
- name: Upload Linux artifacts
|
||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
with:
|
with:
|
||||||
files: |
|
files: |
|
||||||
target/debian/*.deb
|
target/debian/*.deb
|
||||||
target/generate-rpm/*.rpm
|
target/generate-rpm/*.rpm
|
||||||
target/release/adb_cli
|
target/release/adb_cli-linux
|
||||||
|
|
||||||
- name: "Publish crates"
|
release-macos:
|
||||||
run: |
|
name: macOS - Build Binary
|
||||||
cargo publish -p adb_client --token ${CRATES_IO_TOKEN}
|
runs-on: macos-13
|
||||||
cargo publish -p adb_cli --token ${CRATES_IO_TOKEN}
|
|
||||||
env:
|
steps:
|
||||||
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Rust
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
override: true
|
||||||
|
|
||||||
|
- name: Build release
|
||||||
|
run: cargo build --all-features --release
|
||||||
|
|
||||||
|
- name: Rename binary
|
||||||
|
run: mv target/release/adb_cli target/release/adb_cli-macos
|
||||||
|
|
||||||
|
- name: Upload macOS binary
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: target/release/adb_cli-macos
|
||||||
|
|
||||||
|
release-windows:
|
||||||
|
name: Windows - Build Binary
|
||||||
|
runs-on: windows-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Rust
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
override: true
|
||||||
|
|
||||||
|
- name: Build release
|
||||||
|
run: cargo build --all-features --release
|
||||||
|
|
||||||
|
- name: Rename binary
|
||||||
|
run: Rename-Item -Path target/release/adb_cli.exe -NewName adb_cli-windows.exe
|
||||||
|
|
||||||
|
- name: Upload Windows binary
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: target/release/adb_cli-windows.exe
|
||||||
|
|||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,3 +1,6 @@
|
|||||||
target
|
target
|
||||||
Cargo.lock
|
/Cargo.lock
|
||||||
.vscode
|
/.vscode
|
||||||
|
venv
|
||||||
|
/.mypy_cache
|
||||||
|
pyadb_client/pyadb_client.pyi
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["adb_cli", "adb_client"]
|
members = ["adb_cli", "adb_client", "examples/mdns", "pyadb_client"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
authors = ["Corentin LIAUD"]
|
authors = ["Corentin LIAUD"]
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
homepage = "https://github.com/cocool97/adb_client"
|
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.5"
|
version = "2.1.16"
|
||||||
|
rust-version = "1.85.1"
|
||||||
|
|
||||||
# To build locally when working on a new release
|
# To build locally when working on a new release
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
|||||||
37
README.md
37
README.md
@@ -8,6 +8,12 @@
|
|||||||
<a href="https://crates.io/crates/adb_client">
|
<a href="https://crates.io/crates/adb_client">
|
||||||
<img alt="crates.io" src="https://img.shields.io/crates/v/adb_client.svg"/>
|
<img alt="crates.io" src="https://img.shields.io/crates/v/adb_client.svg"/>
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://crates.io/crates/adb_client">
|
||||||
|
<img alt="msrv" src="https://img.shields.io/crates/msrv/adb_client"/>
|
||||||
|
</a>
|
||||||
|
<a href="https://github.com/cocool97/adb_client/actions">
|
||||||
|
<img alt="ci status" src="https://github.com/cocool97/adb_client/actions/workflows/rust-build.yml/badge.svg"/>
|
||||||
|
</a>
|
||||||
<a href="https://deps.rs/repo/github/cocool97/adb_client">
|
<a href="https://deps.rs/repo/github/cocool97/adb_client">
|
||||||
<img alt="dependency status" src="https://deps.rs/repo/github/cocool97/adb_client/status.svg"/>
|
<img alt="dependency status" src="https://deps.rs/repo/github/cocool97/adb_client/status.svg"/>
|
||||||
</a>
|
</a>
|
||||||
@@ -20,27 +26,44 @@
|
|||||||
Main features of this library:
|
Main features of this library:
|
||||||
|
|
||||||
- Full Rust, don't use `adb *` shell commands to interact with devices
|
- Full Rust, don't use `adb *` shell commands to interact with devices
|
||||||
- Supports:
|
- Supports
|
||||||
- **TCP/IP** protocol, using ADB server as a proxy (standard behavior when using `adb` CLI)
|
- Using ADB server as a proxy (standard behavior when using `adb` CLI)
|
||||||
- **USB** protocol, interacting directly with end devices
|
- Connecting directly to end devices (without using adb-server)
|
||||||
|
- Over **USB**
|
||||||
|
- 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
|
||||||
|
|
||||||
Rust library implementing both ADB protocols and providing a high-level abstraction over many supported commands.
|
Rust library implementing both ADB protocols (server and end-devices) and providing a high-level abstraction over the many supported commands.
|
||||||
|
|
||||||
Improved documentation [here](./adb_client/README.md).
|
Improved documentation available [here](./adb_client/README.md).
|
||||||
|
|
||||||
## adb_cli
|
## adb_cli
|
||||||
|
|
||||||
Rust binary providing an improved version of official `adb` CLI, wrapping `adb_client` library. Can act as an usage example of the library.
|
Rust binary providing an improved version of Google's official `adb` CLI, by using `adb_client` library.
|
||||||
|
Provides a "real-world" usage example of this library.
|
||||||
|
|
||||||
Improved documentation [here](./adb_cli/README.md).
|
Improved documentation available [here](./adb_cli/README.md).
|
||||||
|
|
||||||
|
## examples
|
||||||
|
|
||||||
|
Some examples are available in the `examples` directory:
|
||||||
|
|
||||||
|
- `examples/mdns`: mDNS device discovery example
|
||||||
|
|
||||||
|
## pyadb_client
|
||||||
|
|
||||||
|
Python wrapper using `adb_client` library to export classes usable directly from a Python environment.
|
||||||
|
|
||||||
|
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 !
|
||||||
|
|||||||
@@ -7,14 +7,15 @@ license.workspace = true
|
|||||||
name = "adb_cli"
|
name = "adb_cli"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
rust-version.workspace = true
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
adb_client = { version = "2.0.5" }
|
adb_client = { version = "^2.0.0", features = ["mdns", "usb"] }
|
||||||
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.26" }
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
termios = { version = "0.3.3" }
|
termios = { version = "0.3.3" }
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
[](./LICENSE-MIT)
|
[](./LICENSE-MIT)
|
||||||

|

|
||||||
|

|
||||||
|
|
||||||
Rust binary providing an improved version of `adb` CLI.
|
Rust binary providing an improved version of `adb` CLI.
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ Rust binary providing an improved version of `adb` CLI.
|
|||||||
This crate provides a lightweight binary based on the `adb_client` crate. You can install it by running the following command :
|
This crate provides a lightweight binary based on the `adb_client` crate. You can install it by running the following command :
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
cargo install adb_cli
|
cargo install adb_cli
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage is quite simple, and tends to look like `adb`:
|
Usage is quite simple, and tends to look like `adb`:
|
||||||
@@ -18,38 +19,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
|
||||||
@@ -73,4 +65,4 @@ Options:
|
|||||||
-p, --product-id <PID> Hexadecimal product id of this USB device
|
-p, --product-id <PID> Hexadecimal product id of this USB device
|
||||||
-k, --private-key <PATH_TO_PRIVATE_KEY> Path to a custom private key to use for authentication
|
-k, --private-key <PATH_TO_PRIVATE_KEY> Path to a custom private key to use for authentication
|
||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use std::os::unix::prelude::{AsRawFd, RawFd};
|
use std::os::unix::prelude::{AsRawFd, RawFd};
|
||||||
|
|
||||||
use termios::{tcsetattr, Termios, TCSANOW, VMIN, VTIME};
|
use termios::{TCSANOW, Termios, VMIN, VTIME, tcsetattr};
|
||||||
|
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ impl Drop for ADBTermios {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Custom drop implementation, restores previous termios structure.
|
// Custom drop implementation, restores previous termios structure.
|
||||||
if let Err(e) = tcsetattr(self.fd, TCSANOW, &self.old_termios) {
|
if let Err(e) = tcsetattr(self.fd, TCSANOW, &self.old_termios) {
|
||||||
log::error!("Error while droping ADBTermios: {e}")
|
log::error!("Error while dropping ADBTermios: {e}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
use std::net::SocketAddrV4;
|
|
||||||
|
|
||||||
use clap::Parser;
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
|
||||||
pub enum HostCommand {
|
|
||||||
/// Print current ADB version.
|
|
||||||
Version,
|
|
||||||
/// Ask ADB server to quit immediately.
|
|
||||||
Kill,
|
|
||||||
/// List connected devices.
|
|
||||||
Devices {
|
|
||||||
#[clap(short = 'l', long = "long")]
|
|
||||||
long: bool,
|
|
||||||
},
|
|
||||||
/// Track new devices showing up.
|
|
||||||
TrackDevices,
|
|
||||||
/// Pair device with a given code
|
|
||||||
Pair { address: SocketAddrV4, code: String },
|
|
||||||
/// Connect device over WI-FI
|
|
||||||
Connect { address: SocketAddrV4 },
|
|
||||||
/// Disconnect device over WI-FI
|
|
||||||
Disconnect { address: SocketAddrV4 },
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
mod emu;
|
|
||||||
mod host;
|
|
||||||
mod local;
|
|
||||||
mod usb;
|
|
||||||
|
|
||||||
pub use emu::EmuCommand;
|
|
||||||
pub use host::HostCommand;
|
|
||||||
pub use local::LocalCommand;
|
|
||||||
pub use usb::{UsbCommand, UsbCommands};
|
|
||||||
@@ -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,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
20
adb_cli/src/handlers/emulator_commands.rs
Normal file
20
adb_cli/src/handlers/emulator_commands.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use adb_client::emulator::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(())
|
||||||
|
}
|
||||||
85
adb_cli/src/handlers/host_commands.rs
Normal file
85
adb_cli/src/handlers/host_commands.rs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
use adb_client::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, DeviceShort, MDNSBackend, WaitForDeviceState},
|
||||||
|
};
|
||||||
|
|
||||||
|
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()?);
|
||||||
|
}
|
||||||
|
HostCommand::WaitForDevice { transport } => {
|
||||||
|
log::info!("waiting for device to be connected...");
|
||||||
|
adb_server.wait_for_device(WaitForDeviceState::Device, transport)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
35
adb_cli/src/handlers/local_commands.rs
Normal file
35
adb_cli/src/handlers/local_commands.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use std::{fs::File, io::Write};
|
||||||
|
|
||||||
|
use adb_client::server_device::ADBServerDevice;
|
||||||
|
use anyhow::{Result, anyhow};
|
||||||
|
|
||||||
|
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)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
adb_cli/src/handlers/mod.rs
Normal file
7
adb_cli/src/handlers/mod.rs
Normal 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;
|
||||||
@@ -3,235 +3,164 @@
|
|||||||
#[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::{ADBDeviceExt, ADBEmulatorDevice, ADBServer, ADBUSBDevice, DeviceShort};
|
use adb_client::ADBDeviceExt;
|
||||||
use anyhow::{anyhow, Result};
|
use adb_client::mdns::MDNSDiscoveryService;
|
||||||
|
use adb_client::server::ADBServer;
|
||||||
|
use adb_client::server_device::ADBServerDevice;
|
||||||
|
use adb_client::tcp::ADBTcpDevice;
|
||||||
|
use adb_client::usb::ADBUSBDevice;
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||||
|
use adb_termios::ADBTermios;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use commands::{EmuCommand, HostCommand, LocalCommand, UsbCommands};
|
use handlers::{handle_emulator_commands, handle_host_commands, handle_local_commands};
|
||||||
use models::{Command, Opts};
|
use models::{DeviceCommands, LocalCommand, MainCommand, Opts};
|
||||||
|
use std::collections::HashMap;
|
||||||
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 opt = Opts::parse();
|
// This depends on `clap`
|
||||||
env_logger::init();
|
let opts = Opts::parse();
|
||||||
|
|
||||||
match opt.command {
|
// SAFETY:
|
||||||
Command::Local(local) => {
|
// We are assuming the entire process is single-threaded
|
||||||
let mut adb_server = ADBServer::new(opt.address);
|
// at this point.
|
||||||
|
// This seems true for the current version of `clap`,
|
||||||
|
// but there's no guarantee for future updates
|
||||||
|
unsafe { setup_logger(opts.debug) };
|
||||||
|
|
||||||
let mut device = match opt.serial {
|
// Directly handling methods / commands that aren't linked to [`ADBDeviceExt`] trait.
|
||||||
Some(serial) => adb_server.get_device_by_name(&serial)?,
|
// Other methods just have to create a concrete [`ADBDeviceExt`] instance, and return it.
|
||||||
None => adb_server.get_device()?,
|
// 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) => {
|
||||||
|
// Must start server to communicate with device, but only if this is a local one.
|
||||||
|
let server_address_ip = server_command.address.ip();
|
||||||
|
if server_address_ip.is_loopback() || server_address_ip.is_unspecified() {
|
||||||
|
ADBServer::start(&HashMap::default(), &None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let device = match server_command.serial {
|
||||||
|
Some(serial) => ADBServerDevice::new(serial, Some(server_command.address)),
|
||||||
|
None => ADBServerDevice::autodetect(Some(server_command.address)),
|
||||||
};
|
};
|
||||||
|
|
||||||
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(opt.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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Command::Emu(emu) => {
|
|
||||||
let mut emulator = match opt.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)
|
||||||
|
}
|
||||||
|
MainCommand::Tcp(tcp_command) => {
|
||||||
|
let device = ADBTcpDevice::new(tcp_command.address)?;
|
||||||
|
(device.boxed(), tcp_command.commands)
|
||||||
|
}
|
||||||
|
MainCommand::Mdns => {
|
||||||
|
let mut service = MDNSDiscoveryService::new()?;
|
||||||
|
|
||||||
match usb.commands {
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
UsbCommands::Shell { commands } => {
|
service.start(tx)?;
|
||||||
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")))]
|
log::info!("Starting mdns discovery...");
|
||||||
{
|
while let Ok(device) = rx.recv() {
|
||||||
device.shell(std::io::stdin(), std::io::stdout())?;
|
log::info!(
|
||||||
}
|
"Found device {} with addresses {:?}",
|
||||||
} else {
|
device.fullname,
|
||||||
device.shell_command(commands, std::io::stdout())?;
|
device.addresses
|
||||||
}
|
)
|
||||||
}
|
|
||||||
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)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(&mut std::io::stdin(), Box::new(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::Uninstall { package } => {
|
||||||
|
log::info!("Uninstalling the package {package}...");
|
||||||
|
device.uninstall(&package)?;
|
||||||
|
}
|
||||||
|
DeviceCommands::Framebuffer { path } => {
|
||||||
|
device.framebuffer(&path)?;
|
||||||
|
log::info!("Successfully dumped framebuffer at path {path}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,19 @@
|
|||||||
use clap::Parser;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::models::RebootTypeCommand;
|
use clap::Parser;
|
||||||
|
|
||||||
|
use super::RebootTypeCommand;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
pub enum LocalCommand {
|
pub enum DeviceCommands {
|
||||||
/// 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
|
/// 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 { 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 an activity on device specified by the intent
|
||||||
Run {
|
Run {
|
||||||
/// The package whose activity is to be invoked
|
/// The package whose activity is to be invoked
|
||||||
@@ -31,16 +28,19 @@ pub enum LocalCommand {
|
|||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
reboot_type: RebootTypeCommand,
|
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 an APK on device
|
||||||
Install {
|
Install {
|
||||||
/// Path to APK file. Extension must be ".apk"
|
/// Path to APK file. Extension must be ".apk"
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
},
|
},
|
||||||
|
/// Uninstall a package from the device
|
||||||
|
Uninstall {
|
||||||
|
/// Name of the package to uninstall
|
||||||
|
package: String,
|
||||||
|
},
|
||||||
|
/// Dump framebuffer of device
|
||||||
|
Framebuffer {
|
||||||
|
/// Framebuffer image destination path
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
20
adb_cli/src/models/emu.rs
Normal file
20
adb_cli/src/models/emu.rs
Normal 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,
|
||||||
|
}
|
||||||
52
adb_cli/src/models/host.rs
Normal file
52
adb_cli/src/models/host.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
|
use adb_client::{RustADBError, server::WaitForDeviceTransport};
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
fn parse_wait_for_device_device_transport(
|
||||||
|
value: &str,
|
||||||
|
) -> Result<WaitForDeviceTransport, RustADBError> {
|
||||||
|
WaitForDeviceTransport::try_from(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
pub enum HostCommand {
|
||||||
|
/// Print current ADB version.
|
||||||
|
Version,
|
||||||
|
/// Ask ADB server to quit immediately.
|
||||||
|
Kill,
|
||||||
|
/// List connected devices.
|
||||||
|
Devices {
|
||||||
|
#[clap(short = 'l', long = "long")]
|
||||||
|
long: bool,
|
||||||
|
},
|
||||||
|
/// Track new devices showing up.
|
||||||
|
TrackDevices,
|
||||||
|
/// Pair device with a given code
|
||||||
|
Pair { address: SocketAddrV4, code: String },
|
||||||
|
/// Connect device over WI-FI
|
||||||
|
Connect { address: SocketAddrV4 },
|
||||||
|
/// Disconnect device over WI-FI
|
||||||
|
Disconnect { address: SocketAddrV4 },
|
||||||
|
/// MDNS services
|
||||||
|
Mdns {
|
||||||
|
#[clap(subcommand)]
|
||||||
|
subcommand: MdnsCommand,
|
||||||
|
},
|
||||||
|
/// Display server status
|
||||||
|
ServerStatus,
|
||||||
|
/// Wait for a device, on optionally given transport
|
||||||
|
WaitForDevice {
|
||||||
|
/// Transport on which wait for devices
|
||||||
|
#[clap(short = 't', long = "transport", value_parser = parse_wait_for_device_device_transport)]
|
||||||
|
transport: Option<WaitForDeviceTransport>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
pub enum MdnsCommand {
|
||||||
|
/// Check mdns status
|
||||||
|
Check,
|
||||||
|
/// List mdns services available
|
||||||
|
Services,
|
||||||
|
}
|
||||||
24
adb_cli/src/models/local.rs
Normal file
24
adb_cli/src/models/local.rs
Normal 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>,
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -1,30 +1,41 @@
|
|||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
use crate::commands::{EmuCommand, HostCommand, LocalCommand, 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")]
|
||||||
|
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),
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ pub enum RebootTypeCommand {
|
|||||||
Recovery,
|
Recovery,
|
||||||
Sideload,
|
Sideload,
|
||||||
SideloadAutoReboot,
|
SideloadAutoReboot,
|
||||||
|
Fastboot,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<RebootTypeCommand> for RebootType {
|
impl From<RebootTypeCommand> for RebootType {
|
||||||
@@ -18,6 +19,7 @@ impl From<RebootTypeCommand> for RebootType {
|
|||||||
RebootTypeCommand::Recovery => RebootType::Recovery,
|
RebootTypeCommand::Recovery => RebootType::Recovery,
|
||||||
RebootTypeCommand::Sideload => RebootType::Sideload,
|
RebootTypeCommand::Sideload => RebootType::Sideload,
|
||||||
RebootTypeCommand::SideloadAutoReboot => RebootType::SideloadAutoReboot,
|
RebootTypeCommand::SideloadAutoReboot => RebootType::SideloadAutoReboot,
|
||||||
|
RebootTypeCommand::Fastboot => RebootType::Fastboot,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
adb_cli/src/models/tcp.rs
Normal file
11
adb_cli/src/models/tcp.rs
Normal 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
25
adb_cli/src/models/usb.rs
Normal 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,
|
||||||
|
}
|
||||||
17
adb_cli/src/utils.rs
Normal file
17
adb_cli/src/utils.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This conditionally mutates the process' environment.
|
||||||
|
/// See [`std::env::set_var`] for more info.
|
||||||
|
pub unsafe fn setup_logger(debug: bool) {
|
||||||
|
// RUST_LOG variable has more priority then "--debug" flag
|
||||||
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
|
let level = match debug {
|
||||||
|
true => "trace",
|
||||||
|
false => "info",
|
||||||
|
};
|
||||||
|
|
||||||
|
unsafe { std::env::set_var("RUST_LOG", level) };
|
||||||
|
}
|
||||||
|
|
||||||
|
env_logger::init();
|
||||||
|
}
|
||||||
@@ -7,24 +7,59 @@ license.workspace = true
|
|||||||
name = "adb_client"
|
name = "adb_client"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
rust-version.workspace = true
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
all-features = true
|
||||||
|
rustdoc-args = ["--cfg", "docsrs"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
mdns = ["dep:mdns-sd"]
|
||||||
|
usb = ["dep:rsa", "dep:rusb"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
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.40", default-features = false, features = ["std"] }
|
||||||
homedir = { version = "0.3.4" }
|
homedir = { version = "= 0.3.4" }
|
||||||
image = { version = "0.25.4" }
|
image = { version = "0.25.5", default-features = false }
|
||||||
lazy_static = { version = "1.5.0" }
|
log = { version = "0.4.26" }
|
||||||
log = { version = "0.4.22" }
|
|
||||||
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" }
|
||||||
rand = { version = "0.8.5" }
|
quick-protobuf = { version = "0.8.1" }
|
||||||
regex = { version = "1.11.0", features = ["perf", "std", "unicode"] }
|
rand = { version = "0.9.0" }
|
||||||
rsa = { version = "0.9.6" }
|
rcgen = { version = "0.13.1", default-features = false, features = [
|
||||||
rusb = { version = "0.9.4", features = ["vendored"] }
|
"aws_lc_rs",
|
||||||
serde = { version = "1.0.210", features = ["derive"] }
|
"pem",
|
||||||
|
] }
|
||||||
|
regex = { version = "1.11.1", features = ["perf", "std", "unicode"] }
|
||||||
|
rustls = { version = "0.23.27" }
|
||||||
|
rustls-pki-types = { version = "1.11.0" }
|
||||||
|
serde = { version = "1.0.216", features = ["derive"] }
|
||||||
serde_repr = { version = "0.1.19" }
|
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" }
|
||||||
|
|
||||||
|
#########
|
||||||
|
# MDNS-only dependencies
|
||||||
|
mdns-sd = { version = "0.13.9", default-features = false, features = [
|
||||||
|
"logging",
|
||||||
|
], optional = true }
|
||||||
|
#########
|
||||||
|
#########
|
||||||
|
# USB-only dependencies
|
||||||
|
rsa = { version = "0.9.7", optional = true }
|
||||||
|
rusb = { version = "0.9.4", features = ["vendored"], optional = true }
|
||||||
|
#########
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
anyhow = { version = "1.0.93" }
|
||||||
|
criterion = { version = "0.6.0" } # Used for benchmarks
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
harness = false
|
||||||
|
name = "benchmark_adb_push"
|
||||||
|
path = "../benches/benchmark_adb_push.rs"
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
[](./LICENSE-MIT)
|
[](./LICENSE-MIT)
|
||||||
[](https://docs.rs/adb_client)
|
[](https://docs.rs/adb_client)
|
||||||

|
[](https://crates.io/crates/adb_client)
|
||||||
|

|
||||||
|
|
||||||
Rust library implementing ADB protocol.
|
Rust library implementing ADB protocol.
|
||||||
|
|
||||||
@@ -15,71 +16,36 @@ Add `adb_client` crate as a dependency by simply adding it to your `Cargo.toml`:
|
|||||||
adb_client = "*"
|
adb_client = "*"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Crate features
|
||||||
|
|
||||||
|
| Feature | Description | Default? |
|
||||||
|
| :-----: | :---------------------------------------------: | :------: |
|
||||||
|
| `mdns` | Enables mDNS device discovery on local network. | No |
|
||||||
|
| `usb` | Enables interactions with USB devices. | No |
|
||||||
|
|
||||||
|
To deactivate some features you can use the `default-features = false` option in your `Cargo.toml` file and manually specify the features you want to activate:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
adb_client = { version = "*" }
|
||||||
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Get available ADB devices
|
Usage examples can be found in the `examples/` directory of this repository.
|
||||||
|
|
||||||
```rust no_run
|
Some example are also provided in the various `README.md` files of modules.
|
||||||
use adb_client::ADBServer;
|
|
||||||
use std::net::{SocketAddrV4, Ipv4Addr};
|
|
||||||
|
|
||||||
// A custom server address can be provided
|
## Benchmarks
|
||||||
let server_ip = Ipv4Addr::new(127, 0, 0, 1);
|
|
||||||
let server_port = 5037;
|
|
||||||
|
|
||||||
let mut server = ADBServer::new(SocketAddrV4::new(server_ip, server_port));
|
Benchmarks run on `v2.0.6`, on a **Samsung S10 SM-G973F** device and an **Intel i7-1265U** CPU laptop
|
||||||
server.devices();
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using ADB server as proxy
|
### `ADBServerDevice` push vs `adb push`
|
||||||
|
|
||||||
#### [TCP] Launch a command on device
|
`ADBServerDevice` performs all operations by using adb server as a bridge.
|
||||||
|
|
||||||
```rust no_run
|
| File size | Sample size | `ADBServerDevice` | `adb` | Difference |
|
||||||
use adb_client::{ADBServer, ADBDeviceExt};
|
| :-------: | :---------: | :---------------: | :-------: | :------------------------------------: |
|
||||||
|
| 10 MB | 100 | 350,79 ms | 356,30 ms | <div style="color:green">-1,57 %</div> |
|
||||||
let mut server = ADBServer::default();
|
| 500 MB | 50 | 15,60 s | 15,64 s | <div style="color:green">-0,25 %</div> |
|
||||||
let mut device = server.get_device().expect("cannot get device");
|
| 1 GB | 20 | 31,09 s | 31,12 s | <div style="color:green">-0,10 %</div> |
|
||||||
device.shell_command(["df", "-h"],std::io::stdout());
|
|
||||||
```
|
|
||||||
|
|
||||||
#### [TCP] Push a file to the device
|
|
||||||
|
|
||||||
```rust no_run
|
|
||||||
use adb_client::ADBServer;
|
|
||||||
use std::net::Ipv4Addr;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
let mut server = ADBServer::default();
|
|
||||||
let mut device = server.get_device().expect("cannot get device");
|
|
||||||
let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file");
|
|
||||||
device.push(&mut input, "/data/local/tmp");
|
|
||||||
```
|
|
||||||
|
|
||||||
### Interacting directly with device
|
|
||||||
|
|
||||||
#### [USB] Launch a command on device
|
|
||||||
|
|
||||||
```rust no_run
|
|
||||||
use adb_client::{ADBUSBDevice, ADBDeviceExt};
|
|
||||||
|
|
||||||
let vendor_id = 0x04e8;
|
|
||||||
let product_id = 0x6860;
|
|
||||||
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
|
|
||||||
device.shell_command(["df", "-h"],std::io::stdout());
|
|
||||||
```
|
|
||||||
|
|
||||||
#### [USB] Push a file to the device
|
|
||||||
|
|
||||||
```rust no_run
|
|
||||||
use adb_client::{ADBUSBDevice, ADBDeviceExt};
|
|
||||||
use std::fs::File;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
let vendor_id = 0x04e8;
|
|
||||||
let product_id = 0x6860;
|
|
||||||
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
|
|
||||||
let mut input = File::open(Path::new("/tmp/f")).expect("Cannot open file");
|
|
||||||
device.push(&mut input, "/data/local/tmp");
|
|
||||||
```
|
|
||||||
|
|||||||
@@ -1,40 +1,37 @@
|
|||||||
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 [`ADBServerDevice`] and [`ADBUSBDevice`]
|
/// Trait representing all features available on both [`crate::server_device::ADBServerDevice`] and [`crate::usb::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<()>;
|
||||||
|
|
||||||
/// Reboots 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<()>;
|
||||||
|
|
||||||
/// Run `activity` from `package` on device. Return the command output.
|
/// Run `activity` from `package` on device. Return the command output.
|
||||||
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,38 @@ 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<()>;
|
||||||
|
|
||||||
|
/// Uninstall the package `package` from device.
|
||||||
|
fn uninstall(&mut self, package: &str) -> 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
adb_client/src/adb_transport.rs
Normal file
10
adb_client/src/adb_transport.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use crate::Result;
|
||||||
|
|
||||||
|
/// Trait representing a transport usable by ADB protocol.
|
||||||
|
pub trait ADBTransport {
|
||||||
|
/// Initializes the connection to this transport.
|
||||||
|
fn connect(&mut self) -> Result<()>;
|
||||||
|
|
||||||
|
/// Shuts down the connection to this transport.
|
||||||
|
fn disconnect(&mut self) -> Result<()>;
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
pub const BUFFER_SIZE: usize = 65536;
|
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
use std::{
|
||||||
|
net::{Ipv4Addr, SocketAddrV4},
|
||||||
|
sync::LazyLock,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{ADBTransport, Result, RustADBError, TCPEmulatorTransport};
|
use crate::{
|
||||||
use lazy_static::lazy_static;
|
ADBTransport, Result, RustADBError, emulator::tcp_emulator_transport::TCPEmulatorTransport,
|
||||||
|
server_device::ADBServerDevice,
|
||||||
|
};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use super::ADBServerDevice;
|
static EMULATOR_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
|
Regex::new("^emulator-(?P<port>\\d+)$").expect("wrong syntax for emulator regex")
|
||||||
lazy_static! {
|
});
|
||||||
pub static ref EMULATOR_REGEX: Regex =
|
|
||||||
Regex::new("^emulator-(?P<port>\\d+)$").expect("wrong syntax for emulator regex");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Represents an emulator connected to the ADB server.
|
/// Represents an emulator connected to the ADB server.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -31,8 +33,7 @@ impl ADBEmulatorDevice {
|
|||||||
let groups = EMULATOR_REGEX
|
let groups = EMULATOR_REGEX
|
||||||
.captures(&identifier)
|
.captures(&identifier)
|
||||||
.ok_or(RustADBError::DeviceNotFound(format!(
|
.ok_or(RustADBError::DeviceNotFound(format!(
|
||||||
"Device {} is likely not an emulator",
|
"Device {identifier} is likely not an emulator"
|
||||||
identifier
|
|
||||||
)))?;
|
)))?;
|
||||||
|
|
||||||
let port = groups
|
let port = groups
|
||||||
@@ -66,10 +67,15 @@ impl TryFrom<ADBServerDevice> for ADBEmulatorDevice {
|
|||||||
type Error = RustADBError;
|
type Error = RustADBError;
|
||||||
|
|
||||||
fn try_from(value: ADBServerDevice) -> std::result::Result<Self, Self::Error> {
|
fn try_from(value: ADBServerDevice) -> std::result::Result<Self, Self::Error> {
|
||||||
ADBEmulatorDevice::new(
|
match &value.identifier {
|
||||||
value.identifier.clone(),
|
Some(device_identifier) => ADBEmulatorDevice::new(
|
||||||
Some(*value.get_transport().get_socketaddr().ip()),
|
device_identifier.clone(),
|
||||||
)
|
Some(*value.transport.get_socketaddr().ip()),
|
||||||
|
),
|
||||||
|
None => Err(RustADBError::DeviceNotFound(
|
||||||
|
"cannot connect to an emulator device without knowing its identifier".to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
2
adb_client/src/emulator/commands/mod.rs
Normal file
2
adb_client/src/emulator/commands/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
mod rotate;
|
||||||
|
mod sms;
|
||||||
11
adb_client/src/emulator/commands/rotate.rs
Normal file
11
adb_client/src/emulator/commands/rotate.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
emulator::{ADBEmulatorCommand, ADBEmulatorDevice},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl ADBEmulatorDevice {
|
||||||
|
/// Send a SMS to this emulator with given content with given phone number
|
||||||
|
pub fn rotate(&mut self) -> Result<()> {
|
||||||
|
self.connect()?.send_command(ADBEmulatorCommand::Rotate)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result};
|
use crate::{
|
||||||
|
Result,
|
||||||
|
emulator::{ADBEmulatorCommand, ADBEmulatorDevice},
|
||||||
|
};
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,2 +1,7 @@
|
|||||||
mod rotate;
|
mod adb_emulator_device;
|
||||||
mod sms;
|
mod commands;
|
||||||
|
mod models;
|
||||||
|
mod tcp_emulator_transport;
|
||||||
|
|
||||||
|
pub use adb_emulator_device::ADBEmulatorDevice;
|
||||||
|
use models::ADBEmulatorCommand;
|
||||||
|
|||||||
@@ -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,
|
||||||
2
adb_client/src/emulator/models/mod.rs
Normal file
2
adb_client/src/emulator/models/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
mod adb_emulator_command;
|
||||||
|
pub use adb_emulator_command::ADBEmulatorCommand;
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
use crate::{models::ADBEmulatorCommand, ADBEmulatorDevice, Result};
|
|
||||||
|
|
||||||
impl ADBEmulatorDevice {
|
|
||||||
/// Send a SMS to this emulator with given content with given phone number
|
|
||||||
pub fn rotate(&mut self) -> Result<()> {
|
|
||||||
let transport = self.connect()?;
|
|
||||||
transport.send_command(ADBEmulatorCommand::Rotate)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
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 crate::{
|
||||||
use crate::{models::ADBEmulatorCommand, Result, RustADBError};
|
Result, RustADBError, adb_transport::ADBTransport, emulator::models::ADBEmulatorCommand,
|
||||||
|
};
|
||||||
|
|
||||||
/// Emulator transport running on top on TCP.
|
/// Emulator transport running on top on TCP.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -28,8 +29,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",
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
@@ -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,9 @@ pub enum RustADBError {
|
|||||||
#[error("Cannot get home directory")]
|
#[error("Cannot get home directory")]
|
||||||
NoHomeDirectory,
|
NoHomeDirectory,
|
||||||
/// Generic USB error
|
/// Generic USB error
|
||||||
#[error(transparent)]
|
#[cfg(feature = "usb")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "usb")))]
|
||||||
|
#[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}")]
|
||||||
@@ -82,6 +87,8 @@ pub enum RustADBError {
|
|||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Base64EncodeError(#[from] base64::EncodeSliceError),
|
Base64EncodeError(#[from] base64::EncodeSliceError),
|
||||||
/// An error occurred with RSA engine
|
/// An error occurred with RSA engine
|
||||||
|
#[cfg(feature = "usb")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "usb")))]
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
RSAError(#[from] rsa::errors::Error),
|
RSAError(#[from] rsa::errors::Error),
|
||||||
/// Cannot convert given data from slice
|
/// Cannot convert given data from slice
|
||||||
@@ -91,6 +98,40 @@ pub enum RustADBError {
|
|||||||
#[error("wrong file extension: {0}")]
|
#[error("wrong file extension: {0}")]
|
||||||
WrongFileExtension(String),
|
WrongFileExtension(String),
|
||||||
/// An error occurred with PKCS8 data
|
/// An error occurred with PKCS8 data
|
||||||
|
#[cfg(feature = "usb")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "usb")))]
|
||||||
#[error("error with pkcs8: {0}")]
|
#[error("error with pkcs8: {0}")]
|
||||||
RsaPkcs8Error(#[from] rsa::pkcs8::Error),
|
RsaPkcs8Error(#[from] rsa::pkcs8::Error),
|
||||||
|
/// Error during certificate generation
|
||||||
|
#[error(transparent)]
|
||||||
|
CertificateGenerationError(#[from] rcgen::Error),
|
||||||
|
/// TLS Error
|
||||||
|
#[error(transparent)]
|
||||||
|
TLSError(#[from] rustls::Error),
|
||||||
|
/// PEM certificate error
|
||||||
|
#[error(transparent)]
|
||||||
|
PemCertError(#[from] rustls_pki_types::pem::Error),
|
||||||
|
/// Error while locking mutex
|
||||||
|
#[error("error while locking data")]
|
||||||
|
PoisonError,
|
||||||
|
/// Cannot upgrade connection from TCP to TLS
|
||||||
|
#[error("upgrade error: {0}")]
|
||||||
|
UpgradeError(String),
|
||||||
|
/// An error occurred while getting mdns devices
|
||||||
|
#[cfg(feature = "mdns")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
|
||||||
|
#[error(transparent)]
|
||||||
|
MDNSError(#[from] mdns_sd::Error),
|
||||||
|
/// An error occurred while sending data to channel
|
||||||
|
#[error("error sending data to channel")]
|
||||||
|
SendError,
|
||||||
|
/// An unknown transport has been provided
|
||||||
|
#[error("unknown transport: {0}")]
|
||||||
|
UnknownTransport(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<std::sync::PoisonError<T>> for RustADBError {
|
||||||
|
fn from(_err: std::sync::PoisonError<T>) -> Self {
|
||||||
|
Self::PoisonError
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,34 @@
|
|||||||
#![forbid(missing_debug_implementations)]
|
#![forbid(missing_debug_implementations)]
|
||||||
#![forbid(missing_docs)]
|
#![forbid(missing_docs)]
|
||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
// Feature `doc_cfg` is currently only available on nightly.
|
||||||
|
// It is activated when cfg `docsrs` is enabled.
|
||||||
|
// Documentation can be build locally using:
|
||||||
|
// `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-features`
|
||||||
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||||
|
|
||||||
mod adb_device_ext;
|
mod adb_device_ext;
|
||||||
mod constants;
|
mod adb_transport;
|
||||||
mod emulator;
|
/// Emulator-related definitions
|
||||||
|
pub mod emulator;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod message_devices;
|
||||||
mod models;
|
mod models;
|
||||||
mod server;
|
|
||||||
mod transports;
|
/// Server-related definitions
|
||||||
mod usb;
|
pub mod server;
|
||||||
|
|
||||||
|
/// Device reachable by the server related definitions
|
||||||
|
pub mod server_device;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
/// MDNS-related definitions
|
||||||
|
#[cfg(feature = "mdns")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
|
||||||
|
pub mod mdns;
|
||||||
|
|
||||||
pub use adb_device_ext::ADBDeviceExt;
|
pub use adb_device_ext::ADBDeviceExt;
|
||||||
|
use adb_transport::ADBTransport;
|
||||||
pub use error::{Result, RustADBError};
|
pub use error::{Result, RustADBError};
|
||||||
pub use models::{AdbStatResponse, AdbVersion, DeviceLong, DeviceShort, DeviceState, RebootType};
|
pub use message_devices::*;
|
||||||
pub use server::*;
|
pub use models::{AdbStatResponse, HostFeatures, RebootType};
|
||||||
pub use transports::*;
|
|
||||||
pub use usb::ADBUSBDevice;
|
|
||||||
|
|||||||
19
adb_client/src/mdns/mdns_device.rs
Normal file
19
adb_client/src/mdns/mdns_device.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use std::{collections::HashSet, net::IpAddr};
|
||||||
|
|
||||||
|
/// Represent a device found from mdns search
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct MDNSDevice {
|
||||||
|
/// Full device address when resolved
|
||||||
|
pub fullname: String,
|
||||||
|
/// Device IP addresses
|
||||||
|
pub addresses: HashSet<IpAddr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<mdns_sd::ServiceInfo> for MDNSDevice {
|
||||||
|
fn from(value: mdns_sd::ServiceInfo) -> Self {
|
||||||
|
Self {
|
||||||
|
fullname: value.get_fullname().to_string(),
|
||||||
|
addresses: value.get_addresses().to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
75
adb_client/src/mdns/mdns_discovery.rs
Normal file
75
adb_client/src/mdns/mdns_discovery.rs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
||||||
|
use std::{sync::mpsc::Sender, thread::JoinHandle};
|
||||||
|
|
||||||
|
use crate::{Result, RustADBError, mdns::MDNSDevice};
|
||||||
|
|
||||||
|
const ADB_SERVICE_NAME: &str = "_adb-tls-connect._tcp.local.";
|
||||||
|
|
||||||
|
/// Structure holding responsibility over mdns discovery
|
||||||
|
pub struct MDNSDiscoveryService {
|
||||||
|
daemon: ServiceDaemon,
|
||||||
|
thread_handle: Option<JoinHandle<Result<()>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for MDNSDiscoveryService {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("MDNSDiscoveryService")
|
||||||
|
.field("daemon", &self.daemon.get_metrics())
|
||||||
|
.field("handle", &self.thread_handle)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MDNSDiscoveryService {
|
||||||
|
/// Instantiate a new discovery service to find devices over mdns
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
Ok(MDNSDiscoveryService {
|
||||||
|
daemon: ServiceDaemon::new()?,
|
||||||
|
thread_handle: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start discovery by spawning a new thread responsible of getting events.
|
||||||
|
pub fn start(&mut self, sender: Sender<MDNSDevice>) -> Result<()> {
|
||||||
|
let receiver = self.daemon.browse(ADB_SERVICE_NAME)?;
|
||||||
|
|
||||||
|
let handle: JoinHandle<Result<()>> = std::thread::spawn(move || {
|
||||||
|
loop {
|
||||||
|
while let Ok(event) = receiver.recv() {
|
||||||
|
match event {
|
||||||
|
ServiceEvent::SearchStarted(_)
|
||||||
|
| ServiceEvent::ServiceRemoved(_, _)
|
||||||
|
| ServiceEvent::ServiceFound(_, _)
|
||||||
|
| ServiceEvent::SearchStopped(_) => {
|
||||||
|
// Ignoring these events. We are only interesting in found devices
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ServiceEvent::ServiceResolved(service_info) => {
|
||||||
|
return sender
|
||||||
|
.send(MDNSDevice::from(service_info))
|
||||||
|
.map_err(|_| RustADBError::SendError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.thread_handle = Some(handle);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shutdown discovery engines.
|
||||||
|
pub fn shutdown(&mut self) -> Result<()> {
|
||||||
|
match self.daemon.shutdown() {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => match e {
|
||||||
|
mdns_sd::Error::Again => {
|
||||||
|
self.daemon.shutdown()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
e => Err(RustADBError::MDNSError(e)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
adb_client/src/mdns/mod.rs
Normal file
5
adb_client/src/mdns/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
mod mdns_device;
|
||||||
|
mod mdns_discovery;
|
||||||
|
|
||||||
|
pub use mdns_device::MDNSDevice;
|
||||||
|
pub use mdns_discovery::MDNSDiscoveryService;
|
||||||
251
adb_client/src/message_devices/adb_message_device.rs
Normal file
251
adb_client/src/message_devices/adb_message_device.rs
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
use rand::Rng;
|
||||||
|
use std::io::{Cursor, Read, Seek};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
AdbStatResponse, Result, RustADBError,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_transport::ADBMessageTransport,
|
||||||
|
adb_transport_message::ADBTransportMessage,
|
||||||
|
message_commands::{MessageCommand, MessageSubcommand},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const BUFFER_SIZE: usize = 65535;
|
||||||
|
|
||||||
|
/// Generic structure representing an ADB device reachable over an [`ADBMessageTransport`].
|
||||||
|
/// Structure is totally agnostic over which transport is truly used.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ADBMessageDevice<T: ADBMessageTransport> {
|
||||||
|
transport: T,
|
||||||
|
local_id: Option<u32>,
|
||||||
|
remote_id: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
/// Instantiate a new [`ADBMessageTransport`]
|
||||||
|
pub fn new(transport: T) -> Self {
|
||||||
|
Self {
|
||||||
|
transport,
|
||||||
|
local_id: None,
|
||||||
|
remote_id: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_transport(&mut self) -> &T {
|
||||||
|
&self.transport
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_transport_mut(&mut self) -> &mut T {
|
||||||
|
&mut self.transport
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Receive a message and acknowledge it by replying with an `OKAY` command
|
||||||
|
pub(crate) fn recv_and_reply_okay(&mut self) -> Result<ADBTransportMessage> {
|
||||||
|
let message = self.transport.read_message()?;
|
||||||
|
self.transport.write_message(ADBTransportMessage::new(
|
||||||
|
MessageCommand::Okay,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
&[],
|
||||||
|
))?;
|
||||||
|
Ok(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Expect a message with an `OKAY` command after sending a message.
|
||||||
|
pub(crate) fn send_and_expect_okay(
|
||||||
|
&mut self,
|
||||||
|
message: ADBTransportMessage,
|
||||||
|
) -> Result<ADBTransportMessage> {
|
||||||
|
self.transport.write_message(message)?;
|
||||||
|
|
||||||
|
self.transport.read_message().and_then(|message| {
|
||||||
|
message.assert_command(MessageCommand::Okay)?;
|
||||||
|
Ok(message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn recv_file<W: std::io::Write>(
|
||||||
|
&mut self,
|
||||||
|
mut output: W,
|
||||||
|
) -> std::result::Result<(), RustADBError> {
|
||||||
|
let mut len: Option<u64> = None;
|
||||||
|
loop {
|
||||||
|
let payload = self.recv_and_reply_okay()?.into_payload();
|
||||||
|
let mut rdr = Cursor::new(&payload);
|
||||||
|
while rdr.position() != payload.len() as u64 {
|
||||||
|
match len.take() {
|
||||||
|
Some(0) | None => {
|
||||||
|
rdr.seek_relative(4)?;
|
||||||
|
len.replace(rdr.read_u32::<LittleEndian>()? as u64);
|
||||||
|
}
|
||||||
|
Some(length) => {
|
||||||
|
let remaining_bytes = payload.len() as u64 - rdr.position();
|
||||||
|
if length < remaining_bytes {
|
||||||
|
std::io::copy(&mut rdr.by_ref().take(length), &mut output)?;
|
||||||
|
} else {
|
||||||
|
std::io::copy(&mut rdr.take(remaining_bytes), &mut output)?;
|
||||||
|
len.replace(length - remaining_bytes);
|
||||||
|
// this payload is now exhausted
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if Cursor::new(&payload[(payload.len() - 8)..(payload.len() - 4)])
|
||||||
|
.read_u32::<LittleEndian>()?
|
||||||
|
== MessageSubcommand::Done as u32
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn push_file<R: std::io::Read>(
|
||||||
|
&mut self,
|
||||||
|
local_id: u32,
|
||||||
|
remote_id: u32,
|
||||||
|
mut reader: R,
|
||||||
|
) -> std::result::Result<(), RustADBError> {
|
||||||
|
let mut buffer = [0; BUFFER_SIZE];
|
||||||
|
let amount_read = reader.read(&mut buffer)?;
|
||||||
|
let subcommand_data = MessageSubcommand::Data.with_arg(amount_read as u32);
|
||||||
|
|
||||||
|
let mut serialized_message =
|
||||||
|
bincode::serialize(&subcommand_data).map_err(|_e| RustADBError::ConversionError)?;
|
||||||
|
serialized_message.append(&mut buffer[..amount_read].to_vec());
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
local_id,
|
||||||
|
remote_id,
|
||||||
|
&serialized_message,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.send_and_expect_okay(message)?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut buffer = [0; BUFFER_SIZE];
|
||||||
|
|
||||||
|
match reader.read(&mut buffer) {
|
||||||
|
Ok(0) => {
|
||||||
|
// Currently file mtime is not forwarded
|
||||||
|
let subcommand_data = MessageSubcommand::Done.with_arg(0);
|
||||||
|
|
||||||
|
let serialized_message = bincode::serialize(&subcommand_data)
|
||||||
|
.map_err(|_e| RustADBError::ConversionError)?;
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
local_id,
|
||||||
|
remote_id,
|
||||||
|
&serialized_message,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.send_and_expect_okay(message)?;
|
||||||
|
|
||||||
|
// Command should end with a Write => Okay
|
||||||
|
let received = self.transport.read_message()?;
|
||||||
|
match received.header().command() {
|
||||||
|
MessageCommand::Write => return Ok(()),
|
||||||
|
c => {
|
||||||
|
return Err(RustADBError::ADBRequestFailed(format!(
|
||||||
|
"Wrong command received {c}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(size) => {
|
||||||
|
let subcommand_data = MessageSubcommand::Data.with_arg(size as u32);
|
||||||
|
|
||||||
|
let mut serialized_message = bincode::serialize(&subcommand_data)
|
||||||
|
.map_err(|_e| RustADBError::ConversionError)?;
|
||||||
|
serialized_message.append(&mut buffer[..size].to_vec());
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
local_id,
|
||||||
|
remote_id,
|
||||||
|
&serialized_message,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.send_and_expect_okay(message)?;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err(RustADBError::IOError(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn begin_synchronization(&mut self) -> Result<()> {
|
||||||
|
self.open_session(b"sync:\0")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn stat_with_explicit_ids(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
||||||
|
let stat_buffer = MessageSubcommand::Stat.with_arg(remote_path.len() as u32);
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
&bincode::serialize(&stat_buffer).map_err(|_e| RustADBError::ConversionError)?,
|
||||||
|
);
|
||||||
|
self.send_and_expect_okay(message)?;
|
||||||
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
remote_path.as_bytes(),
|
||||||
|
))?;
|
||||||
|
let response = self.transport.read_message()?;
|
||||||
|
// Skip first 4 bytes as this is the literal "STAT".
|
||||||
|
// Interesting part starts right after
|
||||||
|
bincode::deserialize(&response.into_payload()[4..])
|
||||||
|
.map_err(|_e| RustADBError::ConversionError)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn end_transaction(&mut self) -> Result<()> {
|
||||||
|
let quit_buffer = MessageSubcommand::Quit.with_arg(0u32);
|
||||||
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
&bincode::serialize(&quit_buffer).map_err(|_e| RustADBError::ConversionError)?,
|
||||||
|
))?;
|
||||||
|
let _discard_close = self.transport.read_message()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBTransportMessage> {
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Open,
|
||||||
|
rng.random(), // 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(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
use crate::{
|
||||||
|
ADBDeviceExt, RebootType, Result,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
},
|
||||||
|
models::AdbStatResponse,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
io::{Read, Write},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
|
||||||
|
fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
|
||||||
|
self.shell_command(command, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shell(&mut self, reader: &mut dyn Read, writer: Box<dyn Write + Send>) -> Result<()> {
|
||||||
|
self.shell(reader, writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
||||||
|
self.stat(remote_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()> {
|
||||||
|
self.pull(source, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
|
||||||
|
self.push(stream, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
|
||||||
|
self.reboot(reboot_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
|
||||||
|
self.install(apk_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
|
self.framebuffer_inner()
|
||||||
|
}
|
||||||
|
}
|
||||||
32
adb_client/src/message_devices/adb_message_transport.rs
Normal file
32
adb_client/src/message_devices/adb_message_transport.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, adb_transport::ADBTransport,
|
||||||
|
message_devices::adb_transport_message::ADBTransportMessage,
|
||||||
|
};
|
||||||
|
|
||||||
|
const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(u64::MAX);
|
||||||
|
const DEFAULT_WRITE_TIMEOUT: Duration = Duration::from_secs(2);
|
||||||
|
|
||||||
|
/// Trait representing a transport able to read and write messages.
|
||||||
|
pub trait ADBMessageTransport: ADBTransport + Clone + Send + 'static {
|
||||||
|
/// Read a message using given timeout on the underlying transport
|
||||||
|
fn read_message_with_timeout(&mut self, read_timeout: Duration) -> Result<ADBTransportMessage>;
|
||||||
|
|
||||||
|
/// Read data to underlying connection, using default timeout
|
||||||
|
fn read_message(&mut self) -> Result<ADBTransportMessage> {
|
||||||
|
self.read_message_with_timeout(DEFAULT_READ_TIMEOUT)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write a message using given timeout on the underlying transport
|
||||||
|
fn write_message_with_timeout(
|
||||||
|
&mut self,
|
||||||
|
message: ADBTransportMessage,
|
||||||
|
write_timeout: Duration,
|
||||||
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Write data to underlying connection, using default timeout
|
||||||
|
fn write_message(&mut self, message: ADBTransportMessage) -> Result<()> {
|
||||||
|
self.write_message_with_timeout(message, DEFAULT_WRITE_TIMEOUT)
|
||||||
|
}
|
||||||
|
}
|
||||||
116
adb_client/src/message_devices/adb_transport_message.rs
Normal file
116
adb_client/src/message_devices/adb_transport_message.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::{Result, RustADBError, message_devices::message_commands::MessageCommand};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ADBTransportMessage {
|
||||||
|
header: ADBTransportMessageHeader,
|
||||||
|
payload: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct ADBTransportMessageHeader {
|
||||||
|
command: MessageCommand, /* command identifier constant */
|
||||||
|
arg0: u32, /* first argument */
|
||||||
|
arg1: u32, /* second argument */
|
||||||
|
data_length: u32, /* length of payload (0 is allowed) */
|
||||||
|
data_crc32: u32, /* crc32 of data payload */
|
||||||
|
magic: u32, /* command ^ 0xffffffff */
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBTransportMessageHeader {
|
||||||
|
pub fn new(command: MessageCommand, arg0: u32, arg1: u32, data: &[u8]) -> Self {
|
||||||
|
Self {
|
||||||
|
command,
|
||||||
|
arg0,
|
||||||
|
arg1,
|
||||||
|
data_length: data.len() as u32,
|
||||||
|
data_crc32: Self::compute_crc32(data),
|
||||||
|
magic: Self::compute_magic(command),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn command(&self) -> MessageCommand {
|
||||||
|
self.command
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arg0(&self) -> u32 {
|
||||||
|
self.arg0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arg1(&self) -> u32 {
|
||||||
|
self.arg1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn data_length(&self) -> u32 {
|
||||||
|
self.data_length
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn data_crc32(&self) -> u32 {
|
||||||
|
self.data_crc32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn compute_crc32(data: &[u8]) -> u32 {
|
||||||
|
data.iter().map(|&x| x as u32).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute_magic(command: MessageCommand) -> u32 {
|
||||||
|
let command_u32 = command as u32;
|
||||||
|
command_u32 ^ 0xFFFFFFFF
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_bytes(&self) -> Result<Vec<u8>> {
|
||||||
|
bincode::serialize(&self).map_err(|_e| RustADBError::ConversionError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBTransportMessage {
|
||||||
|
pub fn new(command: MessageCommand, arg0: u32, arg1: u32, data: &[u8]) -> Self {
|
||||||
|
Self {
|
||||||
|
header: ADBTransportMessageHeader::new(command, arg0, arg1, data),
|
||||||
|
payload: data.to_vec(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_header_and_payload(header: ADBTransportMessageHeader, payload: Vec<u8>) -> Self {
|
||||||
|
Self { header, payload }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_message_integrity(&self) -> bool {
|
||||||
|
ADBTransportMessageHeader::compute_magic(self.header.command) == self.header.magic
|
||||||
|
&& 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 {
|
||||||
|
&self.header
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn payload(&self) -> &Vec<u8> {
|
||||||
|
&self.payload
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_payload(self) -> Vec<u8> {
|
||||||
|
self.payload
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<[u8; 24]> for ADBTransportMessageHeader {
|
||||||
|
type Error = RustADBError;
|
||||||
|
|
||||||
|
fn try_from(value: [u8; 24]) -> Result<Self> {
|
||||||
|
bincode::deserialize(&value).map_err(|_e| RustADBError::ConversionError)
|
||||||
|
}
|
||||||
|
}
|
||||||
101
adb_client/src/message_devices/commands/framebuffer.rs
Normal file
101
adb_client/src/message_devices/commands/framebuffer.rs
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
use std::io::{Cursor, Read};
|
||||||
|
|
||||||
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
use image::{ImageBuffer, Rgba};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
},
|
||||||
|
models::{FrameBufferInfoV1, FrameBufferInfoV2},
|
||||||
|
};
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
43
adb_client/src/message_devices/commands/install.rs
Normal file
43
adb_client/src/message_devices/commands/install.rs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
use std::{fs::File, path::Path};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
commands::utils::MessageWriter,
|
||||||
|
},
|
||||||
|
utils::check_extension_is_apk,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
|
||||||
|
let mut apk_file = File::open(apk_path)?;
|
||||||
|
|
||||||
|
check_extension_is_apk(apk_path)?;
|
||||||
|
|
||||||
|
let file_size = apk_file.metadata()?.len();
|
||||||
|
|
||||||
|
self.open_session(format!("exec:cmd package 'install' -S {file_size}\0").as_bytes())?;
|
||||||
|
|
||||||
|
let transport = self.get_transport().clone();
|
||||||
|
|
||||||
|
let mut writer = MessageWriter::new(transport, self.get_local_id()?, self.get_remote_id()?);
|
||||||
|
|
||||||
|
std::io::copy(&mut apk_file, &mut writer)?;
|
||||||
|
|
||||||
|
let final_status = self.get_transport_mut().read_message()?;
|
||||||
|
|
||||||
|
match final_status.into_payload().as_slice() {
|
||||||
|
b"Success\n" => {
|
||||||
|
log::info!(
|
||||||
|
"APK file {} successfully installed",
|
||||||
|
apk_path.as_ref().display()
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||||
|
d.to_vec(),
|
||||||
|
)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
adb_client/src/message_devices/commands/mod.rs
Normal file
9
adb_client/src/message_devices/commands/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
mod framebuffer;
|
||||||
|
mod install;
|
||||||
|
mod pull;
|
||||||
|
mod push;
|
||||||
|
mod reboot;
|
||||||
|
mod shell;
|
||||||
|
mod stat;
|
||||||
|
mod uninstall;
|
||||||
|
mod utils;
|
||||||
54
adb_client/src/message_devices/commands/pull.rs
Normal file
54
adb_client/src/message_devices/commands/pull.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice,
|
||||||
|
adb_message_transport::ADBMessageTransport,
|
||||||
|
adb_transport_message::ADBTransportMessage,
|
||||||
|
message_commands::{MessageCommand, MessageSubcommand},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> {
|
||||||
|
self.begin_synchronization()?;
|
||||||
|
let source = source.as_ref();
|
||||||
|
|
||||||
|
let adb_stat_response = self.stat_with_explicit_ids(source)?;
|
||||||
|
|
||||||
|
if adb_stat_response.file_perm == 0 {
|
||||||
|
return Err(RustADBError::UnknownResponseType(
|
||||||
|
"mode is 0: source file does not exist".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let local_id = self.get_local_id()?;
|
||||||
|
let remote_id = self.get_remote_id()?;
|
||||||
|
|
||||||
|
self.get_transport_mut().write_message_with_timeout(
|
||||||
|
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]),
|
||||||
|
std::time::Duration::from_secs(4),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let recv_buffer = MessageSubcommand::Recv.with_arg(source.len() as u32);
|
||||||
|
let recv_buffer =
|
||||||
|
bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?;
|
||||||
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
&recv_buffer,
|
||||||
|
))?;
|
||||||
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
source.as_bytes(),
|
||||||
|
))?;
|
||||||
|
|
||||||
|
self.recv_file(output)?;
|
||||||
|
self.end_transaction()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
37
adb_client/src/message_devices/commands/push.rs
Normal file
37
adb_client/src/message_devices/commands/push.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice,
|
||||||
|
adb_message_transport::ADBMessageTransport,
|
||||||
|
adb_transport_message::ADBTransportMessage,
|
||||||
|
message_commands::{MessageCommand, MessageSubcommand},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
|
||||||
|
self.begin_synchronization()?;
|
||||||
|
|
||||||
|
let path_header = format!("{},0777", path.as_ref());
|
||||||
|
|
||||||
|
let send_buffer = MessageSubcommand::Send.with_arg(path_header.len() as u32);
|
||||||
|
let mut send_buffer =
|
||||||
|
bincode::serialize(&send_buffer).map_err(|_e| RustADBError::ConversionError)?;
|
||||||
|
send_buffer.append(&mut path_header.as_bytes().to_vec());
|
||||||
|
|
||||||
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
|
MessageCommand::Write,
|
||||||
|
self.get_local_id()?,
|
||||||
|
self.get_remote_id()?,
|
||||||
|
&send_buffer,
|
||||||
|
))?;
|
||||||
|
|
||||||
|
self.push_file(self.get_local_id()?, self.get_remote_id()?, stream)?;
|
||||||
|
|
||||||
|
self.end_transaction()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
17
adb_client/src/message_devices/commands/reboot.rs
Normal file
17
adb_client/src/message_devices/commands/reboot.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use crate::{
|
||||||
|
RebootType, Result,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
|
||||||
|
self.open_session(format!("reboot:{reboot_type}\0").as_bytes())?;
|
||||||
|
|
||||||
|
self.get_transport_mut()
|
||||||
|
.read_message()
|
||||||
|
.and_then(|message| message.assert_command(MessageCommand::Okay))
|
||||||
|
}
|
||||||
|
}
|
||||||
84
adb_client/src/message_devices/commands/shell.rs
Normal file
84
adb_client/src/message_devices/commands/shell.rs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
use std::io::{ErrorKind, Read, Write};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
adb_transport_message::ADBTransportMessage, commands::utils::ShellMessageWriter,
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
/// Runs 'command' in a shell on the device, and write its output and error streams into output.
|
||||||
|
pub(crate) fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
|
||||||
|
let response = self.open_session(format!("shell:{}\0", command.join(" "),).as_bytes())?;
|
||||||
|
|
||||||
|
if response.header().command() != MessageCommand::Okay {
|
||||||
|
return Err(RustADBError::ADBRequestFailed(format!(
|
||||||
|
"wrong command {}",
|
||||||
|
response.header().command()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let response = self.get_transport_mut().read_message()?;
|
||||||
|
if response.header().command() != MessageCommand::Write {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
output.write_all(&response.into_payload())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Starts an interactive shell session on the device.
|
||||||
|
/// Input data is read from [reader] and write to [writer].
|
||||||
|
pub(crate) fn shell(
|
||||||
|
&mut self,
|
||||||
|
mut reader: &mut dyn Read,
|
||||||
|
mut writer: Box<dyn Write + Send>,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.open_session(b"shell:\0")?;
|
||||||
|
|
||||||
|
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
|
||||||
|
std::thread::spawn(move || -> Result<()> {
|
||||||
|
loop {
|
||||||
|
let message = transport.read_message()?;
|
||||||
|
|
||||||
|
// Acknowledge for more data
|
||||||
|
let response =
|
||||||
|
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]);
|
||||||
|
transport.write_message(response)?;
|
||||||
|
|
||||||
|
match message.header().command() {
|
||||||
|
MessageCommand::Write => {
|
||||||
|
writer.write_all(&message.into_payload())?;
|
||||||
|
writer.flush()?;
|
||||||
|
}
|
||||||
|
MessageCommand::Okay => continue,
|
||||||
|
_ => return Err(RustADBError::ADBShellNotSupported),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let transport = self.get_transport().clone();
|
||||||
|
let mut shell_writer = ShellMessageWriter::new(transport, local_id, remote_id);
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
match e.kind() {
|
||||||
|
ErrorKind::BrokenPipe => return Ok(()),
|
||||||
|
_ => return Err(RustADBError::IOError(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
15
adb_client/src/message_devices/commands/stat.rs
Normal file
15
adb_client/src/message_devices/commands/stat.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use crate::{
|
||||||
|
AdbStatResponse, Result,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
||||||
|
self.begin_synchronization()?;
|
||||||
|
let adb_stat_response = self.stat_with_explicit_ids(remote_path)?;
|
||||||
|
self.end_transaction()?;
|
||||||
|
Ok(adb_stat_response)
|
||||||
|
}
|
||||||
|
}
|
||||||
24
adb_client/src/message_devices/commands/uninstall.rs
Normal file
24
adb_client/src/message_devices/commands/uninstall.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_device::ADBMessageDevice, adb_message_transport::ADBMessageTransport,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> {
|
||||||
|
self.open_session(format!("exec:cmd package 'uninstall' {package_name}\0").as_bytes())?;
|
||||||
|
|
||||||
|
let final_status = self.get_transport_mut().read_message()?;
|
||||||
|
|
||||||
|
match final_status.into_payload().as_slice() {
|
||||||
|
b"Success\n" => {
|
||||||
|
log::info!("Package {package_name} successfully uninstalled");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||||
|
d.to_vec(),
|
||||||
|
)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
use std::io::{Error, ErrorKind, Result, Write};
|
||||||
|
|
||||||
|
use crate::message_devices::{
|
||||||
|
adb_message_transport::ADBMessageTransport, adb_transport_message::ADBTransportMessage,
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// [`Write`] trait implementation to hide underlying ADB protocol write logic.
|
||||||
|
///
|
||||||
|
/// Read received responses to check that message has been correctly received.
|
||||||
|
pub struct MessageWriter<T: ADBMessageTransport> {
|
||||||
|
transport: T,
|
||||||
|
local_id: u32,
|
||||||
|
remote_id: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> MessageWriter<T> {
|
||||||
|
pub fn new(transport: T, local_id: u32, remote_id: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
transport,
|
||||||
|
local_id,
|
||||||
|
remote_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> Write for MessageWriter<T> {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||||
|
let message =
|
||||||
|
ADBTransportMessage::new(MessageCommand::Write, self.local_id, self.remote_id, buf);
|
||||||
|
self.transport
|
||||||
|
.write_message(message)
|
||||||
|
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
|
||||||
|
|
||||||
|
match self.transport.read_message() {
|
||||||
|
Ok(response) => {
|
||||||
|
response
|
||||||
|
.assert_command(MessageCommand::Okay)
|
||||||
|
.map_err(Error::other)?;
|
||||||
|
Ok(buf.len())
|
||||||
|
}
|
||||||
|
Err(e) => Err(Error::other(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
4
adb_client/src/message_devices/commands/utils/mod.rs
Normal file
4
adb_client/src/message_devices/commands/utils/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
mod message_writer;
|
||||||
|
mod shell_message_writer;
|
||||||
|
pub use message_writer::MessageWriter;
|
||||||
|
pub use shell_message_writer::ShellMessageWriter;
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use crate::message_devices::{
|
||||||
|
adb_message_transport::ADBMessageTransport, adb_transport_message::ADBTransportMessage,
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// [`Write`] trait implementation to hide underlying ADB protocol write logic for shell commands.
|
||||||
|
pub struct ShellMessageWriter<T: ADBMessageTransport> {
|
||||||
|
transport: T,
|
||||||
|
local_id: u32,
|
||||||
|
remote_id: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ShellMessageWriter<T> {
|
||||||
|
pub fn new(transport: T, local_id: u32, remote_id: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
transport,
|
||||||
|
local_id,
|
||||||
|
remote_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> Write for ShellMessageWriter<T> {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
|
let message =
|
||||||
|
ADBTransportMessage::new(MessageCommand::Write, self.local_id, self.remote_id, buf);
|
||||||
|
self.transport
|
||||||
|
.write_message(message)
|
||||||
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
|
||||||
|
Ok(buf.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,43 +4,43 @@ use std::fmt::Display;
|
|||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum USBCommand {
|
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
|
||||||
// Stls 0x534C5453
|
Stls = 0x534C5453,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize_repr, Deserialize_repr)]
|
#[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize_repr, Deserialize_repr)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum USBSubcommand {
|
pub enum MessageSubcommand {
|
||||||
Stat = 0x54415453,
|
Stat = 0x54415453,
|
||||||
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)]
|
||||||
pub struct SubcommandWithArg {
|
pub struct SubcommandWithArg {
|
||||||
subcommand: USBSubcommand,
|
subcommand: MessageSubcommand,
|
||||||
arg: u32,
|
arg: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl USBSubcommand {
|
impl MessageSubcommand {
|
||||||
pub fn with_arg(self, arg: u32) -> SubcommandWithArg {
|
pub fn with_arg(self, arg: u32) -> SubcommandWithArg {
|
||||||
SubcommandWithArg {
|
SubcommandWithArg {
|
||||||
subcommand: self,
|
subcommand: self,
|
||||||
@@ -49,15 +49,16 @@ impl USBSubcommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for USBCommand {
|
impl Display for MessageCommand {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
USBCommand::Cnxn => write!(f, "CNXN"),
|
MessageCommand::Cnxn => write!(f, "CNXN"),
|
||||||
USBCommand::Clse => write!(f, "CLSE"),
|
MessageCommand::Clse => write!(f, "CLSE"),
|
||||||
USBCommand::Auth => write!(f, "AUTH"),
|
MessageCommand::Auth => write!(f, "AUTH"),
|
||||||
USBCommand::Open => write!(f, "OPEN"),
|
MessageCommand::Open => write!(f, "OPEN"),
|
||||||
USBCommand::Write => write!(f, "WRTE"),
|
MessageCommand::Write => write!(f, "WRTE"),
|
||||||
USBCommand::Okay => write!(f, "OKAY"),
|
MessageCommand::Okay => write!(f, "OKAY"),
|
||||||
|
MessageCommand::Stls => write!(f, "STLS"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
14
adb_client/src/message_devices/mod.rs
Normal file
14
adb_client/src/message_devices/mod.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/// USB-related definitions
|
||||||
|
#[cfg(feature = "usb")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "usb")))]
|
||||||
|
pub mod usb;
|
||||||
|
|
||||||
|
/// Device reachable over TCP related definition
|
||||||
|
pub mod tcp;
|
||||||
|
|
||||||
|
mod adb_message_device;
|
||||||
|
mod adb_message_device_commands;
|
||||||
|
mod adb_message_transport;
|
||||||
|
mod adb_transport_message;
|
||||||
|
mod commands;
|
||||||
|
mod message_commands;
|
||||||
13
adb_client/src/message_devices/tcp/README.md
Normal file
13
adb_client/src/message_devices/tcp/README.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Examples
|
||||||
|
|
||||||
|
## Get a shell from device
|
||||||
|
|
||||||
|
```rust no_run
|
||||||
|
use std::net::{SocketAddr, IpAddr, Ipv4Addr};
|
||||||
|
use adb_client::{tcp::ADBTcpDevice, ADBDeviceExt};
|
||||||
|
|
||||||
|
let device_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 10));
|
||||||
|
let device_port = 43210;
|
||||||
|
let mut device = ADBTcpDevice::new(SocketAddr::new(device_ip, device_port)).expect("cannot find device");
|
||||||
|
device.shell(&mut std::io::stdin(), Box::new(std::io::stdout()));
|
||||||
|
```
|
||||||
125
adb_client/src/message_devices/tcp/adb_tcp_device.rs
Normal file
125
adb_client/src/message_devices/tcp/adb_tcp_device.rs
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::{io::Read, net::SocketAddr};
|
||||||
|
|
||||||
|
use crate::message_devices::adb_message_device::ADBMessageDevice;
|
||||||
|
use crate::message_devices::adb_message_transport::ADBMessageTransport;
|
||||||
|
use crate::message_devices::adb_transport_message::ADBTransportMessage;
|
||||||
|
use crate::message_devices::message_commands::MessageCommand;
|
||||||
|
use crate::tcp::tcp_transport::TcpTransport;
|
||||||
|
use crate::{ADBDeviceExt, ADBTransport, Result};
|
||||||
|
|
||||||
|
/// Represent a device reached and available over USB.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ADBTcpDevice {
|
||||||
|
inner: ADBMessageDevice<TcpTransport>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBTcpDevice {
|
||||||
|
/// Instantiate a new [`ADBTcpDevice`]
|
||||||
|
pub fn new(address: SocketAddr) -> Result<Self> {
|
||||||
|
let mut device = Self {
|
||||||
|
inner: ADBMessageDevice::new(TcpTransport::new(address)?),
|
||||||
|
};
|
||||||
|
|
||||||
|
device.connect()?;
|
||||||
|
|
||||||
|
Ok(device)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send initial connect
|
||||||
|
pub fn connect(&mut self) -> Result<()> {
|
||||||
|
self.get_transport_mut().connect()?;
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Cnxn,
|
||||||
|
0x01000000,
|
||||||
|
1048576,
|
||||||
|
format!("host::{}\0", env!("CARGO_PKG_NAME")).as_bytes(),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.get_transport_mut().write_message(message)?;
|
||||||
|
|
||||||
|
let message = self.get_transport_mut().read_message()?;
|
||||||
|
|
||||||
|
// Check if client is requesting a secure connection and upgrade it if necessary
|
||||||
|
match message.header().command() {
|
||||||
|
MessageCommand::Stls => {
|
||||||
|
self.get_transport_mut()
|
||||||
|
.write_message(ADBTransportMessage::new(MessageCommand::Stls, 1, 0, &[]))?;
|
||||||
|
self.get_transport_mut().upgrade_connection()?;
|
||||||
|
log::debug!("Connection successfully upgraded from TCP to TLS");
|
||||||
|
}
|
||||||
|
MessageCommand::Cnxn => {
|
||||||
|
log::debug!("Unencrypted connection established");
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(crate::RustADBError::WrongResponseReceived(
|
||||||
|
"Expected CNXN or STLS command".to_string(),
|
||||||
|
message.header().command().to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn get_transport_mut(&mut self) -> &mut TcpTransport {
|
||||||
|
self.inner.get_transport_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBDeviceExt for ADBTcpDevice {
|
||||||
|
#[inline]
|
||||||
|
fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
|
||||||
|
self.inner.shell_command(command, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn shell(&mut self, reader: &mut dyn Read, writer: Box<dyn Write + Send>) -> Result<()> {
|
||||||
|
self.inner.shell(reader, writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn stat(&mut self, remote_path: &str) -> Result<crate::AdbStatResponse> {
|
||||||
|
self.inner.stat(remote_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()> {
|
||||||
|
self.inner.pull(source, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
|
||||||
|
self.inner.push(stream, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> {
|
||||||
|
self.inner.reboot(reboot_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
|
||||||
|
self.inner.install(apk_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.inner.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
|
self.inner.framebuffer_inner()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ADBTcpDevice {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Best effort here
|
||||||
|
let _ = self.get_transport_mut().disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
6
adb_client/src/message_devices/tcp/mod.rs
Normal file
6
adb_client/src/message_devices/tcp/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#![doc = include_str!("./README.md")]
|
||||||
|
|
||||||
|
mod adb_tcp_device;
|
||||||
|
mod tcp_transport;
|
||||||
|
|
||||||
|
pub use adb_tcp_device::ADBTcpDevice;
|
||||||
346
adb_client/src/message_devices/tcp/tcp_transport.rs
Normal file
346
adb_client/src/message_devices/tcp/tcp_transport.rs
Normal file
@@ -0,0 +1,346 @@
|
|||||||
|
use rcgen::{CertificateParams, KeyPair, PKCS_RSA_SHA256};
|
||||||
|
use rustls::{
|
||||||
|
ClientConfig, ClientConnection, KeyLogFile, SignatureScheme, StreamOwned,
|
||||||
|
client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
|
||||||
|
pki_types::{CertificateDer, PrivatePkcs8KeyDer, pem::PemObject},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
adb_transport::ADBTransport,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_transport::ADBMessageTransport,
|
||||||
|
adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader},
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
},
|
||||||
|
utils::get_default_adb_key_path,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
fs::read_to_string,
|
||||||
|
io::{Read, Write},
|
||||||
|
net::{Shutdown, SocketAddr, TcpStream},
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
|
path::PathBuf,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum CurrentConnection {
|
||||||
|
Tcp(TcpStream),
|
||||||
|
Tls(Box<StreamOwned<ClientConnection, TcpStream>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CurrentConnection {
|
||||||
|
fn set_read_timeout(&self, read_timeout: Duration) -> Result<()> {
|
||||||
|
match self {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => {
|
||||||
|
Ok(tcp_stream.set_read_timeout(Some(read_timeout))?)
|
||||||
|
}
|
||||||
|
CurrentConnection::Tls(stream_owned) => {
|
||||||
|
Ok(stream_owned.sock.set_read_timeout(Some(read_timeout))?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_write_timeout(&self, write_timeout: Duration) -> Result<()> {
|
||||||
|
match self {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => {
|
||||||
|
Ok(tcp_stream.set_write_timeout(Some(write_timeout))?)
|
||||||
|
}
|
||||||
|
CurrentConnection::Tls(stream_owned) => {
|
||||||
|
Ok(stream_owned.sock.set_write_timeout(Some(write_timeout))?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Read for CurrentConnection {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||||
|
match self {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => tcp_stream.read(buf),
|
||||||
|
CurrentConnection::Tls(tls_conn) => tls_conn.read(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for CurrentConnection {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
|
match self {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => tcp_stream.write(buf),
|
||||||
|
CurrentConnection::Tls(tls_conn) => tls_conn.write(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
|
match self {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => tcp_stream.flush(),
|
||||||
|
CurrentConnection::Tls(tls_conn) => tls_conn.flush(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transport running on USB
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct TcpTransport {
|
||||||
|
address: SocketAddr,
|
||||||
|
current_connection: Option<Arc<Mutex<CurrentConnection>>>,
|
||||||
|
private_key_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn certificate_from_pk(key_pair: &KeyPair) -> Result<Vec<CertificateDer<'static>>> {
|
||||||
|
let certificate_params = CertificateParams::default();
|
||||||
|
let certificate = certificate_params.self_signed(key_pair)?;
|
||||||
|
Ok(vec![certificate.der().to_owned()])
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TcpTransport {
|
||||||
|
/// Instantiate a new [`TcpTransport`]
|
||||||
|
pub fn new(address: SocketAddr) -> Result<Self> {
|
||||||
|
Self::new_with_custom_private_key(address, get_default_adb_key_path()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Instantiate a new [`TcpTransport`] using a given private key
|
||||||
|
pub fn new_with_custom_private_key(
|
||||||
|
address: SocketAddr,
|
||||||
|
private_key_path: PathBuf,
|
||||||
|
) -> Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
address,
|
||||||
|
current_connection: None,
|
||||||
|
private_key_path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_connection(&self) -> Result<Arc<Mutex<CurrentConnection>>> {
|
||||||
|
self.current_connection
|
||||||
|
.as_ref()
|
||||||
|
.ok_or(RustADBError::IOError(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::NotConnected,
|
||||||
|
"not connected",
|
||||||
|
)))
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn upgrade_connection(&mut self) -> Result<()> {
|
||||||
|
let current_connection = match self.current_connection.clone() {
|
||||||
|
Some(current_connection) => current_connection,
|
||||||
|
None => {
|
||||||
|
return Err(RustADBError::UpgradeError(
|
||||||
|
"cannot upgrade a non-existing connection...".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut current_conn_locked = current_connection.lock()?;
|
||||||
|
match current_conn_locked.deref() {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => {
|
||||||
|
// TODO: Check if we cannot be more precise
|
||||||
|
|
||||||
|
let pk_content = read_to_string(&self.private_key_path)?;
|
||||||
|
|
||||||
|
let key_pair =
|
||||||
|
KeyPair::from_pkcs8_pem_and_sign_algo(&pk_content, &PKCS_RSA_SHA256)?;
|
||||||
|
|
||||||
|
let certificate = certificate_from_pk(&key_pair)?;
|
||||||
|
let private_key = PrivatePkcs8KeyDer::from_pem_file(&self.private_key_path)?;
|
||||||
|
|
||||||
|
let mut client_config = ClientConfig::builder()
|
||||||
|
.dangerous()
|
||||||
|
.with_custom_certificate_verifier(Arc::new(NoCertificateVerification {}))
|
||||||
|
.with_client_auth_cert(certificate, private_key.into())?;
|
||||||
|
|
||||||
|
client_config.key_log = Arc::new(KeyLogFile::new());
|
||||||
|
|
||||||
|
let rc_config = Arc::new(client_config);
|
||||||
|
let server_name = self.address.ip().into();
|
||||||
|
let conn = ClientConnection::new(rc_config, server_name)?;
|
||||||
|
let owned = tcp_stream.try_clone()?;
|
||||||
|
let client = StreamOwned::new(conn, owned);
|
||||||
|
|
||||||
|
// Update current connection state to now use TLS protocol
|
||||||
|
*current_conn_locked = CurrentConnection::Tls(Box::new(client));
|
||||||
|
}
|
||||||
|
CurrentConnection::Tls(_) => {
|
||||||
|
return Err(RustADBError::UpgradeError(
|
||||||
|
"cannot upgrade a TLS connection...".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = self.read_message()?;
|
||||||
|
match message.header().command() {
|
||||||
|
MessageCommand::Cnxn => {
|
||||||
|
let device_infos = String::from_utf8(message.into_payload())?;
|
||||||
|
log::debug!("received device info: {device_infos}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
c => Err(RustADBError::ADBRequestFailed(format!(
|
||||||
|
"Wrong command received {c}"
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBTransport for TcpTransport {
|
||||||
|
fn connect(&mut self) -> Result<()> {
|
||||||
|
let stream = TcpStream::connect(self.address)?;
|
||||||
|
self.current_connection = Some(Arc::new(Mutex::new(CurrentConnection::Tcp(stream))));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disconnect(&mut self) -> Result<()> {
|
||||||
|
log::debug!("disconnecting...");
|
||||||
|
if let Some(current_connection) = &self.current_connection {
|
||||||
|
let mut lock = current_connection.lock()?;
|
||||||
|
match lock.deref_mut() {
|
||||||
|
CurrentConnection::Tcp(tcp_stream) => {
|
||||||
|
let _ = tcp_stream.shutdown(Shutdown::Both);
|
||||||
|
}
|
||||||
|
CurrentConnection::Tls(tls_conn) => {
|
||||||
|
tls_conn.conn.send_close_notify();
|
||||||
|
let _ = tls_conn.sock.shutdown(Shutdown::Both);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBMessageTransport for TcpTransport {
|
||||||
|
fn read_message_with_timeout(
|
||||||
|
&mut self,
|
||||||
|
read_timeout: std::time::Duration,
|
||||||
|
) -> Result<ADBTransportMessage> {
|
||||||
|
let raw_connection_lock = self.get_current_connection()?;
|
||||||
|
let mut raw_connection = raw_connection_lock.lock()?;
|
||||||
|
|
||||||
|
raw_connection.set_read_timeout(read_timeout)?;
|
||||||
|
|
||||||
|
let mut data = [0; 24];
|
||||||
|
let mut total_read = 0;
|
||||||
|
loop {
|
||||||
|
total_read += raw_connection.read(&mut data[total_read..])?;
|
||||||
|
if total_read == data.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let header = ADBTransportMessageHeader::try_from(data)?;
|
||||||
|
|
||||||
|
if header.data_length() != 0 {
|
||||||
|
let mut msg_data = vec![0_u8; header.data_length() as usize];
|
||||||
|
let mut total_read = 0;
|
||||||
|
loop {
|
||||||
|
total_read += raw_connection.read(&mut msg_data[total_read..])?;
|
||||||
|
if total_read == msg_data.capacity() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::from_header_and_payload(header, msg_data);
|
||||||
|
|
||||||
|
// Check message integrity
|
||||||
|
if !message.check_message_integrity() {
|
||||||
|
return Err(RustADBError::InvalidIntegrity(
|
||||||
|
ADBTransportMessageHeader::compute_crc32(message.payload()),
|
||||||
|
message.header().data_crc32(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ADBTransportMessage::from_header_and_payload(header, vec![]))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_message_with_timeout(
|
||||||
|
&mut self,
|
||||||
|
message: ADBTransportMessage,
|
||||||
|
write_timeout: Duration,
|
||||||
|
) -> Result<()> {
|
||||||
|
let message_bytes = message.header().as_bytes()?;
|
||||||
|
let raw_connection_lock = self.get_current_connection()?;
|
||||||
|
let mut raw_connection = raw_connection_lock.lock()?;
|
||||||
|
|
||||||
|
raw_connection.set_write_timeout(write_timeout)?;
|
||||||
|
|
||||||
|
let mut total_written = 0;
|
||||||
|
loop {
|
||||||
|
total_written += raw_connection.write(&message_bytes[total_written..])?;
|
||||||
|
if total_written == message_bytes.len() {
|
||||||
|
raw_connection.flush()?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let payload = message.into_payload();
|
||||||
|
if !payload.is_empty() {
|
||||||
|
let mut total_written = 0;
|
||||||
|
loop {
|
||||||
|
total_written += raw_connection.write(&payload[total_written..])?;
|
||||||
|
if total_written == payload.len() {
|
||||||
|
raw_connection.flush()?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct NoCertificateVerification;
|
||||||
|
|
||||||
|
impl ServerCertVerifier for NoCertificateVerification {
|
||||||
|
fn verify_server_cert(
|
||||||
|
&self,
|
||||||
|
_end_entity: &rustls::pki_types::CertificateDer<'_>,
|
||||||
|
_intermediates: &[rustls::pki_types::CertificateDer<'_>],
|
||||||
|
_server_name: &rustls::pki_types::ServerName<'_>,
|
||||||
|
_ocsp_response: &[u8],
|
||||||
|
_now: rustls::pki_types::UnixTime,
|
||||||
|
) -> std::result::Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
|
||||||
|
Ok(ServerCertVerified::assertion())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_tls12_signature(
|
||||||
|
&self,
|
||||||
|
_message: &[u8],
|
||||||
|
_cert: &rustls::pki_types::CertificateDer<'_>,
|
||||||
|
_dss: &rustls::DigitallySignedStruct,
|
||||||
|
) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
|
||||||
|
Ok(HandshakeSignatureValid::assertion())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_tls13_signature(
|
||||||
|
&self,
|
||||||
|
_message: &[u8],
|
||||||
|
_cert: &rustls::pki_types::CertificateDer<'_>,
|
||||||
|
_dss: &rustls::DigitallySignedStruct,
|
||||||
|
) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
|
||||||
|
Ok(HandshakeSignatureValid::assertion())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
|
||||||
|
vec![
|
||||||
|
SignatureScheme::RSA_PKCS1_SHA1,
|
||||||
|
SignatureScheme::ECDSA_SHA1_Legacy,
|
||||||
|
SignatureScheme::RSA_PKCS1_SHA256,
|
||||||
|
SignatureScheme::ECDSA_NISTP256_SHA256,
|
||||||
|
SignatureScheme::RSA_PKCS1_SHA384,
|
||||||
|
SignatureScheme::ECDSA_NISTP384_SHA384,
|
||||||
|
SignatureScheme::RSA_PKCS1_SHA512,
|
||||||
|
SignatureScheme::ECDSA_NISTP521_SHA512,
|
||||||
|
SignatureScheme::RSA_PSS_SHA256,
|
||||||
|
SignatureScheme::RSA_PSS_SHA384,
|
||||||
|
SignatureScheme::RSA_PSS_SHA512,
|
||||||
|
SignatureScheme::ED25519,
|
||||||
|
SignatureScheme::ED448,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
26
adb_client/src/message_devices/usb/README.md
Normal file
26
adb_client/src/message_devices/usb/README.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Examples
|
||||||
|
|
||||||
|
## Launch a command on device
|
||||||
|
|
||||||
|
```rust no_run
|
||||||
|
use adb_client::{usb::ADBUSBDevice, ADBDeviceExt};
|
||||||
|
|
||||||
|
let vendor_id = 0x04e8;
|
||||||
|
let product_id = 0x6860;
|
||||||
|
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
|
||||||
|
device.shell_command(&["df", "-h"], &mut std::io::stdout());
|
||||||
|
```
|
||||||
|
|
||||||
|
## Push a file to the device
|
||||||
|
|
||||||
|
```rust no_run
|
||||||
|
use adb_client::{usb::ADBUSBDevice, ADBDeviceExt};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
let vendor_id = 0x04e8;
|
||||||
|
let product_id = 0x6860;
|
||||||
|
let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device");
|
||||||
|
let mut input = File::open(Path::new("/tmp/file.txt")).expect("Cannot open file");
|
||||||
|
device.push(&mut input, &"/data/local/tmp");
|
||||||
|
```
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
use crate::{Result, RustADBError};
|
use crate::{Result, RustADBError};
|
||||||
use base64::{engine::general_purpose::STANDARD, Engine};
|
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||||
use num_bigint::{BigUint, ModInverse};
|
use num_bigint::{BigUint, ModInverse};
|
||||||
use num_traits::cast::ToPrimitive;
|
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
use rand::rngs::OsRng;
|
use num_traits::cast::ToPrimitive;
|
||||||
use rsa::pkcs8::DecodePrivateKey;
|
use rsa::pkcs8::DecodePrivateKey;
|
||||||
use rsa::traits::PublicKeyParts;
|
use rsa::traits::PublicKeyParts;
|
||||||
use rsa::{Pkcs1v15Sign, RsaPrivateKey};
|
use rsa::{Pkcs1v15Sign, RsaPrivateKey};
|
||||||
@@ -44,7 +43,7 @@ impl ADBRsaInternalPublicKey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ADBRsaKey {
|
pub struct ADBRsaKey {
|
||||||
private_key: RsaPrivateKey,
|
private_key: RsaPrivateKey,
|
||||||
}
|
}
|
||||||
@@ -52,7 +51,7 @@ pub struct ADBRsaKey {
|
|||||||
impl ADBRsaKey {
|
impl ADBRsaKey {
|
||||||
pub fn new_random() -> Result<Self> {
|
pub fn new_random() -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
private_key: RsaPrivateKey::new(&mut OsRng, ADB_PRIVATE_KEY_SIZE)?,
|
private_key: RsaPrivateKey::new(&mut rsa::rand_core::OsRng, ADB_PRIVATE_KEY_SIZE)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
307
adb_client/src/message_devices/usb/adb_usb_device.rs
Normal file
307
adb_client/src/message_devices/usb/adb_usb_device.rs
Normal file
@@ -0,0 +1,307 @@
|
|||||||
|
use rusb::Device;
|
||||||
|
use rusb::DeviceDescriptor;
|
||||||
|
use rusb::UsbContext;
|
||||||
|
use rusb::constants::LIBUSB_CLASS_VENDOR_SPEC;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::ADBDeviceExt;
|
||||||
|
use crate::Result;
|
||||||
|
use crate::RustADBError;
|
||||||
|
use crate::adb_transport::ADBTransport;
|
||||||
|
use crate::message_devices::adb_message_device::ADBMessageDevice;
|
||||||
|
use crate::message_devices::adb_message_transport::ADBMessageTransport;
|
||||||
|
use crate::message_devices::adb_transport_message::ADBTransportMessage;
|
||||||
|
use crate::message_devices::message_commands::MessageCommand;
|
||||||
|
use crate::usb::adb_rsa_key::ADBRsaKey;
|
||||||
|
use crate::usb::usb_transport::USBTransport;
|
||||||
|
use crate::utils::get_default_adb_key_path;
|
||||||
|
|
||||||
|
const AUTH_TOKEN: u32 = 1;
|
||||||
|
const AUTH_SIGNATURE: u32 = 2;
|
||||||
|
const AUTH_RSAPUBLICKEY: u32 = 3;
|
||||||
|
|
||||||
|
pub fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Option<ADBRsaKey>> {
|
||||||
|
// Try to read the private key file from given path
|
||||||
|
// If the file is not found, return None
|
||||||
|
// If there is another error while reading the file, return this error
|
||||||
|
// Else, return the private key content
|
||||||
|
let pk = match read_to_string(private_key_path.as_ref()) {
|
||||||
|
Ok(pk) => pk,
|
||||||
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match ADBRsaKey::new_from_pkcs8(&pk) {
|
||||||
|
Ok(pk) => Ok(Some(pk)),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Search for adb devices with known interface class and subclass values
|
||||||
|
pub fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||||
|
let mut found_devices = vec![];
|
||||||
|
for device in rusb::devices()?.iter() {
|
||||||
|
let Ok(des) = device.device_descriptor() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if is_adb_device(&device, &des) {
|
||||||
|
log::debug!(
|
||||||
|
"Autodetect device {:04x}:{:04x}",
|
||||||
|
des.vendor_id(),
|
||||||
|
des.product_id()
|
||||||
|
);
|
||||||
|
found_devices.push((des.vendor_id(), des.product_id()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match (found_devices.first(), found_devices.get(1)) {
|
||||||
|
(None, _) => Ok(None),
|
||||||
|
(Some(identifiers), None) => Ok(Some(*identifiers)),
|
||||||
|
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
|
||||||
|
"Found two Android devices {vid1:04x}:{pid1:04x} and {vid2:04x}:{pid2:04x}",
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check whether a device with given descriptor is an ADB device
|
||||||
|
pub fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
||||||
|
const ADB_SUBCLASS: u8 = 0x42;
|
||||||
|
const ADB_PROTOCOL: u8 = 0x1;
|
||||||
|
|
||||||
|
// Some devices require choosing the file transfer mode
|
||||||
|
// for usb debugging to take effect.
|
||||||
|
const BULK_CLASS: u8 = 0xdc;
|
||||||
|
const BULK_ADB_SUBCLASS: u8 = 2;
|
||||||
|
|
||||||
|
for n in 0..des.num_configurations() {
|
||||||
|
let Ok(config_des) = device.config_descriptor(n) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
for interface in config_des.interfaces() {
|
||||||
|
for interface_des in interface.descriptors() {
|
||||||
|
let proto = interface_des.protocol_code();
|
||||||
|
let class = interface_des.class_code();
|
||||||
|
let subcl = interface_des.sub_class_code();
|
||||||
|
if proto == ADB_PROTOCOL
|
||||||
|
&& ((class == LIBUSB_CLASS_VENDOR_SPEC && subcl == ADB_SUBCLASS)
|
||||||
|
|| (class == BULK_CLASS && subcl == BULK_ADB_SUBCLASS))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represent a device reached and available over USB.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ADBUSBDevice {
|
||||||
|
private_key: ADBRsaKey,
|
||||||
|
inner: ADBMessageDevice<USBTransport>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBUSBDevice {
|
||||||
|
/// Instantiate a new [`ADBUSBDevice`]
|
||||||
|
pub fn new(vendor_id: u16, product_id: u16) -> Result<Self> {
|
||||||
|
Self::new_with_custom_private_key(vendor_id, product_id, get_default_adb_key_path()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Instantiate a new [`ADBUSBDevice`] using a custom private key path
|
||||||
|
pub fn new_with_custom_private_key(
|
||||||
|
vendor_id: u16,
|
||||||
|
product_id: u16,
|
||||||
|
private_key_path: PathBuf,
|
||||||
|
) -> Result<Self> {
|
||||||
|
Self::new_from_transport_inner(USBTransport::new(vendor_id, product_id)?, private_key_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Instantiate a new [`ADBUSBDevice`] from a [`USBTransport`] and an optional private key path.
|
||||||
|
pub fn new_from_transport(
|
||||||
|
transport: USBTransport,
|
||||||
|
private_key_path: Option<PathBuf>,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let private_key_path = match private_key_path {
|
||||||
|
Some(private_key_path) => private_key_path,
|
||||||
|
None => get_default_adb_key_path()?,
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::new_from_transport_inner(transport, private_key_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_from_transport_inner(
|
||||||
|
transport: USBTransport,
|
||||||
|
private_key_path: PathBuf,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let private_key = match read_adb_private_key(&private_key_path)? {
|
||||||
|
Some(pk) => pk,
|
||||||
|
None => {
|
||||||
|
log::warn!(
|
||||||
|
"No private key found at path {}. Using a temporary random one.",
|
||||||
|
private_key_path.display()
|
||||||
|
);
|
||||||
|
ADBRsaKey::new_random()?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut s = Self {
|
||||||
|
private_key,
|
||||||
|
inner: ADBMessageDevice::new(transport),
|
||||||
|
};
|
||||||
|
|
||||||
|
s.connect()?;
|
||||||
|
|
||||||
|
Ok(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// autodetect connected ADB devices and establish a connection with the first device found
|
||||||
|
pub fn autodetect() -> Result<Self> {
|
||||||
|
Self::autodetect_with_custom_private_key(get_default_adb_key_path()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// autodetect connected ADB devices and establish a connection with the first device found using a custom private key path
|
||||||
|
pub fn autodetect_with_custom_private_key(private_key_path: PathBuf) -> Result<Self> {
|
||||||
|
match search_adb_devices()? {
|
||||||
|
Some((vendor_id, product_id)) => {
|
||||||
|
ADBUSBDevice::new_with_custom_private_key(vendor_id, product_id, private_key_path)
|
||||||
|
}
|
||||||
|
_ => Err(RustADBError::DeviceNotFound(
|
||||||
|
"cannot find USB devices matching the signature of an ADB device".into(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send initial connect
|
||||||
|
pub fn connect(&mut self) -> Result<()> {
|
||||||
|
self.get_transport_mut().connect()?;
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(
|
||||||
|
MessageCommand::Cnxn,
|
||||||
|
0x01000000,
|
||||||
|
1048576,
|
||||||
|
format!("host::{}\0", env!("CARGO_PKG_NAME")).as_bytes(),
|
||||||
|
);
|
||||||
|
|
||||||
|
self.get_transport_mut().write_message(message)?;
|
||||||
|
|
||||||
|
let message = self.get_transport_mut().read_message()?;
|
||||||
|
// If the device returned CNXN instead of AUTH it does not require authentication,
|
||||||
|
// so we can skip the auth steps.
|
||||||
|
if message.header().command() == MessageCommand::Cnxn {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
message.assert_command(MessageCommand::Auth)?;
|
||||||
|
|
||||||
|
// At this point, we should have receive an AUTH message with arg0 == 1
|
||||||
|
let auth_message = match message.header().arg0() {
|
||||||
|
AUTH_TOKEN => message,
|
||||||
|
v => {
|
||||||
|
return Err(RustADBError::ADBRequestFailed(format!(
|
||||||
|
"Received AUTH message with type != 1 ({v})"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let sign = self.private_key.sign(auth_message.into_payload())?;
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(MessageCommand::Auth, AUTH_SIGNATURE, 0, &sign);
|
||||||
|
|
||||||
|
self.get_transport_mut().write_message(message)?;
|
||||||
|
|
||||||
|
let received_response = self.get_transport_mut().read_message()?;
|
||||||
|
|
||||||
|
if received_response.header().command() == MessageCommand::Cnxn {
|
||||||
|
log::info!(
|
||||||
|
"Authentication OK, device info {}",
|
||||||
|
String::from_utf8(received_response.into_payload())?
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pubkey = self.private_key.android_pubkey_encode()?.into_bytes();
|
||||||
|
pubkey.push(b'\0');
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::new(MessageCommand::Auth, AUTH_RSAPUBLICKEY, 0, &pubkey);
|
||||||
|
|
||||||
|
self.get_transport_mut().write_message(message)?;
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.get_transport_mut()
|
||||||
|
.read_message_with_timeout(Duration::from_secs(10))
|
||||||
|
.and_then(|message| {
|
||||||
|
message.assert_command(MessageCommand::Cnxn)?;
|
||||||
|
Ok(message)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
log::info!(
|
||||||
|
"Authentication OK, device info {}",
|
||||||
|
String::from_utf8(response.into_payload())?
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn get_transport_mut(&mut self) -> &mut USBTransport {
|
||||||
|
self.inner.get_transport_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBDeviceExt for ADBUSBDevice {
|
||||||
|
#[inline]
|
||||||
|
fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
|
||||||
|
self.inner.shell_command(command, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn shell<'a>(&mut self, reader: &mut dyn Read, writer: Box<dyn Write + Send>) -> Result<()> {
|
||||||
|
self.inner.shell(reader, writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn stat(&mut self, remote_path: &str) -> Result<crate::AdbStatResponse> {
|
||||||
|
self.inner.stat(remote_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn pull(&mut self, source: &dyn AsRef<str>, output: &mut dyn Write) -> Result<()> {
|
||||||
|
self.inner.pull(source, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn push(&mut self, stream: &mut dyn Read, path: &dyn AsRef<str>) -> Result<()> {
|
||||||
|
self.inner.push(stream, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> {
|
||||||
|
self.inner.reboot(reboot_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()> {
|
||||||
|
self.inner.install(apk_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.inner.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
|
self.inner.framebuffer_inner()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ADBUSBDevice {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Best effort here
|
||||||
|
let _ = self.get_transport_mut().disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
adb_client/src/message_devices/usb/mod.rs
Normal file
8
adb_client/src/message_devices/usb/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#![doc = include_str!("./README.md")]
|
||||||
|
|
||||||
|
mod adb_rsa_key;
|
||||||
|
mod adb_usb_device;
|
||||||
|
mod usb_transport;
|
||||||
|
|
||||||
|
pub use adb_usb_device::ADBUSBDevice;
|
||||||
|
pub use usb_transport::USBTransport;
|
||||||
268
adb_client/src/message_devices/usb/usb_transport.rs
Normal file
268
adb_client/src/message_devices/usb/usb_transport.rs
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
|
use rusb::{
|
||||||
|
Device, DeviceHandle, Direction, GlobalContext, TransferType,
|
||||||
|
constants::LIBUSB_CLASS_VENDOR_SPEC,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
adb_transport::ADBTransport,
|
||||||
|
message_devices::{
|
||||||
|
adb_message_transport::ADBMessageTransport,
|
||||||
|
adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader},
|
||||||
|
message_commands::MessageCommand,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct Endpoint {
|
||||||
|
iface: u8,
|
||||||
|
address: u8,
|
||||||
|
max_packet_size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transport running on USB
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct USBTransport {
|
||||||
|
device: Device<GlobalContext>,
|
||||||
|
handle: Option<Arc<DeviceHandle<GlobalContext>>>,
|
||||||
|
read_endpoint: Option<Endpoint>,
|
||||||
|
write_endpoint: Option<Endpoint>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl USBTransport {
|
||||||
|
/// Instantiate a new [`USBTransport`].
|
||||||
|
/// Only the first device with given vendor_id and product_id is returned.
|
||||||
|
pub fn new(vendor_id: u16, product_id: u16) -> Result<Self> {
|
||||||
|
for device in rusb::devices()?.iter() {
|
||||||
|
if let Ok(descriptor) = device.device_descriptor() {
|
||||||
|
if descriptor.vendor_id() == vendor_id && descriptor.product_id() == product_id {
|
||||||
|
return Ok(Self::new_from_device(device));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(RustADBError::DeviceNotFound(format!(
|
||||||
|
"cannot find USB device with vendor_id={vendor_id} and product_id={product_id}",
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Instantiate a new [`USBTransport`] from a [`rusb::Device`].
|
||||||
|
///
|
||||||
|
/// Devices can be enumerated using [`rusb::devices()`] and then filtered out to get desired device.
|
||||||
|
pub fn new_from_device(rusb_device: rusb::Device<GlobalContext>) -> Self {
|
||||||
|
Self {
|
||||||
|
device: rusb_device,
|
||||||
|
handle: None,
|
||||||
|
read_endpoint: None,
|
||||||
|
write_endpoint: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_raw_connection(&self) -> Result<Arc<DeviceHandle<GlobalContext>>> {
|
||||||
|
self.handle
|
||||||
|
.as_ref()
|
||||||
|
.ok_or(RustADBError::IOError(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::NotConnected,
|
||||||
|
"not connected",
|
||||||
|
)))
|
||||||
|
.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<()> {
|
||||||
|
handle.claim_interface(endpoint.iface)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_endpoints(&self, handle: &DeviceHandle<GlobalContext>) -> Result<(Endpoint, Endpoint)> {
|
||||||
|
let mut read_endpoint: Option<Endpoint> = None;
|
||||||
|
let mut write_endpoint: Option<Endpoint> = None;
|
||||||
|
|
||||||
|
for n in 0..handle.device().device_descriptor()?.num_configurations() {
|
||||||
|
let config_desc = match handle.device().config_descriptor(n) {
|
||||||
|
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.transfer_type() == TransferType::Bulk
|
||||||
|
&& interface_desc.class_code() == LIBUSB_CLASS_VENDOR_SPEC
|
||||||
|
&& interface_desc.sub_class_code() == 0x42
|
||||||
|
&& interface_desc.protocol_code() == 0x01
|
||||||
|
{
|
||||||
|
let endpoint = Endpoint {
|
||||||
|
iface: interface_desc.interface_number(),
|
||||||
|
address: endpoint_desc.address(),
|
||||||
|
max_packet_size: endpoint_desc.max_packet_size() as usize,
|
||||||
|
};
|
||||||
|
match endpoint_desc.direction() {
|
||||||
|
Direction::In => {
|
||||||
|
if let Some(write_endpoint) = write_endpoint {
|
||||||
|
return Ok((endpoint, write_endpoint));
|
||||||
|
} else {
|
||||||
|
read_endpoint = Some(endpoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Direction::Out => {
|
||||||
|
if let Some(read_endpoint) = read_endpoint {
|
||||||
|
return Ok((read_endpoint, endpoint));
|
||||||
|
} else {
|
||||||
|
write_endpoint = Some(endpoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(RustADBError::USBNoDescriptorFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_bulk_data(&self, data: &[u8], timeout: Duration) -> Result<()> {
|
||||||
|
let endpoint = self.get_write_endpoint()?;
|
||||||
|
let handle = self.get_raw_connection()?;
|
||||||
|
let max_packet_size = endpoint.max_packet_size;
|
||||||
|
|
||||||
|
let mut offset = 0;
|
||||||
|
let data_len = data.len();
|
||||||
|
while offset < data_len {
|
||||||
|
let end = (offset + max_packet_size).min(data_len);
|
||||||
|
let write_amount = handle.write_bulk(endpoint.address, &data[offset..end], timeout)?;
|
||||||
|
offset += write_amount;
|
||||||
|
|
||||||
|
log::trace!("wrote chunk of size {write_amount} - {offset}/{data_len}",)
|
||||||
|
}
|
||||||
|
|
||||||
|
if offset % max_packet_size == 0 {
|
||||||
|
log::trace!("must send final zero-length packet");
|
||||||
|
handle.write_bulk(endpoint.address, &[], timeout)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBTransport for USBTransport {
|
||||||
|
fn connect(&mut self) -> crate::Result<()> {
|
||||||
|
let device = self.device.open()?;
|
||||||
|
|
||||||
|
let (read_endpoint, write_endpoint) = self.find_endpoints(&device)?;
|
||||||
|
|
||||||
|
Self::configure_endpoint(&device, &read_endpoint)?;
|
||||||
|
log::debug!("got read endpoint: {read_endpoint:?}");
|
||||||
|
self.read_endpoint = Some(read_endpoint);
|
||||||
|
|
||||||
|
Self::configure_endpoint(&device, &write_endpoint)?;
|
||||||
|
log::debug!("got write endpoint: {write_endpoint:?}");
|
||||||
|
self.write_endpoint = Some(write_endpoint);
|
||||||
|
|
||||||
|
self.handle = Some(Arc::new(device));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disconnect(&mut self) -> crate::Result<()> {
|
||||||
|
let message = ADBTransportMessage::new(MessageCommand::Clse, 0, 0, &[]);
|
||||||
|
if let Err(e) = self.write_message(message) {
|
||||||
|
log::error!("error while sending CLSE message: {e}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(handle) = &self.handle {
|
||||||
|
let endpoint = self.read_endpoint.as_ref().or(self.write_endpoint.as_ref());
|
||||||
|
if let Some(endpoint) = &endpoint {
|
||||||
|
match handle.release_interface(endpoint.iface) {
|
||||||
|
Ok(()) => log::debug!("succesfully released interface"),
|
||||||
|
Err(e) => log::error!("error while release interface: {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ADBMessageTransport for USBTransport {
|
||||||
|
fn write_message_with_timeout(
|
||||||
|
&mut self,
|
||||||
|
message: ADBTransportMessage,
|
||||||
|
timeout: Duration,
|
||||||
|
) -> Result<()> {
|
||||||
|
let message_bytes = message.header().as_bytes()?;
|
||||||
|
self.write_bulk_data(&message_bytes, timeout)?;
|
||||||
|
|
||||||
|
log::trace!("successfully write header: {} bytes", message_bytes.len());
|
||||||
|
|
||||||
|
let payload = message.into_payload();
|
||||||
|
if !payload.is_empty() {
|
||||||
|
self.write_bulk_data(&payload, timeout)?;
|
||||||
|
log::trace!("successfully write payload: {} bytes", payload.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_message_with_timeout(&mut self, timeout: Duration) -> Result<ADBTransportMessage> {
|
||||||
|
let endpoint = self.get_read_endpoint()?;
|
||||||
|
let handle = self.get_raw_connection()?;
|
||||||
|
let max_packet_size = endpoint.max_packet_size;
|
||||||
|
|
||||||
|
let mut data = [0u8; 24];
|
||||||
|
let mut offset = 0;
|
||||||
|
while offset < data.len() {
|
||||||
|
let end = (offset + max_packet_size).min(data.len());
|
||||||
|
let chunk = &mut data[offset..end];
|
||||||
|
offset += handle.read_bulk(endpoint.address, chunk, timeout)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let header = ADBTransportMessageHeader::try_from(data)?;
|
||||||
|
log::trace!("received header {header:?}");
|
||||||
|
|
||||||
|
if header.data_length() != 0 {
|
||||||
|
let mut msg_data = vec![0_u8; header.data_length() as usize];
|
||||||
|
let mut offset = 0;
|
||||||
|
while offset < msg_data.len() {
|
||||||
|
let end = (offset + max_packet_size).min(msg_data.len());
|
||||||
|
let chunk = &mut msg_data[offset..end];
|
||||||
|
offset += handle.read_bulk(endpoint.address, chunk, timeout)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = ADBTransportMessage::from_header_and_payload(header, msg_data);
|
||||||
|
|
||||||
|
// Check message integrity
|
||||||
|
if !message.check_message_integrity() {
|
||||||
|
return Err(RustADBError::InvalidIntegrity(
|
||||||
|
ADBTransportMessageHeader::compute_crc32(message.payload()),
|
||||||
|
message.header().data_crc32(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ADBTransportMessage::from_header_and_payload(header, vec![]))
|
||||||
|
}
|
||||||
|
}
|
||||||
97
adb_client/src/models/framebuffer_info.rs
Normal file
97
adb_client/src/models/framebuffer_info.rs
Normal 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)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
/// Available host features.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum HostFeatures {
|
pub enum HostFeatures {
|
||||||
|
/// Shell version 2.
|
||||||
ShellV2,
|
ShellV2,
|
||||||
|
/// Command.
|
||||||
Cmd,
|
Cmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,13 @@
|
|||||||
mod adb_emulator_command;
|
|
||||||
mod adb_request_status;
|
mod adb_request_status;
|
||||||
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 reboot_type;
|
mod reboot_type;
|
||||||
mod sync_command;
|
mod sync_command;
|
||||||
|
|
||||||
pub(crate) use adb_emulator_command::ADBEmulatorCommand;
|
pub(crate) use adb_request_status::AdbRequestStatus;
|
||||||
pub use adb_request_status::AdbRequestStatus;
|
|
||||||
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 reboot_type::RebootType;
|
pub use reboot_type::RebootType;
|
||||||
pub use sync_command::SyncCommand;
|
pub(crate) use sync_command::SyncCommand;
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ pub enum RebootType {
|
|||||||
Sideload,
|
Sideload,
|
||||||
/// Same as `Sideload` but reboots after sideloading
|
/// Same as `Sideload` but reboots after sideloading
|
||||||
SideloadAutoReboot,
|
SideloadAutoReboot,
|
||||||
|
/// Reboots to fastboot
|
||||||
|
Fastboot,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for RebootType {
|
impl Display for RebootType {
|
||||||
@@ -23,6 +25,7 @@ impl Display for RebootType {
|
|||||||
RebootType::Recovery => write!(f, "recovery"),
|
RebootType::Recovery => write!(f, "recovery"),
|
||||||
RebootType::Sideload => write!(f, "sideload"),
|
RebootType::Sideload => write!(f, "sideload"),
|
||||||
RebootType::SideloadAutoReboot => write!(f, "sideload-auto-reboot"),
|
RebootType::SideloadAutoReboot => write!(f, "sideload-auto-reboot"),
|
||||||
|
RebootType::Fastboot => write!(f, "fastboot"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
adb_client/src/server/README.md
Normal file
15
adb_client/src/server/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Examples
|
||||||
|
|
||||||
|
## Get available ADB devices
|
||||||
|
|
||||||
|
```rust no_run
|
||||||
|
use adb_client::server::ADBServer;
|
||||||
|
use std::net::{SocketAddrV4, Ipv4Addr};
|
||||||
|
|
||||||
|
// A custom server address can be provided
|
||||||
|
let server_ip = Ipv4Addr::new(127, 0, 0, 1);
|
||||||
|
let server_port = 5037;
|
||||||
|
|
||||||
|
let mut server = ADBServer::new(SocketAddrV4::new(server_ip, server_port));
|
||||||
|
server.devices();
|
||||||
|
```
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
use crate::ADBTransport;
|
use crate::ADBTransport;
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
use crate::RustADBError;
|
use crate::RustADBError;
|
||||||
use crate::TCPServerTransport;
|
use crate::server::tcp_server_transport::TCPServerTransport;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
@@ -12,6 +13,11 @@ pub struct ADBServer {
|
|||||||
pub(crate) transport: Option<TCPServerTransport>,
|
pub(crate) transport: Option<TCPServerTransport>,
|
||||||
/// Address to connect to
|
/// Address to connect to
|
||||||
pub(crate) socket_addr: Option<SocketAddrV4>,
|
pub(crate) socket_addr: Option<SocketAddrV4>,
|
||||||
|
/// adb-server start envs
|
||||||
|
pub(crate) envs: HashMap<String, String>,
|
||||||
|
/// Path to adb binary
|
||||||
|
/// If not set, will use adb from PATH
|
||||||
|
pub(crate) adb_path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
@@ -20,6 +26,45 @@ impl ADBServer {
|
|||||||
Self {
|
Self {
|
||||||
transport: None,
|
transport: None,
|
||||||
socket_addr: Some(address),
|
socket_addr: Some(address),
|
||||||
|
envs: HashMap::new(),
|
||||||
|
adb_path: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Instantiates a new [ADBServer] with a custom adb path
|
||||||
|
pub fn new_from_path(address: SocketAddrV4, adb_path: Option<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
transport: None,
|
||||||
|
socket_addr: Some(address),
|
||||||
|
envs: HashMap::new(),
|
||||||
|
adb_path,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start an instance of `adb-server`
|
||||||
|
pub fn start(envs: &HashMap<String, String>, adb_path: &Option<String>) {
|
||||||
|
// ADB Server is local, we start it if not already running
|
||||||
|
let mut command = Command::new(adb_path.as_deref().unwrap_or("adb"));
|
||||||
|
command.arg("start-server");
|
||||||
|
for (env_k, env_v) in envs.iter() {
|
||||||
|
command.env(env_k, env_v);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
use std::os::windows::process::CommandExt;
|
||||||
|
// Do not show a prompt on Windows
|
||||||
|
command.creation_flags(0x08000000);
|
||||||
|
}
|
||||||
|
|
||||||
|
let child = command.spawn();
|
||||||
|
match child {
|
||||||
|
Ok(mut child) => {
|
||||||
|
if let Err(e) = child.wait() {
|
||||||
|
log::error!("error while starting adb server: {e}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => log::error!("error while starting adb server: {e}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,16 +93,7 @@ impl ADBServer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if is_local_ip {
|
if is_local_ip {
|
||||||
// ADB Server is local, we start it if not already running
|
Self::start(&self.envs, &self.adb_path);
|
||||||
let child = Command::new("adb").arg("start-server").spawn();
|
|
||||||
match child {
|
|
||||||
Ok(mut child) => {
|
|
||||||
if let Err(e) = child.wait() {
|
|
||||||
log::error!("error while starting adb server: {e}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => log::error!("error while starting adb server: {e}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
transport.connect()?;
|
transport.connect()?;
|
||||||
@@ -69,7 +105,7 @@ impl ADBServer {
|
|||||||
|
|
||||||
impl Drop for ADBServer {
|
impl Drop for ADBServer {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(ref mut transport) = &mut self.transport {
|
if let Some(transport) = &mut self.transport {
|
||||||
let _ = transport.disconnect();
|
let _ = transport.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use super::RebootType;
|
use crate::{
|
||||||
|
RebootType,
|
||||||
|
server::{WaitForDeviceState, WaitForDeviceTransport},
|
||||||
|
};
|
||||||
|
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
pub(crate) enum AdbServerCommand {
|
pub(crate) enum AdbServerCommand {
|
||||||
@@ -16,15 +20,26 @@ pub(crate) enum AdbServerCommand {
|
|||||||
Pair(SocketAddrV4, String),
|
Pair(SocketAddrV4, String),
|
||||||
TransportAny,
|
TransportAny,
|
||||||
TransportSerial(String),
|
TransportSerial(String),
|
||||||
|
MDNSCheck,
|
||||||
|
MDNSServices,
|
||||||
|
ServerStatus,
|
||||||
|
ReconnectOffline,
|
||||||
|
Uninstall(String),
|
||||||
Install(u64),
|
Install(u64),
|
||||||
|
WaitForDevice(WaitForDeviceState, WaitForDeviceTransport),
|
||||||
// Local commands
|
// Local commands
|
||||||
ShellCommand(String),
|
ShellCommand(String),
|
||||||
Shell,
|
Shell,
|
||||||
FrameBuffer,
|
FrameBuffer,
|
||||||
Sync,
|
Sync,
|
||||||
Reboot(RebootType),
|
Reboot(RebootType),
|
||||||
Forward(String, String, String),
|
Forward(String, String),
|
||||||
|
ForwardRemoveAll,
|
||||||
Reverse(String, String),
|
Reverse(String, String),
|
||||||
|
ReverseRemoveAll,
|
||||||
|
Reconnect,
|
||||||
|
TcpIp(u16),
|
||||||
|
Usb,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for AdbServerCommand {
|
impl Display for AdbServerCommand {
|
||||||
@@ -50,19 +65,39 @@ impl Display for AdbServerCommand {
|
|||||||
AdbServerCommand::Reboot(reboot_type) => {
|
AdbServerCommand::Reboot(reboot_type) => {
|
||||||
write!(f, "reboot:{reboot_type}")
|
write!(f, "reboot:{reboot_type}")
|
||||||
}
|
}
|
||||||
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{}", addr),
|
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{addr}"),
|
||||||
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{}", addr),
|
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{addr}"),
|
||||||
AdbServerCommand::Pair(addr, code) => {
|
AdbServerCommand::Pair(addr, code) => {
|
||||||
write!(f, "host:pair:{code}:{addr}")
|
write!(f, "host:pair:{code}:{addr}")
|
||||||
}
|
}
|
||||||
AdbServerCommand::FrameBuffer => write!(f, "framebuffer:"),
|
AdbServerCommand::FrameBuffer => write!(f, "framebuffer:"),
|
||||||
AdbServerCommand::Forward(serial, remote, local) => {
|
AdbServerCommand::Forward(remote, local) => {
|
||||||
write!(f, "host-serial:{serial}:forward:{local};{remote}")
|
write!(f, "host:forward:{local};{remote}")
|
||||||
}
|
}
|
||||||
|
AdbServerCommand::ForwardRemoveAll => write!(f, "host:killforward-all"),
|
||||||
AdbServerCommand::Reverse(remote, local) => {
|
AdbServerCommand::Reverse(remote, local) => {
|
||||||
write!(f, "reverse:forward:{remote};{local}")
|
write!(f, "reverse:forward:{remote};{local}")
|
||||||
}
|
}
|
||||||
|
AdbServerCommand::ReverseRemoveAll => write!(f, "reverse:killforward-all"),
|
||||||
|
AdbServerCommand::MDNSCheck => write!(f, "host:mdns:check"),
|
||||||
|
AdbServerCommand::MDNSServices => write!(f, "host:mdns:services"),
|
||||||
|
AdbServerCommand::ServerStatus => write!(f, "host:server-status"),
|
||||||
|
AdbServerCommand::Reconnect => write!(f, "reconnect"),
|
||||||
|
AdbServerCommand::ReconnectOffline => write!(f, "host:reconnect-offline"),
|
||||||
|
AdbServerCommand::TcpIp(port) => {
|
||||||
|
write!(f, "tcpip:{port}")
|
||||||
|
}
|
||||||
|
AdbServerCommand::Usb => write!(f, "usb:"),
|
||||||
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
|
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
|
||||||
|
AdbServerCommand::Uninstall(package) => {
|
||||||
|
write!(f, "exec:cmd package 'uninstall' {package}")
|
||||||
|
}
|
||||||
|
AdbServerCommand::WaitForDevice(wait_for_device_state, wait_for_device_transport) => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"host:wait-for-{wait_for_device_transport}-{wait_for_device_state}"
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
use crate::{ADBTransport, Result, TCPServerTransport};
|
|
||||||
use std::net::SocketAddrV4;
|
|
||||||
|
|
||||||
/// Represents a device connected to the ADB server.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ADBServerDevice {
|
|
||||||
/// Unique device identifier.
|
|
||||||
pub identifier: String,
|
|
||||||
/// Internal [TCPServerTransport]
|
|
||||||
transport: TCPServerTransport,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ADBServerDevice {
|
|
||||||
/// Instantiates a new [ADBServerDevice]
|
|
||||||
pub fn new(identifier: String, socket_addr: Option<SocketAddrV4>) -> Self {
|
|
||||||
let transport = if let Some(addr) = socket_addr {
|
|
||||||
TCPServerTransport::new(addr)
|
|
||||||
} else {
|
|
||||||
TCPServerTransport::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
Self {
|
|
||||||
identifier,
|
|
||||||
transport,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> {
|
|
||||||
self.transport.connect()?;
|
|
||||||
|
|
||||||
Ok(self.get_transport_mut())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for ADBServerDevice {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
// Best effort here
|
|
||||||
let _ = self.transport.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::{models::AdbServerCommand, ADBServer, Result, RustADBError};
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
server::{ADBServer, AdbServerCommand},
|
||||||
|
};
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
models::{AdbServerCommand, DeviceShort},
|
Result, RustADBError,
|
||||||
ADBEmulatorDevice, ADBServer, ADBServerDevice, DeviceLong, Result, RustADBError,
|
emulator::ADBEmulatorDevice,
|
||||||
|
server::{ADBServer, AdbServerCommand, DeviceLong, DeviceShort},
|
||||||
|
server_device::ADBServerDevice,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
@@ -36,7 +38,7 @@ impl ADBServer {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec_devices.push(DeviceLong::try_from(device.to_vec())?);
|
vec_devices.push(DeviceLong::try_from(device)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(vec_devices)
|
Ok(vec_devices)
|
||||||
@@ -97,7 +99,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())?)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::{models::AdbServerCommand, ADBServer, Result, RustADBError};
|
use crate::{
|
||||||
|
Result, RustADBError,
|
||||||
|
server::{ADBServer, AdbServerCommand},
|
||||||
|
};
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::{models::AdbServerCommand, ADBServer, Result};
|
use crate::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, AdbServerCommand},
|
||||||
|
};
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
/// Asks the ADB server to quit immediately.
|
/// Asks the ADB server to quit immediately.
|
||||||
62
adb_client/src/server/commands/mdns.rs
Normal file
62
adb_client/src/server/commands/mdns.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use std::io::BufRead;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, AdbServerCommand, MDNSServices, models::MDNSBackend},
|
||||||
|
};
|
||||||
|
|
||||||
|
const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN";
|
||||||
|
|
||||||
|
impl ADBServer {
|
||||||
|
/// Check if mdns discovery is available
|
||||||
|
pub fn mdns_check(&mut self) -> Result<bool> {
|
||||||
|
let response = self
|
||||||
|
.connect()?
|
||||||
|
.proxy_connection(AdbServerCommand::MDNSCheck, true)?;
|
||||||
|
|
||||||
|
match String::from_utf8(response) {
|
||||||
|
Ok(s) if s.starts_with("mdns daemon version") => Ok(true),
|
||||||
|
Ok(_) => Ok(false),
|
||||||
|
Err(e) => Err(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List all discovered mdns services
|
||||||
|
pub fn mdns_services(&mut self) -> Result<Vec<MDNSServices>> {
|
||||||
|
let services = self
|
||||||
|
.connect()?
|
||||||
|
.proxy_connection(AdbServerCommand::MDNSServices, true)?;
|
||||||
|
|
||||||
|
let mut vec_services: Vec<MDNSServices> = vec![];
|
||||||
|
for service in services.lines() {
|
||||||
|
match service {
|
||||||
|
Ok(service) => {
|
||||||
|
vec_services.push(MDNSServices::try_from(service.as_bytes())?);
|
||||||
|
}
|
||||||
|
Err(e) => log::error!("{e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(vec_services)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if specified backend mdns service is used, otherwise restart adb server with envs
|
||||||
|
pub fn mdns_force_backend(&mut self, backend: MDNSBackend) -> Result<()> {
|
||||||
|
let server_status = self.server_status()?;
|
||||||
|
if server_status.mdns_backend != backend {
|
||||||
|
self.kill()?;
|
||||||
|
self.envs.insert(
|
||||||
|
OPENSCREEN_MDNS_BACKEND.to_string(),
|
||||||
|
(if backend == MDNSBackend::OpenScreen {
|
||||||
|
"1"
|
||||||
|
} else {
|
||||||
|
"0"
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
self.connect()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,5 +2,9 @@ mod connect;
|
|||||||
mod devices;
|
mod devices;
|
||||||
mod disconnect;
|
mod disconnect;
|
||||||
mod kill;
|
mod kill;
|
||||||
|
mod mdns;
|
||||||
mod pair;
|
mod pair;
|
||||||
|
mod reconnect;
|
||||||
|
mod server_status;
|
||||||
mod version;
|
mod version;
|
||||||
|
mod wait_for_device;
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
use crate::models::AdbServerCommand;
|
use crate::{
|
||||||
use crate::{ADBServer, Result, RustADBError};
|
Result, RustADBError,
|
||||||
|
server::{ADBServer, AdbServerCommand},
|
||||||
|
};
|
||||||
use std::net::SocketAddrV4;
|
use std::net::SocketAddrV4;
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
13
adb_client/src/server/commands/reconnect.rs
Normal file
13
adb_client/src/server/commands/reconnect.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, AdbServerCommand},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl ADBServer {
|
||||||
|
/// Reconnect the device
|
||||||
|
pub fn reconnect_offline(&mut self) -> Result<()> {
|
||||||
|
self.connect()?
|
||||||
|
.proxy_connection(AdbServerCommand::ReconnectOffline, false)
|
||||||
|
.map(|_| ())
|
||||||
|
}
|
||||||
|
}
|
||||||
15
adb_client/src/server/commands/server_status.rs
Normal file
15
adb_client/src/server/commands/server_status.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, AdbServerCommand, models::ServerStatus},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl ADBServer {
|
||||||
|
/// Check ADB server status
|
||||||
|
pub fn server_status(&mut self) -> Result<ServerStatus> {
|
||||||
|
let status = self
|
||||||
|
.connect()?
|
||||||
|
.proxy_connection(AdbServerCommand::ServerStatus, true)?;
|
||||||
|
|
||||||
|
ServerStatus::try_from(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::{models::AdbServerCommand, ADBServer, AdbVersion, Result};
|
use crate::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, AdbServerCommand, AdbVersion},
|
||||||
|
};
|
||||||
|
|
||||||
impl ADBServer {
|
impl ADBServer {
|
||||||
/// Gets server's internal version number.
|
/// Gets server's internal version number.
|
||||||
21
adb_client/src/server/commands/wait_for_device.rs
Normal file
21
adb_client/src/server/commands/wait_for_device.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use crate::{
|
||||||
|
Result,
|
||||||
|
server::{ADBServer, AdbServerCommand, WaitForDeviceState, WaitForDeviceTransport},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl ADBServer {
|
||||||
|
/// Wait for a device in a given state to be connected
|
||||||
|
pub fn wait_for_device(
|
||||||
|
&mut self,
|
||||||
|
state: WaitForDeviceState,
|
||||||
|
transport: Option<WaitForDeviceTransport>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let transport = transport.unwrap_or_default();
|
||||||
|
|
||||||
|
self.connect()?
|
||||||
|
.send_adb_request(AdbServerCommand::WaitForDevice(state, transport))?;
|
||||||
|
|
||||||
|
// Server should respond with an "OKAY" response
|
||||||
|
self.get_transport()?.read_adb_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
use crate::{models::AdbServerCommand, ADBServerDevice, Result};
|
|
||||||
|
|
||||||
impl ADBServerDevice {
|
|
||||||
/// Forward socket connection
|
|
||||||
pub fn forward(&mut self, remote: String, local: String) -> Result<()> {
|
|
||||||
let serial = self.identifier.clone();
|
|
||||||
self.connect()?
|
|
||||||
.send_adb_request(AdbServerCommand::TransportSerial(serial.clone()))?;
|
|
||||||
|
|
||||||
self.get_transport_mut()
|
|
||||||
.proxy_connection(AdbServerCommand::Forward(serial, remote, local), false)
|
|
||||||
.map(|_| ())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,189 +0,0 @@
|
|||||||
use std::{
|
|
||||||
io::{Read, Seek, Write},
|
|
||||||
iter::Map,
|
|
||||||
path::Path,
|
|
||||||
slice::ChunksExact,
|
|
||||||
};
|
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
|
||||||
use image::{ImageBuffer, ImageFormat, Rgba};
|
|
||||||
|
|
||||||
use crate::{models::AdbServerCommand, utils, ADBServerDevice, 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)]
|
|
||||||
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 {
|
|
||||||
/// 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
|
|
||||||
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
|
|
||||||
let serial: String = self.identifier.clone();
|
|
||||||
self.connect()?
|
|
||||||
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
|
|
||||||
|
|
||||||
self.get_transport_mut()
|
|
||||||
.send_adb_request(AdbServerCommand::FrameBuffer)?;
|
|
||||||
|
|
||||||
let version = self
|
|
||||||
.get_transport_mut()
|
|
||||||
.get_raw_connection()?
|
|
||||||
.read_u32::<LittleEndian>()?;
|
|
||||||
|
|
||||||
match version {
|
|
||||||
// RGBA_8888
|
|
||||||
1 => {
|
|
||||||
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV1>()];
|
|
||||||
|
|
||||||
self.get_transport_mut()
|
|
||||||
.get_raw_connection()?
|
|
||||||
.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
let h: FrameBufferInfoV1 = buf.try_into()?;
|
|
||||||
|
|
||||||
let mut data = vec![
|
|
||||||
0_u8;
|
|
||||||
h.size
|
|
||||||
.try_into()
|
|
||||||
.map_err(|_| RustADBError::ConversionError)?
|
|
||||||
];
|
|
||||||
self.get_transport_mut()
|
|
||||||
.get_raw_connection()?
|
|
||||||
.read_exact(&mut data)?;
|
|
||||||
|
|
||||||
Ok(
|
|
||||||
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(h.width, h.height, data)
|
|
||||||
.ok_or_else(|| RustADBError::FramebufferConversionError)?,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
// RGBX_8888
|
|
||||||
2 => {
|
|
||||||
let mut buf = [0u8; std::mem::size_of::<FrameBufferInfoV2>()];
|
|
||||||
|
|
||||||
self.get_transport_mut()
|
|
||||||
.get_raw_connection()?
|
|
||||||
.read_exact(&mut buf)?;
|
|
||||||
|
|
||||||
let h: FrameBufferInfoV2 = buf.try_into()?;
|
|
||||||
|
|
||||||
let mut data = vec![
|
|
||||||
0_u8;
|
|
||||||
h.size
|
|
||||||
.try_into()
|
|
||||||
.map_err(|_| RustADBError::ConversionError)?
|
|
||||||
];
|
|
||||||
self.get_transport_mut()
|
|
||||||
.get_raw_connection()?
|
|
||||||
.read_exact(&mut data)?;
|
|
||||||
|
|
||||||
Ok(
|
|
||||||
ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(h.width, h.height, data)
|
|
||||||
.ok_or_else(|| RustADBError::FramebufferConversionError)?,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
v => Err(RustADBError::UnimplementedFramebufferImageVersion(v)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
models::{AdbServerCommand, RebootType},
|
|
||||||
ADBServerDevice, Result,
|
|
||||||
};
|
|
||||||
|
|
||||||
impl ADBServerDevice {
|
|
||||||
/// Reboots the device
|
|
||||||
pub fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
|
|
||||||
let serial = self.identifier.clone();
|
|
||||||
self.connect()?
|
|
||||||
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
|
|
||||||
|
|
||||||
self.get_transport_mut()
|
|
||||||
.proxy_connection(AdbServerCommand::Reboot(reboot_type), false)
|
|
||||||
.map(|_| ())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
use crate::{models::AdbServerCommand, ADBServerDevice, Result};
|
|
||||||
|
|
||||||
impl ADBServerDevice {
|
|
||||||
/// Reverse socket connection
|
|
||||||
pub fn reverse(&mut self, remote: String, local: String) -> Result<()> {
|
|
||||||
let serial = self.identifier.clone();
|
|
||||||
self.connect()?
|
|
||||||
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
|
|
||||||
|
|
||||||
self.get_transport_mut()
|
|
||||||
.proxy_connection(AdbServerCommand::Reverse(remote, local), false)
|
|
||||||
.map(|_| ())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
mod adb_emulator_device;
|
#![doc = include_str!("./README.md")]
|
||||||
mod adb_server;
|
|
||||||
mod adb_server_device;
|
mod adb_server;
|
||||||
mod adb_server_device_commands;
|
mod adb_server_command;
|
||||||
mod device_commands;
|
mod commands;
|
||||||
mod server_commands;
|
mod models;
|
||||||
|
mod tcp_server_transport;
|
||||||
|
|
||||||
pub use adb_emulator_device::ADBEmulatorDevice;
|
|
||||||
pub use adb_server::ADBServer;
|
pub use adb_server::ADBServer;
|
||||||
pub use adb_server_device::ADBServerDevice;
|
pub(crate) use adb_server_command::AdbServerCommand;
|
||||||
|
pub use models::*;
|
||||||
|
pub use tcp_server_transport::TCPServerTransport;
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user