Refactor FFI bindings

This commit is contained in:
Jackson Coxson
2025-05-26 12:52:23 -06:00
parent 15261861e0
commit fa88c2c87d
22 changed files with 1005 additions and 1476 deletions

View File

@@ -1,20 +1,17 @@
// Jackson Coxson
use idevice::{IdeviceError, IdeviceService, heartbeat::HeartbeatClient};
use crate::{
IdeviceErrorCode, IdeviceHandle, RUNTIME,
provider::{TcpProviderHandle, UsbmuxdProviderHandle},
use idevice::{
IdeviceError, IdeviceService, heartbeat::HeartbeatClient, provider::IdeviceProvider,
};
use crate::{IdeviceErrorCode, IdeviceHandle, RUNTIME, provider::IdeviceProviderHandle};
pub struct HeartbeatClientHandle(pub HeartbeatClient);
#[allow(non_camel_case_types)]
pub struct plist_t;
/// Automatically creates and connects to Installation Proxy, returning a client handle
///
/// # Arguments
/// * [`provider`] - A TcpProvider
/// * [`provider`] - An IdeviceProvider
/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient handle
///
/// # Returns
@@ -24,8 +21,8 @@ pub struct plist_t;
/// `provider` must be a valid pointer to a handle allocated by this library
/// `client` must be a valid, non-null pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn heartbeat_connect_tcp(
provider: *mut TcpProviderHandle,
pub unsafe extern "C" fn heartbeat_connect(
provider: *mut IdeviceProviderHandle,
client: *mut *mut HeartbeatClientHandle,
) -> IdeviceErrorCode {
if provider.is_null() || client.is_null() {
@@ -34,18 +31,9 @@ pub unsafe extern "C" fn heartbeat_connect_tcp(
}
let res: Result<HeartbeatClient, IdeviceError> = RUNTIME.block_on(async move {
// Take ownership of the provider (without immediately dropping it)
let provider_box = unsafe { Box::from_raw(provider) };
// Get a reference to the inner value
let provider_ref = &provider_box.0;
let provider_ref: &dyn IdeviceProvider = unsafe { &*(*provider).0 };
// Connect using the reference
let result = HeartbeatClient::connect(provider_ref).await;
// Explicitly keep the provider_box alive until after connect completes
std::mem::forget(provider_box);
result
HeartbeatClient::connect(provider_ref).await
});
match res {
@@ -63,53 +51,6 @@ pub unsafe extern "C" fn heartbeat_connect_tcp(
}
}
/// Automatically creates and connects to Installation Proxy, returning a client handle
///
/// # Arguments
/// * [`provider`] - A UsbmuxdProvider
/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient handle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
/// `client` must be a valid, non-null pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn heartbeat_connect_usbmuxd(
provider: *mut UsbmuxdProviderHandle,
client: *mut *mut HeartbeatClientHandle,
) -> IdeviceErrorCode {
if provider.is_null() {
log::error!("Provider is null");
return IdeviceErrorCode::InvalidArg;
}
let res: Result<HeartbeatClient, IdeviceError> = RUNTIME.block_on(async move {
// Take ownership of the provider (without immediately dropping it)
let provider_box = unsafe { Box::from_raw(provider) };
// Get a reference to the inner value
let provider_ref = &provider_box.0;
// Connect using the reference
let result = HeartbeatClient::connect(provider_ref).await;
// Explicitly keep the provider_box alive until after connect completes
std::mem::forget(provider_box);
result
});
match res {
Ok(r) => {
let boxed = Box::new(HeartbeatClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Automatically creates and connects to Installation Proxy, returning a client handle
///
/// # Arguments
@@ -120,14 +61,15 @@ pub unsafe extern "C" fn heartbeat_connect_usbmuxd(
/// An error code indicating success or failure
///
/// # Safety
/// `socket` must be a valid pointer to a handle allocated by this library
/// `socket` must be a valid pointer to a handle allocated by this library. The socket is consumed,
/// and may not be used again.
/// `client` must be a valid, non-null pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn heartbeat_new(
socket: *mut IdeviceHandle,
client: *mut *mut HeartbeatClientHandle,
) -> IdeviceErrorCode {
if socket.is_null() {
if socket.is_null() || client.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let socket = unsafe { Box::from_raw(socket) }.0;
@@ -151,16 +93,12 @@ pub unsafe extern "C" fn heartbeat_new(
pub unsafe extern "C" fn heartbeat_send_polo(
client: *mut HeartbeatClientHandle,
) -> IdeviceErrorCode {
if client.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
// Take ownership of the client
let mut client_box = unsafe { Box::from_raw(client) };
// Get a reference to the inner value
let client_ref = &mut client_box.0;
let res = client_ref.send_polo().await;
std::mem::forget(client_box);
res
let client_ref = unsafe { &mut (*client).0 };
client_ref.send_polo().await
});
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
@@ -186,16 +124,12 @@ pub unsafe extern "C" fn heartbeat_get_marco(
interval: u64,
new_interval: *mut u64,
) -> IdeviceErrorCode {
if client.is_null() || new_interval.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let res: Result<u64, IdeviceError> = RUNTIME.block_on(async move {
// Take ownership of the client
let mut client_box = unsafe { Box::from_raw(client) };
// Get a reference to the inner value
let client_ref = &mut client_box.0;
let new = client_ref.get_marco(interval).await;
std::mem::forget(client_box);
new
let client_ref = unsafe { &mut (*client).0 };
client_ref.get_marco(interval).await
});
match res {
Ok(n) => {