ua_subscription.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-2017 (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 "ua_util.h"
  16. #include "ua_types.h"
  17. #include "ua_types_generated.h"
  18. #include "ua_session.h"
  19. /**
  20. * MonitoredItems create Notifications. Subscriptions collect Notifications from
  21. * (several) MonitoredItems and publish them to the client.
  22. *
  23. * Notifications are put into two queues at the same time. One for the
  24. * MonitoredItem that generated the notification. Here we can remove it if the
  25. * space reserved for the MonitoredItem runs full. The second queue is the
  26. * "global" queue for all Notifications generated in a Subscription. For
  27. * publication, the notifications are taken out of the "global" queue in the
  28. * order of their creation.
  29. */
  30. /*****************/
  31. /* MonitoredItem */
  32. /*****************/
  33. typedef enum {
  34. UA_MONITOREDITEMTYPE_CHANGENOTIFY = 1,
  35. UA_MONITOREDITEMTYPE_STATUSNOTIFY = 2,
  36. UA_MONITOREDITEMTYPE_EVENTNOTIFY = 4
  37. } UA_MonitoredItemType;
  38. struct UA_MonitoredItem;
  39. typedef struct UA_MonitoredItem UA_MonitoredItem;
  40. typedef struct UA_EventNotification {
  41. UA_EventFieldList fields;
  42. /* EventFilterResult currently isn't being used
  43. UA_EventFilterResult result; */
  44. } UA_EventNotification;
  45. typedef struct UA_Notification {
  46. TAILQ_ENTRY(UA_Notification) listEntry; /* Notification list for the MonitoredItem */
  47. TAILQ_ENTRY(UA_Notification) globalEntry; /* Notification list for the Subscription */
  48. UA_MonitoredItem *mon;
  49. /* See the monitoredItemType of the MonitoredItem */
  50. union {
  51. UA_EventNotification event;
  52. UA_DataValue value;
  53. } data;
  54. } UA_Notification;
  55. /* Clean up the notification. Must be removed from the lists first. */
  56. void UA_Notification_delete(UA_Notification *n);
  57. typedef TAILQ_HEAD(NotificationQueue, UA_Notification) NotificationQueue;
  58. struct UA_MonitoredItem {
  59. LIST_ENTRY(UA_MonitoredItem) listEntry;
  60. UA_Subscription *subscription;
  61. UA_UInt32 monitoredItemId;
  62. UA_UInt32 clientHandle;
  63. /* Settings */
  64. UA_MonitoredItemType monitoredItemType;
  65. UA_TimestampsToReturn timestampsToReturn;
  66. UA_MonitoringMode monitoringMode;
  67. UA_NodeId monitoredNodeId;
  68. UA_UInt32 attributeId;
  69. UA_String indexRange;
  70. UA_Double samplingInterval; // [ms]
  71. UA_UInt32 maxQueueSize;
  72. UA_Boolean discardOldest;
  73. // TODO: dataEncoding is hardcoded to UA binary
  74. union {
  75. UA_EventFilter eventFilter;
  76. UA_DataChangeFilter dataChangeFilter;
  77. } filter;
  78. UA_Variant lastValue;
  79. /* Sample Callback */
  80. UA_UInt64 sampleCallbackId;
  81. UA_ByteString lastSampledValue;
  82. UA_Boolean sampleCallbackIsRegistered;
  83. /* Notification Queue */
  84. NotificationQueue queue;
  85. UA_UInt32 queueSize;
  86. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  87. UA_MonitoredItem *next;
  88. #endif
  89. };
  90. void UA_MonitoredItem_init(UA_MonitoredItem *mon, UA_Subscription *sub);
  91. void UA_MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *mon);
  92. void UA_MonitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  93. UA_StatusCode UA_MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  94. UA_StatusCode UA_MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  95. /* Remove entries until mon->maxQueueSize is reached. Sets infobits for lost
  96. * data if required. */
  97. UA_StatusCode MonitoredItem_ensureQueueSpace(UA_Server *server, UA_MonitoredItem *mon);
  98. /****************/
  99. /* Subscription */
  100. /****************/
  101. typedef struct UA_NotificationMessageEntry {
  102. TAILQ_ENTRY(UA_NotificationMessageEntry) listEntry;
  103. UA_NotificationMessage message;
  104. } UA_NotificationMessageEntry;
  105. /* We use only a subset of the states defined in the standard */
  106. typedef enum {
  107. /* UA_SUBSCRIPTIONSTATE_CLOSED */
  108. /* UA_SUBSCRIPTIONSTATE_CREATING */
  109. UA_SUBSCRIPTIONSTATE_NORMAL,
  110. UA_SUBSCRIPTIONSTATE_LATE,
  111. UA_SUBSCRIPTIONSTATE_KEEPALIVE
  112. } UA_SubscriptionState;
  113. typedef TAILQ_HEAD(ListOfNotificationMessages, UA_NotificationMessageEntry) ListOfNotificationMessages;
  114. struct UA_Subscription {
  115. LIST_ENTRY(UA_Subscription) listEntry;
  116. UA_Session *session;
  117. UA_UInt32 subscriptionId;
  118. /* Settings */
  119. UA_UInt32 lifeTimeCount;
  120. UA_UInt32 maxKeepAliveCount;
  121. UA_Double publishingInterval; /* in ms */
  122. UA_UInt32 notificationsPerPublish;
  123. UA_Boolean publishingEnabled;
  124. UA_UInt32 priority;
  125. /* Runtime information */
  126. UA_SubscriptionState state;
  127. UA_UInt32 sequenceNumber;
  128. UA_UInt32 currentKeepAliveCount;
  129. UA_UInt32 currentLifetimeCount;
  130. /* Publish Callback */
  131. UA_UInt64 publishCallbackId;
  132. UA_Boolean publishCallbackIsRegistered;
  133. /* MonitoredItems */
  134. UA_UInt32 lastMonitoredItemId; /* increase the identifiers */
  135. LIST_HEAD(UA_ListOfUAMonitoredItems, UA_MonitoredItem) monitoredItems;
  136. UA_UInt32 monitoredItemsSize;
  137. /* Global list of notifications from the MonitoredItems */
  138. NotificationQueue notificationQueue;
  139. UA_UInt32 notificationQueueSize;
  140. UA_UInt32 readyNotifications; /* Notifications to be sent out now (already late) */
  141. /* Retransmission Queue */
  142. ListOfNotificationMessages retransmissionQueue;
  143. UA_UInt32 retransmissionQueueSize;
  144. };
  145. UA_Subscription * UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId);
  146. void UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub);
  147. UA_StatusCode Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub);
  148. UA_StatusCode Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub);
  149. void UA_Subscription_addMonitoredItem(UA_Subscription *sub, UA_MonitoredItem *newMon);
  150. UA_MonitoredItem * UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId);
  151. UA_StatusCode
  152. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  153. UA_UInt32 monitoredItemId);
  154. void UA_Subscription_publish(UA_Server *server, UA_Subscription *sub);
  155. UA_StatusCode UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub, UA_UInt32 sequenceNumber);
  156. void UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session);
  157. UA_Boolean UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session);
  158. #endif /* UA_SUBSCRIPTION_H_ */