ua_server_internal.h 9.9 KB

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