ua_subscription.h 8.3 KB

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