ua_client_subscriptions.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifndef UA_CLIENT_SUBSCRIPTIONS_H_
  5. #define UA_CLIENT_SUBSCRIPTIONS_H_
  6. #include "ua_client.h"
  7. _UA_BEGIN_DECLS
  8. #ifdef UA_ENABLE_SUBSCRIPTIONS
  9. /**
  10. * .. _client-subscriptions:
  11. *
  12. * Subscriptions
  13. * -------------
  14. *
  15. * Subscriptions in OPC UA are asynchronous. That is, the client sends several
  16. * PublishRequests to the server. The server returns PublishResponses with
  17. * notifications. But only when a notification has been generated. The client
  18. * does not wait for the responses and continues normal operations.
  19. *
  20. * Note the difference between Subscriptions and MonitoredItems. Subscriptions
  21. * are used to report back notifications. MonitoredItems are used to generate
  22. * notifications. Every MonitoredItem is attached to exactly one Subscription.
  23. * And a Subscription can contain many MonitoredItems.
  24. *
  25. * The client automatically processes PublishResponses (with a callback) in the
  26. * background and keeps enough PublishRequests in transit. The PublishResponses
  27. * may be recieved during a synchronous service call or in
  28. * ``UA_Client_runAsync``. */
  29. /* Callbacks defined for Subscriptions */
  30. typedef void (*UA_Client_DeleteSubscriptionCallback)
  31. (UA_Client *client, UA_UInt32 subId, void *subContext);
  32. typedef void (*UA_Client_StatusChangeNotificationCallback)
  33. (UA_Client *client, UA_UInt32 subId, void *subContext,
  34. UA_StatusChangeNotification *notification);
  35. /* Provides default values for a new subscription.
  36. *
  37. * RequestedPublishingInterval: 500.0 [ms]
  38. * RequestedLifetimeCount: 10000
  39. * RequestedMaxKeepAliveCount: 10
  40. * MaxNotificationsPerPublish: 0 (unlimited)
  41. * PublishingEnabled: true
  42. * Priority: 0 */
  43. static UA_INLINE UA_CreateSubscriptionRequest
  44. UA_CreateSubscriptionRequest_default(void) {
  45. UA_CreateSubscriptionRequest request;
  46. UA_CreateSubscriptionRequest_init(&request);
  47. request.requestedPublishingInterval = 500.0;
  48. request.requestedLifetimeCount = 10000;
  49. request.requestedMaxKeepAliveCount = 10;
  50. request.maxNotificationsPerPublish = 0;
  51. request.publishingEnabled = true;
  52. request.priority = 0;
  53. return request;
  54. }
  55. UA_CreateSubscriptionResponse UA_EXPORT
  56. UA_Client_Subscriptions_create(UA_Client *client,
  57. const UA_CreateSubscriptionRequest request,
  58. void *subscriptionContext,
  59. UA_Client_StatusChangeNotificationCallback statusChangeCallback,
  60. UA_Client_DeleteSubscriptionCallback deleteCallback);
  61. UA_ModifySubscriptionResponse UA_EXPORT
  62. UA_Client_Subscriptions_modify(UA_Client *client, const UA_ModifySubscriptionRequest request);
  63. UA_DeleteSubscriptionsResponse UA_EXPORT
  64. UA_Client_Subscriptions_delete(UA_Client *client,
  65. const UA_DeleteSubscriptionsRequest request);
  66. /* Delete a single subscription */
  67. UA_StatusCode UA_EXPORT
  68. UA_Client_Subscriptions_deleteSingle(UA_Client *client, UA_UInt32 subscriptionId);
  69. static UA_INLINE UA_SetPublishingModeResponse
  70. UA_Client_Subscriptions_setPublishingMode(UA_Client *client,
  71. const UA_SetPublishingModeRequest request) {
  72. UA_SetPublishingModeResponse response;
  73. __UA_Client_Service(client, &request,
  74. &UA_TYPES[UA_TYPES_SETPUBLISHINGMODEREQUEST], &response,
  75. &UA_TYPES[UA_TYPES_SETPUBLISHINGMODERESPONSE]);
  76. return response;
  77. }
  78. /**
  79. * MonitoredItems
  80. * --------------
  81. *
  82. * MonitoredItems for Events indicate the ``EventNotifier`` attribute. This
  83. * indicates to the server not to monitor changes of the attribute, but to
  84. * forward Event notifications from that node.
  85. *
  86. * During the creation of a MonitoredItem, the server may return changed
  87. * adjusted parameters. Use ``UA_Client_MonitoredItem_getParameters`` to get the
  88. * current parameters. */
  89. /* Provides default values for a new monitored item. */
  90. static UA_INLINE UA_MonitoredItemCreateRequest
  91. UA_MonitoredItemCreateRequest_default(UA_NodeId nodeId) {
  92. UA_MonitoredItemCreateRequest request;
  93. UA_MonitoredItemCreateRequest_init(&request);
  94. request.itemToMonitor.nodeId = nodeId;
  95. request.itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  96. request.monitoringMode = UA_MONITORINGMODE_REPORTING;
  97. request.requestedParameters.samplingInterval = 250;
  98. request.requestedParameters.discardOldest = true;
  99. request.requestedParameters.queueSize = 1;
  100. return request;
  101. }
  102. /* Callback for the deletion of a MonitoredItem */
  103. typedef void (*UA_Client_DeleteMonitoredItemCallback)
  104. (UA_Client *client, UA_UInt32 subId, void *subContext,
  105. UA_UInt32 monId, void *monContext);
  106. /* Callback for DataChange notifications */
  107. typedef void (*UA_Client_DataChangeNotificationCallback)
  108. (UA_Client *client, UA_UInt32 subId, void *subContext,
  109. UA_UInt32 monId, void *monContext,
  110. UA_DataValue *value);
  111. /* Callback for Event notifications */
  112. typedef void (*UA_Client_EventNotificationCallback)
  113. (UA_Client *client, UA_UInt32 subId, void *subContext,
  114. UA_UInt32 monId, void *monContext,
  115. size_t nEventFields, UA_Variant *eventFields);
  116. /* Don't use to monitor the EventNotifier attribute */
  117. UA_CreateMonitoredItemsResponse UA_EXPORT
  118. UA_Client_MonitoredItems_createDataChanges(UA_Client *client,
  119. const UA_CreateMonitoredItemsRequest request, void **contexts,
  120. UA_Client_DataChangeNotificationCallback *callbacks,
  121. UA_Client_DeleteMonitoredItemCallback *deleteCallbacks);
  122. UA_MonitoredItemCreateResult UA_EXPORT
  123. UA_Client_MonitoredItems_createDataChange(UA_Client *client, UA_UInt32 subscriptionId,
  124. UA_TimestampsToReturn timestampsToReturn, const UA_MonitoredItemCreateRequest item,
  125. void *context, UA_Client_DataChangeNotificationCallback callback,
  126. UA_Client_DeleteMonitoredItemCallback deleteCallback);
  127. /* Monitor the EventNotifier attribute only */
  128. UA_CreateMonitoredItemsResponse UA_EXPORT
  129. UA_Client_MonitoredItems_createEvents(UA_Client *client,
  130. const UA_CreateMonitoredItemsRequest request, void **contexts,
  131. UA_Client_EventNotificationCallback *callbacks,
  132. UA_Client_DeleteMonitoredItemCallback *deleteCallback);
  133. UA_MonitoredItemCreateResult UA_EXPORT
  134. UA_Client_MonitoredItems_createEvent(UA_Client *client, UA_UInt32 subscriptionId,
  135. UA_TimestampsToReturn timestampsToReturn, const UA_MonitoredItemCreateRequest item,
  136. void *context, UA_Client_EventNotificationCallback callback,
  137. UA_Client_DeleteMonitoredItemCallback deleteCallback);
  138. UA_DeleteMonitoredItemsResponse UA_EXPORT
  139. UA_Client_MonitoredItems_delete(UA_Client *client, const UA_DeleteMonitoredItemsRequest);
  140. UA_StatusCode UA_EXPORT
  141. UA_Client_MonitoredItems_deleteSingle(UA_Client *client, UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId);
  142. /**
  143. * The following service calls go directly to the server. The MonitoredItem settings are
  144. * not stored in the client. */
  145. static UA_INLINE UA_ModifyMonitoredItemsResponse
  146. UA_Client_MonitoredItems_modify(UA_Client *client,
  147. const UA_ModifyMonitoredItemsRequest request) {
  148. UA_ModifyMonitoredItemsResponse response;
  149. __UA_Client_Service(client,
  150. &request, &UA_TYPES[UA_TYPES_MODIFYMONITOREDITEMSREQUEST],
  151. &response, &UA_TYPES[UA_TYPES_MODIFYMONITOREDITEMSRESPONSE]);
  152. return response;
  153. }
  154. static UA_INLINE UA_SetMonitoringModeResponse
  155. UA_Client_MonitoredItems_setMonitoringMode(UA_Client *client,
  156. const UA_SetMonitoringModeRequest request) {
  157. UA_SetMonitoringModeResponse response;
  158. __UA_Client_Service(client,
  159. &request, &UA_TYPES[UA_TYPES_SETMONITORINGMODEREQUEST],
  160. &response, &UA_TYPES[UA_TYPES_SETMONITORINGMODERESPONSE]);
  161. return response;
  162. }
  163. static UA_INLINE UA_SetTriggeringResponse
  164. UA_Client_MonitoredItems_setTriggering(UA_Client *client,
  165. const UA_SetTriggeringRequest request) {
  166. UA_SetTriggeringResponse response;
  167. __UA_Client_Service(client,
  168. &request, &UA_TYPES[UA_TYPES_SETTRIGGERINGREQUEST],
  169. &response, &UA_TYPES[UA_TYPES_SETTRIGGERINGRESPONSE]);
  170. return response;
  171. }
  172. #endif
  173. _UA_END_DECLS
  174. #endif /* UA_CLIENT_SUBSCRIPTIONS_H_ */