ua_subscription.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "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. /* Ensure enough space is available; Add notification to the linked lists;
  56. * Increase the counters */
  57. void UA_Notification_enqueue(UA_Server *server, UA_Subscription *sub,
  58. UA_MonitoredItem *mon, UA_Notification *n);
  59. /* Delete the notification. Also removes it from the linked lists. */
  60. void UA_Notification_delete(UA_Subscription *sub, UA_MonitoredItem *mon,
  61. UA_Notification *n);
  62. typedef TAILQ_HEAD(NotificationQueue, UA_Notification) NotificationQueue;
  63. struct UA_MonitoredItem {
  64. LIST_ENTRY(UA_MonitoredItem) listEntry;
  65. UA_Subscription *subscription;
  66. UA_UInt32 monitoredItemId;
  67. UA_UInt32 clientHandle;
  68. /* Settings */
  69. UA_MonitoredItemType monitoredItemType;
  70. UA_TimestampsToReturn timestampsToReturn;
  71. UA_MonitoringMode monitoringMode;
  72. UA_NodeId monitoredNodeId;
  73. UA_UInt32 attributeId;
  74. UA_String indexRange;
  75. UA_Double samplingInterval; // [ms]
  76. UA_UInt32 maxQueueSize;
  77. UA_Boolean discardOldest;
  78. // TODO: dataEncoding is hardcoded to UA binary
  79. union {
  80. UA_EventFilter eventFilter;
  81. UA_DataChangeFilter dataChangeFilter;
  82. } filter;
  83. UA_Variant lastValue;
  84. /* Sample Callback */
  85. UA_UInt64 sampleCallbackId;
  86. UA_ByteString lastSampledValue;
  87. UA_Boolean sampleCallbackIsRegistered;
  88. /* Notification Queue */
  89. NotificationQueue queue;
  90. UA_UInt32 queueSize;
  91. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  92. UA_MonitoredItem *next;
  93. #endif
  94. };
  95. void UA_MonitoredItem_init(UA_MonitoredItem *mon, UA_Subscription *sub);
  96. void UA_MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *mon);
  97. void UA_MonitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  98. UA_StatusCode UA_MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  99. UA_StatusCode UA_MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  100. /* Remove entries until mon->maxQueueSize is reached. Sets infobits for lost
  101. * data if required. */
  102. UA_StatusCode MonitoredItem_ensureQueueSpace(UA_Server *server, UA_MonitoredItem *mon);
  103. /****************/
  104. /* Subscription */
  105. /****************/
  106. typedef struct UA_NotificationMessageEntry {
  107. TAILQ_ENTRY(UA_NotificationMessageEntry) listEntry;
  108. UA_NotificationMessage message;
  109. } UA_NotificationMessageEntry;
  110. /* We use only a subset of the states defined in the standard */
  111. typedef enum {
  112. /* UA_SUBSCRIPTIONSTATE_CLOSED */
  113. /* UA_SUBSCRIPTIONSTATE_CREATING */
  114. UA_SUBSCRIPTIONSTATE_NORMAL,
  115. UA_SUBSCRIPTIONSTATE_LATE,
  116. UA_SUBSCRIPTIONSTATE_KEEPALIVE
  117. } UA_SubscriptionState;
  118. typedef TAILQ_HEAD(ListOfNotificationMessages, UA_NotificationMessageEntry) ListOfNotificationMessages;
  119. struct UA_Subscription {
  120. LIST_ENTRY(UA_Subscription) listEntry;
  121. UA_Session *session;
  122. UA_UInt32 subscriptionId;
  123. /* Settings */
  124. UA_UInt32 lifeTimeCount;
  125. UA_UInt32 maxKeepAliveCount;
  126. UA_Double publishingInterval; /* in ms */
  127. UA_UInt32 notificationsPerPublish;
  128. UA_Boolean publishingEnabled;
  129. UA_UInt32 priority;
  130. /* Runtime information */
  131. UA_SubscriptionState state;
  132. UA_UInt32 sequenceNumber;
  133. UA_UInt32 currentKeepAliveCount;
  134. UA_UInt32 currentLifetimeCount;
  135. /* Publish Callback */
  136. UA_UInt64 publishCallbackId;
  137. UA_Boolean publishCallbackIsRegistered;
  138. /* MonitoredItems */
  139. UA_UInt32 lastMonitoredItemId; /* increase the identifiers */
  140. LIST_HEAD(UA_ListOfUAMonitoredItems, UA_MonitoredItem) monitoredItems;
  141. UA_UInt32 monitoredItemsSize;
  142. /* Global list of notifications from the MonitoredItems */
  143. NotificationQueue notificationQueue;
  144. UA_UInt32 notificationQueueSize; /* Total queue size */
  145. UA_UInt32 dataChangeNotifications;
  146. UA_UInt32 eventNotifications;
  147. UA_UInt32 statusChangeNotifications;
  148. /* Notifications to be sent out now (already late). In a regular publish
  149. * callback, all queued notifications are sent out. In a late publish
  150. * response, only the notifications left from the last regular publish
  151. * callback are sent. */
  152. UA_UInt32 readyNotifications;
  153. /* Retransmission Queue */
  154. ListOfNotificationMessages retransmissionQueue;
  155. UA_UInt32 retransmissionQueueSize;
  156. };
  157. UA_Subscription * UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId);
  158. void UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub);
  159. UA_StatusCode Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub);
  160. UA_StatusCode Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub);
  161. void UA_Subscription_addMonitoredItem(UA_Subscription *sub, UA_MonitoredItem *newMon);
  162. UA_MonitoredItem * UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId);
  163. UA_StatusCode
  164. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  165. UA_UInt32 monitoredItemId);
  166. void UA_Subscription_publish(UA_Server *server, UA_Subscription *sub);
  167. UA_StatusCode UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub, UA_UInt32 sequenceNumber);
  168. void UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session);
  169. UA_Boolean UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session);
  170. #endif /* UA_SUBSCRIPTION_H_ */