ua_subscription.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <open62541/types.h>
  16. #include <open62541/types_generated.h>
  17. #include <open62541/plugin/nodestore.h>
  18. #include "ua_session.h"
  19. #include "ua_util_internal.h"
  20. #include "ua_workqueue.h"
  21. _UA_BEGIN_DECLS
  22. #ifdef UA_ENABLE_SUBSCRIPTIONS
  23. #define UA_BOUNDEDVALUE_SETWBOUNDS(BOUNDS, SRC, DST) { \
  24. if(SRC > BOUNDS.max) DST = BOUNDS.max; \
  25. else if(SRC < BOUNDS.min) DST = BOUNDS.min; \
  26. else DST = SRC; \
  27. }
  28. /**
  29. * MonitoredItems create Notifications. Subscriptions collect Notifications from
  30. * (several) MonitoredItems and publish them to the client.
  31. *
  32. * Notifications are put into two queues at the same time. One for the
  33. * MonitoredItem that generated the notification. Here we can remove it if the
  34. * space reserved for the MonitoredItem runs full. The second queue is the
  35. * "global" queue for all Notifications generated in a Subscription. For
  36. * publication, the notifications are taken out of the "global" queue in the
  37. * order of their creation.
  38. */
  39. /*****************/
  40. /* MonitoredItem */
  41. /*****************/
  42. struct UA_MonitoredItem;
  43. typedef struct UA_MonitoredItem UA_MonitoredItem;
  44. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  45. typedef struct UA_EventNotification {
  46. UA_EventFieldList fields;
  47. /* EventFilterResult currently isn't being used
  48. UA_EventFilterResult result; */
  49. } UA_EventNotification;
  50. #endif
  51. typedef struct UA_Notification {
  52. TAILQ_ENTRY(UA_Notification) listEntry; /* Notification list for the MonitoredItem */
  53. TAILQ_ENTRY(UA_Notification) globalEntry; /* Notification list for the Subscription */
  54. UA_MonitoredItem *mon;
  55. /* See the monitoredItemType of the MonitoredItem */
  56. union {
  57. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  58. UA_EventNotification event;
  59. #endif
  60. UA_DataValue value;
  61. } data;
  62. } UA_Notification;
  63. /* Ensure enough space is available; Add notification to the linked lists;
  64. * Increase the counters */
  65. void UA_Notification_enqueue(UA_Server *server, UA_Subscription *sub,
  66. UA_MonitoredItem *mon, UA_Notification *n);
  67. /* Remove the notification from the MonitoredItem's queue and the Subscriptions
  68. * global queue. Reduce the respective counters. */
  69. void UA_Notification_dequeue(UA_Server *server, UA_Notification *n);
  70. /* Delete the notification. Must be dequeued first. */
  71. void UA_Notification_delete(UA_Notification *n);
  72. typedef TAILQ_HEAD(NotificationQueue, UA_Notification) NotificationQueue;
  73. struct UA_MonitoredItem {
  74. UA_DelayedCallback delayedFreePointers;
  75. LIST_ENTRY(UA_MonitoredItem) listEntry;
  76. UA_Subscription *subscription; /* Local MonitoredItem if the subscription is NULL */
  77. UA_UInt32 monitoredItemId;
  78. UA_UInt32 clientHandle;
  79. UA_Boolean registered; /* Was the MonitoredItem registered in Userland with
  80. the callback? */
  81. /* Settings */
  82. UA_TimestampsToReturn timestampsToReturn;
  83. UA_MonitoringMode monitoringMode;
  84. UA_NodeId monitoredNodeId;
  85. UA_UInt32 attributeId;
  86. UA_String indexRange;
  87. UA_Double samplingInterval; // [ms]
  88. UA_Boolean discardOldest;
  89. union {
  90. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  91. UA_EventFilter eventFilter; /* If attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER */
  92. #endif
  93. UA_DataChangeFilter dataChangeFilter;
  94. } filter;
  95. UA_Variant lastValue;
  96. // TODO: dataEncoding is hardcoded to UA binary
  97. /* Sample Callback */
  98. UA_UInt64 sampleCallbackId;
  99. UA_ByteString lastSampledValue;
  100. UA_Boolean sampleCallbackIsRegistered;
  101. /* Notification Queue */
  102. NotificationQueue queue;
  103. UA_UInt32 maxQueueSize; /* The max number of enqueued notifications (not
  104. * counting overflow events) */
  105. UA_UInt32 queueSize;
  106. UA_UInt32 eventOverflows; /* Separate counter for the queue. Can at most
  107. * double the queue size */
  108. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  109. UA_MonitoredItem *next;
  110. #endif
  111. #ifdef UA_ENABLE_DA
  112. UA_StatusCode lastStatus;
  113. #endif
  114. };
  115. void UA_MonitoredItem_init(UA_MonitoredItem *mon, UA_Subscription *sub);
  116. void UA_MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem);
  117. void UA_MonitoredItem_sampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem);
  118. UA_StatusCode UA_MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  119. void UA_MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon);
  120. /* Remove entries until mon->maxQueueSize is reached. Sets infobits for lost
  121. * data if required. */
  122. UA_StatusCode UA_MonitoredItem_ensureQueueSpace(UA_Server *server, UA_MonitoredItem *mon);
  123. UA_StatusCode UA_MonitoredItem_removeNodeEventCallback(UA_Server *server, UA_Session *session,
  124. UA_Node *node, void *data);
  125. /****************/
  126. /* Subscription */
  127. /****************/
  128. typedef struct UA_NotificationMessageEntry {
  129. TAILQ_ENTRY(UA_NotificationMessageEntry) listEntry;
  130. UA_NotificationMessage message;
  131. } UA_NotificationMessageEntry;
  132. /* We use only a subset of the states defined in the standard */
  133. typedef enum {
  134. /* UA_SUBSCRIPTIONSTATE_CLOSED */
  135. /* UA_SUBSCRIPTIONSTATE_CREATING */
  136. UA_SUBSCRIPTIONSTATE_NORMAL,
  137. UA_SUBSCRIPTIONSTATE_LATE,
  138. UA_SUBSCRIPTIONSTATE_KEEPALIVE
  139. } UA_SubscriptionState;
  140. typedef TAILQ_HEAD(ListOfNotificationMessages, UA_NotificationMessageEntry) ListOfNotificationMessages;
  141. struct UA_Subscription {
  142. UA_DelayedCallback delayedFreePointers;
  143. LIST_ENTRY(UA_Subscription) listEntry;
  144. UA_Session *session;
  145. UA_UInt32 subscriptionId;
  146. /* Settings */
  147. UA_UInt32 lifeTimeCount;
  148. UA_UInt32 maxKeepAliveCount;
  149. UA_Double publishingInterval; /* in ms */
  150. UA_UInt32 notificationsPerPublish;
  151. UA_Boolean publishingEnabled;
  152. UA_UInt32 priority;
  153. /* Runtime information */
  154. UA_SubscriptionState state;
  155. UA_UInt32 nextSequenceNumber;
  156. UA_UInt32 currentKeepAliveCount;
  157. UA_UInt32 currentLifetimeCount;
  158. /* Publish Callback */
  159. UA_UInt64 publishCallbackId;
  160. UA_Boolean publishCallbackIsRegistered;
  161. /* MonitoredItems */
  162. UA_UInt32 lastMonitoredItemId; /* increase the identifiers */
  163. LIST_HEAD(, UA_MonitoredItem) monitoredItems;
  164. UA_UInt32 monitoredItemsSize;
  165. /* Global list of notifications from the MonitoredItems */
  166. NotificationQueue notificationQueue;
  167. UA_UInt32 notificationQueueSize; /* Total queue size */
  168. UA_UInt32 dataChangeNotifications;
  169. UA_UInt32 eventNotifications;
  170. UA_UInt32 statusChangeNotifications;
  171. /* Notifications to be sent out now (already late). In a regular publish
  172. * callback, all queued notifications are sent out. In a late publish
  173. * response, only the notifications left from the last regular publish
  174. * callback are sent. */
  175. UA_UInt32 readyNotifications;
  176. /* Retransmission Queue */
  177. ListOfNotificationMessages retransmissionQueue;
  178. size_t retransmissionQueueSize;
  179. };
  180. UA_Subscription * UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId);
  181. void UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub);
  182. UA_StatusCode Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub);
  183. void Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub);
  184. void UA_Subscription_addMonitoredItem(UA_Server *server, UA_Subscription *sub, UA_MonitoredItem *newMon);
  185. UA_MonitoredItem * UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId);
  186. UA_StatusCode
  187. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  188. UA_UInt32 monitoredItemId);
  189. void UA_Subscription_publish(UA_Server *server, UA_Subscription *sub);
  190. UA_StatusCode UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub,
  191. UA_UInt32 sequenceNumber);
  192. void UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session);
  193. UA_Boolean UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session);
  194. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  195. _UA_END_DECLS
  196. #endif /* UA_SUBSCRIPTION_H_ */