mirror of
https://github.com/jkcoxson/idevice.git
synced 2026-03-02 14:36:16 +01:00
Refactor FFI bindings
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use idevice::{syslog_relay::SyslogRelayClient, IdeviceError, IdeviceService};
|
||||
|
||||
use crate::{
|
||||
provider::TcpProviderHandle, IdeviceErrorCode, RUNTIME
|
||||
use idevice::{
|
||||
IdeviceError, IdeviceService, provider::IdeviceProvider, syslog_relay::SyslogRelayClient,
|
||||
};
|
||||
|
||||
use crate::{IdeviceErrorCode, RUNTIME, provider::IdeviceProviderHandle};
|
||||
|
||||
pub struct SyslogRelayClientHandle(pub SyslogRelayClient);
|
||||
|
||||
/// Automatically creates and connects to syslog relay, returning a client handle
|
||||
///
|
||||
/// # Arguments
|
||||
/// * [`provider`] - A TcpProvider
|
||||
/// * [`provider`] - An IdeviceProvider
|
||||
/// * [`client`] - On success, will be set to point to a newly allocated SyslogRelayClient handle
|
||||
///
|
||||
/// # 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 extern "C" fn syslog_relay_connect_tcp(
|
||||
provider: *mut TcpProviderHandle,
|
||||
client: *mut *mut SyslogRelayClientHandle
|
||||
pub unsafe extern "C" fn syslog_relay_connect_tcp(
|
||||
provider: *mut IdeviceProviderHandle,
|
||||
client: *mut *mut SyslogRelayClientHandle,
|
||||
) -> IdeviceErrorCode {
|
||||
if provider.is_null() {
|
||||
log::error!("Null pointer provided");
|
||||
@@ -28,14 +28,8 @@ pub extern "C" fn syslog_relay_connect_tcp(
|
||||
}
|
||||
|
||||
let res: Result<SyslogRelayClient, IdeviceError> = RUNTIME.block_on(async move {
|
||||
let provider_box = unsafe { Box::from_raw(provider) };
|
||||
|
||||
let provider_ref = &provider_box.0;
|
||||
|
||||
let result = SyslogRelayClient::connect(provider_ref).await;
|
||||
|
||||
std::mem::forget(provider_box);
|
||||
result
|
||||
let provider_ref: &dyn IdeviceProvider = unsafe { &*(*provider).0 };
|
||||
SyslogRelayClient::connect(provider_ref).await
|
||||
});
|
||||
|
||||
match res {
|
||||
@@ -62,9 +56,7 @@ pub extern "C" fn syslog_relay_connect_tcp(
|
||||
/// `handle` must be a valid pointer to the handle that was allocated by this library,
|
||||
/// or NULL (in which case this function does nothing)
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn syslog_relay_client_free(
|
||||
handle: *mut SyslogRelayClientHandle
|
||||
) {
|
||||
pub unsafe extern "C" fn syslog_relay_client_free(handle: *mut SyslogRelayClientHandle) {
|
||||
if !handle.is_null() {
|
||||
log::debug!("Freeing syslog relay client");
|
||||
let _ = unsafe { Box::from_raw(handle) };
|
||||
@@ -76,12 +68,12 @@ pub extern "C" fn syslog_relay_client_free(
|
||||
/// # Arguments
|
||||
/// * [`client`] - The SyslogRelayClient handle
|
||||
/// * [`log_message`] - On success a newly allocated cstring will be set to point to the log message
|
||||
///
|
||||
///
|
||||
/// # Safety
|
||||
/// `client` must be a valid pointer to a handle allocated by this library
|
||||
/// `log_message` must be a valid, non-null pointer to a location where the log message will be stored
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn syslog_relay_next(
|
||||
pub unsafe extern "C" fn syslog_relay_next(
|
||||
client: *mut SyslogRelayClientHandle,
|
||||
log_message: *mut *mut c_char,
|
||||
) -> IdeviceErrorCode {
|
||||
@@ -89,20 +81,15 @@ pub extern "C" fn syslog_relay_next(
|
||||
return IdeviceErrorCode::InvalidArg;
|
||||
}
|
||||
|
||||
let res = RUNTIME.block_on(async {
|
||||
unsafe { &mut *client }
|
||||
.0
|
||||
.next()
|
||||
.await
|
||||
});
|
||||
let res = RUNTIME.block_on(async { unsafe { &mut *client }.0.next().await });
|
||||
|
||||
match res {
|
||||
Ok(log) => {
|
||||
use std::ffi::CString;
|
||||
|
||||
|
||||
// null bytes are a curse in C, so just use spaces
|
||||
let safe_log = log.replace('\0', " ");
|
||||
|
||||
|
||||
match CString::new(safe_log) {
|
||||
Ok(c_string) => {
|
||||
unsafe { *log_message = c_string.into_raw() };
|
||||
@@ -116,4 +103,4 @@ pub extern "C" fn syslog_relay_next(
|
||||
}
|
||||
Err(e) => e.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user