ua_server_internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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_asyncmethod_manager.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. #if UA_MULTITHREADING >= 100
  54. struct AsyncMethodQueueElement {
  55. UA_CallMethodRequest m_Request;
  56. UA_CallMethodResult m_Response;
  57. UA_DateTime m_tDispatchTime;
  58. UA_UInt32 m_nRequestId;
  59. UA_NodeId m_nSessionId;
  60. UA_UInt32 m_nIndex;
  61. SIMPLEQ_ENTRY(AsyncMethodQueueElement) next;
  62. };
  63. /* Internal Helper to transfer info */
  64. struct AsyncMethodContextInternal {
  65. UA_UInt32 nRequestId;
  66. UA_NodeId nSessionId;
  67. UA_UInt32 nIndex;
  68. const UA_CallRequest* pRequest;
  69. UA_SecureChannel* pChannel;
  70. };
  71. #endif
  72. struct UA_Server {
  73. /* Config */
  74. UA_ServerConfig config;
  75. UA_DateTime startTime;
  76. UA_DateTime endTime; /* Zeroed out. If a time is set, then the server shuts
  77. * down once the time has been reached */
  78. /* Nodestore */
  79. void *nsCtx;
  80. UA_ServerLifecycle state;
  81. /* Security */
  82. UA_SecureChannelManager secureChannelManager;
  83. UA_SessionManager sessionManager;
  84. #if UA_MULTITHREADING >= 100
  85. UA_AsyncMethodManager asyncMethodManager;
  86. #endif
  87. UA_Session adminSession; /* Local access to the services (for startup and
  88. * maintenance) uses this Session with all possible
  89. * access rights (Session Id: 1) */
  90. /* Namespaces */
  91. size_t namespacesSize;
  92. UA_String *namespaces;
  93. /* Callbacks with a repetition interval */
  94. UA_Timer timer;
  95. /* WorkQueue and worker threads */
  96. UA_WorkQueue workQueue;
  97. /* For bootstrapping, omit some consistency checks, creating a reference to
  98. * the parent and member instantiation */
  99. UA_Boolean bootstrapNS0;
  100. /* Discovery */
  101. #ifdef UA_ENABLE_DISCOVERY
  102. UA_DiscoveryManager discoveryManager;
  103. #endif
  104. /* DataChange Subscriptions */
  105. #ifdef UA_ENABLE_SUBSCRIPTIONS
  106. /* Num active subscriptions */
  107. UA_UInt32 numSubscriptions;
  108. /* Num active monitored items */
  109. UA_UInt32 numMonitoredItems;
  110. /* To be cast to UA_LocalMonitoredItem to get the callback and context */
  111. LIST_HEAD(LocalMonitoredItems, UA_MonitoredItem) localMonitoredItems;
  112. UA_UInt32 lastLocalMonitoredItemId;
  113. #endif
  114. /* Publish/Subscribe */
  115. #ifdef UA_ENABLE_PUBSUB
  116. UA_PubSubManager pubSubManager;
  117. #endif
  118. #if UA_MULTITHREADING >= 100
  119. UA_LOCK_TYPE(networkMutex)
  120. UA_LOCK_TYPE(serviceMutex)
  121. /* Async Method Handling */
  122. UA_UInt32 nMQCurSize; /* actual size of queue */
  123. UA_UInt64 nCBIdIntegrity; /* id of callback queue check callback */
  124. UA_UInt64 nCBIdResponse; /* id of callback check for a response */
  125. UA_LOCK_TYPE(ua_request_queue_lock)
  126. UA_LOCK_TYPE(ua_response_queue_lock)
  127. UA_LOCK_TYPE(ua_pending_list_lock)
  128. SIMPLEQ_HEAD(ua_method_request_queue, AsyncMethodQueueElement) ua_method_request_queue;
  129. SIMPLEQ_HEAD(ua_method_response_queue, AsyncMethodQueueElement) ua_method_response_queue;
  130. SIMPLEQ_HEAD(ua_method_pending_list, AsyncMethodQueueElement) ua_method_pending_list;
  131. #endif /* UA_MULTITHREADING >= 100 */
  132. };
  133. /*****************/
  134. /* Node Handling */
  135. /*****************/
  136. /* Deletes references from the node which are not matching any type in the given
  137. * array. Could be used to e.g. delete all the references, except
  138. * 'HASMODELINGRULE' */
  139. void UA_Node_deleteReferencesSubset(UA_Node *node, size_t referencesSkipSize,
  140. UA_NodeId* referencesSkip);
  141. /* Calls the callback with the node retrieved from the nodestore on top of the
  142. * stack. Either a copy or the original node for in-situ editing. Depends on
  143. * multithreading and the nodestore.*/
  144. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*,
  145. UA_Node *node, void*);
  146. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
  147. const UA_NodeId *nodeId,
  148. UA_EditNodeCallback callback,
  149. void *data);
  150. /*********************/
  151. /* Utility Functions */
  152. /*********************/
  153. /* A few global NodeId definitions */
  154. extern const UA_NodeId subtypeId;
  155. extern const UA_NodeId hierarchicalReferences;
  156. void setupNs1Uri(UA_Server *server);
  157. UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
  158. UA_Boolean
  159. UA_Node_hasSubTypeOrInstances(const UA_Node *node);
  160. /* Recursively searches "upwards" in the tree following specific reference types */
  161. UA_Boolean
  162. isNodeInTree(void *nsCtx, const UA_NodeId *leafNode,
  163. const UA_NodeId *nodeToFind, const UA_NodeId *referenceTypeIds,
  164. size_t referenceTypeIdsSize);
  165. /* Returns an array with the hierarchy of nodes. The start nodes are returned as
  166. * well. The returned array starts at the leaf and continues "upwards" or
  167. * "downwards". Duplicate entries are removed. The parameter `walkDownwards`
  168. * indicates the direction of search. */
  169. UA_StatusCode
  170. browseRecursive(UA_Server *server,
  171. size_t startNodesSize, const UA_NodeId *startNodes,
  172. size_t refTypesSize, const UA_NodeId *refTypes,
  173. UA_BrowseDirection browseDirection, UA_Boolean includeStartNodes,
  174. size_t *resultsSize, UA_ExpandedNodeId **results);
  175. /* If refTypes is non-NULL, tries to realloc and increase the length */
  176. UA_StatusCode
  177. referenceSubtypes(UA_Server *server, const UA_NodeId *refType,
  178. size_t *refTypesSize, UA_NodeId **refTypes);
  179. /* Returns the recursive type and interface hierarchy of the node */
  180. UA_StatusCode
  181. getParentTypeAndInterfaceHierarchy(UA_Server *server, const UA_NodeId *typeNode,
  182. UA_NodeId **typeHierarchy, size_t *typeHierarchySize);
  183. /* Returns the type node from the node on the stack top. The type node is pushed
  184. * on the stack and returned. */
  185. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  186. /* Write a node attribute with a defined session */
  187. UA_StatusCode
  188. writeWithSession(UA_Server *server, UA_Session *session,
  189. const UA_WriteValue *value);
  190. #if UA_MULTITHREADING >= 100
  191. void
  192. UA_Server_InsertMethodResponse(UA_Server *server, const UA_UInt32 nRequestId,
  193. const UA_NodeId* nSessionId, const UA_UInt32 nIndex,
  194. const UA_CallMethodResult* response);
  195. void
  196. UA_Server_CallMethodResponse(UA_Server *server, void* data);
  197. #endif
  198. UA_StatusCode
  199. sendResponse(UA_SecureChannel *channel, UA_UInt32 requestId, UA_UInt32 requestHandle,
  200. UA_ResponseHeader *responseHeader, const UA_DataType *responseType);
  201. /* Many services come as an array of operations. This function generalizes the
  202. * processing of the operations. */
  203. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  204. const void *context,
  205. const void *requestOperation,
  206. void *responseOperation);
  207. UA_StatusCode
  208. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  209. UA_ServiceOperation operationCallback,
  210. const void *context,
  211. const size_t *requestOperations,
  212. const UA_DataType *requestOperationsType,
  213. size_t *responseOperations,
  214. const UA_DataType *responseOperationsType)
  215. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  216. UA_StatusCode
  217. UA_Server_processServiceOperationsAsync(UA_Server *server, UA_Session *session,
  218. UA_ServiceOperation operationCallback,
  219. void *context,
  220. const size_t *requestOperations,
  221. const UA_DataType *requestOperationsType,
  222. size_t *responseOperations,
  223. const UA_DataType *responseOperationsType)
  224. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  225. /******************************************/
  226. /* Internal function calls, without locks */
  227. /******************************************/
  228. UA_StatusCode
  229. deleteNode(UA_Server *server, const UA_NodeId nodeId,
  230. UA_Boolean deleteReferences);
  231. UA_StatusCode
  232. addNode(UA_Server *server, const UA_NodeClass nodeClass, const UA_NodeId *requestedNewNodeId,
  233. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  234. const UA_QualifiedName browseName, const UA_NodeId *typeDefinition,
  235. const UA_NodeAttributes *attr, const UA_DataType *attributeType,
  236. void *nodeContext, UA_NodeId *outNewNodeId);
  237. UA_StatusCode
  238. setVariableNode_dataSource(UA_Server *server, const UA_NodeId nodeId,
  239. const UA_DataSource dataSource);
  240. UA_StatusCode
  241. setMethodNode_callback(UA_Server *server,
  242. const UA_NodeId methodNodeId,
  243. UA_MethodCallback methodCallback);
  244. UA_StatusCode
  245. writeAttribute(UA_Server *server, const UA_WriteValue *value);
  246. UA_StatusCode
  247. writeWithWriteValue(UA_Server *server, const UA_NodeId *nodeId,
  248. const UA_AttributeId attributeId,
  249. const UA_DataType *attr_type,
  250. const void *attr);
  251. UA_DataValue
  252. readAttribute(UA_Server *server, const UA_ReadValueId *item,
  253. UA_TimestampsToReturn timestamps);
  254. UA_StatusCode
  255. readWithReadValue(UA_Server *server, const UA_NodeId *nodeId,
  256. const UA_AttributeId attributeId, void *v);
  257. UA_BrowsePathResult
  258. translateBrowsePathToNodeIds(UA_Server *server, const UA_BrowsePath *browsePath);
  259. #ifdef UA_ENABLE_SUBSCRIPTIONS
  260. void
  261. monitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem);
  262. #endif
  263. UA_BrowsePathResult
  264. browseSimplifiedBrowsePath(UA_Server *server, const UA_NodeId origin,
  265. size_t browsePathSize, const UA_QualifiedName *browsePath);
  266. UA_StatusCode
  267. writeObjectProperty(UA_Server *server, const UA_NodeId objectId,
  268. const UA_QualifiedName propertyName, const UA_Variant value);
  269. UA_StatusCode
  270. getNodeContext(UA_Server *server, UA_NodeId nodeId, void **nodeContext);
  271. void
  272. removeCallback(UA_Server *server, UA_UInt64 callbackId);
  273. UA_StatusCode
  274. changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId, UA_Double interval_ms);
  275. UA_StatusCode
  276. addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  277. void *data, UA_Double interval_ms, UA_UInt64 *callbackId);
  278. #ifdef UA_ENABLE_DISCOVERY
  279. UA_StatusCode
  280. register_server_with_discovery_server(UA_Server *server,
  281. void *client,
  282. const UA_Boolean isUnregister,
  283. const char* semaphoreFilePath);
  284. #endif
  285. /***************************************/
  286. /* Check Information Model Consistency */
  287. /***************************************/
  288. /* Read a node attribute in the context of a "checked-out" node. So the
  289. * attribute will not be copied when possible. The variant then points into the
  290. * node and has UA_VARIANT_DATA_NODELETE set. */
  291. void
  292. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  293. UA_TimestampsToReturn timestampsToReturn,
  294. const UA_ReadValueId *id, UA_DataValue *v);
  295. UA_StatusCode
  296. readValueAttribute(UA_Server *server, UA_Session *session,
  297. const UA_VariableNode *vn, UA_DataValue *v);
  298. /* Test whether the value matches a variable definition given by
  299. * - datatype
  300. * - valueranke
  301. * - array dimensions.
  302. * Sometimes it can be necessary to transform the content of the value, e.g.
  303. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  304. * we try to create a matching variant that points to the original data. */
  305. UA_Boolean
  306. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  307. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  308. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  309. const UA_NumericRange *range);
  310. UA_Boolean
  311. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  312. const UA_UInt32 *constraintArrayDimensions,
  313. size_t testArrayDimensionsSize,
  314. const UA_UInt32 *testArrayDimensions);
  315. UA_Boolean
  316. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  317. const UA_UInt32 *targetArrayDimensions);
  318. UA_Boolean
  319. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  320. UA_Int32 valueRank, size_t arrayDimensionsSize);
  321. UA_Boolean
  322. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  323. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  324. UA_Boolean
  325. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  326. struct BrowseOpts {
  327. UA_UInt32 maxReferences;
  328. UA_Boolean recursive;
  329. };
  330. void
  331. Operation_Browse(UA_Server *server, UA_Session *session, const UA_UInt32 *maxrefs,
  332. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  333. UA_DataValue
  334. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  335. const UA_ReadValueId *item,
  336. UA_TimestampsToReturn timestampsToReturn);
  337. /*****************************/
  338. /* AddNodes Begin and Finish */
  339. /*****************************/
  340. /* Creates a new node in the nodestore. */
  341. UA_StatusCode
  342. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  343. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  344. /* Check the reference to the parent node; Add references. */
  345. UA_StatusCode
  346. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  347. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  348. const UA_NodeId *typeDefinitionId);
  349. /* Type-check type-definition; Run the constructors */
  350. UA_StatusCode
  351. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  352. /**********************/
  353. /* Create Namespace 0 */
  354. /**********************/
  355. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  356. UA_StatusCode writeNs0VariableArray(UA_Server *server, UA_UInt32 id, void *v,
  357. size_t length, const UA_DataType *type);
  358. _UA_END_DECLS
  359. #endif /* UA_SERVER_INTERNAL_H_ */