diff --git a/adb_cli/src/models/adb_cli_error.rs b/adb_cli/src/models/adb_cli_error.rs index 9a04215..e479fcf 100644 --- a/adb_cli/src/models/adb_cli_error.rs +++ b/adb_cli/src/models/adb_cli_error.rs @@ -77,7 +77,8 @@ impl From for ADBCliError { | RustADBError::ADBShellNotSupported | RustADBError::USBDeviceNotFound(_, _) | RustADBError::WrongFileExtension(_) - | RustADBError::AddrParseError(_) => Self::Standard(value), + | RustADBError::AddrParseError(_) + | RustADBError::DeviceBusy => Self::Standard(value), } } } diff --git a/adb_client/src/error.rs b/adb_client/src/error.rs index c1da179..94859af 100644 --- a/adb_client/src/error.rs +++ b/adb_client/src/error.rs @@ -74,6 +74,11 @@ pub enum RustADBError { #[cfg_attr(docsrs, doc(cfg(feature = "usb")))] #[error("USB Error: {0}")] UsbError(#[from] rusb::Error), + /// Selected device is busy. + #[cfg(feature = "usb")] + #[cfg_attr(docsrs, doc(cfg(feature = "usb")))] + #[error("Device is busy. Is ADB server running?")] + DeviceBusy, /// USB device not found #[error("USB Device not found: {0} {1}")] USBDeviceNotFound(u16, u16), diff --git a/adb_client/src/message_devices/usb/usb_transport.rs b/adb_client/src/message_devices/usb/usb_transport.rs index 099c845..74a1d78 100644 --- a/adb_client/src/message_devices/usb/usb_transport.rs +++ b/adb_client/src/message_devices/usb/usb_transport.rs @@ -92,8 +92,12 @@ impl USBTransport { } fn configure_endpoint(handle: &DeviceHandle, endpoint: &Endpoint) -> Result<()> { - handle.claim_interface(endpoint.iface)?; - Ok(()) + match handle.claim_interface(endpoint.iface) { + Ok(()) => Ok(()), + // busy state likely indicates an ADB server is running and has taken the lock over the device + Err(rusb::Error::Busy) => Err(RustADBError::DeviceBusy), + Err(err) => Err(err.into()), + } } fn find_endpoints(handle: &DeviceHandle) -> Result<(Endpoint, Endpoint)> { @@ -185,6 +189,11 @@ impl ADBTransport for USBTransport { } fn disconnect(&mut self) -> crate::Result<()> { + if self.handle.is_none() { + // device has not been initialized, nothing to do + return Ok(()); + } + let message = ADBTransportMessage::try_new(MessageCommand::Clse, 0, 0, &[])?; if let Err(e) = self.write_message(message) { log::error!("error while sending CLSE message: {e}");