Skip to content

Commit

Permalink
Add device lookup by type
Browse files Browse the repository at this point in the history
* Add device lookup by type

* Use is_null check for errors
  • Loading branch information
csnewman authored Oct 11, 2023
1 parent 6bb8ca6 commit aadfdeb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions frida/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeviceType> 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,
}
}
}
24 changes: 24 additions & 0 deletions frida/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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<Device<'a>> {
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> {
Expand Down
4 changes: 4 additions & 0 deletions frida/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit aadfdeb

Please sign in to comment.