adb_client/server/
adb_server.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::ADBTransport;
use crate::Result;
use crate::RustADBError;
use crate::TCPServerTransport;
use std::net::SocketAddrV4;
use std::process::Command;

/// Represents an ADB Server
#[derive(Debug, Default)]
pub struct ADBServer {
    /// Internal [TcpStream], lazily initialized
    pub(crate) transport: Option<TCPServerTransport>,
    /// Address to connect to
    pub(crate) socket_addr: Option<SocketAddrV4>,
}

impl ADBServer {
    /// Instantiates a new [ADBServer]
    pub fn new(address: SocketAddrV4) -> Self {
        Self {
            transport: None,
            socket_addr: Some(address),
        }
    }

    /// Returns the current selected transport
    pub(crate) fn get_transport(&mut self) -> Result<&mut TCPServerTransport> {
        self.transport
            .as_mut()
            .ok_or(RustADBError::IOError(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "server connection not initialized",
            )))
    }

    /// Connect to underlying transport
    pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> {
        let mut is_local_ip = false;
        let mut transport = if let Some(addr) = &self.socket_addr {
            let ip = addr.ip();
            if ip.is_loopback() || ip.is_unspecified() {
                is_local_ip = true;
            }
            TCPServerTransport::new(*addr)
        } else {
            is_local_ip = true;
            TCPServerTransport::default()
        };

        if is_local_ip {
            // ADB Server is local, we start it if not already running
            let child = Command::new("adb").arg("start-server").spawn();
            match child {
                Ok(mut child) => {
                    if let Err(e) = child.wait() {
                        log::error!("error while starting adb server: {e}")
                    }
                }
                Err(e) => log::error!("error while starting adb server: {e}"),
            }
        }

        transport.connect()?;
        self.transport = Some(transport);

        self.get_transport()
    }
}

impl Drop for ADBServer {
    fn drop(&mut self) {
        if let Some(ref mut transport) = &mut self.transport {
            let _ = transport.disconnect();
        }
    }
}