Compare commits
11 Commits
up-session
...
v2.1.16
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34d5811420 | ||
|
|
86e28a6e25 | ||
|
|
9f113bdb93 | ||
|
|
8670d6db58 | ||
|
|
0732a0bbad | ||
|
|
b5673001ca | ||
|
|
81829c1523 | ||
|
|
b9d2b8374f | ||
|
|
5716784f5d | ||
|
|
b6ddc720d8 | ||
|
|
5438e53361 |
@@ -9,7 +9,8 @@ homepage = "https://github.com/cocool97/adb_client"
|
||||
keywords = ["adb", "android", "tcp", "usb"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/cocool97/adb_client"
|
||||
version = "2.1.13"
|
||||
version = "2.1.16"
|
||||
rust-version = "1.85.1"
|
||||
|
||||
# To build locally when working on a new release
|
||||
[patch.crates-io]
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
<a href="https://crates.io/crates/adb_client">
|
||||
<img alt="crates.io" src="https://img.shields.io/crates/v/adb_client.svg"/>
|
||||
</a>
|
||||
<a href="https://crates.io/crates/adb_client">
|
||||
<img alt="msrv" src="https://img.shields.io/crates/msrv/adb_client/latest"/>
|
||||
</a>
|
||||
<a href="https://github.com/cocool97/adb_client/actions">
|
||||
<img alt="ci status" src="https://github.com/cocool97/adb_client/actions/workflows/rust-build.yml/badge.svg"/>
|
||||
</a>
|
||||
|
||||
@@ -7,6 +7,7 @@ license.workspace = true
|
||||
name = "adb_cli"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
[](./LICENSE-MIT)
|
||||

|
||||

|
||||
|
||||
Rust binary providing an improved version of `adb` CLI.
|
||||
|
||||
@@ -10,7 +11,7 @@ Rust binary providing an improved version of `adb` CLI.
|
||||
This crate provides a lightweight binary based on the `adb_client` crate. You can install it by running the following command :
|
||||
|
||||
```shell
|
||||
cargo install adb_cli
|
||||
cargo install adb_cli
|
||||
```
|
||||
|
||||
Usage is quite simple, and tends to look like `adb`:
|
||||
@@ -64,4 +65,4 @@ Options:
|
||||
-p, --product-id <PID> Hexadecimal product id of this USB device
|
||||
-k, --private-key <PATH_TO_PRIVATE_KEY> Path to a custom private key to use for authentication
|
||||
-h, --help Print help
|
||||
```
|
||||
```
|
||||
|
||||
@@ -8,7 +8,7 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
match server_command.command {
|
||||
HostCommand::Version => {
|
||||
let version = adb_server.version()?;
|
||||
log::info!("Android Debug Bridge version {}", version);
|
||||
log::info!("Android Debug Bridge version {version}");
|
||||
log::info!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
|
||||
}
|
||||
HostCommand::Kill => {
|
||||
@@ -18,18 +18,18 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
if long {
|
||||
log::info!("List of devices attached (extended)");
|
||||
for device in adb_server.devices_long()? {
|
||||
log::info!("{}", device);
|
||||
log::info!("{device}");
|
||||
}
|
||||
} else {
|
||||
log::info!("List of devices attached");
|
||||
for device in adb_server.devices()? {
|
||||
log::info!("{}", device);
|
||||
log::info!("{device}");
|
||||
}
|
||||
}
|
||||
}
|
||||
HostCommand::TrackDevices => {
|
||||
let callback = |device: DeviceShort| {
|
||||
log::info!("{}", device);
|
||||
log::info!("{device}");
|
||||
Ok(())
|
||||
};
|
||||
log::info!("Live list of devices attached");
|
||||
@@ -65,7 +65,7 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
MdnsCommand::Services => {
|
||||
log::info!("List of discovered mdns services");
|
||||
for service in adb_server.mdns_services()? {
|
||||
log::info!("{}", service);
|
||||
log::info!("{service}");
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -132,10 +132,10 @@ fn main() -> Result<()> {
|
||||
}
|
||||
DeviceCommands::Stat { path } => {
|
||||
let stat_response = device.stat(&path)?;
|
||||
println!("{}", stat_response);
|
||||
println!("{stat_response}");
|
||||
}
|
||||
DeviceCommands::Reboot { reboot_type } => {
|
||||
log::info!("Reboots device in mode {:?}", reboot_type);
|
||||
log::info!("Reboots device in mode {reboot_type:?}");
|
||||
device.reboot(reboot_type.into())?
|
||||
}
|
||||
DeviceCommands::Push { filename, path } => {
|
||||
@@ -152,7 +152,7 @@ fn main() -> Result<()> {
|
||||
device.install(&path)?;
|
||||
}
|
||||
DeviceCommands::Uninstall { package } => {
|
||||
log::info!("Uninstalling the package {}...", package);
|
||||
log::info!("Uninstalling the package {package}...");
|
||||
device.uninstall(&package)?;
|
||||
}
|
||||
DeviceCommands::Framebuffer { path } => {
|
||||
|
||||
@@ -7,6 +7,7 @@ license.workspace = true
|
||||
name = "adb_client"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
@@ -14,7 +15,7 @@ base64 = { version = "0.22.1" }
|
||||
bincode = { version = "1.3.3" }
|
||||
byteorder = { version = "1.5.0" }
|
||||
chrono = { version = "0.4.40", default-features = false, features = ["std"] }
|
||||
homedir = { version = "0.3.4" }
|
||||
homedir = { version = "= 0.3.4" }
|
||||
image = { version = "0.25.5", default-features = false }
|
||||
log = { version = "0.4.26" }
|
||||
mdns-sd = { version = "0.13.9", default-features = false, features = [
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
[](./LICENSE-MIT)
|
||||
[](https://docs.rs/adb_client)
|
||||
[](https://crates.io/crates/adb_client)
|
||||

|
||||
|
||||
Rust library implementing ADB protocol.
|
||||
|
||||
|
||||
@@ -11,18 +11,18 @@ use super::{ADBTransportMessage, MessageCommand, models::MessageSubcommand};
|
||||
#[derive(Debug)]
|
||||
pub struct ADBMessageDevice<T: ADBMessageTransport> {
|
||||
transport: T,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ADBSession {
|
||||
pub local_id: u32,
|
||||
pub remote_id: u32,
|
||||
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 }
|
||||
Self {
|
||||
transport,
|
||||
local_id: None,
|
||||
remote_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_transport(&mut self) -> &T {
|
||||
@@ -34,15 +34,12 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
}
|
||||
|
||||
/// Receive a message and acknowledge it by replying with an `OKAY` command
|
||||
pub(crate) fn recv_and_reply_okay(
|
||||
&mut self,
|
||||
session: ADBSession,
|
||||
) -> Result<ADBTransportMessage> {
|
||||
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,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
self.get_local_id()?,
|
||||
self.get_remote_id()?,
|
||||
&[],
|
||||
))?;
|
||||
Ok(message)
|
||||
@@ -63,12 +60,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
pub(crate) fn recv_file<W: std::io::Write>(
|
||||
&mut self,
|
||||
session: ADBSession,
|
||||
mut output: W,
|
||||
) -> std::result::Result<(), RustADBError> {
|
||||
let mut len: Option<u64> = None;
|
||||
loop {
|
||||
let payload = self.recv_and_reply_okay(session)?.into_payload();
|
||||
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() {
|
||||
@@ -101,7 +97,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
pub(crate) fn push_file<R: std::io::Read>(
|
||||
&mut self,
|
||||
session: ADBSession,
|
||||
local_id: u32,
|
||||
remote_id: u32,
|
||||
mut reader: R,
|
||||
) -> std::result::Result<(), RustADBError> {
|
||||
let mut buffer = [0; BUFFER_SIZE];
|
||||
@@ -114,8 +111,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
let message = ADBTransportMessage::new(
|
||||
MessageCommand::Write,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
local_id,
|
||||
remote_id,
|
||||
&serialized_message,
|
||||
);
|
||||
|
||||
@@ -134,8 +131,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
let message = ADBTransportMessage::new(
|
||||
MessageCommand::Write,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
local_id,
|
||||
remote_id,
|
||||
&serialized_message,
|
||||
);
|
||||
|
||||
@@ -147,8 +144,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
MessageCommand::Write => return Ok(()),
|
||||
c => {
|
||||
return Err(RustADBError::ADBRequestFailed(format!(
|
||||
"Wrong command received {}",
|
||||
c
|
||||
"Wrong command received {c}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
@@ -162,8 +158,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
let message = ADBTransportMessage::new(
|
||||
MessageCommand::Write,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
local_id,
|
||||
remote_id,
|
||||
&serialized_message,
|
||||
);
|
||||
|
||||
@@ -176,27 +172,24 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn begin_synchronization(&mut self) -> Result<ADBSession> {
|
||||
self.open_session(b"sync:\0")
|
||||
pub(crate) fn begin_synchronization(&mut self) -> Result<()> {
|
||||
self.open_session(b"sync:\0")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn stat_with_explicit_ids(
|
||||
&mut self,
|
||||
session: ADBSession,
|
||||
remote_path: &str,
|
||||
) -> Result<AdbStatResponse> {
|
||||
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,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
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,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
self.get_local_id()?,
|
||||
self.get_remote_id()?,
|
||||
remote_path.as_bytes(),
|
||||
))?;
|
||||
let response = self.transport.read_message()?;
|
||||
@@ -206,25 +199,24 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
.map_err(|_e| RustADBError::ConversionError)
|
||||
}
|
||||
|
||||
pub(crate) fn end_transaction(&mut self, session: ADBSession) -> Result<()> {
|
||||
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,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
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<ADBSession> {
|
||||
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBTransportMessage> {
|
||||
let mut rng = rand::rng();
|
||||
let local_id: u32 = rng.random();
|
||||
|
||||
let message = ADBTransportMessage::new(
|
||||
MessageCommand::Open,
|
||||
local_id, // Our 'local-id'
|
||||
rng.random(), // Our 'local-id'
|
||||
0,
|
||||
data,
|
||||
);
|
||||
@@ -232,25 +224,21 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
let response = self.get_transport_mut().read_message()?;
|
||||
|
||||
if response.header().command() != MessageCommand::Okay {
|
||||
return Err(RustADBError::ADBRequestFailed(format!(
|
||||
"Open session failed: got {} in respone instead of OKAY",
|
||||
response.header().command()
|
||||
)));
|
||||
}
|
||||
self.local_id = Some(response.header().arg1());
|
||||
self.remote_id = Some(response.header().arg0());
|
||||
|
||||
if response.header().arg1() != local_id {
|
||||
return Err(RustADBError::ADBRequestFailed(format!(
|
||||
"Open session failed: respones used {} for our local_id instead of {local_id}",
|
||||
response.header().arg1()
|
||||
)));
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
let session = ADBSession {
|
||||
local_id,
|
||||
remote_id: response.header().arg0(),
|
||||
};
|
||||
pub(crate) fn get_local_id(&self) -> Result<u32> {
|
||||
self.local_id.ok_or(RustADBError::ADBRequestFailed(
|
||||
"connection not opened, no local_id".into(),
|
||||
))
|
||||
}
|
||||
|
||||
Ok(session)
|
||||
pub(crate) fn get_remote_id(&self) -> Result<u32> {
|
||||
self.remote_id.ok_or(RustADBError::ADBRequestFailed(
|
||||
"connection not opened, no remote_id".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,19 +19,24 @@ use crate::device::adb_transport_message::{AUTH_RSAPUBLICKEY, AUTH_SIGNATURE, AU
|
||||
use crate::{Result, RustADBError, USBTransport};
|
||||
|
||||
pub fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Option<ADBRsaKey>> {
|
||||
Ok(read_to_string(private_key_path.as_ref()).map(|pk| {
|
||||
match ADBRsaKey::new_from_pkcs8(&pk) {
|
||||
Ok(pk) => Some(pk),
|
||||
Err(e) => {
|
||||
log::error!("Error while create RSA private key: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
})?)
|
||||
// Try to read the private key file from given path
|
||||
// If the file is not found, return None
|
||||
// If there is another error while reading the file, return this error
|
||||
// Else, return the private key content
|
||||
let pk = match read_to_string(private_key_path.as_ref()) {
|
||||
Ok(pk) => pk,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
|
||||
match ADBRsaKey::new_from_pkcs8(&pk) {
|
||||
Ok(pk) => Ok(Some(pk)),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Search for adb devices with known interface class and subclass values
|
||||
fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||
pub fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||
let mut found_devices = vec![];
|
||||
for device in rusb::devices()?.iter() {
|
||||
let Ok(des) = device.device_descriptor() else {
|
||||
@@ -51,13 +56,13 @@ fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||
(None, _) => Ok(None),
|
||||
(Some(identifiers), None) => Ok(Some(*identifiers)),
|
||||
(Some((vid1, pid1)), Some((vid2, pid2))) => Err(RustADBError::DeviceNotFound(format!(
|
||||
"Found two Android devices {:04x}:{:04x} and {:04x}:{:04x}",
|
||||
vid1, pid1, vid2, pid2
|
||||
"Found two Android devices {vid1:04x}:{pid1:04x} and {vid2:04x}:{pid2:04x}",
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
||||
/// Check whether a device with given descriptor is an ADB device
|
||||
pub fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
||||
const ADB_SUBCLASS: u8 = 0x42;
|
||||
const ADB_PROTOCOL: u8 = 0x1;
|
||||
|
||||
@@ -134,9 +139,15 @@ impl ADBUSBDevice {
|
||||
transport: USBTransport,
|
||||
private_key_path: PathBuf,
|
||||
) -> Result<Self> {
|
||||
let private_key = match read_adb_private_key(private_key_path)? {
|
||||
let private_key = match read_adb_private_key(&private_key_path)? {
|
||||
Some(pk) => pk,
|
||||
None => ADBRsaKey::new_random()?,
|
||||
None => {
|
||||
log::warn!(
|
||||
"No private key found at path {}. Using a temporary random one.",
|
||||
private_key_path.display()
|
||||
);
|
||||
ADBRsaKey::new_random()?
|
||||
}
|
||||
};
|
||||
|
||||
let mut s = Self {
|
||||
@@ -180,6 +191,11 @@ impl ADBUSBDevice {
|
||||
self.get_transport_mut().write_message(message)?;
|
||||
|
||||
let message = self.get_transport_mut().read_message()?;
|
||||
// If the device returned CNXN instead of AUTH it does not require authentication,
|
||||
// so we can skip the auth steps.
|
||||
if message.header().command() == MessageCommand::Cnxn {
|
||||
return Ok(());
|
||||
}
|
||||
message.assert_command(MessageCommand::Auth)?;
|
||||
|
||||
// At this point, we should have receive an AUTH message with arg0 == 1
|
||||
|
||||
@@ -11,9 +11,9 @@ use crate::{
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
|
||||
let session = self.open_session(b"framebuffer:\0")?;
|
||||
self.open_session(b"framebuffer:\0")?;
|
||||
|
||||
let response = self.recv_and_reply_okay(session)?;
|
||||
let response = self.recv_and_reply_okay()?;
|
||||
|
||||
let mut payload_cursor = Cursor::new(response.payload());
|
||||
|
||||
@@ -36,7 +36,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
break;
|
||||
}
|
||||
|
||||
let response = self.recv_and_reply_okay(session)?;
|
||||
let response = self.recv_and_reply_okay()?;
|
||||
|
||||
framebuffer_data.extend_from_slice(&response.into_payload());
|
||||
|
||||
@@ -69,7 +69,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
break;
|
||||
}
|
||||
|
||||
let response = self.recv_and_reply_okay(session)?;
|
||||
let response = self.recv_and_reply_okay()?;
|
||||
|
||||
framebuffer_data.extend_from_slice(&response.into_payload());
|
||||
|
||||
|
||||
@@ -14,12 +14,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
let file_size = apk_file.metadata()?.len();
|
||||
|
||||
let session =
|
||||
self.open_session(format!("exec:cmd package 'install' -S {}\0", file_size).as_bytes())?;
|
||||
self.open_session(format!("exec:cmd package 'install' -S {file_size}\0").as_bytes())?;
|
||||
|
||||
let transport = self.get_transport().clone();
|
||||
|
||||
let mut writer = MessageWriter::new(transport, session.local_id, session.remote_id);
|
||||
let mut writer = MessageWriter::new(transport, self.get_local_id()?, self.get_remote_id()?);
|
||||
|
||||
std::io::copy(&mut apk_file, &mut writer)?;
|
||||
|
||||
|
||||
@@ -10,10 +10,10 @@ use crate::{
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> {
|
||||
let session = self.begin_synchronization()?;
|
||||
self.begin_synchronization()?;
|
||||
let source = source.as_ref();
|
||||
|
||||
let adb_stat_response = self.stat_with_explicit_ids(session, source)?;
|
||||
let adb_stat_response = self.stat_with_explicit_ids(source)?;
|
||||
|
||||
if adb_stat_response.file_perm == 0 {
|
||||
return Err(RustADBError::UnknownResponseType(
|
||||
@@ -21,13 +21,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
));
|
||||
}
|
||||
|
||||
let local_id = self.get_local_id()?;
|
||||
let remote_id = self.get_remote_id()?;
|
||||
|
||||
self.get_transport_mut().write_message_with_timeout(
|
||||
ADBTransportMessage::new(
|
||||
MessageCommand::Okay,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
&[],
|
||||
),
|
||||
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]),
|
||||
std::time::Duration::from_secs(4),
|
||||
)?;
|
||||
|
||||
@@ -36,19 +34,19 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?;
|
||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||
MessageCommand::Write,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
self.get_local_id()?,
|
||||
self.get_remote_id()?,
|
||||
&recv_buffer,
|
||||
))?;
|
||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||
MessageCommand::Write,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
self.get_local_id()?,
|
||||
self.get_remote_id()?,
|
||||
source.as_bytes(),
|
||||
))?;
|
||||
|
||||
self.recv_file(session, output)?;
|
||||
self.end_transaction(session)?;
|
||||
self.recv_file(output)?;
|
||||
self.end_transaction()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::{
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
|
||||
let session = self.begin_synchronization()?;
|
||||
self.begin_synchronization()?;
|
||||
|
||||
let path_header = format!("{},0777", path.as_ref());
|
||||
|
||||
@@ -21,13 +21,14 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
|
||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||
MessageCommand::Write,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
self.get_local_id()?,
|
||||
self.get_remote_id()?,
|
||||
&send_buffer,
|
||||
))?;
|
||||
|
||||
self.push_file(session, stream)?;
|
||||
self.end_transaction(session)?;
|
||||
self.push_file(self.get_local_id()?, self.get_remote_id()?, stream)?;
|
||||
|
||||
self.end_transaction()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::{
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
|
||||
self.open_session(format!("reboot:{}\0", reboot_type).as_bytes())?;
|
||||
self.open_session(format!("reboot:{reboot_type}\0").as_bytes())?;
|
||||
|
||||
self.get_transport_mut()
|
||||
.read_message()
|
||||
|
||||
@@ -10,7 +10,14 @@ use crate::{
|
||||
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 session = self.open_session(format!("shell:{}\0", command.join(" "),).as_bytes())?;
|
||||
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()?;
|
||||
@@ -31,22 +38,21 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
mut reader: &mut dyn Read,
|
||||
mut writer: Box<(dyn Write + Send)>,
|
||||
) -> Result<()> {
|
||||
let session = self.open_session(b"shell:\0")?;
|
||||
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,
|
||||
session.local_id,
|
||||
session.remote_id,
|
||||
&[],
|
||||
);
|
||||
let response =
|
||||
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]);
|
||||
transport.write_message(response)?;
|
||||
|
||||
match message.header().command() {
|
||||
@@ -61,8 +67,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
});
|
||||
|
||||
let transport = self.get_transport().clone();
|
||||
let mut shell_writer =
|
||||
ShellMessageWriter::new(transport, session.local_id, session.remote_id);
|
||||
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) {
|
||||
|
||||
@@ -4,9 +4,9 @@ use crate::{
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
||||
let session = self.begin_synchronization()?;
|
||||
let adb_stat_response = self.stat_with_explicit_ids(session, remote_path)?;
|
||||
self.end_transaction(session)?;
|
||||
self.begin_synchronization()?;
|
||||
let adb_stat_response = self.stat_with_explicit_ids(remote_path)?;
|
||||
self.end_transaction()?;
|
||||
Ok(adb_stat_response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ use crate::{ADBMessageTransport, Result, device::adb_message_device::ADBMessageD
|
||||
|
||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||
pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> {
|
||||
self.open_session(format!("exec:cmd package 'uninstall' {}\0", package_name).as_bytes())?;
|
||||
self.open_session(format!("exec:cmd package 'uninstall' {package_name}\0").as_bytes())?;
|
||||
|
||||
let final_status = self.get_transport_mut().read_message()?;
|
||||
|
||||
match final_status.into_payload().as_slice() {
|
||||
b"Success\n" => {
|
||||
log::info!("Package {} successfully uninstalled", package_name);
|
||||
log::info!("Package {package_name} successfully uninstalled");
|
||||
Ok(())
|
||||
}
|
||||
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||
|
||||
@@ -35,10 +35,10 @@ impl<T: ADBMessageTransport> Write for MessageWriter<T> {
|
||||
Ok(response) => {
|
||||
response
|
||||
.assert_command(MessageCommand::Okay)
|
||||
.map_err(|e| Error::new(ErrorKind::Other, e))?;
|
||||
.map_err(Error::other)?;
|
||||
Ok(buf.len())
|
||||
}
|
||||
Err(e) => Err(Error::new(ErrorKind::Other, e)),
|
||||
Err(e) => Err(Error::other(e)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ mod shell_message_writer;
|
||||
use adb_message_device::ADBMessageDevice;
|
||||
pub use adb_tcp_device::ADBTcpDevice;
|
||||
pub use adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader};
|
||||
pub use adb_usb_device::{ADBUSBDevice, get_default_adb_key_path};
|
||||
pub use adb_usb_device::{
|
||||
ADBUSBDevice, get_default_adb_key_path, is_adb_device, search_adb_devices,
|
||||
};
|
||||
pub use message_writer::MessageWriter;
|
||||
pub use models::{ADBRsaKey, MessageCommand, MessageSubcommand};
|
||||
pub use shell_message_writer::ShellMessageWriter;
|
||||
|
||||
@@ -30,8 +30,7 @@ impl ADBEmulatorDevice {
|
||||
let groups = EMULATOR_REGEX
|
||||
.captures(&identifier)
|
||||
.ok_or(RustADBError::DeviceNotFound(format!(
|
||||
"Device {} is likely not an emulator",
|
||||
identifier
|
||||
"Device {identifier} is likely not an emulator"
|
||||
)))?;
|
||||
|
||||
let port = groups
|
||||
|
||||
@@ -17,7 +17,7 @@ mod transports;
|
||||
mod utils;
|
||||
|
||||
pub use adb_device_ext::ADBDeviceExt;
|
||||
pub use device::{ADBTcpDevice, ADBUSBDevice};
|
||||
pub use device::{ADBTcpDevice, ADBUSBDevice, is_adb_device, search_adb_devices};
|
||||
pub use emulator_device::ADBEmulatorDevice;
|
||||
pub use error::{Result, RustADBError};
|
||||
pub use mdns::*;
|
||||
|
||||
@@ -63,8 +63,8 @@ impl Display for AdbServerCommand {
|
||||
AdbServerCommand::Reboot(reboot_type) => {
|
||||
write!(f, "reboot:{reboot_type}")
|
||||
}
|
||||
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{}", addr),
|
||||
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{}", addr),
|
||||
AdbServerCommand::Connect(addr) => write!(f, "host:connect:{addr}"),
|
||||
AdbServerCommand::Disconnect(addr) => write!(f, "host:disconnect:{addr}"),
|
||||
AdbServerCommand::Pair(addr, code) => {
|
||||
write!(f, "host:pair:{code}:{addr}")
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ impl ADBServer {
|
||||
Ok(service) => {
|
||||
vec_services.push(MDNSServices::try_from(service.as_bytes())?);
|
||||
}
|
||||
Err(e) => log::error!("{}", e),
|
||||
Err(e) => log::error!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ impl ADBServerDevice {
|
||||
"DONE" => {
|
||||
return Ok(());
|
||||
}
|
||||
x => log::error!("Got an unknown response {}", x),
|
||||
x => log::error!("Got an unknown response {x}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,18 +42,14 @@ impl<R: Read> Read for ADBRecvCommandReader<R> {
|
||||
let mut error_msg = vec![0; length];
|
||||
self.inner.read_exact(&mut error_msg)?;
|
||||
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!(
|
||||
"ADB request failed: {}",
|
||||
String::from_utf8_lossy(&error_msg)
|
||||
),
|
||||
))
|
||||
Err(std::io::Error::other(format!(
|
||||
"ADB request failed: {}",
|
||||
String::from_utf8_lossy(&error_msg)
|
||||
)))
|
||||
}
|
||||
_ => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("Unknown response from device {:#?}", header),
|
||||
)),
|
||||
_ => Err(std::io::Error::other(format!(
|
||||
"Unknown response from device {header:#?}"
|
||||
))),
|
||||
}
|
||||
} else {
|
||||
// Computing minimum to ensure to stop reading before next header...
|
||||
|
||||
@@ -31,8 +31,7 @@ impl ADBServerDevice {
|
||||
Ok(data.into())
|
||||
}
|
||||
x => Err(RustADBError::UnknownResponseType(format!(
|
||||
"Unknown response {}",
|
||||
x
|
||||
"Unknown response {x}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ impl ADBServerDevice {
|
||||
|
||||
match &data[0..read_amount] {
|
||||
b"Success\n" => {
|
||||
log::info!("Package {} successfully uninstalled", package_name);
|
||||
log::info!("Package {package_name} successfully uninstalled");
|
||||
Ok(())
|
||||
}
|
||||
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||
|
||||
@@ -99,13 +99,13 @@ impl TCPServerTransport {
|
||||
}
|
||||
|
||||
/// Gets the body length from a LittleEndian value
|
||||
pub(crate) fn get_body_length(&mut self) -> Result<u32> {
|
||||
pub(crate) fn get_body_length(&self) -> Result<u32> {
|
||||
let length_buffer = self.read_body_length()?;
|
||||
Ok(LittleEndian::read_u32(&length_buffer))
|
||||
}
|
||||
|
||||
/// Read 4 bytes representing body length
|
||||
fn read_body_length(&mut self) -> Result<[u8; 4]> {
|
||||
fn read_body_length(&self) -> Result<[u8; 4]> {
|
||||
let mut length_buffer = [0; 4];
|
||||
self.get_raw_connection()?.read_exact(&mut length_buffer)?;
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ impl TcpTransport {
|
||||
})
|
||||
}
|
||||
|
||||
fn get_current_connection(&mut self) -> Result<Arc<Mutex<CurrentConnection>>> {
|
||||
fn get_current_connection(&self) -> Result<Arc<Mutex<CurrentConnection>>> {
|
||||
self.current_connection
|
||||
.as_ref()
|
||||
.ok_or(RustADBError::IOError(std::io::Error::new(
|
||||
@@ -175,8 +175,7 @@ impl TcpTransport {
|
||||
Ok(())
|
||||
}
|
||||
c => Err(RustADBError::ADBRequestFailed(format!(
|
||||
"Wrong command received {}",
|
||||
c
|
||||
"Wrong command received {c}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ impl USBTransport {
|
||||
}
|
||||
|
||||
Err(RustADBError::DeviceNotFound(format!(
|
||||
"cannot find USB device with vendor_id={} and product_id={}",
|
||||
vendor_id, product_id
|
||||
"cannot find USB device with vendor_id={vendor_id} and product_id={product_id}",
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -151,12 +150,7 @@ impl USBTransport {
|
||||
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
|
||||
)
|
||||
log::trace!("wrote chunk of size {write_amount} - {offset}/{data_len}",)
|
||||
}
|
||||
|
||||
if offset % max_packet_size == 0 {
|
||||
@@ -175,11 +169,11 @@ impl ADBTransport for USBTransport {
|
||||
let (read_endpoint, write_endpoint) = self.find_endpoints(&device)?;
|
||||
|
||||
Self::configure_endpoint(&device, &read_endpoint)?;
|
||||
log::debug!("got read endpoint: {:?}", read_endpoint);
|
||||
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);
|
||||
log::debug!("got write endpoint: {write_endpoint:?}");
|
||||
self.write_endpoint = Some(write_endpoint);
|
||||
|
||||
self.handle = Some(Arc::new(device));
|
||||
@@ -197,7 +191,7 @@ impl ADBTransport for USBTransport {
|
||||
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"),
|
||||
Ok(()) => log::debug!("succesfully released interface"),
|
||||
Err(e) => log::error!("error while release interface: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ name = "pyadb_client"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
version.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
Reference in New Issue
Block a user