nodestore.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Julian Grothoff
  7. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  8. */
  9. #ifndef UA_SERVER_NODES_H_
  10. #define UA_SERVER_NODES_H_
  11. /* !!! Warning !!!
  12. *
  13. * If you are not developing a nodestore plugin, then you should not work with
  14. * the definitions from this file directly. The underlying node structures are
  15. * not meant to be used directly by end users. Please use the public server API
  16. * / OPC UA services to interact with the information model. */
  17. #include <open62541/server.h>
  18. #include "ziptree.h"
  19. _UA_BEGIN_DECLS
  20. /* Forward declaration */
  21. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  22. struct UA_MonitoredItem;
  23. #endif
  24. /**
  25. * .. _information-modelling:
  26. *
  27. * Information Modelling
  28. * =====================
  29. *
  30. * Information modelling in OPC UA combines concepts from object-orientation and
  31. * semantic modelling. At the core, an OPC UA information model is a graph made
  32. * up of
  33. *
  34. * - Nodes: There are eight possible Node types (variable, object, method, ...)
  35. * - References: Typed and directed relations between two nodes
  36. *
  37. * Every node is identified by a unique (within the server) :ref:`nodeid`.
  38. * Reference are triples of the form ``(source-nodeid, referencetype-nodeid,
  39. * target-nodeid)``. An example reference between nodes is a
  40. * ``hasTypeDefinition`` reference between a Variable and its VariableType. Some
  41. * ReferenceTypes are *hierarchic* and must not form *directed loops*. See the
  42. * section on :ref:`ReferenceTypes <referencetypenode>` for more details on
  43. * possible references and their semantics.
  44. *
  45. * **Warning!!** The structures defined in this section are only relevant for
  46. * the developers of custom Nodestores. The interaction with the information
  47. * model is possible only via the OPC UA :ref:`services`. So the following
  48. * sections are purely informational so that users may have a clear mental
  49. * model of the underlying representation.
  50. *
  51. * Base Node Attributes
  52. * --------------------
  53. *
  54. * Nodes contain attributes according to their node type. The base node
  55. * attributes are common to all node types. In the OPC UA :ref:`services`,
  56. * attributes are referred to via the :ref:`nodeid` of the containing node and
  57. * an integer :ref:`attribute-id`.
  58. *
  59. * Internally, open62541 uses ``UA_Node`` in places where the exact node type is
  60. * not known or not important. The ``nodeClass`` attribute is used to ensure the
  61. * correctness of casting from ``UA_Node`` to a specific node type. */
  62. /* Ordered tree structure for fast member check */
  63. typedef struct UA_ReferenceTarget {
  64. ZIP_ENTRY(UA_ReferenceTarget) zipfields;
  65. UA_UInt32 targetHash; /* Hash of the target nodeid */
  66. UA_ExpandedNodeId target;
  67. } UA_ReferenceTarget;
  68. ZIP_HEAD(UA_ReferenceTargetHead, UA_ReferenceTarget);
  69. typedef struct UA_ReferenceTargetHead UA_ReferenceTargetHead;
  70. ZIP_PROTTYPE(UA_ReferenceTargetHead, UA_ReferenceTarget, UA_ReferenceTarget)
  71. /* List of reference targets with the same reference type and direction */
  72. typedef struct {
  73. UA_NodeId referenceTypeId;
  74. UA_Boolean isInverse;
  75. size_t refTargetsSize;
  76. UA_ReferenceTarget *refTargets;
  77. UA_ReferenceTargetHead refTargetsTree;
  78. } UA_NodeReferenceKind;
  79. #define UA_NODE_BASEATTRIBUTES \
  80. UA_NodeId nodeId; \
  81. UA_NodeClass nodeClass; \
  82. UA_QualifiedName browseName; \
  83. UA_LocalizedText displayName; \
  84. UA_LocalizedText description; \
  85. UA_UInt32 writeMask; \
  86. size_t referencesSize; \
  87. UA_NodeReferenceKind *references; \
  88. \
  89. /* Members specific to open62541 */ \
  90. void *context; \
  91. UA_Boolean constructed; /* Constructors were called */
  92. typedef struct {
  93. UA_NODE_BASEATTRIBUTES
  94. } UA_Node;
  95. /**
  96. * VariableNode
  97. * ------------
  98. *
  99. * Variables store values in a :ref:`datavalue` together with
  100. * metadata for introspection. Most notably, the attributes data type, value
  101. * rank and array dimensions constrain the possible values the variable can take
  102. * on.
  103. *
  104. * Variables come in two flavours: properties and datavariables. Properties are
  105. * related to a parent with a ``hasProperty`` reference and may not have child
  106. * nodes themselves. Datavariables may contain properties (``hasProperty``) and
  107. * also datavariables (``hasComponents``).
  108. *
  109. * All variables are instances of some :ref:`variabletypenode` in return
  110. * constraining the possible data type, value rank and array dimensions
  111. * attributes.
  112. *
  113. * Data Type
  114. * ^^^^^^^^^
  115. *
  116. * The (scalar) data type of the variable is constrained to be of a specific
  117. * type or one of its children in the type hierarchy. The data type is given as
  118. * a NodeId pointing to a :ref:`datatypenode` in the type hierarchy. See the
  119. * Section :ref:`datatypenode` for more details.
  120. *
  121. * If the data type attribute points to ``UInt32``, then the value attribute
  122. * must be of that exact type since ``UInt32`` does not have children in the
  123. * type hierarchy. If the data type attribute points ``Number``, then the type
  124. * of the value attribute may still be ``UInt32``, but also ``Float`` or
  125. * ``Byte``.
  126. *
  127. * Consistency between the data type attribute in the variable and its
  128. * :ref:`VariableTypeNode` is ensured.
  129. *
  130. * Value Rank
  131. * ^^^^^^^^^^
  132. *
  133. * This attribute indicates whether the value attribute of the variable is an
  134. * array and how many dimensions the array has. It may have the following
  135. * values:
  136. *
  137. * - ``n >= 1``: the value is an array with the specified number of dimensions
  138. * - ``n = 0``: the value is an array with one or more dimensions
  139. * - ``n = -1``: the value is a scalar
  140. * - ``n = -2``: the value can be a scalar or an array with any number of dimensions
  141. * - ``n = -3``: the value can be a scalar or a one dimensional array
  142. *
  143. * Consistency between the value rank attribute in the variable and its
  144. * :ref:`variabletypenode` is ensured.
  145. *
  146. * Array Dimensions
  147. * ^^^^^^^^^^^^^^^^
  148. *
  149. * If the value rank permits the value to be a (multi-dimensional) array, the
  150. * exact length in each dimensions can be further constrained with this
  151. * attribute.
  152. *
  153. * - For positive lengths, the variable value is guaranteed to be of the same
  154. * length in this dimension.
  155. * - The dimension length zero is a wildcard and the actual value may have any
  156. * length in this dimension.
  157. *
  158. * Consistency between the array dimensions attribute in the variable and its
  159. * :ref:`variabletypenode` is ensured. */
  160. /* Indicates whether a variable contains data inline or whether it points to an
  161. * external data source */
  162. typedef enum {
  163. UA_VALUESOURCE_DATA,
  164. UA_VALUESOURCE_DATASOURCE
  165. } UA_ValueSource;
  166. #define UA_NODE_VARIABLEATTRIBUTES \
  167. /* Constraints on possible values */ \
  168. UA_NodeId dataType; \
  169. UA_Int32 valueRank; \
  170. size_t arrayDimensionsSize; \
  171. UA_UInt32 *arrayDimensions; \
  172. \
  173. /* The current value */ \
  174. UA_ValueSource valueSource; \
  175. union { \
  176. struct { \
  177. UA_DataValue value; \
  178. UA_ValueCallback callback; \
  179. } data; \
  180. UA_DataSource dataSource; \
  181. } value;
  182. typedef struct {
  183. UA_NODE_BASEATTRIBUTES
  184. UA_NODE_VARIABLEATTRIBUTES
  185. UA_Byte accessLevel;
  186. UA_Double minimumSamplingInterval;
  187. UA_Boolean historizing;
  188. } UA_VariableNode;
  189. /**
  190. * .. _variabletypenode:
  191. *
  192. * VariableTypeNode
  193. * ----------------
  194. *
  195. * VariableTypes are used to provide type definitions for variables.
  196. * VariableTypes constrain the data type, value rank and array dimensions
  197. * attributes of variable instances. Furthermore, instantiating from a specific
  198. * variable type may provide semantic information. For example, an instance from
  199. * ``MotorTemperatureVariableType`` is more meaningful than a float variable
  200. * instantiated from ``BaseDataVariable``. */
  201. typedef struct {
  202. UA_NODE_BASEATTRIBUTES
  203. UA_NODE_VARIABLEATTRIBUTES
  204. UA_Boolean isAbstract;
  205. /* Members specific to open62541 */
  206. UA_NodeTypeLifecycle lifecycle;
  207. } UA_VariableTypeNode;
  208. /**
  209. * .. _methodnode:
  210. *
  211. * MethodNode
  212. * ----------
  213. *
  214. * Methods define callable functions and are invoked using the :ref:`Call
  215. * <method-services>` service. MethodNodes may have special properties (variable
  216. * childen with a ``hasProperty`` reference) with the :ref:`qualifiedname` ``(0,
  217. * "InputArguments")`` and ``(0, "OutputArguments")``. The input and output
  218. * arguments are both described via an array of ``UA_Argument``. While the Call
  219. * service uses a generic array of :ref:`variant` for input and output, the
  220. * actual argument values are checked to match the signature of the MethodNode.
  221. *
  222. * Note that the same MethodNode may be referenced from several objects (and
  223. * object types). For this, the NodeId of the method *and of the object
  224. * providing context* is part of a Call request message. */
  225. typedef struct {
  226. UA_NODE_BASEATTRIBUTES
  227. UA_Boolean executable;
  228. /* Members specific to open62541 */
  229. UA_MethodCallback method;
  230. } UA_MethodNode;
  231. /**
  232. * ObjectNode
  233. * ----------
  234. *
  235. * Objects are used to represent systems, system components, real-world objects
  236. * and software objects. Objects are instances of an :ref:`object
  237. * type<objecttypenode>` and may contain variables, methods and further
  238. * objects. */
  239. typedef struct {
  240. UA_NODE_BASEATTRIBUTES
  241. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  242. struct UA_MonitoredItem *monitoredItemQueue;
  243. #endif
  244. UA_Byte eventNotifier;
  245. } UA_ObjectNode;
  246. /**
  247. * .. _objecttypenode:
  248. *
  249. * ObjectTypeNode
  250. * --------------
  251. *
  252. * ObjectTypes provide definitions for Objects. Abstract objects cannot be
  253. * instantiated. See :ref:`node-lifecycle` for the use of constructor and
  254. * destructor callbacks. */
  255. typedef struct {
  256. UA_NODE_BASEATTRIBUTES
  257. UA_Boolean isAbstract;
  258. /* Members specific to open62541 */
  259. UA_NodeTypeLifecycle lifecycle;
  260. } UA_ObjectTypeNode;
  261. /**
  262. * .. _referencetypenode:
  263. *
  264. * ReferenceTypeNode
  265. * -----------------
  266. *
  267. * Each reference between two nodes is typed with a ReferenceType that gives
  268. * meaning to the relation. The OPC UA standard defines a set of ReferenceTypes
  269. * as a mandatory part of OPC UA information models.
  270. *
  271. * - Abstract ReferenceTypes cannot be used in actual references and are only
  272. * used to structure the ReferenceTypes hierarchy
  273. * - Symmetric references have the same meaning from the perspective of the
  274. * source and target node
  275. *
  276. * The figure below shows the hierarchy of the standard ReferenceTypes (arrows
  277. * indicate a ``hasSubType`` relation). Refer to Part 3 of the OPC UA
  278. * specification for the full semantics of each ReferenceType.
  279. *
  280. * .. graphviz::
  281. *
  282. * digraph tree {
  283. *
  284. * node [height=0, shape=box, fillcolor="#E5E5E5", concentrate=true]
  285. *
  286. * references [label="References\n(Abstract, Symmetric)"]
  287. * hierarchical_references [label="HierarchicalReferences\n(Abstract)"]
  288. * references -> hierarchical_references
  289. *
  290. * nonhierarchical_references [label="NonHierarchicalReferences\n(Abstract, Symmetric)"]
  291. * references -> nonhierarchical_references
  292. *
  293. * haschild [label="HasChild\n(Abstract)"]
  294. * hierarchical_references -> haschild
  295. *
  296. * aggregates [label="Aggregates\n(Abstract)"]
  297. * haschild -> aggregates
  298. *
  299. * organizes [label="Organizes"]
  300. * hierarchical_references -> organizes
  301. *
  302. * hascomponent [label="HasComponent"]
  303. * aggregates -> hascomponent
  304. *
  305. * hasorderedcomponent [label="HasOrderedComponent"]
  306. * hascomponent -> hasorderedcomponent
  307. *
  308. * hasproperty [label="HasProperty"]
  309. * aggregates -> hasproperty
  310. *
  311. * hassubtype [label="HasSubtype"]
  312. * haschild -> hassubtype
  313. *
  314. * hasmodellingrule [label="HasModellingRule"]
  315. * nonhierarchical_references -> hasmodellingrule
  316. *
  317. * hastypedefinition [label="HasTypeDefinition"]
  318. * nonhierarchical_references -> hastypedefinition
  319. *
  320. * hasencoding [label="HasEncoding"]
  321. * nonhierarchical_references -> hasencoding
  322. *
  323. * hasdescription [label="HasDescription"]
  324. * nonhierarchical_references -> hasdescription
  325. *
  326. * haseventsource [label="HasEventSource"]
  327. * hierarchical_references -> haseventsource
  328. *
  329. * hasnotifier [label="HasNotifier"]
  330. * hierarchical_references -> hasnotifier
  331. *
  332. * generatesevent [label="GeneratesEvent"]
  333. * nonhierarchical_references -> generatesevent
  334. *
  335. * alwaysgeneratesevent [label="AlwaysGeneratesEvent"]
  336. * generatesevent -> alwaysgeneratesevent
  337. *
  338. * {rank=same hierarchical_references nonhierarchical_references}
  339. * {rank=same generatesevent haseventsource hasmodellingrule
  340. * hasencoding hassubtype}
  341. * {rank=same alwaysgeneratesevent hasproperty}
  342. *
  343. * }
  344. *
  345. * The ReferenceType hierarchy can be extended with user-defined ReferenceTypes.
  346. * Many Companion Specifications for OPC UA define new ReferenceTypes to be used
  347. * in their domain of interest.
  348. *
  349. * For the following example of custom ReferenceTypes, we attempt to model the
  350. * structure of a technical system. For this, we introduce two custom
  351. * ReferenceTypes. First, the hierarchical ``contains`` ReferenceType indicates
  352. * that a system (represented by an OPC UA object) contains a component (or
  353. * subsystem). This gives rise to a tree-structure of containment relations. For
  354. * example, the motor (object) is contained in the car and the crankshaft is
  355. * contained in the motor. Second, the symmetric ``connectedTo`` ReferenceType
  356. * indicates that two components are connected. For example, the motor's
  357. * crankshaft is connected to the gear box. Connections are independent of the
  358. * containment hierarchy and can induce a general graph-structure. Further
  359. * subtypes of ``connectedTo`` could be used to differentiate between physical,
  360. * electrical and information related connections. A client can then learn the
  361. * layout of a (physical) system represented in an OPC UA information model
  362. * based on a common understanding of just two custom reference types. */
  363. typedef struct {
  364. UA_NODE_BASEATTRIBUTES
  365. UA_Boolean isAbstract;
  366. UA_Boolean symmetric;
  367. UA_LocalizedText inverseName;
  368. } UA_ReferenceTypeNode;
  369. /**
  370. * .. _datatypenode:
  371. *
  372. * DataTypeNode
  373. * ------------
  374. *
  375. * DataTypes represent simple and structured data types. DataTypes may contain
  376. * arrays. But they always describe the structure of a single instance. In
  377. * open62541, DataTypeNodes in the information model hierarchy are matched to
  378. * ``UA_DataType`` type descriptions for :ref:`generic-types` via their NodeId.
  379. *
  380. * Abstract DataTypes (e.g. ``Number``) cannot be the type of actual values.
  381. * They are used to constrain values to possible child DataTypes (e.g.
  382. * ``UInt32``). */
  383. typedef struct {
  384. UA_NODE_BASEATTRIBUTES
  385. UA_Boolean isAbstract;
  386. } UA_DataTypeNode;
  387. /**
  388. * ViewNode
  389. * --------
  390. *
  391. * Each View defines a subset of the Nodes in the AddressSpace. Views can be
  392. * used when browsing an information model to focus on a subset of nodes and
  393. * references only. ViewNodes can be created and be interacted with. But their
  394. * use in the :ref:`Browse<view-services>` service is currently unsupported in
  395. * open62541. */
  396. typedef struct {
  397. UA_NODE_BASEATTRIBUTES
  398. UA_Byte eventNotifier;
  399. UA_Boolean containsNoLoops;
  400. } UA_ViewNode;
  401. /**
  402. * Nodestore Plugin API
  403. * ====================
  404. *
  405. * The following definitions are used for implementing custom node storage
  406. * backends. **Most users will want to use the default nodestore and don't need
  407. * to work with the nodestore API**.
  408. *
  409. * Outside of custom nodestore implementations, users should not manually edit
  410. * nodes. Please use the OPC UA services for that. Otherwise, all consistency
  411. * checks are omitted. This can crash the application eventually. */
  412. /* For non-multithreaded access, some nodestores allow that nodes are edited
  413. * without a copy/replace. This is not possible when the node is only an
  414. * intermediate representation and stored e.g. in a database backend. */
  415. extern const UA_Boolean inPlaceEditAllowed;
  416. /* Nodestore context and lifecycle */
  417. UA_StatusCode UA_Nodestore_new(void **nsCtx);
  418. void UA_Nodestore_delete(void *nsCtx);
  419. /**
  420. * The following definitions are used to create empty nodes of the different
  421. * node types. The memory is managed by the nodestore. Therefore, the node has
  422. * to be removed via a special deleteNode function. (If the new node is not
  423. * added to the nodestore.) */
  424. UA_Node *
  425. UA_Nodestore_newNode(void *nsCtx, UA_NodeClass nodeClass);
  426. void
  427. UA_Nodestore_deleteNode(void *nsCtx, UA_Node *node);
  428. /**
  429. *``Get`` returns a pointer to an immutable node. ``Release`` indicates that the
  430. * pointer is no longer accessed afterwards. */
  431. const UA_Node *
  432. UA_Nodestore_getNode(void *nsCtx, const UA_NodeId *nodeId);
  433. void
  434. UA_Nodestore_releaseNode(void *nsCtx, const UA_Node *node);
  435. /* Returns an editable copy of a node (needs to be deleted with the
  436. * deleteNode function or inserted / replaced into the nodestore). */
  437. UA_StatusCode
  438. UA_Nodestore_getNodeCopy(void *nsCtx, const UA_NodeId *nodeId,
  439. UA_Node **outNode);
  440. /* Inserts a new node into the nodestore. If the NodeId is zero, then a fresh
  441. * numeric NodeId is assigned. If insertion fails, the node is deleted. */
  442. UA_StatusCode
  443. UA_Nodestore_insertNode(void *nsCtx, UA_Node *node, UA_NodeId *addedNodeId);
  444. /* To replace a node, get an editable copy of the node, edit and replace with
  445. * this function. If the node was already replaced since the copy was made,
  446. * UA_STATUSCODE_BADINTERNALERROR is returned. If the NodeId is not found,
  447. * UA_STATUSCODE_BADNODEIDUNKNOWN is returned. In both error cases, the editable
  448. * node is deleted. */
  449. UA_StatusCode
  450. UA_Nodestore_replaceNode(void *nsCtx, UA_Node *node);
  451. /* Removes a node from the nodestore. */
  452. UA_StatusCode
  453. UA_Nodestore_removeNode(void *nsCtx, const UA_NodeId *nodeId);
  454. /* Execute a callback for every node in the nodestore. */
  455. typedef void (*UA_NodestoreVisitor)(void *visitorCtx, const UA_Node *node);
  456. void
  457. UA_Nodestore_iterate(void *nsCtx, UA_NodestoreVisitor visitor,
  458. void *visitorCtx);
  459. /**
  460. * Node Handling
  461. * =============
  462. *
  463. * To be used only in the nodestore and internally in the SDK. The following
  464. * methods specialize internally for the different node classes, distinguished
  465. * by the NodeClass attribute. */
  466. /* Attributes must be of a matching type (VariableAttributes, ObjectAttributes,
  467. * and so on). The attributes are copied. Note that the attributes structs do
  468. * not contain NodeId, NodeClass and BrowseName. The NodeClass of the node needs
  469. * to be correctly set before calling this method. UA_Node_deleteMembers is
  470. * called on the node when an error occurs internally. */
  471. UA_StatusCode UA_EXPORT
  472. UA_Node_setAttributes(UA_Node *node, const void *attributes,
  473. const UA_DataType *attributeType);
  474. /* Reset the destination node and copy the content of the source */
  475. UA_StatusCode UA_EXPORT
  476. UA_Node_copy(const UA_Node *src, UA_Node *dst);
  477. /* Allocate new node and copy the values from src */
  478. UA_EXPORT UA_Node *
  479. UA_Node_copy_alloc(const UA_Node *src);
  480. /* Add a single reference to the node */
  481. UA_StatusCode UA_EXPORT
  482. UA_Node_addReference(UA_Node *node, const UA_AddReferencesItem *item);
  483. /* Delete a single reference from the node */
  484. UA_StatusCode UA_EXPORT
  485. UA_Node_deleteReference(UA_Node *node, const UA_DeleteReferencesItem *item);
  486. /* Delete all references of the node */
  487. void UA_EXPORT
  488. UA_Node_deleteReferences(UA_Node *node);
  489. /* Remove all malloc'ed members of the node */
  490. void UA_EXPORT
  491. UA_Node_deleteMembers(UA_Node *node);
  492. _UA_END_DECLS
  493. #endif /* UA_SERVER_NODES_H_ */