-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add install command (tcp + usb) (#56)
- Loading branch information
Showing
14 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::{fs::File, io::Read, path::Path}; | ||
|
||
use crate::{models::AdbServerCommand, utils::check_extension_is_apk, ADBServerDevice, Result}; | ||
|
||
impl ADBServerDevice { | ||
/// Install an APK on device | ||
pub fn install<P: AsRef<Path>>(&mut self, apk_path: P) -> Result<()> { | ||
let mut apk_file = File::open(&apk_path)?; | ||
|
||
check_extension_is_apk(&apk_path)?; | ||
|
||
let file_size = apk_file.metadata()?.len(); | ||
|
||
let serial: String = self.identifier.clone(); | ||
self.connect()? | ||
.send_adb_request(AdbServerCommand::TransportSerial(serial))?; | ||
|
||
self.get_transport_mut() | ||
.send_adb_request(AdbServerCommand::Install(file_size))?; | ||
|
||
let mut raw_connection = self.get_transport_mut().get_raw_connection()?; | ||
|
||
std::io::copy(&mut apk_file, &mut raw_connection)?; | ||
|
||
let mut data = [0; 1024]; | ||
let read_amount = self.get_transport().get_raw_connection()?.read(&mut data)?; | ||
|
||
match &data[0..read_amount] { | ||
b"Success\n" => { | ||
log::info!( | ||
"APK file {} successfully installed", | ||
apk_path.as_ref().display() | ||
); | ||
Ok(()) | ||
} | ||
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8( | ||
d.to_vec(), | ||
)?)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod forward; | ||
mod framebuffer; | ||
mod host_features; | ||
mod install; | ||
mod list; | ||
mod logcat; | ||
mod reboot; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::io::{ErrorKind, Write}; | ||
|
||
use crate::USBTransport; | ||
|
||
use super::{ADBUsbMessage, USBCommand}; | ||
|
||
/// Wraps a `Writer` to hide underlying ADB protocol write logic. | ||
/// | ||
/// Read received responses to check that message has been received. | ||
pub struct USBWriter { | ||
transport: USBTransport, | ||
local_id: u32, | ||
remote_id: u32, | ||
} | ||
|
||
impl USBWriter { | ||
pub fn new(transport: USBTransport, local_id: u32, remote_id: u32) -> Self { | ||
Self { | ||
transport, | ||
local_id, | ||
remote_id, | ||
} | ||
} | ||
} | ||
|
||
impl Write for USBWriter { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
let message = ADBUsbMessage::new( | ||
USBCommand::Write, | ||
self.local_id, | ||
self.remote_id, | ||
buf.to_vec(), | ||
); | ||
self.transport | ||
.write_message(message) | ||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; | ||
|
||
match self.transport.read_message() { | ||
Ok(response) => match response.header().command() { | ||
USBCommand::Okay => Ok(buf.len()), | ||
c => Err(std::io::Error::new( | ||
ErrorKind::Other, | ||
format!("wrong response received: {c}"), | ||
)), | ||
}, | ||
Err(e) => Err(std::io::Error::new(ErrorKind::Other, e)), | ||
} | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters