ua_client_subscriptions.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /**
  175. * Deprecated API
  176. * --------------
  177. * The following API is kept for backwards compatibility. It will be removed in
  178. * future releases. */
  179. typedef struct {
  180. UA_Double requestedPublishingInterval;
  181. UA_UInt32 requestedLifetimeCount;
  182. UA_UInt32 requestedMaxKeepAliveCount;
  183. UA_UInt32 maxNotificationsPerPublish;
  184. UA_Boolean publishingEnabled;
  185. UA_Byte priority;
  186. } UA_SubscriptionSettings;
  187. extern const UA_EXPORT UA_SubscriptionSettings UA_SubscriptionSettings_default;
  188. UA_DEPRECATED UA_StatusCode UA_EXPORT
  189. UA_Client_Subscriptions_new(UA_Client *client, UA_SubscriptionSettings settings,
  190. UA_UInt32 *newSubscriptionId);
  191. UA_DEPRECATED UA_StatusCode UA_EXPORT
  192. UA_Client_Subscriptions_remove(UA_Client *client, UA_UInt32 subscriptionId);
  193. /* Send a publish request and wait until a response to the request is processed.
  194. * Note that other publish responses may be processed in the background until
  195. * then. */
  196. UA_DEPRECATED UA_StatusCode UA_EXPORT
  197. UA_Client_Subscriptions_manuallySendPublishRequest(UA_Client *client);
  198. /* For monitoring DataChanges */
  199. typedef void (*UA_MonitoredItemHandlingFunction)(UA_Client *client, UA_UInt32 monId,
  200. UA_DataValue *value, void *context);
  201. UA_DEPRECATED UA_StatusCode UA_EXPORT
  202. UA_Client_Subscriptions_addMonitoredItems(UA_Client *client, const UA_UInt32 subscriptionId,
  203. UA_MonitoredItemCreateRequest *items, size_t itemsSize,
  204. UA_MonitoredItemHandlingFunction *hfs,
  205. void **hfContexts, UA_StatusCode *itemResults,
  206. UA_UInt32 *newMonitoredItemIds);
  207. UA_DEPRECATED UA_StatusCode UA_EXPORT
  208. UA_Client_Subscriptions_addMonitoredItem(UA_Client *client, UA_UInt32 subscriptionId,
  209. UA_NodeId nodeId, UA_UInt32 attributeID,
  210. UA_MonitoredItemHandlingFunction hf,
  211. void *hfContext,
  212. UA_UInt32 *newMonitoredItemId,
  213. UA_Double samplingInterval);
  214. /* Monitored Events have different payloads from DataChanges. So they use a
  215. * different callback method signature. */
  216. typedef void (*UA_MonitoredEventHandlingFunction)(UA_Client *client,
  217. const UA_UInt32 monId,
  218. const size_t nEventFields,
  219. const UA_Variant *eventFields,
  220. void *context);
  221. UA_DEPRECATED UA_StatusCode UA_EXPORT
  222. UA_Client_Subscriptions_addMonitoredEvents(UA_Client *client, const UA_UInt32 subscriptionId,
  223. UA_MonitoredItemCreateRequest *items, size_t itemsSize,
  224. UA_MonitoredEventHandlingFunction *hfs,
  225. void **hfContexts, UA_StatusCode *itemResults,
  226. UA_UInt32 *newMonitoredItemIds);
  227. /* TODO for 0.4: attribute is fix for events. */
  228. UA_DEPRECATED UA_StatusCode UA_EXPORT
  229. UA_Client_Subscriptions_addMonitoredEvent(UA_Client *client, UA_UInt32 subscriptionId,
  230. const UA_NodeId nodeId, UA_UInt32 attributeID,
  231. const UA_SimpleAttributeOperand *selectClauses,
  232. size_t selectClausesSize,
  233. const UA_ContentFilterElement *whereClauses,
  234. size_t whereClausesSize,
  235. const UA_MonitoredEventHandlingFunction hf,
  236. void *hfContext, UA_UInt32 *newMonitoredItemId);
  237. UA_DEPRECATED UA_StatusCode UA_EXPORT
  238. UA_Client_Subscriptions_removeMonitoredItem(UA_Client *client, UA_UInt32 subscriptionId,
  239. UA_UInt32 monitoredItemId);
  240. UA_DEPRECATED UA_StatusCode UA_EXPORT
  241. UA_Client_Subscriptions_removeMonitoredItems(UA_Client *client, UA_UInt32 subscriptionId,
  242. UA_UInt32 *monitoredItemIds, size_t itemsSize,
  243. UA_StatusCode *itemResults);
  244. #endif
  245. #ifdef __cplusplus
  246. } // extern "C"
  247. #endif
  248. #endif /* UA_CLIENT_SUBSCRIPTIONS_H_ */