ua_server_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. browseRecursive(UA_Server *server,
  127. size_t startNodesSize, const UA_NodeId *startNodes,
  128. size_t refTypesSize, const UA_NodeId *refTypes,
  129. UA_BrowseDirection browseDirection, UA_Boolean includeStartNodes,
  130. size_t *resultsSize, UA_ExpandedNodeId **results);
  131. /* If refTypes is non-NULL, tries to realloc and increase the length */
  132. UA_StatusCode
  133. referenceSubtypes(UA_Server *server, const UA_NodeId *refType,
  134. size_t *refTypesSize, UA_NodeId **refTypes);
  135. /* Returns the recursive type and interface hierarchy of the node */
  136. UA_StatusCode
  137. getParentTypeAndInterfaceHierarchy(UA_Server *server, const UA_NodeId *typeNode,
  138. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  139. /* Returns the type node from the node on the stack top. The type node is pushed
  140. * on the stack and returned. */
  141. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  142. /* Write a node attribute with a defined session */
  143. UA_StatusCode
  144. UA_Server_writeWithSession(UA_Server *server, UA_Session *session,
  145. const UA_WriteValue *value);
  146. /* Many services come as an array of operations. This function generalizes the
  147. * processing of the operations. */
  148. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  149. const void *context,
  150. const void *requestOperation,
  151. void *responseOperation);
  152. UA_StatusCode
  153. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  154. UA_ServiceOperation operationCallback,
  155. const void *context,
  156. const size_t *requestOperations,
  157. const UA_DataType *requestOperationsType,
  158. size_t *responseOperations,
  159. const UA_DataType *responseOperationsType)
  160. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  161. /***************************************/
  162. /* Check Information Model Consistency */
  163. /***************************************/
  164. /* Read a node attribute in the context of a "checked-out" node. So the
  165. * attribute will not be copied when possible. The variant then points into the
  166. * node and has UA_VARIANT_DATA_NODELETE set. */
  167. void
  168. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  169. UA_TimestampsToReturn timestampsToReturn,
  170. const UA_ReadValueId *id, UA_DataValue *v);
  171. UA_StatusCode
  172. readValueAttribute(UA_Server *server, UA_Session *session,
  173. const UA_VariableNode *vn, UA_DataValue *v);
  174. /* Test whether the value matches a variable definition given by
  175. * - datatype
  176. * - valueranke
  177. * - array dimensions.
  178. * Sometimes it can be necessary to transform the content of the value, e.g.
  179. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  180. * we try to create a matching variant that points to the original data. */
  181. UA_Boolean
  182. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  183. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  184. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  185. const UA_NumericRange *range);
  186. UA_Boolean
  187. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  188. const UA_UInt32 *constraintArrayDimensions,
  189. size_t testArrayDimensionsSize,
  190. const UA_UInt32 *testArrayDimensions);
  191. UA_Boolean
  192. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  193. const UA_UInt32 *targetArrayDimensions);
  194. UA_Boolean
  195. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  196. UA_Int32 valueRank, size_t arrayDimensionsSize);
  197. UA_Boolean
  198. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  199. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  200. UA_Boolean
  201. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  202. struct BrowseOpts {
  203. UA_UInt32 maxReferences;
  204. UA_Boolean recursive;
  205. };
  206. void
  207. Operation_Browse(UA_Server *server, UA_Session *session, const UA_UInt32 *maxrefs,
  208. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  209. UA_DataValue
  210. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  211. const UA_ReadValueId *item,
  212. UA_TimestampsToReturn timestampsToReturn);
  213. /*****************************/
  214. /* AddNodes Begin and Finish */
  215. /*****************************/
  216. /* Creates a new node in the nodestore. */
  217. UA_StatusCode
  218. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  219. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  220. /* Check the reference to the parent node; Add references. */
  221. UA_StatusCode
  222. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  223. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  224. const UA_NodeId *typeDefinitionId);
  225. /* Type-check type-definition; Run the constructors */
  226. UA_StatusCode
  227. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  228. /**********************/
  229. /* Create Namespace 0 */
  230. /**********************/
  231. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  232. UA_StatusCode writeNs0VariableArray(UA_Server *server, UA_UInt32 id, void *v,
  233. size_t length, const UA_DataType *type);
  234. _UA_END_DECLS
  235. #endif /* UA_SERVER_INTERNAL_H_ */