Update FFI to use error struct for message

This commit is contained in:
Jackson Coxson
2025-06-02 19:42:17 -06:00
parent 5d7aa50a7d
commit 35ba07889b
36 changed files with 1358 additions and 1135 deletions

View File

@@ -1,11 +1,14 @@
// Jackson Coxson
use std::ffi::{CStr, c_char};
use std::{
ffi::{CStr, c_char},
ptr::null_mut,
};
use idevice::{ReadWrite, dvt::process_control::ProcessControlClient};
use plist::{Dictionary, Value};
use crate::{IdeviceErrorCode, RUNTIME, remote_server::RemoteServerHandle};
use crate::{IdeviceFfiError, RUNTIME, ffi_err, remote_server::RemoteServerHandle};
/// Opaque handle to a ProcessControlClient
pub struct ProcessControlHandle<'a>(pub ProcessControlClient<'a, Box<dyn ReadWrite>>);
@@ -17,7 +20,7 @@ pub struct ProcessControlHandle<'a>(pub ProcessControlClient<'a, Box<dyn ReadWri
/// * [`handle`] - Pointer to store the newly created ProcessControlClient handle
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `server` must be a valid pointer to a handle allocated by this library
@@ -26,9 +29,9 @@ pub struct ProcessControlHandle<'a>(pub ProcessControlClient<'a, Box<dyn ReadWri
pub unsafe extern "C" fn process_control_new(
server: *mut RemoteServerHandle,
handle: *mut *mut ProcessControlHandle<'static>,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if server.is_null() || handle.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let server = unsafe { &mut (*server).0 };
@@ -38,9 +41,9 @@ pub unsafe extern "C" fn process_control_new(
Ok(client) => {
let boxed = Box::new(ProcessControlHandle(client));
unsafe { *handle = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => e.into(),
Err(e) => ffi_err!(e),
}
}
@@ -70,7 +73,7 @@ pub unsafe extern "C" fn process_control_free(handle: *mut ProcessControlHandle<
/// * [`pid`] - Pointer to store the process ID of the launched app
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// All pointers must be valid or NULL where appropriate
@@ -85,15 +88,15 @@ pub unsafe extern "C" fn process_control_launch_app(
start_suspended: bool,
kill_existing: bool,
pid: *mut u64,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if handle.is_null() || bundle_id.is_null() || pid.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let bundle_id = unsafe { CStr::from_ptr(bundle_id) };
let bundle_id = match bundle_id.to_str() {
Ok(s) => s.to_string(),
Err(_) => return IdeviceErrorCode::InvalidArg,
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
};
let mut env_dict = Dictionary::new();
@@ -140,9 +143,9 @@ pub unsafe extern "C" fn process_control_launch_app(
match res {
Ok(p) => {
unsafe { *pid = p };
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => e.into(),
Err(e) => ffi_err!(e),
}
}
@@ -153,7 +156,7 @@ pub unsafe extern "C" fn process_control_launch_app(
/// * [`pid`] - The process ID to kill
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library
@@ -161,17 +164,17 @@ pub unsafe extern "C" fn process_control_launch_app(
pub unsafe extern "C" fn process_control_kill_app(
handle: *mut ProcessControlHandle<'static>,
pid: u64,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if handle.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let client = unsafe { &mut (*handle).0 };
let res = RUNTIME.block_on(async move { client.kill_app(pid).await });
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
Err(e) => e.into(),
Ok(_) => null_mut(),
Err(e) => ffi_err!(e),
}
}
@@ -182,7 +185,7 @@ pub unsafe extern "C" fn process_control_kill_app(
/// * [`pid`] - The process ID to modify
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library
@@ -190,16 +193,16 @@ pub unsafe extern "C" fn process_control_kill_app(
pub unsafe extern "C" fn process_control_disable_memory_limit(
handle: *mut ProcessControlHandle<'static>,
pid: u64,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if handle.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let client = unsafe { &mut (*handle).0 };
let res = RUNTIME.block_on(async move { client.disable_memory_limit(pid).await });
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
Err(e) => e.into(),
Ok(_) => null_mut(),
Err(e) => ffi_err!(e),
}
}