mirror of
https://github.com/jkcoxson/idevice.git
synced 2026-03-02 14:36:16 +01:00
Update FFI to use error struct for message
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
// Jackson Coxson
|
||||
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use idevice::{
|
||||
IdeviceError, IdeviceService, heartbeat::HeartbeatClient, provider::IdeviceProvider,
|
||||
};
|
||||
|
||||
use crate::{IdeviceErrorCode, IdeviceHandle, RUNTIME, provider::IdeviceProviderHandle};
|
||||
use crate::{IdeviceFfiError, IdeviceHandle, RUNTIME, ffi_err, provider::IdeviceProviderHandle};
|
||||
|
||||
pub struct HeartbeatClientHandle(pub HeartbeatClient);
|
||||
|
||||
@@ -15,7 +17,7 @@ pub struct HeartbeatClientHandle(pub HeartbeatClient);
|
||||
/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient 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
|
||||
@@ -24,10 +26,10 @@ pub struct HeartbeatClientHandle(pub HeartbeatClient);
|
||||
pub unsafe extern "C" fn heartbeat_connect(
|
||||
provider: *mut IdeviceProviderHandle,
|
||||
client: *mut *mut HeartbeatClientHandle,
|
||||
) -> 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<HeartbeatClient, IdeviceError> = RUNTIME.block_on(async move {
|
||||
@@ -40,13 +42,10 @@ pub unsafe extern "C" fn heartbeat_connect(
|
||||
Ok(r) => {
|
||||
let boxed = Box::new(HeartbeatClientHandle(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 +57,7 @@ pub unsafe extern "C" fn heartbeat_connect(
|
||||
/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient 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 +67,15 @@ pub unsafe extern "C" fn heartbeat_connect(
|
||||
pub unsafe extern "C" fn heartbeat_new(
|
||||
socket: *mut IdeviceHandle,
|
||||
client: *mut *mut HeartbeatClientHandle,
|
||||
) -> IdeviceErrorCode {
|
||||
) -> *mut IdeviceFfiError {
|
||||
if socket.is_null() || client.is_null() {
|
||||
return IdeviceErrorCode::InvalidArg;
|
||||
return ffi_err!(IdeviceError::FfiInvalidArg);
|
||||
}
|
||||
let socket = unsafe { Box::from_raw(socket) }.0;
|
||||
let r = HeartbeatClient::new(socket);
|
||||
let boxed = Box::new(HeartbeatClientHandle(r));
|
||||
unsafe { *client = Box::into_raw(boxed) };
|
||||
IdeviceErrorCode::IdeviceSuccess
|
||||
null_mut()
|
||||
}
|
||||
|
||||
/// Sends a polo to the device
|
||||
@@ -85,24 +84,24 @@ pub unsafe extern "C" fn heartbeat_new(
|
||||
/// * `client` - A valid HeartbeatClient handle
|
||||
///
|
||||
/// # 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
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn heartbeat_send_polo(
|
||||
client: *mut HeartbeatClientHandle,
|
||||
) -> IdeviceErrorCode {
|
||||
) -> *mut IdeviceFfiError {
|
||||
if client.is_null() {
|
||||
return IdeviceErrorCode::InvalidArg;
|
||||
return ffi_err!(IdeviceError::FfiInvalidArg);
|
||||
}
|
||||
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
|
||||
let client_ref = unsafe { &mut (*client).0 };
|
||||
client_ref.send_polo().await
|
||||
});
|
||||
match res {
|
||||
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
|
||||
Err(e) => e.into(),
|
||||
Ok(_) => null_mut(),
|
||||
Err(e) => ffi_err!(e),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +113,7 @@ pub unsafe extern "C" fn heartbeat_send_polo(
|
||||
/// * `new_interval` - A pointer to set the requested marco
|
||||
///
|
||||
/// # 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
|
||||
@@ -123,9 +122,9 @@ pub unsafe extern "C" fn heartbeat_get_marco(
|
||||
client: *mut HeartbeatClientHandle,
|
||||
interval: u64,
|
||||
new_interval: *mut u64,
|
||||
) -> IdeviceErrorCode {
|
||||
) -> *mut IdeviceFfiError {
|
||||
if client.is_null() || new_interval.is_null() {
|
||||
return IdeviceErrorCode::InvalidArg;
|
||||
return ffi_err!(IdeviceError::FfiInvalidArg);
|
||||
}
|
||||
let res: Result<u64, IdeviceError> = RUNTIME.block_on(async move {
|
||||
let client_ref = unsafe { &mut (*client).0 };
|
||||
@@ -134,9 +133,9 @@ pub unsafe extern "C" fn heartbeat_get_marco(
|
||||
match res {
|
||||
Ok(n) => {
|
||||
unsafe { *new_interval = n };
|
||||
IdeviceErrorCode::IdeviceSuccess
|
||||
null_mut()
|
||||
}
|
||||
Err(e) => e.into(),
|
||||
Err(e) => ffi_err!(e),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user