forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_connect.c
148 lines (127 loc) · 4.42 KB
/
client_connect.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
/* 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.h>
#include <open62541/client_subscriptions.h>
#include <open62541/plugin/log_stdout.h>
#include "common.h"
static void usage(void) {
printf("Usage: client [-username name] [-password password] ");
#ifdef UA_ENABLE_ENCRYPTION
printf("[-cert certfile.der] [-key keyfile.der] "
"[-securityMode <0-3>] [-securityPolicy policyUri] ");
#endif
printf("opc.tcp://<host>:<port>\n");
}
int main(int argc, char *argv[]) {
UA_String securityPolicyUri = UA_STRING_NULL;
UA_MessageSecurityMode securityMode = UA_MESSAGESECURITYMODE_INVALID; /* allow everything */
char *serverurl = NULL;
char *username = NULL;
char *password = NULL;
#ifdef UA_ENABLE_ENCRYPTION
char *certfile = NULL;
char *keyfile = NULL;
#endif
/* At least one argument is required for the server uri */
if(argc <= 1) {
usage();
return EXIT_FAILURE;
}
/* Parse the arguments */
for(int argpos = 1; argpos < argc; argpos++) {
if(strcmp(argv[argpos], "--help") == 0 ||
strcmp(argv[argpos], "-h") == 0) {
usage();
return EXIT_SUCCESS;
}
if(argpos + 1 == argc) {
serverurl = argv[argpos];
continue;
}
if(strcmp(argv[argpos], "-username") == 0) {
argpos++;
username = argv[argpos];
continue;
}
if(strcmp(argv[argpos], "-password") == 0) {
argpos++;
password = argv[argpos];
continue;
}
#ifdef UA_ENABLE_ENCRYPTION
if(strcmp(argv[argpos], "-cert") == 0) {
argpos++;
certfile = argv[argpos];
continue;
}
if(strcmp(argv[argpos], "-key") == 0) {
argpos++;
keyfile = argv[argpos];
continue;
}
if(strcmp(argv[argpos], "-securityMode") == 0) {
argpos++;
if(sscanf(argv[argpos], "%i", (int*)&securityMode) != 1) {
usage();
return EXIT_FAILURE;
}
continue;
}
if(strcmp(argv[argpos], "-securityPolicy") == 0) {
argpos++;
securityPolicyUri = UA_String_fromChars(argv[argpos]);
continue;
}
#endif
usage();
return EXIT_FAILURE;
}
/* Create the server and set its config */
UA_Client *client = UA_Client_new();
UA_ClientConfig *cc = UA_Client_getConfig(client);
#ifdef UA_ENABLE_ENCRYPTION
if(certfile) {
UA_ByteString certificate = loadFile(certfile);
UA_ByteString privateKey = loadFile(keyfile);
UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
NULL, 0, NULL, 0);
UA_ByteString_clear(&certificate);
UA_ByteString_clear(&privateKey);
} else {
UA_ClientConfig_setDefault(cc);
}
#else
UA_ClientConfig_setDefault(cc);
#endif
cc->securityMode = securityMode;
cc->securityPolicyUri = securityPolicyUri;
/* Connect to the server */
UA_StatusCode retval = UA_STATUSCODE_GOOD;
if(username)
retval = UA_Client_connectUsername(client, serverurl, username, password);
else
retval = UA_Client_connect(client, serverurl);
if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return EXIT_FAILURE;
}
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Connected!");
/* Read the server-time */
UA_Variant value;
UA_Variant_init(&value);
UA_Client_readValueAttribute(client,
UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME),
&value);
if(UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
UA_DateTimeStruct dts = UA_DateTime_toStruct(*(UA_DateTime *)value.data);
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"The server date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
}
UA_Variant_clear(&value);
/* Clean up */
UA_Client_disconnect(client);
UA_Client_delete(client);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}