Coverage Report

Created: 2026-06-09 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541/include/open62541/client_subscriptions.h
Line
Count
Source
1
/* This Source Code Form i 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
 *
5
 *    Copyright 2025 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
6
 */
7
8
#ifndef UA_CLIENT_SUBSCRIPTIONS_H_
9
#define UA_CLIENT_SUBSCRIPTIONS_H_
10
11
#include <open62541/client.h>
12
#include <open62541/client_highlevel_async.h>
13
14
_UA_BEGIN_DECLS
15
16
/**
17
 * .. _client-subscriptions:
18
 *
19
 * Subscriptions
20
 * -------------
21
 *
22
 * Subscriptions in OPC UA are asynchronous. That is, the client sends several
23
 * PublishRequests to the server. The server returns PublishResponses with
24
 * notifications. But only when a notification has been generated. The client
25
 * does not wait for the responses and continues normal operations.
26
 *
27
 * Note the difference between Subscriptions and MonitoredItems. Subscriptions
28
 * are used to report back notifications. MonitoredItems are used to generate
29
 * notifications. Every MonitoredItem is attached to exactly one Subscription.
30
 * And a Subscription can contain many MonitoredItems.
31
 *
32
 * The client automatically processes PublishResponses (with a callback) in the
33
 * background and keeps enough PublishRequests in transit. The PublishResponses
34
 * may be recieved during a synchronous service call or in
35
 * ``UA_Client_run_iterate``. See more about
36
 * :ref:`asynchronicity<client-async-services>`.
37
 */
38
39
/* Callbacks defined for Subscriptions */
40
typedef void (*UA_Client_DeleteSubscriptionCallback)
41
    (UA_Client *client, UA_UInt32 subId, void *subContext);
42
43
typedef void (*UA_Client_StatusChangeNotificationCallback)
44
    (UA_Client *client, UA_UInt32 subId, void *subContext,
45
     UA_StatusChangeNotification *notification);
46
47
/* Provides default values for a new subscription.
48
 *
49
 * RequestedPublishingInterval:  500.0 [ms]
50
 * RequestedLifetimeCount: 10000
51
 * RequestedMaxKeepAliveCount: 10
52
 * MaxNotificationsPerPublish: 0 (unlimited)
53
 * PublishingEnabled: true
54
 * Priority: 0 */
55
static UA_INLINE UA_CreateSubscriptionRequest
56
0
UA_CreateSubscriptionRequest_default(void) {
57
0
    UA_CreateSubscriptionRequest request;
58
0
    UA_CreateSubscriptionRequest_init(&request);
59
0
60
0
    request.requestedPublishingInterval = 500.0;
61
0
    request.requestedLifetimeCount = 10000;
62
0
    request.requestedMaxKeepAliveCount = 10;
63
0
    request.maxNotificationsPerPublish = 0;
64
0
    request.publishingEnabled = true;
65
0
    request.priority = 0;
66
0
    return request;
67
0
}
Unexecuted instantiation: ua_client.c:UA_CreateSubscriptionRequest_default
Unexecuted instantiation: ua_client_connect.c:UA_CreateSubscriptionRequest_default
Unexecuted instantiation: ua_client_discovery.c:UA_CreateSubscriptionRequest_default
Unexecuted instantiation: ua_client_highlevel.c:UA_CreateSubscriptionRequest_default
Unexecuted instantiation: ua_client_subscriptions.c:UA_CreateSubscriptionRequest_default
Unexecuted instantiation: ua_client_util.c:UA_CreateSubscriptionRequest_default
Unexecuted instantiation: ua_history_data_gathering_default.c:UA_CreateSubscriptionRequest_default
68
69
UA_CreateSubscriptionResponse UA_EXPORT UA_THREADSAFE
70
UA_Client_Subscriptions_create(UA_Client *client,
71
    const UA_CreateSubscriptionRequest request,
72
    void *subscriptionContext,
73
    UA_Client_StatusChangeNotificationCallback statusChangeCallback,
74
    UA_Client_DeleteSubscriptionCallback deleteCallback);
75
76
typedef void
77
(*UA_ClientAsyncCreateSubscriptionCallback)(
78
    UA_Client *client, void *userdata, UA_UInt32 requestId,
79
    UA_CreateSubscriptionResponse *response);
80
81
UA_StatusCode UA_EXPORT UA_THREADSAFE
82
UA_Client_Subscriptions_create_async(UA_Client *client,
83
    const UA_CreateSubscriptionRequest request,
84
    void *subscriptionContext,
85
    UA_Client_StatusChangeNotificationCallback statusChangeCallback,
86
    UA_Client_DeleteSubscriptionCallback deleteCallback,
87
    UA_ClientAsyncCreateSubscriptionCallback callback,
88
    void *userdata, UA_UInt32 *requestId);
89
90
UA_ModifySubscriptionResponse UA_EXPORT UA_THREADSAFE
91
UA_Client_Subscriptions_modify(UA_Client *client,
92
    const UA_ModifySubscriptionRequest request);
93
94
typedef void
95
(*UA_ClientAsyncModifySubscriptionCallback)(
96
    UA_Client *client, void *userdata, UA_UInt32 requestId,
97
    UA_ModifySubscriptionResponse *response);
98
99
UA_StatusCode UA_EXPORT UA_THREADSAFE
100
UA_Client_Subscriptions_modify_async(UA_Client *client,
101
    const UA_ModifySubscriptionRequest request,
102
    UA_ClientAsyncModifySubscriptionCallback callback,
103
    void *userdata, UA_UInt32 *requestId);
104
105
UA_DeleteSubscriptionsResponse UA_EXPORT UA_THREADSAFE
106
UA_Client_Subscriptions_delete(UA_Client *client,
107
    const UA_DeleteSubscriptionsRequest request);
108
109
typedef void
110
(*UA_ClientAsyncDeleteSubscriptionsCallback)(
111
    UA_Client *client, void *userdata, UA_UInt32 requestId,
112
    UA_DeleteSubscriptionsResponse *response);
113
114
UA_StatusCode UA_EXPORT UA_THREADSAFE
115
UA_Client_Subscriptions_delete_async(UA_Client *client,
116
    const UA_DeleteSubscriptionsRequest request,
117
    UA_ClientAsyncDeleteSubscriptionsCallback callback,
118
    void *userdata, UA_UInt32 *requestId);
119
120
/* Delete a single subscription */
121
UA_StatusCode UA_EXPORT UA_THREADSAFE
122
UA_Client_Subscriptions_deleteSingle(UA_Client *client,
123
                                     UA_UInt32 subscriptionId);
124
125
/* Retrieve or change the user supplied subscription contexts */
126
UA_StatusCode UA_EXPORT UA_THREADSAFE
127
UA_Client_Subscriptions_getContext(UA_Client *client,
128
                                   UA_UInt32 subscriptionId,
129
                                   void **subContext);
130
131
UA_StatusCode UA_EXPORT UA_THREADSAFE
132
UA_Client_Subscriptions_setContext(UA_Client *client,
133
                                   UA_UInt32 subscriptionId,
134
                                   void *subContext);
135
136
UA_SetPublishingModeResponse UA_EXPORT UA_THREADSAFE
137
UA_Client_Subscriptions_setPublishingMode(UA_Client *client,
138
    const UA_SetPublishingModeRequest request);
139
140
/**
141
 * MonitoredItems
142
 * ~~~~~~~~~~~~~~
143
 *
144
 * MonitoredItems for Events indicate the ``EventNotifier`` attribute. This
145
 * indicates to the server not to monitor changes of the attribute, but to
146
 * forward Event notifications from that node.
147
 *
148
 * During the creation of a MonitoredItem, the server may return changed
149
 * adjusted parameters. Check the returned ``UA_CreateMonitoredItemsResponse``
150
 * to get the current parameters.
151
 *
152
 * Be aware that the client may process incoming notifications before receiving
153
 * the CreateMonitoredItemsResponse. This is due to the behavior of some server
154
 * SDKs. Without this "early processing" we would miss the initial value, which
155
 * can be an issue if the value changes at a slow rate. */
156
157
/* Provides default values for a new monitored item. */
158
static UA_INLINE UA_MonitoredItemCreateRequest
159
0
UA_MonitoredItemCreateRequest_default(UA_NodeId nodeId) {
160
0
    UA_MonitoredItemCreateRequest request;
161
0
    UA_MonitoredItemCreateRequest_init(&request);
162
0
    request.itemToMonitor.nodeId = nodeId;
163
0
    request.itemToMonitor.attributeId = UA_ATTRIBUTEID_VALUE;
164
0
    request.monitoringMode = UA_MONITORINGMODE_REPORTING;
165
0
    request.requestedParameters.samplingInterval = 250;
166
    request.requestedParameters.discardOldest = true;
167
0
    request.requestedParameters.queueSize = 1;
168
0
    return request;
169
0
}
Unexecuted instantiation: ua_client.c:UA_MonitoredItemCreateRequest_default
Unexecuted instantiation: ua_client_connect.c:UA_MonitoredItemCreateRequest_default
Unexecuted instantiation: ua_client_discovery.c:UA_MonitoredItemCreateRequest_default
Unexecuted instantiation: ua_client_highlevel.c:UA_MonitoredItemCreateRequest_default
Unexecuted instantiation: ua_client_subscriptions.c:UA_MonitoredItemCreateRequest_default
Unexecuted instantiation: ua_client_util.c:UA_MonitoredItemCreateRequest_default
Unexecuted instantiation: ua_history_data_gathering_default.c:UA_MonitoredItemCreateRequest_default
170
171
/**
172
 * The clientHandle parameter cannot be set by the user, any value will be
173
 * replaced by the client with a unique internal ClientHandle value before
174
 * sending the request to the server. */
175
176
/* Callback for the deletion of a MonitoredItem */
177
typedef void (*UA_Client_DeleteMonitoredItemCallback)
178
    (UA_Client *client, UA_UInt32 subId, void *subContext,
179
     UA_UInt32 monId, void *monContext);
180
181
/* Callback for DataChange notifications */
182
typedef void (*UA_Client_DataChangeNotificationCallback)
183
    (UA_Client *client, UA_UInt32 subId, void *subContext,
184
     UA_UInt32 monId, void *monContext,
185
     UA_DataValue *value);
186
187
/* Callback for Event notifications */
188
typedef void (*UA_Client_EventNotificationCallback)
189
    (UA_Client *client, UA_UInt32 subId, void *subContext,
190
     UA_UInt32 monId, void *monContext,
191
     const UA_KeyValueMap eventFields);
192
193
/* Don't use to monitor the EventNotifier attribute */
194
UA_CreateMonitoredItemsResponse UA_EXPORT UA_THREADSAFE
195
UA_Client_MonitoredItems_createDataChanges(UA_Client *client,
196
    const UA_CreateMonitoredItemsRequest request, void **contexts,
197
    UA_Client_DataChangeNotificationCallback *callbacks,
198
    UA_Client_DeleteMonitoredItemCallback *deleteCallbacks);
199
200
typedef void
201
(*UA_ClientAsyncCreateMonitoredItemsCallback)(
202
    UA_Client *client, void *userdata, UA_UInt32 requestId,
203
    UA_CreateMonitoredItemsResponse *response);
204
205
UA_StatusCode UA_EXPORT UA_THREADSAFE
206
UA_Client_MonitoredItems_createDataChanges_async(UA_Client *client,
207
    const UA_CreateMonitoredItemsRequest request, void **contexts,
208
    UA_Client_DataChangeNotificationCallback *callbacks,
209
    UA_Client_DeleteMonitoredItemCallback *deleteCallbacks,
210
    UA_ClientAsyncCreateMonitoredItemsCallback createCallback,
211
    void *userdata, UA_UInt32 *requestId);
212
213
UA_MonitoredItemCreateResult UA_EXPORT UA_THREADSAFE
214
UA_Client_MonitoredItems_createDataChange(UA_Client *client,
215
    UA_UInt32 subscriptionId,
216
    UA_TimestampsToReturn timestampsToReturn,
217
    const UA_MonitoredItemCreateRequest item,
218
    void *context, UA_Client_DataChangeNotificationCallback callback,
219
    UA_Client_DeleteMonitoredItemCallback deleteCallback);
220
221
/* Monitor the EventNotifier attribute only */
222
UA_CreateMonitoredItemsResponse UA_EXPORT UA_THREADSAFE
223
UA_Client_MonitoredItems_createEvents(UA_Client *client,
224
    const UA_CreateMonitoredItemsRequest request, void **contexts,
225
    UA_Client_EventNotificationCallback *callback,
226
    UA_Client_DeleteMonitoredItemCallback *deleteCallback);
227
228
/* Monitor the EventNotifier attribute only */
229
UA_StatusCode UA_EXPORT UA_THREADSAFE
230
UA_Client_MonitoredItems_createEvents_async(UA_Client *client,
231
    const UA_CreateMonitoredItemsRequest request, void **contexts,
232
    UA_Client_EventNotificationCallback *callbacks,
233
    UA_Client_DeleteMonitoredItemCallback *deleteCallbacks,
234
    UA_ClientAsyncCreateMonitoredItemsCallback createCallback,
235
    void *userdata, UA_UInt32 *requestId);
236
237
UA_MonitoredItemCreateResult UA_EXPORT UA_THREADSAFE
238
UA_Client_MonitoredItems_createEvent(UA_Client *client,
239
    UA_UInt32 subscriptionId,
240
    UA_TimestampsToReturn timestampsToReturn,
241
    const UA_MonitoredItemCreateRequest item,
242
    void *context, UA_Client_EventNotificationCallback callback,
243
    UA_Client_DeleteMonitoredItemCallback deleteCallback);
244
245
UA_DeleteMonitoredItemsResponse UA_EXPORT UA_THREADSAFE
246
UA_Client_MonitoredItems_delete(UA_Client *client,
247
    const UA_DeleteMonitoredItemsRequest);
248
249
typedef void
250
(*UA_ClientAsyncDeleteMonitoredItemsCallback)(
251
    UA_Client *client, void *userdata, UA_UInt32 requestId,
252
    UA_DeleteMonitoredItemsResponse *response);
253
254
UA_StatusCode UA_EXPORT UA_THREADSAFE
255
UA_Client_MonitoredItems_delete_async(UA_Client *client,
256
    const UA_DeleteMonitoredItemsRequest request,
257
    UA_ClientAsyncDeleteMonitoredItemsCallback callback,
258
    void *userdata, UA_UInt32 *requestId);
259
260
UA_StatusCode UA_EXPORT UA_THREADSAFE
261
UA_Client_MonitoredItems_deleteSingle(UA_Client *client,
262
    UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId);
263
264
/**
265
 * The "ClientHandle" is part of the MonitoredItem configuration. The handle is
266
 * set internally and not exposed to the user. */
267
268
UA_ModifyMonitoredItemsResponse UA_EXPORT UA_THREADSAFE
269
UA_Client_MonitoredItems_modify(UA_Client *client,
270
    const UA_ModifyMonitoredItemsRequest request);
271
272
typedef void
273
(*UA_ClientAsyncModifyMonitoredItemsCallback)(
274
    UA_Client *client, void *userdata, UA_UInt32 requestId,
275
    UA_DeleteMonitoredItemsResponse *response);
276
277
UA_StatusCode UA_EXPORT UA_THREADSAFE
278
UA_Client_MonitoredItems_modify_async(UA_Client *client,
279
    const UA_ModifyMonitoredItemsRequest request,
280
    UA_ClientAsyncModifyMonitoredItemsCallback callback,
281
    void *userdata, UA_UInt32 *requestId);
282
283
UA_SetMonitoringModeResponse UA_EXPORT UA_THREADSAFE
284
UA_Client_MonitoredItems_setMonitoringMode(
285
    UA_Client *client, const UA_SetMonitoringModeRequest request);
286
287
typedef void
288
(*UA_ClientAsyncSetMonitoringModeCallback)(
289
    UA_Client *client, void *userdata, UA_UInt32 requestId,
290
    UA_SetMonitoringModeResponse *response);
291
292
UA_StatusCode UA_EXPORT UA_THREADSAFE
293
UA_Client_MonitoredItems_setMonitoringMode_async(
294
    UA_Client *client, const UA_SetMonitoringModeRequest request,
295
    UA_ClientAsyncSetMonitoringModeCallback callback,
296
    void *userdata, UA_UInt32 *requestId);
297
298
UA_SetTriggeringResponse UA_EXPORT UA_THREADSAFE
299
UA_Client_MonitoredItems_setTriggering(
300
    UA_Client *client, const UA_SetTriggeringRequest request);
301
302
typedef void
303
(*UA_ClientAsyncSetTriggeringCallback)(
304
    UA_Client *client, void *userdata, UA_UInt32 requestId,
305
    UA_SetTriggeringResponse *response);
306
307
UA_StatusCode UA_EXPORT UA_THREADSAFE
308
UA_Client_MonitoredItems_setTriggering_async(
309
    UA_Client *client, const UA_SetTriggeringRequest request,
310
    UA_ClientAsyncSetTriggeringCallback callback,
311
    void *userdata, UA_UInt32 *requestId);
312
313
/* Retrieve or change the user supplied MonitoredItem context */
314
UA_StatusCode UA_EXPORT UA_THREADSAFE
315
UA_Client_MonitoredItem_getContext(UA_Client *client,
316
    UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId,
317
    void **monContext);
318
319
UA_StatusCode UA_EXPORT UA_THREADSAFE
320
UA_Client_MonitoredItem_setContext(UA_Client *client,
321
    UA_UInt32 subscriptionId, UA_UInt32 monitoredItemId,
322
    void *monContext);
323
324
_UA_END_DECLS
325
326
#endif /* UA_CLIENT_SUBSCRIPTIONS_H_ */