8 Commits

Author SHA1 Message Date
LIAUD Corentin
4126c856d9 wip: feature-based 2025-08-17 10:46:40 +02:00
LIAUD Corentin
d39e98695d fix: wrong msrv badges url 2025-08-03 17:38:11 +02:00
LIAUD Corentin
34d5811420 chore: version 2.1.16 2025-08-03 17:32:50 +02:00
LIAUD Corentin
86e28a6e25 chore: add msrv badges 2025-08-03 17:31:24 +02:00
LIAUD Corentin
9f113bdb93 chore: add msrv + fix clippy lints 2025-08-03 17:27:54 +02:00
cocool97
8670d6db58 fix: use random adb key if not existing (#130) 2025-08-03 17:08:15 +02:00
LIAUD Corentin
0732a0bbad chore: v2.1.15 2025-07-27 20:20:57 +02:00
cocool97
b5673001ca feat: make search_adb_devices and is_adb_device public (#129) 2025-07-27 20:19:03 +02:00
28 changed files with 85 additions and 60 deletions

View File

@@ -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.14"
version = "2.1.16"
rust-version = "1.85.1"
# To build locally when working on a new release
[patch.crates-io]

View File

@@ -8,6 +8,9 @@
<a href="https://crates.io/crates/adb_client">
<img alt="crates.io" src="https://img.shields.io/crates/v/adb_client.svg"/>
</a>
<a href="https://crates.io/crates/adb_client">
<img alt="msrv" src="https://img.shields.io/crates/msrv/adb_client"/>
</a>
<a href="https://github.com/cocool97/adb_client/actions">
<img alt="ci status" src="https://github.com/cocool97/adb_client/actions/workflows/rust-build.yml/badge.svg"/>
</a>

View File

@@ -7,6 +7,7 @@ license.workspace = true
name = "adb_cli"
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
version.workspace = true
[dependencies]

View File

@@ -2,6 +2,7 @@
[![MIT licensed](https://img.shields.io/crates/l/adb_cli.svg)](./LICENSE-MIT)
![Crates.io Total Downloads](https://img.shields.io/crates/d/adb_cli)
![MSRV](https://img.shields.io/crates/msrv/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 :
```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
```
```

View File

@@ -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}");
}
}
},

View File

@@ -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 } => {

View File

@@ -7,8 +7,13 @@ license.workspace = true
name = "adb_client"
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
version.workspace = true
[features]
default = ["mdns"]
mdns = ["dep:mdns-sd"]
[dependencies]
base64 = { version = "0.22.1" }
bincode = { version = "1.3.3" }
@@ -17,9 +22,6 @@ chrono = { version = "0.4.40", default-features = false, features = ["std"] }
homedir = { version = "= 0.3.4" }
image = { version = "0.25.5", default-features = false }
log = { version = "0.4.26" }
mdns-sd = { version = "0.13.9", default-features = false, features = [
"logging",
] }
num-bigint = { version = "0.8.4", package = "num-bigint-dig" }
num-traits = { version = "0.2.19" }
quick-protobuf = { version = "0.8.1" }
@@ -38,6 +40,11 @@ serde_repr = { version = "0.1.19" }
sha1 = { version = "0.10.6", features = ["oid"] }
thiserror = { version = "2.0.7" }
# MDNS
mdns-sd = { version = "0.13.9", default-features = false, optional = true, features = [
"logging",
] }
[dev-dependencies]
anyhow = { version = "1.0.93" }
criterion = { version = "0.6.0" } # Used for benchmarks

View File

@@ -3,6 +3,7 @@
[![MIT licensed](https://img.shields.io/crates/l/adb_client.svg)](./LICENSE-MIT)
[![Documentation](https://docs.rs/adb_client/badge.svg)](https://docs.rs/adb_client)
[![Crates.io Total Downloads](https://img.shields.io/crates/d/adb_client)](https://crates.io/crates/adb_client)
![MSRV](https://img.shields.io/crates/msrv/adb_client)
Rust library implementing ADB protocol.

View File

@@ -144,8 +144,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
MessageCommand::Write => return Ok(()),
c => {
return Err(RustADBError::ADBRequestFailed(format!(
"Wrong command received {}",
c
"Wrong command received {c}"
)));
}
}

View File

@@ -19,15 +19,20 @@ 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
@@ -51,8 +56,7 @@ pub 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}",
))),
}
}
@@ -135,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 {

View File

@@ -14,7 +14,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
let file_size = apk_file.metadata()?.len();
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();

View File

@@ -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()

View File

@@ -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(

View File

@@ -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

View File

@@ -112,6 +112,7 @@ pub enum RustADBError {
#[error("upgrade error: {0}")]
UpgradeError(String),
/// An error occurred while getting mdns devices
#[cfg(feature = "mdns")]
#[error(transparent)]
MDNSError(#[from] mdns_sd::Error),
/// An error occurred while sending data to channel

View File

@@ -9,6 +9,7 @@ mod constants;
mod device;
mod emulator_device;
mod error;
#[cfg(feature = "mdns")]
mod mdns;
mod models;
mod server;
@@ -20,6 +21,7 @@ pub use adb_device_ext::ADBDeviceExt;
pub use device::{ADBTcpDevice, ADBUSBDevice, is_adb_device, search_adb_devices};
pub use emulator_device::ADBEmulatorDevice;
pub use error::{Result, RustADBError};
#[cfg(feature = "mdns")]
pub use mdns::*;
pub use models::{AdbStatResponse, RebootType};
pub use server::*;

View File

@@ -18,7 +18,9 @@ pub(crate) enum AdbServerCommand {
Pair(SocketAddrV4, String),
TransportAny,
TransportSerial(String),
#[cfg(feature = "mdns")]
MDNSCheck,
#[cfg(feature = "mdns")]
MDNSServices,
ServerStatus,
ReconnectOffline,
@@ -63,8 +65,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}")
}
@@ -77,7 +79,9 @@ impl Display for AdbServerCommand {
write!(f, "reverse:forward:{remote};{local}")
}
AdbServerCommand::ReverseRemoveAll => write!(f, "reverse:killforward-all"),
#[cfg(feature = "mdns")]
AdbServerCommand::MDNSCheck => write!(f, "host:mdns:check"),
#[cfg(feature = "mdns")]
AdbServerCommand::MDNSServices => write!(f, "host:mdns:services"),
AdbServerCommand::ServerStatus => write!(f, "host:server-status"),
AdbServerCommand::Reconnect => write!(f, "reconnect"),

View File

@@ -6,6 +6,7 @@ use crate::{
const OPENSCREEN_MDNS_BACKEND: &str = "ADB_MDNS_OPENSCREEN";
#[cfg(feature = "mdns")]
impl ADBServer {
/// Check if mdns discovery is available
pub fn mdns_check(&mut self) -> Result<bool> {
@@ -32,7 +33,7 @@ impl ADBServer {
Ok(service) => {
vec_services.push(MDNSServices::try_from(service.as_bytes())?);
}
Err(e) => log::error!("{}", e),
Err(e) => log::error!("{e}"),
}
}

View File

@@ -2,6 +2,7 @@ mod connect;
mod devices;
mod disconnect;
mod kill;
#[cfg(feature = "mdns")]
mod mdns;
mod pair;
mod reconnect;

View File

@@ -2,6 +2,7 @@ mod adb_version;
mod device_long;
mod device_short;
mod device_state;
#[cfg(feature = "mdns")]
mod mdns_services;
mod server_status;
mod wait_for_device;
@@ -10,6 +11,7 @@ pub use adb_version::AdbVersion;
pub use device_long::DeviceLong;
pub use device_short::DeviceShort;
pub use device_state::DeviceState;
#[cfg(feature = "mdns")]
pub use mdns_services::MDNSServices;
pub use server_status::{MDNSBackend, ServerStatus};
pub use wait_for_device::{WaitForDeviceState, WaitForDeviceTransport};

View File

@@ -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}"),
}
}
}

View File

@@ -48,8 +48,7 @@ impl<R: Read> Read for ADBRecvCommandReader<R> {
)))
}
_ => Err(std::io::Error::other(format!(
"Unknown response from device {:#?}",
header
"Unknown response from device {header:#?}"
))),
}
} else {

View File

@@ -31,8 +31,7 @@ impl ADBServerDevice {
Ok(data.into())
}
x => Err(RustADBError::UnknownResponseType(format!(
"Unknown response {}",
x
"Unknown response {x}"
))),
}
}

View File

@@ -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(

View File

@@ -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)?;

View File

@@ -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}"
))),
}
}

View File

@@ -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}"),
}
}

View File

@@ -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"]