Compare commits
1 Commits
126-method
...
up-session
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
849f7807a9 |
@@ -9,7 +9,7 @@ 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.1.14"
|
version = "2.1.13"
|
||||||
|
|
||||||
# To build locally when working on a new release
|
# To build locally when working on a new release
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ 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.40", default-features = false, features = ["std"] }
|
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 }
|
image = { version = "0.25.5", default-features = false }
|
||||||
log = { version = "0.4.26" }
|
log = { version = "0.4.26" }
|
||||||
mdns-sd = { version = "0.13.9", default-features = false, features = [
|
mdns-sd = { version = "0.13.9", default-features = false, features = [
|
||||||
|
|||||||
@@ -11,18 +11,18 @@ use super::{ADBTransportMessage, MessageCommand, models::MessageSubcommand};
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ADBMessageDevice<T: ADBMessageTransport> {
|
pub struct ADBMessageDevice<T: ADBMessageTransport> {
|
||||||
transport: T,
|
transport: T,
|
||||||
local_id: Option<u32>,
|
}
|
||||||
remote_id: Option<u32>,
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct ADBSession {
|
||||||
|
pub local_id: u32,
|
||||||
|
pub remote_id: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
/// Instantiate a new [`ADBMessageTransport`]
|
/// Instantiate a new [`ADBMessageTransport`]
|
||||||
pub fn new(transport: T) -> Self {
|
pub fn new(transport: T) -> Self {
|
||||||
Self {
|
Self { transport }
|
||||||
transport,
|
|
||||||
local_id: None,
|
|
||||||
remote_id: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_transport(&mut self) -> &T {
|
pub(crate) fn get_transport(&mut self) -> &T {
|
||||||
@@ -34,12 +34,15 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Receive a message and acknowledge it by replying with an `OKAY` command
|
/// Receive a message and acknowledge it by replying with an `OKAY` command
|
||||||
pub(crate) fn recv_and_reply_okay(&mut self) -> Result<ADBTransportMessage> {
|
pub(crate) fn recv_and_reply_okay(
|
||||||
|
&mut self,
|
||||||
|
session: ADBSession,
|
||||||
|
) -> Result<ADBTransportMessage> {
|
||||||
let message = self.transport.read_message()?;
|
let message = self.transport.read_message()?;
|
||||||
self.transport.write_message(ADBTransportMessage::new(
|
self.transport.write_message(ADBTransportMessage::new(
|
||||||
MessageCommand::Okay,
|
MessageCommand::Okay,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
&[],
|
&[],
|
||||||
))?;
|
))?;
|
||||||
Ok(message)
|
Ok(message)
|
||||||
@@ -60,11 +63,12 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
pub(crate) fn recv_file<W: std::io::Write>(
|
pub(crate) fn recv_file<W: std::io::Write>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
session: ADBSession,
|
||||||
mut output: W,
|
mut output: W,
|
||||||
) -> std::result::Result<(), RustADBError> {
|
) -> std::result::Result<(), RustADBError> {
|
||||||
let mut len: Option<u64> = None;
|
let mut len: Option<u64> = None;
|
||||||
loop {
|
loop {
|
||||||
let payload = self.recv_and_reply_okay()?.into_payload();
|
let payload = self.recv_and_reply_okay(session)?.into_payload();
|
||||||
let mut rdr = Cursor::new(&payload);
|
let mut rdr = Cursor::new(&payload);
|
||||||
while rdr.position() != payload.len() as u64 {
|
while rdr.position() != payload.len() as u64 {
|
||||||
match len.take() {
|
match len.take() {
|
||||||
@@ -97,8 +101,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
pub(crate) fn push_file<R: std::io::Read>(
|
pub(crate) fn push_file<R: std::io::Read>(
|
||||||
&mut self,
|
&mut self,
|
||||||
local_id: u32,
|
session: ADBSession,
|
||||||
remote_id: u32,
|
|
||||||
mut reader: R,
|
mut reader: R,
|
||||||
) -> std::result::Result<(), RustADBError> {
|
) -> std::result::Result<(), RustADBError> {
|
||||||
let mut buffer = [0; BUFFER_SIZE];
|
let mut buffer = [0; BUFFER_SIZE];
|
||||||
@@ -111,8 +114,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
let message = ADBTransportMessage::new(
|
let message = ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
local_id,
|
session.local_id,
|
||||||
remote_id,
|
session.remote_id,
|
||||||
&serialized_message,
|
&serialized_message,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -131,8 +134,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
let message = ADBTransportMessage::new(
|
let message = ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
local_id,
|
session.local_id,
|
||||||
remote_id,
|
session.remote_id,
|
||||||
&serialized_message,
|
&serialized_message,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -159,8 +162,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
let message = ADBTransportMessage::new(
|
let message = ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
local_id,
|
session.local_id,
|
||||||
remote_id,
|
session.remote_id,
|
||||||
&serialized_message,
|
&serialized_message,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -173,24 +176,27 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn begin_synchronization(&mut self) -> Result<()> {
|
pub(crate) fn begin_synchronization(&mut self) -> Result<ADBSession> {
|
||||||
self.open_session(b"sync:\0")?;
|
self.open_session(b"sync:\0")
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn stat_with_explicit_ids(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
pub(crate) fn stat_with_explicit_ids(
|
||||||
|
&mut self,
|
||||||
|
session: ADBSession,
|
||||||
|
remote_path: &str,
|
||||||
|
) -> Result<AdbStatResponse> {
|
||||||
let stat_buffer = MessageSubcommand::Stat.with_arg(remote_path.len() as u32);
|
let stat_buffer = MessageSubcommand::Stat.with_arg(remote_path.len() as u32);
|
||||||
let message = ADBTransportMessage::new(
|
let message = ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
&bincode::serialize(&stat_buffer).map_err(|_e| RustADBError::ConversionError)?,
|
&bincode::serialize(&stat_buffer).map_err(|_e| RustADBError::ConversionError)?,
|
||||||
);
|
);
|
||||||
self.send_and_expect_okay(message)?;
|
self.send_and_expect_okay(message)?;
|
||||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
remote_path.as_bytes(),
|
remote_path.as_bytes(),
|
||||||
))?;
|
))?;
|
||||||
let response = self.transport.read_message()?;
|
let response = self.transport.read_message()?;
|
||||||
@@ -200,24 +206,25 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
.map_err(|_e| RustADBError::ConversionError)
|
.map_err(|_e| RustADBError::ConversionError)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn end_transaction(&mut self) -> Result<()> {
|
pub(crate) fn end_transaction(&mut self, session: ADBSession) -> Result<()> {
|
||||||
let quit_buffer = MessageSubcommand::Quit.with_arg(0u32);
|
let quit_buffer = MessageSubcommand::Quit.with_arg(0u32);
|
||||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
&bincode::serialize(&quit_buffer).map_err(|_e| RustADBError::ConversionError)?,
|
&bincode::serialize(&quit_buffer).map_err(|_e| RustADBError::ConversionError)?,
|
||||||
))?;
|
))?;
|
||||||
let _discard_close = self.transport.read_message()?;
|
let _discard_close = self.transport.read_message()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBTransportMessage> {
|
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBSession> {
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
|
let local_id: u32 = rng.random();
|
||||||
|
|
||||||
let message = ADBTransportMessage::new(
|
let message = ADBTransportMessage::new(
|
||||||
MessageCommand::Open,
|
MessageCommand::Open,
|
||||||
rng.random(), // Our 'local-id'
|
local_id, // Our 'local-id'
|
||||||
0,
|
0,
|
||||||
data,
|
data,
|
||||||
);
|
);
|
||||||
@@ -225,21 +232,25 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
let response = self.get_transport_mut().read_message()?;
|
let response = self.get_transport_mut().read_message()?;
|
||||||
|
|
||||||
self.local_id = Some(response.header().arg1());
|
if response.header().command() != MessageCommand::Okay {
|
||||||
self.remote_id = Some(response.header().arg0());
|
return Err(RustADBError::ADBRequestFailed(format!(
|
||||||
|
"Open session failed: got {} in respone instead of OKAY",
|
||||||
Ok(response)
|
response.header().command()
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_local_id(&self) -> Result<u32> {
|
if response.header().arg1() != local_id {
|
||||||
self.local_id.ok_or(RustADBError::ADBRequestFailed(
|
return Err(RustADBError::ADBRequestFailed(format!(
|
||||||
"connection not opened, no local_id".into(),
|
"Open session failed: respones used {} for our local_id instead of {local_id}",
|
||||||
))
|
response.header().arg1()
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_remote_id(&self) -> Result<u32> {
|
let session = ADBSession {
|
||||||
self.remote_id.ok_or(RustADBError::ADBRequestFailed(
|
local_id,
|
||||||
"connection not opened, no remote_id".into(),
|
remote_id: response.header().arg0(),
|
||||||
))
|
};
|
||||||
|
|
||||||
|
Ok(session)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ pub fn read_adb_private_key<P: AsRef<Path>>(private_key_path: P) -> Result<Optio
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Search for adb devices with known interface class and subclass values
|
/// Search for adb devices with known interface class and subclass values
|
||||||
pub fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
||||||
let mut found_devices = vec![];
|
let mut found_devices = vec![];
|
||||||
for device in rusb::devices()?.iter() {
|
for device in rusb::devices()?.iter() {
|
||||||
let Ok(des) = device.device_descriptor() else {
|
let Ok(des) = device.device_descriptor() else {
|
||||||
@@ -57,8 +57,7 @@ pub fn search_adb_devices() -> Result<Option<(u16, u16)>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether a device with given descriptor is an ADB device
|
fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
||||||
pub fn is_adb_device<T: UsbContext>(device: &Device<T>, des: &DeviceDescriptor) -> bool {
|
|
||||||
const ADB_SUBCLASS: u8 = 0x42;
|
const ADB_SUBCLASS: u8 = 0x42;
|
||||||
const ADB_PROTOCOL: u8 = 0x1;
|
const ADB_PROTOCOL: u8 = 0x1;
|
||||||
|
|
||||||
@@ -181,11 +180,6 @@ impl ADBUSBDevice {
|
|||||||
self.get_transport_mut().write_message(message)?;
|
self.get_transport_mut().write_message(message)?;
|
||||||
|
|
||||||
let message = self.get_transport_mut().read_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)?;
|
message.assert_command(MessageCommand::Auth)?;
|
||||||
|
|
||||||
// At this point, we should have receive an AUTH message with arg0 == 1
|
// At this point, we should have receive an AUTH message with arg0 == 1
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ use crate::{
|
|||||||
|
|
||||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
pub(crate) fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
|
pub(crate) fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>> {
|
||||||
self.open_session(b"framebuffer:\0")?;
|
let session = self.open_session(b"framebuffer:\0")?;
|
||||||
|
|
||||||
let response = self.recv_and_reply_okay()?;
|
let response = self.recv_and_reply_okay(session)?;
|
||||||
|
|
||||||
let mut payload_cursor = Cursor::new(response.payload());
|
let mut payload_cursor = Cursor::new(response.payload());
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = self.recv_and_reply_okay()?;
|
let response = self.recv_and_reply_okay(session)?;
|
||||||
|
|
||||||
framebuffer_data.extend_from_slice(&response.into_payload());
|
framebuffer_data.extend_from_slice(&response.into_payload());
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = self.recv_and_reply_okay()?;
|
let response = self.recv_and_reply_okay(session)?;
|
||||||
|
|
||||||
framebuffer_data.extend_from_slice(&response.into_payload());
|
framebuffer_data.extend_from_slice(&response.into_payload());
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,12 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
let file_size = apk_file.metadata()?.len();
|
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 {}\0", file_size).as_bytes())?;
|
||||||
|
|
||||||
let transport = self.get_transport().clone();
|
let transport = self.get_transport().clone();
|
||||||
|
|
||||||
let mut writer = MessageWriter::new(transport, self.get_local_id()?, self.get_remote_id()?);
|
let mut writer = MessageWriter::new(transport, session.local_id, session.remote_id);
|
||||||
|
|
||||||
std::io::copy(&mut apk_file, &mut writer)?;
|
std::io::copy(&mut apk_file, &mut writer)?;
|
||||||
|
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ use crate::{
|
|||||||
|
|
||||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
pub(crate) fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> {
|
pub(crate) fn pull<A: AsRef<str>, W: Write>(&mut self, source: A, output: W) -> Result<()> {
|
||||||
self.begin_synchronization()?;
|
let session = self.begin_synchronization()?;
|
||||||
let source = source.as_ref();
|
let source = source.as_ref();
|
||||||
|
|
||||||
let adb_stat_response = self.stat_with_explicit_ids(source)?;
|
let adb_stat_response = self.stat_with_explicit_ids(session, source)?;
|
||||||
|
|
||||||
if adb_stat_response.file_perm == 0 {
|
if adb_stat_response.file_perm == 0 {
|
||||||
return Err(RustADBError::UnknownResponseType(
|
return Err(RustADBError::UnknownResponseType(
|
||||||
@@ -21,11 +21,13 @@ 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(
|
self.get_transport_mut().write_message_with_timeout(
|
||||||
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]),
|
ADBTransportMessage::new(
|
||||||
|
MessageCommand::Okay,
|
||||||
|
session.local_id,
|
||||||
|
session.remote_id,
|
||||||
|
&[],
|
||||||
|
),
|
||||||
std::time::Duration::from_secs(4),
|
std::time::Duration::from_secs(4),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
@@ -34,19 +36,19 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?;
|
bincode::serialize(&recv_buffer).map_err(|_e| RustADBError::ConversionError)?;
|
||||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
&recv_buffer,
|
&recv_buffer,
|
||||||
))?;
|
))?;
|
||||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
source.as_bytes(),
|
source.as_bytes(),
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
self.recv_file(output)?;
|
self.recv_file(session, output)?;
|
||||||
self.end_transaction()?;
|
self.end_transaction(session)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use crate::{
|
|||||||
|
|
||||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
pub(crate) fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
|
pub(crate) fn push<R: Read, A: AsRef<str>>(&mut self, stream: R, path: A) -> Result<()> {
|
||||||
self.begin_synchronization()?;
|
let session = self.begin_synchronization()?;
|
||||||
|
|
||||||
let path_header = format!("{},0777", path.as_ref());
|
let path_header = format!("{},0777", path.as_ref());
|
||||||
|
|
||||||
@@ -21,14 +21,13 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
self.send_and_expect_okay(ADBTransportMessage::new(
|
self.send_and_expect_okay(ADBTransportMessage::new(
|
||||||
MessageCommand::Write,
|
MessageCommand::Write,
|
||||||
self.get_local_id()?,
|
session.local_id,
|
||||||
self.get_remote_id()?,
|
session.remote_id,
|
||||||
&send_buffer,
|
&send_buffer,
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
self.push_file(self.get_local_id()?, self.get_remote_id()?, stream)?;
|
self.push_file(session, stream)?;
|
||||||
|
self.end_transaction(session)?;
|
||||||
self.end_transaction()?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,14 +10,7 @@ use crate::{
|
|||||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
/// 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.
|
||||||
pub(crate) fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> {
|
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())?;
|
let session = 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 {
|
loop {
|
||||||
let response = self.get_transport_mut().read_message()?;
|
let response = self.get_transport_mut().read_message()?;
|
||||||
@@ -38,21 +31,22 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
mut reader: &mut dyn Read,
|
mut reader: &mut dyn Read,
|
||||||
mut writer: Box<(dyn Write + Send)>,
|
mut writer: Box<(dyn Write + Send)>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.open_session(b"shell:\0")?;
|
let session = self.open_session(b"shell:\0")?;
|
||||||
|
|
||||||
let mut transport = self.get_transport().clone();
|
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
|
// Reading thread, reads response from adbd
|
||||||
std::thread::spawn(move || -> Result<()> {
|
std::thread::spawn(move || -> Result<()> {
|
||||||
loop {
|
loop {
|
||||||
let message = transport.read_message()?;
|
let message = transport.read_message()?;
|
||||||
|
|
||||||
// Acknowledge for more data
|
// Acknowledge for more data
|
||||||
let response =
|
let response = ADBTransportMessage::new(
|
||||||
ADBTransportMessage::new(MessageCommand::Okay, local_id, remote_id, &[]);
|
MessageCommand::Okay,
|
||||||
|
session.local_id,
|
||||||
|
session.remote_id,
|
||||||
|
&[],
|
||||||
|
);
|
||||||
transport.write_message(response)?;
|
transport.write_message(response)?;
|
||||||
|
|
||||||
match message.header().command() {
|
match message.header().command() {
|
||||||
@@ -67,7 +61,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let transport = self.get_transport().clone();
|
let transport = self.get_transport().clone();
|
||||||
let mut shell_writer = ShellMessageWriter::new(transport, local_id, remote_id);
|
let mut shell_writer =
|
||||||
|
ShellMessageWriter::new(transport, session.local_id, session.remote_id);
|
||||||
|
|
||||||
// Read from given reader (that could be stdin e.g), and write content to device adbd
|
// 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) {
|
if let Err(e) = std::io::copy(&mut reader, &mut shell_writer) {
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ use crate::{
|
|||||||
|
|
||||||
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
pub(crate) fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
pub(crate) fn stat(&mut self, remote_path: &str) -> Result<AdbStatResponse> {
|
||||||
self.begin_synchronization()?;
|
let session = self.begin_synchronization()?;
|
||||||
let adb_stat_response = self.stat_with_explicit_ids(remote_path)?;
|
let adb_stat_response = self.stat_with_explicit_ids(session, remote_path)?;
|
||||||
self.end_transaction()?;
|
self.end_transaction(session)?;
|
||||||
Ok(adb_stat_response)
|
Ok(adb_stat_response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ impl<T: ADBMessageTransport> Write for MessageWriter<T> {
|
|||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
response
|
response
|
||||||
.assert_command(MessageCommand::Okay)
|
.assert_command(MessageCommand::Okay)
|
||||||
.map_err(Error::other)?;
|
.map_err(|e| Error::new(ErrorKind::Other, e))?;
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
Err(e) => Err(Error::other(e)),
|
Err(e) => Err(Error::new(ErrorKind::Other, e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,7 @@ mod shell_message_writer;
|
|||||||
use adb_message_device::ADBMessageDevice;
|
use adb_message_device::ADBMessageDevice;
|
||||||
pub use adb_tcp_device::ADBTcpDevice;
|
pub use adb_tcp_device::ADBTcpDevice;
|
||||||
pub use adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader};
|
pub use adb_transport_message::{ADBTransportMessage, ADBTransportMessageHeader};
|
||||||
pub use adb_usb_device::{
|
pub use adb_usb_device::{ADBUSBDevice, get_default_adb_key_path};
|
||||||
ADBUSBDevice, get_default_adb_key_path, is_adb_device, search_adb_devices,
|
|
||||||
};
|
|
||||||
pub use message_writer::MessageWriter;
|
pub use message_writer::MessageWriter;
|
||||||
pub use models::{ADBRsaKey, MessageCommand, MessageSubcommand};
|
pub use models::{ADBRsaKey, MessageCommand, MessageSubcommand};
|
||||||
pub use shell_message_writer::ShellMessageWriter;
|
pub use shell_message_writer::ShellMessageWriter;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ mod transports;
|
|||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
pub use adb_device_ext::ADBDeviceExt;
|
pub use adb_device_ext::ADBDeviceExt;
|
||||||
pub use device::{ADBTcpDevice, ADBUSBDevice, is_adb_device, search_adb_devices};
|
pub use device::{ADBTcpDevice, ADBUSBDevice};
|
||||||
pub use emulator_device::ADBEmulatorDevice;
|
pub use emulator_device::ADBEmulatorDevice;
|
||||||
pub use error::{Result, RustADBError};
|
pub use error::{Result, RustADBError};
|
||||||
pub use mdns::*;
|
pub use mdns::*;
|
||||||
|
|||||||
@@ -42,15 +42,18 @@ impl<R: Read> Read for ADBRecvCommandReader<R> {
|
|||||||
let mut error_msg = vec![0; length];
|
let mut error_msg = vec![0; length];
|
||||||
self.inner.read_exact(&mut error_msg)?;
|
self.inner.read_exact(&mut error_msg)?;
|
||||||
|
|
||||||
Err(std::io::Error::other(format!(
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
format!(
|
||||||
"ADB request failed: {}",
|
"ADB request failed: {}",
|
||||||
String::from_utf8_lossy(&error_msg)
|
String::from_utf8_lossy(&error_msg)
|
||||||
)))
|
),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
_ => Err(std::io::Error::other(format!(
|
_ => Err(std::io::Error::new(
|
||||||
"Unknown response from device {:#?}",
|
std::io::ErrorKind::Other,
|
||||||
header
|
format!("Unknown response from device {:#?}", header),
|
||||||
))),
|
)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Computing minimum to ensure to stop reading before next header...
|
// Computing minimum to ensure to stop reading before next header...
|
||||||
|
|||||||
Reference in New Issue
Block a user