Skip to main content

rustorb_dds_ffi/
condition.rs

1//! Conditions (`rdds_condition`: guard, status and read conditions) and
2//! WaitSets (`rdds_waitset`).
3
4use rustorb_dds::{Condition, GuardCondition, StatusMask, WaitSet};
5
6use crate::entity::RddsDuration;
7use crate::{Res, Status, bad, guard, guard_value, obj, put, release, set};
8
9/// `rdds_condition`.
10#[derive(Debug, Clone)]
11pub struct RddsCondition(pub(crate) Condition);
12
13impl<T: Into<Condition>> From<T> for RddsCondition {
14    fn from(c: T) -> Self {
15        RddsCondition(c.into())
16    }
17}
18
19/// `rdds_guard_condition_new`.
20#[unsafe(no_mangle)]
21pub unsafe extern "C" fn rdds_guard_condition_new(out: *mut *mut RddsCondition) -> Status {
22    guard(|| unsafe { put(out, RddsCondition::from(GuardCondition::new())) })
23}
24
25/// `rdds_condition_release`.
26#[unsafe(no_mangle)]
27pub unsafe extern "C" fn rdds_condition_release(c: *mut RddsCondition) {
28    unsafe { release(c) }
29}
30
31/// `rdds_condition_trigger_value`.
32#[unsafe(no_mangle)]
33pub unsafe extern "C" fn rdds_condition_trigger_value(c: *const RddsCondition) -> bool {
34    guard_value(false, || {
35        unsafe { c.as_ref() }.is_some_and(|c| c.0.trigger_value())
36    })
37}
38
39/// `rdds_condition_equals`: whether two handles are the same condition.
40#[unsafe(no_mangle)]
41pub unsafe extern "C" fn rdds_condition_equals(
42    a: *const RddsCondition,
43    b: *const RddsCondition,
44) -> bool {
45    guard_value(false, || match unsafe { (a.as_ref(), b.as_ref()) } {
46        (Some(a), Some(b)) => a.0 == b.0,
47        _ => false,
48    })
49}
50
51fn guard_condition(c: &RddsCondition) -> Res<&GuardCondition> {
52    match &c.0 {
53        Condition::Guard(g) => Ok(g),
54        _ => Err(bad("not a guard condition")),
55    }
56}
57
58/// `rdds_guard_condition_set_trigger_value`.
59#[unsafe(no_mangle)]
60pub unsafe extern "C" fn rdds_guard_condition_set_trigger_value(
61    c: *const RddsCondition,
62    value: bool,
63) -> Status {
64    guard(|| {
65        let c = unsafe { obj(c, "the condition") }?;
66        guard_condition(c)?.set_trigger_value(value);
67        Ok(())
68    })
69}
70
71/// `rdds_status_condition_get_enabled_statuses`.
72#[unsafe(no_mangle)]
73pub unsafe extern "C" fn rdds_status_condition_get_enabled_statuses(
74    c: *const RddsCondition,
75    out: *mut u32,
76) -> Status {
77    guard(|| {
78        let c = unsafe { obj(c, "the condition") }?;
79        let Condition::Status(s) = &c.0 else {
80            return Err(bad("not a status condition"));
81        };
82        unsafe { set(out, s.enabled_statuses().0) }
83    })
84}
85
86/// `rdds_status_condition_set_enabled_statuses`.
87#[unsafe(no_mangle)]
88pub unsafe extern "C" fn rdds_status_condition_set_enabled_statuses(
89    c: *const RddsCondition,
90    mask: u32,
91) -> Status {
92    guard(|| {
93        let c = unsafe { obj(c, "the condition") }?;
94        let Condition::Status(s) = &c.0 else {
95            return Err(bad("not a status condition"));
96        };
97        s.set_enabled_statuses(StatusMask(mask));
98        Ok(())
99    })
100}
101
102/// `rdds_waitset_new`.
103#[unsafe(no_mangle)]
104pub unsafe extern "C" fn rdds_waitset_new(out: *mut *mut WaitSet) -> Status {
105    guard(|| unsafe { put(out, WaitSet::new()) })
106}
107
108/// `rdds_waitset_release`.
109#[unsafe(no_mangle)]
110pub unsafe extern "C" fn rdds_waitset_release(w: *mut WaitSet) {
111    unsafe { release(w) }
112}
113
114/// `rdds_waitset_attach_condition`.
115#[unsafe(no_mangle)]
116pub unsafe extern "C" fn rdds_waitset_attach_condition(
117    w: *const WaitSet,
118    c: *const RddsCondition,
119) -> Status {
120    guard(|| {
121        let w = unsafe { obj(w, "the waitset") }?;
122        let c = unsafe { obj(c, "the condition") }?;
123        Ok(w.attach_condition(c.0.clone())?)
124    })
125}
126
127/// `rdds_waitset_detach_condition`.
128#[unsafe(no_mangle)]
129pub unsafe extern "C" fn rdds_waitset_detach_condition(
130    w: *const WaitSet,
131    c: *const RddsCondition,
132) -> Status {
133    guard(|| {
134        let w = unsafe { obj(w, "the waitset") }?;
135        let c = unsafe { obj(c, "the condition") }?;
136        Ok(w.detach_condition(c.0.clone())?)
137    })
138}
139
140/// `rdds_waitset_wait`: until a condition is true, or `RETCODE_TIMEOUT`.
141/// The indices (in the order the conditions were attached, counting
142/// those still attached) of the true ones go into `active`, up to
143/// `cap`, and how many there are into `*len`.
144#[unsafe(no_mangle)]
145pub unsafe extern "C" fn rdds_waitset_wait(
146    w: *const WaitSet,
147    timeout: RddsDuration,
148    active: *mut usize,
149    cap: usize,
150    len: *mut usize,
151) -> Status {
152    guard(|| {
153        let w = unsafe { obj(w, "the waitset") }?;
154        let t = timeout_of(timeout)?;
155        let on = w.wait(t)?;
156        let all = w.conditions();
157        let idx: Vec<usize> = all
158            .iter()
159            .enumerate()
160            .filter(|(_, c)| on.contains(c))
161            .map(|(i, _)| i)
162            .collect();
163        for (i, x) in idx.iter().take(cap).enumerate() {
164            // SAFETY: room for `cap` (the header).
165            unsafe { active.add(i).write(*x) };
166        }
167        unsafe { set(len, idx.len()) }
168    })
169}
170
171fn timeout_of(d: RddsDuration) -> Res<std::time::Duration> {
172    if d == crate::entity::DURATION_INFINITE {
173        return Ok(std::time::Duration::from_secs(u32::MAX.into()));
174    }
175    if d.sec < 0 || d.nanosec >= 1_000_000_000 {
176        return Err(bad("a duration out of range"));
177    }
178    Ok(std::time::Duration::new(d.sec as u64, d.nanosec))
179}