Skip to content

Commit

Permalink
Added get_device_by_id to the device_manager and example (#117)
Browse files Browse the repository at this point in the history
* Add get_device_by_id to the device_manager and example

* fixed typo in rust doc that caused doc test to fail

* Fixed format with rust fmt

* fixed unnecessary cast

---------

Co-authored-by: Andras <[email protected]>
  • Loading branch information
iteroji and iteroji authored Dec 14, 2023
1 parent 0cbb308 commit a4482a2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"examples/gum/linux_no_std",
"examples/gum/memory_access_monitor",
"examples/core/hello",
"examples/core/usb_device",
"examples/core/console_log",
]
# We miss our linux_no_std example from the default members since `cargo check`
Expand All @@ -33,5 +34,6 @@ default-members = [
"examples/gum/fast_interceptor",
"examples/gum/memory_access_monitor",
"examples/core/hello",
"examples/core/usb_device",
"examples/core/console_log",
]
12 changes: 12 additions & 0 deletions examples/core/usb_device/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "usb_device"
version = "0.1.0"
authors = ["Andras Marczell <[email protected]>"]
edition = "2018"
license = "wxWindows"
publish = false

[dependencies]
frida = { path = "../../../frida" }
frida-sys = { path = "../../../frida-sys" }
lazy_static = "1.4"
21 changes: 21 additions & 0 deletions examples/core/usb_device/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use frida::DeviceType;

fn main() {
let frida = unsafe { frida::Frida::obtain() };
let device_manager = frida::DeviceManager::obtain(&frida);

// get the first usb device (assuming there is one attached)
let device = device_manager.get_device_by_type(DeviceType::USB).unwrap();
assert_eq!(device.get_type(), DeviceType::USB);
println!(
"found {} with type: {}",
device.get_name(),
device.get_type()
);

// get the device id and use it to obtain a the device by the id
let device_id = device.get_id();
let device = device_manager.get_device_by_id(device_id).unwrap();
assert_eq!(device.get_id(), device_id);
println!("found {} with id: {}", device.get_name(), device.get_id());
}
33 changes: 33 additions & 0 deletions frida/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

use frida_sys::_FridaDeviceManager;
use std::ffi::CString;
use std::marker::PhantomData;

use crate::device::Device;
Expand Down Expand Up @@ -83,6 +84,38 @@ impl<'a> DeviceManager<'a> {

return Ok(Device::from_raw(device_ptr));
}

/// Returns the device with the specified id.
///
/// # Example
///
/// let frida = unsafe { frida::Frida::obtain() };
/// let device_manager = frida::DeviceManager::obtain(&frida);
///
/// let id = "<some id>";
/// let device = device_manager.get_device_by_id(id).unwrap();
/// assert_eq!(device.get_id(), id);
///
pub fn get_device_by_id(&'a self, device_id: &str) -> Result<Device<'a>> {
let mut error: *mut frida_sys::GError = std::ptr::null_mut();
let cstring = CString::new(device_id).unwrap();

let device_ptr = unsafe {
frida_sys::frida_device_manager_get_device_by_id_sync(
self.manager_ptr,
cstring.as_ptr(),
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

0 comments on commit a4482a2

Please sign in to comment.