구조체 디코딩
개요
OPC UA 1.04+ 각 사용자 정의 구조체의 필드 정의를 DataType 노드의 DataTypeDefinition 속성 (AttributeId=27, 제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_StructureKindUA_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 |