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 Sep 18, 2023
1 parent 254ff39 commit 061f260
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 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 @@ -165,3 +179,36 @@ impl<'a> Drop for Device<'a> {
unsafe { frida_sys::frida_unref(self.device_ptr as _) }
}
}

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

/// 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 _,
}

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),
}
}
}

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

0 comments on commit 061f260

Please sign in to comment.