ua_server_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. */
  13. #ifndef UA_SERVER_INTERNAL_H_
  14. #define UA_SERVER_INTERNAL_H_
  15. #include "ua_util_internal.h"
  16. #include "ua_server.h"
  17. #include "ua_server_config.h"
  18. #include "ua_timer.h"
  19. #include "ua_connection_internal.h"
  20. #include "ua_session_manager.h"
  21. #include "ua_securechannel_manager.h"
  22. #include "ua_workqueue.h"
  23. _UA_BEGIN_DECLS
  24. #ifdef UA_ENABLE_PUBSUB
  25. #include "ua_pubsub_manager.h"
  26. #endif
  27. #ifdef UA_ENABLE_DISCOVERY
  28. #include "ua_discovery_manager.h"
  29. #endif
  30. #ifdef UA_ENABLE_SUBSCRIPTIONS
  31. #include "ua_subscription.h"
  32. typedef struct {
  33. UA_MonitoredItem monitoredItem;
  34. void *context;
  35. union {
  36. UA_Server_DataChangeNotificationCallback dataChangeCallback;
  37. /* UA_Server_EventNotificationCallback eventCallback; */
  38. } callback;
  39. } UA_LocalMonitoredItem;
  40. #endif
  41. struct UA_Server {
  42. /* Config */
  43. UA_ServerConfig config;
  44. UA_DateTime startTime;
  45. /* Security */
  46. UA_SecureChannelManager secureChannelManager;
  47. UA_SessionManager sessionManager;
  48. UA_Session adminSession; /* Local access to the services (for startup and
  49. * maintenance) uses this Session with all possible
  50. * access rights (Session Id: 1) */
  51. /* Namespaces */
  52. size_t namespacesSize;
  53. UA_String *namespaces;
  54. /* Callbacks with a repetition interval */
  55. UA_Timer timer;
  56. /* WorkQueue and worker threads */
  57. UA_WorkQueue workQueue;
  58. /* For bootstrapping, omit some consistency checks, creating a reference to
  59. * the parent and member instantiation */
  60. UA_Boolean bootstrapNS0;
  61. /* Discovery */
  62. #ifdef UA_ENABLE_DISCOVERY
  63. UA_DiscoveryManager discoveryManager;
  64. #endif
  65. /* Local MonitoredItems */
  66. #ifdef UA_ENABLE_SUBSCRIPTIONS
  67. /* To be cast to UA_LocalMonitoredItem to get the callback and context */
  68. LIST_HEAD(LocalMonitoredItems, UA_MonitoredItem) localMonitoredItems;
  69. UA_UInt32 lastLocalMonitoredItemId;
  70. #endif
  71. /* Publish/Subscribe */
  72. #ifdef UA_ENABLE_PUBSUB
  73. UA_PubSubManager pubSubManager;
  74. #endif
  75. };
  76. /*****************/
  77. /* Node Handling */
  78. /*****************/
  79. #define UA_Nodestore_get(SERVER, NODEID) \
  80. (SERVER)->config.nodestore.getNode((SERVER)->config.nodestore.context, NODEID)
  81. #define UA_Nodestore_release(SERVER, NODEID) \
  82. (SERVER)->config.nodestore.releaseNode((SERVER)->config.nodestore.context, NODEID)
  83. #define UA_Nodestore_new(SERVER, NODECLASS) \
  84. (SERVER)->config.nodestore.newNode((SERVER)->config.nodestore.context, NODECLASS)
  85. #define UA_Nodestore_getCopy(SERVER, NODEID, OUTNODE) \
  86. (SERVER)->config.nodestore.getNodeCopy((SERVER)->config.nodestore.context, NODEID, OUTNODE)
  87. #define UA_Nodestore_insert(SERVER, NODE, OUTNODEID) \
  88. (SERVER)->config.nodestore.insertNode((SERVER)->config.nodestore.context, NODE, OUTNODEID)
  89. #define UA_Nodestore_delete(SERVER, NODE) \
  90. (SERVER)->config.nodestore.deleteNode((SERVER)->config.nodestore.context, NODE)
  91. #define UA_Nodestore_remove(SERVER, NODEID) \
  92. (SERVER)->config.nodestore.removeNode((SERVER)->config.nodestore.context, NODEID)
  93. /* Deletes references from the node which are not matching any type in the given
  94. * array. Could be used to e.g. delete all the references, except
  95. * 'HASMODELINGRULE' */
  96. void UA_Node_deleteReferencesSubset(UA_Node *node, size_t referencesSkipSize,
  97. UA_NodeId* referencesSkip);
  98. /* Calls the callback with the node retrieved from the nodestore on top of the
  99. * stack. Either a copy or the original node for in-situ editing. Depends on
  100. * multithreading and the nodestore.*/
  101. typedef UA_StatusCode (*UA_EditNodeCallback)(UA_Server*, UA_Session*,
  102. UA_Node *node, void*);
  103. UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
  104. const UA_NodeId *nodeId,
  105. UA_EditNodeCallback callback,
  106. void *data);
  107. /*********************/
  108. /* Utility Functions */
  109. /*********************/
  110. /* A few global NodeId definitions */
  111. extern const UA_NodeId subtypeId;
  112. extern const UA_NodeId hierarchicalReferences;
  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(UA_Nodestore *ns, 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 type nodes. The returned array starts
  122. * at the leaf and continues "upwards" or "downwards" in the hierarchy based on the
  123. * ``hasSubType`` references. Since multiple-inheritance is possible in general,
  124. * duplicate entries are removed.
  125. * The parameter `walkDownwards` indicates the direction of search.
  126. * If set to TRUE it will get all the subtypes of the given
  127. * leafType (including leafType).
  128. * If set to FALSE it will get all the parent types of the given
  129. * leafType (including leafType)*/
  130. UA_StatusCode
  131. getTypeHierarchy(UA_Nodestore *ns, const UA_NodeId *leafType,
  132. UA_NodeId **typeHierarchy, size_t *typeHierarchySize,
  133. UA_Boolean walkDownwards);
  134. /* Same as getTypeHierarchy but takes multiple leafTypes as parameter and returns
  135. * an combined list of all the found types for all the leaf types */
  136. UA_StatusCode
  137. getTypesHierarchy(UA_Nodestore *ns, const UA_NodeId *leafType, size_t leafTypeSize,
  138. UA_NodeId **typeHierarchy, size_t *typeHierarchySize,
  139. UA_Boolean walkDownwards);
  140. /* Returns the type node from the node on the stack top. The type node is pushed
  141. * on the stack and returned. */
  142. const UA_Node * getNodeType(UA_Server *server, const UA_Node *node);
  143. /* Write a node attribute with a defined session */
  144. UA_StatusCode
  145. UA_Server_writeWithSession(UA_Server *server, UA_Session *session,
  146. const UA_WriteValue *value);
  147. /* Many services come as an array of operations. This function generalizes the
  148. * processing of the operations. */
  149. typedef void (*UA_ServiceOperation)(UA_Server *server, UA_Session *session,
  150. void *context,
  151. const void *requestOperation,
  152. void *responseOperation);
  153. UA_StatusCode
  154. UA_Server_processServiceOperations(UA_Server *server, UA_Session *session,
  155. UA_ServiceOperation operationCallback,
  156. void *context,
  157. const size_t *requestOperations,
  158. const UA_DataType *requestOperationsType,
  159. size_t *responseOperations,
  160. const UA_DataType *responseOperationsType)
  161. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  162. /***************************************/
  163. /* Check Information Model Consistency */
  164. /***************************************/
  165. /* Read a node attribute in the context of a "checked-out" node. So the
  166. * attribute will not be copied when possible. The variant then points into the
  167. * node and has UA_VARIANT_DATA_NODELETE set. */
  168. void
  169. ReadWithNode(const UA_Node *node, UA_Server *server, UA_Session *session,
  170. UA_TimestampsToReturn timestampsToReturn,
  171. const UA_ReadValueId *id, UA_DataValue *v);
  172. UA_StatusCode
  173. readValueAttribute(UA_Server *server, UA_Session *session,
  174. const UA_VariableNode *vn, UA_DataValue *v);
  175. /* Test whether the value matches a variable definition given by
  176. * - datatype
  177. * - valueranke
  178. * - array dimensions.
  179. * Sometimes it can be necessary to transform the content of the value, e.g.
  180. * byte array to bytestring or uint32 to some enum. If editableValue is non-NULL,
  181. * we try to create a matching variant that points to the original data. */
  182. UA_Boolean
  183. compatibleValue(UA_Server *server, UA_Session *session, const UA_NodeId *targetDataTypeId,
  184. UA_Int32 targetValueRank, size_t targetArrayDimensionsSize,
  185. const UA_UInt32 *targetArrayDimensions, const UA_Variant *value,
  186. const UA_NumericRange *range);
  187. UA_Boolean
  188. compatibleArrayDimensions(size_t constraintArrayDimensionsSize,
  189. const UA_UInt32 *constraintArrayDimensions,
  190. size_t testArrayDimensionsSize,
  191. const UA_UInt32 *testArrayDimensions);
  192. UA_Boolean
  193. compatibleValueArrayDimensions(const UA_Variant *value, size_t targetArrayDimensionsSize,
  194. const UA_UInt32 *targetArrayDimensions);
  195. UA_Boolean
  196. compatibleValueRankArrayDimensions(UA_Server *server, UA_Session *session,
  197. UA_Int32 valueRank, size_t arrayDimensionsSize);
  198. UA_Boolean
  199. compatibleDataType(UA_Server *server, const UA_NodeId *dataType,
  200. const UA_NodeId *constraintDataType, UA_Boolean isValue);
  201. UA_Boolean
  202. compatibleValueRanks(UA_Int32 valueRank, UA_Int32 constraintValueRank);
  203. void
  204. Operation_Browse(UA_Server *server, UA_Session *session, const UA_UInt32 *maxrefs,
  205. const UA_BrowseDescription *descr, UA_BrowseResult *result);
  206. UA_DataValue
  207. UA_Server_readWithSession(UA_Server *server, UA_Session *session,
  208. const UA_ReadValueId *item,
  209. UA_TimestampsToReturn timestampsToReturn);
  210. /*****************************/
  211. /* AddNodes Begin and Finish */
  212. /*****************************/
  213. /* Creates a new node in the nodestore. */
  214. UA_StatusCode
  215. AddNode_raw(UA_Server *server, UA_Session *session, void *nodeContext,
  216. const UA_AddNodesItem *item, UA_NodeId *outNewNodeId);
  217. /* Check the reference to the parent node; Add references. */
  218. UA_StatusCode
  219. AddNode_addRefs(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId,
  220. const UA_NodeId *parentNodeId, const UA_NodeId *referenceTypeId,
  221. const UA_NodeId *typeDefinitionId);
  222. /* Type-check type-definition; Run the constructors */
  223. UA_StatusCode
  224. AddNode_finish(UA_Server *server, UA_Session *session, const UA_NodeId *nodeId);
  225. /**********************/
  226. /* Create Namespace 0 */
  227. /**********************/
  228. UA_StatusCode UA_Server_initNS0(UA_Server *server);
  229. _UA_END_DECLS
  230. #endif /* UA_SERVER_INTERNAL_H_ */