adb_utils/commands/scripting/
restart.rs

1use std::{fmt::Display, process::Command};
2
3use crate::{ADBCommand, ADBResult};
4
5/// Reboot the device, defaults to system image
6pub struct ADBReboot {
7    shell: Command,
8}
9
10impl ADBReboot {
11    pub fn new() -> Self {
12        let mut cmd = Command::new("adb");
13        cmd.arg("reboot");
14
15        ADBReboot { shell: cmd }
16    }
17
18    /// Reboot the device to this point
19    pub fn to(mut self, method: RebootMethod) -> Self {
20        self.shell.arg(method.to_string());
21        self
22    }
23}
24
25impl Default for ADBReboot {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl ADBCommand for ADBReboot {
32    fn build(&mut self) -> Result<&mut Command, String> {
33        Ok(&mut self.shell)
34    }
35
36    fn process_output(&self, output: ADBResult) -> ADBResult {
37        output
38    }
39}
40
41pub enum RebootMethod {
42    Bootloader,
43    Recovery,
44    Sideload,
45    SideloadAutoReboot,
46}
47
48impl Display for RebootMethod {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(
51            f,
52            "{}",
53            match self {
54                RebootMethod::Bootloader => "bootloader",
55                RebootMethod::Recovery => "recovery",
56                RebootMethod::Sideload => "sideload",
57                RebootMethod::SideloadAutoReboot => "sideload-auto-reboot",
58            }
59        )
60    }
61}
62
63/// Options for the ADB daemon
64pub struct ADBDaemon {
65    shell: Command,
66}
67
68impl ADBDaemon {
69    /// Restart adbd with root permissions
70    pub fn root() -> Self {
71        let mut cmd = Command::new("adb");
72        cmd.arg("root");
73
74        ADBDaemon { shell: cmd }
75    }
76
77    /// Restart adbd without root permissions
78    pub fn unroot() -> Self {
79        let mut cmd = Command::new("adb");
80        cmd.arg("unroot");
81
82        ADBDaemon { shell: cmd }
83    }
84
85    /// Restart adbd listening on USB
86    pub fn usb() -> Self {
87        let mut cmd = Command::new("adb");
88        cmd.arg("usb");
89
90        ADBDaemon { shell: cmd }
91    }
92
93    /// Restart adbd listening on TCP on port
94    pub fn tcpip(port: u32) -> Self {
95        let mut cmd = Command::new("adb");
96        cmd.arg("tcp").arg(port.to_string());
97
98        ADBDaemon { shell: cmd }
99    }
100}
101
102impl ADBCommand for ADBDaemon {
103    fn build(&mut self) -> Result<&mut Command, String> {
104        Ok(&mut self.shell)
105    }
106
107    fn process_output(&self, output: ADBResult) -> ADBResult {
108        output
109    }
110}