ua_server_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014, 2017 (c) Florian Palm
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2015 (c) Chris Iatrou
  9. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  10. * Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2017 (c) Julian Grothoff
  12. * Copyright 2019 (c) Kalycito Infotech Private Limited
  13. */
  14. #ifndef UA_SERVER_INTERNAL_H_
  15. #define UA_SERVER_INTERNAL_H_
  16. #include <open62541/server.h>
  17. #include <open62541/server_config.h>
  18. #include <open62541/plugin/nodestore.h>
  19. #include "ua_connection_internal.h"
  20. #include "ua_securechannel_manager.h"
  21. #include "ua_session_manager.h"
  22. #include "ua_timer.h"
  23. #include "ua_util_internal.h"
  24. #include "ua_workqueue.h"
  25. _UA_BEGIN_DECLS
  26. #ifdef UA_ENABLE_PUBSUB
  27. #include "ua_pubsub_manager.h"
  28. #endif
  29. #ifdef UA_ENABLE_DISCOVERY
  30. #include "ua_discovery_manager.h"
  31. #endif
  32. #ifdef UA_ENABLE_SUBSCRIPTIONS
  33. #include "ua_subscription.h"
  34. typedef struct {
  35. UA_MonitoredItem monitoredItem;
  36. void *context;
  37. union {
  38. UA_Server_DataChangeNotificationCallback dataChangeCallback;
  39. /* UA_Server_EventNotificationCallback eventCallback; */
  40. } callback;
  41. } UA_LocalMonitoredItem;
  42. #endif
  43. typedef enum {
  44. UA_SERVERLIFECYCLE_FRESH,
  45. UA_SERVERLIFECYLE_RUNNING
  46. } UA_ServerLifecycle;
  47. struct UA_Server {
  48. /* Config */
  49. UA_ServerConfig config;
  50. UA_DateTime startTime;
  51. /* Nodestore */
  52. void *nsCtx;
  53. UA_ServerLifecycle state;
  54. /* Security */
  55. UA_SecureChannelManager secureChannelManager;
  56. UA_SessionManager sessionManager;
  57. UA_Session adminSession; /* Local access to the services (for startup and
  58. * maintenance) uses this Session with all possible
  59. * access rights (Session Id: 1) */
  60. /* Namespaces */
  61. size_t namespacesSize;
  62. UA_String *namespaces;
  63. /* Callbacks with a repetition interval */
  64. UA_Timer timer;
  65. /* WorkQueue and worker threads */
  66. UA_WorkQueue workQueue;
  67. /* For bootstrapping, omit some consistency checks, creating a reference to
  68. * the parent and member instantiation */
  69. UA_Boolean bootstrapNS0;
  70. /* Discovery */
  71. #ifdef UA_ENABLE_DISCOVERY
  72. UA_DiscoveryManager discoveryManager;
  73. #endif
  74. /* DataChange Subscriptions */
  75. #ifdef UA_ENABLE_SUBSCRIPTIONS
  76. /* Num active subscriptions */
  77. UA_UInt32 numSubscriptions;
  78. /* Num active monitored items */
  79. UA_UInt32 numMonitoredItems;
  80. /* To be cast to UA_LocalMonitoredItem to get the callback and context */
  81. LIST_HEAD(LocalMonitoredItems, UA_MonitoredItem) localMonitoredItems;
  82. UA_UInt32 lastLocalMonitoredItemId;
  83. #endif
  84. /* Publish/Subscribe */
  85. #ifdef UA_ENABLE_PUBSUB
  86. UA_PubSubManager pubSubManager;
  87. #endif
  88. };
  89. /*****************/
  90. /* Node Handling */
  91. /*****************/
  92. /* Deletes references from the node which are not matching any type in the given
  93. * array. Could be used to e.g. delete all the references, except
  94. * 'HASMODELINGRULE' */
  95. void UA_Node_deleteReferencesSubset(UA_Node *node, size_t referencesSkipSize,
  96. UA_NodeId* referencesSkip);
  97. /* Calls the callback with the node retrieved from the nodestore on top of the
  98. * stack. Either a copy or the original node for in-situ editing. Depends on
  99. * multithreading and the nodestore.*/
  100. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*,
  101. UA_Node *node, void*);
  102. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
  103. const UA_NodeId *nodeId,
  104. UA_EditNodeCallback callback,
  105. void *data);
  106. /*********************/
  107. /* Utility Functions */
  108. /*********************/
  109. /* A few global NodeId definitions */
  110. extern const UA_NodeId subtypeId;
  111. extern const UA_NodeId hierarchicalReferences;
  112. void setupNs1Uri(UA_Server *server);
  113. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  114. UA_Boolean
  115. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  116. /* Recursively searches "upwards" in the tree following specific reference types */
  117. UA_Boolean
  118. isNodeInTree(void *nsCtx, const UA_NodeId *leafNode,
  119. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  120. size_t referenceTypeIdsSize);
  121. /* Returns an array with the hierarchy of nodes. The start nodes are returned as
  122. * well. The returned array starts at the leaf and continues "upwards" or
  123. * "downwards". Duplicate entries are removed. The parameter `walkDownwards`
  124. * indicates the direction of search. */
  125. UA_StatusCode
  126. getLocalRecursiveHierarchy(UA_Server *server, const UA_NodeId *startNodes, size_t startNodesSize,
  127. const UA_NodeId *refTypes, size_t refTypesSize, UA_Boolean walkDownwards,
  128. UA_NodeId **results, size_t *resultsSize);
  129. /* Returns the recursive type and interface hierarchy of the node */
  130. UA_StatusCode
  131. getParentTypeAndInterfaceHierarchy(UA_Server *server, const UA_NodeId *typeNode,
  132. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  133. /* Returns the type node from the node on the stack top. The type node is pushed
  134. * on the stack and returned. */
  135. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  136. /* Write a node attribute with a defined session */
  137. UA_StatusCode
  138. UA_Server_writeWithSession(UA_Server *server, UA_Session *session,
  139. const UA_WriteValue *value);
  140. /* Many services come as an array of operations. This function generalizes the
  141. * processing of the operations. */
  142. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  143. void *context,
  144. const void *requestOperation,
  145. void *responseOperation);
  146. UA_StatusCode
  147. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  148. UA_ServiceOperation operationCallback,
  149. void *context,
  150. const size_t *requestOperations,
  151. const UA_DataType *requestOperationsType,
  152. size_t *responseOperations,
  153. const UA_DataType *responseOperationsType)
  154. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  155. /***************************************/
  156. /* Check Information Model Consistency */
  157. /***************************************/
  158. /* Read a node attribute in the context of a "checked-out" node. So the
  159. * attribute will not be copied when possible. The variant then points into the
  160. * node and has UA_VARIANT_DATA_NODELETE set. */
  161. void
  162. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  163. UA_TimestampsToReturn timestampsToReturn,
  164. const UA_ReadValueId *id, UA_DataValue *v);
  165. UA_StatusCode
  166. readValueAttribute(UA_Server *server, UA_Session *session,
  167. const UA_VariableNode *vn, UA_DataValue *v);
  168. /* Test whether the value matches a variable definition given by
  169. * - datatype
  170. * - valueranke
  171. * - array dimensions.
  172. * Sometimes it can be necessary to transform the content of the value, e.g.
  173. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  174. * we try to create a matching variant that points to the original data. */
  175. UA_Boolean
  176. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  177. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  178. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  179. const UA_NumericRange *range);
  180. UA_Boolean
  181. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  182. const UA_UInt32 *constraintArrayDimensions,
  183. size_t testArrayDimensionsSize,
  184. const UA_UInt32 *testArrayDimensions);
  185. UA_Boolean
  186. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  187. const UA_UInt32 *targetArrayDimensions);
  188. UA_Boolean
  189. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  190. UA_Int32 valueRank, size_t arrayDimensionsSize);
  191. UA_Boolean
  192. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  193. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  194. UA_Boolean
  195. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  196. struct BrowseOpts {
  197. UA_UInt32 maxReferences;
  198. UA_Boolean recursive;
  199. };
  200. void
  201. Operation_Browse(UA_Server *server, UA_Session *session, const struct BrowseOpts *maxrefs,
  202. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  203. UA_DataValue
  204. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  205. const UA_ReadValueId *item,
  206. UA_TimestampsToReturn timestampsToReturn);
  207. /*****************************/
  208. /* AddNodes Begin and Finish */
  209. /*****************************/
  210. /* Creates a new node in the nodestore. */
  211. UA_StatusCode
  212. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  213. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  214. /* Check the reference to the parent node; Add references. */
  215. UA_StatusCode
  216. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  217. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  218. const UA_NodeId *typeDefinitionId);
  219. /* Type-check type-definition; Run the constructors */
  220. UA_StatusCode
  221. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  222. /**********************/
  223. /* Create Namespace 0 */
  224. /**********************/
  225. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  226. UA_StatusCode writeNs0VariableArray(UA_Server *server, UA_UInt32 id, void *v,
  227. size_t length, const UA_DataType *type);
  228. _UA_END_DECLS
  229. #endif /* UA_SERVER_INTERNAL_H_ */