ua_server_internal.h 11 KB

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