Skip to main content

Structure Decoding

Overview​

In OPC UA 1.04+, put the field definitions of each custom structure into the DataType node's DataTypeDefinition attribute (AttributeId=27, Part 5 §6.4.2). The SDK implements the complete protocol layer.

API​

The decoder handle type is DarraUa_StructDecoderHandle (opaque uint32_t, UA_INVALID_STRUCT_DECODER_HANDLE = 0); it internally caches DataTypeDefinition entries that have already been read.

FunctionCategoryReading and WritingNote
DarraUa_StructDecoder_New(session, &out_handle)Structure—Create a decoder, bind a session
DarraUa_StructDecoder_Free(h)Release—Release Decoder + Define Cache; NULL-Safe
DarraUa_StructDecoder_ClearCache(h)Methods—Clear the DataTypeDefinition cache
DarraUa_StructDecoder_CachedCount(h)MethodsReadNumber of cached struct definitions (uint32_t)
DarraUa_StructDecoder_Decode(h, v, &out_st)MethodsReadDecode ExtensionObject Variant
DarraUa_StructDecoder_DecodeBody(h, body, body_len, data_type_node_id, &out_st)MethodsReadDirectly add bytes to the body + DataType NodeId
DarraUa_DecodedStruct_Free(st)Release—Recursive release; NULL-safe
DarraUa_DecodedStruct_FindField(st, field_name)IndexReadSearch by field name; returns NULL if the value does not exist.

The decoding result is DarraUa_DecodedStruct (fields / fields_count field array + type_id / status_text). Each DarraUa_DecodedField is a value combination of name+ DarraUa_FieldValueKind kind +; there are 12 values in total: UA_FIELD_VALUE_NULL / _BOOLEAN / _INT64 / _UINT64 / _DOUBLE / _STRING / _BYTES / _DATETIME / _GUID / _NODE_ID_STR / _NESTED_STRUCT / _ARRAY.

Related Structures:

typedef enum {
UA_STRUCT_KIND_STRUCTURE = 0, // Regular
UA_STRUCT_KIND_STRUCTURE_WITH_SUBTYPED_VALUES = 1, // Subtype
UA_STRUCT_KIND_STRUCTURE_WITH_OPTIONAL_FIELDS = 2, // Includes optional (pre-installed EncodingMask)
UA_STRUCT_KIND_UNION = 3, // Union (prefix selector)
UA_STRUCT_KIND_UNION_WITH_SUBTYPED_VALUES = 4 // Union + Subtype
} DarraUa_StructureKind;

Code Example​

#include <opcua_struct_decoder.h>

DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
DarraUa_SessionHandle ua = 0;
DarraUa_Session_Create(&cfg, &ua);
DarraUa_Session_Connect(ua);

DarraUa_StructDecoderHandle dec = UA_INVALID_STRUCT_DECODER_HANDLE;
DarraUa_StructDecoder_New(ua, &dec);

// 1) Read a custom structure
DarraUa_Status st = UA_STATUS_GOOD;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
ua, "ns=2;s=Robot.CurrentPose", UA_ATTR_VALUE, &st);

const DarraUa_Variant* v = DarraUa_DataValue_GetValue(dv);
DarraUa_DecodedStruct* pose = NULL;
DarraUa_StructDecoder_Decode(dec, v, &pose);

const DarraUa_DecodedField* x_val =
DarraUa_DecodedStruct_FindField(pose, "X");
if (x_val && x_val->kind == UA_FIELD_VALUE_DOUBLE)
printf("x = %f\n", x_val->value.v_dbl);

// 2) Nesting
const DarraUa_DecodedField* quat_val =
DarraUa_DecodedStruct_FindField(pose, "Quaternion");
if (quat_val && quat_val->kind == UA_FIELD_VALUE_NESTED_STRUCT) {
const DarraUa_DecodedField* w_val =
DarraUa_DecodedStruct_FindField(quat_val->value.v_nested, "W");
if (w_val && w_val->kind == UA_FIELD_VALUE_DOUBLE)
printf("qw = %f\n", w_val->value.v_dbl);
}

// 3) Full-field traversal
for (size_t i = 0; i < pose->fields_count; ++i)
printf("%s = ...\n", pose->fields[i].name);

// 4) Decode directly when raw bytes are available (body + DataType ) NodeId
const uint8_t body[] = { /* ExtensionObject body */ };
DarraUa_DecodedStruct* pose2 = NULL;
DarraUa_StructDecoder_DecodeBody(
dec, body, sizeof(body), "ns=2;i=3001", &pose2);
DarraUa_DecodedStruct_Free(pose2);

DarraUa_DecodedStruct_Free(pose);
DarraUa_DataValue_Delete(dv);
DarraUa_StructDecoder_Free(dec);
DarraUa_Session_Close(ua);

Implementation Constraints​

  • When an ExtensionObject's bytes cannot be retrieved, Decode returns UA_STATUS_NOT_IMPLEMENTED; out_st may carry status_text explaining the reason.
  • If raw bytes are already available, use DecodeBody to decode directly (body + DataType NodeId)
  • Skip DiagnosticInfo

Best Practices​

  • Share a decoder; reuse the field table cache
  • Server-side: DataType Make changes DarraUa_StructDecoder_ClearCache, then DarraUa_StructDecoder_CachedCount monitor the cache size.
  • Nested Structures kind == UA_FIELD_VALUE_NESTED_STRUCT Followed by value.v_nested Recursive Iteration
  • Union Watch DarraUa_StructureKind UA_STRUCT_KIND_UNION

Cross-Language Comparison​

C#PythonJavaC++RustC
StructDecoderStructDecoderStructDecoderStructDecoderStructDecoderDarraUa_StructDecoderHandle
DecodedecodedecodeDecodedecodeDarraUa_StructDecoder_Decode
Dictionary<string, object>dictMap<String, Object>std::unordered_map<std::string, std::any>HashMap<String, DecodedValue>DarraUa_DecodedStruct

Next Step​