Skip to main content

Namespaces (C)

OPC UA A node consists NamespaceIndex + Identifier of NodeId, but NamespaceIndex Unstable — A server restart may cause changes. Production code must use NamespaceUri reverse lookup indexes; do not hard-code them ns=2.

Prerequisite Reading
  • Standard = ns0; please refer to the node constants WellKnownNodes.
  • For information on node and Browse path resolution, please refer to Nodes.

API Quick Reference​

FunctionNote
DarraUa_Session_LoadNamespaceArray(h)Proactive Reread Server i=2255
DarraUa_Session_GetNamespaceCount(h)Number of namespaces (including ns0)
DarraUa_Session_GetNamespaceUri(h, idx)URI Query (returns an internal pointer; do not free it)
DarraUa_Session_FindNamespaceIndex(h, uri)Reverse Lookup URI → Index; if not found, return -1

Connect Once successful, LoadNamespaceArray is SDK automatically called once; there is no need to call it manually afterward.

API Detailed Explanation​

OPCUA_API DarraUa_Status OPCUA_CALL
DarraUa_Session_LoadNamespaceArray(DarraUa_SessionHandle h);

OPCUA_API uint32_t OPCUA_CALL
DarraUa_Session_GetNamespaceCount(DarraUa_SessionHandle h);

OPCUA_API const char* OPCUA_CALL
DarraUa_Session_GetNamespaceUri(DarraUa_SessionHandle h, uint32_t index);

OPCUA_API int32_t OPCUA_CALL
DarraUa_Session_FindNamespaceIndex(DarraUa_SessionHandle h, const char* uri);

GetNamespaceUri Returns a stack-internal "const char"*, Its lifecycle follows that of the session; it does not need to (and cannot) be released. If you want to save it, you must do so yourself strdup.

Reverse Lookup: Index Spelling NodeId​

int32_t idx = DarraUa_Session_FindNamespaceIndex(h, "urn:my-company:plc1");
if (idx < 0) {
fprintf(stderr, "Server does not have my namespace\n");
return -1;
}

char node_id[64];
snprintf(node_id, sizeof(node_id), "ns=%d;s=Temperature", (int)idx);

DarraUa_Status st = 0;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, node_id, UA_ATTR_VALUE, &st);
/* ... */
DarraUa_DataValue_Delete(dv);

List all namespaces (debug)​

uint32_t n = DarraUa_Session_GetNamespaceCount(h);
for (uint32_t i = 0; i < n; ++i) {
const char* uri = DarraUa_Session_GetNamespaceUri(h, i);
printf(" [%u] %s\n", (unsigned)i, uri ? uri : "");
}

Output example:

[0] http://opcfoundation.org/UA/
[1] urn:server-host
[2] urn:my-company:plc1
[3] http://my-company.com/UA/

Dynamic Namespace Changes​

Some servers register their namespaces only after they start up (plug-in style), so Connect does not receive the complete NamespaceArray set. In this case, call the re-read LoadNamespaceArray method:

DarraUa_Session_LoadNamespaceArray(h);
int32_t idx = DarraUa_Session_FindNamespaceIndex(h,
"urn:plugin:loaded-after-startup");

Standard namespace (ns=0)​

OPC Foundation It is specified that Index 0 is always http://opcfoundation.org/UA/, and all standard nodes have ns=0:

NodeNodeIdNote
Rooti=84Root
Objectsi=85Business Data Root
Typesi=86Type Definition Root
Serveri=2253The Server Object Itself
ServerStatusi=2256Server Status Structure
ServerStatus.CurrentTimei=2258Current server time
NamespaceArrayi=2255Namespace Array
ServerArrayi=2254Server-side array

For more information, see WellKnownNodes.

Next Step​