opcua_options.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #include "opcua_options.h"
00012 
00013 #include <opc/ua/protocol/string_utils.h>
00014 
00015 #include <boost/program_options/options_description.hpp>
00016 #include <boost/program_options/parsers.hpp>
00017 #include <boost/program_options/variables_map.hpp>
00018 #include <iostream>
00019 
00020 #ifndef CONFIG_PATH
00021 #define CONFIG_PATH "/etc/opcua/client"
00022 #endif
00023 
00024 namespace
00025 {
00026   namespace po = boost::program_options;
00027   using namespace OpcUa;
00028 
00029   const char* OPTION_HELP = "help";
00030   const char* OPTION_GET_ENDPOINTS = "get-endpoints";
00031   const char* OPTION_BROWSE = "browse";
00032   const char* OPTION_READ = "read";
00033   const char* OPTION_WRITE = "write";
00034   const char* OPTION_CREATE_SUBSCRIPTION = "create-subscription";
00035   const char* OPTION_FIND_ServerS = "find-servers";
00036   const char* OPTION_REGISTER_MODULE = "register-module";
00037   const char* OPTION_UNREGISTER_MODULE = "unregister-module";
00038 
00039   const char* OPTION_MODULE_Id = "id";
00040   const char* OPTION_MODULE_PATH = "path";
00041   const char* OPTION_CONFIG_DIR = "config-dir";
00042 
00043   const char* OPTION_Server_URI = "uri";
00044   const char* OPTION_ATTRIBUTE = "attribute";
00045   const char* OPTION_NODE_Id = "node-id";
00046 
00047 
00048   const char* OPTION_VALUE_BYTE  = "value-byte";
00049   const char* OPTION_VALUE_SBYTE = "value-sbyte";
00050   const char* OPTION_VALUE_UINT16 = "value-uint16";
00051   const char* OPTION_VALUE_INT16 = "value-int16";
00052   const char* OPTION_VALUE_UINT32 = "value-uint32";
00053   const char* OPTION_VALUE_INT32 = "value-int32";
00054   const char* OPTION_VALUE_UINT64 = "value-uint64";
00055   const char* OPTION_VALUE_INT64 = "value-int64";
00056   const char* OPTION_VALUE_FLOAT = "value-float";
00057   const char* OPTION_VALUE_DOUBLE = "value-double";
00058   const char* OPTION_VALUE_STRING = "value-string";
00059   
00060   // codegen
00061   #include "opcua_options_attribute_ids.h"
00062 
00063   NodeId GetNodeIdOptionValue(const po::variables_map& vm)
00064   {
00065     const std::string& value = vm[OPTION_NODE_Id].as<std::string>();
00066     return OpcUa::ToNodeId(value);
00067   }
00068 
00069   Variant GetOptionValue(const po::variables_map& vm)
00070   {
00071     if (vm.count(OPTION_VALUE_BYTE))
00072     {
00073       return Variant(vm[OPTION_VALUE_BYTE].as<uint8_t>());
00074     }
00075     if (vm.count(OPTION_VALUE_SBYTE))
00076     {
00077       return Variant(vm[OPTION_VALUE_SBYTE].as<int8_t>());
00078     }
00079     if (vm.count(OPTION_VALUE_UINT16))
00080     {
00081       return Variant(vm[OPTION_VALUE_UINT16].as<uint16_t>());
00082     }
00083     if (vm.count(OPTION_VALUE_INT16))
00084     {
00085       return Variant(vm[OPTION_VALUE_INT16].as<int16_t>());
00086     }
00087     if (vm.count(OPTION_VALUE_UINT32))
00088     {
00089       return Variant(vm[OPTION_VALUE_UINT32].as<uint32_t>());
00090     }
00091     if (vm.count(OPTION_VALUE_INT32))
00092     {
00093       return Variant(vm[OPTION_VALUE_INT32].as<int32_t>());
00094     }
00095     if (vm.count(OPTION_VALUE_UINT64))
00096     {
00097       return Variant(vm[OPTION_VALUE_UINT64].as<uint64_t>());
00098     }
00099     if (vm.count(OPTION_VALUE_INT64))
00100     {
00101       return Variant(vm[OPTION_VALUE_INT64].as<int64_t>());
00102     }
00103     if (vm.count(OPTION_VALUE_FLOAT))
00104     {
00105       return Variant(vm[OPTION_VALUE_FLOAT].as<float>());
00106     }
00107     if (vm.count(OPTION_VALUE_DOUBLE))
00108     {
00109       return Variant(vm[OPTION_VALUE_DOUBLE].as<double>());
00110     }
00111     if (vm.count(OPTION_VALUE_STRING))
00112     {
00113       return Variant(vm[OPTION_VALUE_STRING].as<std::string>());
00114     }
00115     return Variant();
00116   }
00117 
00118 }
00119 
00120 
00121 namespace OpcUa
00122 {
00123 
00124   CommandLine::CommandLine(int argc, char** argv)
00125     : NamespaceIndex(0)
00126     , Attribute(AttributeId::Unknown)
00127     , IsHelp(false)
00128     , IsGetEndpoints(false)
00129     , IsBrowse(false)
00130     , IsRead(false)
00131     , IsWrite(false)
00132     , IsCreateSubscription(false)
00133     , IsFindServers(false)
00134     , IsAddModule(false)
00135     , IsRemoveModule(false)
00136   {
00137     // Declare the supported options.
00138     po::options_description desc("Parameters");
00139     desc.add_options()
00140       (OPTION_HELP, "produce help message")
00141       (OPTION_GET_ENDPOINTS, "List endpoints endpoints.")
00142       (OPTION_BROWSE, "browse command.")
00143       (OPTION_READ, "read command.")
00144       (OPTION_WRITE, "write command.")
00145       (OPTION_CREATE_SUBSCRIPTION, "create subscription command.")
00146       (OPTION_FIND_ServerS, "find servers command.")
00147       (OPTION_REGISTER_MODULE, "Register new module.")
00148       (OPTION_UNREGISTER_MODULE, "Unregister module.")
00149 
00150       (OPTION_Server_URI, po::value<std::string>(), "Uri of the server.")
00151       (OPTION_ATTRIBUTE, po::value<std::string>(), "Name of attribute.")
00152       (OPTION_NODE_Id, po::value<std::string>(), "NodeId in the form 'nsu=uri;srv=1;ns=0;i=84.")
00153       (OPTION_VALUE_BYTE, po::value<uint8_t>(), "Byte value.")
00154       (OPTION_VALUE_SBYTE, po::value<int8_t>(), "Signed byte value.")
00155       (OPTION_VALUE_UINT16, po::value<uint16_t>(), "UInt16 value.")
00156       (OPTION_VALUE_INT16, po::value<int16_t>(), "Int16 value.")
00157       (OPTION_VALUE_UINT32, po::value<uint32_t>(), "UInt32 value.")
00158       (OPTION_VALUE_INT32, po::value<int32_t>(), "Int32 value.")
00159       (OPTION_VALUE_UINT64, po::value<uint64_t>(), "UInt64 value.")
00160       (OPTION_VALUE_INT64, po::value<int64_t>(), "Int64 value.")
00161       (OPTION_VALUE_FLOAT, po::value<float>(), "Float value.")
00162       (OPTION_VALUE_DOUBLE, po::value<double>(), "Double value.")
00163       (OPTION_VALUE_STRING, po::value<std::string>(), "String value.")
00164       (OPTION_MODULE_Id, po::value<std::string>(), "Id of the new module.")
00165       (OPTION_MODULE_PATH, po::value<std::string>(), "Path to the new module shared library.")
00166       (OPTION_CONFIG_DIR, po::value<std::string>(), "Path to the directory with modules configuration files. By default '" CONFIG_PATH "'.");
00167 
00168 
00169     po::variables_map vm;
00170     po::store(po::parse_command_line(argc, argv, desc), vm);
00171     po::notify(vm);
00172 
00173     if (vm.count(OPTION_HELP))
00174     {
00175       IsHelp = true;
00176       desc.print(std::cout);
00177       return;
00178     }
00179 
00180     if (vm.count(OPTION_Server_URI))
00181     {
00182       ServerURI = vm[OPTION_Server_URI].as<std::string>();
00183     }
00184 
00185 
00186     if (vm.count(OPTION_NODE_Id))
00187     {
00188       Node = GetNodeIdOptionValue(vm);
00189     }
00190 
00191     if (vm.count(OPTION_ATTRIBUTE))
00192     {
00193       Attribute = GetAttributeIdOptionValue(vm);
00194     }
00195     Value = GetOptionValue(vm);
00196     IsGetEndpoints = vm.count(OPTION_GET_ENDPOINTS) != 0;
00197     IsBrowse = vm.count(OPTION_BROWSE) != 0;
00198     IsRead = vm.count(OPTION_READ) != 0;
00199     IsWrite = vm.count(OPTION_WRITE) != 0;
00200     IsCreateSubscription = vm.count(OPTION_CREATE_SUBSCRIPTION) != 0;
00201     IsFindServers = vm.count(OPTION_FIND_ServerS) != 0;
00202     if (vm.count(OPTION_REGISTER_MODULE))
00203     {
00204       IsAddModule = true;
00205       ModulePath = vm[OPTION_MODULE_PATH].as<std::string>();
00206       ModuleId = vm[OPTION_MODULE_Id].as<std::string>();
00207     }
00208 
00209     if (vm.count(OPTION_UNREGISTER_MODULE))
00210     {
00211       IsRemoveModule = true;
00212       ModuleId = vm[OPTION_MODULE_Id].as<std::string>();
00213     }
00214 
00215     if (vm.count(OPTION_CONFIG_DIR))
00216     {
00217       ConfigDir = vm[OPTION_CONFIG_DIR].as<std::string>();
00218     }
00219     else
00220     {
00221       ConfigDir = CONFIG_PATH;
00222     }
00223   }
00224 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:56