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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! DCPS-Fehlertypen. An OMG DDS 1.4 §2.2.2.1 `ReturnCode_t` angelehnt,
//! aber als Rust-Result<T, DdsError> — deutlich angenehmer als
//! sprayed-error-codes.
extern crate alloc;
use alloc::string::String;
/// Fehler aus DCPS-Operationen. Halbwegs analog zum Spec-
/// `ReturnCode_t`-Enum; wir lassen allerdings `RETCODE_OK` weg
/// (stattdessen `Result::Ok`) und mergen `BAD_PARAMETER` +
/// `PRECONDITION_NOT_MET` wo die Unterscheidung nichts hilft.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DdsError {
/// Ungueltiger Parameter (z.B. leerer Topic-Name).
BadParameter {
/// Welcher Parameter, kurz.
what: &'static str,
},
/// Operation paesst nicht zum Entity-Zustand (z.B. `write` auf
/// einen DataWriter, dessen Participant beendet wurde).
PreconditionNotMet {
/// Kurze Beschreibung.
reason: &'static str,
},
/// Unterliegende Wire/CDR-Operation ist gefehlschlagen.
WireError {
/// Nachricht, statisch oder dynamisch.
message: String,
},
/// QoS-Policy-Set nicht konsistent. Beispiel: Reliability=
/// Best-Effort + History=KeepAll ohne resource_limits — Spec
/// §2.2.3 nennt einige inkompatible Kombinationen.
InconsistentPolicy {
/// Welche Policy(s) kollidieren.
what: &'static str,
},
/// Transport-Operation fehlgeschlagen.
TransportError {
/// Statisches Label des Fehlers.
label: &'static str,
},
/// Resource-Limit erreicht (max_samples, max_instances, etc.).
OutOfResources {
/// Welches Limit.
what: &'static str,
},
/// Timeout bei blockierender Operation (`take_w_timeout`, etc.).
Timeout,
/// Operation nicht implementiert in v1.2.
Unsupported {
/// Kurz-Beschreibung.
feature: &'static str,
},
/// Operation durch Security-Policy untersagt — DDS-Security 1.2
/// §7.3.25, DCPS 1.4 §2.2.2.1.1 ReturnCode_t = NOT_ALLOWED_BY_SECURITY.
/// Der Permissions-/Access-Plugin hat das Lesen/Schreiben auf einem
/// geschuetzten Topic abgelehnt; der Caller darf nicht erfahren,
/// **welches** Permissions-Detail die Ursache war (Information-Leak).
NotAllowedBySecurity {
/// Kurze, sicherheits-unkritische Beschreibung.
what: &'static str,
},
/// `IllegalOperation` — die Operation passt strukturell nicht
/// (z.B. write() auf einen DataReader). DCPS 1.4 §2.2.2.1.1
/// ReturnCode_t = ILLEGAL_OPERATION.
IllegalOperation {
/// Was nicht geht.
what: &'static str,
},
/// `ImmutablePolicy` — set_qos auf eine post-enable immutable QoS-
/// Policy. DCPS 1.4 §2.2.2.1.1 ReturnCode_t = IMMUTABLE_POLICY.
ImmutablePolicy {
/// Welche Policy.
policy: &'static str,
},
/// `AlreadyDeleted` — Operation auf einer geloeschten Entity.
/// DCPS 1.4 §2.2.2.1.1 ReturnCode_t = ALREADY_DELETED.
AlreadyDeleted,
/// `NoData` — read/take ohne neue Samples. DCPS 1.4 §2.2.2.1.1
/// ReturnCode_t = NO_DATA. Wird in der Praxis oft als leerer
/// Sample-Vec zurueckgegeben statt als Error.
NoData,
/// `Error` — generischer, nicht-spezifischer Fehler. DCPS 1.4
/// §2.2.2.1.1 ReturnCode_t = ERROR. Letzte Wahl wenn keine
/// spezifischere Variante passt; vermeidet Information-Leak
/// gegenueber dem Caller.
Other {
/// Kurze Beschreibung.
reason: &'static str,
},
/// `NotEnabled` — Operation auf einer Entity, die noch nicht via
/// `enable()` aktiviert wurde. DCPS 1.4 §2.2.2.1.1 ReturnCode_t =
/// NOT_ENABLED. §2.1.2 — Entities sind nach `create_*` disabled
/// bis `enable()`; viele Ops MUESSEN dann NotEnabled liefern.
NotEnabled,
}
impl core::fmt::Display for DdsError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BadParameter { what } => write!(f, "bad parameter: {what}"),
Self::PreconditionNotMet { reason } => write!(f, "precondition not met: {reason}"),
Self::WireError { message } => write!(f, "wire/cdr error: {message}"),
Self::InconsistentPolicy { what } => write!(f, "inconsistent qos policy: {what}"),
Self::TransportError { label } => write!(f, "transport error: {label}"),
Self::OutOfResources { what } => write!(f, "out of resources: {what}"),
Self::Timeout => f.write_str("dds timeout"),
Self::Unsupported { feature } => write!(f, "not supported in v1.2: {feature}"),
Self::NotAllowedBySecurity { what } => {
write!(f, "not allowed by security policy: {what}")
}
Self::IllegalOperation { what } => write!(f, "illegal operation: {what}"),
Self::ImmutablePolicy { policy } => {
write!(f, "qos policy is immutable post-enable: {policy}")
}
Self::AlreadyDeleted => f.write_str("entity already deleted"),
Self::NoData => f.write_str("no data available"),
Self::Other { reason } => write!(f, "dds error: {reason}"),
Self::NotEnabled => f.write_str("entity not enabled"),
}
}
}
/// OMG DDS 1.4 `ReturnCode_t`-Werte. Disjoint mit `Result::Ok` —
/// `RETCODE_OK = 0` ist nicht in dieser Liste, weil Ok via
/// `Result::Ok` repraesentiert wird.
pub mod return_code {
/// RETCODE_OK = 0 (nur fuer Wire-Mapping; Rust-API nutzt `Ok`).
pub const OK: i32 = 0;
/// RETCODE_ERROR.
pub const ERROR: i32 = 1;
/// RETCODE_UNSUPPORTED.
pub const UNSUPPORTED: i32 = 2;
/// RETCODE_BAD_PARAMETER.
pub const BAD_PARAMETER: i32 = 3;
/// RETCODE_PRECONDITION_NOT_MET.
pub const PRECONDITION_NOT_MET: i32 = 4;
/// RETCODE_OUT_OF_RESOURCES.
pub const OUT_OF_RESOURCES: i32 = 5;
/// RETCODE_NOT_ENABLED.
pub const NOT_ENABLED: i32 = 6;
/// RETCODE_IMMUTABLE_POLICY.
pub const IMMUTABLE_POLICY: i32 = 7;
/// RETCODE_INCONSISTENT_POLICY.
pub const INCONSISTENT_POLICY: i32 = 8;
/// RETCODE_ALREADY_DELETED.
pub const ALREADY_DELETED: i32 = 9;
/// RETCODE_TIMEOUT.
pub const TIMEOUT: i32 = 10;
/// RETCODE_NO_DATA.
pub const NO_DATA: i32 = 11;
/// RETCODE_ILLEGAL_OPERATION.
pub const ILLEGAL_OPERATION: i32 = 12;
/// RETCODE_NOT_ALLOWED_BY_SECURITY.
pub const NOT_ALLOWED_BY_SECURITY: i32 = 13;
}
impl DdsError {
/// Mappt den Fehler auf den OMG `ReturnCode_t`-Integer-Wert
/// (DCPS 1.4 §2.2.2.1.1). `WireError` und `TransportError` werden
/// auf RETCODE_ERROR (1) abgebildet — das ist Spec-konform: beide
/// sind unspezifizierte Implementations-Fehler.
#[must_use]
pub fn as_return_code(&self) -> i32 {
match self {
Self::BadParameter { .. } => return_code::BAD_PARAMETER,
Self::PreconditionNotMet { .. } => return_code::PRECONDITION_NOT_MET,
Self::WireError { .. } | Self::TransportError { .. } | Self::Other { .. } => {
return_code::ERROR
}
Self::InconsistentPolicy { .. } => return_code::INCONSISTENT_POLICY,
Self::OutOfResources { .. } => return_code::OUT_OF_RESOURCES,
Self::Timeout => return_code::TIMEOUT,
Self::Unsupported { .. } => return_code::UNSUPPORTED,
Self::NotAllowedBySecurity { .. } => return_code::NOT_ALLOWED_BY_SECURITY,
Self::IllegalOperation { .. } => return_code::ILLEGAL_OPERATION,
Self::ImmutablePolicy { .. } => return_code::IMMUTABLE_POLICY,
Self::AlreadyDeleted => return_code::ALREADY_DELETED,
Self::NoData => return_code::NO_DATA,
Self::NotEnabled => return_code::NOT_ENABLED,
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DdsError {}
/// Kurzform fuer DCPS-Ergebnisse.
pub type Result<T> = core::result::Result<T, DdsError>;
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
// ---- §2.2.1.1.x ReturnCode_t Mapping ----
#[test]
fn rc_error_maps_for_wire_and_transport_and_other() {
// Spec §2.2.1.1.2 RC ERROR — generischer Fehler.
assert_eq!(
DdsError::WireError {
message: "x".into()
}
.as_return_code(),
return_code::ERROR
);
assert_eq!(
DdsError::TransportError { label: "y" }.as_return_code(),
return_code::ERROR
);
assert_eq!(
DdsError::Other { reason: "z" }.as_return_code(),
return_code::ERROR
);
}
#[test]
fn rc_unsupported_maps() {
assert_eq!(
DdsError::Unsupported { feature: "f" }.as_return_code(),
return_code::UNSUPPORTED
);
}
#[test]
fn rc_bad_parameter_maps() {
assert_eq!(
DdsError::BadParameter { what: "p" }.as_return_code(),
return_code::BAD_PARAMETER
);
}
#[test]
fn rc_precondition_not_met_maps() {
assert_eq!(
DdsError::PreconditionNotMet { reason: "r" }.as_return_code(),
return_code::PRECONDITION_NOT_MET
);
}
#[test]
fn rc_out_of_resources_maps() {
assert_eq!(
DdsError::OutOfResources { what: "samples" }.as_return_code(),
return_code::OUT_OF_RESOURCES
);
}
#[test]
fn rc_not_enabled_maps() {
assert_eq!(
DdsError::NotEnabled.as_return_code(),
return_code::NOT_ENABLED
);
}
#[test]
fn rc_immutable_policy_maps() {
assert_eq!(
DdsError::ImmutablePolicy {
policy: "Reliability"
}
.as_return_code(),
return_code::IMMUTABLE_POLICY
);
}
#[test]
fn rc_inconsistent_policy_maps() {
assert_eq!(
DdsError::InconsistentPolicy { what: "x" }.as_return_code(),
return_code::INCONSISTENT_POLICY
);
}
#[test]
fn rc_already_deleted_maps() {
assert_eq!(
DdsError::AlreadyDeleted.as_return_code(),
return_code::ALREADY_DELETED
);
}
#[test]
fn rc_timeout_maps() {
assert_eq!(DdsError::Timeout.as_return_code(), return_code::TIMEOUT);
}
#[test]
fn rc_no_data_maps() {
assert_eq!(DdsError::NoData.as_return_code(), return_code::NO_DATA);
}
#[test]
fn rc_illegal_operation_maps() {
assert_eq!(
DdsError::IllegalOperation {
what: "write_on_reader"
}
.as_return_code(),
return_code::ILLEGAL_OPERATION
);
}
#[test]
fn rc_not_allowed_by_security_maps() {
assert_eq!(
DdsError::NotAllowedBySecurity { what: "topic" }.as_return_code(),
return_code::NOT_ALLOWED_BY_SECURITY
);
}
#[test]
fn rc_constants_have_spec_values() {
// OMG DDS 1.4 §2.2.2.1.1 numerische Spec-Werte. Ein Drift hier
// bricht jeden Wire-Mapping-Test in C++/Java-Bridges.
assert_eq!(return_code::OK, 0);
assert_eq!(return_code::ERROR, 1);
assert_eq!(return_code::UNSUPPORTED, 2);
assert_eq!(return_code::BAD_PARAMETER, 3);
assert_eq!(return_code::PRECONDITION_NOT_MET, 4);
assert_eq!(return_code::OUT_OF_RESOURCES, 5);
assert_eq!(return_code::NOT_ENABLED, 6);
assert_eq!(return_code::IMMUTABLE_POLICY, 7);
assert_eq!(return_code::INCONSISTENT_POLICY, 8);
assert_eq!(return_code::ALREADY_DELETED, 9);
assert_eq!(return_code::TIMEOUT, 10);
assert_eq!(return_code::NO_DATA, 11);
assert_eq!(return_code::ILLEGAL_OPERATION, 12);
assert_eq!(return_code::NOT_ALLOWED_BY_SECURITY, 13);
}
#[test]
fn rc_display_does_not_leak_security_details() {
// §2.2.2.1.1 NOT_ALLOWED_BY_SECURITY — Caller darf nicht den
// Permissions-Detail-Grund erfahren (Information-Leak).
let e = DdsError::NotAllowedBySecurity { what: "Topic A" };
let s = format!("{e}");
assert!(s.contains("not allowed by security"));
assert!(s.contains("Topic A"));
// Sollte NICHT konkrete Permissions-Internals erwaehnen.
assert!(!s.to_lowercase().contains("permission file"));
assert!(!s.to_lowercase().contains("ca cert"));
}
}