Skip to content

Commit

Permalink
feat: add reboot to ADBDeviceExt
Browse files Browse the repository at this point in the history
  • Loading branch information
cocool97 committed Oct 25, 2024
1 parent 8d608f6 commit 9c3ad8c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion adb_cli/src/commands/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum LocalCommand {
/// Reboot the device
Reboot {
#[clap(subcommand)]
sub_command: RebootTypeCommand,
reboot_type: RebootTypeCommand,
},
/// Dump framebuffer of device
Framebuffer { path: String },
Expand Down
7 changes: 7 additions & 0 deletions adb_cli/src/commands/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::PathBuf;

use clap::Parser;

use crate::models::RebootTypeCommand;

fn parse_hex_id(id: &str) -> Result<u16, ParseIntError> {
u16::from_str_radix(id, 16)
}
Expand All @@ -26,4 +28,9 @@ pub struct UsbCommand {
pub enum UsbCommands {
/// Spawn an interactive shell or run a list of commands on the device
Shell { commands: Vec<String> },
/// Reboot the device
Reboot {
#[clap(subcommand)]
reboot_type: RebootTypeCommand,
},
}
10 changes: 7 additions & 3 deletions adb_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ fn main() -> Result<()> {
.ok_or(anyhow!("cannot list features"))?;
log::info!("Available host features: {features}");
}
LocalCommand::Reboot { sub_command } => {
log::info!("Reboots device");
device.reboot(sub_command.into())?
LocalCommand::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
LocalCommand::Framebuffer { path } => {
device.framebuffer(&path)?;
Expand Down Expand Up @@ -181,6 +181,10 @@ fn main() -> Result<()> {
device.shell_command(commands, std::io::stdout())?;
}
}
UsbCommands::Reboot { reboot_type } => {
log::info!("Reboots device in mode {:?}", reboot_type);
device.reboot(reboot_type.into())?
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion adb_client/src/adb_device_ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{Read, Write};

use crate::Result;
use crate::{RebootType, Result};

/// Trait representing all features available on both [`ADBServerDevice`] and [`ADBUSBDevice`]
pub trait ADBDeviceExt {
Expand All @@ -15,4 +15,7 @@ pub trait ADBDeviceExt {
/// Input data is read from [reader] and write to [writer].
/// [W] has a 'static bound as it is internally used in a thread.
fn shell<R: Read, W: Write + Send + 'static>(&mut self, reader: R, writer: W) -> Result<()>;

/// Reboots the device using given reboot type
fn reboot(&mut self, reboot_type: RebootType) -> Result<()>;
}
4 changes: 4 additions & 0 deletions adb_client/src/server/adb_server_device_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ impl ADBDeviceExt for ADBServerDevice {

Ok(())
}

fn reboot(&mut self, reboot_type: crate::RebootType) -> Result<()> {
self.reboot(reboot_type)
}
}
22 changes: 21 additions & 1 deletion adb_client/src/usb/adb_usb_device_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand::Rng;

use crate::{
usb::{ADBUsbMessage, USBCommand},
ADBDeviceExt, ADBUSBDevice, Result, RustADBError,
ADBDeviceExt, ADBUSBDevice, RebootType, Result, RustADBError,
};

use super::USBShellWriter;
Expand Down Expand Up @@ -112,4 +112,24 @@ impl ADBDeviceExt for ADBUSBDevice {

Ok(())
}

fn reboot(&mut self, reboot_type: RebootType) -> Result<()> {
let mut rng = rand::thread_rng();

let message = ADBUsbMessage::new(
USBCommand::Open,
rng.gen(), // Our 'local-id'
0,
format!("reboot:{}\0", reboot_type).as_bytes().to_vec(),
);
self.transport.write_message(message)?;

let message = self.transport.read_message()?;

if message.header().command() != USBCommand::Okay {
return Err(RustADBError::ADBShellNotSupported);
}

Ok(())
}
}

0 comments on commit 9c3ad8c

Please sign in to comment.