ua_subscription.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /* Not used yet. Placeholder for a future event implementation. */
  39. typedef struct UA_Event {
  40. UA_Int32 eventId;
  41. } UA_Event;
  42. struct UA_MonitoredItem;
  43. typedef struct UA_MonitoredItem UA_MonitoredItem;
  44. typedef struct UA_Notification {
  45. TAILQ_ENTRY(UA_Notification) listEntry;
  46. TAILQ_ENTRY(UA_Notification) globalEntry;
  47. UA_MonitoredItem *mon;
  48. /* See the monitoredItemType of the MonitoredItem */
  49. union {
  50. UA_Event event;
  51. UA_DataValue value;
  52. } data;
  53. } UA_Notification;
  54. typedef TAILQ_HEAD(NotificationQueue, UA_Notification) NotificationQueue;
  55. struct UA_MonitoredItem {
  56. LIST_ENTRY(UA_MonitoredItem) listEntry;
  57. UA_Subscription *subscription;
  58. UA_UInt32 monitoredItemId;
  59. UA_UInt32 clientHandle;
  60. /* Settings */
  61. UA_MonitoredItemType monitoredItemType;
  62. UA_TimestampsToReturn timestampsToReturn;
  63. UA_MonitoringMode monitoringMode;
  64. UA_NodeId monitoredNodeId;
  65. UA_UInt32 attributeId;
  66. UA_String indexRange;
  67. UA_Double samplingInterval; // [ms]
  68. UA_UInt32 maxQueueSize;
  69. UA_Boolean discardOldest;
  70. // TODO: dataEncoding is hardcoded to UA binary
  71. UA_DataChangeTrigger trigger;
  72. /* Sample Callback */
  73. UA_UInt64 sampleCallbackId;
  74. UA_ByteString lastSampledValue;
  75. UA_Boolean sampleCallbackIsRegistered;
  76. /* Notification Queue */
  77. NotificationQueue queue;
  78. UA_UInt32 queueSize;
  79. };
  80. UA_MonitoredItem * UA_MonitoredItem_new(UA_MonitoredItemType);
  81. void MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem);
  82. void UA_MonitoredItem_SampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem);
  83. UA_StatusCode MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  84. UA_StatusCode MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  85. /* Remove entries until mon->maxQueueSize is reached. Sets infobits for lost
  86. * data if required. */
  87. void MonitoredItem_ensureQueueSpace(UA_MonitoredItem *mon);
  88. /****************/
  89. /* Subscription */
  90. /****************/
  91. typedef struct UA_NotificationMessageEntry {
  92. TAILQ_ENTRY(UA_NotificationMessageEntry) listEntry;
  93. UA_NotificationMessage message;
  94. } UA_NotificationMessageEntry;
  95. /* We use only a subset of the states defined in the standard */
  96. typedef enum {
  97. /* UA_SUBSCRIPTIONSTATE_CLOSED */
  98. /* UA_SUBSCRIPTIONSTATE_CREATING */
  99. UA_SUBSCRIPTIONSTATE_NORMAL,
  100. UA_SUBSCRIPTIONSTATE_LATE,
  101. UA_SUBSCRIPTIONSTATE_KEEPALIVE
  102. } UA_SubscriptionState;
  103. typedef TAILQ_HEAD(ListOfNotificationMessages, UA_NotificationMessageEntry) ListOfNotificationMessages;
  104. struct UA_Subscription {
  105. LIST_ENTRY(UA_Subscription) listEntry;
  106. UA_Session *session;
  107. UA_UInt32 subscriptionId;
  108. /* Settings */
  109. UA_UInt32 lifeTimeCount;
  110. UA_UInt32 maxKeepAliveCount;
  111. UA_Double publishingInterval; /* in ms */
  112. UA_UInt32 notificationsPerPublish;
  113. UA_Boolean publishingEnabled;
  114. UA_UInt32 priority;
  115. /* Runtime information */
  116. UA_SubscriptionState state;
  117. UA_UInt32 sequenceNumber;
  118. UA_UInt32 currentKeepAliveCount;
  119. UA_UInt32 currentLifetimeCount;
  120. /* Publish Callback */
  121. UA_UInt64 publishCallbackId;
  122. UA_Boolean publishCallbackIsRegistered;
  123. /* MonitoredItems */
  124. UA_UInt32 lastMonitoredItemId; /* increase the identifiers */
  125. LIST_HEAD(UA_ListOfUAMonitoredItems, UA_MonitoredItem) monitoredItems;
  126. UA_UInt32 monitoredItemsSize;
  127. /* Global list of notifications from the MonitoredItems */
  128. NotificationQueue notificationQueue;
  129. UA_UInt32 notificationQueueSize;
  130. UA_UInt32 readyNotifications; /* Notifications to be sent out now (already late) */
  131. /* Retransmission Queue */
  132. ListOfNotificationMessages retransmissionQueue;
  133. UA_UInt32 retransmissionQueueSize;
  134. };
  135. UA_Subscription * UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId);
  136. void UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub);
  137. UA_StatusCode Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub);
  138. UA_StatusCode Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub);
  139. void UA_Subscription_addMonitoredItem(UA_Subscription *sub, UA_MonitoredItem *newMon);
  140. UA_MonitoredItem * UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId);
  141. UA_StatusCode
  142. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  143. UA_UInt32 monitoredItemId);
  144. void UA_Subscription_publish(UA_Server *server, UA_Subscription *sub);
  145. UA_StatusCode UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub, UA_UInt32 sequenceNumber);
  146. void UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session);
  147. UA_Boolean UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session);
  148. #endif /* UA_SUBSCRIPTION_H_ */