ua_plugin_nodestore.h 19 KB

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