ua_server_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  112. UA_Boolean
  113. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  114. /* Recursively searches "upwards" in the tree following specific reference types */
  115. UA_Boolean
  116. isNodeInTree(void *nsCtx, const UA_NodeId *leafNode,
  117. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  118. size_t referenceTypeIdsSize);
  119. /* Returns an array with the hierarchy of type nodes. The returned array starts
  120. * at the leaf and continues "upwards" or "downwards" in the hierarchy based on the
  121. * ``hasSubType`` references. Since multiple-inheritance is possible in general,
  122. * duplicate entries are removed.
  123. * The parameter `walkDownwards` indicates the direction of search.
  124. * If set to TRUE it will get all the subtypes of the given
  125. * leafType (including leafType).
  126. * If set to FALSE it will get all the parent types of the given
  127. * leafType (including leafType)*/
  128. UA_StatusCode
  129. getTypeHierarchy(void *nsCtx, const UA_NodeId *leafType,
  130. UA_NodeId **typeHierarchy, size_t *typeHierarchySize,
  131. UA_Boolean walkDownwards);
  132. /* Same as getTypeHierarchy but takes multiple leafTypes as parameter and returns
  133. * an combined list of all the found types for all the leaf types */
  134. UA_StatusCode
  135. getTypesHierarchy(void *nsCtx, const UA_NodeId *leafType, size_t leafTypeSize,
  136. UA_NodeId **typeHierarchy, size_t *typeHierarchySize,
  137. UA_Boolean walkDownwards);
  138. /* Returns the type node from the node on the stack top. The type node is pushed
  139. * on the stack and returned. */
  140. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  141. /* Write a node attribute with a defined session */
  142. UA_StatusCode
  143. UA_Server_writeWithSession(UA_Server *server, UA_Session *session,
  144. const UA_WriteValue *value);
  145. /* Many services come as an array of operations. This function generalizes the
  146. * processing of the operations. */
  147. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  148. void *context,
  149. const void *requestOperation,
  150. void *responseOperation);
  151. UA_StatusCode
  152. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  153. UA_ServiceOperation operationCallback,
  154. void *context,
  155. const size_t *requestOperations,
  156. const UA_DataType *requestOperationsType,
  157. size_t *responseOperations,
  158. const UA_DataType *responseOperationsType)
  159. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  160. /***************************************/
  161. /* Check Information Model Consistency */
  162. /***************************************/
  163. /* Read a node attribute in the context of a "checked-out" node. So the
  164. * attribute will not be copied when possible. The variant then points into the
  165. * node and has UA_VARIANT_DATA_NODELETE set. */
  166. void
  167. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  168. UA_TimestampsToReturn timestampsToReturn,
  169. const UA_ReadValueId *id, UA_DataValue *v);
  170. UA_StatusCode
  171. readValueAttribute(UA_Server *server, UA_Session *session,
  172. const UA_VariableNode *vn, UA_DataValue *v);
  173. /* Test whether the value matches a variable definition given by
  174. * - datatype
  175. * - valueranke
  176. * - array dimensions.
  177. * Sometimes it can be necessary to transform the content of the value, e.g.
  178. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  179. * we try to create a matching variant that points to the original data. */
  180. UA_Boolean
  181. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  182. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  183. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  184. const UA_NumericRange *range);
  185. UA_Boolean
  186. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  187. const UA_UInt32 *constraintArrayDimensions,
  188. size_t testArrayDimensionsSize,
  189. const UA_UInt32 *testArrayDimensions);
  190. UA_Boolean
  191. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  192. const UA_UInt32 *targetArrayDimensions);
  193. UA_Boolean
  194. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  195. UA_Int32 valueRank, size_t arrayDimensionsSize);
  196. UA_Boolean
  197. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  198. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  199. UA_Boolean
  200. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  201. void
  202. Operation_Browse(UA_Server *server, UA_Session *session, const UA_UInt32 *maxrefs,
  203. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  204. UA_DataValue
  205. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  206. const UA_ReadValueId *item,
  207. UA_TimestampsToReturn timestampsToReturn);
  208. /*****************************/
  209. /* AddNodes Begin and Finish */
  210. /*****************************/
  211. /* Creates a new node in the nodestore. */
  212. UA_StatusCode
  213. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  214. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  215. /* Check the reference to the parent node; Add references. */
  216. UA_StatusCode
  217. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  218. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  219. const UA_NodeId *typeDefinitionId);
  220. /* Type-check type-definition; Run the constructors */
  221. UA_StatusCode
  222. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  223. /**********************/
  224. /* Create Namespace 0 */
  225. /**********************/
  226. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  227. _UA_END_DECLS
  228. #endif /* UA_SERVER_INTERNAL_H_ */