forked from epics-modules/opcua
-
Notifications
You must be signed in to change notification settings - Fork 2
/
iocshIntegration.cpp
1105 lines (955 loc) · 41.5 KB
/
iocshIntegration.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************\
* Copyright (c) 2018-2021 ITER Organization.
* This module is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
\*************************************************************************/
/*
* Author: Ralph Lange <ralph.lange@gmx.de>
*
* based on prototype work by Bernhard Kuner <bernhard.kuner@helmholtz-berlin.de>
*/
#include <iostream>
#include <set>
#include <string>
#include <string.h>
#include <stdexcept>
#if defined(_WIN32)
#include <cstdlib>
#include <regex>
#include <winsock2.h>
#endif
#include <iocsh.h>
#include <errlog.h>
#include <epicsThread.h>
#include <epicsExport.h> // defines epicsExportSharedSymbols
#include "devOpcua.h"
#include "iocshVariables.h"
#include "linkParser.h"
#include "Session.h"
#include "Subscription.h"
#include "Registry.h"
#include "RecordConnector.h"
namespace DevOpcua {
// Configurable defaults
// session
double opcua_ConnectTimeout = 5.0; // [s]
int opcua_MaxOperationsPerServiceCall = 0; // no limit (do not batch)
// subscription
double opcua_DefaultPublishInterval = 100.0; // [ms]
// monitored item
double opcua_DefaultSamplingInterval = -1.0; // use publish interval [ms]
int opcua_DefaultServerQueueSize = 1; // no queueing
int opcua_DefaultDiscardOldest = 1; // discard oldest value in case of overrun
int opcua_DefaultUseServerTime = 1; // use server timestamp
int opcua_DefaultOutputReadback = 1; // make outputs bidirectional
double opcua_ClientQueueSizeFactor = 1.5; // client queue size factor (* server side size)
int opcua_MinimumClientQueueSize = 3; // minimum client queue size
extern "C" {
epicsExportAddress(double, opcua_ConnectTimeout);
epicsExportAddress(int, opcua_MaxOperationsPerServiceCall);
epicsExportAddress(double, opcua_DefaultPublishInterval);
epicsExportAddress(double, opcua_DefaultSamplingInterval);
epicsExportAddress(int, opcua_DefaultServerQueueSize);
epicsExportAddress(int, opcua_DefaultDiscardOldest);
epicsExportAddress(int, opcua_DefaultUseServerTime);
epicsExportAddress(int, opcua_DefaultOutputReadback);
epicsExportAddress(double, opcua_ClientQueueSizeFactor);
epicsExportAddress(int, opcua_MinimumClientQueueSize);
}
} // namespace DevOpcua
namespace {
using namespace DevOpcua;
// Create a std::string from a C string.
// On Windows: replacing any %varname%
// with the value of the environment variable varname if it exists
static std::string
replaceEnvVars(const char *path)
{
std::string result(path);
#if defined(_WIN32)
std::string s(path);
std::regex e("%([^ ]+)%"); // env var in Windows notation (e.g. %AppData%)
std::smatch m;
while (std::regex_search(s, m, e)) {
char *v = std::getenv(m.str(1).c_str());
if (v)
result.replace(result.find(m.str(0)), m.str(0).length(), v);
s = m.suffix().str();
}
#endif
return result;
}
static const iocshArg opcuaSessionArg0 = {"name", iocshArgString};
static const iocshArg opcuaSessionArg1 = {"URL", iocshArgString};
static const iocshArg opcuaSessionArg2 = {"[options]", iocshArgString};
static const iocshArg *const opcuaSessionArg[3] = {&opcuaSessionArg0,
&opcuaSessionArg1,
&opcuaSessionArg2};
const char opcuaSessionUsage[]
= "Configures a new OPC UA session, assigning it a name and the URL "
"of the OPC UA server.\nMust be called before iocInit.\n\n"
"name session name (no spaces)\n"
"URL URL of the OPC UA server (e.g. opc.tcp://192.168.1.23:4840)\n"
"[options] colon separated list of options in 'key=value' format\n"
" (see 'help opcuaOptions' for a list of valid options)\n";
static const iocshFuncDef opcuaSessionFuncDef = {"opcuaSession",
3,
opcuaSessionArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaSessionUsage
#endif
};
static void
opcuaSessionCallFunc(const iocshArgBuf *args)
{
try {
bool ok = true;
Session *s = nullptr;
int debug = 0;
if (!args[0].sval) {
errlogPrintf("missing argument #1 (session name)\n");
ok = false;
} else if (strchr(args[0].sval, ' ')) {
errlogPrintf("invalid argument #1 (session name) '%s'\n", args[0].sval);
ok = false;
} else if (RegistryKeyNamespace::global.contains(args[0].sval)) {
errlogPrintf("session name %s already in use\n", args[0].sval);
ok = false;
}
if (!args[1].sval) {
errlogPrintf("missing argument #2 (server URL)\n");
ok = false;
}
std::list<std::pair<std::string, std::string>> setopts;
if (args[2].sval) {
auto options = splitString(args[2].sval, ':');
for (auto &opt : options) {
auto keyval = splitString(opt, '=');
if (keyval.size() != 2) {
errlogPrintf("option '%s' must follow 'key=value' format - ignored\n",
opt.c_str());
} else {
if (keyval.front() == "debug")
debug = std::strtol(keyval.back().c_str(), nullptr, 0);
setopts.emplace_back(keyval.front(), keyval.back());
}
}
}
if (ok) {
s = Session::createSession(args[0].sval, args[1].sval);
if (s && debug)
errlogPrintf("opcuaSession: successfully created session '%s'\n", args[0].sval);
} else {
errlogPrintf("ERROR - no session created\n");
ok = false;
}
if (ok && s) {
for (auto &keyval : setopts)
s->setOption(keyval.first, keyval.second);
}
}
catch(std::exception& e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaSubscriptionArg0 = {"name", iocshArgString};
static const iocshArg opcuaSubscriptionArg1 = {"session", iocshArgString};
static const iocshArg opcuaSubscriptionArg2 = {"publishing interval [ms]", iocshArgDouble};
static const iocshArg opcuaSubscriptionArg3 = {"[options]", iocshArgString};
static const iocshArg *const opcuaSubscriptionArg[4] = {&opcuaSubscriptionArg0,
&opcuaSubscriptionArg1,
&opcuaSubscriptionArg2,
&opcuaSubscriptionArg3};
const char opcuaSubscriptionUsage[]
= "Configures a new OPC UA subscription, assigning it a name and creating it under an existing "
"session.\nMust be called before iocInit.\n\n"
"name subscription name (no spaces)\n"
"session name of the existing OPC UA session for the new subscription\n"
"publishing interval publishing interval for the new subscription (in ms)\n"
"[options] colon separated list of options in 'key=value' format\n"
" (see 'help opcuaOptions' for a list of valid options)\n";
static const iocshFuncDef opcuaSubscriptionFuncDef = {"opcuaSubscription",
4,
opcuaSubscriptionArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaSubscriptionUsage
#endif
};
static
void opcuaSubscriptionCallFunc (const iocshArgBuf *args)
{
try {
bool ok = true;
double publishingInterval = 0.;
int debug = 0;
Subscription *s = nullptr;
if (!args[0].sval) {
errlogPrintf("missing argument #1 (subscription name)\n");
ok = false;
} else if (strchr(args[0].sval, ' ')) {
errlogPrintf("invalid argument #1 (subscription name) '%s'\n",
args[0].sval);
ok = false;
} else if (Subscription::find(args[0].sval)) {
errlogPrintf("subscription name %s already in use\n",
args[0].sval);
ok = false;
}
if (!args[1].sval) {
errlogPrintf("missing argument #2 (session name)\n");
ok = false;
} else if (!Session::find(args[1].sval)) {
errlogPrintf("session %s does not exist\n",
args[1].sval);
ok = false;
}
if (args[2].dval < 0) {
errlogPrintf("invalid argument #3 (publishing interval) '%f' - ignored\n",
args[2].dval);
publishingInterval = opcua_DefaultPublishInterval;
} else if (args[2].dval == 0) {
publishingInterval = opcua_DefaultPublishInterval;
} else {
publishingInterval = args[2].dval;
}
std::list<std::pair<std::string, std::string>> setopts;
if (args[3].sval) {
auto options = splitString(args[3].sval, ':');
for (auto &opt : options) {
auto keyval = splitString(opt, '=');
if (keyval.size() != 2) {
errlogPrintf("option '%s' must follow 'key=value' format - ignored\n",
opt.c_str());
} else {
if (keyval.front() == "debug")
debug = std::strtol(keyval.back().c_str(), nullptr, 0);
setopts.emplace_back(keyval.front(), keyval.back());
}
}
}
if (ok) {
s = Subscription::createSubscription(args[0].sval, args[1].sval, publishingInterval);
if (s && debug)
errlogPrintf("opcuaSubscription: successfully created subscription '%s'\n", args[0].sval);
} else {
errlogPrintf("ERROR - no subscription created\n");
ok = false;
}
if (ok && s) {
for (auto &keyval : setopts)
s->setOption(keyval.first, keyval.second);
}
}
catch(std::exception& e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaOptionsArg0 = {"pattern", iocshArgString};
static const iocshArg opcuaOptionsArg1 = {"[options]", iocshArgString};
static const iocshArg *const opcuaOptionsArg[2] = {&opcuaOptionsArg0, &opcuaOptionsArg1};
static const std::string opcuaOptionsUsage = std::string(Session::optionUsage) + Subscription::optionUsage;
static const iocshFuncDef opcuaOptionsFuncDef = {"opcuaOptions",
2,
opcuaOptionsArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaOptionsUsage.c_str()
#endif
};
static void
opcuaOptionsCallFunc(const iocshArgBuf *args)
{
try {
if (args[0].sval == NULL || args[0].sval[0] == '\0') {
errlogPrintf("missing argument #1 (pattern for name)\n");
} else if (strcmp(args[0].sval, "help") == 0) {
std::cout << opcuaOptionsUsage.c_str() << std::endl;
} else {
if (!args[1].sval) {
errlogPrintf("missing argument #2 (options)\n");
} else {
bool foundSomething = false;
std::set<Session *> sessions = Session::glob(args[0].sval);
if (sessions.size()) {
foundSomething = true;
auto options = splitString(args[1].sval, ':');
for (auto &opt : options) {
auto keyval = splitString(opt, '=');
if (keyval.size() != 2) {
errlogPrintf("option '%s' must follow 'key=value' format - ignored\n",
opt.c_str());
} else {
for (auto &s : sessions)
s->setOption(keyval.front(), keyval.back());
}
}
}
if (!foundSomething) {
std::set<Subscription *> subscriptions = Subscription::glob(args[0].sval);
if (subscriptions.size()) {
foundSomething = true;
auto options = splitString(args[1].sval, ':');
for (auto &opt : options) {
auto keyval = splitString(opt, '=');
if (keyval.size() != 2) {
errlogPrintf(
"option '%s' must follow 'key=value' format - ignored\n",
opt.c_str());
} else {
for (auto &s : subscriptions)
s->setOption(keyval.front(), keyval.back());
}
}
}
}
if (!foundSomething)
errlogPrintf("No matches for pattern '%s'\n", args[0].sval);
}
}
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaShowArg0 = {"pattern", iocshArgString};
static const iocshArg opcuaShowArg1 = {"verbosity", iocshArgInt};
static const iocshArg *const opcuaShowArg[2] = {&opcuaShowArg0, &opcuaShowArg1};
const char opcuaShowUsage[]
= "Prints information about sessions, subscriptions, items and their related data elements.\n\n"
"pattern glob pattern (supports * and ?) for session, subscription, record names\n"
"verbosity amount of printed information (default 0 = sparse)\n";
static const iocshFuncDef opcuaShowFuncDef = {"opcuaShow",
2,
opcuaShowArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaShowUsage
#endif
};
static void
opcuaShowCallFunc(const iocshArgBuf *args)
{
if (args[0].sval == NULL || args[0].sval[0] == '\0') {
errlogPrintf("missing argument #1 (pattern for name)\n");
} else {
bool foundSomething = false;
std::set<Session *> sessions = Session::glob(args[0].sval);
if (sessions.size()) {
foundSomething = true;
for (auto &s : sessions)
s->show(args[1].ival);
}
if (!foundSomething) {
std::set<Subscription *> subscriptions = Subscription::glob(args[0].sval);
if (subscriptions.size()) {
foundSomething = true;
for (auto &s : subscriptions)
s->show(args[1].ival);
}
}
if (!foundSomething) {
std::set<RecordConnector *> connectors = RecordConnector::glob(args[0].sval);
if (connectors.size()) {
foundSomething = true;
for (auto &rc : connectors)
rc->pitem->show(args[1].ival);
}
}
if (!foundSomething)
errlogPrintf("No matches for pattern '%s'\n", args[0].sval);
}
}
static const iocshArg opcuaConnectArg0 = {"session", iocshArgString};
static const iocshArg *const opcuaConnectArg[1] = {&opcuaConnectArg0};
const char opcuaConnectUsage[]
= "Attempts to connect sessions to the configured OPC UA server.\n"
"For sessions with configured autoconnect option, the autoconnector is started.\n\n"
"session glob pattern (supports * and ?) for session names\n";
static const iocshFuncDef opcuaConnectFuncDef = {"opcuaConnect",
1,
opcuaConnectArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaConnectUsage
#endif
};
static
void opcuaConnectCallFunc (const iocshArgBuf *args)
{
bool ok = true;
if (args[0].sval == nullptr) {
errlogPrintf("ERROR : missing argument #1 (session name pattern)\n");
ok = false;
}
if (ok) {
try {
std::set<Session *> sessions = Session::glob(args[0].sval);
if (sessions.size())
for (auto &s : sessions)
s->connect(true);
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
}
static const iocshArg opcuaDisconnectArg0 = {"session name", iocshArgString};
static const iocshArg *const opcuaDisconnectArg[1] = {&opcuaDisconnectArg0};
const char opcuaDisconnectUsage[]
= "Gracefully disconnects sessions from the configured server.\n"
"For sessions with configured autoconnect option, the autoconnector is stopped.\n\n"
"session glob pattern (supports * and ?) for session names\n";
static const iocshFuncDef opcuaDisconnectFuncDef = {"opcuaDisconnect",
1,
opcuaDisconnectArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaDisconnectUsage
#endif
};
static
void opcuaDisconnectCallFunc (const iocshArgBuf *args)
{
bool ok = true;
if (args[0].sval == nullptr) {
errlogPrintf("ERROR : missing argument #1 (session name)\n");
ok = false;
}
if (ok) {
try {
std::set<Session *> sessions = Session::glob(args[0].sval);
if (sessions.size())
for (auto &s : sessions)
s->disconnect();
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
}
static const iocshArg opcuaMapNamespaceArg0 = {"session", iocshArgString};
static const iocshArg opcuaMapNamespaceArg1 = {"namespace index", iocshArgInt};
static const iocshArg opcuaMapNamespaceArg2 = {"namespace URI", iocshArgString};
static const iocshArg *const opcuaMapNamespaceArg[3] = {&opcuaMapNamespaceArg0, &opcuaMapNamespaceArg1,
&opcuaMapNamespaceArg2};
const char opcuaMapNamespaceUsage[]
= "Adds a namespace mapping to the mapping table of the specified session.\n"
"The specified numerical namespace index (used in the loaded databases) will be mapped to "
"the\nspecified namespace URI (on the server).\nThis allows to automatically adapt to servers "
"that use volatile namespace indices.\n\n"
"session existing session name\n"
"namespace index numerical namespace as used in the database files\n"
"namespace URI full URI identification of that namespace\n";
static const iocshFuncDef opcuaMapNamespaceFuncDef = {"opcuaMapNamespace",
3,
opcuaMapNamespaceArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaMapNamespaceUsage
#endif
};
static
void opcuaMapNamespaceCallFunc (const iocshArgBuf *args)
{
try {
bool ok = true;
unsigned short index = 0;
Session *s = nullptr;
if (args[0].sval == nullptr) {
errlogPrintf("missing argument #1 (session name)\n");
ok = false;
} else {
s = Session::find(args[0].sval);
if (!s) {
errlogPrintf("'%s' - no such session\n", args[0].sval);
ok = false;
}
}
if (args[1].ival < 0 || args[1].ival > USHRT_MAX) {
errlogPrintf("invalid argument #2 (namespace index) '%d'\n",
args[1].ival);
} else {
index = static_cast<unsigned short>(args[1].ival);
}
if (args[2].sval == nullptr) {
errlogPrintf("missing argument #3 (namespace URI)\n");
ok = false;
}
if (ok)
s->addNamespaceMapping(index, args[2].sval);
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaShowSecurityArg0 = {"session name [\"\"=client]", iocshArgString};
static const iocshArg *const opcuaShowSecurityArg[1] = {&opcuaShowSecurityArg0};
const char opcuaShowSecurityUsage[]
= "Prints information about the security setup of a sepcific session or the IOC client.\n\n"
"session name name of the session to report on (empty string for client report)\n";
static const iocshFuncDef opcuaShowSecurityFuncDef = {"opcuaShowSecurity",
1,
opcuaShowSecurityArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaShowSecurityUsage
#endif
};
static
void opcuaShowSecurityCallFunc (const iocshArgBuf *args)
{
try {
if (args[0].sval == nullptr || args[0].sval[0] == '\0') {
Session::showClientSecurity();
} else {
Session *s = Session::find(args[0].sval);
if (s)
s->showSecurity();
}
}
catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaClientCertificateArg0 = {"certificate (public key) file", iocshArgString};
static const iocshArg opcuaClientCertificateArg1 = {"private key file", iocshArgString};
static const iocshArg *const opcuaClientCertificateArg[2] = {&opcuaClientCertificateArg0, &opcuaClientCertificateArg1};
const char opcuaClientCertificateUsage[]
= "Sets up the OPC UA client certificates to use for the IOC client.\n\n"
"certificate file path to the file containing the certificate (public key)\n"
"private key file path to the file containing the private key\n";
static const iocshFuncDef opcuaClientCertificateFuncDef = {"opcuaClientCertificate", 2, opcuaClientCertificateArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaClientCertificateUsage
#endif
};
static
void opcuaClientCertificateCallFunc (const iocshArgBuf *args)
{
try {
bool ok = true;
if (args[0].sval == nullptr || args[0].sval[0] == '\0') {
errlogPrintf("missing argument #1 (certificate file)\n");
ok = false;
}
if (args[1].sval == nullptr || args[1].sval[0] == '\0') {
errlogPrintf("missing argument #2 (private key file)\n");
ok = false;
}
if (ok)
Session::setClientCertificate(replaceEnvVars(args[0].sval),
replaceEnvVars(args[1].sval));
}
catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaSetupPKIArg0 = {"PKI / server certs location", iocshArgString};
static const iocshArg opcuaSetupPKIArg1 = {"server revocation lists location", iocshArgString};
static const iocshArg opcuaSetupPKIArg2 = {"issuer certs location", iocshArgString};
static const iocshArg opcuaSetupPKIArg3 = {"issuer revocation lists location", iocshArgString};
static const iocshArg *const opcuaSetupPKIArg[4] = {&opcuaSetupPKIArg0, &opcuaSetupPKIArg1,
&opcuaSetupPKIArg2, &opcuaSetupPKIArg3};
const char opcuaSetupPKIUsage[]
= "Sets up the PKI file store of the IOC client, where certificates and revocation lists are "
"stored.\n"
"The first form (single parameter) expects a standard directory structure under the "
"specified location.\n"
"The second form (four parameters) explicitly defines the specific locations.\n\n"
"PKI / server certs location path to the PKI structure / to the location of "
"trusted server certs\n"
"server revocation lists location path to the location of server revocation lists\n"
"issuer certs location path to the location of issuer certificates\n"
"issuer revocation lists location path to the location of issuer revocation lists\n";
static const iocshFuncDef opcuaSetupPKIFuncDef = {"opcuaSetupPKI",
4,
opcuaSetupPKIArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaSetupPKIUsage
#endif
};
static
void opcuaSetupPKICallFunc (const iocshArgBuf *args)
{
try {
// Special case: only one arg => points to PKI root, use default structure
if (args[0].sval != nullptr && args[1].sval == nullptr) {
std::string pki(replaceEnvVars(args[0].sval));
if (pki.length() && pki.back() != pathsep)
pki.push_back(pathsep);
Session::setupPKI(pki + "trusted" + pathsep + "certs",
pki + "trusted" + pathsep + "crl",
pki + "issuers" + pathsep + "certs",
pki + "issuers" + pathsep + "crl");
} else {
bool ok = true;
for (unsigned i = 0; i < 4; i++) {
if (args[i].sval == nullptr || args[i].sval[0] == '\0') {
errlogPrintf("missing argument #%d - %s\n", i+1, opcuaSetupPKIArg[i]->name);
ok = false;
}
}
if (ok) {
Session::setupPKI(replaceEnvVars(args[0].sval),
replaceEnvVars(args[1].sval),
replaceEnvVars(args[2].sval),
replaceEnvVars(args[3].sval));
}
}
}
catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaSaveRejectedArg0 = {"location for saving rejected certs", iocshArgString};
static const iocshArg *const opcuaSaveRejectedArg[1] = {&opcuaSaveRejectedArg0};
const char opcuaSaveRejectedUsage[]
= "Sets the location where the client will save rejected certificates.\n\n"
"rejected certs location where to save rejected certificates\n";
static const iocshFuncDef opcuaSaveRejectedFuncDef = {"opcuaSaveRejected",
1,
opcuaSaveRejectedArg
#ifdef IOCSHFUNCDEF_HAS_USAGE
,
opcuaSaveRejectedUsage
#endif
};
static
void opcuaSaveRejectedCallFunc (const iocshArgBuf *args)
{
try {
if (args[0].sval == nullptr || args[0].sval[0] == '\0')
Session::saveRejected();
else
Session::saveRejected(replaceEnvVars(args[0].sval));
}
catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
// Deprecated functions (to be removed with v1.0)
// ----------------------------------------------
static const iocshArg opcuaCreateSessionArg0 = {"session name", iocshArgString};
static const iocshArg opcuaCreateSessionArg1 = {"server URL", iocshArgString};
static const iocshArg opcuaCreateSessionArg2 = {"debug level [0]", iocshArgInt};
static const iocshArg opcuaCreateSessionArg3 = {"autoconnect [true]", iocshArgString};
static const iocshArg *const opcuaCreateSessionArg[4] = {&opcuaCreateSessionArg0,
&opcuaCreateSessionArg1,
&opcuaCreateSessionArg2,
&opcuaCreateSessionArg3};
static const iocshFuncDef opcuaCreateSessionFuncDef = {"opcuaCreateSession",
4,
opcuaCreateSessionArg};
static void
opcuaCreateSessionCallFunc(const iocshArgBuf *args)
{
std::cerr
<< "DEPRECATION WARNING: opcuaCreateSession is obsolete; use the improved opcuaSession "
"command instead (that supports a generic option string)."
<< std::endl;
try {
bool ok = true;
int debuglevel = 0;
Session *s = nullptr;
if (args[0].sval == nullptr) {
errlogPrintf("missing argument #1 (session name)\n");
ok = false;
} else if (strchr(args[0].sval, ' ')) {
errlogPrintf("invalid argument #1 (session name) '%s'\n", args[0].sval);
ok = false;
} else if (RegistryKeyNamespace::global.contains(args[0].sval)) {
errlogPrintf("session name %s already in use\n", args[0].sval);
ok = false;
}
if (args[1].sval == nullptr) {
errlogPrintf("missing argument #2 (server URL)\n");
ok = false;
}
if (args[2].ival < 0) {
errlogPrintf("invalid argument #3 (debug level) '%d' - ignored\n", args[2].ival);
} else {
debuglevel = args[2].ival;
}
if (ok) {
s = Session::createSession(args[0].sval, args[1].sval);
if (debuglevel) {
errlogPrintf("opcuaCreateSession: successfully created session '%s'\n",
args[0].sval);
if (s)
s->setOption("debug", std::to_string(debuglevel));
}
} else {
errlogPrintf("ERROR - no session created\n");
}
if (args[3].sval != nullptr) {
if (s)
s->setOption("autoconnect", args[3].sval);
}
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaSetOptionArg0 = {"session name", iocshArgString};
static const iocshArg opcuaSetOptionArg1 = {"option name", iocshArgString};
static const iocshArg opcuaSetOptionArg2 = {"option value", iocshArgString};
static const iocshArg *const opcuaSetOptionArg[3] = {&opcuaSetOptionArg0, &opcuaSetOptionArg1,
&opcuaSetOptionArg2};
static const iocshFuncDef opcuaSetOptionFuncDef = {"opcuaSetOption", 3, opcuaSetOptionArg};
static
void opcuaSetOptionCallFunc (const iocshArgBuf *args)
{
try {
bool ok = true;
bool help = false;
Session *s = nullptr;
if (args[0].sval == nullptr) {
errlogPrintf("missing argument #1 (session name)\n");
ok = false;
} else if (strcmp(args[0].sval, "help") == 0) {
help = true;
} else {
s = Session::find(args[0].sval);
if (!s) {
errlogPrintf("'%s' - no such session\n", args[0].sval);
ok = false;
}
}
if (!help) {
if (args[1].sval == nullptr) {
errlogPrintf("missing argument #2 (option name)\n");
ok = false;
} else if (strchr(args[1].sval, ' ')) {
errlogPrintf("invalid argument #2 (option name) '%s'\n",
args[1].sval);
ok = false;
}
if (args[2].sval == nullptr) {
if (args[1].sval && strcmp(args[1].sval, "help") == 0) {
help = true;
} else {
errlogPrintf("missing argument #3 (value)\n");
ok = false;
}
}
}
if (ok) {
if (help)
std::cout << opcuaOptionsUsage.c_str() << std::endl;
else
s->setOption(args[1].sval, args[2].sval);
}
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaCreateSubscriptionArg0 = {"subscription name", iocshArgString};
static const iocshArg opcuaCreateSubscriptionArg1 = {"session name", iocshArgString};
static const iocshArg opcuaCreateSubscriptionArg2 = {"publishing interval (ms)", iocshArgDouble};
static const iocshArg opcuaCreateSubscriptionArg3 = {"priority [0]", iocshArgInt};
static const iocshArg opcuaCreateSubscriptionArg4 = {"debug level [0]", iocshArgInt};
static const iocshArg *const opcuaCreateSubscriptionArg[5] = {&opcuaCreateSubscriptionArg0, &opcuaCreateSubscriptionArg1,
&opcuaCreateSubscriptionArg2, &opcuaCreateSubscriptionArg3,
&opcuaCreateSubscriptionArg4};
static const iocshFuncDef opcuaCreateSubscriptionFuncDef = {"opcuaCreateSubscription", 5, opcuaCreateSubscriptionArg};
static
void opcuaCreateSubscriptionCallFunc (const iocshArgBuf *args)
{
std::cerr << "DEPRECATION WARNING: opcuaCreateSubscription is obsolete; use the improved "
"opcuaSubscription command instead (that supports a generic option string)."
<< std::endl;
try {
bool ok = true;
int debuglevel = 0;
double publishingInterval = 0.;
Subscription *s = nullptr;
if (args[0].sval == nullptr) {
errlogPrintf("missing argument #1 (subscription name)\n");
ok = false;
} else if (strchr(args[0].sval, ' ')) {
errlogPrintf("invalid argument #1 (subscription name) '%s'\n",
args[0].sval);
ok = false;
} else if (Subscription::find(args[0].sval)) {
errlogPrintf("subscription name %s already in use\n",
args[0].sval);
ok = false;
}
if (args[1].sval == nullptr) {
errlogPrintf("missing argument #2 (session name)\n");
ok = false;
} else if (strchr(args[1].sval, ' ')) {
errlogPrintf("invalid argument #2 (session name) '%s'\n",
args[1].sval);
ok = false;
} else if (!Session::find(args[1].sval)) {
errlogPrintf("session %s does not exist\n",
args[1].sval);
ok = false;
}
if (args[2].dval < 0) {
errlogPrintf("invalid argument #3 (publishing interval) '%f'\n",
args[2].dval);
ok = false;
} else if (args[2].dval == 0) {
publishingInterval = opcua_DefaultPublishInterval;
} else {
publishingInterval = args[2].dval;
}
if (args[4].ival < 0) {
errlogPrintf("out-of-range argument #5 (debug level) '%d' - ignored\n", args[4].ival);
} else {
debuglevel = args[4].ival;
}
if (ok) {
s = Subscription::createSubscription(args[0].sval, args[1].sval, publishingInterval);
if (s && debuglevel) {
errlogPrintf("opcuaCreateSubscription: successfully configured subscription '%s'\n",
args[0].sval);
s->setOption("debug", std::to_string(debuglevel));
}
} else {
errlogPrintf("ERROR - no subscription created\n");
}
if (args[3].ival < 0 || args[3].ival > 255) {
errlogPrintf("out-of-range argument #4 (priority) '%d' - ignored\n",
args[3].ival);
} else {
if (s)
s->setOption("priority", std::to_string(args[3].ival));
}
}
catch(std::exception& e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaShowSessionArg0 = {"session name", iocshArgString};
static const iocshArg opcuaShowSessionArg1 = {"verbosity", iocshArgInt};
static const iocshArg *const opcuaShowSessionArg[2] = {&opcuaShowSessionArg0, &opcuaShowSessionArg1};
static const iocshFuncDef opcuaShowSessionFuncDef = {"opcuaShowSession", 2, opcuaShowSessionArg};
static
void opcuaShowSessionCallFunc (const iocshArgBuf *args)
{
std::cerr << "DEPRECATION WARNING: opcuaShowSession is obsolete; use the improved opcuaShow "
"command instead (that supports glob patterns)."
<< std::endl;
try {
if (args[0].sval == nullptr || args[0].sval[0] == '\0') {
Session::showAll(args[1].ival);
} else {
Session *s = Session::find(args[0].sval);
if (s)
s->show(args[1].ival);
}
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaDebugSessionArg0 = {"session name [\"\"=all]", iocshArgString};
static const iocshArg opcuaDebugSessionArg1 = {"debug level [0]", iocshArgInt};
static const iocshArg *const opcuaDebugSessionArg[2] = {&opcuaDebugSessionArg0, &opcuaDebugSessionArg1};
static const iocshFuncDef opcuaDebugSessionFuncDef = {"opcuaDebugSession", 2, opcuaDebugSessionArg};
static
void opcuaDebugSessionCallFunc (const iocshArgBuf *args)
{
try {
Session *s = Session::find(args[0].sval);
if (s)
s->debug = args[1].ival;
} catch (std::exception &e) {
std::cerr << "ERROR : " << e.what() << std::endl;
}
}
static const iocshArg opcuaShowSubscriptionArg0 = {"subscription name", iocshArgString};
static const iocshArg opcuaShowSubscriptionArg1 = {"verbosity", iocshArgInt};
static const iocshArg *const opcuaShowSubscriptionArg[2] = {&opcuaShowSubscriptionArg0, &opcuaShowSubscriptionArg1};
static const iocshFuncDef opcuaShowSubscriptionFuncDef = {"opcuaShowSubscription", 2, opcuaShowSubscriptionArg};