ua_server_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #if UA_MULTITHREADING >= 100
  27. #undef UA_THREADSAFE
  28. #define UA_THREADSAFE UA_DEPRECATED
  29. #endif
  30. #ifdef UA_ENABLE_PUBSUB
  31. #include "ua_pubsub_manager.h"
  32. #endif
  33. #ifdef UA_ENABLE_DISCOVERY
  34. #include "ua_discovery_manager.h"
  35. #endif
  36. #ifdef UA_ENABLE_SUBSCRIPTIONS
  37. #include "ua_subscription.h"
  38. typedef struct {
  39. UA_MonitoredItem monitoredItem;
  40. void *context;
  41. union {
  42. UA_Server_DataChangeNotificationCallback dataChangeCallback;
  43. /* UA_Server_EventNotificationCallback eventCallback; */
  44. } callback;
  45. } UA_LocalMonitoredItem;
  46. #endif
  47. typedef enum {
  48. UA_SERVERLIFECYCLE_FRESH,
  49. UA_SERVERLIFECYLE_RUNNING
  50. } UA_ServerLifecycle;
  51. struct UA_Server {
  52. /* Config */
  53. UA_ServerConfig config;
  54. UA_DateTime startTime;
  55. UA_DateTime endTime; /* Zeroed out. If a time is set, then the server shuts
  56. * down once the time has been reached */
  57. /* Nodestore */
  58. void *nsCtx;
  59. UA_ServerLifecycle state;
  60. /* Security */
  61. UA_SecureChannelManager secureChannelManager;
  62. UA_SessionManager sessionManager;
  63. UA_Session adminSession; /* Local access to the services (for startup and
  64. * maintenance) uses this Session with all possible
  65. * access rights (Session Id: 1) */
  66. /* Namespaces */
  67. size_t namespacesSize;
  68. UA_String *namespaces;
  69. /* Callbacks with a repetition interval */
  70. UA_Timer timer;
  71. /* WorkQueue and worker threads */
  72. UA_WorkQueue workQueue;
  73. /* For bootstrapping, omit some consistency checks, creating a reference to
  74. * the parent and member instantiation */
  75. UA_Boolean bootstrapNS0;
  76. /* Discovery */
  77. #ifdef UA_ENABLE_DISCOVERY
  78. UA_DiscoveryManager discoveryManager;
  79. #endif
  80. /* DataChange Subscriptions */
  81. #ifdef UA_ENABLE_SUBSCRIPTIONS
  82. /* Num active subscriptions */
  83. UA_UInt32 numSubscriptions;
  84. /* Num active monitored items */
  85. UA_UInt32 numMonitoredItems;
  86. /* To be cast to UA_LocalMonitoredItem to get the callback and context */
  87. LIST_HEAD(LocalMonitoredItems, UA_MonitoredItem) localMonitoredItems;
  88. UA_UInt32 lastLocalMonitoredItemId;
  89. #endif
  90. /* Publish/Subscribe */
  91. #ifdef UA_ENABLE_PUBSUB
  92. UA_PubSubManager pubSubManager;
  93. #endif
  94. #if UA_MULTITHREADING >= 100
  95. UA_LOCK_TYPE(networkMutex)
  96. UA_LOCK_TYPE(serviceMutex)
  97. #endif
  98. };
  99. /*****************/
  100. /* Node Handling */
  101. /*****************/
  102. /* Deletes references from the node which are not matching any type in the given
  103. * array. Could be used to e.g. delete all the references, except
  104. * 'HASMODELINGRULE' */
  105. void UA_Node_deleteReferencesSubset(UA_Node *node, size_t referencesSkipSize,
  106. UA_NodeId* referencesSkip);
  107. /* Calls the callback with the node retrieved from the nodestore on top of the
  108. * stack. Either a copy or the original node for in-situ editing. Depends on
  109. * multithreading and the nodestore.*/
  110. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*,
  111. UA_Node *node, void*);
  112. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
  113. const UA_NodeId *nodeId,
  114. UA_EditNodeCallback callback,
  115. void *data);
  116. /*********************/
  117. /* Utility Functions */
  118. /*********************/
  119. /* A few global NodeId definitions */
  120. extern const UA_NodeId subtypeId;
  121. extern const UA_NodeId hierarchicalReferences;
  122. void setupNs1Uri(UA_Server *server);
  123. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  124. UA_Boolean
  125. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  126. /* Recursively searches "upwards" in the tree following specific reference types */
  127. UA_Boolean
  128. isNodeInTree(void *nsCtx, const UA_NodeId *leafNode,
  129. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  130. size_t referenceTypeIdsSize);
  131. /* Returns an array with the hierarchy of nodes. The start nodes are returned as
  132. * well. The returned array starts at the leaf and continues "upwards" or
  133. * "downwards". Duplicate entries are removed. The parameter `walkDownwards`
  134. * indicates the direction of search. */
  135. UA_StatusCode
  136. browseRecursive(UA_Server *server,
  137. size_t startNodesSize, const UA_NodeId *startNodes,
  138. size_t refTypesSize, const UA_NodeId *refTypes,
  139. UA_BrowseDirection browseDirection, UA_Boolean includeStartNodes,
  140. size_t *resultsSize, UA_ExpandedNodeId **results);
  141. /* If refTypes is non-NULL, tries to realloc and increase the length */
  142. UA_StatusCode
  143. referenceSubtypes(UA_Server *server, const UA_NodeId *refType,
  144. size_t *refTypesSize, UA_NodeId **refTypes);
  145. /* Returns the recursive type and interface hierarchy of the node */
  146. UA_StatusCode
  147. getParentTypeAndInterfaceHierarchy(UA_Server *server, const UA_NodeId *typeNode,
  148. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  149. /* Returns the type node from the node on the stack top. The type node is pushed
  150. * on the stack and returned. */
  151. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  152. /* Write a node attribute with a defined session */
  153. UA_StatusCode
  154. writeWithSession(UA_Server *server, UA_Session *session,
  155. const UA_WriteValue *value);
  156. /* Many services come as an array of operations. This function generalizes the
  157. * processing of the operations. */
  158. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  159. const void *context,
  160. const void *requestOperation,
  161. void *responseOperation);
  162. UA_StatusCode
  163. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  164. UA_ServiceOperation operationCallback,
  165. const void *context,
  166. const size_t *requestOperations,
  167. const UA_DataType *requestOperationsType,
  168. size_t *responseOperations,
  169. const UA_DataType *responseOperationsType)
  170. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  171. /******************************************/
  172. /* Internal function calls, without locks */
  173. /******************************************/
  174. UA_StatusCode
  175. deleteNode(UA_Server *server, const UA_NodeId nodeId,
  176. UA_Boolean deleteReferences);
  177. UA_StatusCode
  178. addNode(UA_Server *server, const UA_NodeClass nodeClass, const UA_NodeId *requestedNewNodeId,
  179. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  180. const UA_QualifiedName browseName, const UA_NodeId *typeDefinition,
  181. const UA_NodeAttributes *attr, const UA_DataType *attributeType,
  182. void *nodeContext, UA_NodeId *outNewNodeId);
  183. UA_StatusCode
  184. setVariableNode_dataSource(UA_Server *server, const UA_NodeId nodeId,
  185. const UA_DataSource dataSource);
  186. UA_StatusCode
  187. setMethodNode_callback(UA_Server *server,
  188. const UA_NodeId methodNodeId,
  189. UA_MethodCallback methodCallback);
  190. UA_StatusCode
  191. writeAttribute(UA_Server *server, const UA_WriteValue *value);
  192. UA_StatusCode
  193. writeWithWriteValue(UA_Server *server, const UA_NodeId *nodeId,
  194. const UA_AttributeId attributeId,
  195. const UA_DataType *attr_type,
  196. const void *attr);
  197. UA_DataValue
  198. readAttribute(UA_Server *server, const UA_ReadValueId *item,
  199. UA_TimestampsToReturn timestamps);
  200. UA_StatusCode
  201. readWithReadValue(UA_Server *server, const UA_NodeId *nodeId,
  202. const UA_AttributeId attributeId, void *v);
  203. UA_BrowsePathResult
  204. translateBrowsePathToNodeIds(UA_Server *server, const UA_BrowsePath *browsePath);
  205. void
  206. monitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem);
  207. UA_BrowsePathResult
  208. browseSimplifiedBrowsePath(UA_Server *server, const UA_NodeId origin,
  209. size_t browsePathSize, const UA_QualifiedName *browsePath);
  210. UA_StatusCode
  211. writeObjectProperty(UA_Server *server, const UA_NodeId objectId,
  212. const UA_QualifiedName propertyName, const UA_Variant value);
  213. /***************************************/
  214. /* Check Information Model Consistency */
  215. /***************************************/
  216. /* Read a node attribute in the context of a "checked-out" node. So the
  217. * attribute will not be copied when possible. The variant then points into the
  218. * node and has UA_VARIANT_DATA_NODELETE set. */
  219. void
  220. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  221. UA_TimestampsToReturn timestampsToReturn,
  222. const UA_ReadValueId *id, UA_DataValue *v);
  223. UA_StatusCode
  224. readValueAttribute(UA_Server *server, UA_Session *session,
  225. const UA_VariableNode *vn, UA_DataValue *v);
  226. /* Test whether the value matches a variable definition given by
  227. * - datatype
  228. * - valueranke
  229. * - array dimensions.
  230. * Sometimes it can be necessary to transform the content of the value, e.g.
  231. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  232. * we try to create a matching variant that points to the original data. */
  233. UA_Boolean
  234. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  235. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  236. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  237. const UA_NumericRange *range);
  238. UA_Boolean
  239. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  240. const UA_UInt32 *constraintArrayDimensions,
  241. size_t testArrayDimensionsSize,
  242. const UA_UInt32 *testArrayDimensions);
  243. UA_Boolean
  244. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  245. const UA_UInt32 *targetArrayDimensions);
  246. UA_Boolean
  247. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  248. UA_Int32 valueRank, size_t arrayDimensionsSize);
  249. UA_Boolean
  250. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  251. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  252. UA_Boolean
  253. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  254. struct BrowseOpts {
  255. UA_UInt32 maxReferences;
  256. UA_Boolean recursive;
  257. };
  258. void
  259. Operation_Browse(UA_Server *server, UA_Session *session, const UA_UInt32 *maxrefs,
  260. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  261. UA_DataValue
  262. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  263. const UA_ReadValueId *item,
  264. UA_TimestampsToReturn timestampsToReturn);
  265. /*****************************/
  266. /* AddNodes Begin and Finish */
  267. /*****************************/
  268. /* Creates a new node in the nodestore. */
  269. UA_StatusCode
  270. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  271. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  272. /* Check the reference to the parent node; Add references. */
  273. UA_StatusCode
  274. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  275. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  276. const UA_NodeId *typeDefinitionId);
  277. /* Type-check type-definition; Run the constructors */
  278. UA_StatusCode
  279. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  280. /**********************/
  281. /* Create Namespace 0 */
  282. /**********************/
  283. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  284. UA_StatusCode writeNs0VariableArray(UA_Server *server, UA_UInt32 id, void *v,
  285. size_t length, const UA_DataType *type);
  286. _UA_END_DECLS
  287. #endif /* UA_SERVER_INTERNAL_H_ */