As am starting out with opc ua , i just wanted to know what happens under the hood of the communication layer of opc ua.
Lets take an example of a very simple server implementation that has 3 nodes in the address space. These nodes present data that can be written and read by an opc-UA client.
From reading a part of the code that comes with open62541, i've learnt that the communication happen via TCP. Meaning the server initiates a socket that a client can connect to and enable the client to perform various operations on the nodes.
My question is, how does the client know about available server nodes ? I know it browses through the address space but where exactly does it browse to find available nodes ? what exposure mechanism does opc-UA use to present available nodes to the client ?. Does the server write available information & nodes on a some xml file or anywhere else and hence when a clients connects, it tries to read the contents of the file to understand the addressSpace structure ?
Sample server implementation for open62541
#include <stdio.h>
#include <open62541.h>
#include <signal.h>
static void
addVariable(UA_Server *server) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32 myInteger = 43;
UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
attr.description = UA_LOCALIZEDTEXT("en-US", "the answer");
attr.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
/* Add the variable node to the information model */
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
parentReferenceNodeId, myIntegerName,
UA_NODEID_NULL, attr, NULL, NULL);
}
static void
addThirdVariable(UA_Server *server) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_String myInteger = UA_STRING("My name is variable 3"); // variable name
UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_STRING]);
attr.description = UA_LOCALIZEDTEXT("en-US", "the answer");
attr.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
/* Add the variable node to the information model */
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "third.variable");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "third varaible");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
parentReferenceNodeId, myIntegerName,
UA_NODEID_NULL, attr, NULL, NULL);
}
void addSecondVariable(UA_Server * server) {
//variable attributes
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_String machine_name = UA_STRING("My name is a machine"); // variable name
UA_Variant_setScalar(&attr.value, &machine_name, &UA_TYPES[UA_TYPES_STRING]);
attr.description = UA_LOCALIZEDTEXT("en-US", "machine name");
attr.displayName = UA_LOCALIZEDTEXT("en-US", "machine name");
attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
//setting access level not important
//add the variable to the information model
UA_NodeId myStringNodeID = UA_NODEID_STRING(1, "the.machine");
UA_QualifiedName myStringName = UA_QUALIFIEDNAME(1, "the machine");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myStringNodeID, parentNodeId,
parentReferenceNodeId, myStringName,
UA_NODEID_NULL, attr, NULL, NULL);
}
UA_Boolean running = true;
static void stopHandler(int sign) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
}
int main(void) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_ServerConfig *config = UA_ServerConfig_new_default();
UA_Server *server = UA_Server_new(config);
addVariable(server);
addSecondVariable(server);
addThirdVariable(server);
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
UA_ServerConfig_delete(config);
return (int)retval;
}