oxictl 0.1.1

Pure Rust Real-Time Control Systems Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! QoS endpoint compatibility matching per OMG DDS 1.4 §2.2.3.

use heapless::Vec;

use crate::protocol::dds::discovery::qos::{
    cmp_duration, DestinationOrderKind, DestinationOrderQosPolicy, DurabilityKind, LivelinessKind,
    OwnershipKind, OwnershipQosPolicy, ReliabilityKind,
};
use crate::protocol::dds::discovery::qos_profile::QosProfile;
use crate::protocol::dds::types::time::Duration;

/// A single QoS incompatibility found during endpoint matching.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IncompatibleQos {
    /// Reliability mismatch: reader requires stronger guarantee than writer offers.
    Reliability {
        reader: ReliabilityKind,
        writer: ReliabilityKind,
    },
    /// Durability mismatch: reader requires stronger persistence than writer offers.
    Durability {
        reader: DurabilityKind,
        writer: DurabilityKind,
    },
    /// Deadline mismatch: reader's required period is shorter than writer's offered period.
    Deadline {
        reader_period: Duration,
        writer_period: Duration,
    },
    /// Liveliness kind mismatch: reader requires stronger kind than writer offers.
    LivelinessKind {
        reader: LivelinessKind,
        writer: LivelinessKind,
    },
    /// Liveliness lease mismatch: reader's required lease is shorter than writer's offered lease.
    LivelinessLease {
        reader_lease: Duration,
        writer_lease: Duration,
    },
    /// Ownership kind mismatch: reader and writer must use the same ownership kind.
    Ownership {
        reader: OwnershipKind,
        writer: OwnershipKind,
    },
    /// DestinationOrder mismatch: reader requires BySourceTimestamp but writer only offers ByReceptionTimestamp.
    DestinationOrder {
        reader: DestinationOrderKind,
        writer: DestinationOrderKind,
    },
}

/// Check whether `reader` and `writer` QoS profiles are compatible.
///
/// Returns `Ok(())` if all 4 RxO policies are compatible.
/// Returns `Err(violations)` with every incompatibility found (never short-circuits).
/// The returned [`Vec`] has capacity 5 (one slot per distinct violation type).
pub fn match_endpoint_qos(
    reader: &QosProfile,
    writer: &QosProfile,
) -> Result<(), Vec<IncompatibleQos, 5>> {
    let mut v: Vec<IncompatibleQos, 5> = Vec::new();

    // Reliability: reader.kind <= writer.kind
    if !reader.reliability.is_compatible_with(&writer.reliability) {
        let _ = v.push(IncompatibleQos::Reliability {
            reader: reader.reliability.kind,
            writer: writer.reliability.kind,
        });
    }

    // Durability: reader.kind <= writer.kind
    if !reader.durability.is_compatible_with(&writer.durability) {
        let _ = v.push(IncompatibleQos::Durability {
            reader: reader.durability.kind,
            writer: writer.durability.kind,
        });
    }

    // Deadline: reader.period >= writer.period
    if !reader.deadline.is_compatible_with(&writer.deadline) {
        let _ = v.push(IncompatibleQos::Deadline {
            reader_period: reader.deadline.period,
            writer_period: writer.deadline.period,
        });
    }

    // Liveliness: report kind and lease mismatches as separate entries so callers can distinguish.
    let lv_kind_ok = (reader.liveliness.kind as i32) <= (writer.liveliness.kind as i32);
    let lv_lease_ok = cmp_duration(
        reader.liveliness.lease_duration,
        writer.liveliness.lease_duration,
    ) != core::cmp::Ordering::Less;

    if !lv_kind_ok {
        let _ = v.push(IncompatibleQos::LivelinessKind {
            reader: reader.liveliness.kind,
            writer: writer.liveliness.kind,
        });
    }
    if !lv_lease_ok {
        let _ = v.push(IncompatibleQos::LivelinessLease {
            reader_lease: reader.liveliness.lease_duration,
            writer_lease: writer.liveliness.lease_duration,
        });
    }

    if v.is_empty() {
        Ok(())
    } else {
        Err(v)
    }
}

/// Extended endpoint matcher that also checks `Ownership` and `DestinationOrder` RxO policies.
///
/// The base 5 checks (Reliability, Durability, Deadline, Liveliness ×2) are delegated to
/// [`match_endpoint_qos`].  The two additional policies are passed as explicit arguments
/// because they are not yet embedded in [`QosProfile`].
///
/// Returns `Ok(())` when all policies are compatible.  Returns `Err(violations)` with
/// every incompatibility found — never short-circuits.
/// The returned [`Vec`] has capacity 7 (5 from base checks + 2 new policies).
///
/// # Note on return size
/// The heapless fixed-capacity array cannot be heap-allocated in no_std contexts, so the
/// large-err lint is suppressed here — boxing is not available without `alloc`.
#[allow(clippy::result_large_err)]
pub fn match_endpoint_qos_extended(
    reader: &QosProfile,
    writer: &QosProfile,
    reader_ownership: &OwnershipQosPolicy,
    writer_ownership: &OwnershipQosPolicy,
    reader_dest_order: &DestinationOrderQosPolicy,
    writer_dest_order: &DestinationOrderQosPolicy,
) -> Result<(), Vec<IncompatibleQos, 7>> {
    let mut v: Vec<IncompatibleQos, 7> = Vec::new();

    // Run the base 5-slot RxO checks (Reliability, Durability, Deadline, Liveliness ×2).
    if let Err(base_errs) = match_endpoint_qos(reader, writer) {
        for e in base_errs.iter() {
            let _ = v.push(*e);
        }
    }

    // Ownership: both sides must have the same kind.
    if !reader_ownership.is_compatible_with(writer_ownership) {
        let _ = v.push(IncompatibleQos::Ownership {
            reader: reader_ownership.kind,
            writer: writer_ownership.kind,
        });
    }

    // DestinationOrder: reader.kind <= writer.kind.
    if !reader_dest_order.is_compatible_with(writer_dest_order) {
        let _ = v.push(IncompatibleQos::DestinationOrder {
            reader: reader_dest_order.kind,
            writer: writer_dest_order.kind,
        });
    }

    if v.is_empty() {
        Ok(())
    } else {
        Err(v)
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::dds::discovery::qos::{
        DeadlineQosPolicy, DestinationOrderQosPolicy, DurabilityQosPolicy, HistoryKind,
        HistoryQosPolicy, LivelinessQosPolicy, OwnershipQosPolicy, ReliabilityQosPolicy,
    };
    use crate::protocol::dds::discovery::qos_profile::QosProfile;
    use crate::protocol::dds::types::time::Duration;

    const DURATION_INFINITE: Duration = Duration {
        seconds: 0x7FFF_FFFF,
        fraction: 0xFFFF_FFFF,
    };

    fn dur(seconds: i32, fraction: u32) -> Duration {
        Duration { seconds, fraction }
    }

    fn base_profile() -> QosProfile {
        QosProfile::ros2_default()
    }

    // ── Reliability tests ────────────────────────────────────────────────────

    #[test]
    fn reliability_reader_reliable_writer_best_effort_incompatible() {
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.reliability = ReliabilityQosPolicy::reliable();
        writer.reliability = ReliabilityQosPolicy::best_effort();
        let result = match_endpoint_qos(&reader, &writer);
        let errs = result.expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::Reliability { .. })));
    }

    #[test]
    fn reliability_reader_best_effort_writer_reliable_compatible() {
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.reliability = ReliabilityQosPolicy::best_effort();
        writer.reliability = ReliabilityQosPolicy::reliable();
        assert!(match_endpoint_qos(&reader, &writer).is_ok());
    }

    // ── Durability tests ─────────────────────────────────────────────────────

    #[test]
    fn durability_reader_transient_local_writer_volatile_incompatible() {
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.durability = DurabilityQosPolicy {
            kind: DurabilityKind::TransientLocal,
        };
        writer.durability = DurabilityQosPolicy {
            kind: DurabilityKind::Volatile,
        };
        let errs = match_endpoint_qos(&reader, &writer).expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::Durability { .. })));
    }

    #[test]
    fn durability_reader_volatile_writer_persistent_compatible() {
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.durability = DurabilityQosPolicy {
            kind: DurabilityKind::Volatile,
        };
        writer.durability = DurabilityQosPolicy {
            kind: DurabilityKind::Persistent,
        };
        assert!(match_endpoint_qos(&reader, &writer).is_ok());
    }

    // ── Deadline tests ───────────────────────────────────────────────────────

    #[test]
    fn deadline_reader_period_less_than_writer_incompatible() {
        // reader requires 1s cadence; writer only offers 2s cadence → incompatible
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.deadline = DeadlineQosPolicy { period: dur(1, 0) };
        writer.deadline = DeadlineQosPolicy { period: dur(2, 0) };
        let errs = match_endpoint_qos(&reader, &writer).expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::Deadline { .. })));
    }

    #[test]
    fn deadline_reader_period_equal_writer_compatible() {
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.deadline = DeadlineQosPolicy { period: dur(1, 0) };
        writer.deadline = DeadlineQosPolicy { period: dur(1, 0) };
        assert!(match_endpoint_qos(&reader, &writer).is_ok());
    }

    #[test]
    fn deadline_reader_period_greater_writer_compatible() {
        // reader period 2s, writer period 1s → reader is less strict → compatible
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.deadline = DeadlineQosPolicy { period: dur(2, 0) };
        writer.deadline = DeadlineQosPolicy { period: dur(1, 0) };
        assert!(match_endpoint_qos(&reader, &writer).is_ok());
    }

    // ── Liveliness tests ─────────────────────────────────────────────────────

    #[test]
    fn liveliness_kind_reader_manual_topic_writer_automatic_incompatible() {
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.liveliness = LivelinessQosPolicy {
            kind: LivelinessKind::ManualByTopic,
            lease_duration: DURATION_INFINITE,
        };
        writer.liveliness = LivelinessQosPolicy {
            kind: LivelinessKind::Automatic,
            lease_duration: DURATION_INFINITE,
        };
        let errs = match_endpoint_qos(&reader, &writer).expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::LivelinessKind { .. })));
    }

    #[test]
    fn liveliness_kind_compatible_lease_too_short_on_writer_incompatible() {
        // reader lease = 100ms, writer lease = 200ms → reader wants ≥ 200ms from writer but
        // writer only offers 200ms while reader's required is 100ms.
        // Reader lease 100ms < writer lease 200ms → reader needs shorter, writer offers longer.
        // RxO: reader.lease >= writer.lease must hold; 100ms < 200ms → incompatible.
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.liveliness = LivelinessQosPolicy {
            kind: LivelinessKind::Automatic,
            lease_duration: dur(0, 0x1999_9999), // 100ms
        };
        writer.liveliness = LivelinessQosPolicy {
            kind: LivelinessKind::Automatic,
            lease_duration: dur(0, 0x3333_3333), // ~200ms
        };
        let errs = match_endpoint_qos(&reader, &writer).expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::LivelinessLease { .. })));
    }

    #[test]
    fn liveliness_lease_infinite_reader_finite_writer_compatible() {
        // reader lease = INF >= writer lease = 1s → compatible
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.liveliness = LivelinessQosPolicy {
            kind: LivelinessKind::Automatic,
            lease_duration: DURATION_INFINITE,
        };
        writer.liveliness = LivelinessQosPolicy {
            kind: LivelinessKind::Automatic,
            lease_duration: dur(1, 0),
        };
        assert!(match_endpoint_qos(&reader, &writer).is_ok());
    }

    // ── Multi-violation and ok tests ─────────────────────────────────────────

    #[test]
    fn match_returns_all_violations() {
        // Reliable reader + BestEffort writer AND TransientLocal reader + Volatile writer → 2 violations
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.reliability = ReliabilityQosPolicy::reliable();
        writer.reliability = ReliabilityQosPolicy::best_effort();
        reader.durability = DurabilityQosPolicy {
            kind: DurabilityKind::TransientLocal,
        };
        writer.durability = DurabilityQosPolicy {
            kind: DurabilityKind::Volatile,
        };
        let errs = match_endpoint_qos(&reader, &writer).expect_err("should have violations");
        assert_eq!(errs.len(), 2);
    }

    #[test]
    fn match_compatible_returns_ok() {
        let p = QosProfile::ros2_default();
        assert!(match_endpoint_qos(&p, &p).is_ok());
    }

    // ── History policy test (informational, not RxO) ─────────────────────────

    #[test]
    fn history_policy_not_checked_for_compatibility() {
        // Different history settings do not affect match result
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.history = HistoryQosPolicy {
            kind: HistoryKind::KeepAll,
            depth: 0,
        };
        writer.history = HistoryQosPolicy {
            kind: HistoryKind::KeepLast,
            depth: 1,
        };
        assert!(match_endpoint_qos(&reader, &writer).is_ok());
    }

    // ── match_endpoint_qos_extended tests ────────────────────────────────────

    fn shared_ownership() -> OwnershipQosPolicy {
        OwnershipQosPolicy::shared()
    }

    fn exclusive_ownership() -> OwnershipQosPolicy {
        OwnershipQosPolicy::exclusive()
    }

    fn by_reception() -> DestinationOrderQosPolicy {
        DestinationOrderQosPolicy::by_reception_timestamp()
    }

    fn by_source() -> DestinationOrderQosPolicy {
        DestinationOrderQosPolicy::by_source_timestamp()
    }

    #[test]
    fn extended_match_ownership_incompatible() {
        let p = base_profile();
        // reader=Exclusive, writer=Shared → ownership mismatch
        let errs = match_endpoint_qos_extended(
            &p,
            &p,
            &exclusive_ownership(),
            &shared_ownership(),
            &by_reception(),
            &by_reception(),
        )
        .expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::Ownership { .. })));
    }

    #[test]
    fn extended_match_destination_order_incompatible() {
        let p = base_profile();
        // reader=BySource, writer=ByReception → destination order mismatch
        let errs = match_endpoint_qos_extended(
            &p,
            &p,
            &shared_ownership(),
            &shared_ownership(),
            &by_source(),
            &by_reception(),
        )
        .expect_err("should be incompatible");
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::DestinationOrder { .. })));
    }

    #[test]
    fn extended_match_base_and_extra_violations_all_returned() {
        // Reliable reader + BestEffort writer (base violation)
        // + Exclusive reader + Shared writer (ownership violation)
        // + BySource reader + ByReception writer (dest-order violation)
        let mut reader = base_profile();
        let mut writer = base_profile();
        reader.reliability = ReliabilityQosPolicy::reliable();
        writer.reliability = ReliabilityQosPolicy::best_effort();

        let errs = match_endpoint_qos_extended(
            &reader,
            &writer,
            &exclusive_ownership(),
            &shared_ownership(),
            &by_source(),
            &by_reception(),
        )
        .expect_err("should have violations");

        assert!(errs.len() >= 3);
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::Reliability { .. })));
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::Ownership { .. })));
        assert!(errs
            .iter()
            .any(|e| matches!(e, IncompatibleQos::DestinationOrder { .. })));
    }

    #[test]
    fn extended_match_all_compatible() {
        let p = base_profile();
        // reader=Shared, writer=Shared; reader=ByReception, writer=BySource → all compatible
        let result = match_endpoint_qos_extended(
            &p,
            &p,
            &shared_ownership(),
            &shared_ownership(),
            &by_reception(),
            &by_source(),
        );
        assert!(result.is_ok());
    }
}