ua_client_subscriptions.h 8.4 KB

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