client_subscriptions.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_StatusCode UA_EXPORT
  62. UA_Client_Subscriptions_create_async(
  63. UA_Client *client, const UA_CreateSubscriptionRequest request,
  64. void *subscriptionContext,
  65. UA_Client_StatusChangeNotificationCallback statusChangeCallback,
  66. UA_Client_DeleteSubscriptionCallback deleteCallback,
  67. UA_ClientAsyncServiceCallback callback, void *userdata, UA_UInt32 *requestId);
  68. UA_ModifySubscriptionResponse UA_EXPORT
  69. UA_Client_Subscriptions_modify(UA_Client *client, const UA_ModifySubscriptionRequest request);
  70. UA_StatusCode UA_EXPORT
  71. UA_Client_Subscriptions_modify_async(UA_Client *client,
  72. const UA_ModifySubscriptionRequest request,
  73. UA_ClientAsyncServiceCallback callback,
  74. void *userdata, UA_UInt32 *requestId);
  75. UA_DeleteSubscriptionsResponse UA_EXPORT
  76. UA_Client_Subscriptions_delete(UA_Client *client,
  77. const UA_DeleteSubscriptionsRequest request);
  78. UA_StatusCode UA_EXPORT
  79. UA_Client_Subscriptions_delete_async(UA_Client *client,
  80. const UA_DeleteSubscriptionsRequest request,
  81. UA_ClientAsyncServiceCallback callback,
  82. void *userdata, UA_UInt32 *requestId);
  83. /* Delete a single subscription */
  84. UA_StatusCode UA_EXPORT
  85. UA_Client_Subscriptions_deleteSingle(UA_Client *client, UA_UInt32 subscriptionId);
  86. static UA_INLINE UA_SetPublishingModeResponse
  87. UA_Client_Subscriptions_setPublishingMode(UA_Client *client,
  88. const UA_SetPublishingModeRequest request) {
  89. UA_SetPublishingModeResponse response;
  90. __UA_Client_Service(client, &request,
  91. &UA_TYPES[UA_TYPES_SETPUBLISHINGMODEREQUEST], &response,
  92. &UA_TYPES[UA_TYPES_SETPUBLISHINGMODERESPONSE]);
  93. return response;
  94. }
  95. /**
  96. * MonitoredItems
  97. * --------------
  98. *
  99. * MonitoredItems for Events indicate the ``EventNotifier`` attribute. This
  100. * indicates to the server not to monitor changes of the attribute, but to
  101. * forward Event notifications from that node.
  102. *
  103. * During the creation of a MonitoredItem, the server may return changed
  104. * adjusted parameters. Check the returned ``UA_CreateMonitoredItemsResponse``
  105. * to get the current parameters. */
  106. /* Provides default values for a new monitored item. */
  107. static UA_INLINE UA_MonitoredItemCreateRequest
  108. UA_MonitoredItemCreateRequest_default(UA_NodeId nodeId) {
  109. UA_MonitoredItemCreateRequest request;
  110. UA_MonitoredItemCreateRequest_init(&request);
  111. request.itemToMonitor.nodeId = nodeId;
  112. request.itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
  113. request.monitoringMode = UA_MONITORINGMODE_REPORTING;
  114. request.requestedParameters.samplingInterval = 250;
  115. request.requestedParameters.discardOldest = true;
  116. request.requestedParameters.queueSize = 1;
  117. return request;
  118. }
  119. /**
  120. * The clientHandle parameter can't be set by the user, any value will be replaced
  121. * by the client before sending the request to the server. */
  122. /* Callback for the deletion of a MonitoredItem */
  123. typedef void (*UA_Client_DeleteMonitoredItemCallback)
  124. (UA_Client *client, UA_UInt32 subId, void *subContext,
  125. UA_UInt32 monId, void *monContext);
  126. /* Callback for DataChange notifications */
  127. typedef void (*UA_Client_DataChangeNotificationCallback)
  128. (UA_Client *client, UA_UInt32 subId, void *subContext,
  129. UA_UInt32 monId, void *monContext,
  130. UA_DataValue *value);
  131. /* Callback for Event notifications */
  132. typedef void (*UA_Client_EventNotificationCallback)
  133. (UA_Client *client, UA_UInt32 subId, void *subContext,
  134. UA_UInt32 monId, void *monContext,
  135. size_t nEventFields, UA_Variant *eventFields);
  136. /* Don't use to monitor the EventNotifier attribute */
  137. UA_CreateMonitoredItemsResponse UA_EXPORT
  138. UA_Client_MonitoredItems_createDataChanges(UA_Client *client,
  139. const UA_CreateMonitoredItemsRequest request, void **contexts,
  140. UA_Client_DataChangeNotificationCallback *callbacks,
  141. UA_Client_DeleteMonitoredItemCallback *deleteCallbacks);
  142. UA_StatusCode UA_EXPORT
  143. UA_Client_MonitoredItems_createDataChanges_async(
  144. UA_Client *client, const UA_CreateMonitoredItemsRequest request, void **contexts,
  145. UA_Client_DataChangeNotificationCallback *callbacks,
  146. UA_Client_DeleteMonitoredItemCallback *deleteCallbacks,
  147. UA_ClientAsyncServiceCallback createCallback, void *userdata, UA_UInt32 *requestId);
  148. UA_MonitoredItemCreateResult UA_EXPORT
  149. UA_Client_MonitoredItems_createDataChange(UA_Client *client, UA_UInt32 subscriptionId,
  150. UA_TimestampsToReturn timestampsToReturn, const UA_MonitoredItemCreateRequest item,
  151. void *context, UA_Client_DataChangeNotificationCallback callback,
  152. UA_Client_DeleteMonitoredItemCallback deleteCallback);
  153. /* Monitor the EventNotifier attribute only */
  154. UA_CreateMonitoredItemsResponse UA_EXPORT
  155. UA_Client_MonitoredItems_createEvents(UA_Client *client,
  156. const UA_CreateMonitoredItemsRequest request, void **contexts,
  157. UA_Client_EventNotificationCallback *callback,
  158. UA_Client_DeleteMonitoredItemCallback *deleteCallback);
  159. /* Monitor the EventNotifier attribute only */
  160. UA_StatusCode UA_EXPORT
  161. UA_Client_MonitoredItems_createEvents_async(
  162. UA_Client *client, const UA_CreateMonitoredItemsRequest request, void **contexts,
  163. UA_Client_EventNotificationCallback *callbacks,
  164. UA_Client_DeleteMonitoredItemCallback *deleteCallbacks,
  165. UA_ClientAsyncServiceCallback createCallback, void *userdata, UA_UInt32 *requestId);
  166. UA_MonitoredItemCreateResult UA_EXPORT
  167. UA_Client_MonitoredItems_createEvent(UA_Client *client, UA_UInt32 subscriptionId,
  168. UA_TimestampsToReturn timestampsToReturn, const UA_MonitoredItemCreateRequest item,
  169. void *context, UA_Client_EventNotificationCallback callback,
  170. UA_Client_DeleteMonitoredItemCallback deleteCallback);
  171. UA_DeleteMonitoredItemsResponse UA_EXPORT
  172. UA_Client_MonitoredItems_delete(UA_Client *client, const UA_DeleteMonitoredItemsRequest);
  173. UA_StatusCode UA_EXPORT
  174. UA_Client_MonitoredItems_delete_async(UA_Client *client,
  175. const UA_DeleteMonitoredItemsRequest request,
  176. UA_ClientAsyncServiceCallback callback,
  177. void *userdata, UA_UInt32 *requestId);
  178. UA_StatusCode UA_EXPORT
  179. UA_Client_MonitoredItems_deleteSingle(UA_Client *client, UA_UInt32 subscriptionId,
  180. UA_UInt32 monitoredItemId);
  181. /* The clientHandle parameter will be filled automatically */
  182. UA_ModifyMonitoredItemsResponse UA_EXPORT
  183. UA_Client_MonitoredItems_modify(UA_Client *client,
  184. const UA_ModifyMonitoredItemsRequest request);
  185. /**
  186. * The following service calls go directly to the server. The MonitoredItem settings are
  187. * not stored in the client. */
  188. static UA_INLINE UA_SetMonitoringModeResponse
  189. UA_Client_MonitoredItems_setMonitoringMode(UA_Client *client,
  190. const UA_SetMonitoringModeRequest request) {
  191. UA_SetMonitoringModeResponse response;
  192. __UA_Client_Service(client,
  193. &request, &UA_TYPES[UA_TYPES_SETMONITORINGMODEREQUEST],
  194. &response, &UA_TYPES[UA_TYPES_SETMONITORINGMODERESPONSE]);
  195. return response;
  196. }
  197. static UA_INLINE UA_SetTriggeringResponse
  198. UA_Client_MonitoredItems_setTriggering(UA_Client *client,
  199. const UA_SetTriggeringRequest request) {
  200. UA_SetTriggeringResponse response;
  201. __UA_Client_Service(client,
  202. &request, &UA_TYPES[UA_TYPES_SETTRIGGERINGREQUEST],
  203. &response, &UA_TYPES[UA_TYPES_SETTRIGGERINGRESPONSE]);
  204. return response;
  205. }
  206. static UA_INLINE UA_StatusCode
  207. UA_Client_MonitoredItems_modify_async(UA_Client *client,
  208. const UA_ModifyMonitoredItemsRequest request,
  209. UA_ClientAsyncServiceCallback callback,
  210. void *userdata, UA_UInt32 *requestId) {
  211. return __UA_Client_AsyncService(
  212. client, &request, &UA_TYPES[UA_TYPES_MODIFYMONITOREDITEMSREQUEST], callback,
  213. &UA_TYPES[UA_TYPES_MODIFYMONITOREDITEMSRESPONSE], userdata, requestId);
  214. }
  215. static UA_INLINE UA_StatusCode
  216. UA_Client_MonitoredItems_setMonitoringMode_async(
  217. UA_Client *client, const UA_SetMonitoringModeRequest request,
  218. UA_ClientAsyncServiceCallback callback, void *userdata, UA_UInt32 *requestId) {
  219. return __UA_Client_AsyncService(
  220. client, &request, &UA_TYPES[UA_TYPES_SETMONITORINGMODEREQUEST], callback,
  221. &UA_TYPES[UA_TYPES_SETMONITORINGMODERESPONSE], userdata, requestId);
  222. }
  223. static UA_INLINE UA_StatusCode
  224. UA_Client_MonitoredItems_setTriggering_async(UA_Client *client,
  225. const UA_SetTriggeringRequest request,
  226. UA_ClientAsyncServiceCallback callback,
  227. void *userdata, UA_UInt32 *requestId) {
  228. return __UA_Client_AsyncService(
  229. client, &request, &UA_TYPES[UA_TYPES_SETTRIGGERINGREQUEST], callback,
  230. &UA_TYPES[UA_TYPES_SETTRIGGERINGRESPONSE], userdata, requestId);
  231. }
  232. #endif
  233. _UA_END_DECLS
  234. #endif /* UA_CLIENT_SUBSCRIPTIONS_H_ */