OpcUaNodeId — Strongly typed NodeId
Overview
OPC UA node IDs are string-encoded by default ("ns=2;s=Foo"). DarraUa_OwnedNodeId is a strongly typed struct that owns its memory, explicitly carrying namespace_index / type / identifier.
Corresponding section of the standard: Part 6 §5.2.2.
API
| Function | Category | Reading and Writing | Note |
|---|---|---|---|
| DarraUa_NodeId_Parse(s) | Factory | — | Parses a string; returns a DarraUa_OwnedNodeId; returns NULL on failure |
| DarraUa_NodeId_FromNumeric(ns, id) | Factory | — | Numeric; Returns a DarraUa_OwnedNodeId* |
| DarraUa_NodeId_FromString(ns, str) | Factory | — | String type (internal strdup; str can be released immediately) |
| DarraUa_NodeId_FromGuid(ns, guid16) | Factory | — | GUID Type (guid16 points to 16 bytes) |
| DarraUa_NodeId_FromOpaque(ns, data, length) | Factory | — | Opaque type (internal deep copy of length bytes) |
| DarraUa_NodeId_ToString(nid, buf, buf_len) | Serialization | — | Writes to the caller's buffer and returns the number of bytes written (excluding the last byte NUL); if the buffer is insufficient, returns the number of bytes required. |
| DarraUa_NodeId_IsNull(nid) | Determination | Read | Returns int32_t (0/1); a NULL nid is also counted as 1 |
| DarraUa_NodeId_ToBorrowed(owned, &out_borrowed) | Mutual Conversion | — | Shallow copy to the stack DarraUa_NodeId; 1 = OK, 0 = Invalid parameter |
| DarraUa_NodeId_FromBorrowed(&borrowed) | Mutual Conversion | — | Deep DarraUa_NodeId copy from the stack; returns NULL on failure |
| DarraUa_NodeId_Free(nid) | Release | — | Deallocate (including internal strings / opaque values); NULL-safe |
typedef struct {
uint16_t namespace_index;
DarraUa_NodeIdType type;
union {
uint32_t numeric;
char* string; /* owned (heap, strdup) */
uint8_t guid[16];
struct {
uint8_t* data; /* owned */
size_t length;
} opaque;
} identifier;
} DarraUa_OwnedNodeId;
Related Structures:
typedef enum {
UA_NODE_ID_TYPE_NUMERIC = 0, // i = code, uint32
UA_NODE_ID_TYPE_STRING = 1, // s = code, UTF-8
UA_NODE_ID_TYPE_GUID = 2, // g = code, {xxxx-...}
UA_NODE_ID_TYPE_OPAQUE = 3 // b = code, base64
} DarraUa_NodeIdType;
Code Example
#include <opcua_node_id.h>
int main(void) {
/* 1) Factory — All return DarraUa_OwnedNodeId* allocated on the heap SDK */
DarraUa_OwnedNodeId* n1 = DarraUa_NodeId_FromNumeric(0, 84);
DarraUa_OwnedNodeId* n2 = DarraUa_NodeId_FromString(2, "Boiler1.Temp");
uint8_t guid[16] = { /* ... */ };
DarraUa_OwnedNodeId* n3 = DarraUa_NodeId_FromGuid(3, guid);
uint8_t bytes[3] = { 1, 2, 3 };
DarraUa_OwnedNodeId* n4 = DarraUa_NodeId_FromOpaque(4, bytes, 3);
/* 2) Analysis — Return on failure NULL */
DarraUa_OwnedNodeId* n5 = DarraUa_NodeId_Parse("ns=2;s=Demo.Tag");
if (n5)
printf("ns=%u, type=%d\n", n5->namespace_index, (int)n5->type);
DarraUa_OwnedNodeId* n6 = DarraUa_NodeId_Parse("ns=99;i=foo");
if (!n6)
printf("invalid\n");
/* 3) Serialization — Write to the caller's buffer; the buffer does not need to be freed */
char s[128];
DarraUa_NodeId_ToString(n2, s, sizeof(s));
printf("%s\n", s); /* "ns=2;s=Boiler1.Temp" */
/* 4) Direct DarraUa_NodeId conversion to and from Stack */
DarraUa_NodeId borrowed;
if (DarraUa_NodeId_ToBorrowed(n2, &borrowed) == 1) {
/* "borrowed" is a shallow copy; lifecycle = n2 */
}
DarraUa_OwnedNodeId* n7 = DarraUa_NodeId_FromBorrowed(&borrowed); /* Deep Copy */
/* 5) Null Check */
printf("%d\n", DarraUa_NodeId_IsNull(n2)); /* 0 */
/* 6) Release */
DarraUa_NodeId_Free(n1);
DarraUa_NodeId_Free(n2);
DarraUa_NodeId_Free(n3);
DarraUa_NodeId_Free(n4);
if (n5) DarraUa_NodeId_Free(n5);
if (n7) DarraUa_NodeId_Free(n7);
return 0;
}
Best Practices
- Store strings in the configuration file; use to load them
DarraUa_NodeId_Parse - Use strong typing within the code
DarraUa_OwnedNodeId - Numeric is the priority
- Parse user input first, and check for NULL returns
- Free: free any
DarraUa_OwnedNodeId*withDarraUa_NodeId_Free(including internal string / opaque)
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | NodeId | OpcUaNodeId | DarraUa_OwnedNodeId |
| Parse | parse | parse | Parse | parse | DarraUa_NodeId_Parse |
| FromNumeric | from_numeric | fromNumeric | FromNumeric | from_numeric | DarraUa_NodeId_FromNumeric |
| FromString | from_string | fromString | FromString | from_string | DarraUa_NodeId_FromString |