ua_plugin_nodestore.h 20 KB

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