feat: add disconnect + rework with new archi
This commit is contained in:
@@ -3,18 +3,14 @@ use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Write};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::net::SocketAddrV4;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(about, version, author)]
|
||||
pub struct Args {
|
||||
/// Sets the listening address of ADB server
|
||||
#[clap(short = 'a', long = "address", default_value = "127.0.0.1")]
|
||||
pub address: Ipv4Addr,
|
||||
/// Sets the listening port of ADB server
|
||||
#[clap(short = 'p', long = "port", default_value = "5037")]
|
||||
pub port: u16,
|
||||
#[clap(short = 'a', long = "address", default_value = "127.0.0.1:5037")]
|
||||
pub address: SocketAddrV4,
|
||||
/// Serial id of a specific device. Every request will be sent to this device.
|
||||
#[clap(short = 's', long = "serial")]
|
||||
pub serial: Option<String>,
|
||||
@@ -64,14 +60,12 @@ pub enum HostCommand {
|
||||
},
|
||||
/// Track new devices showing up.
|
||||
TrackDevices,
|
||||
/// Pair new device on a specific port with a given code
|
||||
Pair {
|
||||
address: Ipv4Addr,
|
||||
port: u16,
|
||||
code: u32,
|
||||
},
|
||||
/// Pair device with a given code
|
||||
Pair { address: SocketAddrV4, code: u32 },
|
||||
/// Connect device over WI-FI
|
||||
Connect { address: Ipv4Addr, port: u16 },
|
||||
Connect { address: SocketAddrV4 },
|
||||
/// Disconnect device over WI-FI
|
||||
Disconnect { address: SocketAddrV4 },
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
@@ -98,7 +92,7 @@ impl From<RebootTypeCommand> for RebootType {
|
||||
fn main() -> Result<()> {
|
||||
let opt = Args::parse();
|
||||
|
||||
let mut adb_server = ADBServer::new(opt.address, opt.port);
|
||||
let mut adb_server = ADBServer::new(opt.address);
|
||||
|
||||
match opt.command {
|
||||
Command::LocalCommand(local) => {
|
||||
@@ -171,17 +165,17 @@ fn main() -> Result<()> {
|
||||
println!("Live list of devices attached");
|
||||
adb_server.track_devices(callback)?;
|
||||
}
|
||||
HostCommand::Pair {
|
||||
address,
|
||||
port,
|
||||
code,
|
||||
} => {
|
||||
println!("Pairing device");
|
||||
adb_server.pair(address, port, code)?
|
||||
HostCommand::Pair { address, code } => {
|
||||
adb_server.pair(address, code)?;
|
||||
println!("paired device {address}");
|
||||
}
|
||||
HostCommand::Connect { address, port } => {
|
||||
println!("Connect device");
|
||||
adb_server.connect_device(address, port)?
|
||||
HostCommand::Connect { address } => {
|
||||
adb_server.connect_device(address)?;
|
||||
println!("connected to {address}");
|
||||
}
|
||||
HostCommand::Disconnect { address } => {
|
||||
adb_server.disconnect_device(address)?;
|
||||
println!("disconnected {address}");
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use std::net::Ipv4Addr;
|
||||
use super::RebootType;
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
pub(crate) enum AdbCommand {
|
||||
Version,
|
||||
@@ -10,8 +10,9 @@ pub(crate) enum AdbCommand {
|
||||
DevicesLong,
|
||||
TrackDevices,
|
||||
HostFeatures,
|
||||
Connect(Ipv4Addr, u16),
|
||||
Pair(Ipv4Addr, u16, u32),
|
||||
Connect(SocketAddrV4),
|
||||
Disconnect(SocketAddrV4),
|
||||
Pair(SocketAddrV4, u32),
|
||||
// TODO: NOT IMPLEMENTED YET
|
||||
// Emulator(u16),
|
||||
// Transport(String),
|
||||
@@ -72,9 +73,12 @@ impl Display for AdbCommand {
|
||||
AdbCommand::HostFeatures => write!(f, "host:features"),
|
||||
AdbCommand::Reboot(reboot_type) => {
|
||||
write!(f, "reboot:{reboot_type}")
|
||||
},
|
||||
AdbCommand::Connect(addr, port) => write!(f, "host:connect:{addr}:{port}"),
|
||||
AdbCommand::Pair(addr, port, code) => write!(f, "host:pair:{code}:{addr}:{port}")
|
||||
}
|
||||
AdbCommand::Connect(addr) => write!(f, "host:connect:{}", addr),
|
||||
AdbCommand::Disconnect(addr) => write!(f, "host:disconnect:{}", addr),
|
||||
AdbCommand::Pair(addr, code) => {
|
||||
write!(f, "host:pair:{code}:{}", addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ use std::str::FromStr;
|
||||
use std::{fmt::Display, str};
|
||||
|
||||
use crate::{DeviceState, RustADBError};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::bytes::Regex;
|
||||
|
||||
lazy_static! {
|
||||
static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P<identifier>\\S+)\\s+(?P<state>\\w+) (usb:(?P<usb>.*) )?(product:(?P<product>\\w+) model:(?P<model>\\w+) device:(?P<device>\\w+) )?transport_id:(?P<transport_id>\\d+)$").unwrap();
|
||||
static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P<identifier>\\S+)\\s+(?P<state>\\w+) ((usb:(?P<usb1>.*)|(?P<usb2>\\d-\\d)) )?(product:(?P<product>\\w+) model:(?P<model>\\w+) device:(?P<device>\\w+) )?transport_id:(?P<transport_id>\\d+)$").expect("cannot build devices long regex");
|
||||
|
||||
}
|
||||
|
||||
/// Represents a new device with more informations helded.
|
||||
@@ -45,10 +45,6 @@ impl Display for DeviceLong {
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref DEVICES_LONG_REGEX: Regex = Regex::new("^(?P<identifier>\\S+)\\s+(?P<state>\\w+) ((usb:(?P<usb1>.*)|(?P<usb2>\\d-\\d)) )?(product:(?P<product>\\w+) model:(?P<model>\\w+) device:(?P<device>\\w+) )?transport_id:(?P<transport_id>\\d+)$").expect("cannot build devices long regex");
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for DeviceLong {
|
||||
type Error = RustADBError;
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ use crate::Result;
|
||||
use crate::RustADBError;
|
||||
use crate::TCPServerProtocol;
|
||||
use crate::Transport;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
/// Represents an ADB Server
|
||||
@@ -16,10 +15,10 @@ pub struct ADBServer {
|
||||
|
||||
impl ADBServer {
|
||||
/// Instantiates a new [ADBServer]
|
||||
pub fn new(ip: Ipv4Addr, port: u16) -> Self {
|
||||
pub fn new(address: SocketAddrV4) -> Self {
|
||||
Self {
|
||||
transport: None,
|
||||
socket_addr: Some(SocketAddrV4::new(ip, port)),
|
||||
socket_addr: Some(address),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
use crate::{models::AdbCommand, ADBServer, Result, RustADBError};
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::{models::AdbCommand, ADBServer, Result, RustADBError};
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
impl ADBServer {
|
||||
/// Connect device over tcp with address and port
|
||||
pub fn connect_device(&mut self, address: Ipv4Addr, port: u16) -> Result<()> {
|
||||
let response = self.connect()?.proxy_connection(AdbCommand::Connect(address, port), true)?;
|
||||
pub fn connect_device(&mut self, address: SocketAddrV4) -> Result<()> {
|
||||
let response = self
|
||||
.connect()?
|
||||
.proxy_connection(AdbCommand::Connect(address), true)?;
|
||||
|
||||
match String::from_utf8(response).unwrap() {
|
||||
s if s.starts_with("connected to") => Ok(()),
|
||||
s => Err(RustADBError::ADBRequestFailed(s))
|
||||
s => Err(RustADBError::ADBRequestFailed(s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
src/server/server_commands/disconnect.rs
Normal file
16
src/server/server_commands/disconnect.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
use crate::{models::AdbCommand, ADBServer, Result, RustADBError};
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
impl ADBServer {
|
||||
/// Connect device over tcp with address and port
|
||||
pub fn disconnect_device(&mut self, address: SocketAddrV4) -> Result<()> {
|
||||
let response = self
|
||||
.connect()?
|
||||
.proxy_connection(AdbCommand::Disconnect(address), true)?;
|
||||
|
||||
match String::from_utf8(response).unwrap() {
|
||||
s if s.starts_with("disconnected") => Ok(()),
|
||||
s => Err(RustADBError::ADBRequestFailed(s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
mod connect;
|
||||
mod devices;
|
||||
mod disconnect;
|
||||
mod kill;
|
||||
mod pair;
|
||||
mod version;
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
use std::net::Ipv4Addr;
|
||||
use crate::{ADBServer, Result, RustADBError};
|
||||
use crate::models::AdbCommand;
|
||||
use crate::{ADBServer, Result, RustADBError};
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
impl ADBServer {
|
||||
/// Pair device on a specific port with a generated 'code'
|
||||
pub fn pair(&mut self, address: Ipv4Addr, port: u16, code: u32) -> Result<()> {
|
||||
let response = self.connect()?.proxy_connection(AdbCommand::Pair(address, port, code), true)?;
|
||||
pub fn pair(&mut self, address: SocketAddrV4, code: u32) -> Result<()> {
|
||||
let response = self
|
||||
.connect()?
|
||||
.proxy_connection(AdbCommand::Pair(address, code), true)?;
|
||||
|
||||
match String::from_utf8(response).unwrap() {
|
||||
s if s.starts_with("Successfully paired to") => Ok(()),
|
||||
s => Err(RustADBError::ADBRequestFailed(s))
|
||||
s => Err(RustADBError::ADBRequestFailed(s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::str::FromStr;
|
||||
|
||||
use adb_client::{ADBServer, ADBServerDevice, DeviceLong};
|
||||
use rand::Rng;
|
||||
@@ -51,13 +49,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_wrong_addr() {
|
||||
let address = Ipv4Addr::from_str("127.0.0.300").unwrap();
|
||||
let _ = ADBServer::new(address, 5037);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_recv() {
|
||||
// Create random "Reader" in memory
|
||||
|
||||
Reference in New Issue
Block a user