feat: add wait-for-device command (#96)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use adb_client::{ADBServer, DeviceShort, MDNSBackend, Result};
|
||||
use adb_client::{ADBServer, DeviceShort, MDNSBackend, Result, WaitForDeviceState};
|
||||
|
||||
use crate::models::{HostCommand, MdnsCommand, ServerCommand};
|
||||
|
||||
@@ -72,6 +72,10 @@ pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Resul
|
||||
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(())
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
use adb_client::{RustADBError, 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.
|
||||
@@ -28,6 +35,12 @@ pub enum HostCommand {
|
||||
},
|
||||
/// 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)]
|
||||
|
||||
@@ -117,6 +117,9 @@ pub enum RustADBError {
|
||||
/// An error occurred while sending data to channel
|
||||
#[error(transparent)]
|
||||
SendError(#[from] std::sync::mpsc::SendError<crate::MDNSDevice>),
|
||||
/// An unknown transport has been provided
|
||||
#[error("unknown transport: {0}")]
|
||||
UnknownTransport(String),
|
||||
}
|
||||
|
||||
impl<T> From<std::sync::PoisonError<T>> for RustADBError {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::{WaitForDeviceState, WaitForDeviceTransport};
|
||||
|
||||
use super::RebootType;
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
@@ -22,6 +24,7 @@ pub(crate) enum AdbServerCommand {
|
||||
ReconnectOffline,
|
||||
Uninstall(String),
|
||||
Install(u64),
|
||||
WaitForDevice(WaitForDeviceState, WaitForDeviceTransport),
|
||||
// Local commands
|
||||
ShellCommand(String),
|
||||
Shell,
|
||||
@@ -87,6 +90,12 @@ impl Display for AdbServerCommand {
|
||||
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}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,3 +7,4 @@ mod pair;
|
||||
mod reconnect;
|
||||
mod server_status;
|
||||
mod version;
|
||||
mod wait_for_device;
|
||||
|
||||
20
adb_client/src/server/commands/wait_for_device.rs
Normal file
20
adb_client/src/server/commands/wait_for_device.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use crate::{
|
||||
models::AdbServerCommand, ADBServer, Result, 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()
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ mod device_short;
|
||||
mod device_state;
|
||||
mod mdns_services;
|
||||
mod server_status;
|
||||
mod wait_for_device;
|
||||
|
||||
pub use adb_version::AdbVersion;
|
||||
pub use device_long::DeviceLong;
|
||||
@@ -11,3 +12,4 @@ pub use device_short::DeviceShort;
|
||||
pub use device_state::DeviceState;
|
||||
pub use mdns_services::MDNSServices;
|
||||
pub use server_status::{MDNSBackend, ServerStatus};
|
||||
pub use wait_for_device::{WaitForDeviceState, WaitForDeviceTransport};
|
||||
|
||||
67
adb_client/src/server/models/wait_for_device.rs
Normal file
67
adb_client/src/server/models/wait_for_device.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::RustADBError;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
/// List of available transports to wait for.
|
||||
pub enum WaitForDeviceTransport {
|
||||
/// USB transport
|
||||
Usb,
|
||||
/// Local transport
|
||||
Local,
|
||||
/// Any transport (default value)
|
||||
Any,
|
||||
}
|
||||
|
||||
impl Default for WaitForDeviceTransport {
|
||||
fn default() -> Self {
|
||||
Self::Any
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for WaitForDeviceTransport {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
WaitForDeviceTransport::Usb => write!(f, "usb"),
|
||||
WaitForDeviceTransport::Local => write!(f, "local"),
|
||||
WaitForDeviceTransport::Any => write!(f, "any"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for WaitForDeviceTransport {
|
||||
type Error = RustADBError;
|
||||
|
||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
"usb" => Ok(Self::Usb),
|
||||
"local" => Ok(Self::Local),
|
||||
"any" => Ok(Self::Any),
|
||||
t => Err(RustADBError::UnknownTransport(t.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// List of available states to wait for.
|
||||
pub enum WaitForDeviceState {
|
||||
/// Device in "device" state
|
||||
Device,
|
||||
/// Device in "recovery" state
|
||||
Recovery,
|
||||
/// Device in "sideload" state
|
||||
Sideload,
|
||||
/// Device in "bootloader" state
|
||||
Bootloader,
|
||||
}
|
||||
|
||||
impl Display for WaitForDeviceState {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
WaitForDeviceState::Device => write!(f, "device"),
|
||||
WaitForDeviceState::Recovery => write!(f, "recovery"),
|
||||
WaitForDeviceState::Sideload => write!(f, "sideload"),
|
||||
WaitForDeviceState::Bootloader => write!(f, "bootloader"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,6 +113,11 @@ impl TCPServerTransport {
|
||||
self.get_raw_connection()?
|
||||
.write_all(adb_request.as_bytes())?;
|
||||
|
||||
self.read_adb_response()
|
||||
}
|
||||
|
||||
/// Read a response from ADB server
|
||||
pub(crate) fn read_adb_response(&mut self) -> Result<()> {
|
||||
// Reads returned status code from ADB server
|
||||
let mut request_status = [0; 4];
|
||||
self.get_raw_connection()?.read_exact(&mut request_status)?;
|
||||
|
||||
Reference in New Issue
Block a user