forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
261 lines (241 loc) · 12 KB
/
client.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
259
260
261
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#ifdef UA_NO_AMALGAMATION
# include "ua_types.h"
# include "ua_client.h"
# include "ua_client_highlevel.h"
# include "ua_nodeids.h"
# include "ua_network_tcp.h"
# include "ua_config_standard.h"
#else
# include "open62541.h"
# include <string.h>
# include <stdlib.h>
#endif
#include <stdio.h>
static void
handler_TheAnswerChanged(UA_UInt32 monId, UA_DataValue *value, void *context) {
printf("The Answer has changed!\n");
}
static UA_StatusCode
nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void *handle) {
if(isInverse)
return UA_STATUSCODE_GOOD;
UA_NodeId *parent = (UA_NodeId *)handle;
printf("%d, %d --- %d ---> NodeId %d, %d\n",
parent->namespaceIndex, parent->identifier.numeric,
referenceTypeId.identifier.numeric, childId.namespaceIndex,
childId.identifier.numeric);
return UA_STATUSCODE_GOOD;
}
int main(int argc, char *argv[]) {
UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
/* Listing endpoints */
UA_EndpointDescription* endpointArray = NULL;
size_t endpointArraySize = 0;
UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:16664",
&endpointArraySize, &endpointArray);
if(retval != UA_STATUSCODE_GOOD) {
UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
UA_Client_delete(client);
return (int)retval;
}
printf("%i endpoints found\n", (int)endpointArraySize);
for(size_t i=0;i<endpointArraySize;i++){
printf("URL of endpoint %i is %.*s\n", (int)i,
(int)endpointArray[i].endpointUrl.length,
endpointArray[i].endpointUrl.data);
}
UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
/* Connect to a server */
/* anonymous connect would be: retval = UA_Client_connect(client, "opc.tcp://localhost:16664"); */
retval = UA_Client_connect_username(client, "opc.tcp://localhost:16664", "user1", "password");
if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return (int)retval;
}
/* Browse some objects */
printf("Browsing nodes in objects folder:\n");
UA_BrowseRequest bReq;
UA_BrowseRequest_init(&bReq);
bReq.requestedMaxReferencesPerNode = 0;
bReq.nodesToBrowse = UA_BrowseDescription_new();
bReq.nodesToBrowseSize = 1;
bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
for (size_t i = 0; i < bResp.resultsSize; ++i) {
for (size_t j = 0; j < bResp.results[i].referencesSize; ++j) {
UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length,
ref->browseName.name.data, (int)ref->displayName.text.length,
ref->displayName.text.data);
} else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
(int)ref->nodeId.nodeId.identifier.string.length,
ref->nodeId.nodeId.identifier.string.data,
(int)ref->browseName.name.length, ref->browseName.name.data,
(int)ref->displayName.text.length, ref->displayName.text.data);
}
/* TODO: distinguish further types */
}
}
UA_BrowseRequest_deleteMembers(&bReq);
UA_BrowseResponse_deleteMembers(&bResp);
/* Same thing, this time using the node iterator... */
UA_NodeId *parent = UA_NodeId_new();
*parent = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
nodeIter, (void *) parent);
UA_NodeId_delete(parent);
#ifdef UA_ENABLE_SUBSCRIPTIONS
/* Create a subscription */
UA_UInt32 subId = 0;
UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_standard, &subId);
if(subId)
printf("Create subscription succeeded, id %u\n", subId);
/* Add a MonitoredItem */
UA_NodeId monitorThis = UA_NODEID_STRING(1, "the.answer");
UA_UInt32 monId = 0;
UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE,
&handler_TheAnswerChanged, NULL, &monId);
if (monId)
printf("Monitoring 'the.answer', id %u\n", subId);
/* The first publish request should return the initial value of the variable */
UA_Client_Subscriptions_manuallySendPublishRequest(client);
#endif
/* Read attribute */
UA_Int32 value = 0;
printf("\nReading the value of node (1, \"the.answer\"):\n");
UA_Variant *val = UA_Variant_new();
retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
val->type == &UA_TYPES[UA_TYPES_INT32]) {
value = *(UA_Int32*)val->data;
printf("the value is: %i\n", value);
}
UA_Variant_delete(val);
/* Write node attribute */
value++;
printf("\nWriting a value of node (1, \"the.answer\"):\n");
UA_WriteRequest wReq;
UA_WriteRequest_init(&wReq);
wReq.nodesToWrite = UA_WriteValue_new();
wReq.nodesToWriteSize = 1;
wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
wReq.nodesToWrite[0].value.hasValue = true;
wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; /* do not free the integer on deletion */
wReq.nodesToWrite[0].value.value.data = &value;
UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
printf("the new value is: %i\n", value);
UA_WriteRequest_deleteMembers(&wReq);
UA_WriteResponse_deleteMembers(&wResp);
/* Write node attribute (using the highlevel API) */
value++;
UA_Variant *myVariant = UA_Variant_new();
UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
UA_Client_writeValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), myVariant);
UA_Variant_delete(myVariant);
#ifdef UA_ENABLE_SUBSCRIPTIONS
/* Take another look at the.answer */
UA_Client_Subscriptions_manuallySendPublishRequest(client);
/* Delete the subscription */
if(!UA_Client_Subscriptions_remove(client, subId))
printf("Subscription removed\n");
#endif
#ifdef UA_ENABLE_METHODCALLS
/* Call a remote method */
UA_Variant input;
UA_String argString = UA_STRING("Hello Server");
UA_Variant_init(&input);
UA_Variant_setScalarCopy(&input, &argString, &UA_TYPES[UA_TYPES_STRING]);
size_t outputSize;
UA_Variant *output;
retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(1, 62541), 1, &input, &outputSize, &output);
if(retval == UA_STATUSCODE_GOOD) {
printf("Method call was successfull, and %lu returned values available.\n",
(unsigned long)outputSize);
UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
} else {
printf("Method call was unsuccessfull, and %x returned values available.\n", retval);
}
UA_Variant_deleteMembers(&input);
#endif
#ifdef UA_ENABLE_NODEMANAGEMENT
/* Add new nodes*/
/* New ReferenceType */
UA_NodeId ref_id;
UA_ReferenceTypeAttributes ref_attr;
UA_ReferenceTypeAttributes_init(&ref_attr);
ref_attr.displayName = UA_LOCALIZEDTEXT("en_US", "NewReference");
ref_attr.description = UA_LOCALIZEDTEXT("en_US", "References something that might or might not exist");
ref_attr.inverseName = UA_LOCALIZEDTEXT("en_US", "IsNewlyReferencedBy");
retval = UA_Client_addReferenceTypeNode(client,
UA_NODEID_NUMERIC(1, 12133),
UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
UA_QUALIFIEDNAME(1, "NewReference"),
ref_attr, &ref_id);
if(retval == UA_STATUSCODE_GOOD )
printf("Created 'NewReference' with numeric NodeID %u\n", ref_id.identifier.numeric);
/* New ObjectType */
UA_NodeId objt_id;
UA_ObjectTypeAttributes objt_attr;
UA_ObjectTypeAttributes_init(&objt_attr);
objt_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewObjectType");
objt_attr.description = UA_LOCALIZEDTEXT("en_US", "Put innovative description here");
retval = UA_Client_addObjectTypeNode(client,
UA_NODEID_NUMERIC(1, 12134),
UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
UA_QUALIFIEDNAME(1, "NewObjectType"),
objt_attr, &objt_id);
if(retval == UA_STATUSCODE_GOOD)
printf("Created 'NewObjectType' with numeric NodeID %u\n", objt_id.identifier.numeric);
/* New Object */
UA_NodeId obj_id;
UA_ObjectAttributes obj_attr;
UA_ObjectAttributes_init(&obj_attr);
obj_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewGreatNode");
obj_attr.description = UA_LOCALIZEDTEXT("de_DE", "Hier koennte Ihre Webung stehen!");
retval = UA_Client_addObjectNode(client,
UA_NODEID_NUMERIC(1, 0),
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
UA_QUALIFIEDNAME(1, "TheGreatNode"),
UA_NODEID_NUMERIC(1, 12134),
obj_attr, &obj_id);
if(retval == UA_STATUSCODE_GOOD )
printf("Created 'NewObject' with numeric NodeID %u\n", obj_id.identifier.numeric);
/* New Integer Variable */
UA_NodeId var_id;
UA_VariableAttributes var_attr;
UA_VariableAttributes_init(&var_attr);
var_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewVariableNode");
var_attr.description =
UA_LOCALIZEDTEXT("en_US", "This integer is just amazing - it has digits and everything.");
UA_Int32 int_value = 1234;
/* This does not copy the value */
UA_Variant_setScalar(&var_attr.value, &int_value, &UA_TYPES[UA_TYPES_INT32]);
var_attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
retval = UA_Client_addVariableNode(client,
UA_NODEID_NUMERIC(1, 0), // Assign new/random NodeID
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
UA_QUALIFIEDNAME(0, "VariableNode"),
UA_NODEID_NULL, // no variable type
var_attr, &var_id);
if(retval == UA_STATUSCODE_GOOD )
printf("Created 'NewVariable' with numeric NodeID %u\n", var_id.identifier.numeric);
#endif
UA_Client_disconnect(client);
UA_Client_delete(client);
return (int) UA_STATUSCODE_GOOD;
}