client_subscriptions.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <open62541/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. Check the returned ``UA_CreateMonitoredItemsResponse``
  88. * to get the 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. /**
  103. * The clientHandle parameter can't be set by the user, any value will be replaced
  104. * by the client before sending the request to the server. */
  105. /* Callback for the deletion of a MonitoredItem */
  106. typedef void (*UA_Client_DeleteMonitoredItemCallback)
  107. (UA_Client *client, UA_UInt32 subId, void *subContext,
  108. UA_UInt32 monId, void *monContext);
  109. /* Callback for DataChange notifications */
  110. typedef void (*UA_Client_DataChangeNotificationCallback)
  111. (UA_Client *client, UA_UInt32 subId, void *subContext,
  112. UA_UInt32 monId, void *monContext,
  113. UA_DataValue *value);
  114. /* Callback for Event notifications */
  115. typedef void (*UA_Client_EventNotificationCallback)
  116. (UA_Client *client, UA_UInt32 subId, void *subContext,
  117. UA_UInt32 monId, void *monContext,
  118. size_t nEventFields, UA_Variant *eventFields);
  119. /* Don't use to monitor the EventNotifier attribute */
  120. UA_CreateMonitoredItemsResponse UA_EXPORT
  121. UA_Client_MonitoredItems_createDataChanges(UA_Client *client,
  122. const UA_CreateMonitoredItemsRequest request, void **contexts,
  123. UA_Client_DataChangeNotificationCallback *callbacks,
  124. UA_Client_DeleteMonitoredItemCallback *deleteCallbacks);
  125. UA_MonitoredItemCreateResult UA_EXPORT
  126. UA_Client_MonitoredItems_createDataChange(UA_Client *client, UA_UInt32 subscriptionId,
  127. UA_TimestampsToReturn timestampsToReturn, const UA_MonitoredItemCreateRequest item,
  128. void *context, UA_Client_DataChangeNotificationCallback callback,
  129. UA_Client_DeleteMonitoredItemCallback deleteCallback);
  130. /* Monitor the EventNotifier attribute only */
  131. UA_CreateMonitoredItemsResponse UA_EXPORT
  132. UA_Client_MonitoredItems_createEvents(UA_Client *client,
  133. const UA_CreateMonitoredItemsRequest request, void **contexts,
  134. UA_Client_EventNotificationCallback *callback,
  135. UA_Client_DeleteMonitoredItemCallback *deleteCallback);
  136. UA_MonitoredItemCreateResult UA_EXPORT
  137. UA_Client_MonitoredItems_createEvent(UA_Client *client, UA_UInt32 subscriptionId,
  138. UA_TimestampsToReturn timestampsToReturn, const UA_MonitoredItemCreateRequest item,
  139. void *context, UA_Client_EventNotificationCallback callback,
  140. UA_Client_DeleteMonitoredItemCallback deleteCallback);
  141. UA_DeleteMonitoredItemsResponse UA_EXPORT
  142. UA_Client_MonitoredItems_delete(UA_Client *client, const UA_DeleteMonitoredItemsRequest);
  143. UA_StatusCode UA_EXPORT
  144. UA_Client_MonitoredItems_deleteSingle(UA_Client *client, UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId);
  145. /* The clientHandle parameter will be filled automatically */
  146. UA_ModifyMonitoredItemsResponse UA_EXPORT
  147. UA_Client_MonitoredItems_modify(UA_Client *client,
  148. const UA_ModifyMonitoredItemsRequest request);
  149. /**
  150. * The following service calls go directly to the server. The MonitoredItem settings are
  151. * not stored in the client. */
  152. static UA_INLINE UA_SetMonitoringModeResponse
  153. UA_Client_MonitoredItems_setMonitoringMode(UA_Client *client,
  154. const UA_SetMonitoringModeRequest request) {
  155. UA_SetMonitoringModeResponse response;
  156. __UA_Client_Service(client,
  157. &request, &UA_TYPES[UA_TYPES_SETMONITORINGMODEREQUEST],
  158. &response, &UA_TYPES[UA_TYPES_SETMONITORINGMODERESPONSE]);
  159. return response;
  160. }
  161. static UA_INLINE UA_SetTriggeringResponse
  162. UA_Client_MonitoredItems_setTriggering(UA_Client *client,
  163. const UA_SetTriggeringRequest request) {
  164. UA_SetTriggeringResponse response;
  165. __UA_Client_Service(client,
  166. &request, &UA_TYPES[UA_TYPES_SETTRIGGERINGREQUEST],
  167. &response, &UA_TYPES[UA_TYPES_SETTRIGGERINGRESPONSE]);
  168. return response;
  169. }
  170. #endif
  171. _UA_END_DECLS
  172. #endif /* UA_CLIENT_SUBSCRIPTIONS_H_ */