tutorial_noderelations.rst 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. Examining and interacting with node relations
  2. =============================================
  3. In the past tutorials you have learned to compile the stack in various configurations, create/delete nodes and manage variables. The core of OPC UA is its data modelling capabilities, and you will invariably find yourself confronted to investigate these relations during runtime. This tutorial will show you how to interact with object and type hierarchies and how to create your own.
  4. Compile XML Namespaces
  5. ----------------------
  6. So far we have made due with the hardcoded mini-namespace in the server stack. When writing an application, it is more then likely that you will want to create your own data models using some comfortable GUI based tools like UA Modeller. Most tools can export data to the OPC UA XML specification. open62541 contains a python based namespace compiler that can embed datamodels contained in XML files into the server stack.
  7. Note beforehand that the pyUANamespace compiler you can find in the *tools* subfolder is *not* a XML transformation tool but a compiler. That means that it will create an internal representation (dAST) when parsing the XML files and attempt to understand this representation in order to generate C Code. In consequence, the compiler will refuse to print any inconsistencies or invalid nodes.
  8. As an example, we will create a simple object model using UA Modeller and embed this into the servers nodeset, which is exported to the following XML file:
  9. .. code-block:: xml
  10. <UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:s1="http://yourorganisation.org/example_nodeset/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  11. <NamespaceUris>
  12. <Uri>http://yourorganisation.org/example_nodeset/</Uri>
  13. </NamespaceUris>
  14. <Aliases>
  15. <Alias Alias="Boolean">i=1</Alias>
  16. <Alias Alias="UInt32">i=7</Alias>
  17. <Alias Alias="String">i=12</Alias>
  18. <Alias Alias="HasModellingRule">i=37</Alias>
  19. <Alias Alias="HasTypeDefinition">i=40</Alias>
  20. <Alias Alias="HasSubtype">i=45</Alias>
  21. <Alias Alias="HasProperty">i=46</Alias>
  22. <Alias Alias="HasComponent">i=47</Alias>
  23. <Alias Alias="Argument">i=296</Alias>
  24. </Aliases>
  25. <Extensions>
  26. <Extension>
  27. <ModelInfo Tool="UaModeler" Hash="Zs8w1AQI71W8P/GOk3k/xQ==" Version="1.3.4"/>
  28. </Extension>
  29. </Extensions>
  30. <UAReferenceType NodeId="ns=1;i=4001" BrowseName="1:providesInputTo">
  31. <DisplayName>providesInputTo</DisplayName>
  32. <References>
  33. <Reference ReferenceType="HasSubtype" IsForward="false">i=33</Reference>
  34. </References>
  35. <InverseName Locale="en_US">inputProcidedBy</InverseName>
  36. </UAReferenceType>
  37. <UAObjectType IsAbstract="true" NodeId="ns=1;i=1001" BrowseName="1:FieldDevice">
  38. <DisplayName>FieldDevice</DisplayName>
  39. <References>
  40. <Reference ReferenceType="HasSubtype" IsForward="false">i=58</Reference>
  41. <Reference ReferenceType="HasComponent">ns=1;i=6001</Reference>
  42. <Reference ReferenceType="HasComponent">ns=1;i=6002</Reference>
  43. </References>
  44. </UAObjectType>
  45. <UAVariable DataType="String" ParentNodeId="ns=1;i=1001" NodeId="ns=1;i=6001" BrowseName="1:ManufacturerName" UserAccessLevel="3" AccessLevel="3">
  46. <DisplayName>ManufacturerName</DisplayName>
  47. <References>
  48. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  49. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  50. <Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1001</Reference>
  51. </References>
  52. </UAVariable>
  53. <UAVariable DataType="String" ParentNodeId="ns=1;i=1001" NodeId="ns=1;i=6002" BrowseName="1:ModelName" UserAccessLevel="3" AccessLevel="3">
  54. <DisplayName>ModelName</DisplayName>
  55. <References>
  56. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  57. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  58. <Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1001</Reference>
  59. </References>
  60. </UAVariable>
  61. <UAObjectType NodeId="ns=1;i=1002" BrowseName="1:Pump">
  62. <DisplayName>Pump</DisplayName>
  63. <References>
  64. <Reference ReferenceType="HasComponent">ns=1;i=6003</Reference>
  65. <Reference ReferenceType="HasComponent">ns=1;i=6004</Reference>
  66. <Reference ReferenceType="HasSubtype" IsForward="false">ns=1;i=1001</Reference>
  67. <Reference ReferenceType="HasComponent">ns=1;i=7001</Reference>
  68. <Reference ReferenceType="HasComponent">ns=1;i=7002</Reference>
  69. </References>
  70. </UAObjectType>
  71. <UAVariable DataType="Boolean" ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=6003" BrowseName="1:isOn" UserAccessLevel="3" AccessLevel="3">
  72. <DisplayName>isOn</DisplayName>
  73. <References>
  74. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  75. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  76. <Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1002</Reference>
  77. </References>
  78. </UAVariable>
  79. <UAVariable DataType="UInt32" ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=6004" BrowseName="1:MotorRPM" UserAccessLevel="3" AccessLevel="3">
  80. <DisplayName>MotorRPM</DisplayName>
  81. <References>
  82. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  83. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  84. <Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1002</Reference>
  85. </References>
  86. </UAVariable>
  87. <UAMethod ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=7001" BrowseName="1:startPump">
  88. <DisplayName>startPump</DisplayName>
  89. <References>
  90. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  91. <Reference ReferenceType="HasProperty">ns=1;i=6005</Reference>
  92. <Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1002</Reference>
  93. </References>
  94. </UAMethod>
  95. <UAVariable DataType="Argument" ParentNodeId="ns=1;i=7001" ValueRank="1" NodeId="ns=1;i=6005" ArrayDimensions="1" BrowseName="OutputArguments">
  96. <DisplayName>OutputArguments</DisplayName>
  97. <References>
  98. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  99. <Reference ReferenceType="HasProperty" IsForward="false">ns=1;i=7001</Reference>
  100. <Reference ReferenceType="HasTypeDefinition">i=68</Reference>
  101. </References>
  102. <Value>
  103. <ListOfExtensionObject>
  104. <ExtensionObject>
  105. <TypeId>
  106. <Identifier>i=297</Identifier>
  107. </TypeId>
  108. <Body>
  109. <Argument>
  110. <Name>started</Name>
  111. <DataType>
  112. <Identifier>i=1</Identifier>
  113. </DataType>
  114. <ValueRank>-1</ValueRank>
  115. <ArrayDimensions></ArrayDimensions>
  116. <Description/>
  117. </Argument>
  118. </Body>
  119. </ExtensionObject>
  120. </ListOfExtensionObject>
  121. </Value>
  122. </UAVariable>
  123. <UAMethod ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=7002" BrowseName="1:stopPump">
  124. <DisplayName>stopPump</DisplayName>
  125. <References>
  126. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  127. <Reference ReferenceType="HasProperty">ns=1;i=6006</Reference>
  128. <Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=1002</Reference>
  129. </References>
  130. </UAMethod>
  131. <UAVariable DataType="Argument" ParentNodeId="ns=1;i=7002" ValueRank="1" NodeId="ns=1;i=6006" ArrayDimensions="1" BrowseName="OutputArguments">
  132. <DisplayName>OutputArguments</DisplayName>
  133. <References>
  134. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  135. <Reference ReferenceType="HasProperty" IsForward="false">ns=1;i=7002</Reference>
  136. <Reference ReferenceType="HasTypeDefinition">i=68</Reference>
  137. </References>
  138. <Value>
  139. <ListOfExtensionObject>
  140. <ExtensionObject>
  141. <TypeId>
  142. <Identifier>i=297</Identifier>
  143. </TypeId>
  144. <Body>
  145. <Argument>
  146. <Name>stopped</Name>
  147. <DataType>
  148. <Identifier>i=1</Identifier>
  149. </DataType>
  150. <ValueRank>-1</ValueRank>
  151. <ArrayDimensions></ArrayDimensions>
  152. <Description/>
  153. </Argument>
  154. </Body>
  155. </ExtensionObject>
  156. </ListOfExtensionObject>
  157. </Value>
  158. </UAVariable>
  159. </UANodeSet>
  160. Or, more consiscly, this::
  161. +------------------+
  162. | <<ObjectType>> |
  163. | FieldDevice |
  164. +------------------+
  165. | +------------------+
  166. | | <<Variable>> |
  167. |------------->| ManufacturerName |
  168. | hasComponent +------------------+
  169. | +------------------+
  170. | | <<Variable>> |
  171. |------------->| ModelName |
  172. | hasComponent +------------------+
  173. | +----------------+
  174. | | <<ObjectType>> |
  175. '------------->| Pump |
  176. hasSubtype +----------------+
  177. |
  178. |
  179. | +------------------+
  180. | | <<Variable>> |
  181. |--------------->| MotorRPM |
  182. | hasComponent +------------------+
  183. | +------------------+
  184. | | <<Variable>> |
  185. |--------------->| isOn |
  186. | hasComponent +------------------+
  187. | +------------------+ +------------------+
  188. | | <<Method>> | | <<Variable>> |
  189. |--------------->| startPump |--->| outputArguments |
  190. | hasProperty +------------------+ +------------------+
  191. | +------------------+ +------------------+
  192. | | <<Method>> | | <<Variable>> |
  193. '--------------->| stopPump |--->| outputArguments |
  194. hasProperty +------------------+ +------------------+
  195. UA Modeler prepends the namespace qualifier "uax:" to some fields - this is not supported by the namespace compiler, who has strict aliasing rules concerning field names. If a datatype defines a field called "Argument", the compiler expects to find "<Argument>" tags, not "<uax:Argument>". Remove/Substitute such fields to remove namespace qualifiers.
  196. The namespace compiler can be invoked manually and has numerous options. In its simplest form, an invokation will look like this::
  197. python ./generate_open62541CCode.py ../schema/namespace0/Opc.Ua.NodeSet2.xml <path>/<to>/<more>/<files>.xml <path>/<to>/<evenmore>/<files>.xml myNamespace
  198. The above call first parses Namespace 0, which provides all dataTypes, referenceTypes, etc.. An arbitrary amount of further xml files can be passed as options, whose nodes will be added to the abstract syntax tree. The script will then create the files ``myNamespace.c`` and ``myNamespace.h`` containing the C code necessary to instantiate those namespaces.
  199. Although it is possible to run the compiler this way, it is highly discouraged. If you care to examine the CMakeLists.txt (toplevel directory), you will find that compiling the stack with ``DENABLE_GENERATE_NAMESPACE0`` will execute the following command::
  200. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py
  201. -i ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_AssumeExternal.txt
  202. -s description -b ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_Blacklist.txt
  203. ${PROJECT_SOURCE_DIR}/tools/schema/namespace0/${GENERATE_NAMESPACE0_FILE}
  204. ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated
  205. Albeit a bit more complicates then the previous description, you can see that a the namespace 0 XML file is loaded in the line before the last, and that the output will be in ``ua_namespaceinit_generated.c/h``. In order to take advantage of the namespace compiler, we will simply append our nodeset to this call and have cmake care for the rest. Modify the CMakeLists.txt line above to contain the relative path to your own XML file like this::
  206. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py
  207. -i ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_AssumeExternal.txt
  208. -s description -b ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_Blacklist.txt
  209. ${PROJECT_SOURCE_DIR}/tools/schema/namespace0/${GENERATE_NAMESPACE0_FILE}
  210. ${PROJECT_SOURCE_DIR}/<relative>/<path>/<to>/<your>/<namespace>.xml
  211. ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated
  212. Always make sure that your XML file comes *after* namespace 0. Also, take into consideration that any node ID's you specify that already exist in previous files will overwrite the previous file (yes, you could intentionally overwrite the NS0 Server node if you wanted to). The namespace compiler will now automatically embedd you namespace definitions into the namespace of the server. So in total, all that was necessary was:
  213. * Creating your namespace XML description
  214. * Adding the relative path to the file into CMakeLists.txt
  215. * Compiling the stack
  216. After adding you XML file to CMakeLists.txt, rerun cmake in your build directory and enable ``DENABLE_GENERATE_NAMESPACE0``. Make especially sure that you are using the option ``CMAKE_BUILD_TYPE=Debug``. The generated namespace contains more than 30000 lines of code and many strings. Optimizing this amount of code with -O2 or -Os options will require several hours on most PCs! Also make sure to enable ``-DENABLE_METHODCALLS``, as namespace 0 does contain methods that need to be encoded::
  217. ichrispa@Cassandra:open62541/build> cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_METHODCALLS=On -BUILD_EXAMPLECLIENT=On -BUILD_EXAMPLESERVER=On -DENABLE_GENERATE_NAMESPACE0=On ../
  218. -- Git version: v0.1.0-RC4-403-g198597c-dirty
  219. -- Configuring done
  220. -- Generating done
  221. -- Build files have been written to: /home/ichrispa/work/svn/working_copies/open62541/build
  222. ichrispa@Cassandra:open62541/build> make
  223. [ 3%] Generating src_generated/ua_nodeids.h
  224. [ 6%] Generating src_generated/ua_types_generated.c, src_generated/ua_types_generated.h
  225. [ 10%] Generating src_generated/ua_transport_generated.c, src_generated/ua_transport_generated.h
  226. [ 13%] Generating src_generated/ua_namespaceinit_generated.c, src_generated/ua_namespaceinit_generated.h
  227. At this point, the make process will most likely hang for 30-60s until the namespace is parsed, checked, linked and finally generated (be pacient). It sould continue as follows::
  228. Scanning dependencies of target open62541-object
  229. [ 17%] Building C object CMakeFiles/open62541-object.dir/src/ua_types.c.o
  230. [ 20%] Building C object CMakeFiles/open62541-object.dir/src/ua_types_encoding_binary.c.o
  231. [ 24%] Building C object CMakeFiles/open62541-object.dir/src_generated/ua_types_generated.c.o
  232. [ 27%] Building C object CMakeFiles/open62541-object.dir/src_generated/ua_transport_generated.c.o
  233. [ 31%] Building C object CMakeFiles/open62541-object.dir/src/ua_connection.c.o
  234. [ 34%] Building C object CMakeFiles/open62541-object.dir/src/ua_securechannel.c.o
  235. [ 37%] Building C object CMakeFiles/open62541-object.dir/src/ua_session.c.o
  236. [ 41%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server.c.o
  237. [ 44%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_addressspace.c.o
  238. [ 48%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_binary.c.o
  239. [ 51%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_nodes.c.o
  240. [ 55%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_worker.c.o
  241. [ 58%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_securechannel_manager.c.o
  242. [ 62%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_session_manager.c.o
  243. [ 65%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_discovery.c.o
  244. [ 68%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_securechannel.c.o
  245. [ 72%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_session.c.o
  246. [ 75%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_attribute.c.o
  247. [ 79%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_nodemanagement.c.o
  248. [ 82%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_view.c.o
  249. [ 86%] Building C object CMakeFiles/open62541-object.dir/src/client/ua_client.c.o
  250. [ 89%] Building C object CMakeFiles/open62541-object.dir/examples/networklayer_tcp.c.o
  251. [ 93%] Building C object CMakeFiles/open62541-object.dir/examples/logger_stdout.c.o
  252. [ 96%] Building C object CMakeFiles/open62541-object.dir/src_generated/ua_namespaceinit_generated.c.o
  253. And at this point, you are going to see the compiler hanging again. If you specified ``-DCMAKE_BUILD_TYPE=Debug``, you are looking at about 5-10 seconds of waiting. If you forgot, you can now drink a cup of coffee, go to the movies or take a loved one out for dinner (or abort the build with CTRL+C). Shortly after::
  254. [ 83%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_call.c.o
  255. [ 86%] Building C object CMakeFiles/open62541-object.dir/src/server/ua_nodestore.c.o
  256. [100%] Built target open62541-object
  257. Scanning dependencies of target open62541
  258. Linking C shared library libopen62541.so
  259. [100%] Built target open62541
  260. If you open the header ``src_generated/ua_namespaceinit_generated.h`` and take a short look at the generated defines, you will notice the following definitions have been created:
  261. .. code-block:: c
  262. #define UA_NS1ID_PROVIDESINPUTTO
  263. #define UA_NS1ID_FIELDDEVICE
  264. #define UA_NS1ID_PUMP
  265. #define UA_NS1ID_STARTPUMP
  266. #define UA_NS1ID_STOPPUMP
  267. These definitions are generated for all types, but not variables, objects or views (as their names may be ambiguous and may not a be unique identifier). You can use these definitions in your code as you already used the ``UA_NS0ID_`` equivalents.
  268. Now switch back to your own source directory and update your libopen62541 library (in case you have not linked it into the build folder). Compile our example server as follows::
  269. ichrispa@Cassandra:open62541/build-tutorials> gcc -g -std=c99 -Wl,-rpath,`pwd` -I ./include -L . -DENABLE_METHODCALLS -o server ./server.c -lopen62541
  270. Note that we need to also define the method-calls here, as the header files may choose to ommit functions such as UA_Server_addMethodNode() if they believe you do not use them. If you run the server, you should now see a new dataType in the browse path ``/Types/ObjectTypes/BaseObjectType/FieldDevice`` when viewing the nodes in UAExpert.
  271. If you take a look at any of the variables, like ``ManufacturerName``, you will notice it is shown as a Boolean; this is not an error. The node does not include a variant and as you learned in our previous tutorial, it is that variant that would hold the dataType ID.
  272. A minor list of some of the miriad things that can go wrong:
  273. * Your file was not found. The namespace compiler will complain, print a help message, and exit.
  274. * A structure/DataType you created with a value was not encoded. The namespace compiler can currently not handle nested extensionObjects.
  275. * Nodes are not or wrongly encoded or you get nodeId errors. The namespace compiler can currently not encode bytestring or guid node id's and external server uris are not supported either.
  276. * You get compiler complaints for non-existant variants. Check that you have removed any namespace qualifiers (like "uax:") from the XML file.
  277. * You get "invalid reference to addMethodNode" style errors. Make sure ``-DDENABLE_METHODCALLS=On`` is defined.
  278. Creating object instances
  279. -------------------------
  280. Defining an object type is only usefull if it ends up making our lives easier in some way (though it is always the proper thing to do). One of the key benefits of defining object types is being able to create object instances fairly easily. As an example, we will modify the server to create 2 pump instances:
  281. .. code-block:: c
  282. #include <stdio.h>
  283. #include <signal.h>
  284. #include "ua_types.h"
  285. #include "ua_server.h"
  286. #include "ua_namespaceinit_generated.h"
  287. #include "logger_stdout.h"
  288. #include "networklayer_tcp.h"
  289. UA_Boolean running;
  290. UA_Int32 global_accessCounter = 0;
  291. void stopHandler(int signal) {
  292. running = 0;
  293. }
  294. UA_StatusCode pumpInstantiationCallback(UA_NodeId objectId, UA_NodeId definitionId, void *handle);
  295. UA_StatusCode pumpInstantiationCallback(UA_NodeId objectId, UA_NodeId definitionId, void *handle) {
  296. printf("Created new node ns=%d;i=%d according to template ns=%d;i=%d (handle was %d)\n", objectId.namespaceIndex, objectId.identifier.numeric,
  297. definitionId.namespaceIndex, definitionId.identifier.numeric, *((UA_Int32 *) handle));
  298. return UA_STATUSCODE_GOOD;
  299. }
  300. int main(void) {
  301. signal(SIGINT, stopHandler);
  302. signal(SIGTERM, stopHandler);
  303. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  304. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  305. running = UA_TRUE;
  306. UA_Int32 myHandle = 42;
  307. UA_Server_addInstanceOf(server, UA_NODEID_NUMERIC(1, 0),
  308. UA_QUALIFIEDNAME(1, "Pump1"), UA_LOCALIZEDTEXT("en_US","Pump1"), UA_LOCALIZEDTEXT("en_US","A pump!"),
  309. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  310. 0, 0, UA_EXPANDEDNODEID_NUMERIC(1, UA_NS1ID_PUMP), pumpInstantiationCallback, (void *) &myHandle, NULL);
  311. UA_Server_addInstanceOf(server, UA_NODEID_NUMERIC(1, 0),
  312. UA_QUALIFIEDNAME(1, "Pump2"), UA_LOCALIZEDTEXT("en_US","Pump2"), UA_LOCALIZEDTEXT("en_US","Another pump!"),
  313. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  314. 0, 0, UA_EXPANDEDNODEID_NUMERIC(1, UA_NS1ID_PUMP), pumpInstantiationCallback, (void *) &myHandle, NULL);
  315. UA_Server_run(server, 1, &running);
  316. UA_Server_delete(server);
  317. printf("Bye\n");
  318. return 0;
  319. }
  320. Make sure you have updated the headers and libs in your project, then recompile and run the server. Make especially sure you have added ``ua_namespaceinit_generated.h`` to your include folder and that you have removed any references to header in ``server``. The only include you are going to need is ``ua_types.h``.
  321. As you can see instantiating an object is not much different from creating an object node. The main difference is that you *must* use an objectType node as typeDefinition and you (may) pass a callback function (``pumpInstantiationCallback``) and a handle (``myHandle``). You should already be familiar with callbacks and handles from our previous tutorial and you can easily derive how the callback is used by running the server binary, which produces the following output::
  322. Created new node ns=1;i=1505 according to template ns=1;i=6001 (handle was 42)
  323. Created new node ns=1;i=1506 according to template ns=1;i=6002 (handle was 42)
  324. Created new node ns=1;i=1507 according to template ns=1;i=6003 (handle was 42)
  325. Created new node ns=1;i=1508 according to template ns=1;i=6004 (handle was 42)
  326. Created new node ns=1;i=1510 according to template ns=1;i=6001 (handle was 42)
  327. Created new node ns=1;i=1511 according to template ns=1;i=6002 (handle was 42)
  328. Created new node ns=1;i=1512 according to template ns=1;i=6003 (handle was 42)
  329. Created new node ns=1;i=1513 according to template ns=1;i=6004 (handle was 42)
  330. If you start the server and inspect the nodes with UA Expert, you will find two pumps in the objects folder, which look like this::
  331. +------------+
  332. | <<Object>> |
  333. | Pump1 |
  334. +------------+
  335. |
  336. | +------------------+
  337. |->| <<Variable>> |
  338. | | ManufacturerName |
  339. | +------------------+
  340. | +------------------+
  341. | | <<Variable>> |
  342. |->| ModelName |
  343. | +------------------+
  344. | +------------------+
  345. | | <<Variable>> |
  346. |->| MotorRPM |
  347. | +------------------+
  348. | +------------------+
  349. | | <<Variable>> |
  350. |->| isOn |
  351. | +------------------+
  352. | +------------------+ +------------------+
  353. | | <<Method>> | | <<Variable>> |
  354. |->| startPump |--->| outputArguments |
  355. | +------------------+ +------------------+
  356. | +------------------+ +------------------+
  357. | | <<Method>> | | <<Variable>> |
  358. '->| stopPump |--->| outputArguments |
  359. +------------------+ +------------------+
  360. As you can see the pump has inherited it's parents attributes (ManufacturerName and ModelName). You may also notice that the callback was not called for the methods, even though they are obviously where they are supposed to be. Methods, in contrast to objects and variables, are never cloned but instead only linked. The reason is that you will quite propably attach a method callback to a central method, not each object. Objects are instantiated if they are *below* the object you are creating, so any object (like an object called associatedServer of ServerType) that is part of pump will be instantiated as well. Objects *above* you object are never instantiated, so the same ServerType object in Fielddevices would have been ommitted (the reason is that the recursive instantiation function protects itself from infinite recursions, which are hard to track when first ascending, then redescending into a tree).
  361. For each object and variable created by the call, the callback was invoked. The callback gives you the nodeId of the new node along with the Id of the Type template used to create it. You can thereby effectively use setAttributeValue() functions (or others) to adapt the properties of these new nodes, as they can be identified by there templates.
  362. If you want to overwrite an attribute of the parent definition, you will have to delete the node instantiated by the parent's template (this as a **FIXME** for developers).
  363. Iterating over Child nodes
  364. --------------------------
  365. A common usecase is wanting to perform something akin to ``for each node referenced by X, call ...``; you may for example be searching for a specific browseName or instance which was created with a dynamic nodeId. There is no way of telling what you are searching for beforehand (inverse hasComponents, typedefinitions, etc.), but all usescases of "searching for" basically means iterating over each reference of a node.
  366. Since searching in nodes is a common operation, the high-level branch provides a function to help you perform this operation: ``UA_(Server|Client)_forEachChildNodeCall();``. These functions will iterate over all references of a given node, invoking a callback (with a handle) for every found reference. Since in our last tutorial we created a server that instantiates two pumps, we are now going to build a client that will search for pumps in all object instances on the server.
  367. .. code-block:: c
  368. #include <stdio.h>
  369. #include "ua_types.h"
  370. #include "ua_server.h"
  371. #include "ua_client.h"
  372. #include "ua_namespaceinit_generated.h"
  373. #include "logger_stdout.h"
  374. #include "networklayer_tcp.h"
  375. UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void *handle);
  376. UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void *handle) {
  377. struct {
  378. UA_Client *client;
  379. UA_Boolean isAPump;
  380. UA_NodeId PumpId;
  381. } *nodeIterParam = handle;
  382. if (isInverse == UA_TRUE)
  383. return UA_STATUSCODE_GOOD;
  384. if (childId.namespaceIndex != 1)
  385. return UA_STATUSCODE_GOOD;
  386. if (nodeIterParam == NULL)
  387. return UA_STATUSCODE_GOODNODATA;
  388. UA_QualifiedName *childBrowseName = NULL;
  389. UA_Client_getAttributeValue(nodeIterParam->client, childId, UA_ATTRIBUTEID_BROWSENAME, (void**) &childBrowseName);
  390. UA_String pumpName = UA_STRING("Pump");
  391. if (childBrowseName != NULL) {
  392. if (childBrowseName->namespaceIndex == 1) {
  393. if (!strncmp(childBrowseName->name.data, pumpName.data, pumpName.length))
  394. printf("Found %s with NodeId ns=1,i=%d\n", childBrowseName->name.data, childId.identifier.numeric);
  395. inodeIterParam->isAPump = UA_TRUE;
  396. UA_NodeId_copy(&childId, &nodeIterParam->PumpId);
  397. }
  398. }
  399. UA_QualifiedName_delete(childBrowseName);
  400. return UA_STATUSCODE_GOOD;
  401. }
  402. int main(void) {
  403. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  404. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect, "opc.tcp://localhost:16664");
  405. if(retval != UA_STATUSCODE_GOOD) {
  406. UA_Client_delete(client);
  407. return retval;
  408. }
  409. struct {
  410. UA_Client *client;
  411. UA_Boolean isAPump;
  412. UA_NodeId PumpId;
  413. } nodeIterParam;
  414. nodeIterParam.client = client;
  415. nodeIterParam.isAPump = UA_FALSE;
  416. UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), nodeIter, (void *) &nodeIterParam);
  417. if (nodeIterParam.isAPump == UA_TRUE)
  418. printf("Found at least one pump\n");
  419. UA_Client_disconnect(client);
  420. UA_Client_delete(client);
  421. return 0;
  422. }
  423. If the client is run while the example server is running in the background, it will produce the following output::
  424. Found Pump1 with NodeId ns=1,i=1504
  425. Found Pump2 with NodeId ns=1,i=1509
  426. How does it work? The nodeIter function is called by UA_Client_forEachChildNodeCall() for each reference contained in the objectsFolder. The iterator is passed the id of the target and the type of the reference, along with the references directionality. Since we are browsing the Object node, this iterator will be called mutliple times, indicating links to the root node, server, the two pump instances and the nodes type definition.
  427. We are only interested in nodes in namespace 1 that are referenced forwardly, so the iterator returns early if these conditions are not met.
  428. We are searching the nodes by name, so we are comparing the name of the nodes to a string; We could also (in a more complicated example) repeat the node iteration inside the iterator, ie inspect the references of each node to see if it has the dataType "Pump", which would be a more reliable way to operate this sort of search. In either case we need to pass parameters to and from the iterator(s). Note the plural.
  429. You can use the handle to contain a pointer to a struct, which can hold multiple arguments as in the example above. In a more thorough example, the field PumpId could have been an array or a linked list. That struct could also be defined as a global dataType instead of using in-function definitions. Since the handle can be passed between multiple calls of iterators (or any other function that accept handles), the data contents can be communicated between different functions easily.
  430. Examining node copies
  431. ---------------------
  432. So far we have always used the getAttribute() functions to inspect node contents. There may be isolated cases where these are insuficient because you want to examine the properties of a node "in bulk". As mentioned in the first tutorials, the user can not directly interact with the servers nodestore; but the userspace may request a copy of a node, including all its attributes and references. The following functions server the purpose of getting and getting rid of node copies.
  433. .. code-block:: c
  434. UA_(Server|Client)_getNodeCopy()
  435. UA_(Server|Client)_destroyNodeCopy()
  436. Since you are trying to see a struct (node types) that are usually hidden from userspace, you will have to include ``include/ua_nodes.h``, ``src/ua_types_encoding_binary.h`` and ``deps/queue.h`` in addition to the previous includes (link them into the includes folder).
  437. Let's suppose we wanted to do something elaborate with our pump instance that was returned by the iterator of the previous example, or simply "print" all its fields. We could modify the above client's main function like so:
  438. .. code-block:: c
  439. int main(void) {
  440. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  441. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect, "opc.tcp://localhost:16664");
  442. if(retval != UA_STATUSCODE_GOOD) {
  443. UA_Client_delete(client);
  444. return retval;
  445. }
  446. struct {
  447. UA_Client *client;
  448. UA_Boolean isAPump;
  449. UA_NodeId PumpId;
  450. } nodeIterParam;
  451. nodeIterParam.client = client;
  452. nodeIterParam.isAPump = UA_FALSE;
  453. UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), nodeIter, (void *) &nodeIterParam);
  454. if (nodeIterParam.isAPump == UA_TRUE) {
  455. UA_ObjectNode *aPump;
  456. UA_Client_getNodeCopy(client, nodeIterParam.PumpId, (void **) &aPump);
  457. printf("The pump %s with NodeId ns=1,i=%d was returned\n", aPump->browseName.name.data, aPump->nodeId.identifier.numeric);
  458. UA_Client_deleteNodeCopy(client, (void **) &aPump);
  459. }
  460. UA_Client_disconnect(client);
  461. UA_Client_delete(client);
  462. return 0;
  463. }
  464. **Warning** in both examples, we are printing strings contained in UA_String types. These are fundamentaly different from strings in C in that they are *not* necessarlity NULL terminated; they are exactly as long as the string length indicates. It is quite possible that printf() will keep printing trailing data after the UA_String until it finds a NULL. If you intend to really print strings in an application, use the "length" field of the UA_String struct to allocate a null-initialized buffer, then copy the string data into that buffer before printing it.
  465. Conclusion
  466. ----------
  467. In this tutorial, you have learned how to compile your own namespaces, instantiate data and examine the relations of the new nodes. You have learned about node iterators and how to pack multiple pass-through parameters into handles; a technique that is by no means limited to iterators but can also be applied to any other callback, such as methods or value sources.