Skip to content

Commit

Permalink
allow options to be separated by space or comma
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-zimoch authored and ralphlange committed Jun 21, 2024
1 parent 5fcc86c commit a58ddae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion devOpcuaSup/UaSdk/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Session::showClientSecurity()
const char Session::optionUsage[]
= "Sets options for existing OPC UA sessions or subscriptions.\n\n"
"pattern pattern for session or subscription names (* and ? supported)\n"
"[options] colon separated list of options in 'key=value' format\n\n"
"[options] list of options in 'key=value' format\n\n"
"Valid session options are:\n"
"debug debug level [default 0 = no debug]\n"
"autoconnect automatically connect sessions [default y]\n"
Expand Down
71 changes: 40 additions & 31 deletions devOpcuaSup/iocshIntegration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ replaceEnvVars(const char *path)

static const iocshArg opcuaSessionArg0 = {"name", iocshArgString};
static const iocshArg opcuaSessionArg1 = {"URL", iocshArgString};
static const iocshArg opcuaSessionArg2 = {"[options]", iocshArgString};
static const iocshArg opcuaSessionArg2 = {"[options]", iocshArgArgv};

static const iocshArg *const opcuaSessionArg[3] = {&opcuaSessionArg0,
&opcuaSessionArg1,
Expand All @@ -109,7 +109,7 @@ const char opcuaSessionUsage[]
"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"
"[options] list of options in 'key=value' format\n"
" (see 'help opcuaOptions' for a list of valid options)\n";

static const iocshFuncDef opcuaSessionFuncDef = {"opcuaSession",
Expand Down Expand Up @@ -146,9 +146,10 @@ opcuaSessionCallFunc(const iocshArgBuf *args)
}

std::list<std::pair<std::string, std::string>> setopts;
if (args[2].sval) {
auto options = splitString(args[2].sval, ':');
for (int i = 1; i < args[2].aval.ac; i++) {
auto options = splitString(args[2].aval.av[i], ':');
for (auto &opt : options) {
if (opt.empty()) continue;
auto keyval = splitString(opt, '=');
if (keyval.size() != 2) {
errlogPrintf("option '%s' must follow 'key=value' format - ignored\n",
Expand Down Expand Up @@ -183,7 +184,7 @@ opcuaSessionCallFunc(const iocshArgBuf *args)
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 opcuaSubscriptionArg3 = {"[options]", iocshArgArgv};

static const iocshArg *const opcuaSubscriptionArg[4] = {&opcuaSubscriptionArg0,
&opcuaSubscriptionArg1,
Expand All @@ -196,7 +197,7 @@ const char opcuaSubscriptionUsage[]
"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"
"[options] list of options in 'key=value' format\n"
" (see 'help opcuaOptions' for a list of valid options)\n";

static const iocshFuncDef opcuaSubscriptionFuncDef = {"opcuaSubscription",
Expand Down Expand Up @@ -250,9 +251,10 @@ static
}

std::list<std::pair<std::string, std::string>> setopts;
if (args[3].sval) {
auto options = splitString(args[3].sval, ':');
for (int i = 1; i < args[3].aval.ac; i++) {
auto options = splitString(args[3].aval.av[i], ':');
for (auto &opt : options) {
if (opt.empty()) continue;
auto keyval = splitString(opt, '=');
if (keyval.size() != 2) {
errlogPrintf("option '%s' must follow 'key=value' format - ignored\n",
Expand Down Expand Up @@ -285,7 +287,7 @@ static
}

static const iocshArg opcuaOptionsArg0 = {"pattern", iocshArgString};
static const iocshArg opcuaOptionsArg1 = {"[options]", iocshArgString};
static const iocshArg opcuaOptionsArg1 = {"[options]", iocshArgArgv};

static const iocshArg *const opcuaOptionsArg[2] = {&opcuaOptionsArg0, &opcuaOptionsArg1};

Expand All @@ -309,39 +311,46 @@ opcuaOptionsCallFunc(const iocshArgBuf *args)
} else if (strcmp(args[0].sval, "help") == 0) {
std::cout << opcuaOptionsUsage.c_str() << std::endl;
} else {
if (!args[1].sval) {
std::cerr << args[1].aval.ac << " options: " << args[1].aval.av[0] << std::endl;
if (args[1].aval.ac <= 1) {
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());
for (int i = 1; i < args[1].aval.ac; i++) {
auto options = splitString(args[1].aval.av[i], ':');
for (auto &opt : options) {
if (opt.empty()) continue;
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());
for (int i = 1; i < args[1].aval.ac; i++) {
auto options = splitString(args[1].aval.av[i], ':');
for (auto &opt : options) {
if (opt.empty()) continue;
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());
}
}
}
}
Expand Down Expand Up @@ -742,7 +751,7 @@ opcuaCreateSessionCallFunc(const iocshArgBuf *args)
{
std::cerr
<< "DEPRECATION WARNING: opcuaCreateSession is obsolete; use the improved opcuaSession "
"command instead (that supports a generic option string)."
"command instead (that supports a generic option list)."
<< std::endl;

try {
Expand Down Expand Up @@ -871,7 +880,7 @@ 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)."
"opcuaSubscription command instead (that supports a generic option list)."
<< std::endl;

try {
Expand Down
2 changes: 1 addition & 1 deletion devOpcuaSup/open62541/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Session::showClientSecurity()
const char Session::optionUsage[]
= "Sets options for existing OPC UA sessions or subscriptions.\n\n"
"pattern pattern for session or subscription names (* and ? supported)\n"
"[options] colon separated list of options in 'key=value' format\n\n"
"[options] list of options in 'key=value' format\n\n"
"Valid session options are:\n"
"debug debug level [default 0 = no debug]\n"
"autoconnect automatically connect sessions [default y]\n"
Expand Down

0 comments on commit a58ddae

Please sign in to comment.