Skip to content

Commit

Permalink
feat(device): Add a getter for the device type
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Oct 24, 2023
1 parent fd259c6 commit 30c6b5f
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions frida/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl<'a> Device<'a> {
id.to_str().unwrap_or_default()
}

/// Returns the device's type
///
/// # Example
/// ```
///# use frida::DeviceType;
///# let frida = unsafe { frida::Frida::obtain() };
///# let device_manager = frida::DeviceManager::obtain(&frida);
///# let device = device_manager.enumerate_all_devices().into_iter().find(|device| device.get_id() == "local").unwrap();
/// assert_eq!(device.get_type(), DeviceType::Local);
/// ```
pub fn get_type(&self) -> DeviceType {
unsafe { frida_sys::frida_device_get_dtype(self.device_ptr).into() }
}

/// Returns the device's system parameters
///
/// # Example
Expand Down Expand Up @@ -166,14 +180,46 @@ impl<'a> Drop for Device<'a> {
}
}

#[repr(u32)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
/// Frida device type.
///
/// Represents different connection types
// On Windows, the constants are i32 instead of u32, so we need to cast accordingly.
pub enum DeviceType {
/// Local Frida device.
Local,
/// Remote Frida device.
Remote,
/// USB Frida device.
USB,
Local = frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_LOCAL as _,

/// Remote Frida device, connected via network
Remote = frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_REMOTE as _,

/// Device connected via USB
USB = frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_USB as _,
}

#[cfg(not(target_family = "windows"))]
impl From<u32> for DeviceType {
fn from(value: u32) -> Self {
match value {
frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_LOCAL => Self::Local,
frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_REMOTE => Self::Remote,
frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_USB => Self::USB,
value => unreachable!("Invalid Device type {}", value),
}
}
}

#[cfg(target_family = "windows")]
impl From<i32> for DeviceType {
fn from(value: i32) -> Self {
match value {
frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_LOCAL => Self::Local,
frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_REMOTE => Self::Remote,
frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_USB => Self::USB,
value => unreachable!("Invalid Device type {}", value),
}
}
}

impl From<DeviceType> for frida_sys::FridaDeviceType {
Expand All @@ -185,3 +231,9 @@ impl From<DeviceType> for frida_sys::FridaDeviceType {
}
}
}

impl std::fmt::Display for DeviceType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

0 comments on commit 30c6b5f

Please sign in to comment.