ua_subscription.h 7.4 KB

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