forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_method_async.c
258 lines (222 loc) · 10.1 KB
/
client_method_async.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#include <open62541/client_config_default.h>
#include <open62541/client_highlevel_async.h>
#include <open62541/client_subscriptions.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server_config_default.h>
#include <open62541/client_subscriptions.h>
#include <stdlib.h>
#include <signal.h>
UA_Boolean running = true;
static void InitCallMulti(UA_Client* client);
#ifdef UA_ENABLE_METHODCALLS
static void
methodCalled(UA_Client *client, void *userdata, UA_UInt32 requestId,
UA_CallResponse *response) {
UA_UInt32 i;
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"**** CallRequest Response - Req:%u with %u results",
requestId, (UA_UInt32)response->resultsSize);
UA_StatusCode retval = response->responseHeader.serviceResult;
if (retval == UA_STATUSCODE_GOOD) {
for (i = 0; i < response->resultsSize; i++) {
if (response->resultsSize >= i)
retval = response->results[i].statusCode;
else
retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
if (retval != UA_STATUSCODE_GOOD) {
UA_CallResponse_clear(response);
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"**** CallRequest Response - Req: %u (%u) failed", requestId,i);
if (i == response->resultsSize)
return;
else
continue;
}
/* Move the output arguments */
UA_Variant *output = response->results[i].outputArguments;
size_t outputSize = response->results[i].outputArgumentsSize;
response->results[i].outputArguments = NULL;
response->results[i].outputArgumentsSize = 0;
if (retval == UA_STATUSCODE_GOOD) {
printf("---Method call was successful, returned %lu values.\n",
(unsigned long)outputSize);
UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
}
else {
printf("---Method call was unsuccessful, returned %x values.\n",
retval);
}
}
}
else
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "**** CallRequest Response - Req:%u FAILED", requestId);
}
UA_CallResponse_clear(response);
/* We initiate the MultiCall (2 methods within one CallRequest) */
InitCallMulti(client);
}
#ifdef UA_ENABLE_METHODCALLS
/* Workaround because we do not have an API for that yet */
static UA_StatusCode
UA_Client_call_asyncMulti(UA_Client *client,
const UA_NodeId objectId1, const UA_NodeId methodId1, size_t inputSize1, const UA_Variant *input1,
const UA_NodeId objectId2, const UA_NodeId methodId2, size_t inputSize2, const UA_Variant *input2,
UA_ClientAsyncServiceCallback callback, void *userdata, UA_UInt32 *reqId) {
UA_CallRequest request;
UA_CallRequest_init(&request);
UA_CallMethodRequest item[2];
UA_CallMethodRequest_init(&item[0]);
item[0].methodId = methodId1;
item[0].objectId = objectId1;
item[0].inputArguments = (UA_Variant *)(void*)(uintptr_t)input1; // cast const...
item[0].inputArgumentsSize = inputSize1;
UA_CallMethodRequest_init(&item[1]);
item[1].methodId = methodId2;
item[1].objectId = objectId2;
item[1].inputArguments = (UA_Variant *)(void*)(uintptr_t)input2; // cast const...
item[1].inputArgumentsSize = inputSize2;
request.methodsToCall = &item[0];
request.methodsToCallSize = 2;
return __UA_Client_AsyncService(client, &request,
&UA_TYPES[UA_TYPES_CALLREQUEST], callback,
&UA_TYPES[UA_TYPES_CALLRESPONSE], userdata, reqId);
}
/* End Workaround */
static void InitCallMulti(UA_Client* client) {
UA_UInt32 reqId = 0;
UA_Variant input;
UA_Variant_init(&input);
UA_String stringValue = UA_String_fromChars("World 3 (multi)");
UA_Variant_setScalar(&input, &stringValue, &UA_TYPES[UA_TYPES_STRING]);
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "**** Initiating CallRequest 3");
UA_Client_call_asyncMulti(client,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(1, 62542), 1, &input,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(1, 62541), 1, &input,
(UA_ClientAsyncServiceCallback)methodCalled, NULL, &reqId);
UA_String_clear(&stringValue);
}
#endif
#endif /* UA_ENABLE_METHODCALLS */
static void stopHandler(int sign) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Received Ctrl-C");
running = 0;
}
#ifdef UA_ENABLE_SUBSCRIPTIONS
static void
handler_currentTimeChanged(UA_Client *client, UA_UInt32 subId, void *subContext,
UA_UInt32 monId, void *monContext, UA_DataValue *value) {
//UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "currentTime has changed!");
if (UA_Variant_hasScalarType(&value->value, &UA_TYPES[UA_TYPES_DATETIME])) {
UA_DateTime raw_date = *(UA_DateTime *)value->value.data;
UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
}
}
static void
deleteSubscriptionCallback(UA_Client *client, UA_UInt32 subscriptionId, void *subscriptionContext) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Subscription Id %u was deleted", subscriptionId);
}
static void
subscriptionInactivityCallback(UA_Client *client, UA_UInt32 subId, void *subContext) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Inactivity for subscription %u", subId);
}
#endif
static void
stateCallback(UA_Client *client, UA_SecureChannelState channelState,
UA_SessionState sessionState, UA_StatusCode connectStatus) {
if(sessionState == UA_SESSIONSTATE_ACTIVATED) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"A session with the server is activated");
#ifdef UA_ENABLE_SUBSCRIPTIONS
/* A new session was created. We need to create the subscription. */
/* Create a subscription */
UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
NULL, NULL, deleteSubscriptionCallback);
if (response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Create subscription succeeded, id %u", response.subscriptionId);
else
return;
/* Add a MonitoredItem */
UA_MonitoredItemCreateRequest monRequest =
UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
UA_MonitoredItemCreateResult monResponse =
UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
UA_TIMESTAMPSTORETURN_BOTH,
monRequest, NULL, handler_currentTimeChanged, NULL);
if (monResponse.statusCode == UA_STATUSCODE_GOOD)
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Monitoring UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME', id %u",
monResponse.monitoredItemId);
#endif
//TODO: check the existance of the nodes inside these functions (otherwise seg faults)
#ifdef UA_ENABLE_METHODCALLS
UA_UInt32 reqId = 0;
UA_Variant input;
UA_Variant_init(&input);
UA_String stringValue = UA_String_fromChars("World 1");
UA_Variant_setScalar(&input, &stringValue, &UA_TYPES[UA_TYPES_STRING]);
/* Initiate Call 1 */
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "**** Initiating CallRequest 1");
UA_Client_call_async(client,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(1, 62541), 1, &input,
methodCalled, NULL, &reqId);
UA_String_clear(&stringValue);
/* Initiate Call 2 */
UA_Variant_init(&input);
stringValue = UA_String_fromChars("World 2");
UA_Variant_setScalar(&input, &stringValue, &UA_TYPES[UA_TYPES_STRING]);
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "**** Initiating CallRequest 2");
UA_Client_call_async(client,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(1, 62542), 1, &input,
methodCalled, NULL, &reqId);
UA_String_clear(&stringValue);
#endif /* UA_ENABLE_METHODCALLS */
}
if(sessionState == UA_SESSIONSTATE_CLOSED)
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Session disconnected");
}
int
main(int argc, char *argv[]) {
signal(SIGINT, stopHandler); /* catches ctrl-c */
UA_Client *client = UA_Client_new();
UA_ClientConfig *cc = UA_Client_getConfig(client);
UA_ClientConfig_setDefault(cc);
/* we use a high timeout because there may be other client and
* processing may take long if many method calls are waiting */
cc->timeout = 60000;
/* Set stateCallback */
cc->stateCallback = stateCallback;
#ifdef UA_ENABLE_SUBSCRIPTIONS
cc->subscriptionInactivityCallback = subscriptionInactivityCallback;
#endif
UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Not connected. Retrying to connect in 1 second");
UA_Client_delete(client);
return EXIT_SUCCESS;
}
/* Endless loop runAsync */
while (running) {
UA_Client_run_iterate(client, 100);
}
/* Clean up */
/* Async disconnect kills unprocessed requests */
// UA_Client_disconnect_async (client, &reqId); //can only be used when connected = true
// UA_Client_run_iterate (client, &timedOut);
UA_Client_disconnect(client);
UA_Client_delete(client);
return EXIT_SUCCESS;
}