ua_subscription.h 7.5 KB

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