nodeset_compiler.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. XML Nodeset Compiler
  2. --------------------
  3. When writing an application, it is more comfortable to create information models using some GUI tools. Most tools can export data according the OPC UA Nodeset XML schema. open62541 contains a python based nodeset compiler that can transform these information model definitions into a working server.
  4. Note that the nodeset compiler you can find in the *tools/nodeset_compiler* subfolder is *not* an XML transformation tool but a compiler. That means that it will create an internal representation when parsing the XML files and attempt to understand and verify the correctness of this representation in order to generate C Code.
  5. Getting started
  6. ...............
  7. We take the following information model snippet as the starting point of the following tutorial:
  8. .. code-block:: xml
  9. <UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10. xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd"
  11. xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd"
  12. xmlns:s1="http://yourorganisation.org/example_nodeset/"
  13. xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  14. <NamespaceUris>
  15. <Uri>http://yourorganisation.org/example_nodeset/</Uri>
  16. </NamespaceUris>
  17. <Aliases>
  18. <Alias Alias="Boolean">i=1</Alias>
  19. <Alias Alias="UInt32">i=7</Alias>
  20. <Alias Alias="String">i=12</Alias>
  21. <Alias Alias="HasModellingRule">i=37</Alias>
  22. <Alias Alias="HasTypeDefinition">i=40</Alias>
  23. <Alias Alias="HasSubtype">i=45</Alias>
  24. <Alias Alias="HasProperty">i=46</Alias>
  25. <Alias Alias="HasComponent">i=47</Alias>
  26. <Alias Alias="Argument">i=296</Alias>
  27. </Aliases>
  28. <Extensions>
  29. <Extension>
  30. <ModelInfo Tool="UaModeler" Hash="Zs8w1AQI71W8P/GOk3k/xQ=="
  31. Version="1.3.4"/>
  32. </Extension>
  33. </Extensions>
  34. <UAReferenceType NodeId="ns=1;i=4001" BrowseName="1:providesInputTo">
  35. <DisplayName>providesInputTo</DisplayName>
  36. <References>
  37. <Reference ReferenceType="HasSubtype" IsForward="false">
  38. i=33
  39. </Reference>
  40. </References>
  41. <InverseName Locale="en-US">inputProcidedBy</InverseName>
  42. </UAReferenceType>
  43. <UAObjectType IsAbstract="true" NodeId="ns=1;i=1001"
  44. BrowseName="1:FieldDevice">
  45. <DisplayName>FieldDevice</DisplayName>
  46. <References>
  47. <Reference ReferenceType="HasSubtype" IsForward="false">
  48. i=58
  49. </Reference>
  50. <Reference ReferenceType="HasComponent">ns=1;i=6001</Reference>
  51. <Reference ReferenceType="HasComponent">ns=1;i=6002</Reference>
  52. </References>
  53. </UAObjectType>
  54. <UAVariable DataType="String" ParentNodeId="ns=1;i=1001"
  55. NodeId="ns=1;i=6001" BrowseName="1:ManufacturerName"
  56. UserAccessLevel="3" AccessLevel="3">
  57. <DisplayName>ManufacturerName</DisplayName>
  58. <References>
  59. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  60. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  61. <Reference ReferenceType="HasComponent" IsForward="false">
  62. ns=1;i=1001
  63. </Reference>
  64. </References>
  65. </UAVariable>
  66. <UAVariable DataType="String" ParentNodeId="ns=1;i=1001"
  67. NodeId="ns=1;i=6002" BrowseName="1:ModelName"
  68. UserAccessLevel="3" AccessLevel="3">
  69. <DisplayName>ModelName</DisplayName>
  70. <References>
  71. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  72. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  73. <Reference ReferenceType="HasComponent" IsForward="false">
  74. ns=1;i=1001
  75. </Reference>
  76. </References>
  77. </UAVariable>
  78. <UAObjectType NodeId="ns=1;i=1002" BrowseName="1:Pump">
  79. <DisplayName>Pump</DisplayName>
  80. <References>
  81. <Reference ReferenceType="HasComponent">ns=1;i=6003</Reference>
  82. <Reference ReferenceType="HasComponent">ns=1;i=6004</Reference>
  83. <Reference ReferenceType="HasSubtype" IsForward="false">
  84. ns=1;i=1001
  85. </Reference>
  86. <Reference ReferenceType="HasComponent">ns=1;i=7001</Reference>
  87. <Reference ReferenceType="HasComponent">ns=1;i=7002</Reference>
  88. </References>
  89. </UAObjectType>
  90. <UAVariable DataType="Boolean" ParentNodeId="ns=1;i=1002"
  91. NodeId="ns=1;i=6003" BrowseName="1:isOn" UserAccessLevel="3"
  92. AccessLevel="3">
  93. <DisplayName>isOn</DisplayName>
  94. <References>
  95. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  96. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  97. <Reference ReferenceType="HasComponent" IsForward="false">
  98. ns=1;i=1002
  99. </Reference>
  100. </References>
  101. </UAVariable>
  102. <UAVariable DataType="UInt32" ParentNodeId="ns=1;i=1002"
  103. NodeId="ns=1;i=6004" BrowseName="1:MotorRPM"
  104. UserAccessLevel="3" AccessLevel="3">
  105. <DisplayName>MotorRPM</DisplayName>
  106. <References>
  107. <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
  108. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  109. <Reference ReferenceType="HasComponent" IsForward="false">
  110. ns=1;i=1002
  111. </Reference>
  112. </References>
  113. </UAVariable>
  114. <UAMethod ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=7001"
  115. BrowseName="1:startPump">
  116. <DisplayName>startPump</DisplayName>
  117. <References>
  118. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  119. <Reference ReferenceType="HasProperty">ns=1;i=6005</Reference>
  120. <Reference ReferenceType="HasComponent" IsForward="false">
  121. ns=1;i=1002
  122. </Reference>
  123. </References>
  124. </UAMethod>
  125. <UAVariable DataType="Argument" ParentNodeId="ns=1;i=7001" ValueRank="1"
  126. NodeId="ns=1;i=6005" ArrayDimensions="1"
  127. BrowseName="OutputArguments">
  128. <DisplayName>OutputArguments</DisplayName>
  129. <References>
  130. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  131. <Reference ReferenceType="HasProperty"
  132. IsForward="false">ns=1;i=7001</Reference>
  133. <Reference ReferenceType="HasTypeDefinition">i=68</Reference>
  134. </References>
  135. <Value>
  136. <ListOfExtensionObject>
  137. <ExtensionObject>
  138. <TypeId>
  139. <Identifier>i=297</Identifier>
  140. </TypeId>
  141. <Body>
  142. <Argument>
  143. <Name>started</Name>
  144. <DataType>
  145. <Identifier>i=1</Identifier>
  146. </DataType>
  147. <ValueRank>-1</ValueRank>
  148. <ArrayDimensions></ArrayDimensions>
  149. <Description/>
  150. </Argument>
  151. </Body>
  152. </ExtensionObject>
  153. </ListOfExtensionObject>
  154. </Value>
  155. </UAVariable>
  156. <UAMethod ParentNodeId="ns=1;i=1002" NodeId="ns=1;i=7002"
  157. BrowseName="1:stopPump">
  158. <DisplayName>stopPump</DisplayName>
  159. <References>
  160. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  161. <Reference ReferenceType="HasProperty">ns=1;i=6006</Reference>
  162. <Reference ReferenceType="HasComponent"
  163. IsForward="false">ns=1;i=1002</Reference>
  164. </References>
  165. </UAMethod>
  166. <UAVariable DataType="Argument" ParentNodeId="ns=1;i=7002" ValueRank="1"
  167. NodeId="ns=1;i=6006" ArrayDimensions="1"
  168. BrowseName="OutputArguments">
  169. <DisplayName>OutputArguments</DisplayName>
  170. <References>
  171. <Reference ReferenceType="HasModellingRule">i=78</Reference>
  172. <Reference ReferenceType="HasProperty" IsForward="false">
  173. ns=1;i=7002
  174. </Reference>
  175. <Reference ReferenceType="HasTypeDefinition">i=68</Reference>
  176. </References>
  177. <Value>
  178. <ListOfExtensionObject>
  179. <ExtensionObject>
  180. <TypeId>
  181. <Identifier>i=297</Identifier>
  182. </TypeId>
  183. <Body>
  184. <Argument>
  185. <Name>stopped</Name>
  186. <DataType>
  187. <Identifier>i=1</Identifier>
  188. </DataType>
  189. <ValueRank>-1</ValueRank>
  190. <ArrayDimensions></ArrayDimensions>
  191. <Description/>
  192. </Argument>
  193. </Body>
  194. </ExtensionObject>
  195. </ListOfExtensionObject>
  196. </Value>
  197. </UAVariable>
  198. </UANodeSet>
  199. Take the previous snippet and save it to a file ``myNS.xml``. To compile this nodeset into the corresponding C code, which can then be used by the open62541 stack, the nodeset compiler needs some arguments when you call it. The output of the help command gives you the following info:
  200. .. code-block:: bash
  201. $ python ./nodeset_compiler.py -h
  202. usage: nodeset_compiler.py [-h] [-e <existingNodeSetXML>] [-x <nodeSetXML>]
  203. [--generate-ns0] [--internal-headers]
  204. [-b <blacklistFile>] [-i <ignoreFile>]
  205. [-t <typesArray>]
  206. [--max-string-length MAX_STRING_LENGTH] [-v]
  207. <outputFile>
  208. positional arguments:
  209. <outputFile> The path/basename for the <output file>.c and <output
  210. file>.h files to be generated. This will also be the
  211. function name used in the header and c-file.
  212. optional arguments:
  213. -h, --help show this help message and exit
  214. -e <existingNodeSetXML>, --existing <existingNodeSetXML>
  215. NodeSet XML files with nodes that are already present
  216. on the server.
  217. -x <nodeSetXML>, --xml <nodeSetXML>
  218. NodeSet XML files with nodes that shall be generated.
  219. --generate-ns0 Omit some consistency checks for bootstrapping
  220. namespace 0, create references to parents and type
  221. definitions manually
  222. --internal-headers Include internal headers instead of amalgamated header
  223. -b <blacklistFile>, --blacklist <blacklistFile>
  224. Loads a list of NodeIDs stored in blacklistFile (one
  225. NodeID per line). Any of the nodeIds encountered in
  226. this file will be removed from the nodeset prior to
  227. compilation. Any references to these nodes will also
  228. be removed
  229. -i <ignoreFile>, --ignore <ignoreFile>
  230. Loads a list of NodeIDs stored in ignoreFile (one
  231. NodeID per line). Any of the nodeIds encountered in
  232. this file will be kept in the nodestore but not
  233. printed in the generated code
  234. -t <typesArray>, --types-array <typesArray>
  235. Types array for the given namespace. Can be used
  236. mutliple times to define (in the same order as the
  237. .xml files, first for --existing, then --xml) the type
  238. arrays
  239. --max-string-length MAX_STRING_LENGTH
  240. Maximum allowed length of a string literal. If longer,
  241. it will be set to an empty string
  242. -v, --verbose Make the script more verbose. Can be applied up to 4
  243. times
  244. So the resulting call looks like this:
  245. .. code-block:: bash
  246. $ python ./nodeset_compiler.py --types-array=UA_TYPES --existing ../../deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml --xml myNS.xml myNS
  247. And the output of the command:
  248. .. code-block:: bash
  249. INFO:__main__:Preprocessing (existing) ../../deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml
  250. INFO:__main__:Preprocessing myNS.xml
  251. INFO:__main__:Generating Code
  252. INFO:__main__:NodeSet generation code successfully printed
  253. The first argument ``--types-array=UA_TYPES`` defines the name of the global array in open62541 which contains the corresponding types used within the nodeset in ``NodeSet2.xml``. If you do not define your own datatypes, you can always use the ``UA_TYPES`` value. More on that later in this tutorial.
  254. The next argument ``--existing ../../deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml`` points to the XML definition of the standard-defined namespace 0 (NS0). Namespace 0 is assumed to be loaded beforehand and provides definitions for data type, reference types, and so. Since we reference nodes from NS0 in our myNS.xml we need to tell the nodeset compiler that it should also load that nodeset, but not compile it into the output.
  255. Note that you may need to initialize the git submodule to get the ``deps/ua-nodeset`` folder (``git submodule update --init``) or download the full ``NodeSet2.xml`` manually.
  256. The argument ``--xml myNS.xml`` points to the user-defined information model, whose nodes will be added to the abstract syntax tree. The script will then create the files ``myNS.c`` and ``myNS.h`` (indicated by the last argument ``myNS``) containing the C code necessary to instantiate those namespaces.
  257. Although it is possible to run the compiler this way, it is highly discouraged. If you care to examine the CMakeLists.txt (examples/nodeset/CMakeLists.txt), you will find out that the file ``server_nodeset.xml`` is compiled with the command::
  258. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodeset_compiler.py
  259. --types-array=UA_TYPES
  260. --existing ${UA_FILE_NS0}
  261. --xml ${PROJECT_SOURCE_DIR}/examples/nodeset/server_nodeset.xml
  262. ${PROJECT_BINARY_DIR}/src_generated/example_nodeset
  263. If you look into the files generated by the nodeset compiler, you will see that it generated a method called ``extern UA_StatusCode myNS(UA_Server *server);``. You need to include the header and source file and then call the ``myNS(server)`` method right after creating the server instance with ``UA_Server_new``. This will automatically add all the nodes to the server and return ``UA_STATUSCODE_GOOD`` if there weren't any errors. Additionally you need to compile the open62541 stack with the full NS0 by setting ``UA_ENABLE_FULL_NS0=ON`` in CMake. Otherwise the stack uses a minimal subset where many nodes are not included and thus adding a custom nodeset may fail.
  264. This is how you can use the nodeset compiler to compile simple NodeSet XMLs to be used by the open62541 stack.
  265. Creating object instances
  266. .........................
  267. One of the key benefits of defining object types is being able to create object instances fairly easily. Object instantiation is handled automatically when the typedefinition NodeId points to a valid ObjectType node. All Attributes and Methods contained in the objectType definition will be instantiated along with the object node.
  268. While variables are copied from the objetType definition (allowing the user for example to attach new dataSources to them), methods are always only linked. This paradigm is identical to languages like C++: The method called is always the same piece of code, but the first argument is a pointer to an object. Likewise, in OPC UA, only one methodCallback can be attached to a specific methodNode. If that methodNode is called, the parent objectId will be passed to the method - it is the methods job to derefence which object instance it belongs to in that moment.
  269. Let's look at an example that will create a pump instance given the newly defined objectType from myNS.xml:
  270. .. code-block:: c
  271. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  272. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  273. #include <signal.h>
  274. #include <stdio.h>
  275. #include "open62541.h"
  276. /* Files myNS.h and myNS.c are created from myNS.xml */
  277. #include "myNS.h"
  278. UA_Boolean running = true;
  279. static void stopHandler(int sign) {
  280. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  281. running = false;
  282. }
  283. int main(int argc, char **argv) {
  284. signal(SIGINT, stopHandler);
  285. signal(SIGTERM, stopHandler);
  286. UA_ServerConfig *config = UA_ServerConfig_new_default();
  287. UA_Server *server = UA_Server_new(config);
  288. UA_StatusCode retval;
  289. /* create nodes from nodeset */
  290. if (myNS(server) != UA_STATUSCODE_GOOD) {
  291. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Could not add the example nodeset. "
  292. "Check previous output for any error.");
  293. retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
  294. } else {
  295. UA_NodeId createdNodeId;
  296. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  297. object_attr.description = UA_LOCALIZEDTEXT("en-US", "A pump!");
  298. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Pump1");
  299. // we assume that the myNS nodeset was added in namespace 2.
  300. // You should always use UA_Server_addNamespace to check what the
  301. // namespace index is for a given namespace URI. UA_Server_addNamespace
  302. // will just return the index if it is already added.
  303. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  304. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  305. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  306. UA_QUALIFIEDNAME(1, "Pump1"),
  307. UA_NODEID_NUMERIC(2, 1002),
  308. object_attr, NULL, &createdNodeId);
  309. retval = UA_Server_run(server, &running);
  310. }
  311. UA_Server_delete(server);
  312. UA_ServerConfig_delete(config);
  313. return (int) retval;
  314. }
  315. Make sure you have updated the headers and libs in your project, then recompile and run the server. Make especially sure you have added ``myNS.h`` to your include folder.
  316. 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.
  317. If you start the server and inspect the nodes with UA Expert, you will find the pump in the objects folder, which look like this :numref:`nodeset-compiler-pump`.
  318. .. _nodeset-compiler-pump:
  319. .. figure:: nodeset_compiler_pump.png
  320. :alt: Instantiated Pump Object with inherited children
  321. Instantiated Pump Object with inherited children
  322. As you can see the pump has inherited it's parents attributes (ManufacturerName and ModelName). 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).
  323. Combination of multiple nodesets
  324. ................................
  325. In previous section you have seen how you can use the nodeset compiler with one single nodeset which depends on the default nodeset (NS0) ``Opc.Ua.NodeSet2.xml``. The nodeset compiler also supports nodesets which depend on more than one nodeset. We will show this use-case with the PLCopen nodeset. The PLCopen nodeset ``Opc.Ua.Plc.NodeSet2.xml`` depends on the DI nodeset ``Opc.Ua.Di.NodeSet2.xml`` which then depends on NS0. This example is also shown in ``examples/nodeset/CMakeLists.txt``.
  326. For every nodeset we are depending, we need to call the nodeset compiler, bottom-up. The first nodeset is NS0. This is automatically created by the stack if you enable ``UA_ENABLE_FULL_NS0=ON`` in CMake. So we do not need to care about that. If you look into the main CMakeLists.txt you can find the corresponding call to the nodeset compiler. It uses some additional flags which should only be used for NS0 and are not required for other nodesets.
  327. The next nodeset in the list is the DI nodeset. This nodeset defines some additional data types in ``deps/ua-nodeset/DI/Opc.Ua.Di.Types.bsd``. Since we also need these types within the generated code, we first need to compile the types into C code. The generated code is mainly a definition of the binary representation of the types required for encoding and decoding. The generation can be done using the ``tools/generate_datatypes.py`` script::
  328. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py
  329. --namespace=2
  330. --type-csv=${PROJECT_SOURCE_DIR}/deps/ua-nodeset/DI/OpcUaDiModel.csv
  331. --type-bsd=${PROJECT_SOURCE_DIR}/deps/ua-nodeset/DI/Opc.Ua.Di.Types.bsd
  332. --no-builtin
  333. ${PROJECT_BINARY_DIR}/src_generated/ua_types_di
  334. The ``namespace`` parameter indicates the namespace index of the generated node IDs for the type definitions. Currently we need to rely that the namespace is also added at this position in the final server. There is no automatic inferring yet (pull requests are warmly welcome).
  335. The CSV and BSD files contain the metadata and definition for the types. The ``--no-builtin`` argument tells the script to skip internal datatypes which are always included in the stack. The last parameter is the output file and at the same time the name of the types array: ``UA_TYPES_DI``.
  336. Now you can compile the DI nodeset XML using the following command::
  337. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodeset_compiler.py
  338. --internal-headers
  339. --types-array=UA_TYPES
  340. --types-array=UA_TYPES_DI
  341. --existing ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml
  342. --xml ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/DI/Opc.Ua.Di.NodeSet2.xml
  343. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_di
  344. There are now two new arguments: ``--internal-headers`` indicates that internal headers (and non public API) should be included within the generated source code. This is currently required for nodesets which use structures as data values, and will probably be fixed in the future.
  345. Then you see two times the ``--types-array`` argument. The types array argument is matched with the nodesets in the same order as they appear on the command line: first the ``existing`` ones, and then the ``xml``. It tells the nodeset compiler which types array it should use: ``UA_TYPES`` for ``Opc.Ua.NodeSet2.xml`` and ``UA_TYPES_DI`` for ``Opc.Ua.Di.NodeSet2.xml``. This is the type array generated by the ``generate_datatypes.py`` script. The rest is similar to the example in previous section: ``Opc.Ua.NodeSet2.xml`` is assumed to exist already and only needs to be loaded for consistency checks, ``Opc.Ua.Di.NodeSet2.xml`` will be generated in the output file ``ua_namespace_di.c/.h``
  346. Next we can generate the PLCopen nodeset. Since it doesn't require any additional datatype definitions, we can immediately start with the nodeset compiler command::
  347. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodeset_compiler.py
  348. --internal-headers
  349. --types-array=UA_TYPES
  350. --types-array=UA_TYPES_DI
  351. # PLCopen has no specific type definition, thus use the default UA_TYPES to ignore it
  352. --types-array=UA_TYPES
  353. --existing ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml
  354. --existing ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/DI/Opc.Ua.Di.NodeSet2.xml
  355. --xml ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/PLCopen/Opc.Ua.Plc.NodeSet2.xml
  356. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_plc
  357. This call is quite similar to the compilation of the DI nodeset. As you can see, we do not define any specific types array for the PLCopen nodeset, but just use ``UA_TYPES`` to ignore it. Since the PLCopen nodeset depends on the NS0 and DI nodeset, we need to tell the nodeset compiler that these two nodesets should be seen as already existing. Make sure that the order is the same as in your XML file, e.g., in this case the order indicated in ``Opc.Ua.Plc.NodeSet2.xml -> UANodeSet -> Models -> Model``.
  358. As a result of the previous scripts you will have multiple source files:
  359. * ua_types_di_generated.c
  360. * ua_types_di_generated.h
  361. * ua_types_di_generated_encoding_binary.h
  362. * ua_types_di_generated_handling.h
  363. * ua_namespace_di.c
  364. * ua_namespace_di.h
  365. * ua_namespace_plc.c
  366. * ua_namespace_plc.h
  367. Finally you need to include all these files in your build process and call the corresponding initialization methods for the nodesets. An example application could look like this:
  368. .. code-block:: c
  369. UA_ServerConfig *config = UA_ServerConfig_new_default();
  370. UA_Server *server = UA_Server_new(config);
  371. /* create nodes from nodeset */
  372. UA_StatusCode retval = ua_namespace_di(server);
  373. if(retval != UA_STATUSCODE_GOOD) {
  374. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Adding the DI namespace failed. Please check previous error output.");
  375. UA_Server_delete(server);
  376. UA_ServerConfig_delete(config);
  377. return (int)UA_STATUSCODE_BADUNEXPECTEDERROR;
  378. }
  379. retval |= ua_namespace_plc(server);
  380. if(retval != UA_STATUSCODE_GOOD) {
  381. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Adding the PLCopen namespace failed. Please check previous error output.");
  382. UA_Server_delete(server);
  383. UA_ServerConfig_delete(config);
  384. return (int)UA_STATUSCODE_BADUNEXPECTEDERROR;
  385. }
  386. retval = UA_Server_run(server, &running);