zerodds-opcua-pubsub 1.0.0-rc.3.1

OPC-UA Pub/Sub Part 14 (UADP) native wire stack: Part-6 binary codec, NetworkMessage/DataSetMessage framing, PubSub config, discovery, SecurityGroup/SKS, transport carriers and a DDS bridge. Pure-Rust no_std + alloc.
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! Dynamic, metadata-driven decoding of RawData DataSet fields whose DataType
//! is a *custom* (non-built-in) structured, enumerated or simple type
//! (OPC-UA Part 6 §5.4 reversible structured encoding, RawData variant of
//! Part 14 §7.2.2.3.4).
//!
//! The RawData encoding carries no per-field type tags, so a custom struct is
//! written inline field-by-field per its
//! [`StructureDefinition`](crate::config::StructureDefinition). The
//! `zerodds-opcua-gateway` `Variant` only models the 25 built-in types, so
//! such values are decoded here into a [`DynamicValue`] tree driven by the
//! [`DataSetMetaData`] DataType descriptions — built-in leaves become
//! [`VariantValue`]s, custom structs become nested [`DynamicValue::Structure`]s.
//!
//! This is the decoding counterpart to the metadata round-trip in
//! [`crate::uadp::discovery`]; together they make custom-typed PubSub DataSets
//! fully usable without a dynamic type system in the gateway.

use alloc::string::String;
use alloc::vec::Vec;

use zerodds_opcua_gateway::data_value::{Variant, VariantValue};
use zerodds_opcua_gateway::node_id::{NodeId, NodeIdentifier};

use crate::binary::{UaReader, builtin_type_from_value, decode_builtin_value};
use crate::config::{
    DataSetMetaData, EnumDescription, SimpleTypeDescription, StructureDescription, StructureType,
};
use crate::error::DecodeError;

/// Guards against pathological / cyclic DataType definitions.
const MAX_DEPTH: u32 = 32;

/// A decoded DataSet field value of arbitrary (built-in or custom) DataType.
#[derive(Debug, Clone, PartialEq)]
pub enum DynamicValue {
    /// A built-in scalar value.
    Scalar(VariantValue),
    /// An array of values (any element kind).
    Array(Vec<DynamicValue>),
    /// A custom structure as ordered named members. A `Union` carries its one
    /// selected member; a null union or an absent optional field is
    /// [`DynamicValue::Null`].
    Structure(Vec<DynamicField>),
    /// A null value (null array, null union, or absent optional field).
    Null,
}

/// One named member of a [`DynamicValue::Structure`].
#[derive(Debug, Clone, PartialEq)]
pub struct DynamicField {
    /// Member name (from the [`StructureField`](crate::config::StructureField)).
    pub name: String,
    /// Member value.
    pub value: DynamicValue,
}

/// Converts a self-describing [`Variant`] (from the Variant/DataValue
/// encodings) into the uniform [`DynamicValue`] view.
#[must_use]
pub fn variant_to_dynamic(v: &Variant) -> DynamicValue {
    if v.value.is_empty() {
        return DynamicValue::Null;
    }
    if v.array_dimensions.is_empty() && v.value.len() == 1 {
        DynamicValue::Scalar(v.value[0].clone())
    } else {
        DynamicValue::Array(v.value.iter().cloned().map(DynamicValue::Scalar).collect())
    }
}

/// Decodes a complete RawData payload against `meta`, producing one
/// [`DynamicField`] per metadata field.
///
/// # Errors
/// [`DecodeError`] if a DataType is not described in `meta`, the payload is
/// truncated, the nesting exceeds `MAX_DEPTH`, or trailing bytes remain.
pub fn decode_raw_dataset(
    bytes: &[u8],
    meta: &DataSetMetaData,
) -> Result<Vec<DynamicField>, DecodeError> {
    let mut r = UaReader::new(bytes);
    let mut out = Vec::with_capacity(meta.fields.len());
    for f in &meta.fields {
        let value = decode_value(&mut r, &f.data_type, f.value_rank, meta, 0)?;
        out.push(DynamicField {
            name: f.name.clone(),
            value,
        });
    }
    if !r.is_empty() {
        return Err(DecodeError::MalformedMessage {
            message: "RawData payload longer than the DataSetMetaData fields describe",
        });
    }
    Ok(out)
}

/// A DataType resolved against the metadata descriptions.
enum Resolved<'a> {
    Builtin(zerodds_opcua_gateway::types::BuiltinTypeKind),
    Structure(&'a StructureDescription),
    Enum(&'a EnumDescription),
    Simple(&'a SimpleTypeDescription),
}

/// Resolves a DataType NodeId to a built-in type or a custom description.
fn resolve_kind<'a>(dt: &NodeId, meta: &'a DataSetMetaData) -> Result<Resolved<'a>, DecodeError> {
    if dt.namespace_index == 0 {
        if let NodeIdentifier::Numeric(id) = dt.identifier_type {
            if (1..=25).contains(&id) {
                return Ok(Resolved::Builtin(builtin_type_from_value(id as u8)?));
            }
        }
    }
    if let Some(s) = meta
        .structure_data_types
        .iter()
        .find(|d| d.data_type_id == *dt)
    {
        return Ok(Resolved::Structure(s));
    }
    if let Some(e) = meta.enum_data_types.iter().find(|d| d.data_type_id == *dt) {
        return Ok(Resolved::Enum(e));
    }
    if let Some(s) = meta
        .simple_data_types
        .iter()
        .find(|d| d.data_type_id == *dt)
    {
        return Ok(Resolved::Simple(s));
    }
    Err(DecodeError::MalformedMessage {
        message: "RawData field references a DataType not described in the DataSetMetaData",
    })
}

/// Decodes a value of `dt` honouring its value rank (scalar vs.
/// length-prefixed array).
fn decode_value(
    r: &mut UaReader<'_>,
    dt: &NodeId,
    value_rank: i32,
    meta: &DataSetMetaData,
    depth: u32,
) -> Result<DynamicValue, DecodeError> {
    if depth > MAX_DEPTH {
        return Err(DecodeError::MalformedMessage {
            message: "custom DataType nesting exceeds the supported depth",
        });
    }
    let resolved = resolve_kind(dt, meta)?;
    if value_rank < 0 {
        decode_one(r, &resolved, meta, depth)
    } else {
        let len = r.read_i32()?;
        if len < 0 {
            return Ok(DynamicValue::Null);
        }
        let mut items = Vec::with_capacity(len as usize);
        for _ in 0..len {
            items.push(decode_one(r, &resolved, meta, depth)?);
        }
        Ok(DynamicValue::Array(items))
    }
}

/// Decodes a single (scalar) value of an already-resolved DataType.
fn decode_one(
    r: &mut UaReader<'_>,
    resolved: &Resolved<'_>,
    meta: &DataSetMetaData,
    depth: u32,
) -> Result<DynamicValue, DecodeError> {
    match resolved {
        Resolved::Builtin(bt) => Ok(DynamicValue::Scalar(decode_builtin_value(r, *bt)?)),
        Resolved::Simple(s) => Ok(DynamicValue::Scalar(decode_builtin_value(
            r,
            builtin_type_from_value(s.builtin_type)?,
        )?)),
        // An enumeration value is encoded as its underlying built-in integer.
        Resolved::Enum(e) => Ok(DynamicValue::Scalar(decode_builtin_value(
            r,
            builtin_type_from_value(e.builtin_type)?,
        )?)),
        Resolved::Structure(sd) => decode_struct(r, sd, meta, depth + 1),
    }
}

/// Decodes a structured value per its
/// [`StructureDefinition`](crate::config::StructureDefinition) kind.
fn decode_struct(
    r: &mut UaReader<'_>,
    sd: &StructureDescription,
    meta: &DataSetMetaData,
    depth: u32,
) -> Result<DynamicValue, DecodeError> {
    let def = &sd.structure_definition;
    match def.structure_type {
        StructureType::Union | StructureType::UnionWithSubtypedValues => {
            // UInt32 switch (1-based selector; 0 = null union).
            let switch = r.read_u32()?;
            if switch == 0 {
                return Ok(DynamicValue::Null);
            }
            let field =
                def.fields
                    .get((switch - 1) as usize)
                    .ok_or(DecodeError::MalformedMessage {
                        message: "union switch value out of range",
                    })?;
            let value = decode_value(r, &field.data_type, field.value_rank, meta, depth)?;
            Ok(DynamicValue::Structure(alloc::vec![DynamicField {
                name: field.name.clone(),
                value,
            }]))
        }
        StructureType::StructureWithOptionalFields => {
            // UInt32 encoding mask: one bit per optional field, in order.
            let mask = r.read_u32()?;
            let mut out = Vec::with_capacity(def.fields.len());
            let mut optional_index = 0u32;
            for field in &def.fields {
                let present = if field.is_optional {
                    let p = (mask >> optional_index) & 1 == 1;
                    optional_index += 1;
                    p
                } else {
                    true
                };
                let value = if present {
                    decode_value(r, &field.data_type, field.value_rank, meta, depth)?
                } else {
                    DynamicValue::Null
                };
                out.push(DynamicField {
                    name: field.name.clone(),
                    value,
                });
            }
            Ok(DynamicValue::Structure(out))
        }
        StructureType::Structure | StructureType::StructureWithSubtypedValues => {
            let mut out = Vec::with_capacity(def.fields.len());
            for field in &def.fields {
                let value = decode_value(r, &field.data_type, field.value_rank, meta, depth)?;
                out.push(DynamicField {
                    name: field.name.clone(),
                    value,
                });
            }
            Ok(DynamicValue::Structure(out))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::binary::{UaEncode, UaWriter};
    use crate::config::{
        EnumDefinition, EnumDescription, EnumField, FieldMetaData, StructureDefinition,
        StructureField,
    };
    use zerodds_opcua_gateway::types::{BuiltinTypeKind, LocalizedText};

    fn loc() -> LocalizedText {
        LocalizedText {
            locale: None,
            text: None,
        }
    }

    fn field(name: &str, data_type: NodeId, value_rank: i32, is_optional: bool) -> StructureField {
        StructureField {
            name: String::from(name),
            description: loc(),
            data_type,
            value_rank,
            array_dimensions: Vec::new(),
            max_string_length: 0,
            is_optional,
        }
    }

    fn struct_desc(
        id: u32,
        ty: StructureType,
        fields: Vec<StructureField>,
    ) -> StructureDescription {
        StructureDescription {
            data_type_id: NodeId::numeric(2, id),
            name: zerodds_opcua_gateway::types::QualifiedName {
                namespace_index: 2,
                name: String::from("T"),
            },
            structure_definition: StructureDefinition {
                default_encoding_id: NodeId::numeric(2, id + 1),
                base_data_type: NodeId::numeric(0, 22),
                structure_type: ty,
                fields,
            },
        }
    }

    /// metadata: one field `p` of custom struct Point{x:Double, y:Double}.
    fn point_meta() -> DataSetMetaData {
        let mut m = DataSetMetaData::new(
            "ds",
            alloc::vec![FieldMetaData {
                name: String::from("p"),
                description: loc(),
                field_flags: 0,
                builtin_type: BuiltinTypeKind::ExtensionObject,
                data_type: NodeId::numeric(2, 4000),
                value_rank: -1,
                array_dimensions: Vec::new(),
                max_string_length: 0,
                data_set_field_id: zerodds_opcua_gateway::types::Guid::from_bytes([0; 16]),
                properties: Vec::new(),
            }],
        );
        m.structure_data_types = alloc::vec![struct_desc(
            4000,
            StructureType::Structure,
            alloc::vec![
                field("x", NodeId::numeric(0, 11), -1, false),
                field("y", NodeId::numeric(0, 11), -1, false),
            ],
        )];
        m
    }

    #[test]
    fn decodes_plain_custom_struct() {
        // RawData: x=1.5, y=-2.0 back to back (no tags).
        let mut w = UaWriter::new();
        VariantValue::Double(1.5).encode(&mut w).unwrap();
        VariantValue::Double(-2.0).encode(&mut w).unwrap();
        let bytes = w.into_vec();

        let fields = decode_raw_dataset(&bytes, &point_meta()).expect("decode");
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].name, "p");
        assert_eq!(
            fields[0].value,
            DynamicValue::Structure(alloc::vec![
                DynamicField {
                    name: String::from("x"),
                    value: DynamicValue::Scalar(VariantValue::Double(1.5)),
                },
                DynamicField {
                    name: String::from("y"),
                    value: DynamicValue::Scalar(VariantValue::Double(-2.0)),
                },
            ])
        );
    }

    #[test]
    fn decodes_optional_fields_via_mask() {
        // Struct Opt{ a:Int32 (mandatory), b:Int32 (optional), c:Int32 (optional) }
        let mut m = DataSetMetaData::new(
            "ds",
            alloc::vec![FieldMetaData {
                name: String::from("o"),
                description: loc(),
                field_flags: 0,
                builtin_type: BuiltinTypeKind::ExtensionObject,
                data_type: NodeId::numeric(2, 4000),
                value_rank: -1,
                array_dimensions: Vec::new(),
                max_string_length: 0,
                data_set_field_id: zerodds_opcua_gateway::types::Guid::from_bytes([0; 16]),
                properties: Vec::new(),
            }],
        );
        m.structure_data_types = alloc::vec![struct_desc(
            4000,
            StructureType::StructureWithOptionalFields,
            alloc::vec![
                field("a", NodeId::numeric(0, 6), -1, false),
                field("b", NodeId::numeric(0, 6), -1, true),
                field("c", NodeId::numeric(0, 6), -1, true),
            ],
        )];

        // mask = 0b01 → b present, c absent. a=10, b=20.
        let mut w = UaWriter::new();
        w.write_u32(0b01);
        VariantValue::Int32(10).encode(&mut w).unwrap();
        VariantValue::Int32(20).encode(&mut w).unwrap();
        let bytes = w.into_vec();

        let fields = decode_raw_dataset(&bytes, &m).expect("decode");
        let DynamicValue::Structure(members) = &fields[0].value else {
            panic!("expected struct");
        };
        assert_eq!(
            members[0].value,
            DynamicValue::Scalar(VariantValue::Int32(10))
        );
        assert_eq!(
            members[1].value,
            DynamicValue::Scalar(VariantValue::Int32(20))
        );
        assert_eq!(members[2].value, DynamicValue::Null);
    }

    #[test]
    fn decodes_union_switch() {
        // Union U{ i:Int32 | d:Double }; switch=2 → d selected.
        let mut m = DataSetMetaData::new(
            "ds",
            alloc::vec![FieldMetaData {
                name: String::from("u"),
                description: loc(),
                field_flags: 0,
                builtin_type: BuiltinTypeKind::ExtensionObject,
                data_type: NodeId::numeric(2, 4000),
                value_rank: -1,
                array_dimensions: Vec::new(),
                max_string_length: 0,
                data_set_field_id: zerodds_opcua_gateway::types::Guid::from_bytes([0; 16]),
                properties: Vec::new(),
            }],
        );
        m.structure_data_types = alloc::vec![struct_desc(
            4000,
            StructureType::Union,
            alloc::vec![
                field("i", NodeId::numeric(0, 6), -1, false),
                field("d", NodeId::numeric(0, 11), -1, false),
            ],
        )];

        let mut w = UaWriter::new();
        w.write_u32(2); // select field 2 (d)
        VariantValue::Double(3.25).encode(&mut w).unwrap();
        let bytes = w.into_vec();

        let fields = decode_raw_dataset(&bytes, &m).expect("decode");
        assert_eq!(
            fields[0].value,
            DynamicValue::Structure(alloc::vec![DynamicField {
                name: String::from("d"),
                value: DynamicValue::Scalar(VariantValue::Double(3.25)),
            }])
        );
    }

    #[test]
    fn decodes_nested_struct_and_array() {
        // Outer{ name:String, points:Point[] }, Point{x:Int32,y:Int32}.
        let mut m = DataSetMetaData::new(
            "ds",
            alloc::vec![FieldMetaData {
                name: String::from("outer"),
                description: loc(),
                field_flags: 0,
                builtin_type: BuiltinTypeKind::ExtensionObject,
                data_type: NodeId::numeric(2, 5000),
                value_rank: -1,
                array_dimensions: Vec::new(),
                max_string_length: 0,
                data_set_field_id: zerodds_opcua_gateway::types::Guid::from_bytes([0; 16]),
                properties: Vec::new(),
            }],
        );
        m.structure_data_types = alloc::vec![
            struct_desc(
                4000,
                StructureType::Structure,
                alloc::vec![
                    field("x", NodeId::numeric(0, 6), -1, false),
                    field("y", NodeId::numeric(0, 6), -1, false),
                ],
            ),
            struct_desc(
                5000,
                StructureType::Structure,
                alloc::vec![
                    field("name", NodeId::numeric(0, 12), -1, false),
                    field("points", NodeId::numeric(2, 4000), 1, false),
                ],
            ),
        ];

        let mut w = UaWriter::new();
        VariantValue::String(String::from("hi"))
            .encode(&mut w)
            .unwrap();
        w.write_i32(2); // points array length
        VariantValue::Int32(1).encode(&mut w).unwrap();
        VariantValue::Int32(2).encode(&mut w).unwrap();
        VariantValue::Int32(3).encode(&mut w).unwrap();
        VariantValue::Int32(4).encode(&mut w).unwrap();
        let bytes = w.into_vec();

        let fields = decode_raw_dataset(&bytes, &m).expect("decode");
        let DynamicValue::Structure(outer) = &fields[0].value else {
            panic!("struct");
        };
        assert_eq!(
            outer[0].value,
            DynamicValue::Scalar(VariantValue::String(String::from("hi")))
        );
        let DynamicValue::Array(points) = &outer[1].value else {
            panic!("array");
        };
        assert_eq!(points.len(), 2);
        assert_eq!(
            points[0],
            DynamicValue::Structure(alloc::vec![
                DynamicField {
                    name: String::from("x"),
                    value: DynamicValue::Scalar(VariantValue::Int32(1)),
                },
                DynamicField {
                    name: String::from("y"),
                    value: DynamicValue::Scalar(VariantValue::Int32(2)),
                },
            ])
        );
    }

    #[test]
    fn decodes_enum_as_underlying_integer() {
        let mut m = DataSetMetaData::new(
            "ds",
            alloc::vec![FieldMetaData {
                name: String::from("color"),
                description: loc(),
                field_flags: 0,
                builtin_type: BuiltinTypeKind::Int32,
                data_type: NodeId::numeric(2, 7000),
                value_rank: -1,
                array_dimensions: Vec::new(),
                max_string_length: 0,
                data_set_field_id: zerodds_opcua_gateway::types::Guid::from_bytes([0; 16]),
                properties: Vec::new(),
            }],
        );
        m.enum_data_types = alloc::vec![EnumDescription {
            data_type_id: NodeId::numeric(2, 7000),
            name: zerodds_opcua_gateway::types::QualifiedName {
                namespace_index: 2,
                name: String::from("Color"),
            },
            enum_definition: EnumDefinition {
                fields: alloc::vec![EnumField {
                    value: 2,
                    display_name: loc(),
                    description: loc(),
                    name: String::from("Green"),
                }],
            },
            builtin_type: 6, // Int32
        }];

        let mut w = UaWriter::new();
        VariantValue::Int32(2).encode(&mut w).unwrap();
        let fields = decode_raw_dataset(&w.into_vec(), &m).expect("decode");
        assert_eq!(
            fields[0].value,
            DynamicValue::Scalar(VariantValue::Int32(2))
        );
    }

    #[test]
    fn unknown_datatype_is_rejected() {
        let mut m = point_meta();
        // Point references no metadata → strip the description.
        m.structure_data_types.clear();
        let bytes = alloc::vec![0u8; 16];
        assert!(matches!(
            decode_raw_dataset(&bytes, &m),
            Err(DecodeError::MalformedMessage { .. })
        ));
    }
}