ua_server_internal.h 15 KB

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