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.
| Function | Category | Reading and Writing | Note |
|---|---|---|---|
| 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) | Methods | Read | Number of cached struct definitions (uint32_t) |
| DarraUa_StructDecoder_Decode(h, v, &out_st) | Methods | Read | Decode ExtensionObject Variant |
| DarraUa_StructDecoder_DecodeBody(h, body, body_len, data_type_node_id, &out_st) | Methods | Read | Directly add bytes to the body + DataType NodeId |
| DarraUa_DecodedStruct_Free(st) | Release | — | Recursive release; NULL-safe |
| DarraUa_DecodedStruct_FindField(st, field_name) | Index | Read | Search 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_stmay carrystatus_textexplaining 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, thenDarraUa_StructDecoder_CachedCountmonitor the cache size. - Nested Structures
kind == UA_FIELD_VALUE_NESTED_STRUCTFollowed byvalue.v_nestedRecursive Iteration - Union Watch
DarraUa_StructureKindUA_STRUCT_KIND_UNION
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| StructDecoder | StructDecoder | StructDecoder | StructDecoder | StructDecoder | DarraUa_StructDecoderHandle |
| Decode | decode | decode | Decode | decode | DarraUa_StructDecoder_Decode |
| Dictionary<string, object> | dict | Map<String, Object> | std::unordered_map<std::string, std::any> | HashMap<String, DecodedValue> | DarraUa_DecodedStruct |