構造体のデコード
概要
OPC UA 1.04+ では、各カスタム構造体のフィールド定義を DataType ノードの DataTypeDefinition 属性(AttributeId=27、Part 5 §6.4.2)に配置する。SDK は完全なプロトコル層を実装する。
API
デコーダのハンドルタイプは DarraUa_StructDecoderHandle (不透明 uint32_t, UA_INVALID_STRUCT_DECODER_HANDLE = 0) で、内部キャッシュにはすでに読み込まれた DataTypeDefinition が格納されています。
| 関数 | カテゴリ | 読み書き | 説明 |
|---|---|---|---|
| DarraUa_StructDecoder_New(session, &out_handle) | 構造 | — | デコーダーの作成、セッションのバインド |
| DarraUa_StructDecoder_Free(h) | 解放 | — | デコーダー + 定義キャッシュの解放; NULL セーフ |
| DarraUa_StructDecoder_ClearCache(h) | 方法 | — | DataTypeDefinition キャッシュをクリアする |
| DarraUa_StructDecoder_CachedCount(h) | 方法 | 読み取り可 | キャッシュされた構造体の定義数 (uint32_t) |
| DarraUa_StructDecoder_Decode(h, v, &out_st) | 方法 | 読み取り可 | デコード ExtensionObject Variant |
| DarraUa_StructDecoder_DecodeBody(h, body, body_len, data_type_node_id, &out_st) | 方法 | 読み取り可 | body に直接バイト数を追加 + DataType NodeId |
| DarraUa_DecodedStruct_Free(st) | 解放 | — | 再帰的な解放; NULL セーフ |
| DarraUa_DecodedStruct_FindField(st, field_name) | 索引 | 読み取り可 | フィールド名で検索し、存在しない場合はNULLを返す |
デコード結果は DarraUa_DecodedStruct (fields / fields_count フィールド配列 + type_id / status_text) です。各 DarraUa_DecodedField は name + DarraUa_FieldValueKind kind + value の組み合わせです。kind には計 12 個の値があります: UA_FIELD_VALUE_NULL / _BOOLEAN / _INT64 / _UINT64 / _DOUBLE / _STRING / _BYTES / _DATETIME / _GUID / _NODE_ID_STR / _NESTED_STRUCT / _ARRAY。
関連する構造:
typedef enum {
UA_STRUCT_KIND_STRUCTURE = 0, // 普通
UA_STRUCT_KIND_STRUCTURE_WITH_SUBTYPED_VALUES = 1, // 子を含むタイプ
UA_STRUCT_KIND_STRUCTURE_WITH_OPTIONAL_FIELDS = 2, // オプションを含む(前置 EncodingMask)
UA_STRUCT_KIND_UNION = 3, // Union(前置セレクタ)
UA_STRUCT_KIND_UNION_WITH_SUBTYPED_VALUES = 4 // Union + サブタイプ
} DarraUa_StructureKind;
コード例
#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) カスタム構造を読み込む
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) ネスト
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) 全フィールドの走査
for (size_t i = 0; i < pose->fields_count; ++i)
printf("%s = ...\n", pose->fields[i].name);
// 4) 元のバイトデータが存在する場合は、直接デコードする (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);
実装上の制約
- ExtensionObject のバイトを取得できない場合、Decode は
UA_STATUS_NOT_IMPLEMENTEDを返し、out_stには場合によってはその原因を説明するメッセージstatus_textが添えられることがあります - 元のバイトデータが存在する場合は、DecodeBody で直接デコードする (body + DataType NodeId)
- DiagnosticInfo をスキップ
ベストプラクティス
- デコーダを共有し、フィールドテーブルのキャッシュを再利用する
- サーバー側の DataType 変更後は
DarraUa_StructDecoder_ClearCacheを呼び、DarraUa_StructDecoder_CachedCountでキャッシュ量を監視する - ネスト構造は
kind == UA_FIELD_VALUE_NESTED_STRUCTのとき、value.v_nestedで再帰的にアクセスする - Union は
DarraUa_StructureKindのUA_STRUCT_KIND_UNIONを参照
多言語対照
| 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 |