Skip to content

Commit

Permalink
fix: minor refactoring / changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cocool97 committed Nov 14, 2024
1 parent 7fcd6ca commit e0ea7e4
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 131 deletions.
2 changes: 1 addition & 1 deletion adb_cli/src/commands/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ pub enum HostCommand {
Connect { address: SocketAddrV4 },
/// Disconnect device over WI-FI
Disconnect { address: SocketAddrV4 },
// Server status
/// Display server status
ServerStatus,
}
4 changes: 2 additions & 2 deletions adb_client/src/models/adb_server_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) enum AdbServerCommand {
ReverseRemoveAll,
Reconnect,
TcpIp(u16),
USB,
Usb,
}

impl Display for AdbServerCommand {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Display for AdbServerCommand {
AdbServerCommand::TcpIp(port) => {
write!(f, "tcpip:{port}")
}
AdbServerCommand::USB => write!(f, "usb:"),
AdbServerCommand::Usb => write!(f, "usb:"),
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions adb_client/src/models/mdns_services.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use lazy_static::lazy_static;
use regex::bytes::Regex;
use std::{fmt::Display, str::FromStr};
use std::net::SocketAddrV4;
use std::{fmt::Display, str::FromStr};

use crate::RustADBError;

lazy_static! {
static ref MDNS_SERVICES_REGEX: Regex =
Regex::new("^(\\S+)\t(\\S+)\t([\\d\\.]+:\\d+)\n?$").expect("Cannot build mdns services regex");
static ref MDNS_SERVICES_REGEX: Regex = Regex::new("^(\\S+)\t(\\S+)\t([\\d\\.]+:\\d+)\n?$")
.expect("Cannot build mdns services regex");
}

/// Represents MDNS Services
Expand All @@ -23,16 +23,20 @@ pub struct MDNSServices {

impl Display for MDNSServices {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}\t{}\t{}", self.service_name, self.reg_type, self.socket_v4)
write!(
f,
"{}\t{}\t{}",
self.service_name, self.reg_type, self.socket_v4
)
}
}

impl TryFrom<Vec<u8>> for MDNSServices {
impl TryFrom<&[u8]> for MDNSServices {
type Error = RustADBError;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let groups = MDNS_SERVICES_REGEX
.captures(&value)
.captures(value)
.ok_or(RustADBError::RegexParsingError)?;
Ok(MDNSServices {
service_name: String::from_utf8(
Expand All @@ -58,4 +62,4 @@ impl TryFrom<Vec<u8>> for MDNSServices {
)?)?,
})
}
}
}
10 changes: 5 additions & 5 deletions adb_client/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ mod device_long;
mod device_short;
mod device_state;
mod host_features;
mod reboot_type;
mod sync_command;
mod mdns_services;
mod reboot_type;
mod server_status;
mod sync_command;

pub(crate) use adb_emulator_command::ADBEmulatorCommand;
pub use adb_request_status::AdbRequestStatus;
Expand All @@ -21,8 +21,8 @@ pub use device_long::DeviceLong;
pub use device_short::DeviceShort;
pub use device_state::DeviceState;
pub use host_features::HostFeatures;
pub use reboot_type::RebootType;
pub use sync_command::SyncCommand;
pub use mdns_services::MDNSServices;
pub use server_status::ServerStatus;
pub use reboot_type::RebootType;
pub use server_status::MDNSBackend;
pub use server_status::ServerStatus;
pub use sync_command::SyncCommand;
93 changes: 36 additions & 57 deletions adb_client/src/models/server_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,95 @@ use std::fmt::Display;

use crate::RustADBError;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum UsbBackend {
UNKNOWN,
NATIVE,
LIBUSB,
#[default]
Unknown,
Native,
LibUSB,
}

impl Display for UsbBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UsbBackend::UNKNOWN => write!(f, "UNKNOWN"),
UsbBackend::NATIVE => write!(f, "NATIVE"),
UsbBackend::LIBUSB => write!(f, "LIBUSB"),
UsbBackend::Unknown => write!(f, "UNKNOWN"),
UsbBackend::Native => write!(f, "NATIVE"),
UsbBackend::LibUSB => write!(f, "LIBUSB"),
}
}
}

impl From<i32> for UsbBackend {
fn from(value: i32) -> UsbBackend {
match value {
1 => UsbBackend::NATIVE,
2 => UsbBackend::LIBUSB,
_ => UsbBackend::UNKNOWN,
1 => UsbBackend::Native,
2 => UsbBackend::LibUSB,
_ => UsbBackend::Unknown,
}
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum MDNSBackend {
UNKNOWN,
BONJOUR,
OPENSCREEN,
#[default]
Unknown,
Bonjour,
OpenScreen,
}

impl Display for MDNSBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MDNSBackend::UNKNOWN => write!(f, "UNKNOWN"),
MDNSBackend::BONJOUR => write!(f, "BONJOUR"),
MDNSBackend::OPENSCREEN => write!(f, "OPENSCREEN"),
MDNSBackend::Unknown => write!(f, "UNKNOWN"),
MDNSBackend::Bonjour => write!(f, "BONJOUR"),
MDNSBackend::OpenScreen => write!(f, "OPENSCREEN"),
}
}
}

impl From<i32> for MDNSBackend {
fn from(value: i32) -> MDNSBackend {
match value {
1 => MDNSBackend::BONJOUR,
2 => MDNSBackend::OPENSCREEN,
_ => MDNSBackend::UNKNOWN,
1 => MDNSBackend::Bonjour,
2 => MDNSBackend::OpenScreen,
_ => MDNSBackend::Unknown,
}
}
}

/// Represents Server Status
#[derive(Debug, Clone)]
/// Structure representing current server status
#[derive(Debug, Clone, Default)]
pub struct ServerStatus {
/// usb backend
pub usb_backend: UsbBackend,

pub usb_backend_forced: bool,

pub mdns_backend: MDNSBackend,

pub mdns_backend_forced: bool,

pub version: String,

pub build: String,

pub executable_absolute_path: String,

pub log_absolute_path: String,

pub os: String,
}

impl Display for ServerStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "usb_backend: {}\n", self.usb_backend)?;
if self.usb_backend_forced {
write!(f, "usb_backend_forced: {}\n", self.usb_backend_forced)?;
}
write!(f, "mdns_backend: {}\n", self.mdns_backend)?;
if self.mdns_backend_forced {
write!(f, "mdns_backend_forced: {}\n", self.mdns_backend_forced)?;
}
write!(f, "version: \"{}\"\n", self.version)?;
write!(f, "build: \"{}\"\n", self.build)?;
write!(
writeln!(f, "usb_backend: {}", self.usb_backend)?;
writeln!(f, "usb_backend_forced: {}", self.usb_backend_forced)?;
writeln!(f, "mdns_backend: {}", self.mdns_backend)?;
writeln!(f, "mdns_backend_forced: {}", self.mdns_backend_forced)?;
writeln!(f, "version: \"{}\"", self.version)?;
writeln!(f, "build: \"{}\"", self.build)?;
writeln!(
f,
"executable_absolute_path: \"{}\"\n",
"executable_absolute_path: \"{}\"",
self.executable_absolute_path
)?;
write!(f, "log_absolute_path: \"{}\"\n", self.log_absolute_path)?;
write!(f, "os: \"{}\"\n", self.os)
writeln!(f, "log_absolute_path: \"{}\"", self.log_absolute_path)?;
writeln!(f, "os: \"{}\"", self.os)
}
}

fn parse_tag(cursor: &mut &[u8]) -> Result<(u8, u8), RustADBError>{
fn parse_tag(cursor: &mut &[u8]) -> Result<(u8, u8), RustADBError> {
if cursor.is_empty() {
return Err(RustADBError::ConversionError);
}
Expand Down Expand Up @@ -151,17 +140,7 @@ impl TryFrom<Vec<u8>> for ServerStatus {
type Error = RustADBError;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let mut server_status = ServerStatus {
usb_backend: UsbBackend::UNKNOWN,
usb_backend_forced: false,
mdns_backend: MDNSBackend::UNKNOWN,
mdns_backend_forced: false,
version: "".to_string(),
build: "".to_string(),
executable_absolute_path: "".to_string(),
log_absolute_path: "".to_string(),
os: "".to_string(),
};
let mut server_status = ServerStatus::default();
let mut cursor = &value[..];
while !cursor.is_empty() {
let (field_number, wire_type) = parse_tag(&mut cursor)?;
Expand Down
45 changes: 22 additions & 23 deletions adb_client/src/server/adb_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ impl ADBServer {

/// Connect to underlying transport
pub(crate) fn connect(&mut self) -> Result<&mut TCPServerTransport> {
self.connect_with_envs(None)
}

pub(crate) fn connect_with_envs(
&mut self,
envs: Option<HashMap<String, String>>,
) -> Result<&mut TCPServerTransport> {
let mut is_local_ip = false;
let mut transport = if let Some(addr) = &self.socket_addr {
let ip = addr.ip();
Expand All @@ -57,29 +50,35 @@ impl ADBServer {

if is_local_ip {
// ADB Server is local, we start it if not already running
let mut command = Command::new("adb");
command.arg("start-server");
if let Some(envs) = envs {
for (env_k, env_v) in envs {
command.env(env_k, env_v);
}
}
let child = command.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}"),
}
self.start_server(HashMap::default())?;
}

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

self.get_transport()
}

/// Start a new instance of adb-server using given environment variables
pub fn start_server(&self, envs: HashMap<String, String>) -> Result<()> {
let mut command = Command::new("adb");
command.arg("start-server");
for (env_k, env_v) in envs {
command.env(env_k, env_v);
}

let child = command.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}"),
}

Ok(())
}
}

impl Drop for ADBServer {
Expand Down
2 changes: 1 addition & 1 deletion adb_client/src/server/device_commands/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl ADBServerDevice {
.map(|_| ())
}

/// remove all forward rules
/// Remove all previously applied forward rules
pub fn forward_remove_all(&mut self) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
Expand Down
4 changes: 2 additions & 2 deletions adb_client/src/server/device_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ mod host_features;
mod list;
mod logcat;
mod reboot;
mod reconnect;
mod recv;
mod reverse;
mod send;
mod stat;
mod transport;
mod tcpip;
mod transport;
mod usb;
mod reconnect;
7 changes: 2 additions & 5 deletions adb_client/src/server/device_commands/reconnect.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
models::AdbServerCommand,
ADBServerDevice, Result,
};
use crate::{models::AdbServerCommand, ADBServerDevice, Result};

impl ADBServerDevice {
/// reconnect device
/// Reconnect device
pub fn reconnect(&mut self) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
Expand Down
2 changes: 1 addition & 1 deletion adb_client/src/server/device_commands/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl ADBServerDevice {
.map(|_| ())
}

/// remove all reverse rules
/// Remove all reverse rules
pub fn reverse_remove_all(&mut self) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
Expand Down
7 changes: 2 additions & 5 deletions adb_client/src/server/device_commands/tcpip.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
models::AdbServerCommand,
ADBServerDevice, Result,
};
use crate::{models::AdbServerCommand, ADBServerDevice, Result};

impl ADBServerDevice {
/// Set adb daemon to tcpip mode
/// Set adb daemon to tcp/ip mode
pub fn tcpip(&mut self, port: u16) -> Result<()> {
let serial = self.identifier.clone();
self.connect()?
Expand Down
Loading

0 comments on commit e0ea7e4

Please sign in to comment.