Skip to content

Commit

Permalink
[eclipse-iceoryx#507] Adjust FFI binding
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 18, 2024
1 parent a9c8b4b commit bfa9033
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
2 changes: 2 additions & 0 deletions examples/rust/event_multiplexing/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("");
}

CallbackProgression::Continue
};

// loops until the user has pressed CTRL+c, the application has received a SIGTERM or SIGINT
Expand Down
52 changes: 17 additions & 35 deletions iceoryx2-ffi/ffi/src/api/waitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use std::{ffi::c_int, mem::ManuallyDrop, time::Duration};

use crate::{
c_size_t, iox2_callback_context, iox2_file_descriptor_ptr, iox2_service_type_e,
iox2_waitset_attachment_id_h, iox2_waitset_attachment_id_t, iox2_waitset_guard_h,
iox2_waitset_guard_t, AttachmentIdUnion, GuardUnion, IOX2_OK,
c_size_t, iox2_callback_context, iox2_callback_progression_e, iox2_file_descriptor_ptr,
iox2_service_type_e, iox2_waitset_attachment_id_h, iox2_waitset_attachment_id_t,
iox2_waitset_guard_h, iox2_waitset_guard_t, AttachmentIdUnion, GuardUnion, IOX2_OK,
};

use super::{AssertNonNullHandle, HandleToType, IntoCInt};
Expand Down Expand Up @@ -50,8 +50,6 @@ impl IntoCInt for WaitSetRunError {
}
WaitSetRunError::InternalError => iox2_waitset_run_error_e::INTERNAL_ERROR,
WaitSetRunError::NoAttachments => iox2_waitset_run_error_e::NO_ATTACHMENTS,
WaitSetRunError::TerminationRequest => iox2_waitset_run_error_e::TERMINATION_REQUEST,
WaitSetRunError::Interrupt => iox2_waitset_run_error_e::INTERRUPT,
}) as c_int
}
}
Expand All @@ -62,6 +60,7 @@ pub enum iox2_waitset_run_result_e {
TERMINATION_REQUEST = IOX2_OK as isize + 1,
INTERRUPT,
STOP_REQUEST,
ALL_EVENTS_HANDLED,
}

impl IntoCInt for WaitSetRunResult {
Expand All @@ -76,6 +75,7 @@ impl From<WaitSetRunResult> for iox2_waitset_run_result_e {
WaitSetRunResult::TerminationRequest => iox2_waitset_run_result_e::TERMINATION_REQUEST,
WaitSetRunResult::Interrupt => iox2_waitset_run_result_e::INTERRUPT,
WaitSetRunResult::StopRequest => iox2_waitset_run_result_e::STOP_REQUEST,
WaitSetRunResult::AllEventsHandled => iox2_waitset_run_result_e::ALL_EVENTS_HANDLED,
}
}
}
Expand Down Expand Up @@ -201,8 +201,10 @@ impl HandleToType for iox2_waitset_h_ref {
}
}

pub type iox2_waitset_run_callback =
extern "C" fn(iox2_waitset_attachment_id_h, iox2_callback_context);
pub type iox2_waitset_run_callback = extern "C" fn(
iox2_waitset_attachment_id_h,
iox2_callback_context,
) -> iox2_callback_progression_e;
// END type definition

// BEGIN C API
Expand Down Expand Up @@ -283,26 +285,6 @@ pub unsafe extern "C" fn iox2_waitset_capacity(handle: iox2_waitset_h_ref) -> c_
}
}

/// Stops the current [`iox2_waitset_wait_and_process()`] operation. Any [`iox2_waitset_wait_and_process()`]
/// call after this call is not affected and the user needs to call
/// [`iox2_waitset_stop()`] again.
///
/// # Safety
///
/// * `handle` must be valid and acquired with
/// [`iox2_waitset_builder_create()`](crate::iox2_waitset_builder_create())
#[no_mangle]
pub unsafe extern "C" fn iox2_waitset_stop(handle: iox2_waitset_h_ref) {
handle.assert_non_null();

let waitset = &mut *handle.as_type();

match waitset.service_type {
iox2_service_type_e::IPC => waitset.value.as_ref().ipc.stop(),
iox2_service_type_e::LOCAL => waitset.value.as_ref().local.stop(),
}
}

/// Attaches a provided [`iox2_file_descriptor_ptr`] as notification to the
/// [`iox2_waitset_h`]. As soon as the attachment receives data, the WaitSet
/// wakes up in [`iox2_waitset_wait_and_process()`] and informs the user.
Expand Down Expand Up @@ -568,7 +550,7 @@ pub unsafe extern "C" fn iox2_waitset_attach_interval(
/// * the provided [`iox2_waitset_attachment_id_h`] in the callback must be released via
/// [`iox2_waitset_attachment_id_drop()`](crate::iox2_waitset_attachment_id_drop())
#[no_mangle]
pub unsafe extern "C" fn iox2_waitset_try_wait_and_process(
pub unsafe extern "C" fn iox2_waitset_wait_and_process_once(
handle: iox2_waitset_h_ref,
callback: iox2_waitset_run_callback,
callback_ctx: iox2_callback_context,
Expand All @@ -583,37 +565,37 @@ pub unsafe extern "C" fn iox2_waitset_try_wait_and_process(
.value
.as_ref()
.ipc
.try_wait_and_process(|attachment_id| {
.wait_and_process_once(|attachment_id| {
let attachment_id_ptr = iox2_waitset_attachment_id_t::alloc();
(*attachment_id_ptr).init(
waitset.service_type,
AttachmentIdUnion::new_ipc(attachment_id),
iox2_waitset_attachment_id_t::dealloc,
);
let attachment_id_handle_ptr = (*attachment_id_ptr).as_handle();
callback(attachment_id_handle_ptr, callback_ctx);
callback(attachment_id_handle_ptr, callback_ctx).into()
})
}
iox2_service_type_e::LOCAL => {
waitset
.value
.as_ref()
.local
.try_wait_and_process(|attachment_id| {
.wait_and_process_once(|attachment_id| {
let attachment_id_ptr = iox2_waitset_attachment_id_t::alloc();
(*attachment_id_ptr).init(
waitset.service_type,
AttachmentIdUnion::new_local(attachment_id),
iox2_waitset_attachment_id_t::dealloc,
);
let attachment_id_handle_ptr = (*attachment_id_ptr).as_handle();
callback(attachment_id_handle_ptr, callback_ctx);
callback(attachment_id_handle_ptr, callback_ctx).into()
})
}
};

match run_once_result {
Ok(()) => IOX2_OK,
Ok(v) => v.into_c_int(),
Err(e) => e.into_c_int(),
}
}
Expand Down Expand Up @@ -667,7 +649,7 @@ pub unsafe extern "C" fn iox2_waitset_wait_and_process(
iox2_waitset_attachment_id_t::dealloc,
);
let attachment_id_handle_ptr = (*attachment_id_ptr).as_handle();
callback(attachment_id_handle_ptr, callback_ctx);
callback(attachment_id_handle_ptr, callback_ctx).into()
}),
iox2_service_type_e::LOCAL => {
waitset
Expand All @@ -682,7 +664,7 @@ pub unsafe extern "C" fn iox2_waitset_wait_and_process(
iox2_waitset_attachment_id_t::dealloc,
);
let attachment_id_handle_ptr = (*attachment_id_ptr).as_handle();
callback(attachment_id_handle_ptr, callback_ctx);
callback(attachment_id_handle_ptr, callback_ctx).into()
})
}
};
Expand Down

0 comments on commit bfa9033

Please sign in to comment.