ua_subscription.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 2015-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2015 (c) Chris Iatrou
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2015 (c) Oleksiy Vasylyev
  9. * Copyright 2017 (c) Florian Palm
  10. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2017 (c) Mattias Bornhager
  12. */
  13. #ifndef UA_SUBSCRIPTION_H_
  14. #define UA_SUBSCRIPTION_H_
  15. #include <open62541/types.h>
  16. #include <open62541/types_generated.h>
  17. #include <open62541/plugin/nodestore.h>
  18. #include "ua_session.h"
  19. #include "ua_util_internal.h"
  20. #include "ua_workqueue.h"
  21. _UA_BEGIN_DECLS
  22. #ifdef UA_ENABLE_SUBSCRIPTIONS
  23. /**
  24. * MonitoredItems create Notifications. Subscriptions collect Notifications from
  25. * (several) MonitoredItems and publish them to the client.
  26. *
  27. * Notifications are put into two queues at the same time. One for the
  28. * MonitoredItem that generated the notification. Here we can remove it if the
  29. * space reserved for the MonitoredItem runs full. The second queue is the
  30. * "global" queue for all Notifications generated in a Subscription. For
  31. * publication, the notifications are taken out of the "global" queue in the
  32. * order of their creation.
  33. */
  34. /*****************/
  35. /* MonitoredItem */
  36. /*****************/
  37. struct UA_MonitoredItem;
  38. typedef struct UA_MonitoredItem UA_MonitoredItem;
  39. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  40. typedef struct UA_EventNotification {
  41. UA_EventFieldList fields;
  42. /* EventFilterResult currently isn't being used
  43. UA_EventFilterResult result; */
  44. } UA_EventNotification;
  45. #endif
  46. typedef struct UA_Notification {
  47. TAILQ_ENTRY(UA_Notification) listEntry; /* Notification list for the MonitoredItem */
  48. TAILQ_ENTRY(UA_Notification) globalEntry; /* Notification list for the Subscription */
  49. UA_MonitoredItem *mon;
  50. /* See the monitoredItemType of the MonitoredItem */
  51. union {
  52. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  53. UA_EventNotification event;
  54. #endif
  55. UA_DataValue value;
  56. } data;
  57. } UA_Notification;
  58. /* Ensure enough space is available; Add notification to the linked lists;
  59. * Increase the counters */
  60. void UA_Notification_enqueue(UA_Server *server, UA_Subscription *sub,
  61. UA_MonitoredItem *mon, UA_Notification *n);
  62. /* Remove the notification from the MonitoredItem's queue and the Subscriptions
  63. * global queue. Reduce the respective counters. */
  64. void UA_Notification_dequeue(UA_Server *server, UA_Notification *n);
  65. /* Delete the notification. Must be dequeued first. */
  66. void UA_Notification_delete(UA_Notification *n);
  67. typedef TAILQ_HEAD(NotificationQueue, UA_Notification) NotificationQueue;
  68. struct UA_MonitoredItem {
  69. UA_DelayedCallback delayedFreePointers;
  70. LIST_ENTRY(UA_MonitoredItem) listEntry;
  71. UA_Subscription *subscription; /* Local MonitoredItem if the subscription is NULL */
  72. UA_UInt32 monitoredItemId;
  73. UA_UInt32 clientHandle;
  74. UA_Boolean registered; /* Was the MonitoredItem registered in Userland with
  75. the callback? */
  76. /* Settings */
  77. UA_TimestampsToReturn timestampsToReturn;
  78. UA_MonitoringMode monitoringMode;
  79. UA_NodeId monitoredNodeId;
  80. UA_UInt32 attributeId;
  81. UA_String indexRange;
  82. UA_Double samplingInterval; // [ms]
  83. UA_Boolean discardOldest;
  84. union {
  85. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  86. UA_EventFilter eventFilter; /* If attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER */
  87. #endif
  88. UA_DataChangeFilter dataChangeFilter;
  89. } filter;
  90. UA_Variant lastValue;
  91. // TODO: dataEncoding is hardcoded to UA binary
  92. /* Sample Callback */
  93. UA_UInt64 sampleCallbackId;
  94. UA_ByteString lastSampledValue;
  95. UA_Boolean sampleCallbackIsRegistered;
  96. /* Notification Queue */
  97. NotificationQueue queue;
  98. UA_UInt32 maxQueueSize; /* The max number of enqueued notifications (not
  99. * counting overflow events) */
  100. UA_UInt32 queueSize;
  101. UA_UInt32 eventOverflows; /* Separate counter for the queue. Can at most
  102. * double the queue size */
  103. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  104. UA_MonitoredItem *next;
  105. #endif
  106. #ifdef UA_ENABLE_DA
  107. UA_StatusCode lastStatus;
  108. #endif
  109. };
  110. void UA_MonitoredItem_init(UA_MonitoredItem *mon, UA_Subscription *sub);
  111. void UA_MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem);
  112. void UA_MonitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem);
  113. UA_StatusCode UA_MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  114. void UA_MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  115. /* Remove entries until mon->maxQueueSize is reached. Sets infobits for lost
  116. * data if required. */
  117. UA_StatusCode UA_MonitoredItem_ensureQueueSpace(UA_Server *server, UA_MonitoredItem *mon);
  118. UA_StatusCode UA_MonitoredItem_removeNodeEventCallback(UA_Server *server, UA_Session *session,
  119. UA_Node *node, void *data);
  120. /****************/
  121. /* Subscription */
  122. /****************/
  123. typedef struct UA_NotificationMessageEntry {
  124. TAILQ_ENTRY(UA_NotificationMessageEntry) listEntry;
  125. UA_NotificationMessage message;
  126. } UA_NotificationMessageEntry;
  127. /* We use only a subset of the states defined in the standard */
  128. typedef enum {
  129. /* UA_SUBSCRIPTIONSTATE_CLOSED */
  130. /* UA_SUBSCRIPTIONSTATE_CREATING */
  131. UA_SUBSCRIPTIONSTATE_NORMAL,
  132. UA_SUBSCRIPTIONSTATE_LATE,
  133. UA_SUBSCRIPTIONSTATE_KEEPALIVE
  134. } UA_SubscriptionState;
  135. typedef TAILQ_HEAD(ListOfNotificationMessages, UA_NotificationMessageEntry) ListOfNotificationMessages;
  136. struct UA_Subscription {
  137. UA_DelayedCallback delayedFreePointers;
  138. LIST_ENTRY(UA_Subscription) listEntry;
  139. UA_Session *session;
  140. UA_UInt32 subscriptionId;
  141. /* Settings */
  142. UA_UInt32 lifeTimeCount;
  143. UA_UInt32 maxKeepAliveCount;
  144. UA_Double publishingInterval; /* in ms */
  145. UA_UInt32 notificationsPerPublish;
  146. UA_Boolean publishingEnabled;
  147. UA_UInt32 priority;
  148. /* Runtime information */
  149. UA_SubscriptionState state;
  150. UA_UInt32 nextSequenceNumber;
  151. UA_UInt32 currentKeepAliveCount;
  152. UA_UInt32 currentLifetimeCount;
  153. /* Publish Callback */
  154. UA_UInt64 publishCallbackId;
  155. UA_Boolean publishCallbackIsRegistered;
  156. /* MonitoredItems */
  157. UA_UInt32 lastMonitoredItemId; /* increase the identifiers */
  158. LIST_HEAD(, UA_MonitoredItem) monitoredItems;
  159. UA_UInt32 monitoredItemsSize;
  160. /* Global list of notifications from the MonitoredItems */
  161. NotificationQueue notificationQueue;
  162. UA_UInt32 notificationQueueSize; /* Total queue size */
  163. UA_UInt32 dataChangeNotifications;
  164. UA_UInt32 eventNotifications;
  165. UA_UInt32 statusChangeNotifications;
  166. /* Notifications to be sent out now (already late). In a regular publish
  167. * callback, all queued notifications are sent out. In a late publish
  168. * response, only the notifications left from the last regular publish
  169. * callback are sent. */
  170. UA_UInt32 readyNotifications;
  171. /* Retransmission Queue */
  172. ListOfNotificationMessages retransmissionQueue;
  173. size_t retransmissionQueueSize;
  174. };
  175. UA_Subscription * UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId);
  176. void UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub);
  177. UA_StatusCode Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub);
  178. void Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub);
  179. void UA_Subscription_addMonitoredItem(UA_Server *server, UA_Subscription *sub, UA_MonitoredItem *newMon);
  180. UA_MonitoredItem * UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId);
  181. UA_StatusCode
  182. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  183. UA_UInt32 monitoredItemId);
  184. void UA_Subscription_publish(UA_Server *server, UA_Subscription *sub);
  185. UA_StatusCode UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub,
  186. UA_UInt32 sequenceNumber);
  187. void UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session);
  188. UA_Boolean UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session);
  189. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  190. _UA_END_DECLS
  191. #endif /* UA_SUBSCRIPTION_H_ */