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 @@
use std::ffi::{CStr, c_void};
use std::{
ffi::{CStr, c_void},
ptr::null_mut,
};
use idevice::{
IdeviceError, IdeviceService, provider::IdeviceProvider,
springboardservices::SpringBoardServicesClient,
};
use crate::{IdeviceErrorCode, IdeviceHandle, RUNTIME, provider::IdeviceProviderHandle};
use crate::{IdeviceFfiError, IdeviceHandle, RUNTIME, ffi_err, provider::IdeviceProviderHandle};
pub struct SpringBoardServicesClientHandle(pub SpringBoardServicesClient);
@@ -16,7 +19,7 @@ pub struct SpringBoardServicesClientHandle(pub SpringBoardServicesClient);
/// * [`client`] - On success, will be set to point to a newly allocated SpringBoardServicesClient handle
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
@@ -25,10 +28,10 @@ pub struct SpringBoardServicesClientHandle(pub SpringBoardServicesClient);
pub unsafe extern "C" fn springboard_services_connect(
provider: *mut IdeviceProviderHandle,
client: *mut *mut SpringBoardServicesClientHandle,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if provider.is_null() || client.is_null() {
log::error!("Null pointer provided");
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let res: Result<SpringBoardServicesClient, IdeviceError> = RUNTIME.block_on(async move {
@@ -40,13 +43,13 @@ pub unsafe extern "C" fn springboard_services_connect(
Ok(r) => {
let boxed = Box::new(SpringBoardServicesClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => {
// If connection failed, the provider_box was already forgotten,
// so we need to reconstruct it to avoid leak
let _ = unsafe { Box::from_raw(provider) };
e.into()
ffi_err!(e)
}
}
}
@@ -58,7 +61,7 @@ pub unsafe extern "C" fn springboard_services_connect(
/// * [`client`] - On success, will be set to point to a newly allocated SpringBoardServicesClient handle
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `socket` must be a valid pointer to a handle allocated by this library. The socket is consumed,
@@ -68,15 +71,15 @@ pub unsafe extern "C" fn springboard_services_connect(
pub unsafe extern "C" fn springboard_services_new(
socket: *mut IdeviceHandle,
client: *mut *mut SpringBoardServicesClientHandle,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if socket.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let socket = unsafe { Box::from_raw(socket) }.0;
let r = SpringBoardServicesClient::new(socket);
let boxed = Box::new(SpringBoardServicesClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
/// Gets the icon of the specified app by bundle identifier
@@ -87,7 +90,7 @@ pub unsafe extern "C" fn springboard_services_new(
/// * `out_result` - On success, will be set to point to a newly allocated png data
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
@@ -98,17 +101,17 @@ pub unsafe extern "C" fn springboard_services_get_icon(
bundle_identifier: *const libc::c_char,
out_result: *mut *mut c_void,
out_result_len: *mut libc::size_t,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if client.is_null() || out_result.is_null() || out_result_len.is_null() {
log::error!("Invalid arguments: {client:?}, {out_result:?}");
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let client = unsafe { &mut *client };
let name_cstr = unsafe { CStr::from_ptr(bundle_identifier) };
let bundle_id = match name_cstr.to_str() {
Ok(s) => s.to_string(),
Err(_) => return IdeviceErrorCode::InvalidArg,
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
};
let res: Result<Vec<u8>, IdeviceError> =
@@ -125,9 +128,9 @@ pub unsafe extern "C" fn springboard_services_get_icon(
*out_result = ptr as *mut c_void;
*out_result_len = len;
}
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => e.into(),
Err(e) => ffi_err!(e),
}
}