ua_plugin_nodestore.h 20 KB

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