diff --git a/frida/src/device.rs b/frida/src/device.rs index 1533b85..b89e591 100644 --- a/frida/src/device.rs +++ b/frida/src/device.rs @@ -105,3 +105,23 @@ impl<'a> Drop for Device<'a> { unsafe { frida_sys::frida_unref(self.device_ptr as _) } } } + +/// Frida device type. +pub enum DeviceType { + /// Local Frida device. + Local, + /// Remote Frida device. + Remote, + /// USB Frida device. + USB, +} + +impl From for frida_sys::FridaDeviceType { + fn from(value: DeviceType) -> Self { + match value { + DeviceType::Local => frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_LOCAL, + DeviceType::Remote => frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_REMOTE, + DeviceType::USB => frida_sys::FridaDeviceType_FRIDA_DEVICE_TYPE_USB, + } + } +} diff --git a/frida/src/device_manager.rs b/frida/src/device_manager.rs index 426e4f2..0dbcf39 100644 --- a/frida/src/device_manager.rs +++ b/frida/src/device_manager.rs @@ -8,7 +8,10 @@ use frida_sys::_FridaDeviceManager; use std::marker::PhantomData; use crate::device::Device; +use crate::DeviceType; +use crate::Error; use crate::Frida; +use crate::Result; /// Platform-independent device manager abstraction access. pub struct DeviceManager<'a> { @@ -59,6 +62,27 @@ impl<'a> DeviceManager<'a> { unsafe { frida_sys::frida_unref(devices_ptr as _) } devices } + + /// Returns the device of the specified type. + pub fn get_device_by_type(&'a self, r#type: DeviceType) -> Result> { + let mut error: *mut frida_sys::GError = std::ptr::null_mut(); + + let device_ptr = unsafe { + frida_sys::frida_device_manager_get_device_by_type_sync( + self.manager_ptr, + r#type.into(), + 0, + std::ptr::null_mut(), + &mut error, + ) + }; + + if !error.is_null() { + return Err(Error::DeviceLookupFailed); + } + + return Ok(Device::from_raw(device_ptr)); + } } impl<'a> Drop for DeviceManager<'a> { diff --git a/frida/src/error.rs b/frida/src/error.rs index f6870ec..9b1ebd9 100644 --- a/frida/src/error.rs +++ b/frida/src/error.rs @@ -13,6 +13,10 @@ pub enum Error { #[error("Failed to attach")] DeviceAttachError, + /// Failled to lookup a device. + #[error("Failed to lookup device")] + DeviceLookupFailed, + /// Failed to detach a session. #[error("Failed to detach the current session")] SessionDetachError,